org.el 559 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131
  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. (defgroup org-startup nil
  167. "Options concerning startup of Org-mode."
  168. :tag "Org Startup"
  169. :group 'org)
  170. (defcustom org-startup-folded t
  171. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  172. This can also be configured on a per-file basis by adding one of
  173. the following lines anywhere in the buffer:
  174. #+STARTUP: fold
  175. #+STARTUP: nofold
  176. #+STARTUP: content"
  177. :group 'org-startup
  178. :type '(choice
  179. (const :tag "nofold: show all" nil)
  180. (const :tag "fold: overview" t)
  181. (const :tag "content: all headlines" content)))
  182. (defcustom org-startup-truncated t
  183. "Non-nil means, entering Org-mode will set `truncate-lines'.
  184. This is useful since some lines containing links can be very long and
  185. uninteresting. Also tables look terrible when wrapped."
  186. :group 'org-startup
  187. :type 'boolean)
  188. (defcustom org-startup-align-all-tables nil
  189. "Non-nil means, align all tables when visiting a file.
  190. This is useful when the column width in tables is forced with <N> cookies
  191. in table fields. Such tables will look correct only after the first re-align.
  192. This can also be configured on a per-file basis by adding one of
  193. the following lines anywhere in the buffer:
  194. #+STARTUP: align
  195. #+STARTUP: noalign"
  196. :group 'org-startup
  197. :type 'boolean)
  198. (defcustom org-insert-mode-line-in-empty-file nil
  199. "Non-nil means insert the first line setting Org-mode in empty files.
  200. When the function `org-mode' is called interactively in an empty file, this
  201. normally means that the file name does not automatically trigger Org-mode.
  202. To ensure that the file will always be in Org-mode in the future, a
  203. line enforcing Org-mode will be inserted into the buffer, if this option
  204. has been set."
  205. :group 'org-startup
  206. :type 'boolean)
  207. (defcustom org-replace-disputed-keys nil
  208. "Non-nil means use alternative key bindings for some keys.
  209. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  210. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  211. If you want to use Org-mode together with one of these other modes,
  212. or more generally if you would like to move some Org-mode commands to
  213. other keys, set this variable and configure the keys with the variable
  214. `org-disputed-keys'.
  215. This option is only relevant at load-time of Org-mode, and must be set
  216. *before* org.el is loaded. Changing it requires a restart of Emacs to
  217. become effective."
  218. :group 'org-startup
  219. :type 'boolean)
  220. (if (fboundp 'defvaralias)
  221. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  222. (defcustom org-disputed-keys
  223. '(([(shift up)] . [(meta p)])
  224. ([(shift down)] . [(meta n)])
  225. ([(shift left)] . [(meta -)])
  226. ([(shift right)] . [(meta +)])
  227. ([(control shift right)] . [(meta shift +)])
  228. ([(control shift left)] . [(meta shift -)]))
  229. "Keys for which Org-mode and other modes compete.
  230. This is an alist, cars are the default keys, second element specifies
  231. the alternative to use when `org-replace-disputed-keys' is t.
  232. Keys can be specified in any syntax supported by `define-key'.
  233. The value of this option takes effect only at Org-mode's startup,
  234. therefore you'll have to restart Emacs to apply it after changing."
  235. :group 'org-startup
  236. :type 'alist)
  237. (defun org-key (key)
  238. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  239. Or return the original if not disputed."
  240. (if org-replace-disputed-keys
  241. (let* ((nkey (key-description key))
  242. (x (org-find-if (lambda (x)
  243. (equal (key-description (car x)) nkey))
  244. org-disputed-keys)))
  245. (if x (cdr x) key))
  246. key))
  247. (defun org-find-if (predicate seq)
  248. (catch 'exit
  249. (while seq
  250. (if (funcall predicate (car seq))
  251. (throw 'exit (car seq))
  252. (pop seq)))))
  253. (defun org-defkey (keymap key def)
  254. "Define a key, possibly translated, as returned by `org-key'."
  255. (define-key keymap (org-key key) def))
  256. (defcustom org-ellipsis nil
  257. "The ellipsis to use in the Org-mode outline.
  258. When nil, just use the standard three dots. When a string, use that instead,
  259. When a face, use the standart 3 dots, but with the specified face.
  260. The change affects only Org-mode (which will then use its own display table).
  261. Changing this requires executing `M-x org-mode' in a buffer to become
  262. effective."
  263. :group 'org-startup
  264. :type '(choice (const :tag "Default" nil)
  265. (face :tag "Face" :value org-warning)
  266. (string :tag "String" :value "...#")))
  267. (defvar org-display-table nil
  268. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  269. (defgroup org-keywords nil
  270. "Keywords in Org-mode."
  271. :tag "Org Keywords"
  272. :group 'org)
  273. (defcustom org-deadline-string "DEADLINE:"
  274. "String to mark deadline entries.
  275. A deadline is this string, followed by a time stamp. Should be a word,
  276. terminated by a colon. You can insert a schedule keyword and
  277. a timestamp with \\[org-deadline].
  278. Changes become only effective after restarting Emacs."
  279. :group 'org-keywords
  280. :type 'string)
  281. (defcustom org-scheduled-string "SCHEDULED:"
  282. "String to mark scheduled TODO entries.
  283. A schedule is this string, followed by a time stamp. Should be a word,
  284. terminated by a colon. You can insert a schedule keyword and
  285. a timestamp with \\[org-schedule].
  286. Changes become only effective after restarting Emacs."
  287. :group 'org-keywords
  288. :type 'string)
  289. (defcustom org-closed-string "CLOSED:"
  290. "String used as the prefix for timestamps logging closing a TODO entry."
  291. :group 'org-keywords
  292. :type 'string)
  293. (defcustom org-clock-string "CLOCK:"
  294. "String used as prefix for timestamps clocking work hours on an item."
  295. :group 'org-keywords
  296. :type 'string)
  297. (defcustom org-comment-string "COMMENT"
  298. "Entries starting with this keyword will never be exported.
  299. An entry can be toggled between COMMENT and normal with
  300. \\[org-toggle-comment].
  301. Changes become only effective after restarting Emacs."
  302. :group 'org-keywords
  303. :type 'string)
  304. (defcustom org-quote-string "QUOTE"
  305. "Entries starting with this keyword will be exported in fixed-width font.
  306. Quoting applies only to the text in the entry following the headline, and does
  307. not extend beyond the next headline, even if that is lower level.
  308. An entry can be toggled between QUOTE and normal with
  309. \\[org-toggle-fixed-width-section]."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defconst org-repeat-re
  313. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  314. "Regular expression for specifying repeated events.
  315. After a match, group 1 contains the repeat expression.")
  316. (defgroup org-structure nil
  317. "Options concerning the general structure of Org-mode files."
  318. :tag "Org Structure"
  319. :group 'org)
  320. (defgroup org-reveal-location nil
  321. "Options about how to make context of a location visible."
  322. :tag "Org Reveal Location"
  323. :group 'org-structure)
  324. (defconst org-context-choice
  325. '(choice
  326. (const :tag "Always" t)
  327. (const :tag "Never" nil)
  328. (repeat :greedy t :tag "Individual contexts"
  329. (cons
  330. (choice :tag "Context"
  331. (const agenda)
  332. (const org-goto)
  333. (const occur-tree)
  334. (const tags-tree)
  335. (const link-search)
  336. (const mark-goto)
  337. (const bookmark-jump)
  338. (const isearch)
  339. (const default))
  340. (boolean))))
  341. "Contexts for the reveal options.")
  342. (defcustom org-show-hierarchy-above '((default . t))
  343. "Non-nil means, show full hierarchy when revealing a location.
  344. Org-mode often shows locations in an org-mode file which might have
  345. been invisible before. When this is set, the hierarchy of headings
  346. above the exposed location is shown.
  347. Turning this off for example for sparse trees makes them very compact.
  348. Instead of t, this can also be an alist specifying this option for different
  349. contexts. Valid contexts are
  350. agenda when exposing an entry from the agenda
  351. org-goto when using the command `org-goto' on key C-c C-j
  352. occur-tree when using the command `org-occur' on key C-c /
  353. tags-tree when constructing a sparse tree based on tags matches
  354. link-search when exposing search matches associated with a link
  355. mark-goto when exposing the jump goal of a mark
  356. bookmark-jump when exposing a bookmark location
  357. isearch when exiting from an incremental search
  358. default default for all contexts not set explicitly"
  359. :group 'org-reveal-location
  360. :type org-context-choice)
  361. (defcustom org-show-following-heading '((default . nil))
  362. "Non-nil means, show following heading when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the heading following the
  365. match is shown.
  366. Turning this off for example for sparse trees makes them very compact,
  367. but makes it harder to edit the location of the match. In such a case,
  368. use the command \\[org-reveal] to show more context.
  369. Instead of t, this can also be an alist specifying this option for different
  370. contexts. See `org-show-hierarchy-above' for valid contexts."
  371. :group 'org-reveal-location
  372. :type org-context-choice)
  373. (defcustom org-show-siblings '((default . nil) (isearch t))
  374. "Non-nil means, show all sibling heading when revealing a location.
  375. Org-mode often shows locations in an org-mode file which might have
  376. been invisible before. When this is set, the sibling of the current entry
  377. heading are all made visible. If `org-show-hierarchy-above' is t,
  378. the same happens on each level of the hierarchy above the current entry.
  379. By default this is on for the isearch context, off for all other contexts.
  380. Turning this off for example for sparse trees makes them very compact,
  381. but makes it harder to edit the location of the match. In such a case,
  382. use the command \\[org-reveal] to show more context.
  383. Instead of t, this can also be an alist specifying this option for different
  384. contexts. See `org-show-hierarchy-above' for valid contexts."
  385. :group 'org-reveal-location
  386. :type org-context-choice)
  387. (defcustom org-show-entry-below '((default . nil))
  388. "Non-nil means, show the entry below a headline when revealing a location.
  389. Org-mode often shows locations in an org-mode file which might have
  390. been invisible before. When this is set, the text below the headline that is
  391. exposed is also shown.
  392. By default this is off for all contexts.
  393. Instead of t, this can also be an alist specifying this option for different
  394. contexts. See `org-show-hierarchy-above' for valid contexts."
  395. :group 'org-reveal-location
  396. :type org-context-choice)
  397. (defcustom org-indirect-buffer-display 'other-window
  398. "How should indirect tree buffers be displayed?
  399. This applies to indirect buffers created with the commands
  400. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  401. Valid values are:
  402. current-window Display in the current window
  403. other-window Just display in another window.
  404. dedicated-frame Create one new frame, and re-use it each time.
  405. new-frame Make a new frame each time. Note that in this case
  406. previously-made indirect buffers are kept, and you need to
  407. kill these buffers yourself."
  408. :group 'org-structure
  409. :group 'org-agenda-windows
  410. :type '(choice
  411. (const :tag "In current window" current-window)
  412. (const :tag "In current frame, other window" other-window)
  413. (const :tag "Each time a new frame" new-frame)
  414. (const :tag "One dedicated frame" dedicated-frame)))
  415. (defgroup org-cycle nil
  416. "Options concerning visibility cycling in Org-mode."
  417. :tag "Org Cycle"
  418. :group 'org-structure)
  419. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  420. "Names of drawers. Drawers are not opened by cycling on the headline above.
  421. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  422. this:
  423. :DRAWERNAME:
  424. .....
  425. :END:
  426. The drawer \"PROPERTIES\" is special for capturing properties through
  427. the property API.
  428. Drawers can be defined on the per-file basis with a line like:
  429. #+DRAWERS: HIDDEN STATE PROPERTIES"
  430. :group 'org-structure
  431. :type '(repeat (string :tag "Drawer Name")))
  432. (defcustom org-cycle-global-at-bob nil
  433. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  434. This makes it possible to do global cycling without having to use S-TAB or
  435. C-u TAB. For this special case to work, the first line of the buffer
  436. must not be a headline - it may be empty ot some other text. When used in
  437. this way, `org-cycle-hook' is disables temporarily, to make sure the
  438. cursor stays at the beginning of the buffer.
  439. When this option is nil, don't do anything special at the beginning
  440. of the buffer."
  441. :group 'org-cycle
  442. :type 'boolean)
  443. (defcustom org-cycle-emulate-tab t
  444. "Where should `org-cycle' emulate TAB.
  445. nil Never
  446. white Only in completely white lines
  447. whitestart Only at the beginning of lines, before the first non-white char
  448. t Everywhere except in headlines
  449. exc-hl-bol Everywhere except at the start of a headline
  450. If TAB is used in a place where it does not emulate TAB, the current subtree
  451. visibility is cycled."
  452. :group 'org-cycle
  453. :type '(choice (const :tag "Never" nil)
  454. (const :tag "Only in completely white lines" white)
  455. (const :tag "Before first char in a line" whitestart)
  456. (const :tag "Everywhere except in headlines" t)
  457. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  458. ))
  459. (defcustom org-cycle-separator-lines 2
  460. "Number of empty lines needed to keep an empty line between collapsed trees.
  461. If you leave an empty line between the end of a subtree and the following
  462. headline, this empty line is hidden when the subtree is folded.
  463. Org-mode will leave (exactly) one empty line visible if the number of
  464. empty lines is equal or larger to the number given in this variable.
  465. So the default 2 means, at least 2 empty lines after the end of a subtree
  466. are needed to produce free space between a collapsed subtree and the
  467. following headline.
  468. Special case: when 0, never leave empty lines in collapsed view."
  469. :group 'org-cycle
  470. :type 'integer)
  471. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  472. org-cycle-hide-drawers
  473. org-cycle-show-empty-lines
  474. org-optimize-window-after-visibility-change)
  475. "Hook that is run after `org-cycle' has changed the buffer visibility.
  476. The function(s) in this hook must accept a single argument which indicates
  477. the new state that was set by the most recent `org-cycle' command. The
  478. argument is a symbol. After a global state change, it can have the values
  479. `overview', `content', or `all'. After a local state change, it can have
  480. the values `folded', `children', or `subtree'."
  481. :group 'org-cycle
  482. :type 'hook)
  483. (defgroup org-edit-structure nil
  484. "Options concerning structure editing in Org-mode."
  485. :tag "Org Edit Structure"
  486. :group 'org-structure)
  487. (defcustom org-odd-levels-only nil
  488. "Non-nil means, skip even levels and only use odd levels for the outline.
  489. This has the effect that two stars are being added/taken away in
  490. promotion/demotion commands. It also influences how levels are
  491. handled by the exporters.
  492. Changing it requires restart of `font-lock-mode' to become effective
  493. for fontification also in regions already fontified.
  494. You may also set this on a per-file basis by adding one of the following
  495. lines to the buffer:
  496. #+STARTUP: odd
  497. #+STARTUP: oddeven"
  498. :group 'org-edit-structure
  499. :group 'org-font-lock
  500. :type 'boolean)
  501. (defcustom org-adapt-indentation t
  502. "Non-nil means, adapt indentation when promoting and demoting.
  503. When this is set and the *entire* text in an entry is indented, the
  504. indentation is increased by one space in a demotion command, and
  505. decreased by one in a promotion command. If any line in the entry
  506. body starts at column 0, indentation is not changed at all."
  507. :group 'org-edit-structure
  508. :type 'boolean)
  509. (defcustom org-special-ctrl-a/e nil
  510. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  511. When t, `C-a' will bring back the cursor to the beginning of the
  512. headline text, i.e. after the stars and after a possible TODO keyword.
  513. In an item, this will be the position after the bullet.
  514. When the cursor is already at that position, another `C-a' will bring
  515. it to the beginning of the line.
  516. `C-e' will jump to the end of the headline, ignoring the presence of tags
  517. in the headline. A second `C-e' will then jump to the true end of the
  518. line, after any tags.
  519. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  520. and only a directly following, identical keypress will bring the cursor
  521. to the special positions."
  522. :group 'org-edit-structure
  523. :type '(choice
  524. (const :tag "off" nil)
  525. (const :tag "after bullet first" t)
  526. (const :tag "border first" reversed)))
  527. (if (fboundp 'defvaralias)
  528. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  529. (defcustom org-special-ctrl-k nil
  530. "Non-nil means `C-k' will behave specially in headlines.
  531. When nil, `C-k' will call the default `kill-line' command.
  532. When t, the following will happen while the cursor is in the headline:
  533. - When the cursor is at the beginning of a headline, kill the entire
  534. line and possible the folded subtree below the line.
  535. - When in the middle of the headline text, kill the headline up to the tags.
  536. - When after the headline text, kill the tags."
  537. :group 'org-edit-structure
  538. :type 'boolean)
  539. (defcustom org-M-RET-may-split-line '((default . t))
  540. "Non-nil means, M-RET will split the line at the cursor position.
  541. When nil, it will go to the end of the line before making a
  542. new line.
  543. You may also set this option in a different way for different
  544. contexts. Valid contexts are:
  545. headline when creating a new headline
  546. item when creating a new item
  547. table in a table field
  548. default the value to be used for all contexts not explicitly
  549. customized"
  550. :group 'org-structure
  551. :group 'org-table
  552. :type '(choice
  553. (const :tag "Always" t)
  554. (const :tag "Never" nil)
  555. (repeat :greedy t :tag "Individual contexts"
  556. (cons
  557. (choice :tag "Context"
  558. (const headline)
  559. (const item)
  560. (const table)
  561. (const default))
  562. (boolean)))))
  563. (defcustom org-blank-before-new-entry '((heading . nil)
  564. (plain-list-item . nil))
  565. "Should `org-insert-heading' leave a blank line before new heading/item?
  566. The value is an alist, with `heading' and `plain-list-item' as car,
  567. and a boolean flag as cdr."
  568. :group 'org-edit-structure
  569. :type '(list
  570. (cons (const heading) (boolean))
  571. (cons (const plain-list-item) (boolean))))
  572. (defcustom org-insert-heading-hook nil
  573. "Hook being run after inserting a new heading."
  574. :group 'org-edit-structure
  575. :type 'hook)
  576. (defcustom org-enable-fixed-width-editor t
  577. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  578. This currently only means, they are never auto-wrapped.
  579. When nil, such lines will be treated like ordinary lines.
  580. See also the QUOTE keyword."
  581. :group 'org-edit-structure
  582. :type 'boolean)
  583. (defcustom org-goto-auto-isearch t
  584. "Non-nil means, typing characters in org-goto starts incremental search."
  585. :group 'org-edit-structure
  586. :type 'boolean)
  587. (defgroup org-sparse-trees nil
  588. "Options concerning sparse trees in Org-mode."
  589. :tag "Org Sparse Trees"
  590. :group 'org-structure)
  591. (defcustom org-highlight-sparse-tree-matches t
  592. "Non-nil means, highlight all matches that define a sparse tree.
  593. The highlights will automatically disappear the next time the buffer is
  594. changed by an edit command."
  595. :group 'org-sparse-trees
  596. :type 'boolean)
  597. (defcustom org-remove-highlights-with-change t
  598. "Non-nil means, any change to the buffer will remove temporary highlights.
  599. Such highlights are created by `org-occur' and `org-clock-display'.
  600. When nil, `C-c C-c needs to be used to get rid of the highlights.
  601. The highlights created by `org-preview-latex-fragment' always need
  602. `C-c C-c' to be removed."
  603. :group 'org-sparse-trees
  604. :group 'org-time
  605. :type 'boolean)
  606. (defcustom org-occur-hook '(org-first-headline-recenter)
  607. "Hook that is run after `org-occur' has constructed a sparse tree.
  608. This can be used to recenter the window to show as much of the structure
  609. as possible."
  610. :group 'org-sparse-trees
  611. :type 'hook)
  612. (defgroup org-plain-lists nil
  613. "Options concerning plain lists in Org-mode."
  614. :tag "Org Plain lists"
  615. :group 'org-structure)
  616. (defcustom org-cycle-include-plain-lists nil
  617. "Non-nil means, include plain lists into visibility cycling.
  618. This means that during cycling, plain list items will *temporarily* be
  619. interpreted as outline headlines with a level given by 1000+i where i is the
  620. indentation of the bullet. In all other operations, plain list items are
  621. not seen as headlines. For example, you cannot assign a TODO keyword to
  622. such an item."
  623. :group 'org-plain-lists
  624. :type 'boolean)
  625. (defcustom org-plain-list-ordered-item-terminator t
  626. "The character that makes a line with leading number an ordered list item.
  627. Valid values are ?. and ?\). To get both terminators, use t. While
  628. ?. may look nicer, it creates the danger that a line with leading
  629. number may be incorrectly interpreted as an item. ?\) therefore is
  630. the safe choice."
  631. :group 'org-plain-lists
  632. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  633. (const :tag "paren like in \"2)\"" ?\))
  634. (const :tab "both" t)))
  635. (defcustom org-auto-renumber-ordered-lists t
  636. "Non-nil means, automatically renumber ordered plain lists.
  637. Renumbering happens when the sequence have been changed with
  638. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  639. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  640. :group 'org-plain-lists
  641. :type 'boolean)
  642. (defcustom org-provide-checkbox-statistics t
  643. "Non-nil means, update checkbox statistics after insert and toggle.
  644. When this is set, checkbox statistics is updated each time you either insert
  645. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  646. with \\[org-ctrl-c-ctrl-c\\]."
  647. :group 'org-plain-lists
  648. :type 'boolean)
  649. (defgroup org-archive nil
  650. "Options concerning archiving in Org-mode."
  651. :tag "Org Archive"
  652. :group 'org-structure)
  653. (defcustom org-archive-tag "ARCHIVE"
  654. "The tag that marks a subtree as archived.
  655. An archived subtree does not open during visibility cycling, and does
  656. not contribute to the agenda listings.
  657. After changing this, font-lock must be restarted in the relevant buffers to
  658. get the proper fontification."
  659. :group 'org-archive
  660. :group 'org-keywords
  661. :type 'string)
  662. (defcustom org-agenda-skip-archived-trees t
  663. "Non-nil means, the agenda will skip any items located in archived trees.
  664. An archived tree is a tree marked with the tag ARCHIVE."
  665. :group 'org-archive
  666. :group 'org-agenda-skip
  667. :type 'boolean)
  668. (defcustom org-cycle-open-archived-trees nil
  669. "Non-nil means, `org-cycle' will open archived trees.
  670. An archived tree is a tree marked with the tag ARCHIVE.
  671. When nil, archived trees will stay folded. You can still open them with
  672. normal outline commands like `show-all', but not with the cycling commands."
  673. :group 'org-archive
  674. :group 'org-cycle
  675. :type 'boolean)
  676. (defcustom org-sparse-tree-open-archived-trees nil
  677. "Non-nil means sparse tree construction shows matches in archived trees.
  678. When nil, matches in these trees are highlighted, but the trees are kept in
  679. collapsed state."
  680. :group 'org-archive
  681. :group 'org-sparse-trees
  682. :type 'boolean)
  683. (defcustom org-archive-location "%s_archive::"
  684. "The location where subtrees should be archived.
  685. This string consists of two parts, separated by a double-colon.
  686. The first part is a file name - when omitted, archiving happens in the same
  687. file. %s will be replaced by the current file name (without directory part).
  688. Archiving to a different file is useful to keep archived entries from
  689. contributing to the Org-mode Agenda.
  690. The part after the double colon is a headline. The archived entries will be
  691. filed under that headline. When omitted, the subtrees are simply filed away
  692. at the end of the file, as top-level entries.
  693. Here are a few examples:
  694. \"%s_archive::\"
  695. If the current file is Projects.org, archive in file
  696. Projects.org_archive, as top-level trees. This is the default.
  697. \"::* Archived Tasks\"
  698. Archive in the current file, under the top-level headline
  699. \"* Archived Tasks\".
  700. \"~/org/archive.org::\"
  701. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  702. \"basement::** Finished Tasks\"
  703. Archive in file ./basement (relative path), as level 3 trees
  704. below the level 2 heading \"** Finished Tasks\".
  705. You may set this option on a per-file basis by adding to the buffer a
  706. line like
  707. #+ARCHIVE: basement::** Finished Tasks"
  708. :group 'org-archive
  709. :type 'string)
  710. (defcustom org-archive-mark-done t
  711. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  712. This can be a string to set the keyword to use. When t, Org-mode will
  713. use the first keyword in its list that means done."
  714. :group 'org-archive
  715. :type '(choice
  716. (const :tag "No" nil)
  717. (const :tag "Yes" t)
  718. (string :tag "Use this keyword")))
  719. (defcustom org-archive-stamp-time t
  720. "Non-nil means, add a time stamp to entries moved to an archive file.
  721. This variable is obsolete and has no effect anymore, instead add ot remove
  722. `time' from the variablle `org-archive-save-context-info'."
  723. :group 'org-archive
  724. :type 'boolean)
  725. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  726. "Parts of context info that should be stored as properties when archiving.
  727. When a subtree is moved to an archive file, it looses information given by
  728. context, like inherited tags, the category, and possibly also the TODO
  729. state (depending on the variable `org-archive-mark-done').
  730. This variable can be a list of any of the following symbols:
  731. time The time of archiving.
  732. file The file where the entry originates.
  733. itags The local tags, in the headline of the subtree.
  734. ltags The tags the subtree inherits from further up the hierarchy.
  735. todo The pre-archive TODO state.
  736. category The category, taken from file name or #+CATEGORY lines.
  737. olpath The outline path to the item. These are all headlines above
  738. the current item, separated by /, like a file path.
  739. For each symbol present in the list, a property will be created in
  740. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  741. information."
  742. :group 'org-archive
  743. :type '(set :greedy t
  744. (const :tag "Time" time)
  745. (const :tag "File" file)
  746. (const :tag "Category" category)
  747. (const :tag "TODO state" todo)
  748. (const :tag "TODO state" priority)
  749. (const :tag "Inherited tags" itags)
  750. (const :tag "Outline path" olpath)
  751. (const :tag "Local tags" ltags)))
  752. (defgroup org-imenu-and-speedbar nil
  753. "Options concerning imenu and speedbar in Org-mode."
  754. :tag "Org Imenu and Speedbar"
  755. :group 'org-structure)
  756. (defcustom org-imenu-depth 2
  757. "The maximum level for Imenu access to Org-mode headlines.
  758. This also applied for speedbar access."
  759. :group 'org-imenu-and-speedbar
  760. :type 'number)
  761. (defgroup org-table nil
  762. "Options concerning tables in Org-mode."
  763. :tag "Org Table"
  764. :group 'org)
  765. (defcustom org-enable-table-editor 'optimized
  766. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  767. When nil, such lines will be treated like ordinary lines.
  768. When equal to the symbol `optimized', the table editor will be optimized to
  769. do the following:
  770. - Automatic overwrite mode in front of whitespace in table fields.
  771. This makes the structure of the table stay in tact as long as the edited
  772. field does not exceed the column width.
  773. - Minimize the number of realigns. Normally, the table is aligned each time
  774. TAB or RET are pressed to move to another field. With optimization this
  775. happens only if changes to a field might have changed the column width.
  776. Optimization requires replacing the functions `self-insert-command',
  777. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  778. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  779. very good at guessing when a re-align will be necessary, but you can always
  780. force one with \\[org-ctrl-c-ctrl-c].
  781. If you would like to use the optimized version in Org-mode, but the
  782. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  783. This variable can be used to turn on and off the table editor during a session,
  784. but in order to toggle optimization, a restart is required.
  785. See also the variable `org-table-auto-blank-field'."
  786. :group 'org-table
  787. :type '(choice
  788. (const :tag "off" nil)
  789. (const :tag "on" t)
  790. (const :tag "on, optimized" optimized)))
  791. (defcustom org-table-tab-recognizes-table.el t
  792. "Non-nil means, TAB will automatically notice a table.el table.
  793. When it sees such a table, it moves point into it and - if necessary -
  794. calls `table-recognize-table'."
  795. :group 'org-table-editing
  796. :type 'boolean)
  797. (defgroup org-link nil
  798. "Options concerning links in Org-mode."
  799. :tag "Org Link"
  800. :group 'org)
  801. (defvar org-link-abbrev-alist-local nil
  802. "Buffer-local version of `org-link-abbrev-alist', which see.
  803. The value of this is taken from the #+LINK lines.")
  804. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  805. (defcustom org-link-abbrev-alist nil
  806. "Alist of link abbreviations.
  807. The car of each element is a string, to be replaced at the start of a link.
  808. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  809. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  810. [[linkkey:tag][description]]
  811. If REPLACE is a string, the tag will simply be appended to create the link.
  812. If the string contains \"%s\", the tag will be inserted there.
  813. REPLACE may also be a function that will be called with the tag as the
  814. only argument to create the link, which should be returned as a string.
  815. See the manual for examples."
  816. :group 'org-link
  817. :type 'alist)
  818. (defcustom org-descriptive-links t
  819. "Non-nil means, hide link part and only show description of bracket links.
  820. Bracket links are like [[link][descritpion]]. This variable sets the initial
  821. state in new org-mode buffers. The setting can then be toggled on a
  822. per-buffer basis from the Org->Hyperlinks menu."
  823. :group 'org-link
  824. :type 'boolean)
  825. (defcustom org-link-file-path-type 'adaptive
  826. "How the path name in file links should be stored.
  827. Valid values are:
  828. relative Relative to the current directory, i.e. the directory of the file
  829. into which the link is being inserted.
  830. absolute Absolute path, if possible with ~ for home directory.
  831. noabbrev Absolute path, no abbreviation of home directory.
  832. adaptive Use relative path for files in the current directory and sub-
  833. directories of it. For other files, use an absolute path."
  834. :group 'org-link
  835. :type '(choice
  836. (const relative)
  837. (const absolute)
  838. (const noabbrev)
  839. (const adaptive)))
  840. (defcustom org-activate-links '(bracket angle plain radio tag date)
  841. "Types of links that should be activated in Org-mode files.
  842. This is a list of symbols, each leading to the activation of a certain link
  843. type. In principle, it does not hurt to turn on most link types - there may
  844. be a small gain when turning off unused link types. The types are:
  845. bracket The recommended [[link][description]] or [[link]] links with hiding.
  846. angular Links in angular brackes that may contain whitespace like
  847. <bbdb:Carsten Dominik>.
  848. plain Plain links in normal text, no whitespace, like http://google.com.
  849. radio Text that is matched by a radio target, see manual for details.
  850. tag Tag settings in a headline (link to tag search).
  851. date Time stamps (link to calendar).
  852. Changing this variable requires a restart of Emacs to become effective."
  853. :group 'org-link
  854. :type '(set (const :tag "Double bracket links (new style)" bracket)
  855. (const :tag "Angular bracket links (old style)" angular)
  856. (const :tag "Plain text links" plain)
  857. (const :tag "Radio target matches" radio)
  858. (const :tag "Tags" tag)
  859. (const :tag "Timestamps" date)))
  860. (defgroup org-link-store nil
  861. "Options concerning storing links in Org-mode."
  862. :tag "Org Store Link"
  863. :group 'org-link)
  864. (defcustom org-email-link-description-format "Email %c: %.30s"
  865. "Format of the description part of a link to an email or usenet message.
  866. The following %-excapes will be replaced by corresponding information:
  867. %F full \"From\" field
  868. %f name, taken from \"From\" field, address if no name
  869. %T full \"To\" field
  870. %t first name in \"To\" field, address if no name
  871. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  872. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  873. %s subject
  874. %m message-id.
  875. You may use normal field width specification between the % and the letter.
  876. This is for example useful to limit the length of the subject.
  877. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  878. :group 'org-link-store
  879. :type 'string)
  880. (defcustom org-from-is-user-regexp
  881. (let (r1 r2)
  882. (when (and user-mail-address (not (string= user-mail-address "")))
  883. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  884. (when (and user-full-name (not (string= user-full-name "")))
  885. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  886. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  887. "Regexp mached against the \"From:\" header of an email or usenet message.
  888. It should match if the message is from the user him/herself."
  889. :group 'org-link-store
  890. :type 'regexp)
  891. (defcustom org-context-in-file-links t
  892. "Non-nil means, file links from `org-store-link' contain context.
  893. A search string will be added to the file name with :: as separator and
  894. used to find the context when the link is activated by the command
  895. `org-open-at-point'.
  896. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  897. negates this setting for the duration of the command."
  898. :group 'org-link-store
  899. :type 'boolean)
  900. (defcustom org-keep-stored-link-after-insertion nil
  901. "Non-nil means, keep link in list for entire session.
  902. The command `org-store-link' adds a link pointing to the current
  903. location to an internal list. These links accumulate during a session.
  904. The command `org-insert-link' can be used to insert links into any
  905. Org-mode file (offering completion for all stored links). When this
  906. option is nil, every link which has been inserted once using \\[org-insert-link]
  907. will be removed from the list, to make completing the unused links
  908. more efficient."
  909. :group 'org-link-store
  910. :type 'boolean)
  911. (defgroup org-link-follow nil
  912. "Options concerning following links in Org-mode."
  913. :tag "Org Follow Link"
  914. :group 'org-link)
  915. (defcustom org-follow-link-hook nil
  916. "Hook that is run after a link has been followed."
  917. :group 'org-link-follow
  918. :type 'hook)
  919. (defcustom org-tab-follows-link nil
  920. "Non-nil means, on links TAB will follow the link.
  921. Needs to be set before org.el is loaded."
  922. :group 'org-link-follow
  923. :type 'boolean)
  924. (defcustom org-return-follows-link nil
  925. "Non-nil means, on links RET will follow the link.
  926. Needs to be set before org.el is loaded."
  927. :group 'org-link-follow
  928. :type 'boolean)
  929. (defcustom org-mouse-1-follows-link
  930. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  931. "Non-nil means, mouse-1 on a link will follow the link.
  932. A longer mouse click will still set point. Does not work on XEmacs.
  933. Needs to be set before org.el is loaded."
  934. :group 'org-link-follow
  935. :type 'boolean)
  936. (defcustom org-mark-ring-length 4
  937. "Number of different positions to be recorded in the ring
  938. Changing this requires a restart of Emacs to work correctly."
  939. :group 'org-link-follow
  940. :type 'interger)
  941. (defcustom org-link-frame-setup
  942. '((vm . vm-visit-folder-other-frame)
  943. (gnus . gnus-other-frame)
  944. (file . find-file-other-window))
  945. "Setup the frame configuration for following links.
  946. When following a link with Emacs, it may often be useful to display
  947. this link in another window or frame. This variable can be used to
  948. set this up for the different types of links.
  949. For VM, use any of
  950. `vm-visit-folder'
  951. `vm-visit-folder-other-frame'
  952. For Gnus, use any of
  953. `gnus'
  954. `gnus-other-frame'
  955. For FILE, use any of
  956. `find-file'
  957. `find-file-other-window'
  958. `find-file-other-frame'
  959. For the calendar, use the variable `calendar-setup'.
  960. For BBDB, it is currently only possible to display the matches in
  961. another window."
  962. :group 'org-link-follow
  963. :type '(list
  964. (cons (const vm)
  965. (choice
  966. (const vm-visit-folder)
  967. (const vm-visit-folder-other-window)
  968. (const vm-visit-folder-other-frame)))
  969. (cons (const gnus)
  970. (choice
  971. (const gnus)
  972. (const gnus-other-frame)))
  973. (cons (const file)
  974. (choice
  975. (const find-file)
  976. (const find-file-other-window)
  977. (const find-file-other-frame)))))
  978. (defcustom org-display-internal-link-with-indirect-buffer nil
  979. "Non-nil means, use indirect buffer to display infile links.
  980. Activating internal links (from one location in a file to another location
  981. in the same file) normally just jumps to the location. When the link is
  982. activated with a C-u prefix (or with mouse-3), the link is displayed in
  983. another window. When this option is set, the other window actually displays
  984. an indirect buffer clone of the current buffer, to avoid any visibility
  985. changes to the current buffer."
  986. :group 'org-link-follow
  987. :type 'boolean)
  988. (defcustom org-open-non-existing-files nil
  989. "Non-nil means, `org-open-file' will open non-existing files.
  990. When nil, an error will be generated."
  991. :group 'org-link-follow
  992. :type 'boolean)
  993. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  994. "Function and arguments to call for following mailto links.
  995. This is a list with the first element being a lisp function, and the
  996. remaining elements being arguments to the function. In string arguments,
  997. %a will be replaced by the address, and %s will be replaced by the subject
  998. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  999. :group 'org-link-follow
  1000. :type '(choice
  1001. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1002. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1003. (const :tag "message-mail" (message-mail "%a" "%s"))
  1004. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1005. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1006. "Non-nil means, ask for confirmation before executing shell links.
  1007. Shell links can be dangerous: just think about a link
  1008. [[shell:rm -rf ~/*][Google Search]]
  1009. This link would show up in your Org-mode document as \"Google Search\",
  1010. but really it would remove your entire home directory.
  1011. Therefore we advise against setting this variable to nil.
  1012. Just change it to `y-or-n-p' of you want to confirm with a
  1013. single keystroke rather than having to type \"yes\"."
  1014. :group 'org-link-follow
  1015. :type '(choice
  1016. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1017. (const :tag "with y-or-n (faster)" y-or-n-p)
  1018. (const :tag "no confirmation (dangerous)" nil)))
  1019. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1020. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1021. Elisp links can be dangerous: just think about a link
  1022. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1023. This link would show up in your Org-mode document as \"Google Search\",
  1024. but really it would remove your entire home directory.
  1025. Therefore we advise against setting this variable to nil.
  1026. Just change it to `y-or-n-p' of you want to confirm with a
  1027. single keystroke rather than having to type \"yes\"."
  1028. :group 'org-link-follow
  1029. :type '(choice
  1030. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1031. (const :tag "with y-or-n (faster)" y-or-n-p)
  1032. (const :tag "no confirmation (dangerous)" nil)))
  1033. (defconst org-file-apps-defaults-gnu
  1034. '((remote . emacs)
  1035. (t . mailcap))
  1036. "Default file applications on a UNIX or GNU/Linux system.
  1037. See `org-file-apps'.")
  1038. (defconst org-file-apps-defaults-macosx
  1039. '((remote . emacs)
  1040. (t . "open %s")
  1041. ("ps" . "gv %s")
  1042. ("ps.gz" . "gv %s")
  1043. ("eps" . "gv %s")
  1044. ("eps.gz" . "gv %s")
  1045. ("dvi" . "xdvi %s")
  1046. ("fig" . "xfig %s"))
  1047. "Default file applications on a MacOS X system.
  1048. The system \"open\" is known as a default, but we use X11 applications
  1049. for some files for which the OS does not have a good default.
  1050. See `org-file-apps'.")
  1051. (defconst org-file-apps-defaults-windowsnt
  1052. (list
  1053. '(remote . emacs)
  1054. (cons t
  1055. (list (if (featurep 'xemacs)
  1056. 'mswindows-shell-execute
  1057. 'w32-shell-execute)
  1058. "open" 'file)))
  1059. "Default file applications on a Windows NT system.
  1060. The system \"open\" is used for most files.
  1061. See `org-file-apps'.")
  1062. (defcustom org-file-apps
  1063. '(
  1064. ("txt" . emacs)
  1065. ("tex" . emacs)
  1066. ("ltx" . emacs)
  1067. ("org" . emacs)
  1068. ("el" . emacs)
  1069. ("bib" . emacs)
  1070. )
  1071. "External applications for opening `file:path' items in a document.
  1072. Org-mode uses system defaults for different file types, but
  1073. you can use this variable to set the application for a given file
  1074. extension. The entries in this list are cons cells where the car identifies
  1075. files and the cdr the corresponding command. Possible values for the
  1076. file identifier are
  1077. \"ext\" A string identifying an extension
  1078. `directory' Matches a directory
  1079. `remote' Matches a remote file, accessible through tramp or efs.
  1080. Remote files most likely should be visited through Emacs
  1081. because external applications cannot handle such paths.
  1082. t Default for all remaining files
  1083. Possible values for the command are:
  1084. `emacs' The file will be visited by the current Emacs process.
  1085. `default' Use the default application for this file type.
  1086. string A command to be executed by a shell; %s will be replaced
  1087. by the path to the file.
  1088. sexp A Lisp form which will be evaluated. The file path will
  1089. be available in the Lisp variable `file'.
  1090. For more examples, see the system specific constants
  1091. `org-file-apps-defaults-macosx'
  1092. `org-file-apps-defaults-windowsnt'
  1093. `org-file-apps-defaults-gnu'."
  1094. :group 'org-link-follow
  1095. :type '(repeat
  1096. (cons (choice :value ""
  1097. (string :tag "Extension")
  1098. (const :tag "Default for unrecognized files" t)
  1099. (const :tag "Remote file" remote)
  1100. (const :tag "Links to a directory" directory))
  1101. (choice :value ""
  1102. (const :tag "Visit with Emacs" emacs)
  1103. (const :tag "Use system default" default)
  1104. (string :tag "Command")
  1105. (sexp :tag "Lisp form")))))
  1106. (defgroup org-refile nil
  1107. "Options concerning refiling entries in Org-mode."
  1108. :tag "Org Remember"
  1109. :group 'org)
  1110. (defcustom org-directory "~/org"
  1111. "Directory with org files.
  1112. This directory will be used as default to prompt for org files.
  1113. Used by the hooks for remember.el."
  1114. :group 'org-refile
  1115. :group 'org-remember
  1116. :type 'directory)
  1117. (defcustom org-default-notes-file "~/.notes"
  1118. "Default target for storing notes.
  1119. Used by the hooks for remember.el. This can be a string, or nil to mean
  1120. the value of `remember-data-file'.
  1121. You can set this on a per-template basis with the variable
  1122. `org-remember-templates'."
  1123. :group 'org-refile
  1124. :group 'org-remember
  1125. :type '(choice
  1126. (const :tag "Default from remember-data-file" nil)
  1127. file))
  1128. (defcustom org-goto-interface 'outline
  1129. "The default interface to be used for `org-goto'.
  1130. Allowed vaues are:
  1131. outline The interface shows an outline of the relevant file
  1132. and the correct heading is found by moving through
  1133. the outline or by searching with incremental search.
  1134. outline-path-completion Headlines in the current buffer are offered via
  1135. completion."
  1136. :group 'org-refile
  1137. :type '(choice
  1138. (const :tag "Outline" outline)
  1139. (const :tag "Outline-path-completion" outline-path-completion)))
  1140. (defcustom org-reverse-note-order nil
  1141. "Non-nil means, store new notes at the beginning of a file or entry.
  1142. When nil, new notes will be filed to the end of a file or entry.
  1143. This can also be a list with cons cells of regular expressions that
  1144. are matched against file names, and values."
  1145. :group 'org-remember
  1146. :type '(choice
  1147. (const :tag "Reverse always" t)
  1148. (const :tag "Reverse never" nil)
  1149. (repeat :tag "By file name regexp"
  1150. (cons regexp boolean))))
  1151. (defcustom org-refile-targets nil
  1152. "Targets for refiling entries with \\[org-refile].
  1153. This is list of cons cells. Each cell contains:
  1154. - a specification of the files to be considered, either a list of files,
  1155. or a symbol whose function or variable value will be used to retrieve
  1156. a file name or a list of file names. Nil means, refile to a different
  1157. heading in the current buffer.
  1158. - A specification of how to find candidate refile targets. This may be
  1159. any of
  1160. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1161. This tag has to be present in all target headlines, inheritance will
  1162. not be considered.
  1163. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1164. todo keyword.
  1165. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1166. headlines that are refiling targets.
  1167. - a cons cell (:level . N). Any headline of level N is considered a target.
  1168. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1169. :group 'org-remember
  1170. :type '(repeat
  1171. (cons
  1172. (choice :value org-agenda-files
  1173. (const :tag "All agenda files" org-agenda-files)
  1174. (const :tag "Current buffer" nil)
  1175. (function) (variable) (file))
  1176. (choice :tag "Identify target headline by"
  1177. (cons :tag "Specific tag" (const :tag) (string))
  1178. (cons :tag "TODO keyword" (const :todo) (string))
  1179. (cons :tag "Regular expression" (const :regexp) (regexp))
  1180. (cons :tag "Level number" (const :level) (integer))
  1181. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1182. (defcustom org-refile-use-outline-path nil
  1183. "Non-nil means, provide refile targets as paths.
  1184. So a level 3 headline will be available as level1/level2/level3.
  1185. When the value is `file', also include the file name (without directory)
  1186. into the path. When `full-file-path', include the full file path."
  1187. :group 'org-remember
  1188. :type '(choice
  1189. (const :tag "Not" nil)
  1190. (const :tag "Yes" t)
  1191. (const :tag "Start with file name" file)
  1192. (const :tag "Start with full file path" full-file-path)))
  1193. (defgroup org-todo nil
  1194. "Options concerning TODO items in Org-mode."
  1195. :tag "Org TODO"
  1196. :group 'org)
  1197. (defgroup org-progress nil
  1198. "Options concerning Progress logging in Org-mode."
  1199. :tag "Org Progress"
  1200. :group 'org-time)
  1201. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1202. "List of TODO entry keyword sequences and their interpretation.
  1203. \\<org-mode-map>This is a list of sequences.
  1204. Each sequence starts with a symbol, either `sequence' or `type',
  1205. indicating if the keywords should be interpreted as a sequence of
  1206. action steps, or as different types of TODO items. The first
  1207. keywords are states requiring action - these states will select a headline
  1208. for inclusion into the global TODO list Org-mode produces. If one of
  1209. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1210. signify that no further action is necessary. If \"|\" is not found,
  1211. the last keyword is treated as the only DONE state of the sequence.
  1212. The command \\[org-todo] cycles an entry through these states, and one
  1213. additional state where no keyword is present. For details about this
  1214. cycling, see the manual.
  1215. TODO keywords and interpretation can also be set on a per-file basis with
  1216. the special #+SEQ_TODO and #+TYP_TODO lines.
  1217. Each keyword can optionally specify a character for fast state selection
  1218. \(in combination with the variable `org-use-fast-todo-selection')
  1219. and specifiers for state change logging, using the same syntax
  1220. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1221. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1222. indicates to record a time stamp each time this state is selected.
  1223. Each keyword may also specify if a timestamp or a note should be
  1224. recorded when entering or leaving the state, by adding additional
  1225. characters in the parenthesis after the keyword. This looks like this:
  1226. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1227. record only the time of the state change. With X and Y being either
  1228. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1229. Y when leaving the state if and only if the *target* state does not
  1230. define X. You may omit any of the fast-selection key or X or /Y,
  1231. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1232. For backward compatibility, this variable may also be just a list
  1233. of keywords - in this case the interptetation (sequence or type) will be
  1234. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1235. :group 'org-todo
  1236. :group 'org-keywords
  1237. :type '(choice
  1238. (repeat :tag "Old syntax, just keywords"
  1239. (string :tag "Keyword"))
  1240. (repeat :tag "New syntax"
  1241. (cons
  1242. (choice
  1243. :tag "Interpretation"
  1244. (const :tag "Sequence (cycling hits every state)" sequence)
  1245. (const :tag "Type (cycling directly to DONE)" type))
  1246. (repeat
  1247. (string :tag "Keyword"))))))
  1248. (defvar org-todo-keywords-1 nil
  1249. "All TODO and DONE keywords active in a buffer.")
  1250. (make-variable-buffer-local 'org-todo-keywords-1)
  1251. (defvar org-todo-keywords-for-agenda nil)
  1252. (defvar org-done-keywords-for-agenda nil)
  1253. (defvar org-not-done-keywords nil)
  1254. (make-variable-buffer-local 'org-not-done-keywords)
  1255. (defvar org-done-keywords nil)
  1256. (make-variable-buffer-local 'org-done-keywords)
  1257. (defvar org-todo-heads nil)
  1258. (make-variable-buffer-local 'org-todo-heads)
  1259. (defvar org-todo-sets nil)
  1260. (make-variable-buffer-local 'org-todo-sets)
  1261. (defvar org-todo-log-states nil)
  1262. (make-variable-buffer-local 'org-todo-log-states)
  1263. (defvar org-todo-kwd-alist nil)
  1264. (make-variable-buffer-local 'org-todo-kwd-alist)
  1265. (defvar org-todo-key-alist nil)
  1266. (make-variable-buffer-local 'org-todo-key-alist)
  1267. (defvar org-todo-key-trigger nil)
  1268. (make-variable-buffer-local 'org-todo-key-trigger)
  1269. (defcustom org-todo-interpretation 'sequence
  1270. "Controls how TODO keywords are interpreted.
  1271. This variable is in principle obsolete and is only used for
  1272. backward compatibility, if the interpretation of todo keywords is
  1273. not given already in `org-todo-keywords'. See that variable for
  1274. more information."
  1275. :group 'org-todo
  1276. :group 'org-keywords
  1277. :type '(choice (const sequence)
  1278. (const type)))
  1279. (defcustom org-use-fast-todo-selection 'prefix
  1280. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1281. This variable describes if and under what circumstances the cycling
  1282. mechanism for TODO keywords will be replaced by a single-key, direct
  1283. selection scheme.
  1284. When nil, fast selection is never used.
  1285. When the symbol `prefix', it will be used when `org-todo' is called with
  1286. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1287. in an agenda buffer.
  1288. When t, fast selection is used by default. In this case, the prefix
  1289. argument forces cycling instead.
  1290. In all cases, the special interface is only used if access keys have actually
  1291. been assigned by the user, i.e. if keywords in the configuration are followed
  1292. by a letter in parenthesis, like TODO(t)."
  1293. :group 'org-todo
  1294. :type '(choice
  1295. (const :tag "Never" nil)
  1296. (const :tag "By default" t)
  1297. (const :tag "Only with C-u C-c C-t" prefix)))
  1298. (defcustom org-after-todo-state-change-hook nil
  1299. "Hook which is run after the state of a TODO item was changed.
  1300. The new state (a string with a TODO keyword, or nil) is available in the
  1301. Lisp variable `state'."
  1302. :group 'org-todo
  1303. :type 'hook)
  1304. (defcustom org-log-done nil
  1305. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1306. When equal to the list (done), also prompt for a closing note.
  1307. This can also be configured on a per-file basis by adding one of
  1308. the following lines anywhere in the buffer:
  1309. #+STARTUP: logdone
  1310. #+STARTUP: lognotedone
  1311. #+STARTUP: nologdone"
  1312. :group 'org-todo
  1313. :group 'org-progress
  1314. :type '(choice
  1315. (const :tag "No logging" nil)
  1316. (const :tag "Record CLOSED timestamp" time)
  1317. (const :tag "Record CLOSED timestamp with closing note." note)))
  1318. ;; Normalize old uses of org-log-done.
  1319. (cond
  1320. ((eq org-log-done t) (setq org-log-done 'time))
  1321. ((and (listp org-log-done) (memq 'done org-log-done))
  1322. (setq org-log-done 'note)))
  1323. (defcustom org-log-note-clock-out nil
  1324. "Non-nil means, recored a note when clocking out of an item.
  1325. This can also be configured on a per-file basis by adding one of
  1326. the following lines anywhere in the buffer:
  1327. #+STARTUP: lognoteclock-out
  1328. #+STARTUP: nolognoteclock-out"
  1329. :group 'org-todo
  1330. :group 'org-progress
  1331. :type 'boolean)
  1332. (defcustom org-log-done-with-time t
  1333. "Non-nil means, the CLOSED time stamp will contain date and time.
  1334. When nil, only the date will be recorded."
  1335. :group 'org-progress
  1336. :type 'boolean)
  1337. (defcustom org-log-note-headings
  1338. '((done . "CLOSING NOTE %t")
  1339. (state . "State %-12s %t")
  1340. (note . "Note taken on %t")
  1341. (clock-out . ""))
  1342. "Headings for notes added to entries.
  1343. The value is an alist, with the car being a symbol indicating the note
  1344. context, and the cdr is the heading to be used. The heading may also be the
  1345. empty string.
  1346. %t in the heading will be replaced by a time stamp.
  1347. %s will be replaced by the new TODO state, in double quotes.
  1348. %u will be replaced by the user name.
  1349. %U will be replaced by the full user name."
  1350. :group 'org-todo
  1351. :group 'org-progress
  1352. :type '(list :greedy t
  1353. (cons (const :tag "Heading when closing an item" done) string)
  1354. (cons (const :tag
  1355. "Heading when changing todo state (todo sequence only)"
  1356. state) string)
  1357. (cons (const :tag "Heading when just taking a note" note) string)
  1358. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1359. (unless (assq 'note org-log-note-headings)
  1360. (push '(note . "%t") org-log-note-headings))
  1361. (defcustom org-log-states-order-reversed t
  1362. "Non-nil means, the latest state change note will be directly after heading.
  1363. When nil, the notes will be orderer according to time."
  1364. :group 'org-todo
  1365. :group 'org-progress
  1366. :type 'boolean)
  1367. (defcustom org-log-repeat 'time
  1368. "Non-nil means, record moving through the DONE state when triggering repeat.
  1369. An auto-repeating tasks is immediately switched back to TODO when marked
  1370. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1371. the TODO keyword definition, or recording a cloing note by setting
  1372. `org-log-done', there will be no record of the task moving trhough DONE.
  1373. This variable forces taking a note anyway. Possible values are:
  1374. nil Don't force a record
  1375. time Record a time stamp
  1376. note Record a note
  1377. This option can also be set with on a per-file-basis with
  1378. #+STARTUP: logrepeat
  1379. #+STARTUP: lognoterepeat
  1380. #+STARTUP: nologrepeat
  1381. You can have local logging settings for a subtree by setting the LOGGING
  1382. property to one or more of these keywords."
  1383. :group 'org-todo
  1384. :group 'org-progress
  1385. :type '(choice
  1386. (const :tag "Don't force a record" nil)
  1387. (const :tag "Force recording the DONE state" time)
  1388. (const :tag "Force recording a note with the DONE state" note)))
  1389. (defgroup org-priorities nil
  1390. "Priorities in Org-mode."
  1391. :tag "Org Priorities"
  1392. :group 'org-todo)
  1393. (defcustom org-highest-priority ?A
  1394. "The highest priority of TODO items. A character like ?A, ?B etc.
  1395. Must have a smaller ASCII number than `org-lowest-priority'."
  1396. :group 'org-priorities
  1397. :type 'character)
  1398. (defcustom org-lowest-priority ?C
  1399. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1400. Must have a larger ASCII number than `org-highest-priority'."
  1401. :group 'org-priorities
  1402. :type 'character)
  1403. (defcustom org-default-priority ?B
  1404. "The default priority of TODO items.
  1405. This is the priority an item get if no explicit priority is given."
  1406. :group 'org-priorities
  1407. :type 'character)
  1408. (defcustom org-priority-start-cycle-with-default t
  1409. "Non-nil means, start with default priority when starting to cycle.
  1410. When this is nil, the first step in the cycle will be (depending on the
  1411. command used) one higher or lower that the default priority."
  1412. :group 'org-priorities
  1413. :type 'boolean)
  1414. (defgroup org-time nil
  1415. "Options concerning time stamps and deadlines in Org-mode."
  1416. :tag "Org Time"
  1417. :group 'org)
  1418. (defcustom org-insert-labeled-timestamps-at-point nil
  1419. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1420. When nil, these labeled time stamps are forces into the second line of an
  1421. entry, just after the headline. When scheduling from the global TODO list,
  1422. the time stamp will always be forced into the second line."
  1423. :group 'org-time
  1424. :type 'boolean)
  1425. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1426. "Formats for `format-time-string' which are used for time stamps.
  1427. It is not recommended to change this constant.")
  1428. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1429. "Number of minutes to round time stamps to.
  1430. These are two values, the first applies when first creating a time stamp.
  1431. The second applies when changing it with the commands `S-up' and `S-down'.
  1432. When changing the time stamp, this means that it will change in steps
  1433. of N minutes, as given by the second value.
  1434. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1435. numbers should be factors of 60, so for example 5, 10, 15.
  1436. When this is larger than 1, you can still force an exact time-stamp by using
  1437. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1438. and by using a prefix arg to `S-up/down' to specify the exact number
  1439. of minutes to shift."
  1440. :group 'org-time
  1441. :get '(lambda (var) ; Make sure all entries have 5 elements
  1442. (if (integerp (default-value var))
  1443. (list (default-value var) 5)
  1444. (default-value var)))
  1445. :type '(list
  1446. (integer :tag "when inserting times")
  1447. (integer :tag "when modifying times")))
  1448. ;; Normalize old customizations of this variable.
  1449. (when (integerp org-time-stamp-rounding-minutes)
  1450. (setq org-time-stamp-rounding-minutes
  1451. (list org-time-stamp-rounding-minutes
  1452. org-time-stamp-rounding-minutes)))
  1453. (defcustom org-display-custom-times nil
  1454. "Non-nil means, overlay custom formats over all time stamps.
  1455. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1456. To turn this on on a per-file basis, insert anywhere in the file:
  1457. #+STARTUP: customtime"
  1458. :group 'org-time
  1459. :set 'set-default
  1460. :type 'sexp)
  1461. (make-variable-buffer-local 'org-display-custom-times)
  1462. (defcustom org-time-stamp-custom-formats
  1463. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1464. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1465. These are overlayed over the default ISO format if the variable
  1466. `org-display-custom-times' is set. Time like %H:%M should be at the
  1467. end of the second format."
  1468. :group 'org-time
  1469. :type 'sexp)
  1470. (defun org-time-stamp-format (&optional long inactive)
  1471. "Get the right format for a time string."
  1472. (let ((f (if long (cdr org-time-stamp-formats)
  1473. (car org-time-stamp-formats))))
  1474. (if inactive
  1475. (concat "[" (substring f 1 -1) "]")
  1476. f)))
  1477. (defcustom org-deadline-warning-days 14
  1478. "No. of days before expiration during which a deadline becomes active.
  1479. This variable governs the display in sparse trees and in the agenda.
  1480. When 0 or negative, it means use this number (the absolute value of it)
  1481. even if a deadline has a different individual lead time specified."
  1482. :group 'org-time
  1483. :group 'org-agenda-daily/weekly
  1484. :type 'number)
  1485. (defcustom org-read-date-prefer-future t
  1486. "Non-nil means, assume future for incomplete date input from user.
  1487. This affects the following situations:
  1488. 1. The user gives a day, but no month.
  1489. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1490. read this as the third of *next* month. However, if you enter \"17\",
  1491. it will be considered as *this* month.
  1492. 2. The user gives a month but not a year.
  1493. For example, if it is april and you enter \"feb 2\", this will be read
  1494. as feb 2, *next* year. \"May 5\", however, will be this year.
  1495. Currently this does not work for ISO week specifications.
  1496. When this option is nil, the current month and year will always be used
  1497. as defaults."
  1498. :group 'org-time
  1499. :type 'boolean)
  1500. (defcustom org-read-date-display-live t
  1501. "Non-nil means, display current interpretation of date prompt live.
  1502. This display will be in an overlay, in the minibuffer."
  1503. :group 'org-time
  1504. :type 'boolean)
  1505. (defcustom org-read-date-popup-calendar t
  1506. "Non-nil means, pop up a calendar when prompting for a date.
  1507. In the calendar, the date can be selected with mouse-1. However, the
  1508. minibuffer will also be active, and you can simply enter the date as well.
  1509. When nil, only the minibuffer will be available."
  1510. :group 'org-time
  1511. :type 'boolean)
  1512. (if (fboundp 'defvaralias)
  1513. (defvaralias 'org-popup-calendar-for-date-prompt
  1514. 'org-read-date-popup-calendar))
  1515. (defcustom org-extend-today-until 0
  1516. "The hour when your day really ends.
  1517. This has influence for the following applications:
  1518. - When switching the agenda to \"today\". It it is still earlier than
  1519. the time given here, the day recognized as TODAY is actually yesterday.
  1520. - When a date is read from the user and it is still before the time given
  1521. here, the current date and time will be assumed to be yesterday, 23:59.
  1522. FIXME:
  1523. IMPORTANT: This is still a very experimental feature, it may disappear
  1524. again or it may be extended to mean more things."
  1525. :group 'org-time
  1526. :type 'number)
  1527. (defcustom org-edit-timestamp-down-means-later nil
  1528. "Non-nil means, S-down will increase the time in a time stamp.
  1529. When nil, S-up will increase."
  1530. :group 'org-time
  1531. :type 'boolean)
  1532. (defcustom org-calendar-follow-timestamp-change t
  1533. "Non-nil means, make the calendar window follow timestamp changes.
  1534. When a timestamp is modified and the calendar window is visible, it will be
  1535. moved to the new date."
  1536. :group 'org-time
  1537. :type 'boolean)
  1538. (defgroup org-tags nil
  1539. "Options concerning tags in Org-mode."
  1540. :tag "Org Tags"
  1541. :group 'org)
  1542. (defcustom org-tag-alist nil
  1543. "List of tags allowed in Org-mode files.
  1544. When this list is nil, Org-mode will base TAG input on what is already in the
  1545. buffer.
  1546. The value of this variable is an alist, the car of each entry must be a
  1547. keyword as a string, the cdr may be a character that is used to select
  1548. that tag through the fast-tag-selection interface.
  1549. See the manual for details."
  1550. :group 'org-tags
  1551. :type '(repeat
  1552. (choice
  1553. (cons (string :tag "Tag name")
  1554. (character :tag "Access char"))
  1555. (const :tag "Start radio group" (:startgroup))
  1556. (const :tag "End radio group" (:endgroup)))))
  1557. (defcustom org-use-fast-tag-selection 'auto
  1558. "Non-nil means, use fast tag selection scheme.
  1559. This is a special interface to select and deselect tags with single keys.
  1560. When nil, fast selection is never used.
  1561. When the symbol `auto', fast selection is used if and only if selection
  1562. characters for tags have been configured, either through the variable
  1563. `org-tag-alist' or through a #+TAGS line in the buffer.
  1564. When t, fast selection is always used and selection keys are assigned
  1565. automatically if necessary."
  1566. :group 'org-tags
  1567. :type '(choice
  1568. (const :tag "Always" t)
  1569. (const :tag "Never" nil)
  1570. (const :tag "When selection characters are configured" 'auto)))
  1571. (defcustom org-fast-tag-selection-single-key nil
  1572. "Non-nil means, fast tag selection exits after first change.
  1573. When nil, you have to press RET to exit it.
  1574. During fast tag selection, you can toggle this flag with `C-c'.
  1575. This variable can also have the value `expert'. In this case, the window
  1576. displaying the tags menu is not even shown, until you press C-c again."
  1577. :group 'org-tags
  1578. :type '(choice
  1579. (const :tag "No" nil)
  1580. (const :tag "Yes" t)
  1581. (const :tag "Expert" expert)))
  1582. (defvar org-fast-tag-selection-include-todo nil
  1583. "Non-nil means, fast tags selection interface will also offer TODO states.
  1584. This is an undocumented feature, you should not rely on it.")
  1585. (defcustom org-tags-column -80
  1586. "The column to which tags should be indented in a headline.
  1587. If this number is positive, it specifies the column. If it is negative,
  1588. it means that the tags should be flushright to that column. For example,
  1589. -80 works well for a normal 80 character screen."
  1590. :group 'org-tags
  1591. :type 'integer)
  1592. (defcustom org-auto-align-tags t
  1593. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1594. These operations change the length of a headline and therefore shift
  1595. the tags around. With this options turned on, after each such operation
  1596. the tags are again aligned to `org-tags-column'."
  1597. :group 'org-tags
  1598. :type 'boolean)
  1599. (defcustom org-use-tag-inheritance t
  1600. "Non-nil means, tags in levels apply also for sublevels.
  1601. When nil, only the tags directly given in a specific line apply there.
  1602. If you turn off this option, you very likely want to turn on the
  1603. companion option `org-tags-match-list-sublevels'.
  1604. This may also be a list of tags that should be inherited, or a regexp that
  1605. matches tags that should be inherited."
  1606. :group 'org-tags
  1607. :type '(choice
  1608. (const :tag "Not" nil)
  1609. (const :tag "Always" t)
  1610. (repeat :tag "Specific tags" (string :tag "Tag"))
  1611. (regexp :tag "Tags matched by regexp")))
  1612. (defun org-tag-inherit-p (tag)
  1613. "Check if TAG is one that should be inherited."
  1614. (cond
  1615. ((eq org-use-tag-inheritance t) t)
  1616. ((not org-use-tag-inheritance) nil)
  1617. ((stringp org-use-tag-inheritance)
  1618. (string-match org-use-tag-inheritance tag))
  1619. ((listp org-use-tag-inheritance)
  1620. (member tag org-use-tag-inheritance))
  1621. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1622. (defcustom org-tags-match-list-sublevels nil
  1623. "Non-nil means list also sublevels of headlines matching tag search.
  1624. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1625. the sublevels of a headline matching a tag search often also match
  1626. the same search. Listing all of them can create very long lists.
  1627. Setting this variable to nil causes subtrees of a match to be skipped.
  1628. This option is off by default, because inheritance in on. If you turn
  1629. inheritance off, you very likely want to turn this option on.
  1630. As a special case, if the tag search is restricted to TODO items, the
  1631. value of this variable is ignored and sublevels are always checked, to
  1632. make sure all corresponding TODO items find their way into the list."
  1633. :group 'org-tags
  1634. :type 'boolean)
  1635. (defvar org-tags-history nil
  1636. "History of minibuffer reads for tags.")
  1637. (defvar org-last-tags-completion-table nil
  1638. "The last used completion table for tags.")
  1639. (defvar org-after-tags-change-hook nil
  1640. "Hook that is run after the tags in a line have changed.")
  1641. (defgroup org-properties nil
  1642. "Options concerning properties in Org-mode."
  1643. :tag "Org Properties"
  1644. :group 'org)
  1645. (defcustom org-property-format "%-10s %s"
  1646. "How property key/value pairs should be formatted by `indent-line'.
  1647. When `indent-line' hits a property definition, it will format the line
  1648. according to this format, mainly to make sure that the values are
  1649. lined-up with respect to each other."
  1650. :group 'org-properties
  1651. :type 'string)
  1652. (defcustom org-use-property-inheritance nil
  1653. "Non-nil means, properties apply also for sublevels.
  1654. This setting is chiefly used during property searches. Turning it on can
  1655. cause significant overhead when doing a search, which is why it is not
  1656. on by default.
  1657. When nil, only the properties directly given in the current entry count.
  1658. When t, every property is inherited. The value may also be a list of
  1659. properties that should have inheritance, or a regular expression matching
  1660. properties that should be inherited.
  1661. However, note that some special properties use inheritance under special
  1662. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1663. and the properties ending in \"_ALL\" when they are used as descriptor
  1664. for valid values of a property.
  1665. Note for programmers:
  1666. When querying an entry with `org-entry-get', you can control if inheritance
  1667. should be used. By default, `org-entry-get' looks only at the local
  1668. properties. You can request inheritance by setting the inherit argument
  1669. to t (to force inheritance) or to `selective' (to respect the setting
  1670. in this variable)."
  1671. :group 'org-properties
  1672. :type '(choice
  1673. (const :tag "Not" nil)
  1674. (const :tag "Always" t)
  1675. (repeat :tag "Specific properties" (string :tag "Property"))
  1676. (regexp :tag "Properties matched by regexp")))
  1677. (defun org-property-inherit-p (property)
  1678. "Check if PROPERTY is one that should be inherited."
  1679. (cond
  1680. ((eq org-use-property-inheritance t) t)
  1681. ((not org-use-property-inheritance) nil)
  1682. ((stringp org-use-property-inheritance)
  1683. (string-match org-use-property-inheritance property))
  1684. ((listp org-use-property-inheritance)
  1685. (member property org-use-property-inheritance))
  1686. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1687. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1688. "The default column format, if no other format has been defined.
  1689. This variable can be set on the per-file basis by inserting a line
  1690. #+COLUMNS: %25ITEM ....."
  1691. :group 'org-properties
  1692. :type 'string)
  1693. (defcustom org-global-properties nil
  1694. "List of property/value pairs that can be inherited by any entry.
  1695. You can set buffer-local values for this by adding lines like
  1696. #+PROPERTY: NAME VALUE"
  1697. :group 'org-properties
  1698. :type '(repeat
  1699. (cons (string :tag "Property")
  1700. (string :tag "Value"))))
  1701. (defvar org-local-properties nil
  1702. "List of property/value pairs that can be inherited by any entry.
  1703. Valid for the current buffer.
  1704. This variable is populated from #+PROPERTY lines.")
  1705. (defgroup org-agenda nil
  1706. "Options concerning agenda views in Org-mode."
  1707. :tag "Org Agenda"
  1708. :group 'org)
  1709. (defvar org-category nil
  1710. "Variable used by org files to set a category for agenda display.
  1711. Such files should use a file variable to set it, for example
  1712. # -*- mode: org; org-category: \"ELisp\"
  1713. or contain a special line
  1714. #+CATEGORY: ELisp
  1715. If the file does not specify a category, then file's base name
  1716. is used instead.")
  1717. (make-variable-buffer-local 'org-category)
  1718. (defcustom org-agenda-files nil
  1719. "The files to be used for agenda display.
  1720. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1721. \\[org-remove-file]. You can also use customize to edit the list.
  1722. If an entry is a directory, all files in that directory that are matched by
  1723. `org-agenda-file-regexp' will be part of the file list.
  1724. If the value of the variable is not a list but a single file name, then
  1725. the list of agenda files is actually stored and maintained in that file, one
  1726. agenda file per line."
  1727. :group 'org-agenda
  1728. :type '(choice
  1729. (repeat :tag "List of files and directories" file)
  1730. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1731. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1732. "Regular expression to match files for `org-agenda-files'.
  1733. If any element in the list in that variable contains a directory instead
  1734. of a normal file, all files in that directory that are matched by this
  1735. regular expression will be included."
  1736. :group 'org-agenda
  1737. :type 'regexp)
  1738. (defcustom org-agenda-text-search-extra-files nil
  1739. "List of extra files to be searched by text search commands.
  1740. These files will be search in addition to the agenda files bu the
  1741. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1742. Note that these files will only be searched for text search commands,
  1743. not for the other agenda views like todo lists, tag earches or the weekly
  1744. agenda. This variable is intended to list notes and possibly archive files
  1745. that should also be searched by these two commands."
  1746. :group 'org-agenda
  1747. :type '(repeat file))
  1748. (if (fboundp 'defvaralias)
  1749. (defvaralias 'org-agenda-multi-occur-extra-files
  1750. 'org-agenda-text-search-extra-files))
  1751. (defcustom org-agenda-skip-unavailable-files nil
  1752. "t means to just skip non-reachable files in `org-agenda-files'.
  1753. Nil means to remove them, after a query, from the list."
  1754. :group 'org-agenda
  1755. :type 'boolean)
  1756. (defcustom org-calendar-to-agenda-key [?c]
  1757. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1758. The command `org-calendar-goto-agenda' will be bound to this key. The
  1759. default is the character `c' because then `c' can be used to switch back and
  1760. forth between agenda and calendar."
  1761. :group 'org-agenda
  1762. :type 'sexp)
  1763. (eval-after-load "calendar"
  1764. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1765. 'org-calendar-goto-agenda))
  1766. (defgroup org-latex nil
  1767. "Options for embedding LaTeX code into Org-mode."
  1768. :tag "Org LaTeX"
  1769. :group 'org)
  1770. (defcustom org-format-latex-options
  1771. '(:foreground default :background default :scale 1.0
  1772. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1773. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1774. "Options for creating images from LaTeX fragments.
  1775. This is a property list with the following properties:
  1776. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1777. `default' means use the forground of the default face.
  1778. :background the background color, or \"Transparent\".
  1779. `default' means use the background of the default face.
  1780. :scale a scaling factor for the size of the images
  1781. :html-foreground, :html-background, :html-scale
  1782. The same numbers for HTML export.
  1783. :matchers a list indicating which matchers should be used to
  1784. find LaTeX fragments. Valid members of this list are:
  1785. \"begin\" find environments
  1786. \"$\" find math expressions surrounded by $...$
  1787. \"$$\" find math expressions surrounded by $$....$$
  1788. \"\\(\" find math expressions surrounded by \\(...\\)
  1789. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1790. :group 'org-latex
  1791. :type 'plist)
  1792. (defcustom org-format-latex-header "\\documentclass{article}
  1793. \\usepackage{fullpage} % do not remove
  1794. \\usepackage{amssymb}
  1795. \\usepackage[usenames]{color}
  1796. \\usepackage{amsmath}
  1797. \\usepackage{latexsym}
  1798. \\usepackage[mathscr]{eucal}
  1799. \\pagestyle{empty} % do not remove"
  1800. "The document header used for processing LaTeX fragments."
  1801. :group 'org-latex
  1802. :type 'string)
  1803. (defgroup org-font-lock nil
  1804. "Font-lock settings for highlighting in Org-mode."
  1805. :tag "Org Font Lock"
  1806. :group 'org)
  1807. (defcustom org-level-color-stars-only nil
  1808. "Non-nil means fontify only the stars in each headline.
  1809. When nil, the entire headline is fontified.
  1810. Changing it requires restart of `font-lock-mode' to become effective
  1811. also in regions already fontified."
  1812. :group 'org-font-lock
  1813. :type 'boolean)
  1814. (defcustom org-hide-leading-stars nil
  1815. "Non-nil means, hide the first N-1 stars in a headline.
  1816. This works by using the face `org-hide' for these stars. This
  1817. face is white for a light background, and black for a dark
  1818. background. You may have to customize the face `org-hide' to
  1819. make this work.
  1820. Changing it requires restart of `font-lock-mode' to become effective
  1821. also in regions already fontified.
  1822. You may also set this on a per-file basis by adding one of the following
  1823. lines to the buffer:
  1824. #+STARTUP: hidestars
  1825. #+STARTUP: showstars"
  1826. :group 'org-font-lock
  1827. :type 'boolean)
  1828. (defcustom org-fontify-done-headline nil
  1829. "Non-nil means, change the face of a headline if it is marked DONE.
  1830. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1831. When this is non-nil, the headline after the keyword is set to the
  1832. `org-headline-done' as an additional indication."
  1833. :group 'org-font-lock
  1834. :type 'boolean)
  1835. (defcustom org-fontify-emphasized-text t
  1836. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1837. Changing this variable requires a restart of Emacs to take effect."
  1838. :group 'org-font-lock
  1839. :type 'boolean)
  1840. (defcustom org-highlight-latex-fragments-and-specials nil
  1841. "Non-nil means, fontify what is treated specially by the exporters."
  1842. :group 'org-font-lock
  1843. :type 'boolean)
  1844. (defcustom org-hide-emphasis-markers nil
  1845. "Non-nil mean font-lock should hide the emphasis marker characters."
  1846. :group 'org-font-lock
  1847. :type 'boolean)
  1848. (defvar org-emph-re nil
  1849. "Regular expression for matching emphasis.")
  1850. (defvar org-verbatim-re nil
  1851. "Regular expression for matching verbatim text.")
  1852. (defvar org-emphasis-regexp-components) ; defined just below
  1853. (defvar org-emphasis-alist) ; defined just below
  1854. (defun org-set-emph-re (var val)
  1855. "Set variable and compute the emphasis regular expression."
  1856. (set var val)
  1857. (when (and (boundp 'org-emphasis-alist)
  1858. (boundp 'org-emphasis-regexp-components)
  1859. org-emphasis-alist org-emphasis-regexp-components)
  1860. (let* ((e org-emphasis-regexp-components)
  1861. (pre (car e))
  1862. (post (nth 1 e))
  1863. (border (nth 2 e))
  1864. (body (nth 3 e))
  1865. (nl (nth 4 e))
  1866. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1867. (body1 (concat body "*?"))
  1868. (markers (mapconcat 'car org-emphasis-alist ""))
  1869. (vmarkers (mapconcat
  1870. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1871. org-emphasis-alist "")))
  1872. ;; make sure special characters appear at the right position in the class
  1873. (if (string-match "\\^" markers)
  1874. (setq markers (concat (replace-match "" t t markers) "^")))
  1875. (if (string-match "-" markers)
  1876. (setq markers (concat (replace-match "" t t markers) "-")))
  1877. (if (string-match "\\^" vmarkers)
  1878. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1879. (if (string-match "-" vmarkers)
  1880. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1881. (if (> nl 0)
  1882. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1883. (int-to-string nl) "\\}")))
  1884. ;; Make the regexp
  1885. (setq org-emph-re
  1886. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1887. "\\("
  1888. "\\([" markers "]\\)"
  1889. "\\("
  1890. "[^" border "]\\|"
  1891. "[^" border (if (and nil stacked) markers) "]"
  1892. body1
  1893. "[^" border (if (and nil stacked) markers) "]"
  1894. "\\)"
  1895. "\\3\\)"
  1896. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1897. (setq org-verbatim-re
  1898. (concat "\\([" pre "]\\|^\\)"
  1899. "\\("
  1900. "\\([" vmarkers "]\\)"
  1901. "\\("
  1902. "[^" border "]\\|"
  1903. "[^" border "]"
  1904. body1
  1905. "[^" border "]"
  1906. "\\)"
  1907. "\\3\\)"
  1908. "\\([" post "]\\|$\\)")))))
  1909. (defcustom org-emphasis-regexp-components
  1910. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1911. "Components used to build the regular expression for emphasis.
  1912. This is a list with 6 entries. Terminology: In an emphasis string
  1913. like \" *strong word* \", we call the initial space PREMATCH, the final
  1914. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1915. and \"trong wor\" is the body. The different components in this variable
  1916. specify what is allowed/forbidden in each part:
  1917. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1918. post Chars allowed as postmatch. End of line will be allowed too.
  1919. border The chars *forbidden* as border characters.
  1920. body-regexp A regexp like \".\" to match a body character. Don't use
  1921. non-shy groups here, and don't allow newline here.
  1922. newline The maximum number of newlines allowed in an emphasis exp.
  1923. Use customize to modify this, or restart Emacs after changing it."
  1924. :group 'org-font-lock
  1925. :set 'org-set-emph-re
  1926. :type '(list
  1927. (sexp :tag "Allowed chars in pre ")
  1928. (sexp :tag "Allowed chars in post ")
  1929. (sexp :tag "Forbidden chars in border ")
  1930. (sexp :tag "Regexp for body ")
  1931. (integer :tag "number of newlines allowed")
  1932. (option (boolean :tag "Stacking (DISABLED) "))))
  1933. (defcustom org-emphasis-alist
  1934. '(("*" bold "<b>" "</b>")
  1935. ("/" italic "<i>" "</i>")
  1936. ("_" underline "<u>" "</u>")
  1937. ("=" org-code "<code>" "</code>" verbatim)
  1938. ("~" org-verbatim "" "" verbatim)
  1939. ("+" (:strike-through t) "<del>" "</del>")
  1940. )
  1941. "Special syntax for emphasized text.
  1942. Text starting and ending with a special character will be emphasized, for
  1943. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1944. characters, the face to be used by font-lock for highlighting in Org-mode
  1945. Emacs buffers, and the HTML tags to be used for this.
  1946. Use customize to modify this, or restart Emacs after changing it."
  1947. :group 'org-font-lock
  1948. :set 'org-set-emph-re
  1949. :type '(repeat
  1950. (list
  1951. (string :tag "Marker character")
  1952. (choice
  1953. (face :tag "Font-lock-face")
  1954. (plist :tag "Face property list"))
  1955. (string :tag "HTML start tag")
  1956. (string :tag "HTML end tag")
  1957. (option (const verbatim)))))
  1958. ;;; Miscellaneous options
  1959. (defgroup org-completion nil
  1960. "Completion in Org-mode."
  1961. :tag "Org Completion"
  1962. :group 'org)
  1963. (defcustom org-completion-fallback-command 'hippie-expand
  1964. "The expansion command called by \\[org-complete] in normal context.
  1965. Normal means, no org-mode-specific context."
  1966. :group 'org-completion
  1967. :type 'function)
  1968. ;;; The faces
  1969. (defgroup org-faces nil
  1970. "Faces in Org-mode."
  1971. :tag "Org Faces"
  1972. :group 'org-font-lock)
  1973. (defface org-hide
  1974. '((((background light)) (:foreground "white"))
  1975. (((background dark)) (:foreground "black")))
  1976. "Face used to hide leading stars in headlines.
  1977. The forground color of this face should be equal to the background
  1978. color of the frame."
  1979. :group 'org-faces)
  1980. (defface org-level-1 ;; font-lock-function-name-face
  1981. (org-compatible-face 'outline-1
  1982. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  1983. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  1984. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  1985. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  1986. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  1987. (t (:bold t))))
  1988. "Face used for level 1 headlines."
  1989. :group 'org-faces)
  1990. (defface org-level-2 ;; font-lock-variable-name-face
  1991. (org-compatible-face 'outline-2
  1992. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  1993. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  1994. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  1995. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  1996. (t (:bold t))))
  1997. "Face used for level 2 headlines."
  1998. :group 'org-faces)
  1999. (defface org-level-3 ;; font-lock-keyword-face
  2000. (org-compatible-face 'outline-3
  2001. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2002. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2003. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2004. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2005. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2006. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2007. (t (:bold t))))
  2008. "Face used for level 3 headlines."
  2009. :group 'org-faces)
  2010. (defface org-level-4 ;; font-lock-comment-face
  2011. (org-compatible-face 'outline-4
  2012. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2013. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2014. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2015. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2016. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2017. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2018. (t (:bold t))))
  2019. "Face used for level 4 headlines."
  2020. :group 'org-faces)
  2021. (defface org-level-5 ;; font-lock-type-face
  2022. (org-compatible-face 'outline-5
  2023. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2024. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2025. (((class color) (min-colors 8)) (:foreground "green"))))
  2026. "Face used for level 5 headlines."
  2027. :group 'org-faces)
  2028. (defface org-level-6 ;; font-lock-constant-face
  2029. (org-compatible-face 'outline-6
  2030. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2031. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2032. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2033. "Face used for level 6 headlines."
  2034. :group 'org-faces)
  2035. (defface org-level-7 ;; font-lock-builtin-face
  2036. (org-compatible-face 'outline-7
  2037. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2038. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2039. (((class color) (min-colors 8)) (:foreground "blue"))))
  2040. "Face used for level 7 headlines."
  2041. :group 'org-faces)
  2042. (defface org-level-8 ;; font-lock-string-face
  2043. (org-compatible-face 'outline-8
  2044. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2045. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2046. (((class color) (min-colors 8)) (:foreground "green"))))
  2047. "Face used for level 8 headlines."
  2048. :group 'org-faces)
  2049. (defface org-special-keyword ;; font-lock-string-face
  2050. (org-compatible-face nil
  2051. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2052. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2053. (t (:italic t))))
  2054. "Face used for special keywords."
  2055. :group 'org-faces)
  2056. (defface org-drawer ;; font-lock-function-name-face
  2057. (org-compatible-face nil
  2058. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2059. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2060. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2061. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2062. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2063. (t (:bold t))))
  2064. "Face used for drawers."
  2065. :group 'org-faces)
  2066. (defface org-property-value nil
  2067. "Face used for the value of a property."
  2068. :group 'org-faces)
  2069. (defface org-column
  2070. (org-compatible-face nil
  2071. '((((class color) (min-colors 16) (background light))
  2072. (:background "grey90"))
  2073. (((class color) (min-colors 16) (background dark))
  2074. (:background "grey30"))
  2075. (((class color) (min-colors 8))
  2076. (:background "cyan" :foreground "black"))
  2077. (t (:inverse-video t))))
  2078. "Face for column display of entry properties."
  2079. :group 'org-faces)
  2080. (when (fboundp 'set-face-attribute)
  2081. ;; Make sure that a fixed-width face is used when we have a column table.
  2082. (set-face-attribute 'org-column nil
  2083. :height (face-attribute 'default :height)
  2084. :family (face-attribute 'default :family)))
  2085. (defface org-warning
  2086. (org-compatible-face 'font-lock-warning-face
  2087. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2088. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2089. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2090. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2091. (t (:bold t))))
  2092. "Face for deadlines and TODO keywords."
  2093. :group 'org-faces)
  2094. (defface org-archived ; similar to shadow
  2095. (org-compatible-face 'shadow
  2096. '((((class color grayscale) (min-colors 88) (background light))
  2097. (:foreground "grey50"))
  2098. (((class color grayscale) (min-colors 88) (background dark))
  2099. (:foreground "grey70"))
  2100. (((class color) (min-colors 8) (background light))
  2101. (:foreground "green"))
  2102. (((class color) (min-colors 8) (background dark))
  2103. (:foreground "yellow"))))
  2104. "Face for headline with the ARCHIVE tag."
  2105. :group 'org-faces)
  2106. (defface org-link
  2107. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2108. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2109. (t (:underline t)))
  2110. "Face for links."
  2111. :group 'org-faces)
  2112. (defface org-ellipsis
  2113. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  2114. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  2115. (t (:strike-through t)))
  2116. "Face for the ellipsis in folded text."
  2117. :group 'org-faces)
  2118. (defface org-target
  2119. '((((class color) (background light)) (:underline t))
  2120. (((class color) (background dark)) (:underline t))
  2121. (t (:underline t)))
  2122. "Face for links."
  2123. :group 'org-faces)
  2124. (defface org-date
  2125. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2126. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2127. (t (:underline t)))
  2128. "Face for links."
  2129. :group 'org-faces)
  2130. (defface org-sexp-date
  2131. '((((class color) (background light)) (:foreground "Purple"))
  2132. (((class color) (background dark)) (:foreground "Cyan"))
  2133. (t (:underline t)))
  2134. "Face for links."
  2135. :group 'org-faces)
  2136. (defface org-tag
  2137. '((t (:bold t)))
  2138. "Face for tags."
  2139. :group 'org-faces)
  2140. (defface org-todo ; font-lock-warning-face
  2141. (org-compatible-face nil
  2142. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2143. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2144. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2145. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2146. (t (:inverse-video t :bold t))))
  2147. "Face for TODO keywords."
  2148. :group 'org-faces)
  2149. (defface org-done ;; font-lock-type-face
  2150. (org-compatible-face nil
  2151. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2152. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2153. (((class color) (min-colors 8)) (:foreground "green"))
  2154. (t (:bold t))))
  2155. "Face used for todo keywords that indicate DONE items."
  2156. :group 'org-faces)
  2157. (defface org-headline-done ;; font-lock-string-face
  2158. (org-compatible-face nil
  2159. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2160. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2161. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2162. "Face used to indicate that a headline is DONE.
  2163. This face is only used if `org-fontify-done-headline' is set. If applies
  2164. to the part of the headline after the DONE keyword."
  2165. :group 'org-faces)
  2166. (defcustom org-todo-keyword-faces nil
  2167. "Faces for specific TODO keywords.
  2168. This is a list of cons cells, with TODO keywords in the car
  2169. and faces in the cdr. The face can be a symbol, or a property
  2170. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  2171. :group 'org-faces
  2172. :group 'org-todo
  2173. :type '(repeat
  2174. (cons
  2175. (string :tag "keyword")
  2176. (sexp :tag "face"))))
  2177. (defface org-table ;; font-lock-function-name-face
  2178. (org-compatible-face nil
  2179. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2180. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2181. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2182. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2183. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2184. (((class color) (min-colors 8) (background dark)))))
  2185. "Face used for tables."
  2186. :group 'org-faces)
  2187. (defface org-formula
  2188. (org-compatible-face nil
  2189. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2190. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2191. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2192. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2193. (t (:bold t :italic t))))
  2194. "Face for formulas."
  2195. :group 'org-faces)
  2196. (defface org-code
  2197. (org-compatible-face nil
  2198. '((((class color grayscale) (min-colors 88) (background light))
  2199. (:foreground "grey50"))
  2200. (((class color grayscale) (min-colors 88) (background dark))
  2201. (:foreground "grey70"))
  2202. (((class color) (min-colors 8) (background light))
  2203. (:foreground "green"))
  2204. (((class color) (min-colors 8) (background dark))
  2205. (:foreground "yellow"))))
  2206. "Face for fixed-with text like code snippets."
  2207. :group 'org-faces
  2208. :version "22.1")
  2209. (defface org-verbatim
  2210. (org-compatible-face nil
  2211. '((((class color grayscale) (min-colors 88) (background light))
  2212. (:foreground "grey50" :underline t))
  2213. (((class color grayscale) (min-colors 88) (background dark))
  2214. (:foreground "grey70" :underline t))
  2215. (((class color) (min-colors 8) (background light))
  2216. (:foreground "green" :underline t))
  2217. (((class color) (min-colors 8) (background dark))
  2218. (:foreground "yellow" :underline t))))
  2219. "Face for fixed-with text like code snippets."
  2220. :group 'org-faces
  2221. :version "22.1")
  2222. (defface org-agenda-structure ;; font-lock-function-name-face
  2223. (org-compatible-face nil
  2224. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2225. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2226. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2227. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2228. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2229. (t (:bold t))))
  2230. "Face used in agenda for captions and dates."
  2231. :group 'org-faces)
  2232. (defface org-scheduled-today
  2233. (org-compatible-face nil
  2234. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2235. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2236. (((class color) (min-colors 8)) (:foreground "green"))
  2237. (t (:bold t :italic t))))
  2238. "Face for items scheduled for a certain day."
  2239. :group 'org-faces)
  2240. (defface org-scheduled-previously
  2241. (org-compatible-face nil
  2242. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2243. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2244. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2245. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2246. (t (:bold t))))
  2247. "Face for items scheduled previously, and not yet done."
  2248. :group 'org-faces)
  2249. (defface org-upcoming-deadline
  2250. (org-compatible-face nil
  2251. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2252. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2253. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2254. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2255. (t (:bold t))))
  2256. "Face for items scheduled previously, and not yet done."
  2257. :group 'org-faces)
  2258. (defcustom org-agenda-deadline-faces
  2259. '((1.0 . org-warning)
  2260. (0.5 . org-upcoming-deadline)
  2261. (0.0 . default))
  2262. "Faces for showing deadlines in the agenda.
  2263. This is a list of cons cells. The cdr of each cell is a face to be used,
  2264. and it can also just be like '(:foreground \"yellow\").
  2265. Each car is a fraction of the head-warning time that must have passed for
  2266. this the face in the cdr to be used for display. The numbers must be
  2267. given in descending order. The head-warning time is normally taken
  2268. from `org-deadline-warning-days', but can also be specified in the deadline
  2269. timestamp itself, like this:
  2270. DEADLINE: <2007-08-13 Mon -8d>
  2271. You may use d for days, w for weeks, m for months and y for years. Months
  2272. and years will only be treated in an approximate fashion (30.4 days for a
  2273. month and 365.24 days for a year)."
  2274. :group 'org-faces
  2275. :group 'org-agenda-daily/weekly
  2276. :type '(repeat
  2277. (cons
  2278. (number :tag "Fraction of head-warning time passed")
  2279. (sexp :tag "Face"))))
  2280. (defface org-agenda-restriction-lock
  2281. (org-compatible-face nil
  2282. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  2283. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  2284. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  2285. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  2286. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  2287. (t (:inverse-video t))))
  2288. "Face for showing the agenda restriction lock."
  2289. :group 'org-faces)
  2290. (defface org-time-grid ;; font-lock-variable-name-face
  2291. (org-compatible-face nil
  2292. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2293. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2294. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2295. "Face used for time grids."
  2296. :group 'org-faces)
  2297. (defconst org-level-faces
  2298. '(org-level-1 org-level-2 org-level-3 org-level-4
  2299. org-level-5 org-level-6 org-level-7 org-level-8
  2300. ))
  2301. (defcustom org-n-level-faces (length org-level-faces)
  2302. "The number of different faces to be used for headlines.
  2303. Org-mode defines 8 different headline faces, so this can be at most 8.
  2304. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  2305. :type 'number
  2306. :group 'org-faces)
  2307. ;;; Functions and variables from ther packages
  2308. ;; Declared here to avoid compiler warnings
  2309. ;; XEmacs only
  2310. (defvar outline-mode-menu-heading)
  2311. (defvar outline-mode-menu-show)
  2312. (defvar outline-mode-menu-hide)
  2313. (defvar zmacs-regions) ; XEmacs regions
  2314. ;; Emacs only
  2315. (defvar mark-active)
  2316. ;; Various packages
  2317. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  2318. (declare-function calendar-forward-day "cal-move" (arg))
  2319. (declare-function calendar-goto-date "cal-move" (date))
  2320. (declare-function calendar-goto-today "cal-move" ())
  2321. (declare-function calendar-iso-from-absolute "cal-iso" (&optional date))
  2322. (defvar calc-embedded-close-formula)
  2323. (defvar calc-embedded-open-formula)
  2324. (declare-function cdlatex-tab "ext:cdlatex" ())
  2325. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2326. (defvar font-lock-unfontify-region-function)
  2327. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2328. (declare-function org-agenda-skip "org-agenda" ())
  2329. (declare-function org-format-agenda-item "org-agenda"
  2330. (extra txt &optional category tags dotime noprefix remove-re))
  2331. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2332. (declare-function org-agenda-change-all-lines "org-agenda"
  2333. (newhead hdmarker &optional fixface))
  2334. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2335. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2336. (declare-function parse-time-string "parse-time" (string))
  2337. (declare-function remember "remember" (&optional initial))
  2338. (declare-function remember-buffer-desc "remember" ())
  2339. (declare-function remember-finalize "remember" ())
  2340. (defvar remember-save-after-remembering)
  2341. (defvar remember-data-file)
  2342. (defvar remember-register)
  2343. (defvar remember-buffer)
  2344. (defvar remember-handler-functions)
  2345. (defvar remember-annotation-functions)
  2346. (defvar texmathp-why)
  2347. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2348. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2349. (defvar w3m-current-url)
  2350. (defvar w3m-current-title)
  2351. (defvar org-latex-regexps)
  2352. ;;; Autoload and prepare some org modules
  2353. ;; Some table stuff that needs to be defined here, because it is used
  2354. ;; by the functions setting up org-mode or checking for table context.
  2355. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2356. "Detects an org-type or table-type table.")
  2357. (defconst org-table-line-regexp "^[ \t]*|"
  2358. "Detects an org-type table line.")
  2359. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2360. "Detects an org-type table line.")
  2361. (defconst org-table-hline-regexp "^[ \t]*|-"
  2362. "Detects an org-type table hline.")
  2363. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2364. "Detects a table-type table hline.")
  2365. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2366. "Searching from within a table (any type) this finds the first line
  2367. outside the table.")
  2368. ;; Autoload the functions in org-table.el that are needed by functions here.
  2369. (eval-and-compile
  2370. (org-autoload "org-table"
  2371. '(org-table-align org-table-begin org-table-blank-field
  2372. org-table-convert org-table-convert-region org-table-copy-down
  2373. org-table-copy-region org-table-create
  2374. org-table-create-or-convert-from-region
  2375. org-table-create-with-table.el org-table-current-dline
  2376. org-table-cut-region org-table-delete-column org-table-edit-field
  2377. org-table-edit-formulas org-table-end org-table-eval-formula
  2378. org-table-export org-table-field-info
  2379. org-table-get-stored-formulas org-table-goto-column
  2380. org-table-hline-and-move org-table-import org-table-insert-column
  2381. org-table-insert-hline org-table-insert-row org-table-iterate
  2382. org-table-justify-field-maybe org-table-kill-row
  2383. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2384. org-table-move-column org-table-move-column-left
  2385. org-table-move-column-right org-table-move-row
  2386. org-table-move-row-down org-table-move-row-up
  2387. org-table-next-field org-table-next-row org-table-paste-rectangle
  2388. org-table-previous-field org-table-recalculate
  2389. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2390. org-table-toggle-coordinate-overlays
  2391. org-table-toggle-formula-debugger org-table-wrap-region
  2392. orgtbl-mode turn-on-orgtbl)))
  2393. (defun org-at-table-p (&optional table-type)
  2394. "Return t if the cursor is inside an org-type table.
  2395. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2396. (if org-enable-table-editor
  2397. (save-excursion
  2398. (beginning-of-line 1)
  2399. (looking-at (if table-type org-table-any-line-regexp
  2400. org-table-line-regexp)))
  2401. nil))
  2402. (defsubst org-table-p () (org-at-table-p))
  2403. (defun org-at-table.el-p ()
  2404. "Return t if and only if we are at a table.el table."
  2405. (and (org-at-table-p 'any)
  2406. (save-excursion
  2407. (goto-char (org-table-begin 'any))
  2408. (looking-at org-table1-hline-regexp))))
  2409. (defun org-table-recognize-table.el ()
  2410. "If there is a table.el table nearby, recognize it and move into it."
  2411. (if org-table-tab-recognizes-table.el
  2412. (if (org-at-table.el-p)
  2413. (progn
  2414. (beginning-of-line 1)
  2415. (if (looking-at org-table-dataline-regexp)
  2416. nil
  2417. (if (looking-at org-table1-hline-regexp)
  2418. (progn
  2419. (beginning-of-line 2)
  2420. (if (looking-at org-table-any-border-regexp)
  2421. (beginning-of-line -1)))))
  2422. (if (re-search-forward "|" (org-table-end t) t)
  2423. (progn
  2424. (require 'table)
  2425. (if (table--at-cell-p (point))
  2426. t
  2427. (message "recognizing table.el table...")
  2428. (table-recognize-table)
  2429. (message "recognizing table.el table...done")))
  2430. (error "This should not happen..."))
  2431. t)
  2432. nil)
  2433. nil))
  2434. (defun org-at-table-hline-p ()
  2435. "Return t if the cursor is inside a hline in a table."
  2436. (if org-enable-table-editor
  2437. (save-excursion
  2438. (beginning-of-line 1)
  2439. (looking-at org-table-hline-regexp))
  2440. nil))
  2441. (defvar org-table-clean-did-remove-column nil)
  2442. (defun org-table-map-tables (function)
  2443. "Apply FUNCTION to the start of all tables in the buffer."
  2444. (save-excursion
  2445. (save-restriction
  2446. (widen)
  2447. (goto-char (point-min))
  2448. (while (re-search-forward org-table-any-line-regexp nil t)
  2449. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2450. (beginning-of-line 1)
  2451. (if (looking-at org-table-line-regexp)
  2452. (save-excursion (funcall function)))
  2453. (re-search-forward org-table-any-border-regexp nil 1))))
  2454. (message "Mapping tables: done"))
  2455. ;; Declare and autoload functions from org-exp.el
  2456. (declare-function org-default-export-plist "org-exp")
  2457. (declare-function org-infile-export-plist "org-exp")
  2458. (declare-function org-get-current-options "org-exp")
  2459. (eval-and-compile
  2460. (org-autoload "org-exp"
  2461. '(org-export org-export-as-ascii org-export-visible
  2462. org-insert-export-options-template org-export-as-html-and-open
  2463. org-export-as-html-batch org-export-as-html-to-buffer
  2464. org-replace-region-by-html org-export-region-as-html
  2465. org-export-as-html org-export-icalendar-this-file
  2466. org-export-icalendar-all-agenda-files
  2467. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2468. ;; Autoload org-remember
  2469. (eval-and-compile
  2470. (org-autoload "org-remember"
  2471. '(org-remember-insinuate org-remember-annotation
  2472. org-remember-apply-template org-remember org-remember-handler)))
  2473. ;; Autoload org-clock.el
  2474. (defvar org-clock-marker (make-marker)
  2475. "Marker recording the last clock-in.")
  2476. (eval-and-compile
  2477. (org-autoload
  2478. "org-clock"
  2479. '(org-clock-in org-clock-out org-clock-cancel
  2480. org-clock-goto org-clock-sum org-clock-display
  2481. org-remove-clock-overlays org-clock-report
  2482. org-clocktable-shift org-dblock-write:clocktable
  2483. org-get-clocktable)))
  2484. (defun org-clock-update-time-maybe ()
  2485. "If this is a CLOCK line, update it and return t.
  2486. Otherwise, return nil."
  2487. (interactive)
  2488. (save-excursion
  2489. (beginning-of-line 1)
  2490. (skip-chars-forward " \t")
  2491. (when (looking-at org-clock-string)
  2492. (let ((re (concat "[ \t]*" org-clock-string
  2493. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2494. "\\([ \t]*=>.*\\)?"))
  2495. ts te h m s)
  2496. (if (not (looking-at re))
  2497. nil
  2498. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2499. (end-of-line 1)
  2500. (setq ts (match-string 1)
  2501. te (match-string 2))
  2502. (setq s (- (time-to-seconds
  2503. (apply 'encode-time (org-parse-time-string te)))
  2504. (time-to-seconds
  2505. (apply 'encode-time (org-parse-time-string ts))))
  2506. h (floor (/ s 3600))
  2507. s (- s (* 3600 h))
  2508. m (floor (/ s 60))
  2509. s (- s (* 60 s)))
  2510. (insert " => " (format "%2d:%02d" h m))
  2511. t)))))
  2512. (defun org-check-running-clock ()
  2513. "Check if the current buffer contains the running clock.
  2514. If yes, offer to stop it and to save the buffer with the changes."
  2515. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2516. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2517. (buffer-name))))
  2518. (org-clock-out)
  2519. (when (y-or-n-p "Save changed buffer?")
  2520. (save-buffer))))
  2521. (defun org-clocktable-try-shift (dir n)
  2522. "Check if this line starts a clock table, if yes, shift the time block."
  2523. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2524. (org-clocktable-shift dir n)))
  2525. ;;; Variables for pre-computed regular expressions, all buffer local
  2526. (defvar org-drawer-regexp nil
  2527. "Matches first line of a hidden block.")
  2528. (make-variable-buffer-local 'org-drawer-regexp)
  2529. (defvar org-todo-regexp nil
  2530. "Matches any of the TODO state keywords.")
  2531. (make-variable-buffer-local 'org-todo-regexp)
  2532. (defvar org-not-done-regexp nil
  2533. "Matches any of the TODO state keywords except the last one.")
  2534. (make-variable-buffer-local 'org-not-done-regexp)
  2535. (defvar org-todo-line-regexp nil
  2536. "Matches a headline and puts TODO state into group 2 if present.")
  2537. (make-variable-buffer-local 'org-todo-line-regexp)
  2538. (defvar org-complex-heading-regexp nil
  2539. "Matches a headline and puts everything into groups:
  2540. group 1: the stars
  2541. group 2: The todo keyword, maybe
  2542. group 3: Priority cookie
  2543. group 4: True headline
  2544. group 5: Tags")
  2545. (make-variable-buffer-local 'org-complex-heading-regexp)
  2546. (defvar org-todo-line-tags-regexp nil
  2547. "Matches a headline and puts TODO state into group 2 if present.
  2548. Also put tags into group 4 if tags are present.")
  2549. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2550. (defvar org-nl-done-regexp nil
  2551. "Matches newline followed by a headline with the DONE keyword.")
  2552. (make-variable-buffer-local 'org-nl-done-regexp)
  2553. (defvar org-looking-at-done-regexp nil
  2554. "Matches the DONE keyword a point.")
  2555. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2556. (defvar org-ds-keyword-length 12
  2557. "Maximum length of the Deadline and SCHEDULED keywords.")
  2558. (make-variable-buffer-local 'org-ds-keyword-length)
  2559. (defvar org-deadline-regexp nil
  2560. "Matches the DEADLINE keyword.")
  2561. (make-variable-buffer-local 'org-deadline-regexp)
  2562. (defvar org-deadline-time-regexp nil
  2563. "Matches the DEADLINE keyword together with a time stamp.")
  2564. (make-variable-buffer-local 'org-deadline-time-regexp)
  2565. (defvar org-deadline-line-regexp nil
  2566. "Matches the DEADLINE keyword and the rest of the line.")
  2567. (make-variable-buffer-local 'org-deadline-line-regexp)
  2568. (defvar org-scheduled-regexp nil
  2569. "Matches the SCHEDULED keyword.")
  2570. (make-variable-buffer-local 'org-scheduled-regexp)
  2571. (defvar org-scheduled-time-regexp nil
  2572. "Matches the SCHEDULED keyword together with a time stamp.")
  2573. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2574. (defvar org-closed-time-regexp nil
  2575. "Matches the CLOSED keyword together with a time stamp.")
  2576. (make-variable-buffer-local 'org-closed-time-regexp)
  2577. (defvar org-keyword-time-regexp nil
  2578. "Matches any of the 4 keywords, together with the time stamp.")
  2579. (make-variable-buffer-local 'org-keyword-time-regexp)
  2580. (defvar org-keyword-time-not-clock-regexp nil
  2581. "Matches any of the 3 keywords, together with the time stamp.")
  2582. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2583. (defvar org-maybe-keyword-time-regexp nil
  2584. "Matches a timestamp, possibly preceeded by a keyword.")
  2585. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2586. (defvar org-planning-or-clock-line-re nil
  2587. "Matches a line with planning or clock info.")
  2588. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2589. (defconst org-plain-time-of-day-regexp
  2590. (concat
  2591. "\\(\\<[012]?[0-9]"
  2592. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2593. "\\(--?"
  2594. "\\(\\<[012]?[0-9]"
  2595. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2596. "\\)?")
  2597. "Regular expression to match a plain time or time range.
  2598. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2599. groups carry important information:
  2600. 0 the full match
  2601. 1 the first time, range or not
  2602. 8 the second time, if it is a range.")
  2603. (defconst org-plain-time-extension-regexp
  2604. (concat
  2605. "\\(\\<[012]?[0-9]"
  2606. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2607. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2608. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2609. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2610. groups carry important information:
  2611. 0 the full match
  2612. 7 hours of duration
  2613. 9 minutes of duration")
  2614. (defconst org-stamp-time-of-day-regexp
  2615. (concat
  2616. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2617. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2618. "\\(--?"
  2619. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2620. "Regular expression to match a timestamp time or time range.
  2621. After a match, the following groups carry important information:
  2622. 0 the full match
  2623. 1 date plus weekday, for backreferencing to make sure both times on same day
  2624. 2 the first time, range or not
  2625. 4 the second time, if it is a range.")
  2626. (defconst org-startup-options
  2627. '(("fold" org-startup-folded t)
  2628. ("overview" org-startup-folded t)
  2629. ("nofold" org-startup-folded nil)
  2630. ("showall" org-startup-folded nil)
  2631. ("content" org-startup-folded content)
  2632. ("hidestars" org-hide-leading-stars t)
  2633. ("showstars" org-hide-leading-stars nil)
  2634. ("odd" org-odd-levels-only t)
  2635. ("oddeven" org-odd-levels-only nil)
  2636. ("align" org-startup-align-all-tables t)
  2637. ("noalign" org-startup-align-all-tables nil)
  2638. ("customtime" org-display-custom-times t)
  2639. ("logdone" org-log-done time)
  2640. ("lognotedone" org-log-done note)
  2641. ("nologdone" org-log-done nil)
  2642. ("lognoteclock-out" org-log-note-clock-out t)
  2643. ("nolognoteclock-out" org-log-note-clock-out nil)
  2644. ("logrepeat" org-log-repeat state)
  2645. ("lognoterepeat" org-log-repeat note)
  2646. ("nologrepeat" org-log-repeat nil)
  2647. ("constcgs" constants-unit-system cgs)
  2648. ("constSI" constants-unit-system SI))
  2649. "Variable associated with STARTUP options for org-mode.
  2650. Each element is a list of three items: The startup options as written
  2651. in the #+STARTUP line, the corresponding variable, and the value to
  2652. set this variable to if the option is found. An optional forth element PUSH
  2653. means to push this value onto the list in the variable.")
  2654. (defun org-set-regexps-and-options ()
  2655. "Precompute regular expressions for current buffer."
  2656. (when (org-mode-p)
  2657. (org-set-local 'org-todo-kwd-alist nil)
  2658. (org-set-local 'org-todo-key-alist nil)
  2659. (org-set-local 'org-todo-key-trigger nil)
  2660. (org-set-local 'org-todo-keywords-1 nil)
  2661. (org-set-local 'org-done-keywords nil)
  2662. (org-set-local 'org-todo-heads nil)
  2663. (org-set-local 'org-todo-sets nil)
  2664. (org-set-local 'org-todo-log-states nil)
  2665. (let ((re (org-make-options-regexp
  2666. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2667. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2668. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2669. (splitre "[ \t]+")
  2670. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2671. tail sep kws1 prio props drawers)
  2672. (save-excursion
  2673. (save-restriction
  2674. (widen)
  2675. (goto-char (point-min))
  2676. (while (re-search-forward re nil t)
  2677. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2678. (cond
  2679. ((equal key "CATEGORY")
  2680. (if (string-match "[ \t]+$" value)
  2681. (setq value (replace-match "" t t value)))
  2682. (setq cat value))
  2683. ((member key '("SEQ_TODO" "TODO"))
  2684. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2685. ((equal key "TYP_TODO")
  2686. (push (cons 'type (org-split-string value splitre)) kwds))
  2687. ((equal key "TAGS")
  2688. (setq tags (append tags (org-split-string value splitre))))
  2689. ((equal key "COLUMNS")
  2690. (org-set-local 'org-columns-default-format value))
  2691. ((equal key "LINK")
  2692. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2693. (push (cons (match-string 1 value)
  2694. (org-trim (match-string 2 value)))
  2695. links)))
  2696. ((equal key "PRIORITIES")
  2697. (setq prio (org-split-string value " +")))
  2698. ((equal key "PROPERTY")
  2699. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2700. (push (cons (match-string 1 value) (match-string 2 value))
  2701. props)))
  2702. ((equal key "DRAWERS")
  2703. (setq drawers (org-split-string value splitre)))
  2704. ((equal key "CONSTANTS")
  2705. (setq const (append const (org-split-string value splitre))))
  2706. ((equal key "STARTUP")
  2707. (let ((opts (org-split-string value splitre))
  2708. l var val)
  2709. (while (setq l (pop opts))
  2710. (when (setq l (assoc l org-startup-options))
  2711. (setq var (nth 1 l) val (nth 2 l))
  2712. (if (not (nth 3 l))
  2713. (set (make-local-variable var) val)
  2714. (if (not (listp (symbol-value var)))
  2715. (set (make-local-variable var) nil))
  2716. (set (make-local-variable var) (symbol-value var))
  2717. (add-to-list var val))))))
  2718. ((equal key "ARCHIVE")
  2719. (string-match " *$" value)
  2720. (setq arch (replace-match "" t t value))
  2721. (remove-text-properties 0 (length arch)
  2722. '(face t fontified t) arch)))
  2723. )))
  2724. (when cat
  2725. (org-set-local 'org-category (intern cat))
  2726. (push (cons "CATEGORY" cat) props))
  2727. (when prio
  2728. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2729. (setq prio (mapcar 'string-to-char prio))
  2730. (org-set-local 'org-highest-priority (nth 0 prio))
  2731. (org-set-local 'org-lowest-priority (nth 1 prio))
  2732. (org-set-local 'org-default-priority (nth 2 prio)))
  2733. (and props (org-set-local 'org-local-properties (nreverse props)))
  2734. (and drawers (org-set-local 'org-drawers drawers))
  2735. (and arch (org-set-local 'org-archive-location arch))
  2736. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2737. ;; Process the TODO keywords
  2738. (unless kwds
  2739. ;; Use the global values as if they had been given locally.
  2740. (setq kwds (default-value 'org-todo-keywords))
  2741. (if (stringp (car kwds))
  2742. (setq kwds (list (cons org-todo-interpretation
  2743. (default-value 'org-todo-keywords)))))
  2744. (setq kwds (reverse kwds)))
  2745. (setq kwds (nreverse kwds))
  2746. (let (inter kws kw)
  2747. (while (setq kws (pop kwds))
  2748. (setq inter (pop kws) sep (member "|" kws)
  2749. kws0 (delete "|" (copy-sequence kws))
  2750. kwsa nil
  2751. kws1 (mapcar
  2752. (lambda (x)
  2753. ;; 1 2
  2754. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2755. (progn
  2756. (setq kw (match-string 1 x)
  2757. key (and (match-end 2) (match-string 2 x))
  2758. log (org-extract-log-state-settings x))
  2759. (push (cons kw (and key (string-to-char key))) kwsa)
  2760. (and log (push log org-todo-log-states))
  2761. kw)
  2762. (error "Invalid TODO keyword %s" x)))
  2763. kws0)
  2764. kwsa (if kwsa (append '((:startgroup))
  2765. (nreverse kwsa)
  2766. '((:endgroup))))
  2767. hw (car kws1)
  2768. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2769. tail (list inter hw (car dws) (org-last dws)))
  2770. (add-to-list 'org-todo-heads hw 'append)
  2771. (push kws1 org-todo-sets)
  2772. (setq org-done-keywords (append org-done-keywords dws nil))
  2773. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2774. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2775. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2776. (setq org-todo-sets (nreverse org-todo-sets)
  2777. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2778. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2779. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2780. ;; Process the constants
  2781. (when const
  2782. (let (e cst)
  2783. (while (setq e (pop const))
  2784. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2785. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2786. (setq org-table-formula-constants-local cst)))
  2787. ;; Process the tags.
  2788. (when tags
  2789. (let (e tgs)
  2790. (while (setq e (pop tags))
  2791. (cond
  2792. ((equal e "{") (push '(:startgroup) tgs))
  2793. ((equal e "}") (push '(:endgroup) tgs))
  2794. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2795. (push (cons (match-string 1 e)
  2796. (string-to-char (match-string 2 e)))
  2797. tgs))
  2798. (t (push (list e) tgs))))
  2799. (org-set-local 'org-tag-alist nil)
  2800. (while (setq e (pop tgs))
  2801. (or (and (stringp (car e))
  2802. (assoc (car e) org-tag-alist))
  2803. (push e org-tag-alist))))))
  2804. ;; Compute the regular expressions and other local variables
  2805. (if (not org-done-keywords)
  2806. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2807. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2808. (length org-scheduled-string)))
  2809. org-drawer-regexp
  2810. (concat "^[ \t]*:\\("
  2811. (mapconcat 'regexp-quote org-drawers "\\|")
  2812. "\\):[ \t]*$")
  2813. org-not-done-keywords
  2814. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2815. org-todo-regexp
  2816. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2817. "\\|") "\\)\\>")
  2818. org-not-done-regexp
  2819. (concat "\\<\\("
  2820. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2821. "\\)\\>")
  2822. org-todo-line-regexp
  2823. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2824. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2825. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2826. org-complex-heading-regexp
  2827. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2828. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2829. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2830. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2831. org-nl-done-regexp
  2832. (concat "\n\\*+[ \t]+"
  2833. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2834. "\\)" "\\>")
  2835. org-todo-line-tags-regexp
  2836. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2837. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2838. (org-re
  2839. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2840. org-looking-at-done-regexp
  2841. (concat "^" "\\(?:"
  2842. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2843. "\\>")
  2844. org-deadline-regexp (concat "\\<" org-deadline-string)
  2845. org-deadline-time-regexp
  2846. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2847. org-deadline-line-regexp
  2848. (concat "\\<\\(" org-deadline-string "\\).*")
  2849. org-scheduled-regexp
  2850. (concat "\\<" org-scheduled-string)
  2851. org-scheduled-time-regexp
  2852. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2853. org-closed-time-regexp
  2854. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2855. org-keyword-time-regexp
  2856. (concat "\\<\\(" org-scheduled-string
  2857. "\\|" org-deadline-string
  2858. "\\|" org-closed-string
  2859. "\\|" org-clock-string "\\)"
  2860. " *[[<]\\([^]>]+\\)[]>]")
  2861. org-keyword-time-not-clock-regexp
  2862. (concat "\\<\\(" org-scheduled-string
  2863. "\\|" org-deadline-string
  2864. "\\|" org-closed-string
  2865. "\\)"
  2866. " *[[<]\\([^]>]+\\)[]>]")
  2867. org-maybe-keyword-time-regexp
  2868. (concat "\\(\\<\\(" org-scheduled-string
  2869. "\\|" org-deadline-string
  2870. "\\|" org-closed-string
  2871. "\\|" org-clock-string "\\)\\)?"
  2872. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2873. org-planning-or-clock-line-re
  2874. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2875. "\\|" org-deadline-string
  2876. "\\|" org-closed-string "\\|" org-clock-string
  2877. "\\)\\>\\)")
  2878. )
  2879. (org-compute-latex-and-specials-regexp)
  2880. (org-set-font-lock-defaults)))
  2881. (defun org-extract-log-state-settings (x)
  2882. "Extract the log state setting from a TODO keyword string.
  2883. This will extract info from a string like \"WAIT(w@/!)\"."
  2884. (let (kw key log1 log2)
  2885. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2886. (setq kw (match-string 1 x)
  2887. key (and (match-end 2) (match-string 2 x))
  2888. log1 (and (match-end 3) (match-string 3 x))
  2889. log2 (and (match-end 4) (match-string 4 x)))
  2890. (and (or log1 log2)
  2891. (list kw
  2892. (and log1 (if (equal log1 "!") 'time 'note))
  2893. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2894. (defun org-remove-keyword-keys (list)
  2895. "Remove a pair of parenthesis at the end of each string in LIST."
  2896. (mapcar (lambda (x)
  2897. (if (string-match "(.*)$" x)
  2898. (substring x 0 (match-beginning 0))
  2899. x))
  2900. list))
  2901. ;; FIXME: this could be done much better, using second characters etc.
  2902. (defun org-assign-fast-keys (alist)
  2903. "Assign fast keys to a keyword-key alist.
  2904. Respect keys that are already there."
  2905. (let (new e k c c1 c2 (char ?a))
  2906. (while (setq e (pop alist))
  2907. (cond
  2908. ((equal e '(:startgroup)) (push e new))
  2909. ((equal e '(:endgroup)) (push e new))
  2910. (t
  2911. (setq k (car e) c2 nil)
  2912. (if (cdr e)
  2913. (setq c (cdr e))
  2914. ;; automatically assign a character.
  2915. (setq c1 (string-to-char
  2916. (downcase (substring
  2917. k (if (= (string-to-char k) ?@) 1 0)))))
  2918. (if (or (rassoc c1 new) (rassoc c1 alist))
  2919. (while (or (rassoc char new) (rassoc char alist))
  2920. (setq char (1+ char)))
  2921. (setq c2 c1))
  2922. (setq c (or c2 char)))
  2923. (push (cons k c) new))))
  2924. (nreverse new)))
  2925. ;;; Some variables used in various places
  2926. (defvar org-window-configuration nil
  2927. "Used in various places to store a window configuration.")
  2928. (defvar org-finish-function nil
  2929. "Function to be called when `C-c C-c' is used.
  2930. This is for getting out of special buffers like remember.")
  2931. ;; FIXME: Occasionally check by commenting these, to make sure
  2932. ;; no other functions uses these, forgetting to let-bind them.
  2933. (defvar entry)
  2934. (defvar state)
  2935. (defvar last-state)
  2936. (defvar date)
  2937. (defvar description)
  2938. ;; Defined somewhere in this file, but used before definition.
  2939. (defvar org-html-entities)
  2940. (defvar org-struct-menu)
  2941. (defvar org-org-menu)
  2942. (defvar org-tbl-menu)
  2943. (defvar org-agenda-keymap)
  2944. ;;;; Define the Org-mode
  2945. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2946. (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."))
  2947. ;; We use a before-change function to check if a table might need
  2948. ;; an update.
  2949. (defvar org-table-may-need-update t
  2950. "Indicates that a table might need an update.
  2951. This variable is set by `org-before-change-function'.
  2952. `org-table-align' sets it back to nil.")
  2953. (defun org-before-change-function (beg end)
  2954. "Every change indicates that a table might need an update."
  2955. (setq org-table-may-need-update t))
  2956. (defvar org-mode-map)
  2957. (defvar org-mode-hook nil)
  2958. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2959. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2960. (defvar org-table-buffer-is-an nil)
  2961. (defconst org-outline-regexp "\\*+ ")
  2962. ;;;###autoload
  2963. (define-derived-mode org-mode outline-mode "Org"
  2964. "Outline-based notes management and organizer, alias
  2965. \"Carsten's outline-mode for keeping track of everything.\"
  2966. Org-mode develops organizational tasks around a NOTES file which
  2967. contains information about projects as plain text. Org-mode is
  2968. implemented on top of outline-mode, which is ideal to keep the content
  2969. of large files well structured. It supports ToDo items, deadlines and
  2970. time stamps, which magically appear in the diary listing of the Emacs
  2971. calendar. Tables are easily created with a built-in table editor.
  2972. Plain text URL-like links connect to websites, emails (VM), Usenet
  2973. messages (Gnus), BBDB entries, and any files related to the project.
  2974. For printing and sharing of notes, an Org-mode file (or a part of it)
  2975. can be exported as a structured ASCII or HTML file.
  2976. The following commands are available:
  2977. \\{org-mode-map}"
  2978. ;; Get rid of Outline menus, they are not needed
  2979. ;; Need to do this here because define-derived-mode sets up
  2980. ;; the keymap so late. Still, it is a waste to call this each time
  2981. ;; we switch another buffer into org-mode.
  2982. (if (featurep 'xemacs)
  2983. (when (boundp 'outline-mode-menu-heading)
  2984. ;; Assume this is Greg's port, it used easymenu
  2985. (easy-menu-remove outline-mode-menu-heading)
  2986. (easy-menu-remove outline-mode-menu-show)
  2987. (easy-menu-remove outline-mode-menu-hide))
  2988. (define-key org-mode-map [menu-bar headings] 'undefined)
  2989. (define-key org-mode-map [menu-bar hide] 'undefined)
  2990. (define-key org-mode-map [menu-bar show] 'undefined))
  2991. (org-load-modules-maybe)
  2992. (easy-menu-add org-org-menu)
  2993. (easy-menu-add org-tbl-menu)
  2994. (org-install-agenda-files-menu)
  2995. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2996. (org-add-to-invisibility-spec '(org-cwidth))
  2997. (when (featurep 'xemacs)
  2998. (org-set-local 'line-move-ignore-invisible t))
  2999. (org-set-local 'outline-regexp org-outline-regexp)
  3000. (org-set-local 'outline-level 'org-outline-level)
  3001. (when (and org-ellipsis
  3002. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3003. (fboundp 'make-glyph-code))
  3004. (unless org-display-table
  3005. (setq org-display-table (make-display-table)))
  3006. (set-display-table-slot
  3007. org-display-table 4
  3008. (vconcat (mapcar
  3009. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3010. org-ellipsis)))
  3011. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3012. (setq buffer-display-table org-display-table))
  3013. (org-set-regexps-and-options)
  3014. ;; Calc embedded
  3015. (org-set-local 'calc-embedded-open-mode "# ")
  3016. (modify-syntax-entry ?# "<")
  3017. (modify-syntax-entry ?@ "w")
  3018. (if org-startup-truncated (setq truncate-lines t))
  3019. (org-set-local 'font-lock-unfontify-region-function
  3020. 'org-unfontify-region)
  3021. ;; Activate before-change-function
  3022. (org-set-local 'org-table-may-need-update t)
  3023. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3024. 'local)
  3025. ;; Check for running clock before killing a buffer
  3026. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3027. ;; Paragraphs and auto-filling
  3028. (org-set-autofill-regexps)
  3029. (setq indent-line-function 'org-indent-line-function)
  3030. (org-update-radio-target-regexp)
  3031. ;; Comment characters
  3032. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3033. (org-set-local 'comment-padding " ")
  3034. ;; Align options lines
  3035. (org-set-local
  3036. 'align-mode-rules-list
  3037. '((org-in-buffer-settings
  3038. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3039. (modes . '(org-mode)))))
  3040. ;; Imenu
  3041. (org-set-local 'imenu-create-index-function
  3042. 'org-imenu-get-tree)
  3043. ;; Make isearch reveal context
  3044. (if (or (featurep 'xemacs)
  3045. (not (boundp 'outline-isearch-open-invisible-function)))
  3046. ;; Emacs 21 and XEmacs make use of the hook
  3047. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3048. ;; Emacs 22 deals with this through a special variable
  3049. (org-set-local 'outline-isearch-open-invisible-function
  3050. (lambda (&rest ignore) (org-show-context 'isearch))))
  3051. ;; If empty file that did not turn on org-mode automatically, make it to.
  3052. (if (and org-insert-mode-line-in-empty-file
  3053. (interactive-p)
  3054. (= (point-min) (point-max)))
  3055. (insert "# -*- mode: org -*-\n\n"))
  3056. (unless org-inhibit-startup
  3057. (when org-startup-align-all-tables
  3058. (let ((bmp (buffer-modified-p)))
  3059. (org-table-map-tables 'org-table-align)
  3060. (set-buffer-modified-p bmp)))
  3061. (org-cycle-hide-drawers 'all)
  3062. (cond
  3063. ((eq org-startup-folded t)
  3064. (org-cycle '(4)))
  3065. ((eq org-startup-folded 'content)
  3066. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3067. (org-cycle '(4)) (org-cycle '(4)))))))
  3068. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3069. (defun org-current-time ()
  3070. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3071. (if (> (car org-time-stamp-rounding-minutes) 1)
  3072. (let ((r (car org-time-stamp-rounding-minutes))
  3073. (time (decode-time)))
  3074. (apply 'encode-time
  3075. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3076. (nthcdr 2 time))))
  3077. (current-time)))
  3078. ;;;; Font-Lock stuff, including the activators
  3079. (defvar org-mouse-map (make-sparse-keymap))
  3080. (org-defkey org-mouse-map
  3081. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3082. (org-defkey org-mouse-map
  3083. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3084. (when org-mouse-1-follows-link
  3085. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3086. (when org-tab-follows-link
  3087. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3088. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3089. (when org-return-follows-link
  3090. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3091. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3092. (require 'font-lock)
  3093. (defconst org-non-link-chars "]\t\n\r<>")
  3094. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3095. "shell" "elisp"))
  3096. (defvar org-link-re-with-space nil
  3097. "Matches a link with spaces, optional angular brackets around it.")
  3098. (defvar org-link-re-with-space2 nil
  3099. "Matches a link with spaces, optional angular brackets around it.")
  3100. (defvar org-angle-link-re nil
  3101. "Matches link with angular brackets, spaces are allowed.")
  3102. (defvar org-plain-link-re nil
  3103. "Matches plain link, without spaces.")
  3104. (defvar org-bracket-link-regexp nil
  3105. "Matches a link in double brackets.")
  3106. (defvar org-bracket-link-analytic-regexp nil
  3107. "Regular expression used to analyze links.
  3108. Here is what the match groups contain after a match:
  3109. 1: http:
  3110. 2: http
  3111. 3: path
  3112. 4: [desc]
  3113. 5: desc")
  3114. (defvar org-any-link-re nil
  3115. "Regular expression matching any link.")
  3116. (defun org-make-link-regexps ()
  3117. "Update the link regular expressions.
  3118. This should be called after the variable `org-link-types' has changed."
  3119. (setq org-link-re-with-space
  3120. (concat
  3121. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3122. "\\([^" org-non-link-chars " ]"
  3123. "[^" org-non-link-chars "]*"
  3124. "[^" org-non-link-chars " ]\\)>?")
  3125. org-link-re-with-space2
  3126. (concat
  3127. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3128. "\\([^" org-non-link-chars " ]"
  3129. "[^]\t\n\r]*"
  3130. "[^" org-non-link-chars " ]\\)>?")
  3131. org-angle-link-re
  3132. (concat
  3133. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3134. "\\([^" org-non-link-chars " ]"
  3135. "[^" org-non-link-chars "]*"
  3136. "\\)>")
  3137. org-plain-link-re
  3138. (concat
  3139. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3140. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3141. org-bracket-link-regexp
  3142. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3143. org-bracket-link-analytic-regexp
  3144. (concat
  3145. "\\[\\["
  3146. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3147. "\\([^]]+\\)"
  3148. "\\]"
  3149. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3150. "\\]")
  3151. org-any-link-re
  3152. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3153. org-angle-link-re "\\)\\|\\("
  3154. org-plain-link-re "\\)")))
  3155. (org-make-link-regexps)
  3156. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3157. "Regular expression for fast time stamp matching.")
  3158. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3159. "Regular expression for fast time stamp matching.")
  3160. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3161. "Regular expression matching time strings for analysis.
  3162. This one does not require the space after the date, so it can be used
  3163. on a string that terminates immediately after the date.")
  3164. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3165. "Regular expression matching time strings for analysis.")
  3166. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3167. "Regular expression matching time stamps, with groups.")
  3168. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3169. "Regular expression matching time stamps (also [..]), with groups.")
  3170. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3171. "Regular expression matching a time stamp range.")
  3172. (defconst org-tr-regexp-both
  3173. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3174. "Regular expression matching a time stamp range.")
  3175. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3176. org-ts-regexp "\\)?")
  3177. "Regular expression matching a time stamp or time stamp range.")
  3178. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3179. org-ts-regexp-both "\\)?")
  3180. "Regular expression matching a time stamp or time stamp range.
  3181. The time stamps may be either active or inactive.")
  3182. (defvar org-emph-face nil)
  3183. (defun org-do-emphasis-faces (limit)
  3184. "Run through the buffer and add overlays to links."
  3185. (let (rtn)
  3186. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3187. (if (not (= (char-after (match-beginning 3))
  3188. (char-after (match-beginning 4))))
  3189. (progn
  3190. (setq rtn t)
  3191. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3192. 'face
  3193. (nth 1 (assoc (match-string 3)
  3194. org-emphasis-alist)))
  3195. (add-text-properties (match-beginning 2) (match-end 2)
  3196. '(font-lock-multiline t))
  3197. (when org-hide-emphasis-markers
  3198. (add-text-properties (match-end 4) (match-beginning 5)
  3199. '(invisible org-link))
  3200. (add-text-properties (match-beginning 3) (match-end 3)
  3201. '(invisible org-link)))))
  3202. (backward-char 1))
  3203. rtn))
  3204. (defun org-emphasize (&optional char)
  3205. "Insert or change an emphasis, i.e. a font like bold or italic.
  3206. If there is an active region, change that region to a new emphasis.
  3207. If there is no region, just insert the marker characters and position
  3208. the cursor between them.
  3209. CHAR should be either the marker character, or the first character of the
  3210. HTML tag associated with that emphasis. If CHAR is a space, the means
  3211. to remove the emphasis of the selected region.
  3212. If char is not given (for example in an interactive call) it
  3213. will be prompted for."
  3214. (interactive)
  3215. (let ((eal org-emphasis-alist) e det
  3216. (erc org-emphasis-regexp-components)
  3217. (prompt "")
  3218. (string "") beg end move tag c s)
  3219. (if (org-region-active-p)
  3220. (setq beg (region-beginning) end (region-end)
  3221. string (buffer-substring beg end))
  3222. (setq move t))
  3223. (while (setq e (pop eal))
  3224. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3225. c (aref tag 0))
  3226. (push (cons c (string-to-char (car e))) det)
  3227. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3228. (substring tag 1)))))
  3229. (unless char
  3230. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3231. (setq char (read-char-exclusive)))
  3232. (setq char (or (cdr (assoc char det)) char))
  3233. (if (equal char ?\ )
  3234. (setq s "" move nil)
  3235. (unless (assoc (char-to-string char) org-emphasis-alist)
  3236. (error "No such emphasis marker: \"%c\"" char))
  3237. (setq s (char-to-string char)))
  3238. (while (and (> (length string) 1)
  3239. (equal (substring string 0 1) (substring string -1))
  3240. (assoc (substring string 0 1) org-emphasis-alist))
  3241. (setq string (substring string 1 -1)))
  3242. (setq string (concat s string s))
  3243. (if beg (delete-region beg end))
  3244. (unless (or (bolp)
  3245. (string-match (concat "[" (nth 0 erc) "\n]")
  3246. (char-to-string (char-before (point)))))
  3247. (insert " "))
  3248. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3249. (char-to-string (char-after (point))))
  3250. (insert " ") (backward-char 1))
  3251. (insert string)
  3252. (and move (backward-char 1))))
  3253. (defconst org-nonsticky-props
  3254. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3255. (defun org-activate-plain-links (limit)
  3256. "Run through the buffer and add overlays to links."
  3257. (catch 'exit
  3258. (let (f)
  3259. (while (re-search-forward org-plain-link-re limit t)
  3260. (setq f (get-text-property (match-beginning 0) 'face))
  3261. (if (or (eq f 'org-tag)
  3262. (and (listp f) (memq 'org-tag f)))
  3263. nil
  3264. (add-text-properties (match-beginning 0) (match-end 0)
  3265. (list 'mouse-face 'highlight
  3266. 'rear-nonsticky org-nonsticky-props
  3267. 'keymap org-mouse-map
  3268. ))
  3269. (throw 'exit t))))))
  3270. (defun org-activate-code (limit)
  3271. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3272. (unless (get-text-property (match-beginning 1) 'face)
  3273. (remove-text-properties (match-beginning 0) (match-end 0)
  3274. '(display t invisible t intangible t))
  3275. t)))
  3276. (defun org-activate-angle-links (limit)
  3277. "Run through the buffer and add overlays to links."
  3278. (if (re-search-forward org-angle-link-re limit t)
  3279. (progn
  3280. (add-text-properties (match-beginning 0) (match-end 0)
  3281. (list 'mouse-face 'highlight
  3282. 'rear-nonsticky org-nonsticky-props
  3283. 'keymap org-mouse-map
  3284. ))
  3285. t)))
  3286. (defun org-activate-bracket-links (limit)
  3287. "Run through the buffer and add overlays to bracketed links."
  3288. (if (re-search-forward org-bracket-link-regexp limit t)
  3289. (let* ((help (concat "LINK: "
  3290. (org-match-string-no-properties 1)))
  3291. ;; FIXME: above we should remove the escapes.
  3292. ;; but that requires another match, protecting match data,
  3293. ;; a lot of overhead for font-lock.
  3294. (ip (org-maybe-intangible
  3295. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3296. 'keymap org-mouse-map 'mouse-face 'highlight
  3297. 'font-lock-multiline t 'help-echo help)))
  3298. (vp (list 'rear-nonsticky org-nonsticky-props
  3299. 'keymap org-mouse-map 'mouse-face 'highlight
  3300. ' font-lock-multiline t 'help-echo help)))
  3301. ;; We need to remove the invisible property here. Table narrowing
  3302. ;; may have made some of this invisible.
  3303. (remove-text-properties (match-beginning 0) (match-end 0)
  3304. '(invisible nil))
  3305. (if (match-end 3)
  3306. (progn
  3307. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3308. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3309. (add-text-properties (match-end 3) (match-end 0) ip))
  3310. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3311. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3312. (add-text-properties (match-end 1) (match-end 0) ip))
  3313. t)))
  3314. (defun org-activate-dates (limit)
  3315. "Run through the buffer and add overlays to dates."
  3316. (if (re-search-forward org-tsr-regexp-both limit t)
  3317. (progn
  3318. (add-text-properties (match-beginning 0) (match-end 0)
  3319. (list 'mouse-face 'highlight
  3320. 'rear-nonsticky org-nonsticky-props
  3321. 'keymap org-mouse-map))
  3322. (when org-display-custom-times
  3323. (if (match-end 3)
  3324. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3325. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3326. t)))
  3327. (defvar org-target-link-regexp nil
  3328. "Regular expression matching radio targets in plain text.")
  3329. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3330. "Regular expression matching a link target.")
  3331. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3332. "Regular expression matching a radio target.")
  3333. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3334. "Regular expression matching any target.")
  3335. (defun org-activate-target-links (limit)
  3336. "Run through the buffer and add overlays to target matches."
  3337. (when org-target-link-regexp
  3338. (let ((case-fold-search t))
  3339. (if (re-search-forward org-target-link-regexp limit t)
  3340. (progn
  3341. (add-text-properties (match-beginning 0) (match-end 0)
  3342. (list 'mouse-face 'highlight
  3343. 'rear-nonsticky org-nonsticky-props
  3344. 'keymap org-mouse-map
  3345. 'help-echo "Radio target link"
  3346. 'org-linked-text t))
  3347. t)))))
  3348. (defun org-update-radio-target-regexp ()
  3349. "Find all radio targets in this file and update the regular expression."
  3350. (interactive)
  3351. (when (memq 'radio org-activate-links)
  3352. (setq org-target-link-regexp
  3353. (org-make-target-link-regexp (org-all-targets 'radio)))
  3354. (org-restart-font-lock)))
  3355. (defun org-hide-wide-columns (limit)
  3356. (let (s e)
  3357. (setq s (text-property-any (point) (or limit (point-max))
  3358. 'org-cwidth t))
  3359. (when s
  3360. (setq e (next-single-property-change s 'org-cwidth))
  3361. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3362. (goto-char e)
  3363. t)))
  3364. (defvar org-latex-and-specials-regexp nil
  3365. "Regular expression for highlighting export special stuff.")
  3366. (defvar org-match-substring-regexp)
  3367. (defvar org-match-substring-with-braces-regexp)
  3368. (defvar org-export-html-special-string-regexps)
  3369. (defun org-compute-latex-and-specials-regexp ()
  3370. "Compute regular expression for stuff treated specially by exporters."
  3371. (if (not org-highlight-latex-fragments-and-specials)
  3372. (org-set-local 'org-latex-and-specials-regexp nil)
  3373. (require 'org-exp)
  3374. (let*
  3375. ((matchers (plist-get org-format-latex-options :matchers))
  3376. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3377. org-latex-regexps)))
  3378. (options (org-combine-plists (org-default-export-plist)
  3379. (org-infile-export-plist)))
  3380. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3381. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3382. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3383. (org-export-html-expand (plist-get options :expand-quoted-html))
  3384. (org-export-with-special-strings (plist-get options :special-strings))
  3385. (re-sub
  3386. (cond
  3387. ((equal org-export-with-sub-superscripts '{})
  3388. (list org-match-substring-with-braces-regexp))
  3389. (org-export-with-sub-superscripts
  3390. (list org-match-substring-regexp))
  3391. (t nil)))
  3392. (re-latex
  3393. (if org-export-with-LaTeX-fragments
  3394. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3395. (re-macros
  3396. (if org-export-with-TeX-macros
  3397. (list (concat "\\\\"
  3398. (regexp-opt
  3399. (append (mapcar 'car org-html-entities)
  3400. (if (boundp 'org-latex-entities)
  3401. org-latex-entities nil))
  3402. 'words))) ; FIXME
  3403. ))
  3404. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3405. (re-special (if org-export-with-special-strings
  3406. (mapcar (lambda (x) (car x))
  3407. org-export-html-special-string-regexps)))
  3408. (re-rest
  3409. (delq nil
  3410. (list
  3411. (if org-export-html-expand "@<[^>\n]+>")
  3412. ))))
  3413. (org-set-local
  3414. 'org-latex-and-specials-regexp
  3415. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3416. re-rest) "\\|")))))
  3417. (defface org-latex-and-export-specials
  3418. (let ((font (cond ((assq :inherit custom-face-attributes)
  3419. '(:inherit underline))
  3420. (t '(:underline t)))))
  3421. `((((class grayscale) (background light))
  3422. (:foreground "DimGray" ,@font))
  3423. (((class grayscale) (background dark))
  3424. (:foreground "LightGray" ,@font))
  3425. (((class color) (background light))
  3426. (:foreground "SaddleBrown"))
  3427. (((class color) (background dark))
  3428. (:foreground "burlywood"))
  3429. (t (,@font))))
  3430. "Face used to highlight math latex and other special exporter stuff."
  3431. :group 'org-faces)
  3432. (defun org-do-latex-and-special-faces (limit)
  3433. "Run through the buffer and add overlays to links."
  3434. (when org-latex-and-specials-regexp
  3435. (let (rtn d)
  3436. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3437. limit t))
  3438. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3439. 'face))
  3440. '(org-code org-verbatim underline)))
  3441. (progn
  3442. (setq rtn t
  3443. d (cond ((member (char-after (1+ (match-beginning 0)))
  3444. '(?_ ?^)) 1)
  3445. (t 0)))
  3446. (font-lock-prepend-text-property
  3447. (+ d (match-beginning 0)) (match-end 0)
  3448. 'face 'org-latex-and-export-specials)
  3449. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3450. '(font-lock-multiline t)))))
  3451. rtn)))
  3452. (defun org-restart-font-lock ()
  3453. "Restart font-lock-mode, to force refontification."
  3454. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3455. (font-lock-mode -1)
  3456. (font-lock-mode 1)))
  3457. (defun org-all-targets (&optional radio)
  3458. "Return a list of all targets in this file.
  3459. With optional argument RADIO, only find radio targets."
  3460. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3461. rtn)
  3462. (save-excursion
  3463. (goto-char (point-min))
  3464. (while (re-search-forward re nil t)
  3465. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3466. rtn)))
  3467. (defun org-make-target-link-regexp (targets)
  3468. "Make regular expression matching all strings in TARGETS.
  3469. The regular expression finds the targets also if there is a line break
  3470. between words."
  3471. (and targets
  3472. (concat
  3473. "\\<\\("
  3474. (mapconcat
  3475. (lambda (x)
  3476. (while (string-match " +" x)
  3477. (setq x (replace-match "\\s-+" t t x)))
  3478. x)
  3479. targets
  3480. "\\|")
  3481. "\\)\\>")))
  3482. (defun org-activate-tags (limit)
  3483. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3484. (progn
  3485. (add-text-properties (match-beginning 1) (match-end 1)
  3486. (list 'mouse-face 'highlight
  3487. 'rear-nonsticky org-nonsticky-props
  3488. 'keymap org-mouse-map))
  3489. t)))
  3490. (defun org-outline-level ()
  3491. (save-excursion
  3492. (looking-at outline-regexp)
  3493. (if (match-beginning 1)
  3494. (+ (org-get-string-indentation (match-string 1)) 1000)
  3495. (1- (- (match-end 0) (match-beginning 0))))))
  3496. (defvar org-font-lock-keywords nil)
  3497. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  3498. "Regular expression matching a property line.")
  3499. (defun org-set-font-lock-defaults ()
  3500. (let* ((em org-fontify-emphasized-text)
  3501. (lk org-activate-links)
  3502. (org-font-lock-extra-keywords
  3503. (list
  3504. ;; Headlines
  3505. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3506. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3507. ;; Table lines
  3508. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3509. (1 'org-table t))
  3510. ;; Table internals
  3511. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3512. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3513. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3514. ;; Drawers
  3515. (list org-drawer-regexp '(0 'org-special-keyword t))
  3516. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3517. ;; Properties
  3518. (list org-property-re
  3519. '(1 'org-special-keyword t)
  3520. '(3 'org-property-value t))
  3521. (if org-format-transports-properties-p
  3522. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3523. ;; Links
  3524. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3525. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3526. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3527. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3528. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3529. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3530. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3531. '(org-hide-wide-columns (0 nil append))
  3532. ;; TODO lines
  3533. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3534. '(1 (org-get-todo-face 1) t))
  3535. ;; DONE
  3536. (if org-fontify-done-headline
  3537. (list (concat "^[*]+ +\\<\\("
  3538. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3539. "\\)\\(.*\\)")
  3540. '(2 'org-headline-done t))
  3541. nil)
  3542. ;; Priorities
  3543. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3544. ;; Special keywords
  3545. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3546. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3547. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3548. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3549. ;; Emphasis
  3550. (if em
  3551. (if (featurep 'xemacs)
  3552. '(org-do-emphasis-faces (0 nil append))
  3553. '(org-do-emphasis-faces)))
  3554. ;; Checkboxes
  3555. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3556. 2 'bold prepend)
  3557. (if org-provide-checkbox-statistics
  3558. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3559. (0 (org-get-checkbox-statistics-face) t)))
  3560. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3561. '(1 'org-archived prepend))
  3562. ;; Specials
  3563. '(org-do-latex-and-special-faces)
  3564. ;; Code
  3565. '(org-activate-code (1 'org-code t))
  3566. ;; COMMENT
  3567. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3568. "\\|" org-quote-string "\\)\\>")
  3569. '(1 'org-special-keyword t))
  3570. '("^#.*" (0 'font-lock-comment-face t))
  3571. )))
  3572. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3573. ;; Now set the full font-lock-keywords
  3574. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3575. (org-set-local 'font-lock-defaults
  3576. '(org-font-lock-keywords t nil nil backward-paragraph))
  3577. (kill-local-variable 'font-lock-keywords) nil))
  3578. (defvar org-m nil)
  3579. (defvar org-l nil)
  3580. (defvar org-f nil)
  3581. (defun org-get-level-face (n)
  3582. "Get the right face for match N in font-lock matching of healdines."
  3583. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3584. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3585. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3586. (cond
  3587. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3588. ((eq n 2) org-f)
  3589. (t (if org-level-color-stars-only nil org-f))))
  3590. (defun org-get-todo-face (kwd)
  3591. "Get the right face for a TODO keyword KWD.
  3592. If KWD is a number, get the corresponding match group."
  3593. (if (numberp kwd) (setq kwd (match-string kwd)))
  3594. (or (cdr (assoc kwd org-todo-keyword-faces))
  3595. (and (member kwd org-done-keywords) 'org-done)
  3596. 'org-todo))
  3597. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3598. "Remove fontification and activation overlays from links."
  3599. (font-lock-default-unfontify-region beg end)
  3600. (let* ((buffer-undo-list t)
  3601. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3602. (inhibit-modification-hooks t)
  3603. deactivate-mark buffer-file-name buffer-file-truename)
  3604. (remove-text-properties beg end
  3605. '(mouse-face t keymap t org-linked-text t
  3606. invisible t intangible t))))
  3607. ;;;; Visibility cycling, including org-goto and indirect buffer
  3608. ;;; Cycling
  3609. (defvar org-cycle-global-status nil)
  3610. (make-variable-buffer-local 'org-cycle-global-status)
  3611. (defvar org-cycle-subtree-status nil)
  3612. (make-variable-buffer-local 'org-cycle-subtree-status)
  3613. ;;;###autoload
  3614. (defun org-cycle (&optional arg)
  3615. "Visibility cycling for Org-mode.
  3616. - When this function is called with a prefix argument, rotate the entire
  3617. buffer through 3 states (global cycling)
  3618. 1. OVERVIEW: Show only top-level headlines.
  3619. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3620. 3. SHOW ALL: Show everything.
  3621. - When point is at the beginning of a headline, rotate the subtree started
  3622. by this line through 3 different states (local cycling)
  3623. 1. FOLDED: Only the main headline is shown.
  3624. 2. CHILDREN: The main headline and the direct children are shown.
  3625. From this state, you can move to one of the children
  3626. and zoom in further.
  3627. 3. SUBTREE: Show the entire subtree, including body text.
  3628. - When there is a numeric prefix, go up to a heading with level ARG, do
  3629. a `show-subtree' and return to the previous cursor position. If ARG
  3630. is negative, go up that many levels.
  3631. - When point is not at the beginning of a headline, execute
  3632. `indent-relative', like TAB normally does. See the option
  3633. `org-cycle-emulate-tab' for details.
  3634. - Special case: if point is at the beginning of the buffer and there is
  3635. no headline in line 1, this function will act as if called with prefix arg.
  3636. But only if also the variable `org-cycle-global-at-bob' is t."
  3637. (interactive "P")
  3638. (org-load-modules-maybe)
  3639. (let* ((outline-regexp
  3640. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3641. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3642. outline-regexp))
  3643. (bob-special (and org-cycle-global-at-bob (bobp)
  3644. (not (looking-at outline-regexp))))
  3645. (org-cycle-hook
  3646. (if bob-special
  3647. (delq 'org-optimize-window-after-visibility-change
  3648. (copy-sequence org-cycle-hook))
  3649. org-cycle-hook))
  3650. (pos (point)))
  3651. (if (or bob-special (equal arg '(4)))
  3652. ;; special case: use global cycling
  3653. (setq arg t))
  3654. (cond
  3655. ((org-at-table-p 'any)
  3656. ;; Enter the table or move to the next field in the table
  3657. (or (org-table-recognize-table.el)
  3658. (progn
  3659. (if arg (org-table-edit-field t)
  3660. (org-table-justify-field-maybe)
  3661. (call-interactively 'org-table-next-field)))))
  3662. ((eq arg t) ;; Global cycling
  3663. (cond
  3664. ((and (eq last-command this-command)
  3665. (eq org-cycle-global-status 'overview))
  3666. ;; We just created the overview - now do table of contents
  3667. ;; This can be slow in very large buffers, so indicate action
  3668. (message "CONTENTS...")
  3669. (org-content)
  3670. (message "CONTENTS...done")
  3671. (setq org-cycle-global-status 'contents)
  3672. (run-hook-with-args 'org-cycle-hook 'contents))
  3673. ((and (eq last-command this-command)
  3674. (eq org-cycle-global-status 'contents))
  3675. ;; We just showed the table of contents - now show everything
  3676. (show-all)
  3677. (message "SHOW ALL")
  3678. (setq org-cycle-global-status 'all)
  3679. (run-hook-with-args 'org-cycle-hook 'all))
  3680. (t
  3681. ;; Default action: go to overview
  3682. (org-overview)
  3683. (message "OVERVIEW")
  3684. (setq org-cycle-global-status 'overview)
  3685. (run-hook-with-args 'org-cycle-hook 'overview))))
  3686. ((and org-drawers org-drawer-regexp
  3687. (save-excursion
  3688. (beginning-of-line 1)
  3689. (looking-at org-drawer-regexp)))
  3690. ;; Toggle block visibility
  3691. (org-flag-drawer
  3692. (not (get-char-property (match-end 0) 'invisible))))
  3693. ((integerp arg)
  3694. ;; Show-subtree, ARG levels up from here.
  3695. (save-excursion
  3696. (org-back-to-heading)
  3697. (outline-up-heading (if (< arg 0) (- arg)
  3698. (- (funcall outline-level) arg)))
  3699. (org-show-subtree)))
  3700. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3701. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3702. ;; At a heading: rotate between three different views
  3703. (org-back-to-heading)
  3704. (let ((goal-column 0) eoh eol eos)
  3705. ;; First, some boundaries
  3706. (save-excursion
  3707. (org-back-to-heading)
  3708. (save-excursion
  3709. (beginning-of-line 2)
  3710. (while (and (not (eobp)) ;; this is like `next-line'
  3711. (get-char-property (1- (point)) 'invisible))
  3712. (beginning-of-line 2)) (setq eol (point)))
  3713. (outline-end-of-heading) (setq eoh (point))
  3714. (org-end-of-subtree t)
  3715. (unless (eobp)
  3716. (skip-chars-forward " \t\n")
  3717. (beginning-of-line 1) ; in case this is an item
  3718. )
  3719. (setq eos (1- (point))))
  3720. ;; Find out what to do next and set `this-command'
  3721. (cond
  3722. ((= eos eoh)
  3723. ;; Nothing is hidden behind this heading
  3724. (message "EMPTY ENTRY")
  3725. (setq org-cycle-subtree-status nil)
  3726. (save-excursion
  3727. (goto-char eos)
  3728. (outline-next-heading)
  3729. (if (org-invisible-p) (org-flag-heading nil))))
  3730. ((or (>= eol eos)
  3731. (not (string-match "\\S-" (buffer-substring eol eos))))
  3732. ;; Entire subtree is hidden in one line: open it
  3733. (org-show-entry)
  3734. (show-children)
  3735. (message "CHILDREN")
  3736. (save-excursion
  3737. (goto-char eos)
  3738. (outline-next-heading)
  3739. (if (org-invisible-p) (org-flag-heading nil)))
  3740. (setq org-cycle-subtree-status 'children)
  3741. (run-hook-with-args 'org-cycle-hook 'children))
  3742. ((and (eq last-command this-command)
  3743. (eq org-cycle-subtree-status 'children))
  3744. ;; We just showed the children, now show everything.
  3745. (org-show-subtree)
  3746. (message "SUBTREE")
  3747. (setq org-cycle-subtree-status 'subtree)
  3748. (run-hook-with-args 'org-cycle-hook 'subtree))
  3749. (t
  3750. ;; Default action: hide the subtree.
  3751. (hide-subtree)
  3752. (message "FOLDED")
  3753. (setq org-cycle-subtree-status 'folded)
  3754. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3755. ;; TAB emulation
  3756. (buffer-read-only (org-back-to-heading))
  3757. ((org-try-cdlatex-tab))
  3758. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3759. (or (not (bolp))
  3760. (not (looking-at outline-regexp))))
  3761. (call-interactively (global-key-binding "\t")))
  3762. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3763. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3764. (or (and (eq org-cycle-emulate-tab 'white)
  3765. (= (match-end 0) (point-at-eol)))
  3766. (and (eq org-cycle-emulate-tab 'whitestart)
  3767. (>= (match-end 0) pos))))
  3768. t
  3769. (eq org-cycle-emulate-tab t))
  3770. (call-interactively (global-key-binding "\t")))
  3771. (t (save-excursion
  3772. (org-back-to-heading)
  3773. (org-cycle))))))
  3774. ;;;###autoload
  3775. (defun org-global-cycle (&optional arg)
  3776. "Cycle the global visibility. For details see `org-cycle'."
  3777. (interactive "P")
  3778. (let ((org-cycle-include-plain-lists
  3779. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3780. (if (integerp arg)
  3781. (progn
  3782. (show-all)
  3783. (hide-sublevels arg)
  3784. (setq org-cycle-global-status 'contents))
  3785. (org-cycle '(4)))))
  3786. (defun org-overview ()
  3787. "Switch to overview mode, shoing only top-level headlines.
  3788. Really, this shows all headlines with level equal or greater than the level
  3789. of the first headline in the buffer. This is important, because if the
  3790. first headline is not level one, then (hide-sublevels 1) gives confusing
  3791. results."
  3792. (interactive)
  3793. (let ((level (save-excursion
  3794. (goto-char (point-min))
  3795. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3796. (progn
  3797. (goto-char (match-beginning 0))
  3798. (funcall outline-level))))))
  3799. (and level (hide-sublevels level))))
  3800. (defun org-content (&optional arg)
  3801. "Show all headlines in the buffer, like a table of contents.
  3802. With numerical argument N, show content up to level N."
  3803. (interactive "P")
  3804. (save-excursion
  3805. ;; Visit all headings and show their offspring
  3806. (and (integerp arg) (org-overview))
  3807. (goto-char (point-max))
  3808. (catch 'exit
  3809. (while (and (progn (condition-case nil
  3810. (outline-previous-visible-heading 1)
  3811. (error (goto-char (point-min))))
  3812. t)
  3813. (looking-at outline-regexp))
  3814. (if (integerp arg)
  3815. (show-children (1- arg))
  3816. (show-branches))
  3817. (if (bobp) (throw 'exit nil))))))
  3818. (defun org-optimize-window-after-visibility-change (state)
  3819. "Adjust the window after a change in outline visibility.
  3820. This function is the default value of the hook `org-cycle-hook'."
  3821. (when (get-buffer-window (current-buffer))
  3822. (cond
  3823. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3824. ; ((eq state 'overview) (org-beginning-of-line))
  3825. ((eq state 'content) nil)
  3826. ((eq state 'all) nil)
  3827. ((eq state 'folded) nil)
  3828. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3829. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3830. (defun org-compact-display-after-subtree-move ()
  3831. (let (beg end)
  3832. (save-excursion
  3833. (if (org-up-heading-safe)
  3834. (progn
  3835. (hide-subtree)
  3836. (show-entry)
  3837. (show-children)
  3838. (org-cycle-show-empty-lines 'children)
  3839. (org-cycle-hide-drawers 'children))
  3840. (org-overview)))))
  3841. (defun org-cycle-show-empty-lines (state)
  3842. "Show empty lines above all visible headlines.
  3843. The region to be covered depends on STATE when called through
  3844. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3845. entire buffer covered. Note that an empty line is only shown if there
  3846. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3847. (when (> org-cycle-separator-lines 0)
  3848. (save-excursion
  3849. (let* ((n org-cycle-separator-lines)
  3850. (re (cond
  3851. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3852. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3853. (t (let ((ns (number-to-string (- n 2))))
  3854. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3855. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3856. beg end)
  3857. (cond
  3858. ((memq state '(overview contents t))
  3859. (setq beg (point-min) end (point-max)))
  3860. ((memq state '(children folded))
  3861. (setq beg (point) end (progn (org-end-of-subtree t t)
  3862. (beginning-of-line 2)
  3863. (point)))))
  3864. (when beg
  3865. (goto-char beg)
  3866. (while (re-search-forward re end t)
  3867. (if (not (get-char-property (match-end 1) 'invisible))
  3868. (outline-flag-region
  3869. (match-beginning 1) (match-end 1) nil)))))))
  3870. ;; Never hide empty lines at the end of the file.
  3871. (save-excursion
  3872. (goto-char (point-max))
  3873. (outline-previous-heading)
  3874. (outline-end-of-heading)
  3875. (if (and (looking-at "[ \t\n]+")
  3876. (= (match-end 0) (point-max)))
  3877. (outline-flag-region (point) (match-end 0) nil))))
  3878. (defun org-subtree-end-visible-p ()
  3879. "Is the end of the current subtree visible?"
  3880. (pos-visible-in-window-p
  3881. (save-excursion (org-end-of-subtree t) (point))))
  3882. (defun org-first-headline-recenter (&optional N)
  3883. "Move cursor to the first headline and recenter the headline.
  3884. Optional argument N means, put the headline into the Nth line of the window."
  3885. (goto-char (point-min))
  3886. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3887. (beginning-of-line)
  3888. (recenter (prefix-numeric-value N))))
  3889. ;;; Org-goto
  3890. (defvar org-goto-window-configuration nil)
  3891. (defvar org-goto-marker nil)
  3892. (defvar org-goto-map
  3893. (let ((map (make-sparse-keymap)))
  3894. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3895. (while (setq cmd (pop cmds))
  3896. (substitute-key-definition cmd cmd map global-map)))
  3897. (suppress-keymap map)
  3898. (org-defkey map "\C-m" 'org-goto-ret)
  3899. (org-defkey map [(return)] 'org-goto-ret)
  3900. (org-defkey map [(left)] 'org-goto-left)
  3901. (org-defkey map [(right)] 'org-goto-right)
  3902. (org-defkey map [(control ?g)] 'org-goto-quit)
  3903. (org-defkey map "\C-i" 'org-cycle)
  3904. (org-defkey map [(tab)] 'org-cycle)
  3905. (org-defkey map [(down)] 'outline-next-visible-heading)
  3906. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3907. (if org-goto-auto-isearch
  3908. (if (fboundp 'define-key-after)
  3909. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3910. nil)
  3911. (org-defkey map "q" 'org-goto-quit)
  3912. (org-defkey map "n" 'outline-next-visible-heading)
  3913. (org-defkey map "p" 'outline-previous-visible-heading)
  3914. (org-defkey map "f" 'outline-forward-same-level)
  3915. (org-defkey map "b" 'outline-backward-same-level)
  3916. (org-defkey map "u" 'outline-up-heading))
  3917. (org-defkey map "/" 'org-occur)
  3918. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3919. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3920. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3921. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3922. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3923. map))
  3924. (defconst org-goto-help
  3925. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3926. RET=jump to location [Q]uit and return to previous location
  3927. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3928. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3929. (defun org-goto (&optional alternative-interface)
  3930. "Look up a different location in the current file, keeping current visibility.
  3931. When you want look-up or go to a different location in a document, the
  3932. fastest way is often to fold the entire buffer and then dive into the tree.
  3933. This method has the disadvantage, that the previous location will be folded,
  3934. which may not be what you want.
  3935. This command works around this by showing a copy of the current buffer
  3936. in an indirect buffer, in overview mode. You can dive into the tree in
  3937. that copy, use org-occur and incremental search to find a location.
  3938. When pressing RET or `Q', the command returns to the original buffer in
  3939. which the visibility is still unchanged. After RET is will also jump to
  3940. the location selected in the indirect buffer and expose the
  3941. the headline hierarchy above."
  3942. (interactive "P")
  3943. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3944. (org-refile-use-outline-path t)
  3945. (interface
  3946. (if (not alternative-interface)
  3947. org-goto-interface
  3948. (if (eq org-goto-interface 'outline)
  3949. 'outline-path-completion
  3950. 'outline)))
  3951. (org-goto-start-pos (point))
  3952. (selected-point
  3953. (if (eq interface 'outline)
  3954. (car (org-get-location (current-buffer) org-goto-help))
  3955. (nth 3 (org-refile-get-location "Goto: ")))))
  3956. (if selected-point
  3957. (progn
  3958. (org-mark-ring-push org-goto-start-pos)
  3959. (goto-char selected-point)
  3960. (if (or (org-invisible-p) (org-invisible-p2))
  3961. (org-show-context 'org-goto)))
  3962. (message "Quit"))))
  3963. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3964. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3965. (defvar org-goto-local-auto-isearch-map) ; defined below
  3966. (defun org-get-location (buf help)
  3967. "Let the user select a location in the Org-mode buffer BUF.
  3968. This function uses a recursive edit. It returns the selected position
  3969. or nil."
  3970. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3971. (isearch-hide-immediately nil)
  3972. (isearch-search-fun-function
  3973. (lambda () 'org-goto-local-search-forward-headings))
  3974. (org-goto-selected-point org-goto-exit-command))
  3975. (save-excursion
  3976. (save-window-excursion
  3977. (delete-other-windows)
  3978. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3979. (switch-to-buffer
  3980. (condition-case nil
  3981. (make-indirect-buffer (current-buffer) "*org-goto*")
  3982. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3983. (with-output-to-temp-buffer "*Help*"
  3984. (princ help))
  3985. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3986. (setq buffer-read-only nil)
  3987. (let ((org-startup-truncated t)
  3988. (org-startup-folded nil)
  3989. (org-startup-align-all-tables nil))
  3990. (org-mode)
  3991. (org-overview))
  3992. (setq buffer-read-only t)
  3993. (if (and (boundp 'org-goto-start-pos)
  3994. (integer-or-marker-p org-goto-start-pos))
  3995. (let ((org-show-hierarchy-above t)
  3996. (org-show-siblings t)
  3997. (org-show-following-heading t))
  3998. (goto-char org-goto-start-pos)
  3999. (and (org-invisible-p) (org-show-context)))
  4000. (goto-char (point-min)))
  4001. (org-beginning-of-line)
  4002. (message "Select location and press RET")
  4003. (use-local-map org-goto-map)
  4004. (recursive-edit)
  4005. ))
  4006. (kill-buffer "*org-goto*")
  4007. (cons org-goto-selected-point org-goto-exit-command)))
  4008. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4009. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4010. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4011. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4012. (defun org-goto-local-search-forward-headings (string bound noerror)
  4013. "Search and make sure that anu matches are in headlines."
  4014. (catch 'return
  4015. (while (search-forward string bound noerror)
  4016. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4017. (and (member :headline context)
  4018. (not (member :tags context))))
  4019. (throw 'return (point))))))
  4020. (defun org-goto-local-auto-isearch ()
  4021. "Start isearch."
  4022. (interactive)
  4023. (goto-char (point-min))
  4024. (let ((keys (this-command-keys)))
  4025. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4026. (isearch-mode t)
  4027. (isearch-process-search-char (string-to-char keys)))))
  4028. (defun org-goto-ret (&optional arg)
  4029. "Finish `org-goto' by going to the new location."
  4030. (interactive "P")
  4031. (setq org-goto-selected-point (point)
  4032. org-goto-exit-command 'return)
  4033. (throw 'exit nil))
  4034. (defun org-goto-left ()
  4035. "Finish `org-goto' by going to the new location."
  4036. (interactive)
  4037. (if (org-on-heading-p)
  4038. (progn
  4039. (beginning-of-line 1)
  4040. (setq org-goto-selected-point (point)
  4041. org-goto-exit-command 'left)
  4042. (throw 'exit nil))
  4043. (error "Not on a heading")))
  4044. (defun org-goto-right ()
  4045. "Finish `org-goto' by going to the new location."
  4046. (interactive)
  4047. (if (org-on-heading-p)
  4048. (progn
  4049. (setq org-goto-selected-point (point)
  4050. org-goto-exit-command 'right)
  4051. (throw 'exit nil))
  4052. (error "Not on a heading")))
  4053. (defun org-goto-quit ()
  4054. "Finish `org-goto' without cursor motion."
  4055. (interactive)
  4056. (setq org-goto-selected-point nil)
  4057. (setq org-goto-exit-command 'quit)
  4058. (throw 'exit nil))
  4059. ;;; Indirect buffer display of subtrees
  4060. (defvar org-indirect-dedicated-frame nil
  4061. "This is the frame being used for indirect tree display.")
  4062. (defvar org-last-indirect-buffer nil)
  4063. (defun org-tree-to-indirect-buffer (&optional arg)
  4064. "Create indirect buffer and narrow it to current subtree.
  4065. With numerical prefix ARG, go up to this level and then take that tree.
  4066. If ARG is negative, go up that many levels.
  4067. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4068. indirect buffer previously made with this command, to avoid proliferation of
  4069. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4070. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4071. is kept so that you can work with several indirect buffers at the same time.
  4072. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4073. requests that a new frame be made for the new buffer, so that the dedicated
  4074. frame is not changed."
  4075. (interactive "P")
  4076. (let ((cbuf (current-buffer))
  4077. (cwin (selected-window))
  4078. (pos (point))
  4079. beg end level heading ibuf)
  4080. (save-excursion
  4081. (org-back-to-heading t)
  4082. (when (numberp arg)
  4083. (setq level (org-outline-level))
  4084. (if (< arg 0) (setq arg (+ level arg)))
  4085. (while (> (setq level (org-outline-level)) arg)
  4086. (outline-up-heading 1 t)))
  4087. (setq beg (point)
  4088. heading (org-get-heading))
  4089. (org-end-of-subtree t) (setq end (point)))
  4090. (if (and (buffer-live-p org-last-indirect-buffer)
  4091. (not (eq org-indirect-buffer-display 'new-frame))
  4092. (not arg))
  4093. (kill-buffer org-last-indirect-buffer))
  4094. (setq ibuf (org-get-indirect-buffer cbuf)
  4095. org-last-indirect-buffer ibuf)
  4096. (cond
  4097. ((or (eq org-indirect-buffer-display 'new-frame)
  4098. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4099. (select-frame (make-frame))
  4100. (delete-other-windows)
  4101. (switch-to-buffer ibuf)
  4102. (org-set-frame-title heading))
  4103. ((eq org-indirect-buffer-display 'dedicated-frame)
  4104. (raise-frame
  4105. (select-frame (or (and org-indirect-dedicated-frame
  4106. (frame-live-p org-indirect-dedicated-frame)
  4107. org-indirect-dedicated-frame)
  4108. (setq org-indirect-dedicated-frame (make-frame)))))
  4109. (delete-other-windows)
  4110. (switch-to-buffer ibuf)
  4111. (org-set-frame-title (concat "Indirect: " heading)))
  4112. ((eq org-indirect-buffer-display 'current-window)
  4113. (switch-to-buffer ibuf))
  4114. ((eq org-indirect-buffer-display 'other-window)
  4115. (pop-to-buffer ibuf))
  4116. (t (error "Invalid value.")))
  4117. (if (featurep 'xemacs)
  4118. (save-excursion (org-mode) (turn-on-font-lock)))
  4119. (narrow-to-region beg end)
  4120. (show-all)
  4121. (goto-char pos)
  4122. (and (window-live-p cwin) (select-window cwin))))
  4123. (defun org-get-indirect-buffer (&optional buffer)
  4124. (setq buffer (or buffer (current-buffer)))
  4125. (let ((n 1) (base (buffer-name buffer)) bname)
  4126. (while (buffer-live-p
  4127. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4128. (setq n (1+ n)))
  4129. (condition-case nil
  4130. (make-indirect-buffer buffer bname 'clone)
  4131. (error (make-indirect-buffer buffer bname)))))
  4132. (defun org-set-frame-title (title)
  4133. "Set the title of the current frame to the string TITLE."
  4134. ;; FIXME: how to name a single frame in XEmacs???
  4135. (unless (featurep 'xemacs)
  4136. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4137. ;;;; Structure editing
  4138. ;;; Inserting headlines
  4139. (defun org-insert-heading (&optional force-heading)
  4140. "Insert a new heading or item with same depth at point.
  4141. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4142. If point is at the beginning of a headline, insert a sibling before the
  4143. current headline. If point is not at the beginning, do not split the line,
  4144. but create the new hedline after the current line."
  4145. (interactive "P")
  4146. (if (= (buffer-size) 0)
  4147. (insert "\n* ")
  4148. (when (or force-heading (not (org-insert-item)))
  4149. (let* ((head (save-excursion
  4150. (condition-case nil
  4151. (progn
  4152. (org-back-to-heading)
  4153. (match-string 0))
  4154. (error "*"))))
  4155. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4156. pos)
  4157. (cond
  4158. ((and (org-on-heading-p) (bolp)
  4159. (or (bobp)
  4160. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4161. ;; insert before the current line
  4162. (open-line (if blank 2 1)))
  4163. ((and (bolp)
  4164. (or (bobp)
  4165. (save-excursion
  4166. (backward-char 1) (not (org-invisible-p)))))
  4167. ;; insert right here
  4168. nil)
  4169. (t
  4170. ;; in the middle of the line
  4171. (org-show-entry)
  4172. (let ((split
  4173. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4174. tags pos)
  4175. (if (org-on-heading-p)
  4176. (progn
  4177. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4178. (setq tags (and (match-end 2) (match-string 2)))
  4179. (and (match-end 1)
  4180. (delete-region (match-beginning 1) (match-end 1)))
  4181. (setq pos (point-at-bol))
  4182. (or split (end-of-line 1))
  4183. (delete-horizontal-space)
  4184. (newline (if blank 2 1))
  4185. (when tags
  4186. (save-excursion
  4187. (goto-char pos)
  4188. (end-of-line 1)
  4189. (insert " " tags)
  4190. (org-set-tags nil 'align))))
  4191. (or split (end-of-line 1))
  4192. (newline (if blank 2 1))))))
  4193. (insert head) (just-one-space)
  4194. (setq pos (point))
  4195. (end-of-line 1)
  4196. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4197. (run-hooks 'org-insert-heading-hook)))))
  4198. (defun org-get-heading (&optional no-tags)
  4199. "Return the heading of the current entry, without the stars."
  4200. (save-excursion
  4201. (org-back-to-heading t)
  4202. (if (looking-at
  4203. (if no-tags
  4204. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4205. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4206. (match-string 1) "")))
  4207. (defun org-insert-heading-after-current ()
  4208. "Insert a new heading with same level as current, after current subtree."
  4209. (interactive)
  4210. (org-back-to-heading)
  4211. (org-insert-heading)
  4212. (org-move-subtree-down)
  4213. (end-of-line 1))
  4214. (defun org-insert-todo-heading (arg)
  4215. "Insert a new heading with the same level and TODO state as current heading.
  4216. If the heading has no TODO state, or if the state is DONE, use the first
  4217. state (TODO by default). Also with prefix arg, force first state."
  4218. (interactive "P")
  4219. (when (not (org-insert-item 'checkbox))
  4220. (org-insert-heading)
  4221. (save-excursion
  4222. (org-back-to-heading)
  4223. (outline-previous-heading)
  4224. (looking-at org-todo-line-regexp))
  4225. (if (or arg
  4226. (not (match-beginning 2))
  4227. (member (match-string 2) org-done-keywords))
  4228. (insert (car org-todo-keywords-1) " ")
  4229. (insert (match-string 2) " "))))
  4230. (defun org-insert-subheading (arg)
  4231. "Insert a new subheading and demote it.
  4232. Works for outline headings and for plain lists alike."
  4233. (interactive "P")
  4234. (org-insert-heading arg)
  4235. (cond
  4236. ((org-on-heading-p) (org-do-demote))
  4237. ((org-at-item-p) (org-indent-item 1))))
  4238. (defun org-insert-todo-subheading (arg)
  4239. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4240. Works for outline headings and for plain lists alike."
  4241. (interactive "P")
  4242. (org-insert-todo-heading arg)
  4243. (cond
  4244. ((org-on-heading-p) (org-do-demote))
  4245. ((org-at-item-p) (org-indent-item 1))))
  4246. ;;; Promotion and Demotion
  4247. (defun org-promote-subtree ()
  4248. "Promote the entire subtree.
  4249. See also `org-promote'."
  4250. (interactive)
  4251. (save-excursion
  4252. (org-map-tree 'org-promote))
  4253. (org-fix-position-after-promote))
  4254. (defun org-demote-subtree ()
  4255. "Demote the entire subtree. See `org-demote'.
  4256. See also `org-promote'."
  4257. (interactive)
  4258. (save-excursion
  4259. (org-map-tree 'org-demote))
  4260. (org-fix-position-after-promote))
  4261. (defun org-do-promote ()
  4262. "Promote the current heading higher up the tree.
  4263. If the region is active in `transient-mark-mode', promote all headings
  4264. in the region."
  4265. (interactive)
  4266. (save-excursion
  4267. (if (org-region-active-p)
  4268. (org-map-region 'org-promote (region-beginning) (region-end))
  4269. (org-promote)))
  4270. (org-fix-position-after-promote))
  4271. (defun org-do-demote ()
  4272. "Demote the current heading lower down the tree.
  4273. If the region is active in `transient-mark-mode', demote all headings
  4274. in the region."
  4275. (interactive)
  4276. (save-excursion
  4277. (if (org-region-active-p)
  4278. (org-map-region 'org-demote (region-beginning) (region-end))
  4279. (org-demote)))
  4280. (org-fix-position-after-promote))
  4281. (defun org-fix-position-after-promote ()
  4282. "Make sure that after pro/demotion cursor position is right."
  4283. (let ((pos (point)))
  4284. (when (save-excursion
  4285. (beginning-of-line 1)
  4286. (looking-at org-todo-line-regexp)
  4287. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4288. (cond ((eobp) (insert " "))
  4289. ((eolp) (insert " "))
  4290. ((equal (char-after) ?\ ) (forward-char 1))))))
  4291. (defun org-reduced-level (l)
  4292. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4293. (defun org-get-valid-level (level &optional change)
  4294. "Rectify a level change under the influence of `org-odd-levels-only'
  4295. LEVEL is a current level, CHANGE is by how much the level should be
  4296. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4297. even level numbers will become the next higher odd number."
  4298. (if org-odd-levels-only
  4299. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4300. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4301. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4302. (max 1 (+ level change))))
  4303. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4304. (define-obsolete-function-alias 'org-get-legal-level
  4305. 'org-get-valid-level)
  4306. (define-obsolete-function-alias 'org-get-legal-level
  4307. 'org-get-valid-level "23.1"))
  4308. (defun org-promote ()
  4309. "Promote the current heading higher up the tree.
  4310. If the region is active in `transient-mark-mode', promote all headings
  4311. in the region."
  4312. (org-back-to-heading t)
  4313. (let* ((level (save-match-data (funcall outline-level)))
  4314. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4315. (diff (abs (- level (length up-head) -1))))
  4316. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4317. (replace-match up-head nil t)
  4318. ;; Fixup tag positioning
  4319. (and org-auto-align-tags (org-set-tags nil t))
  4320. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4321. (defun org-demote ()
  4322. "Demote the current heading lower down the tree.
  4323. If the region is active in `transient-mark-mode', demote all headings
  4324. in the region."
  4325. (org-back-to-heading t)
  4326. (let* ((level (save-match-data (funcall outline-level)))
  4327. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4328. (diff (abs (- level (length down-head) -1))))
  4329. (replace-match down-head nil t)
  4330. ;; Fixup tag positioning
  4331. (and org-auto-align-tags (org-set-tags nil t))
  4332. (if org-adapt-indentation (org-fixup-indentation diff))))
  4333. (defun org-map-tree (fun)
  4334. "Call FUN for every heading underneath the current one."
  4335. (org-back-to-heading)
  4336. (let ((level (funcall outline-level)))
  4337. (save-excursion
  4338. (funcall fun)
  4339. (while (and (progn
  4340. (outline-next-heading)
  4341. (> (funcall outline-level) level))
  4342. (not (eobp)))
  4343. (funcall fun)))))
  4344. (defun org-map-region (fun beg end)
  4345. "Call FUN for every heading between BEG and END."
  4346. (let ((org-ignore-region t))
  4347. (save-excursion
  4348. (setq end (copy-marker end))
  4349. (goto-char beg)
  4350. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4351. (< (point) end))
  4352. (funcall fun))
  4353. (while (and (progn
  4354. (outline-next-heading)
  4355. (< (point) end))
  4356. (not (eobp)))
  4357. (funcall fun)))))
  4358. (defun org-fixup-indentation (diff)
  4359. "Change the indentation in the current entry by DIFF
  4360. However, if any line in the current entry has no indentation, or if it
  4361. would end up with no indentation after the change, nothing at all is done."
  4362. (save-excursion
  4363. (let ((end (save-excursion (outline-next-heading)
  4364. (point-marker)))
  4365. (prohibit (if (> diff 0)
  4366. "^\\S-"
  4367. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4368. col)
  4369. (unless (save-excursion (end-of-line 1)
  4370. (re-search-forward prohibit end t))
  4371. (while (and (< (point) end)
  4372. (re-search-forward "^[ \t]+" end t))
  4373. (goto-char (match-end 0))
  4374. (setq col (current-column))
  4375. (if (< diff 0) (replace-match ""))
  4376. (indent-to (+ diff col))))
  4377. (move-marker end nil))))
  4378. (defun org-convert-to-odd-levels ()
  4379. "Convert an org-mode file with all levels allowed to one with odd levels.
  4380. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4381. level 5 etc."
  4382. (interactive)
  4383. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4384. (let ((org-odd-levels-only nil) n)
  4385. (save-excursion
  4386. (goto-char (point-min))
  4387. (while (re-search-forward "^\\*\\*+ " nil t)
  4388. (setq n (- (length (match-string 0)) 2))
  4389. (while (>= (setq n (1- n)) 0)
  4390. (org-demote))
  4391. (end-of-line 1))))))
  4392. (defun org-convert-to-oddeven-levels ()
  4393. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4394. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4395. section with an even level, conversion would destroy the structure of the file. An error
  4396. is signaled in this case."
  4397. (interactive)
  4398. (goto-char (point-min))
  4399. ;; First check if there are no even levels
  4400. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4401. (org-show-context t)
  4402. (error "Not all levels are odd in this file. Conversion not possible."))
  4403. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4404. (let ((org-odd-levels-only nil) n)
  4405. (save-excursion
  4406. (goto-char (point-min))
  4407. (while (re-search-forward "^\\*\\*+ " nil t)
  4408. (setq n (/ (1- (length (match-string 0))) 2))
  4409. (while (>= (setq n (1- n)) 0)
  4410. (org-promote))
  4411. (end-of-line 1))))))
  4412. (defun org-tr-level (n)
  4413. "Make N odd if required."
  4414. (if org-odd-levels-only (1+ (/ n 2)) n))
  4415. ;;; Vertical tree motion, cutting and pasting of subtrees
  4416. (defun org-move-subtree-up (&optional arg)
  4417. "Move the current subtree up past ARG headlines of the same level."
  4418. (interactive "p")
  4419. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4420. (defun org-move-subtree-down (&optional arg)
  4421. "Move the current subtree down past ARG headlines of the same level."
  4422. (interactive "p")
  4423. (setq arg (prefix-numeric-value arg))
  4424. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4425. 'outline-get-last-sibling))
  4426. (ins-point (make-marker))
  4427. (cnt (abs arg))
  4428. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4429. ;; Select the tree
  4430. (org-back-to-heading)
  4431. (setq beg0 (point))
  4432. (save-excursion
  4433. (setq ne-beg (org-back-over-empty-lines))
  4434. (setq beg (point)))
  4435. (save-match-data
  4436. (save-excursion (outline-end-of-heading)
  4437. (setq folded (org-invisible-p)))
  4438. (outline-end-of-subtree))
  4439. (outline-next-heading)
  4440. (setq ne-end (org-back-over-empty-lines))
  4441. (setq end (point))
  4442. (goto-char beg0)
  4443. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4444. ;; include less whitespace
  4445. (save-excursion
  4446. (goto-char beg)
  4447. (forward-line (- ne-beg ne-end))
  4448. (setq beg (point))))
  4449. ;; Find insertion point, with error handling
  4450. (while (> cnt 0)
  4451. (or (and (funcall movfunc) (looking-at outline-regexp))
  4452. (progn (goto-char beg0)
  4453. (error "Cannot move past superior level or buffer limit")))
  4454. (setq cnt (1- cnt)))
  4455. (if (> arg 0)
  4456. ;; Moving forward - still need to move over subtree
  4457. (progn (org-end-of-subtree t t)
  4458. (save-excursion
  4459. (org-back-over-empty-lines)
  4460. (or (bolp) (newline)))))
  4461. (setq ne-ins (org-back-over-empty-lines))
  4462. (move-marker ins-point (point))
  4463. (setq txt (buffer-substring beg end))
  4464. (delete-region beg end)
  4465. (outline-flag-region (1- beg) beg nil)
  4466. (outline-flag-region (1- (point)) (point) nil)
  4467. (insert txt)
  4468. (or (bolp) (insert "\n"))
  4469. (setq ins-end (point))
  4470. (goto-char ins-point)
  4471. (org-skip-whitespace)
  4472. (when (and (< arg 0)
  4473. (org-first-sibling-p)
  4474. (> ne-ins ne-beg))
  4475. ;; Move whitespace back to beginning
  4476. (save-excursion
  4477. (goto-char ins-end)
  4478. (let ((kill-whole-line t))
  4479. (kill-line (- ne-ins ne-beg)) (point)))
  4480. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4481. (move-marker ins-point nil)
  4482. (org-compact-display-after-subtree-move)
  4483. (unless folded
  4484. (org-show-entry)
  4485. (show-children)
  4486. (org-cycle-hide-drawers 'children))))
  4487. (defvar org-subtree-clip ""
  4488. "Clipboard for cut and paste of subtrees.
  4489. This is actually only a copy of the kill, because we use the normal kill
  4490. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4491. (defvar org-subtree-clip-folded nil
  4492. "Was the last copied subtree folded?
  4493. This is used to fold the tree back after pasting.")
  4494. (defun org-cut-subtree (&optional n)
  4495. "Cut the current subtree into the clipboard.
  4496. With prefix arg N, cut this many sequential subtrees.
  4497. This is a short-hand for marking the subtree and then cutting it."
  4498. (interactive "p")
  4499. (org-copy-subtree n 'cut))
  4500. (defun org-copy-subtree (&optional n cut)
  4501. "Cut the current subtree into the clipboard.
  4502. With prefix arg N, cut this many sequential subtrees.
  4503. This is a short-hand for marking the subtree and then copying it.
  4504. If CUT is non-nil, actually cut the subtree."
  4505. (interactive "p")
  4506. (let (beg end folded (beg0 (point)))
  4507. (if (interactive-p)
  4508. (org-back-to-heading nil) ; take what looks like a subtree
  4509. (org-back-to-heading t)) ; take what is really there
  4510. (org-back-over-empty-lines)
  4511. (setq beg (point))
  4512. (skip-chars-forward " \t\r\n")
  4513. (save-match-data
  4514. (save-excursion (outline-end-of-heading)
  4515. (setq folded (org-invisible-p)))
  4516. (condition-case nil
  4517. (outline-forward-same-level (1- n))
  4518. (error nil))
  4519. (org-end-of-subtree t t))
  4520. (org-back-over-empty-lines)
  4521. (setq end (point))
  4522. (goto-char beg0)
  4523. (when (> end beg)
  4524. (setq org-subtree-clip-folded folded)
  4525. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4526. (setq org-subtree-clip (current-kill 0))
  4527. (message "%s: Subtree(s) with %d characters"
  4528. (if cut "Cut" "Copied")
  4529. (length org-subtree-clip)))))
  4530. (defun org-paste-subtree (&optional level tree)
  4531. "Paste the clipboard as a subtree, with modification of headline level.
  4532. The entire subtree is promoted or demoted in order to match a new headline
  4533. level. By default, the new level is derived from the visible headings
  4534. before and after the insertion point, and taken to be the inferior headline
  4535. level of the two. So if the previous visible heading is level 3 and the
  4536. next is level 4 (or vice versa), level 4 will be used for insertion.
  4537. This makes sure that the subtree remains an independent subtree and does
  4538. not swallow low level entries.
  4539. You can also force a different level, either by using a numeric prefix
  4540. argument, or by inserting the heading marker by hand. For example, if the
  4541. cursor is after \"*****\", then the tree will be shifted to level 5.
  4542. If you want to insert the tree as is, just use \\[yank].
  4543. If optional TREE is given, use this text instead of the kill ring."
  4544. (interactive "P")
  4545. (unless (org-kill-is-subtree-p tree)
  4546. (error "%s"
  4547. (substitute-command-keys
  4548. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4549. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4550. (^re (concat "^\\(" outline-regexp "\\)"))
  4551. (re (concat "\\(" outline-regexp "\\)"))
  4552. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4553. (old-level (if (string-match ^re txt)
  4554. (- (match-end 0) (match-beginning 0) 1)
  4555. -1))
  4556. (force-level (cond (level (prefix-numeric-value level))
  4557. ((string-match
  4558. ^re_ (buffer-substring (point-at-bol) (point)))
  4559. (- (match-end 1) (match-beginning 1)))
  4560. (t nil)))
  4561. (previous-level (save-excursion
  4562. (condition-case nil
  4563. (progn
  4564. (outline-previous-visible-heading 1)
  4565. (if (looking-at re)
  4566. (- (match-end 0) (match-beginning 0) 1)
  4567. 1))
  4568. (error 1))))
  4569. (next-level (save-excursion
  4570. (condition-case nil
  4571. (progn
  4572. (or (looking-at outline-regexp)
  4573. (outline-next-visible-heading 1))
  4574. (if (looking-at re)
  4575. (- (match-end 0) (match-beginning 0) 1)
  4576. 1))
  4577. (error 1))))
  4578. (new-level (or force-level (max previous-level next-level)))
  4579. (shift (if (or (= old-level -1)
  4580. (= new-level -1)
  4581. (= old-level new-level))
  4582. 0
  4583. (- new-level old-level)))
  4584. (delta (if (> shift 0) -1 1))
  4585. (func (if (> shift 0) 'org-demote 'org-promote))
  4586. (org-odd-levels-only nil)
  4587. beg end)
  4588. ;; Remove the forced level indicator
  4589. (if force-level
  4590. (delete-region (point-at-bol) (point)))
  4591. ;; Paste
  4592. (beginning-of-line 1)
  4593. (org-back-over-empty-lines)
  4594. (setq beg (point))
  4595. (insert-before-markers txt)
  4596. (unless (string-match "\n\\'" txt) (insert "\n"))
  4597. (setq end (point))
  4598. (goto-char beg)
  4599. (skip-chars-forward " \t\n\r")
  4600. (setq beg (point))
  4601. ;; Shift if necessary
  4602. (unless (= shift 0)
  4603. (save-restriction
  4604. (narrow-to-region beg end)
  4605. (while (not (= shift 0))
  4606. (org-map-region func (point-min) (point-max))
  4607. (setq shift (+ delta shift)))
  4608. (goto-char (point-min))))
  4609. (when (interactive-p)
  4610. (message "Clipboard pasted as level %d subtree" new-level))
  4611. (if (and kill-ring
  4612. (eq org-subtree-clip (current-kill 0))
  4613. org-subtree-clip-folded)
  4614. ;; The tree was folded before it was killed/copied
  4615. (hide-subtree))))
  4616. (defun org-kill-is-subtree-p (&optional txt)
  4617. "Check if the current kill is an outline subtree, or a set of trees.
  4618. Returns nil if kill does not start with a headline, or if the first
  4619. headline level is not the largest headline level in the tree.
  4620. So this will actually accept several entries of equal levels as well,
  4621. which is OK for `org-paste-subtree'.
  4622. If optional TXT is given, check this string instead of the current kill."
  4623. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4624. (start-level (and kill
  4625. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4626. org-outline-regexp "\\)")
  4627. kill)
  4628. (- (match-end 2) (match-beginning 2) 1)))
  4629. (re (concat "^" org-outline-regexp))
  4630. (start (1+ (match-beginning 2))))
  4631. (if (not start-level)
  4632. (progn
  4633. nil) ;; does not even start with a heading
  4634. (catch 'exit
  4635. (while (setq start (string-match re kill (1+ start)))
  4636. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4637. (throw 'exit nil)))
  4638. t))))
  4639. (defun org-narrow-to-subtree ()
  4640. "Narrow buffer to the current subtree."
  4641. (interactive)
  4642. (save-excursion
  4643. (save-match-data
  4644. (narrow-to-region
  4645. (progn (org-back-to-heading) (point))
  4646. (progn (org-end-of-subtree t t) (point))))))
  4647. ;;; Outline Sorting
  4648. (defun org-sort (with-case)
  4649. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4650. Optional argument WITH-CASE means sort case-sensitively."
  4651. (interactive "P")
  4652. (if (org-at-table-p)
  4653. (org-call-with-arg 'org-table-sort-lines with-case)
  4654. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4655. (defun org-sort-remove-invisible (s)
  4656. (remove-text-properties 0 (length s) org-rm-props s)
  4657. (while (string-match org-bracket-link-regexp s)
  4658. (setq s (replace-match (if (match-end 2)
  4659. (match-string 3 s)
  4660. (match-string 1 s)) t t s)))
  4661. s)
  4662. (defvar org-priority-regexp) ; defined later in the file
  4663. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4664. "Sort entries on a certain level of an outline tree.
  4665. If there is an active region, the entries in the region are sorted.
  4666. Else, if the cursor is before the first entry, sort the top-level items.
  4667. Else, the children of the entry at point are sorted.
  4668. Sorting can be alphabetically, numerically, and by date/time as given by
  4669. the first time stamp in the entry. The command prompts for the sorting
  4670. type unless it has been given to the function through the SORTING-TYPE
  4671. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4672. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4673. called with point at the beginning of the record. It must return either
  4674. a string or a number that should serve as the sorting key for that record.
  4675. Comparing entries ignores case by default. However, with an optional argument
  4676. WITH-CASE, the sorting considers case as well."
  4677. (interactive "P")
  4678. (let ((case-func (if with-case 'identity 'downcase))
  4679. start beg end stars re re2
  4680. txt what tmp plain-list-p)
  4681. ;; Find beginning and end of region to sort
  4682. (cond
  4683. ((org-region-active-p)
  4684. ;; we will sort the region
  4685. (setq end (region-end)
  4686. what "region")
  4687. (goto-char (region-beginning))
  4688. (if (not (org-on-heading-p)) (outline-next-heading))
  4689. (setq start (point)))
  4690. ((org-at-item-p)
  4691. ;; we will sort this plain list
  4692. (org-beginning-of-item-list) (setq start (point))
  4693. (org-end-of-item-list) (setq end (point))
  4694. (goto-char start)
  4695. (setq plain-list-p t
  4696. what "plain list"))
  4697. ((or (org-on-heading-p)
  4698. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4699. ;; we will sort the children of the current headline
  4700. (org-back-to-heading)
  4701. (setq start (point)
  4702. end (progn (org-end-of-subtree t t)
  4703. (org-back-over-empty-lines)
  4704. (point))
  4705. what "children")
  4706. (goto-char start)
  4707. (show-subtree)
  4708. (outline-next-heading))
  4709. (t
  4710. ;; we will sort the top-level entries in this file
  4711. (goto-char (point-min))
  4712. (or (org-on-heading-p) (outline-next-heading))
  4713. (setq start (point) end (point-max) what "top-level")
  4714. (goto-char start)
  4715. (show-all)))
  4716. (setq beg (point))
  4717. (if (>= beg end) (error "Nothing to sort"))
  4718. (unless plain-list-p
  4719. (looking-at "\\(\\*+\\)")
  4720. (setq stars (match-string 1)
  4721. re (concat "^" (regexp-quote stars) " +")
  4722. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4723. txt (buffer-substring beg end))
  4724. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4725. (if (and (not (equal stars "*")) (string-match re2 txt))
  4726. (error "Region to sort contains a level above the first entry")))
  4727. (unless sorting-type
  4728. (message
  4729. (if plain-list-p
  4730. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4731. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  4732. what)
  4733. (setq sorting-type (read-char-exclusive))
  4734. (and (= (downcase sorting-type) ?f)
  4735. (setq getkey-func
  4736. (completing-read "Sort using function: "
  4737. obarray 'fboundp t nil nil))
  4738. (setq getkey-func (intern getkey-func)))
  4739. (and (= (downcase sorting-type) ?r)
  4740. (setq property
  4741. (completing-read "Property: "
  4742. (mapcar 'list (org-buffer-property-keys t))
  4743. nil t))))
  4744. (message "Sorting entries...")
  4745. (save-restriction
  4746. (narrow-to-region start end)
  4747. (let ((dcst (downcase sorting-type))
  4748. (now (current-time)))
  4749. (sort-subr
  4750. (/= dcst sorting-type)
  4751. ;; This function moves to the beginning character of the "record" to
  4752. ;; be sorted.
  4753. (if plain-list-p
  4754. (lambda nil
  4755. (if (org-at-item-p) t (goto-char (point-max))))
  4756. (lambda nil
  4757. (if (re-search-forward re nil t)
  4758. (goto-char (match-beginning 0))
  4759. (goto-char (point-max)))))
  4760. ;; This function moves to the last character of the "record" being
  4761. ;; sorted.
  4762. (if plain-list-p
  4763. 'org-end-of-item
  4764. (lambda nil
  4765. (save-match-data
  4766. (condition-case nil
  4767. (outline-forward-same-level 1)
  4768. (error
  4769. (goto-char (point-max)))))))
  4770. ;; This function returns the value that gets sorted against.
  4771. (if plain-list-p
  4772. (lambda nil
  4773. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4774. (cond
  4775. ((= dcst ?n)
  4776. (string-to-number (buffer-substring (match-end 0)
  4777. (point-at-eol))))
  4778. ((= dcst ?a)
  4779. (buffer-substring (match-end 0) (point-at-eol)))
  4780. ((= dcst ?t)
  4781. (if (re-search-forward org-ts-regexp
  4782. (point-at-eol) t)
  4783. (org-time-string-to-time (match-string 0))
  4784. now))
  4785. ((= dcst ?f)
  4786. (if getkey-func
  4787. (progn
  4788. (setq tmp (funcall getkey-func))
  4789. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4790. tmp)
  4791. (error "Invalid key function `%s'" getkey-func)))
  4792. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4793. (lambda nil
  4794. (cond
  4795. ((= dcst ?n)
  4796. (if (looking-at outline-regexp)
  4797. (string-to-number (buffer-substring (match-end 0)
  4798. (point-at-eol)))
  4799. nil))
  4800. ((= dcst ?a)
  4801. (funcall case-func (buffer-substring (point-at-bol)
  4802. (point-at-eol))))
  4803. ((= dcst ?t)
  4804. (if (re-search-forward org-ts-regexp
  4805. (save-excursion
  4806. (forward-line 2)
  4807. (point)) t)
  4808. (org-time-string-to-time (match-string 0))
  4809. now))
  4810. ((= dcst ?p)
  4811. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4812. (string-to-char (match-string 2))
  4813. org-default-priority))
  4814. ((= dcst ?r)
  4815. (or (org-entry-get nil property) ""))
  4816. ((= dcst ?f)
  4817. (if getkey-func
  4818. (progn
  4819. (setq tmp (funcall getkey-func))
  4820. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4821. tmp)
  4822. (error "Invalid key function `%s'" getkey-func)))
  4823. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4824. nil
  4825. (cond
  4826. ((= dcst ?a) 'string<)
  4827. ((= dcst ?t) 'time-less-p)
  4828. (t nil)))))
  4829. (message "Sorting entries...done")))
  4830. (defun org-do-sort (table what &optional with-case sorting-type)
  4831. "Sort TABLE of WHAT according to SORTING-TYPE.
  4832. The user will be prompted for the SORTING-TYPE if the call to this
  4833. function does not specify it. WHAT is only for the prompt, to indicate
  4834. what is being sorted. The sorting key will be extracted from
  4835. the car of the elements of the table.
  4836. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4837. (unless sorting-type
  4838. (message
  4839. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4840. what)
  4841. (setq sorting-type (read-char-exclusive)))
  4842. (let ((dcst (downcase sorting-type))
  4843. extractfun comparefun)
  4844. ;; Define the appropriate functions
  4845. (cond
  4846. ((= dcst ?n)
  4847. (setq extractfun 'string-to-number
  4848. comparefun (if (= dcst sorting-type) '< '>)))
  4849. ((= dcst ?a)
  4850. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4851. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4852. comparefun (if (= dcst sorting-type)
  4853. 'string<
  4854. (lambda (a b) (and (not (string< a b))
  4855. (not (string= a b)))))))
  4856. ((= dcst ?t)
  4857. (setq extractfun
  4858. (lambda (x)
  4859. (if (string-match org-ts-regexp x)
  4860. (time-to-seconds
  4861. (org-time-string-to-time (match-string 0 x)))
  4862. 0))
  4863. comparefun (if (= dcst sorting-type) '< '>)))
  4864. (t (error "Invalid sorting type `%c'" sorting-type)))
  4865. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4866. table)
  4867. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4868. ;;;; Plain list items, including checkboxes
  4869. ;;; Plain list items
  4870. (defun org-at-item-p ()
  4871. "Is point in a line starting a hand-formatted item?"
  4872. (let ((llt org-plain-list-ordered-item-terminator))
  4873. (save-excursion
  4874. (goto-char (point-at-bol))
  4875. (looking-at
  4876. (cond
  4877. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4878. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4879. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4880. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4881. (defun org-in-item-p ()
  4882. "It the cursor inside a plain list item.
  4883. Does not have to be the first line."
  4884. (save-excursion
  4885. (condition-case nil
  4886. (progn
  4887. (org-beginning-of-item)
  4888. (org-at-item-p)
  4889. t)
  4890. (error nil))))
  4891. (defun org-insert-item (&optional checkbox)
  4892. "Insert a new item at the current level.
  4893. Return t when things worked, nil when we are not in an item."
  4894. (when (save-excursion
  4895. (condition-case nil
  4896. (progn
  4897. (org-beginning-of-item)
  4898. (org-at-item-p)
  4899. (if (org-invisible-p) (error "Invisible item"))
  4900. t)
  4901. (error nil)))
  4902. (let* ((bul (match-string 0))
  4903. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4904. (match-end 0)))
  4905. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4906. pos)
  4907. (cond
  4908. ((and (org-at-item-p) (<= (point) eow))
  4909. ;; before the bullet
  4910. (beginning-of-line 1)
  4911. (open-line (if blank 2 1)))
  4912. ((<= (point) eow)
  4913. (beginning-of-line 1))
  4914. (t
  4915. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4916. (end-of-line 1)
  4917. (delete-horizontal-space))
  4918. (newline (if blank 2 1))))
  4919. (insert bul (if checkbox "[ ]" ""))
  4920. (just-one-space)
  4921. (setq pos (point))
  4922. (end-of-line 1)
  4923. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4924. (org-maybe-renumber-ordered-list)
  4925. (and checkbox (org-update-checkbox-count-maybe))
  4926. t))
  4927. ;;; Checkboxes
  4928. (defun org-at-item-checkbox-p ()
  4929. "Is point at a line starting a plain-list item with a checklet?"
  4930. (and (org-at-item-p)
  4931. (save-excursion
  4932. (goto-char (match-end 0))
  4933. (skip-chars-forward " \t")
  4934. (looking-at "\\[[- X]\\]"))))
  4935. (defun org-toggle-checkbox (&optional arg)
  4936. "Toggle the checkbox in the current line."
  4937. (interactive "P")
  4938. (catch 'exit
  4939. (let (beg end status (firstnew 'unknown))
  4940. (cond
  4941. ((org-region-active-p)
  4942. (setq beg (region-beginning) end (region-end)))
  4943. ((org-on-heading-p)
  4944. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4945. ((org-at-item-checkbox-p)
  4946. (let ((pos (point)))
  4947. (replace-match
  4948. (cond (arg "[-]")
  4949. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4950. (t "[ ]"))
  4951. t t)
  4952. (goto-char pos))
  4953. (throw 'exit t))
  4954. (t (error "Not at a checkbox or heading, and no active region")))
  4955. (save-excursion
  4956. (goto-char beg)
  4957. (while (< (point) end)
  4958. (when (org-at-item-checkbox-p)
  4959. (setq status (equal (match-string 0) "[X]"))
  4960. (when (eq firstnew 'unknown)
  4961. (setq firstnew (not status)))
  4962. (replace-match
  4963. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4964. (beginning-of-line 2)))))
  4965. (org-update-checkbox-count-maybe))
  4966. (defun org-update-checkbox-count-maybe ()
  4967. "Update checkbox statistics unless turned off by user."
  4968. (when org-provide-checkbox-statistics
  4969. (org-update-checkbox-count)))
  4970. (defun org-update-checkbox-count (&optional all)
  4971. "Update the checkbox statistics in the current section.
  4972. This will find all statistic cookies like [57%] and [6/12] and update them
  4973. with the current numbers. With optional prefix argument ALL, do this for
  4974. the whole buffer."
  4975. (interactive "P")
  4976. (save-excursion
  4977. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4978. (beg (condition-case nil
  4979. (progn (outline-back-to-heading) (point))
  4980. (error (point-min))))
  4981. (end (move-marker (make-marker)
  4982. (progn (outline-next-heading) (point))))
  4983. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4984. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4985. (re-find (concat re "\\|" re-box))
  4986. beg-cookie end-cookie is-percent c-on c-off lim
  4987. eline curr-ind next-ind continue-from startsearch
  4988. (cstat 0)
  4989. )
  4990. (when all
  4991. (goto-char (point-min))
  4992. (outline-next-heading)
  4993. (setq beg (point) end (point-max)))
  4994. (goto-char end)
  4995. ;; find each statistic cookie
  4996. (while (re-search-backward re-find beg t)
  4997. (setq beg-cookie (match-beginning 1)
  4998. end-cookie (match-end 1)
  4999. cstat (+ cstat (if end-cookie 1 0))
  5000. startsearch (point-at-eol)
  5001. continue-from (point-at-bol)
  5002. is-percent (match-beginning 2)
  5003. lim (cond
  5004. ((org-on-heading-p) (outline-next-heading) (point))
  5005. ((org-at-item-p) (org-end-of-item) (point))
  5006. (t nil))
  5007. c-on 0
  5008. c-off 0)
  5009. (when lim
  5010. ;; find first checkbox for this cookie and gather
  5011. ;; statistics from all that are at this indentation level
  5012. (goto-char startsearch)
  5013. (if (re-search-forward re-box lim t)
  5014. (progn
  5015. (org-beginning-of-item)
  5016. (setq curr-ind (org-get-indentation))
  5017. (setq next-ind curr-ind)
  5018. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  5019. (save-excursion (end-of-line) (setq eline (point)))
  5020. (if (re-search-forward re-box eline t)
  5021. (if (member (match-string 2) '("[ ]" "[-]"))
  5022. (setq c-off (1+ c-off))
  5023. (setq c-on (1+ c-on))
  5024. )
  5025. )
  5026. (org-end-of-item)
  5027. (setq next-ind (org-get-indentation))
  5028. )))
  5029. (goto-char continue-from)
  5030. ;; update cookie
  5031. (when end-cookie
  5032. (delete-region beg-cookie end-cookie)
  5033. (goto-char beg-cookie)
  5034. (insert
  5035. (if is-percent
  5036. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5037. (format "[%d/%d]" c-on (+ c-on c-off)))))
  5038. ;; update items checkbox if it has one
  5039. (when (org-at-item-p)
  5040. (org-beginning-of-item)
  5041. (when (and (> (+ c-on c-off) 0)
  5042. (re-search-forward re-box (point-at-eol) t))
  5043. (setq beg-cookie (match-beginning 2)
  5044. end-cookie (match-end 2))
  5045. (delete-region beg-cookie end-cookie)
  5046. (goto-char beg-cookie)
  5047. (cond ((= c-off 0) (insert "[X]"))
  5048. ((= c-on 0) (insert "[ ]"))
  5049. (t (insert "[-]")))
  5050. )))
  5051. (goto-char continue-from))
  5052. (when (interactive-p)
  5053. (message "Checkbox satistics updated %s (%d places)"
  5054. (if all "in entire file" "in current outline entry") cstat)))))
  5055. (defun org-get-checkbox-statistics-face ()
  5056. "Select the face for checkbox statistics.
  5057. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5058. it will be `org-todo'."
  5059. (if (match-end 1)
  5060. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5061. (if (and (> (match-end 2) (match-beginning 2))
  5062. (equal (match-string 2) (match-string 3)))
  5063. 'org-done
  5064. 'org-todo)))
  5065. (defun org-get-indentation (&optional line)
  5066. "Get the indentation of the current line, interpreting tabs.
  5067. When LINE is given, assume it represents a line and compute its indentation."
  5068. (if line
  5069. (if (string-match "^ *" (org-remove-tabs line))
  5070. (match-end 0))
  5071. (save-excursion
  5072. (beginning-of-line 1)
  5073. (skip-chars-forward " \t")
  5074. (current-column))))
  5075. (defun org-remove-tabs (s &optional width)
  5076. "Replace tabulators in S with spaces.
  5077. Assumes that s is a single line, starting in column 0."
  5078. (setq width (or width tab-width))
  5079. (while (string-match "\t" s)
  5080. (setq s (replace-match
  5081. (make-string
  5082. (- (* width (/ (+ (match-beginning 0) width) width))
  5083. (match-beginning 0)) ?\ )
  5084. t t s)))
  5085. s)
  5086. (defun org-fix-indentation (line ind)
  5087. "Fix indentation in LINE.
  5088. IND is a cons cell with target and minimum indentation.
  5089. If the current indenation in LINE is smaller than the minimum,
  5090. leave it alone. If it is larger than ind, set it to the target."
  5091. (let* ((l (org-remove-tabs line))
  5092. (i (org-get-indentation l))
  5093. (i1 (car ind)) (i2 (cdr ind)))
  5094. (if (>= i i2) (setq l (substring line i2)))
  5095. (if (> i1 0)
  5096. (concat (make-string i1 ?\ ) l)
  5097. l)))
  5098. (defcustom org-empty-line-terminates-plain-lists nil
  5099. "Non-nil means, an empty line ends all plain list levels.
  5100. When nil, empty lines are part of the preceeding item."
  5101. :group 'org-plain-lists
  5102. :type 'boolean)
  5103. (defun org-beginning-of-item ()
  5104. "Go to the beginning of the current hand-formatted item.
  5105. If the cursor is not in an item, throw an error."
  5106. (interactive)
  5107. (let ((pos (point))
  5108. (limit (save-excursion
  5109. (condition-case nil
  5110. (progn
  5111. (org-back-to-heading)
  5112. (beginning-of-line 2) (point))
  5113. (error (point-min)))))
  5114. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5115. ind ind1)
  5116. (if (org-at-item-p)
  5117. (beginning-of-line 1)
  5118. (beginning-of-line 1)
  5119. (skip-chars-forward " \t")
  5120. (setq ind (current-column))
  5121. (if (catch 'exit
  5122. (while t
  5123. (beginning-of-line 0)
  5124. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5125. (if (looking-at "[ \t]*$")
  5126. (setq ind1 ind-empty)
  5127. (skip-chars-forward " \t")
  5128. (setq ind1 (current-column)))
  5129. (if (< ind1 ind)
  5130. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5131. nil
  5132. (goto-char pos)
  5133. (error "Not in an item")))))
  5134. (defun org-end-of-item ()
  5135. "Go to the end of the current hand-formatted item.
  5136. If the cursor is not in an item, throw an error."
  5137. (interactive)
  5138. (let* ((pos (point))
  5139. ind1
  5140. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5141. (limit (save-excursion (outline-next-heading) (point)))
  5142. (ind (save-excursion
  5143. (org-beginning-of-item)
  5144. (skip-chars-forward " \t")
  5145. (current-column)))
  5146. (end (catch 'exit
  5147. (while t
  5148. (beginning-of-line 2)
  5149. (if (eobp) (throw 'exit (point)))
  5150. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5151. (if (looking-at "[ \t]*$")
  5152. (setq ind1 ind-empty)
  5153. (skip-chars-forward " \t")
  5154. (setq ind1 (current-column)))
  5155. (if (<= ind1 ind)
  5156. (throw 'exit (point-at-bol)))))))
  5157. (if end
  5158. (goto-char end)
  5159. (goto-char pos)
  5160. (error "Not in an item"))))
  5161. (defun org-next-item ()
  5162. "Move to the beginning of the next item in the current plain list.
  5163. Error if not at a plain list, or if this is the last item in the list."
  5164. (interactive)
  5165. (let (ind ind1 (pos (point)))
  5166. (org-beginning-of-item)
  5167. (setq ind (org-get-indentation))
  5168. (org-end-of-item)
  5169. (setq ind1 (org-get-indentation))
  5170. (unless (and (org-at-item-p) (= ind ind1))
  5171. (goto-char pos)
  5172. (error "On last item"))))
  5173. (defun org-previous-item ()
  5174. "Move to the beginning of the previous item in the current plain list.
  5175. Error if not at a plain list, or if this is the first item in the list."
  5176. (interactive)
  5177. (let (beg ind ind1 (pos (point)))
  5178. (org-beginning-of-item)
  5179. (setq beg (point))
  5180. (setq ind (org-get-indentation))
  5181. (goto-char beg)
  5182. (catch 'exit
  5183. (while t
  5184. (beginning-of-line 0)
  5185. (if (looking-at "[ \t]*$")
  5186. nil
  5187. (if (<= (setq ind1 (org-get-indentation)) ind)
  5188. (throw 'exit t)))))
  5189. (condition-case nil
  5190. (if (or (not (org-at-item-p))
  5191. (< ind1 (1- ind)))
  5192. (error "")
  5193. (org-beginning-of-item))
  5194. (error (goto-char pos)
  5195. (error "On first item")))))
  5196. (defun org-first-list-item-p ()
  5197. "Is this heading the item in a plain list?"
  5198. (unless (org-at-item-p)
  5199. (error "Not at a plain list item"))
  5200. (org-beginning-of-item)
  5201. (= (point) (save-excursion (org-beginning-of-item-list))))
  5202. (defun org-move-item-down ()
  5203. "Move the plain list item at point down, i.e. swap with following item.
  5204. Subitems (items with larger indentation) are considered part of the item,
  5205. so this really moves item trees."
  5206. (interactive)
  5207. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5208. (org-beginning-of-item)
  5209. (setq beg0 (point))
  5210. (save-excursion
  5211. (setq ne-beg (org-back-over-empty-lines))
  5212. (setq beg (point)))
  5213. (goto-char beg0)
  5214. (setq ind (org-get-indentation))
  5215. (org-end-of-item)
  5216. (setq end0 (point))
  5217. (setq ind1 (org-get-indentation))
  5218. (setq ne-end (org-back-over-empty-lines))
  5219. (setq end (point))
  5220. (goto-char beg0)
  5221. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5222. ;; include less whitespace
  5223. (save-excursion
  5224. (goto-char beg)
  5225. (forward-line (- ne-beg ne-end))
  5226. (setq beg (point))))
  5227. (goto-char end0)
  5228. (if (and (org-at-item-p) (= ind ind1))
  5229. (progn
  5230. (org-end-of-item)
  5231. (org-back-over-empty-lines)
  5232. (setq txt (buffer-substring beg end))
  5233. (save-excursion
  5234. (delete-region beg end))
  5235. (setq pos (point))
  5236. (insert txt)
  5237. (goto-char pos) (org-skip-whitespace)
  5238. (org-maybe-renumber-ordered-list))
  5239. (goto-char pos)
  5240. (error "Cannot move this item further down"))))
  5241. (defun org-move-item-up (arg)
  5242. "Move the plain list item at point up, i.e. swap with previous item.
  5243. Subitems (items with larger indentation) are considered part of the item,
  5244. so this really moves item trees."
  5245. (interactive "p")
  5246. (let (beg beg0 end ind ind1 (pos (point)) txt
  5247. ne-beg ne-ins ins-end)
  5248. (org-beginning-of-item)
  5249. (setq beg0 (point))
  5250. (setq ind (org-get-indentation))
  5251. (save-excursion
  5252. (setq ne-beg (org-back-over-empty-lines))
  5253. (setq beg (point)))
  5254. (goto-char beg0)
  5255. (org-end-of-item)
  5256. (setq end (point))
  5257. (goto-char beg0)
  5258. (catch 'exit
  5259. (while t
  5260. (beginning-of-line 0)
  5261. (if (looking-at "[ \t]*$")
  5262. (if org-empty-line-terminates-plain-lists
  5263. (progn
  5264. (goto-char pos)
  5265. (error "Cannot move this item further up"))
  5266. nil)
  5267. (if (<= (setq ind1 (org-get-indentation)) ind)
  5268. (throw 'exit t)))))
  5269. (condition-case nil
  5270. (org-beginning-of-item)
  5271. (error (goto-char beg)
  5272. (error "Cannot move this item further up")))
  5273. (setq ind1 (org-get-indentation))
  5274. (if (and (org-at-item-p) (= ind ind1))
  5275. (progn
  5276. (setq ne-ins (org-back-over-empty-lines))
  5277. (setq txt (buffer-substring beg end))
  5278. (save-excursion
  5279. (delete-region beg end))
  5280. (setq pos (point))
  5281. (insert txt)
  5282. (setq ins-end (point))
  5283. (goto-char pos) (org-skip-whitespace)
  5284. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5285. ;; Move whitespace back to beginning
  5286. (save-excursion
  5287. (goto-char ins-end)
  5288. (let ((kill-whole-line t))
  5289. (kill-line (- ne-ins ne-beg)) (point)))
  5290. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5291. (org-maybe-renumber-ordered-list))
  5292. (goto-char pos)
  5293. (error "Cannot move this item further up"))))
  5294. (defun org-maybe-renumber-ordered-list ()
  5295. "Renumber the ordered list at point if setup allows it.
  5296. This tests the user option `org-auto-renumber-ordered-lists' before
  5297. doing the renumbering."
  5298. (interactive)
  5299. (when (and org-auto-renumber-ordered-lists
  5300. (org-at-item-p))
  5301. (if (match-beginning 3)
  5302. (org-renumber-ordered-list 1)
  5303. (org-fix-bullet-type))))
  5304. (defun org-maybe-renumber-ordered-list-safe ()
  5305. (condition-case nil
  5306. (save-excursion
  5307. (org-maybe-renumber-ordered-list))
  5308. (error nil)))
  5309. (defun org-cycle-list-bullet (&optional which)
  5310. "Cycle through the different itemize/enumerate bullets.
  5311. This cycle the entire list level through the sequence:
  5312. `-' -> `+' -> `*' -> `1.' -> `1)'
  5313. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5314. 0 meand `-', 1 means `+' etc."
  5315. (interactive "P")
  5316. (org-preserve-lc
  5317. (org-beginning-of-item-list)
  5318. (org-at-item-p)
  5319. (beginning-of-line 1)
  5320. (let ((current (match-string 0))
  5321. (prevp (eq which 'previous))
  5322. new)
  5323. (setq new (cond
  5324. ((and (numberp which)
  5325. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5326. ((string-match "-" current) (if prevp "1)" "+"))
  5327. ((string-match "\\+" current)
  5328. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5329. ((string-match "\\*" current) (if prevp "+" "1."))
  5330. ((string-match "\\." current) (if prevp "*" "1)"))
  5331. ((string-match ")" current) (if prevp "1." "-"))
  5332. (t (error "This should not happen"))))
  5333. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5334. (org-fix-bullet-type)
  5335. (org-maybe-renumber-ordered-list))))
  5336. (defun org-get-string-indentation (s)
  5337. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5338. (let ((n -1) (i 0) (w tab-width) c)
  5339. (catch 'exit
  5340. (while (< (setq n (1+ n)) (length s))
  5341. (setq c (aref s n))
  5342. (cond ((= c ?\ ) (setq i (1+ i)))
  5343. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5344. (t (throw 'exit t)))))
  5345. i))
  5346. (defun org-renumber-ordered-list (arg)
  5347. "Renumber an ordered plain list.
  5348. Cursor needs to be in the first line of an item, the line that starts
  5349. with something like \"1.\" or \"2)\"."
  5350. (interactive "p")
  5351. (unless (and (org-at-item-p)
  5352. (match-beginning 3))
  5353. (error "This is not an ordered list"))
  5354. (let ((line (org-current-line))
  5355. (col (current-column))
  5356. (ind (org-get-string-indentation
  5357. (buffer-substring (point-at-bol) (match-beginning 3))))
  5358. ;; (term (substring (match-string 3) -1))
  5359. ind1 (n (1- arg))
  5360. fmt)
  5361. ;; find where this list begins
  5362. (org-beginning-of-item-list)
  5363. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5364. (setq fmt (concat "%d" (match-string 1)))
  5365. (beginning-of-line 0)
  5366. ;; walk forward and replace these numbers
  5367. (catch 'exit
  5368. (while t
  5369. (catch 'next
  5370. (beginning-of-line 2)
  5371. (if (eobp) (throw 'exit nil))
  5372. (if (looking-at "[ \t]*$") (throw 'next nil))
  5373. (skip-chars-forward " \t") (setq ind1 (current-column))
  5374. (if (> ind1 ind) (throw 'next t))
  5375. (if (< ind1 ind) (throw 'exit t))
  5376. (if (not (org-at-item-p)) (throw 'exit nil))
  5377. (delete-region (match-beginning 2) (match-end 2))
  5378. (goto-char (match-beginning 2))
  5379. (insert (format fmt (setq n (1+ n)))))))
  5380. (goto-line line)
  5381. (move-to-column col)))
  5382. (defun org-fix-bullet-type ()
  5383. "Make sure all items in this list have the same bullet as the firsst item."
  5384. (interactive)
  5385. (unless (org-at-item-p) (error "This is not a list"))
  5386. (let ((line (org-current-line))
  5387. (col (current-column))
  5388. (ind (current-indentation))
  5389. ind1 bullet)
  5390. ;; find where this list begins
  5391. (org-beginning-of-item-list)
  5392. (beginning-of-line 1)
  5393. ;; find out what the bullet type is
  5394. (looking-at "[ \t]*\\(\\S-+\\)")
  5395. (setq bullet (match-string 1))
  5396. ;; walk forward and replace these numbers
  5397. (beginning-of-line 0)
  5398. (catch 'exit
  5399. (while t
  5400. (catch 'next
  5401. (beginning-of-line 2)
  5402. (if (eobp) (throw 'exit nil))
  5403. (if (looking-at "[ \t]*$") (throw 'next nil))
  5404. (skip-chars-forward " \t") (setq ind1 (current-column))
  5405. (if (> ind1 ind) (throw 'next t))
  5406. (if (< ind1 ind) (throw 'exit t))
  5407. (if (not (org-at-item-p)) (throw 'exit nil))
  5408. (skip-chars-forward " \t")
  5409. (looking-at "\\S-+")
  5410. (replace-match bullet))))
  5411. (goto-line line)
  5412. (move-to-column col)
  5413. (if (string-match "[0-9]" bullet)
  5414. (org-renumber-ordered-list 1))))
  5415. (defun org-beginning-of-item-list ()
  5416. "Go to the beginning of the current item list.
  5417. I.e. to the first item in this list."
  5418. (interactive)
  5419. (org-beginning-of-item)
  5420. (let ((pos (point-at-bol))
  5421. (ind (org-get-indentation))
  5422. ind1)
  5423. ;; find where this list begins
  5424. (catch 'exit
  5425. (while t
  5426. (catch 'next
  5427. (beginning-of-line 0)
  5428. (if (looking-at "[ \t]*$")
  5429. (throw (if (bobp) 'exit 'next) t))
  5430. (skip-chars-forward " \t") (setq ind1 (current-column))
  5431. (if (or (< ind1 ind)
  5432. (and (= ind1 ind)
  5433. (not (org-at-item-p)))
  5434. (bobp))
  5435. (throw 'exit t)
  5436. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5437. (goto-char pos)))
  5438. (defun org-end-of-item-list ()
  5439. "Go to the end of the current item list.
  5440. I.e. to the text after the last item."
  5441. (interactive)
  5442. (org-beginning-of-item)
  5443. (let ((pos (point-at-bol))
  5444. (ind (org-get-indentation))
  5445. ind1)
  5446. ;; find where this list begins
  5447. (catch 'exit
  5448. (while t
  5449. (catch 'next
  5450. (beginning-of-line 2)
  5451. (if (looking-at "[ \t]*$")
  5452. (throw (if (eobp) 'exit 'next) t))
  5453. (skip-chars-forward " \t") (setq ind1 (current-column))
  5454. (if (or (< ind1 ind)
  5455. (and (= ind1 ind)
  5456. (not (org-at-item-p)))
  5457. (eobp))
  5458. (progn
  5459. (setq pos (point-at-bol))
  5460. (throw 'exit t))))))
  5461. (goto-char pos)))
  5462. (defvar org-last-indent-begin-marker (make-marker))
  5463. (defvar org-last-indent-end-marker (make-marker))
  5464. (defun org-outdent-item (arg)
  5465. "Outdent a local list item."
  5466. (interactive "p")
  5467. (org-indent-item (- arg)))
  5468. (defun org-indent-item (arg)
  5469. "Indent a local list item."
  5470. (interactive "p")
  5471. (unless (org-at-item-p)
  5472. (error "Not on an item"))
  5473. (save-excursion
  5474. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5475. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5476. (setq beg org-last-indent-begin-marker
  5477. end org-last-indent-end-marker)
  5478. (org-beginning-of-item)
  5479. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5480. (org-end-of-item)
  5481. (setq end (move-marker org-last-indent-end-marker (point))))
  5482. (goto-char beg)
  5483. (setq tmp (org-item-indent-positions)
  5484. ind (car tmp)
  5485. ind-down (nth 2 tmp)
  5486. ind-up (nth 1 tmp)
  5487. delta (if (> arg 0)
  5488. (if ind-down (- ind-down ind) 2)
  5489. (if ind-up (- ind-up ind) -2)))
  5490. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5491. (while (< (point) end)
  5492. (beginning-of-line 1)
  5493. (skip-chars-forward " \t") (setq ind1 (current-column))
  5494. (delete-region (point-at-bol) (point))
  5495. (or (eolp) (indent-to-column (+ ind1 delta)))
  5496. (beginning-of-line 2))))
  5497. (org-fix-bullet-type)
  5498. (org-maybe-renumber-ordered-list-safe)
  5499. (save-excursion
  5500. (beginning-of-line 0)
  5501. (condition-case nil (org-beginning-of-item) (error nil))
  5502. (org-maybe-renumber-ordered-list-safe)))
  5503. (defun org-item-indent-positions ()
  5504. "Return indentation for plain list items.
  5505. This returns a list with three values: The current indentation, the
  5506. parent indentation and the indentation a child should habe.
  5507. Assumes cursor in item line."
  5508. (let* ((bolpos (point-at-bol))
  5509. (ind (org-get-indentation))
  5510. ind-down ind-up pos)
  5511. (save-excursion
  5512. (org-beginning-of-item-list)
  5513. (skip-chars-backward "\n\r \t")
  5514. (when (org-in-item-p)
  5515. (org-beginning-of-item)
  5516. (setq ind-up (org-get-indentation))))
  5517. (setq pos (point))
  5518. (save-excursion
  5519. (cond
  5520. ((and (condition-case nil (progn (org-previous-item) t)
  5521. (error nil))
  5522. (or (forward-char 1) t)
  5523. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5524. (setq ind-down (org-get-indentation)))
  5525. ((and (goto-char pos)
  5526. (org-at-item-p))
  5527. (goto-char (match-end 0))
  5528. (skip-chars-forward " \t")
  5529. (setq ind-down (current-column)))))
  5530. (list ind ind-up ind-down)))
  5531. ;;; The orgstruct minor mode
  5532. ;; Define a minor mode which can be used in other modes in order to
  5533. ;; integrate the org-mode structure editing commands.
  5534. ;; This is really a hack, because the org-mode structure commands use
  5535. ;; keys which normally belong to the major mode. Here is how it
  5536. ;; works: The minor mode defines all the keys necessary to operate the
  5537. ;; structure commands, but wraps the commands into a function which
  5538. ;; tests if the cursor is currently at a headline or a plain list
  5539. ;; item. If that is the case, the structure command is used,
  5540. ;; temporarily setting many Org-mode variables like regular
  5541. ;; expressions for filling etc. However, when any of those keys is
  5542. ;; used at a different location, function uses `key-binding' to look
  5543. ;; up if the key has an associated command in another currently active
  5544. ;; keymap (minor modes, major mode, global), and executes that
  5545. ;; command. There might be problems if any of the keys is otherwise
  5546. ;; used as a prefix key.
  5547. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5548. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5549. ;; addresses this by checking explicitly for both bindings.
  5550. (defvar orgstruct-mode-map (make-sparse-keymap)
  5551. "Keymap for the minor `orgstruct-mode'.")
  5552. (defvar org-local-vars nil
  5553. "List of local variables, for use by `orgstruct-mode'")
  5554. ;;;###autoload
  5555. (define-minor-mode orgstruct-mode
  5556. "Toggle the minor more `orgstruct-mode'.
  5557. This mode is for using Org-mode structure commands in other modes.
  5558. The following key behave as if Org-mode was active, if the cursor
  5559. is on a headline, or on a plain list item (both in the definition
  5560. of Org-mode).
  5561. M-up Move entry/item up
  5562. M-down Move entry/item down
  5563. M-left Promote
  5564. M-right Demote
  5565. M-S-up Move entry/item up
  5566. M-S-down Move entry/item down
  5567. M-S-left Promote subtree
  5568. M-S-right Demote subtree
  5569. M-q Fill paragraph and items like in Org-mode
  5570. C-c ^ Sort entries
  5571. C-c - Cycle list bullet
  5572. TAB Cycle item visibility
  5573. M-RET Insert new heading/item
  5574. S-M-RET Insert new TODO heading / Chekbox item
  5575. C-c C-c Set tags / toggle checkbox"
  5576. nil " OrgStruct" nil
  5577. (org-load-modules-maybe)
  5578. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5579. ;;;###autoload
  5580. (defun turn-on-orgstruct ()
  5581. "Unconditionally turn on `orgstruct-mode'."
  5582. (orgstruct-mode 1))
  5583. ;;;###autoload
  5584. (defun turn-on-orgstruct++ ()
  5585. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5586. In addition to setting orgstruct-mode, this also exports all indentation and
  5587. autofilling variables from org-mode into the buffer. Note that turning
  5588. off orgstruct-mode will *not* remove these additional settings."
  5589. (orgstruct-mode 1)
  5590. (let (var val)
  5591. (mapc
  5592. (lambda (x)
  5593. (when (string-match
  5594. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5595. (symbol-name (car x)))
  5596. (setq var (car x) val (nth 1 x))
  5597. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5598. org-local-vars)))
  5599. (defun orgstruct-error ()
  5600. "Error when there is no default binding for a structure key."
  5601. (interactive)
  5602. (error "This key has no function outside structure elements"))
  5603. (defun orgstruct-setup ()
  5604. "Setup orgstruct keymaps."
  5605. (let ((nfunc 0)
  5606. (bindings
  5607. (list
  5608. '([(meta up)] org-metaup)
  5609. '([(meta down)] org-metadown)
  5610. '([(meta left)] org-metaleft)
  5611. '([(meta right)] org-metaright)
  5612. '([(meta shift up)] org-shiftmetaup)
  5613. '([(meta shift down)] org-shiftmetadown)
  5614. '([(meta shift left)] org-shiftmetaleft)
  5615. '([(meta shift right)] org-shiftmetaright)
  5616. '([(shift up)] org-shiftup)
  5617. '([(shift down)] org-shiftdown)
  5618. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5619. '("\M-q" fill-paragraph)
  5620. '("\C-c^" org-sort)
  5621. '("\C-c-" org-cycle-list-bullet)))
  5622. elt key fun cmd)
  5623. (while (setq elt (pop bindings))
  5624. (setq nfunc (1+ nfunc))
  5625. (setq key (org-key (car elt))
  5626. fun (nth 1 elt)
  5627. cmd (orgstruct-make-binding fun nfunc key))
  5628. (org-defkey orgstruct-mode-map key cmd))
  5629. ;; Special treatment needed for TAB and RET
  5630. (org-defkey orgstruct-mode-map [(tab)]
  5631. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5632. (org-defkey orgstruct-mode-map "\C-i"
  5633. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5634. (org-defkey orgstruct-mode-map "\M-\C-m"
  5635. (orgstruct-make-binding 'org-insert-heading 105
  5636. "\M-\C-m" [(meta return)]))
  5637. (org-defkey orgstruct-mode-map [(meta return)]
  5638. (orgstruct-make-binding 'org-insert-heading 106
  5639. [(meta return)] "\M-\C-m"))
  5640. (org-defkey orgstruct-mode-map [(shift meta return)]
  5641. (orgstruct-make-binding 'org-insert-todo-heading 107
  5642. [(meta return)] "\M-\C-m"))
  5643. (unless org-local-vars
  5644. (setq org-local-vars (org-get-local-variables)))
  5645. t))
  5646. (defun orgstruct-make-binding (fun n &rest keys)
  5647. "Create a function for binding in the structure minor mode.
  5648. FUN is the command to call inside a table. N is used to create a unique
  5649. command name. KEYS are keys that should be checked in for a command
  5650. to execute outside of tables."
  5651. (eval
  5652. (list 'defun
  5653. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5654. '(arg)
  5655. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5656. "Outside of structure, run the binding of `"
  5657. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5658. "'.")
  5659. '(interactive "p")
  5660. (list 'if
  5661. '(org-context-p 'headline 'item)
  5662. (list 'org-run-like-in-org-mode (list 'quote fun))
  5663. (list 'let '(orgstruct-mode)
  5664. (list 'call-interactively
  5665. (append '(or)
  5666. (mapcar (lambda (k)
  5667. (list 'key-binding k))
  5668. keys)
  5669. '('orgstruct-error))))))))
  5670. (defun org-context-p (&rest contexts)
  5671. "Check if local context is and of CONTEXTS.
  5672. Possible values in the list of contexts are `table', `headline', and `item'."
  5673. (let ((pos (point)))
  5674. (goto-char (point-at-bol))
  5675. (prog1 (or (and (memq 'table contexts)
  5676. (looking-at "[ \t]*|"))
  5677. (and (memq 'headline contexts)
  5678. (looking-at "\\*+"))
  5679. (and (memq 'item contexts)
  5680. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5681. (goto-char pos))))
  5682. (defun org-get-local-variables ()
  5683. "Return a list of all local variables in an org-mode buffer."
  5684. (let (varlist)
  5685. (with-current-buffer (get-buffer-create "*Org tmp*")
  5686. (erase-buffer)
  5687. (org-mode)
  5688. (setq varlist (buffer-local-variables)))
  5689. (kill-buffer "*Org tmp*")
  5690. (delq nil
  5691. (mapcar
  5692. (lambda (x)
  5693. (setq x
  5694. (if (symbolp x)
  5695. (list x)
  5696. (list (car x) (list 'quote (cdr x)))))
  5697. (if (string-match
  5698. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5699. (symbol-name (car x)))
  5700. x nil))
  5701. varlist))))
  5702. ;;;###autoload
  5703. (defun org-run-like-in-org-mode (cmd)
  5704. (org-load-modules-maybe)
  5705. (unless org-local-vars
  5706. (setq org-local-vars (org-get-local-variables)))
  5707. (eval (list 'let org-local-vars
  5708. (list 'call-interactively (list 'quote cmd)))))
  5709. ;;;; Archiving
  5710. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5711. (defun org-archive-subtree (&optional find-done)
  5712. "Move the current subtree to the archive.
  5713. The archive can be a certain top-level heading in the current file, or in
  5714. a different file. The tree will be moved to that location, the subtree
  5715. heading be marked DONE, and the current time will be added.
  5716. When called with prefix argument FIND-DONE, find whole trees without any
  5717. open TODO items and archive them (after getting confirmation from the user).
  5718. If the cursor is not at a headline when this comand is called, try all level
  5719. 1 trees. If the cursor is on a headline, only try the direct children of
  5720. this heading."
  5721. (interactive "P")
  5722. (if find-done
  5723. (org-archive-all-done)
  5724. ;; Save all relevant TODO keyword-relatex variables
  5725. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5726. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5727. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5728. (tr-org-done-keywords org-done-keywords)
  5729. (tr-org-todo-regexp org-todo-regexp)
  5730. (tr-org-todo-line-regexp org-todo-line-regexp)
  5731. (tr-org-odd-levels-only org-odd-levels-only)
  5732. (this-buffer (current-buffer))
  5733. (org-archive-location org-archive-location)
  5734. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5735. ;; start of variables that will be used for saving context
  5736. ;; The compiler complains about them - keep them anyway!
  5737. (file (abbreviate-file-name (buffer-file-name)))
  5738. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  5739. (time (format-time-string
  5740. (substring (cdr org-time-stamp-formats) 1 -1)
  5741. (current-time)))
  5742. afile heading buffer level newfile-p
  5743. category todo priority
  5744. ;; start of variables that will be used for savind context
  5745. ltags itags prop)
  5746. ;; Try to find a local archive location
  5747. (save-excursion
  5748. (save-restriction
  5749. (widen)
  5750. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  5751. (if (and prop (string-match "\\S-" prop))
  5752. (setq org-archive-location prop)
  5753. (if (or (re-search-backward re nil t)
  5754. (re-search-forward re nil t))
  5755. (setq org-archive-location (match-string 1))))))
  5756. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5757. (progn
  5758. (setq afile (format (match-string 1 org-archive-location)
  5759. (file-name-nondirectory buffer-file-name))
  5760. heading (match-string 2 org-archive-location)))
  5761. (error "Invalid `org-archive-location'"))
  5762. (if (> (length afile) 0)
  5763. (setq newfile-p (not (file-exists-p afile))
  5764. buffer (find-file-noselect afile))
  5765. (setq buffer (current-buffer)))
  5766. (unless buffer
  5767. (error "Cannot access file \"%s\"" afile))
  5768. (if (and (> (length heading) 0)
  5769. (string-match "^\\*+" heading))
  5770. (setq level (match-end 0))
  5771. (setq heading nil level 0))
  5772. (save-excursion
  5773. (org-back-to-heading t)
  5774. ;; Get context information that will be lost by moving the tree
  5775. (org-refresh-category-properties)
  5776. (setq category (org-get-category)
  5777. todo (and (looking-at org-todo-line-regexp)
  5778. (match-string 2))
  5779. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  5780. ltags (org-get-tags)
  5781. itags (org-delete-all ltags (org-get-tags-at)))
  5782. (setq ltags (mapconcat 'identity ltags " ")
  5783. itags (mapconcat 'identity itags " "))
  5784. ;; We first only copy, in case something goes wrong
  5785. ;; we need to protect this-command, to avoid kill-region sets it,
  5786. ;; which would lead to duplication of subtrees
  5787. (let (this-command) (org-copy-subtree))
  5788. (set-buffer buffer)
  5789. ;; Enforce org-mode for the archive buffer
  5790. (if (not (org-mode-p))
  5791. ;; Force the mode for future visits.
  5792. (let ((org-insert-mode-line-in-empty-file t)
  5793. (org-inhibit-startup t))
  5794. (call-interactively 'org-mode)))
  5795. (when newfile-p
  5796. (goto-char (point-max))
  5797. (insert (format "\nArchived entries from file %s\n\n"
  5798. (buffer-file-name this-buffer))))
  5799. ;; Force the TODO keywords of the original buffer
  5800. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5801. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5802. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5803. (org-done-keywords tr-org-done-keywords)
  5804. (org-todo-regexp tr-org-todo-regexp)
  5805. (org-todo-line-regexp tr-org-todo-line-regexp)
  5806. (org-odd-levels-only
  5807. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5808. org-odd-levels-only
  5809. tr-org-odd-levels-only)))
  5810. (goto-char (point-min))
  5811. (show-all)
  5812. (if heading
  5813. (progn
  5814. (if (re-search-forward
  5815. (concat "^" (regexp-quote heading)
  5816. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  5817. nil t)
  5818. (goto-char (match-end 0))
  5819. ;; Heading not found, just insert it at the end
  5820. (goto-char (point-max))
  5821. (or (bolp) (insert "\n"))
  5822. (insert "\n" heading "\n")
  5823. (end-of-line 0))
  5824. ;; Make the subtree visible
  5825. (show-subtree)
  5826. (org-end-of-subtree t)
  5827. (skip-chars-backward " \t\r\n")
  5828. (and (looking-at "[ \t\r\n]*")
  5829. (replace-match "\n\n")))
  5830. ;; No specific heading, just go to end of file.
  5831. (goto-char (point-max)) (insert "\n"))
  5832. ;; Paste
  5833. (org-paste-subtree (org-get-valid-level level 1))
  5834. ;; Mark the entry as done
  5835. (when (and org-archive-mark-done
  5836. (looking-at org-todo-line-regexp)
  5837. (or (not (match-end 2))
  5838. (not (member (match-string 2) org-done-keywords))))
  5839. (let (org-log-done org-todo-log-states)
  5840. (org-todo
  5841. (car (or (member org-archive-mark-done org-done-keywords)
  5842. org-done-keywords)))))
  5843. ;; Add the context info
  5844. (when org-archive-save-context-info
  5845. (let ((l org-archive-save-context-info) e n v)
  5846. (while (setq e (pop l))
  5847. (when (and (setq v (symbol-value e))
  5848. (stringp v) (string-match "\\S-" v))
  5849. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  5850. (org-entry-put (point) n v)))))
  5851. ;; Save and kill the buffer, if it is not the same buffer.
  5852. (if (not (eq this-buffer buffer))
  5853. (progn (save-buffer) (kill-buffer buffer)))))
  5854. ;; Here we are back in the original buffer. Everything seems to have
  5855. ;; worked. So now cut the tree and finish up.
  5856. (let (this-command) (org-cut-subtree))
  5857. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  5858. (message "Subtree archived %s"
  5859. (if (eq this-buffer buffer)
  5860. (concat "under heading: " heading)
  5861. (concat "in file: " (abbreviate-file-name afile)))))))
  5862. (defun org-get-category (&optional pos)
  5863. "Get the category applying to position POS."
  5864. (get-text-property (or pos (point)) 'org-category))
  5865. (defun org-refresh-category-properties ()
  5866. "Refresh category text properties in the buffer."
  5867. (let ((def-cat (cond
  5868. ((null org-category)
  5869. (if buffer-file-name
  5870. (file-name-sans-extension
  5871. (file-name-nondirectory buffer-file-name))
  5872. "???"))
  5873. ((symbolp org-category) (symbol-name org-category))
  5874. (t org-category)))
  5875. beg end cat pos optionp)
  5876. (org-unmodified
  5877. (save-excursion
  5878. (save-restriction
  5879. (widen)
  5880. (goto-char (point-min))
  5881. (put-text-property (point) (point-max) 'org-category def-cat)
  5882. (while (re-search-forward
  5883. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5884. (setq pos (match-end 0)
  5885. optionp (equal (char-after (match-beginning 0)) ?#)
  5886. cat (org-trim (match-string 2)))
  5887. (if optionp
  5888. (setq beg (point-at-bol) end (point-max))
  5889. (org-back-to-heading t)
  5890. (setq beg (point) end (org-end-of-subtree t t)))
  5891. (put-text-property beg end 'org-category cat)
  5892. (goto-char pos)))))))
  5893. (defun org-archive-all-done (&optional tag)
  5894. "Archive sublevels of the current tree without open TODO items.
  5895. If the cursor is not on a headline, try all level 1 trees. If
  5896. it is on a headline, try all direct children.
  5897. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5898. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5899. (rea (concat ".*:" org-archive-tag ":"))
  5900. (begm (make-marker))
  5901. (endm (make-marker))
  5902. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5903. "Move subtree to archive (no open TODO items)? "))
  5904. beg end (cntarch 0))
  5905. (if (org-on-heading-p)
  5906. (progn
  5907. (setq re1 (concat "^" (regexp-quote
  5908. (make-string
  5909. (1+ (- (match-end 0) (match-beginning 0) 1))
  5910. ?*))
  5911. " "))
  5912. (move-marker begm (point))
  5913. (move-marker endm (org-end-of-subtree t)))
  5914. (setq re1 "^* ")
  5915. (move-marker begm (point-min))
  5916. (move-marker endm (point-max)))
  5917. (save-excursion
  5918. (goto-char begm)
  5919. (while (re-search-forward re1 endm t)
  5920. (setq beg (match-beginning 0)
  5921. end (save-excursion (org-end-of-subtree t) (point)))
  5922. (goto-char beg)
  5923. (if (re-search-forward re end t)
  5924. (goto-char end)
  5925. (goto-char beg)
  5926. (if (and (or (not tag) (not (looking-at rea)))
  5927. (y-or-n-p question))
  5928. (progn
  5929. (if tag
  5930. (org-toggle-tag org-archive-tag 'on)
  5931. (org-archive-subtree))
  5932. (setq cntarch (1+ cntarch)))
  5933. (goto-char end)))))
  5934. (message "%d trees archived" cntarch)))
  5935. (defun org-cycle-hide-drawers (state)
  5936. "Re-hide all drawers after a visibility state change."
  5937. (when (and (org-mode-p)
  5938. (not (memq state '(overview folded))))
  5939. (save-excursion
  5940. (let* ((globalp (memq state '(contents all)))
  5941. (beg (if globalp (point-min) (point)))
  5942. (end (if globalp (point-max) (org-end-of-subtree t))))
  5943. (goto-char beg)
  5944. (while (re-search-forward org-drawer-regexp end t)
  5945. (org-flag-drawer t))))))
  5946. (defun org-flag-drawer (flag)
  5947. (save-excursion
  5948. (beginning-of-line 1)
  5949. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5950. (let ((b (match-end 0))
  5951. (outline-regexp org-outline-regexp))
  5952. (if (re-search-forward
  5953. "^[ \t]*:END:"
  5954. (save-excursion (outline-next-heading) (point)) t)
  5955. (outline-flag-region b (point-at-eol) flag)
  5956. (error ":END: line missing"))))))
  5957. (defun org-cycle-hide-archived-subtrees (state)
  5958. "Re-hide all archived subtrees after a visibility state change."
  5959. (when (and (not org-cycle-open-archived-trees)
  5960. (not (memq state '(overview folded))))
  5961. (save-excursion
  5962. (let* ((globalp (memq state '(contents all)))
  5963. (beg (if globalp (point-min) (point)))
  5964. (end (if globalp (point-max) (org-end-of-subtree t))))
  5965. (org-hide-archived-subtrees beg end)
  5966. (goto-char beg)
  5967. (if (looking-at (concat ".*:" org-archive-tag ":"))
  5968. (message "%s" (substitute-command-keys
  5969. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  5970. (defun org-force-cycle-archived ()
  5971. "Cycle subtree even if it is archived."
  5972. (interactive)
  5973. (setq this-command 'org-cycle)
  5974. (let ((org-cycle-open-archived-trees t))
  5975. (call-interactively 'org-cycle)))
  5976. (defun org-hide-archived-subtrees (beg end)
  5977. "Re-hide all archived subtrees after a visibility state change."
  5978. (save-excursion
  5979. (let* ((re (concat ":" org-archive-tag ":")))
  5980. (goto-char beg)
  5981. (while (re-search-forward re end t)
  5982. (and (org-on-heading-p) (hide-subtree))
  5983. (org-end-of-subtree t)))))
  5984. (defun org-toggle-tag (tag &optional onoff)
  5985. "Toggle the tag TAG for the current line.
  5986. If ONOFF is `on' or `off', don't toggle but set to this state."
  5987. (unless (org-on-heading-p t) (error "Not on headling"))
  5988. (let (res current)
  5989. (save-excursion
  5990. (beginning-of-line)
  5991. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  5992. (point-at-eol) t)
  5993. (progn
  5994. (setq current (match-string 1))
  5995. (replace-match ""))
  5996. (setq current ""))
  5997. (setq current (nreverse (org-split-string current ":")))
  5998. (cond
  5999. ((eq onoff 'on)
  6000. (setq res t)
  6001. (or (member tag current) (push tag current)))
  6002. ((eq onoff 'off)
  6003. (or (not (member tag current)) (setq current (delete tag current))))
  6004. (t (if (member tag current)
  6005. (setq current (delete tag current))
  6006. (setq res t)
  6007. (push tag current))))
  6008. (end-of-line 1)
  6009. (if current
  6010. (progn
  6011. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  6012. (org-set-tags nil t))
  6013. (delete-horizontal-space))
  6014. (run-hooks 'org-after-tags-change-hook))
  6015. res))
  6016. (defun org-toggle-archive-tag (&optional arg)
  6017. "Toggle the archive tag for the current headline.
  6018. With prefix ARG, check all children of current headline and offer tagging
  6019. the children that do not contain any open TODO items."
  6020. (interactive "P")
  6021. (if arg
  6022. (org-archive-all-done 'tag)
  6023. (let (set)
  6024. (save-excursion
  6025. (org-back-to-heading t)
  6026. (setq set (org-toggle-tag org-archive-tag))
  6027. (when set (hide-subtree)))
  6028. (and set (beginning-of-line 1))
  6029. (message "Subtree %s" (if set "archived" "unarchived")))))
  6030. ;;;; Link Stuff
  6031. ;;; Link abbreviations
  6032. (defun org-link-expand-abbrev (link)
  6033. "Apply replacements as defined in `org-link-abbrev-alist."
  6034. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6035. (let* ((key (match-string 1 link))
  6036. (as (or (assoc key org-link-abbrev-alist-local)
  6037. (assoc key org-link-abbrev-alist)))
  6038. (tag (and (match-end 2) (match-string 3 link)))
  6039. rpl)
  6040. (if (not as)
  6041. link
  6042. (setq rpl (cdr as))
  6043. (cond
  6044. ((symbolp rpl) (funcall rpl tag))
  6045. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6046. (t (concat rpl tag)))))
  6047. link))
  6048. ;;; Storing and inserting links
  6049. (defvar org-insert-link-history nil
  6050. "Minibuffer history for links inserted with `org-insert-link'.")
  6051. (defvar org-stored-links nil
  6052. "Contains the links stored with `org-store-link'.")
  6053. (defvar org-store-link-plist nil
  6054. "Plist with info about the most recently link created with `org-store-link'.")
  6055. (defvar org-link-protocols nil
  6056. "Link protocols added to Org-mode using `org-add-link-type'.")
  6057. (defvar org-store-link-functions nil
  6058. "List of functions that are called to create and store a link.
  6059. Each function will be called in turn until one returns a non-nil
  6060. value. Each function should check if it is responsible for creating
  6061. this link (for example by looking at the major mode).
  6062. If not, it must exit and return nil.
  6063. If yes, it should return a non-nil value after a calling
  6064. `org-store-link-props' with a list of properties and values.
  6065. Special properties are:
  6066. :type The link prefix. like \"http\". This must be given.
  6067. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6068. This is obligatory as well.
  6069. :description Optional default description for the second pair
  6070. of brackets in an Org-mode link. The user can still change
  6071. this when inserting this link into an Org-mode buffer.
  6072. In addition to these, any additional properties can be specified
  6073. and then used in remember templates.")
  6074. (defun org-add-link-type (type &optional follow export)
  6075. "Add TYPE to the list of `org-link-types'.
  6076. Re-compute all regular expressions depending on `org-link-types'
  6077. FOLLOW and EXPORT are two functions.
  6078. FOLLOW should take the link path as the single argument and do whatever
  6079. is necessary to follow the link, for example find a file or display
  6080. a mail message.
  6081. EXPORT should format the link path for export to one of the export formats.
  6082. It should be a function accepting three arguments:
  6083. path the path of the link, the text after the prefix (like \"http:\")
  6084. desc the description of the link, if any, nil if there was no descripton
  6085. format the export format, a symbol like `html' or `latex'.
  6086. The function may use the FORMAT information to return different values
  6087. depending on the format. The return value will be put literally into
  6088. the exported file.
  6089. Org-mode has a built-in default for exporting links. If you are happy with
  6090. this default, there is no need to define an export function for the link
  6091. type. For a simple example of an export function, see `org-bbdb.el'."
  6092. (add-to-list 'org-link-types type t)
  6093. (org-make-link-regexps)
  6094. (if (assoc type org-link-protocols)
  6095. (setcdr (assoc type org-link-protocols) (list follow export))
  6096. (push (list type follow export) org-link-protocols)))
  6097. ;;;###autoload
  6098. (defun org-store-link (arg)
  6099. "\\<org-mode-map>Store an org-link to the current location.
  6100. This link is added to `org-stored-links' and can later be inserted
  6101. into an org-buffer with \\[org-insert-link].
  6102. For some link types, a prefix arg is interpreted:
  6103. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  6104. For file links, arg negates `org-context-in-file-links'."
  6105. (interactive "P")
  6106. (org-load-modules-maybe)
  6107. (setq org-store-link-plist nil) ; reset
  6108. (let (link cpltxt desc description search txt)
  6109. (cond
  6110. ((run-hook-with-args-until-success 'org-store-link-functions)
  6111. (setq link (plist-get org-store-link-plist :link)
  6112. desc (or (plist-get org-store-link-plist :description) link)))
  6113. ((eq major-mode 'calendar-mode)
  6114. (let ((cd (calendar-cursor-to-date)))
  6115. (setq link
  6116. (format-time-string
  6117. (car org-time-stamp-formats)
  6118. (apply 'encode-time
  6119. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6120. nil nil nil))))
  6121. (org-store-link-props :type "calendar" :date cd)))
  6122. ((eq major-mode 'w3-mode)
  6123. (setq cpltxt (url-view-url t)
  6124. link (org-make-link cpltxt))
  6125. (org-store-link-props :type "w3" :url (url-view-url t)))
  6126. ((eq major-mode 'w3m-mode)
  6127. (setq cpltxt (or w3m-current-title w3m-current-url)
  6128. link (org-make-link w3m-current-url))
  6129. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6130. ((setq search (run-hook-with-args-until-success
  6131. 'org-create-file-search-functions))
  6132. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6133. "::" search))
  6134. (setq cpltxt (or description link)))
  6135. ((eq major-mode 'image-mode)
  6136. (setq cpltxt (concat "file:"
  6137. (abbreviate-file-name buffer-file-name))
  6138. link (org-make-link cpltxt))
  6139. (org-store-link-props :type "image" :file buffer-file-name))
  6140. ((eq major-mode 'dired-mode)
  6141. ;; link to the file in the current line
  6142. (setq cpltxt (concat "file:"
  6143. (abbreviate-file-name
  6144. (expand-file-name
  6145. (dired-get-filename nil t))))
  6146. link (org-make-link cpltxt)))
  6147. ((and buffer-file-name (org-mode-p))
  6148. ;; Just link to current headline
  6149. (setq cpltxt (concat "file:"
  6150. (abbreviate-file-name buffer-file-name)))
  6151. ;; Add a context search string
  6152. (when (org-xor org-context-in-file-links arg)
  6153. ;; Check if we are on a target
  6154. (if (org-in-regexp "<<\\(.*?\\)>>")
  6155. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  6156. (setq txt (cond
  6157. ((org-on-heading-p) nil)
  6158. ((org-region-active-p)
  6159. (buffer-substring (region-beginning) (region-end)))
  6160. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  6161. (when (or (null txt) (string-match "\\S-" txt))
  6162. (setq cpltxt
  6163. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6164. desc "NONE"))))
  6165. (if (string-match "::\\'" cpltxt)
  6166. (setq cpltxt (substring cpltxt 0 -2)))
  6167. (setq link (org-make-link cpltxt)))
  6168. ((buffer-file-name (buffer-base-buffer))
  6169. ;; Just link to this file here.
  6170. (setq cpltxt (concat "file:"
  6171. (abbreviate-file-name
  6172. (buffer-file-name (buffer-base-buffer)))))
  6173. ;; Add a context string
  6174. (when (org-xor org-context-in-file-links arg)
  6175. (setq txt (if (org-region-active-p)
  6176. (buffer-substring (region-beginning) (region-end))
  6177. (buffer-substring (point-at-bol) (point-at-eol))))
  6178. ;; Only use search option if there is some text.
  6179. (when (string-match "\\S-" txt)
  6180. (setq cpltxt
  6181. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6182. desc "NONE")))
  6183. (setq link (org-make-link cpltxt)))
  6184. ((interactive-p)
  6185. (error "Cannot link to a buffer which is not visiting a file"))
  6186. (t (setq link nil)))
  6187. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6188. (setq link (or link cpltxt)
  6189. desc (or desc cpltxt))
  6190. (if (equal desc "NONE") (setq desc nil))
  6191. (if (and (interactive-p) link)
  6192. (progn
  6193. (setq org-stored-links
  6194. (cons (list link desc) org-stored-links))
  6195. (message "Stored: %s" (or desc link)))
  6196. (and link (org-make-link-string link desc)))))
  6197. (defun org-store-link-props (&rest plist)
  6198. "Store link properties, extract names and addresses."
  6199. (let (x adr)
  6200. (when (setq x (plist-get plist :from))
  6201. (setq adr (mail-extract-address-components x))
  6202. (plist-put plist :fromname (car adr))
  6203. (plist-put plist :fromaddress (nth 1 adr)))
  6204. (when (setq x (plist-get plist :to))
  6205. (setq adr (mail-extract-address-components x))
  6206. (plist-put plist :toname (car adr))
  6207. (plist-put plist :toaddress (nth 1 adr))))
  6208. (let ((from (plist-get plist :from))
  6209. (to (plist-get plist :to)))
  6210. (when (and from to org-from-is-user-regexp)
  6211. (plist-put plist :fromto
  6212. (if (string-match org-from-is-user-regexp from)
  6213. (concat "to %t")
  6214. (concat "from %f")))))
  6215. (setq org-store-link-plist plist))
  6216. (defun org-add-link-props (&rest plist)
  6217. "Add these properties to the link property list."
  6218. (let (key value)
  6219. (while plist
  6220. (setq key (pop plist) value (pop plist))
  6221. (setq org-store-link-plist
  6222. (plist-put org-store-link-plist key value)))))
  6223. (defun org-email-link-description (&optional fmt)
  6224. "Return the description part of an email link.
  6225. This takes information from `org-store-link-plist' and formats it
  6226. according to FMT (default from `org-email-link-description-format')."
  6227. (setq fmt (or fmt org-email-link-description-format))
  6228. (let* ((p org-store-link-plist)
  6229. (to (plist-get p :toaddress))
  6230. (from (plist-get p :fromaddress))
  6231. (table
  6232. (list
  6233. (cons "%c" (plist-get p :fromto))
  6234. (cons "%F" (plist-get p :from))
  6235. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6236. (cons "%T" (plist-get p :to))
  6237. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6238. (cons "%s" (plist-get p :subject))
  6239. (cons "%m" (plist-get p :message-id)))))
  6240. (when (string-match "%c" fmt)
  6241. ;; Check if the user wrote this message
  6242. (if (and org-from-is-user-regexp from to
  6243. (save-match-data (string-match org-from-is-user-regexp from)))
  6244. (setq fmt (replace-match "to %t" t t fmt))
  6245. (setq fmt (replace-match "from %f" t t fmt))))
  6246. (org-replace-escapes fmt table)))
  6247. (defun org-make-org-heading-search-string (&optional string heading)
  6248. "Make search string for STRING or current headline."
  6249. (interactive)
  6250. (let ((s (or string (org-get-heading))))
  6251. (unless (and string (not heading))
  6252. ;; We are using a headline, clean up garbage in there.
  6253. (if (string-match org-todo-regexp s)
  6254. (setq s (replace-match "" t t s)))
  6255. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6256. (setq s (replace-match "" t t s)))
  6257. (setq s (org-trim s))
  6258. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6259. org-comment-string "\\)") s)
  6260. (setq s (replace-match "" t t s)))
  6261. (while (string-match org-ts-regexp s)
  6262. (setq s (replace-match "" t t s))))
  6263. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6264. (setq s (replace-match " " t t s)))
  6265. (or string (setq s (concat "*" s))) ; Add * for headlines
  6266. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6267. (defun org-make-link (&rest strings)
  6268. "Concatenate STRINGS."
  6269. (apply 'concat strings))
  6270. (defun org-make-link-string (link &optional description)
  6271. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6272. (unless (string-match "\\S-" link)
  6273. (error "Empty link"))
  6274. (when (stringp description)
  6275. ;; Remove brackets from the description, they are fatal.
  6276. (while (string-match "\\[" description)
  6277. (setq description (replace-match "{" t t description)))
  6278. (while (string-match "\\]" description)
  6279. (setq description (replace-match "}" t t description))))
  6280. (when (equal (org-link-escape link) description)
  6281. ;; No description needed, it is identical
  6282. (setq description nil))
  6283. (when (and (not description)
  6284. (not (equal link (org-link-escape link))))
  6285. (setq description link))
  6286. (concat "[[" (org-link-escape link) "]"
  6287. (if description (concat "[" description "]") "")
  6288. "]"))
  6289. (defconst org-link-escape-chars
  6290. '((?\ . "%20")
  6291. (?\[ . "%5B")
  6292. (?\] . "%5D")
  6293. (?\340 . "%E0") ; `a
  6294. (?\342 . "%E2") ; ^a
  6295. (?\347 . "%E7") ; ,c
  6296. (?\350 . "%E8") ; `e
  6297. (?\351 . "%E9") ; 'e
  6298. (?\352 . "%EA") ; ^e
  6299. (?\356 . "%EE") ; ^i
  6300. (?\364 . "%F4") ; ^o
  6301. (?\371 . "%F9") ; `u
  6302. (?\373 . "%FB") ; ^u
  6303. (?\; . "%3B")
  6304. (?? . "%3F")
  6305. (?= . "%3D")
  6306. (?+ . "%2B")
  6307. )
  6308. "Association list of escapes for some characters problematic in links.
  6309. This is the list that is used for internal purposes.")
  6310. (defconst org-link-escape-chars-browser
  6311. '((?\ . "%20")) ; 32 for the SPC char
  6312. "Association list of escapes for some characters problematic in links.
  6313. This is the list that is used before handing over to the browser.")
  6314. (defun org-link-escape (text &optional table)
  6315. "Escape charaters in TEXT that are problematic for links."
  6316. (setq table (or table org-link-escape-chars))
  6317. (when text
  6318. (let ((re (mapconcat (lambda (x) (regexp-quote
  6319. (char-to-string (car x))))
  6320. table "\\|")))
  6321. (while (string-match re text)
  6322. (setq text
  6323. (replace-match
  6324. (cdr (assoc (string-to-char (match-string 0 text))
  6325. table))
  6326. t t text)))
  6327. text)))
  6328. (defun org-link-unescape (text &optional table)
  6329. "Reverse the action of `org-link-escape'."
  6330. (setq table (or table org-link-escape-chars))
  6331. (when text
  6332. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6333. table "\\|")))
  6334. (while (string-match re text)
  6335. (setq text
  6336. (replace-match
  6337. (char-to-string (car (rassoc (match-string 0 text) table)))
  6338. t t text)))
  6339. text)))
  6340. (defun org-xor (a b)
  6341. "Exclusive or."
  6342. (if a (not b) b))
  6343. (defun org-get-header (header)
  6344. "Find a header field in the current buffer."
  6345. (save-excursion
  6346. (goto-char (point-min))
  6347. (let ((case-fold-search t) s)
  6348. (cond
  6349. ((eq header 'from)
  6350. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6351. (setq s (match-string 1)))
  6352. (while (string-match "\"" s)
  6353. (setq s (replace-match "" t t s)))
  6354. (if (string-match "[<(].*" s)
  6355. (setq s (replace-match "" t t s))))
  6356. ((eq header 'message-id)
  6357. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6358. (setq s (match-string 1))))
  6359. ((eq header 'subject)
  6360. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6361. (setq s (match-string 1)))))
  6362. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6363. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6364. s)))
  6365. (defun org-fixup-message-id-for-http (s)
  6366. "Replace special characters in a message id, so it can be used in an http query."
  6367. (while (string-match "<" s)
  6368. (setq s (replace-match "%3C" t t s)))
  6369. (while (string-match ">" s)
  6370. (setq s (replace-match "%3E" t t s)))
  6371. (while (string-match "@" s)
  6372. (setq s (replace-match "%40" t t s)))
  6373. s)
  6374. ;;;###autoload
  6375. (defun org-insert-link-global ()
  6376. "Insert a link like Org-mode does.
  6377. This command can be called in any mode to insert a link in Org-mode syntax."
  6378. (interactive)
  6379. (org-load-modules-maybe)
  6380. (org-run-like-in-org-mode 'org-insert-link))
  6381. (defun org-insert-link (&optional complete-file)
  6382. "Insert a link. At the prompt, enter the link.
  6383. Completion can be used to select a link previously stored with
  6384. `org-store-link'. When the empty string is entered (i.e. if you just
  6385. press RET at the prompt), the link defaults to the most recently
  6386. stored link. As SPC triggers completion in the minibuffer, you need to
  6387. use M-SPC or C-q SPC to force the insertion of a space character.
  6388. You will also be prompted for a description, and if one is given, it will
  6389. be displayed in the buffer instead of the link.
  6390. If there is already a link at point, this command will allow you to edit link
  6391. and description parts.
  6392. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  6393. selected using completion. The path to the file will be relative to
  6394. the current directory if the file is in the current directory or a
  6395. subdirectory. Otherwise, the link will be the absolute path as
  6396. completed in the minibuffer (i.e. normally ~/path/to/file).
  6397. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  6398. is in the current directory or below.
  6399. With three \\[universal-argument] prefixes, negate the meaning of
  6400. `org-keep-stored-link-after-insertion'."
  6401. (interactive "P")
  6402. (let* ((wcf (current-window-configuration))
  6403. (region (if (org-region-active-p)
  6404. (buffer-substring (region-beginning) (region-end))))
  6405. (remove (and region (list (region-beginning) (region-end))))
  6406. (desc region)
  6407. tmphist ; byte-compile incorrectly complains about this
  6408. link entry file)
  6409. (cond
  6410. ((org-in-regexp org-bracket-link-regexp 1)
  6411. ;; We do have a link at point, and we are going to edit it.
  6412. (setq remove (list (match-beginning 0) (match-end 0)))
  6413. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6414. (setq link (read-string "Link: "
  6415. (org-link-unescape
  6416. (org-match-string-no-properties 1)))))
  6417. ((or (org-in-regexp org-angle-link-re)
  6418. (org-in-regexp org-plain-link-re))
  6419. ;; Convert to bracket link
  6420. (setq remove (list (match-beginning 0) (match-end 0))
  6421. link (read-string "Link: "
  6422. (org-remove-angle-brackets (match-string 0)))))
  6423. ((equal complete-file '(4))
  6424. ;; Completing read for file names.
  6425. (setq file (read-file-name "File: "))
  6426. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6427. (pwd1 (file-name-as-directory (abbreviate-file-name
  6428. (expand-file-name ".")))))
  6429. (cond
  6430. ((equal complete-file '(16))
  6431. (setq link (org-make-link
  6432. "file:"
  6433. (abbreviate-file-name (expand-file-name file)))))
  6434. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6435. (setq link (org-make-link "file:" (match-string 1 file))))
  6436. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6437. (expand-file-name file))
  6438. (setq link (org-make-link
  6439. "file:" (match-string 1 (expand-file-name file)))))
  6440. (t (setq link (org-make-link "file:" file))))))
  6441. (t
  6442. ;; Read link, with completion for stored links.
  6443. (with-output-to-temp-buffer "*Org Links*"
  6444. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6445. (when org-stored-links
  6446. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6447. (princ (mapconcat
  6448. (lambda (x)
  6449. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6450. (reverse org-stored-links) "\n"))))
  6451. (let ((cw (selected-window)))
  6452. (select-window (get-buffer-window "*Org Links*"))
  6453. (shrink-window-if-larger-than-buffer)
  6454. (setq truncate-lines t)
  6455. (select-window cw))
  6456. ;; Fake a link history, containing the stored links.
  6457. (setq tmphist (append (mapcar 'car org-stored-links)
  6458. org-insert-link-history))
  6459. (unwind-protect
  6460. (setq link (org-completing-read
  6461. "Link: "
  6462. (append
  6463. (mapcar (lambda (x) (list (concat (car x) ":")))
  6464. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6465. (mapcar (lambda (x) (list (concat x ":")))
  6466. org-link-types))
  6467. nil nil nil
  6468. 'tmphist
  6469. (or (car (car org-stored-links)))))
  6470. (set-window-configuration wcf)
  6471. (kill-buffer "*Org Links*"))
  6472. (setq entry (assoc link org-stored-links))
  6473. (or entry (push link org-insert-link-history))
  6474. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6475. (not org-keep-stored-link-after-insertion))
  6476. (setq org-stored-links (delq (assoc link org-stored-links)
  6477. org-stored-links)))
  6478. (setq desc (or desc (nth 1 entry)))))
  6479. (if (string-match org-plain-link-re link)
  6480. ;; URL-like link, normalize the use of angular brackets.
  6481. (setq link (org-make-link (org-remove-angle-brackets link))))
  6482. ;; Check if we are linking to the current file with a search option
  6483. ;; If yes, simplify the link by using only the search option.
  6484. (when (and buffer-file-name
  6485. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6486. (let* ((path (match-string 1 link))
  6487. (case-fold-search nil)
  6488. (search (match-string 2 link)))
  6489. (save-match-data
  6490. (if (equal (file-truename buffer-file-name) (file-truename path))
  6491. ;; We are linking to this same file, with a search option
  6492. (setq link search)))))
  6493. ;; Check if we can/should use a relative path. If yes, simplify the link
  6494. (when (string-match "\\<file:\\(.*\\)" link)
  6495. (let* ((path (match-string 1 link))
  6496. (origpath path)
  6497. (case-fold-search nil))
  6498. (cond
  6499. ((eq org-link-file-path-type 'absolute)
  6500. (setq path (abbreviate-file-name (expand-file-name path))))
  6501. ((eq org-link-file-path-type 'noabbrev)
  6502. (setq path (expand-file-name path)))
  6503. ((eq org-link-file-path-type 'relative)
  6504. (setq path (file-relative-name path)))
  6505. (t
  6506. (save-match-data
  6507. (if (string-match (concat "^" (regexp-quote
  6508. (file-name-as-directory
  6509. (expand-file-name "."))))
  6510. (expand-file-name path))
  6511. ;; We are linking a file with relative path name.
  6512. (setq path (substring (expand-file-name path)
  6513. (match-end 0)))))))
  6514. (setq link (concat "file:" path))
  6515. (if (equal desc origpath)
  6516. (setq desc path))))
  6517. (setq desc (read-string "Description: " desc))
  6518. (unless (string-match "\\S-" desc) (setq desc nil))
  6519. (if remove (apply 'delete-region remove))
  6520. (insert (org-make-link-string link desc))))
  6521. (defun org-completing-read (&rest args)
  6522. (let ((minibuffer-local-completion-map
  6523. (copy-keymap minibuffer-local-completion-map)))
  6524. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6525. (apply 'completing-read args)))
  6526. ;;; Opening/following a link
  6527. (defvar org-link-search-failed nil)
  6528. (defun org-next-link ()
  6529. "Move forward to the next link.
  6530. If the link is in hidden text, expose it."
  6531. (interactive)
  6532. (when (and org-link-search-failed (eq this-command last-command))
  6533. (goto-char (point-min))
  6534. (message "Link search wrapped back to beginning of buffer"))
  6535. (setq org-link-search-failed nil)
  6536. (let* ((pos (point))
  6537. (ct (org-context))
  6538. (a (assoc :link ct)))
  6539. (if a (goto-char (nth 2 a)))
  6540. (if (re-search-forward org-any-link-re nil t)
  6541. (progn
  6542. (goto-char (match-beginning 0))
  6543. (if (org-invisible-p) (org-show-context)))
  6544. (goto-char pos)
  6545. (setq org-link-search-failed t)
  6546. (error "No further link found"))))
  6547. (defun org-previous-link ()
  6548. "Move backward to the previous link.
  6549. If the link is in hidden text, expose it."
  6550. (interactive)
  6551. (when (and org-link-search-failed (eq this-command last-command))
  6552. (goto-char (point-max))
  6553. (message "Link search wrapped back to end of buffer"))
  6554. (setq org-link-search-failed nil)
  6555. (let* ((pos (point))
  6556. (ct (org-context))
  6557. (a (assoc :link ct)))
  6558. (if a (goto-char (nth 1 a)))
  6559. (if (re-search-backward org-any-link-re nil t)
  6560. (progn
  6561. (goto-char (match-beginning 0))
  6562. (if (org-invisible-p) (org-show-context)))
  6563. (goto-char pos)
  6564. (setq org-link-search-failed t)
  6565. (error "No further link found"))))
  6566. (defun org-find-file-at-mouse (ev)
  6567. "Open file link or URL at mouse."
  6568. (interactive "e")
  6569. (mouse-set-point ev)
  6570. (org-open-at-point 'in-emacs))
  6571. (defun org-open-at-mouse (ev)
  6572. "Open file link or URL at mouse."
  6573. (interactive "e")
  6574. (mouse-set-point ev)
  6575. (org-open-at-point))
  6576. (defvar org-window-config-before-follow-link nil
  6577. "The window configuration before following a link.
  6578. This is saved in case the need arises to restore it.")
  6579. (defvar org-open-link-marker (make-marker)
  6580. "Marker pointing to the location where `org-open-at-point; was called.")
  6581. ;;;###autoload
  6582. (defun org-open-at-point-global ()
  6583. "Follow a link like Org-mode does.
  6584. This command can be called in any mode to follow a link that has
  6585. Org-mode syntax."
  6586. (interactive)
  6587. (org-run-like-in-org-mode 'org-open-at-point))
  6588. (defun org-open-at-point (&optional in-emacs)
  6589. "Open link at or after point.
  6590. If there is no link at point, this function will search forward up to
  6591. the end of the current subtree.
  6592. Normally, files will be opened by an appropriate application. If the
  6593. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6594. (interactive "P")
  6595. (org-load-modules-maybe)
  6596. (move-marker org-open-link-marker (point))
  6597. (setq org-window-config-before-follow-link (current-window-configuration))
  6598. (org-remove-occur-highlights nil nil t)
  6599. (if (org-at-timestamp-p t)
  6600. (org-follow-timestamp-link)
  6601. (let (type path link line search (pos (point)))
  6602. (catch 'match
  6603. (save-excursion
  6604. (skip-chars-forward "^]\n\r")
  6605. (when (org-in-regexp org-bracket-link-regexp)
  6606. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6607. (while (string-match " *\n *" link)
  6608. (setq link (replace-match " " t t link)))
  6609. (setq link (org-link-expand-abbrev link))
  6610. (if (string-match org-link-re-with-space2 link)
  6611. (setq type (match-string 1 link) path (match-string 2 link))
  6612. (setq type "thisfile" path link))
  6613. (throw 'match t)))
  6614. (when (get-text-property (point) 'org-linked-text)
  6615. (setq type "thisfile"
  6616. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6617. (1+ (point)) (point))
  6618. path (buffer-substring
  6619. (previous-single-property-change pos 'org-linked-text)
  6620. (next-single-property-change pos 'org-linked-text)))
  6621. (throw 'match t))
  6622. (save-excursion
  6623. (when (or (org-in-regexp org-angle-link-re)
  6624. (org-in-regexp org-plain-link-re))
  6625. (setq type (match-string 1) path (match-string 2))
  6626. (throw 'match t)))
  6627. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6628. (setq type "tree-match"
  6629. path (match-string 1))
  6630. (throw 'match t))
  6631. (save-excursion
  6632. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6633. (setq type "tags"
  6634. path (match-string 1))
  6635. (while (string-match ":" path)
  6636. (setq path (replace-match "+" t t path)))
  6637. (throw 'match t))))
  6638. (unless path
  6639. (error "No link found"))
  6640. ;; Remove any trailing spaces in path
  6641. (if (string-match " +\\'" path)
  6642. (setq path (replace-match "" t t path)))
  6643. (cond
  6644. ((assoc type org-link-protocols)
  6645. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6646. ((equal type "mailto")
  6647. (let ((cmd (car org-link-mailto-program))
  6648. (args (cdr org-link-mailto-program)) args1
  6649. (address path) (subject "") a)
  6650. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6651. (setq address (match-string 1 path)
  6652. subject (org-link-escape (match-string 2 path))))
  6653. (while args
  6654. (cond
  6655. ((not (stringp (car args))) (push (pop args) args1))
  6656. (t (setq a (pop args))
  6657. (if (string-match "%a" a)
  6658. (setq a (replace-match address t t a)))
  6659. (if (string-match "%s" a)
  6660. (setq a (replace-match subject t t a)))
  6661. (push a args1))))
  6662. (apply cmd (nreverse args1))))
  6663. ((member type '("http" "https" "ftp" "news"))
  6664. (browse-url (concat type ":" (org-link-escape
  6665. path org-link-escape-chars-browser))))
  6666. ((member type '("message"))
  6667. (browse-url (concat type ":" path)))
  6668. ((string= type "tags")
  6669. (org-tags-view in-emacs path))
  6670. ((string= type "thisfile")
  6671. (if in-emacs
  6672. (switch-to-buffer-other-window
  6673. (org-get-buffer-for-internal-link (current-buffer)))
  6674. (org-mark-ring-push))
  6675. (let ((cmd `(org-link-search
  6676. ,path
  6677. ,(cond ((equal in-emacs '(4)) 'occur)
  6678. ((equal in-emacs '(16)) 'org-occur)
  6679. (t nil))
  6680. ,pos)))
  6681. (condition-case nil (eval cmd)
  6682. (error (progn (widen) (eval cmd))))))
  6683. ((string= type "tree-match")
  6684. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6685. ((string= type "file")
  6686. (if (string-match "::\\([0-9]+\\)\\'" path)
  6687. (setq line (string-to-number (match-string 1 path))
  6688. path (substring path 0 (match-beginning 0)))
  6689. (if (string-match "::\\(.+\\)\\'" path)
  6690. (setq search (match-string 1 path)
  6691. path (substring path 0 (match-beginning 0)))))
  6692. (if (string-match "[*?{]" (file-name-nondirectory path))
  6693. (dired path)
  6694. (org-open-file path in-emacs line search)))
  6695. ((string= type "news")
  6696. (require 'org-gnus)
  6697. (org-gnus-follow-link path))
  6698. ((string= type "shell")
  6699. (let ((cmd path))
  6700. (if (or (not org-confirm-shell-link-function)
  6701. (funcall org-confirm-shell-link-function
  6702. (format "Execute \"%s\" in shell? "
  6703. (org-add-props cmd nil
  6704. 'face 'org-warning))))
  6705. (progn
  6706. (message "Executing %s" cmd)
  6707. (shell-command cmd))
  6708. (error "Abort"))))
  6709. ((string= type "elisp")
  6710. (let ((cmd path))
  6711. (if (or (not org-confirm-elisp-link-function)
  6712. (funcall org-confirm-elisp-link-function
  6713. (format "Execute \"%s\" as elisp? "
  6714. (org-add-props cmd nil
  6715. 'face 'org-warning))))
  6716. (message "%s => %s" cmd (eval (read cmd)))
  6717. (error "Abort"))))
  6718. (t
  6719. (browse-url-at-point)))))
  6720. (move-marker org-open-link-marker nil)
  6721. (run-hook-with-args 'org-follow-link-hook))
  6722. ;;; File search
  6723. (defvar org-create-file-search-functions nil
  6724. "List of functions to construct the right search string for a file link.
  6725. These functions are called in turn with point at the location to
  6726. which the link should point.
  6727. A function in the hook should first test if it would like to
  6728. handle this file type, for example by checking the major-mode or
  6729. the file extension. If it decides not to handle this file, it
  6730. should just return nil to give other functions a chance. If it
  6731. does handle the file, it must return the search string to be used
  6732. when following the link. The search string will be part of the
  6733. file link, given after a double colon, and `org-open-at-point'
  6734. will automatically search for it. If special measures must be
  6735. taken to make the search successful, another function should be
  6736. added to the companion hook `org-execute-file-search-functions',
  6737. which see.
  6738. A function in this hook may also use `setq' to set the variable
  6739. `description' to provide a suggestion for the descriptive text to
  6740. be used for this link when it gets inserted into an Org-mode
  6741. buffer with \\[org-insert-link].")
  6742. (defvar org-execute-file-search-functions nil
  6743. "List of functions to execute a file search triggered by a link.
  6744. Functions added to this hook must accept a single argument, the
  6745. search string that was part of the file link, the part after the
  6746. double colon. The function must first check if it would like to
  6747. handle this search, for example by checking the major-mode or the
  6748. file extension. If it decides not to handle this search, it
  6749. should just return nil to give other functions a chance. If it
  6750. does handle the search, it must return a non-nil value to keep
  6751. other functions from trying.
  6752. Each function can access the current prefix argument through the
  6753. variable `current-prefix-argument'. Note that a single prefix is
  6754. used to force opening a link in Emacs, so it may be good to only
  6755. use a numeric or double prefix to guide the search function.
  6756. In case this is needed, a function in this hook can also restore
  6757. the window configuration before `org-open-at-point' was called using:
  6758. (set-window-configuration org-window-config-before-follow-link)")
  6759. (defun org-link-search (s &optional type avoid-pos)
  6760. "Search for a link search option.
  6761. If S is surrounded by forward slashes, it is interpreted as a
  6762. regular expression. In org-mode files, this will create an `org-occur'
  6763. sparse tree. In ordinary files, `occur' will be used to list matches.
  6764. If the current buffer is in `dired-mode', grep will be used to search
  6765. in all files. If AVOID-POS is given, ignore matches near that position."
  6766. (let ((case-fold-search t)
  6767. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6768. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6769. (append '(("") (" ") ("\t") ("\n"))
  6770. org-emphasis-alist)
  6771. "\\|") "\\)"))
  6772. (pos (point))
  6773. (pre "") (post "")
  6774. words re0 re1 re2 re3 re4 re5 re2a reall)
  6775. (cond
  6776. ;; First check if there are any special
  6777. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6778. ;; Now try the builtin stuff
  6779. ((save-excursion
  6780. (goto-char (point-min))
  6781. (and
  6782. (re-search-forward
  6783. (concat "<<" (regexp-quote s0) ">>") nil t)
  6784. (setq pos (match-beginning 0))))
  6785. ;; There is an exact target for this
  6786. (goto-char pos))
  6787. ((string-match "^/\\(.*\\)/$" s)
  6788. ;; A regular expression
  6789. (cond
  6790. ((org-mode-p)
  6791. (org-occur (match-string 1 s)))
  6792. ;;((eq major-mode 'dired-mode)
  6793. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6794. (t (org-do-occur (match-string 1 s)))))
  6795. (t
  6796. ;; A normal search strings
  6797. (when (equal (string-to-char s) ?*)
  6798. ;; Anchor on headlines, post may include tags.
  6799. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6800. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6801. s (substring s 1)))
  6802. (remove-text-properties
  6803. 0 (length s)
  6804. '(face nil mouse-face nil keymap nil fontified nil) s)
  6805. ;; Make a series of regular expressions to find a match
  6806. (setq words (org-split-string s "[ \n\r\t]+")
  6807. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6808. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6809. "\\)" markers)
  6810. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6811. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6812. re1 (concat pre re2 post)
  6813. re3 (concat pre re4 post)
  6814. re5 (concat pre ".*" re4)
  6815. re2 (concat pre re2)
  6816. re2a (concat pre re2a)
  6817. re4 (concat pre re4)
  6818. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6819. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6820. re5 "\\)"
  6821. ))
  6822. (cond
  6823. ((eq type 'org-occur) (org-occur reall))
  6824. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6825. (t (goto-char (point-min))
  6826. (if (or (org-search-not-self 1 re0 nil t)
  6827. (org-search-not-self 1 re1 nil t)
  6828. (org-search-not-self 1 re2 nil t)
  6829. (org-search-not-self 1 re2a nil t)
  6830. (org-search-not-self 1 re3 nil t)
  6831. (org-search-not-self 1 re4 nil t)
  6832. (org-search-not-self 1 re5 nil t)
  6833. )
  6834. (goto-char (match-beginning 1))
  6835. (goto-char pos)
  6836. (error "No match")))))
  6837. (t
  6838. ;; Normal string-search
  6839. (goto-char (point-min))
  6840. (if (search-forward s nil t)
  6841. (goto-char (match-beginning 0))
  6842. (error "No match"))))
  6843. (and (org-mode-p) (org-show-context 'link-search))))
  6844. (defun org-search-not-self (group &rest args)
  6845. "Execute `re-search-forward', but only accept matches that do not
  6846. enclose the position of `org-open-link-marker'."
  6847. (let ((m org-open-link-marker))
  6848. (catch 'exit
  6849. (while (apply 're-search-forward args)
  6850. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6851. (goto-char (match-end group))
  6852. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6853. (> (match-beginning 0) (marker-position m))
  6854. (< (match-end 0) (marker-position m)))
  6855. (save-match-data
  6856. (or (not (org-in-regexp
  6857. org-bracket-link-analytic-regexp 1))
  6858. (not (match-end 4)) ; no description
  6859. (and (<= (match-beginning 4) (point))
  6860. (>= (match-end 4) (point))))))
  6861. (throw 'exit (point))))))))
  6862. (defun org-get-buffer-for-internal-link (buffer)
  6863. "Return a buffer to be used for displaying the link target of internal links."
  6864. (cond
  6865. ((not org-display-internal-link-with-indirect-buffer)
  6866. buffer)
  6867. ((string-match "(Clone)$" (buffer-name buffer))
  6868. (message "Buffer is already a clone, not making another one")
  6869. ;; we also do not modify visibility in this case
  6870. buffer)
  6871. (t ; make a new indirect buffer for displaying the link
  6872. (let* ((bn (buffer-name buffer))
  6873. (ibn (concat bn "(Clone)"))
  6874. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6875. (with-current-buffer ib (org-overview))
  6876. ib))))
  6877. (defun org-do-occur (regexp &optional cleanup)
  6878. "Call the Emacs command `occur'.
  6879. If CLEANUP is non-nil, remove the printout of the regular expression
  6880. in the *Occur* buffer. This is useful if the regex is long and not useful
  6881. to read."
  6882. (occur regexp)
  6883. (when cleanup
  6884. (let ((cwin (selected-window)) win beg end)
  6885. (when (setq win (get-buffer-window "*Occur*"))
  6886. (select-window win))
  6887. (goto-char (point-min))
  6888. (when (re-search-forward "match[a-z]+" nil t)
  6889. (setq beg (match-end 0))
  6890. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6891. (setq end (1- (match-beginning 0)))))
  6892. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6893. (goto-char (point-min))
  6894. (select-window cwin))))
  6895. ;;; The mark ring for links jumps
  6896. (defvar org-mark-ring nil
  6897. "Mark ring for positions before jumps in Org-mode.")
  6898. (defvar org-mark-ring-last-goto nil
  6899. "Last position in the mark ring used to go back.")
  6900. ;; Fill and close the ring
  6901. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6902. (loop for i from 1 to org-mark-ring-length do
  6903. (push (make-marker) org-mark-ring))
  6904. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6905. org-mark-ring)
  6906. (defun org-mark-ring-push (&optional pos buffer)
  6907. "Put the current position or POS into the mark ring and rotate it."
  6908. (interactive)
  6909. (setq pos (or pos (point)))
  6910. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6911. (move-marker (car org-mark-ring)
  6912. (or pos (point))
  6913. (or buffer (current-buffer)))
  6914. (message "%s"
  6915. (substitute-command-keys
  6916. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6917. (defun org-mark-ring-goto (&optional n)
  6918. "Jump to the previous position in the mark ring.
  6919. With prefix arg N, jump back that many stored positions. When
  6920. called several times in succession, walk through the entire ring.
  6921. Org-mode commands jumping to a different position in the current file,
  6922. or to another Org-mode file, automatically push the old position
  6923. onto the ring."
  6924. (interactive "p")
  6925. (let (p m)
  6926. (if (eq last-command this-command)
  6927. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6928. (setq p org-mark-ring))
  6929. (setq org-mark-ring-last-goto p)
  6930. (setq m (car p))
  6931. (switch-to-buffer (marker-buffer m))
  6932. (goto-char m)
  6933. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6934. (defun org-remove-angle-brackets (s)
  6935. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6936. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6937. s)
  6938. (defun org-add-angle-brackets (s)
  6939. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6940. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6941. s)
  6942. ;;; Following specific links
  6943. (defun org-follow-timestamp-link ()
  6944. (cond
  6945. ((org-at-date-range-p t)
  6946. (let ((org-agenda-start-on-weekday)
  6947. (t1 (match-string 1))
  6948. (t2 (match-string 2)))
  6949. (setq t1 (time-to-days (org-time-string-to-time t1))
  6950. t2 (time-to-days (org-time-string-to-time t2)))
  6951. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6952. ((org-at-timestamp-p t)
  6953. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6954. (substring (match-string 1) 0 10)))
  6955. 1))
  6956. (t (error "This should not happen"))))
  6957. ;;; Following file links
  6958. (defun org-open-file (path &optional in-emacs line search)
  6959. "Open the file at PATH.
  6960. First, this expands any special file name abbreviations. Then the
  6961. configuration variable `org-file-apps' is checked if it contains an
  6962. entry for this file type, and if yes, the corresponding command is launched.
  6963. If no application is found, Emacs simply visits the file.
  6964. With optional argument IN-EMACS, Emacs will visit the file.
  6965. Optional LINE specifies a line to go to, optional SEARCH a string to
  6966. search for. If LINE or SEARCH is given, the file will always be
  6967. opened in Emacs.
  6968. If the file does not exist, an error is thrown."
  6969. (setq in-emacs (or in-emacs line search))
  6970. (let* ((file (if (equal path "")
  6971. buffer-file-name
  6972. (substitute-in-file-name (expand-file-name path))))
  6973. (apps (append org-file-apps (org-default-apps)))
  6974. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6975. (dirp (if remp nil (file-directory-p file)))
  6976. (dfile (downcase file))
  6977. (old-buffer (current-buffer))
  6978. (old-pos (point))
  6979. (old-mode major-mode)
  6980. ext cmd)
  6981. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6982. (setq ext (match-string 1 dfile))
  6983. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6984. (setq ext (match-string 1 dfile))))
  6985. (if in-emacs
  6986. (setq cmd 'emacs)
  6987. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6988. (and dirp (cdr (assoc 'directory apps)))
  6989. (cdr (assoc ext apps))
  6990. (cdr (assoc t apps)))))
  6991. (when (eq cmd 'mailcap)
  6992. (require 'mailcap)
  6993. (mailcap-parse-mailcaps)
  6994. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6995. (command (mailcap-mime-info mime-type)))
  6996. (if (stringp command)
  6997. (setq cmd command)
  6998. (setq cmd 'emacs))))
  6999. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7000. (not (file-exists-p file))
  7001. (not org-open-non-existing-files))
  7002. (error "No such file: %s" file))
  7003. (cond
  7004. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7005. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7006. (while (string-match "['\"]%s['\"]" cmd)
  7007. (setq cmd (replace-match "%s" t t cmd)))
  7008. (while (string-match "%s" cmd)
  7009. (setq cmd (replace-match
  7010. (save-match-data (shell-quote-argument file))
  7011. t t cmd)))
  7012. (save-window-excursion
  7013. (start-process-shell-command cmd nil cmd)))
  7014. ((or (stringp cmd)
  7015. (eq cmd 'emacs))
  7016. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7017. (widen)
  7018. (if line (goto-line line)
  7019. (if search (org-link-search search))))
  7020. ((consp cmd)
  7021. (eval cmd))
  7022. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7023. (and (org-mode-p) (eq old-mode 'org-mode)
  7024. (or (not (equal old-buffer (current-buffer)))
  7025. (not (equal old-pos (point))))
  7026. (org-mark-ring-push old-pos old-buffer))))
  7027. (defun org-default-apps ()
  7028. "Return the default applications for this operating system."
  7029. (cond
  7030. ((eq system-type 'darwin)
  7031. org-file-apps-defaults-macosx)
  7032. ((eq system-type 'windows-nt)
  7033. org-file-apps-defaults-windowsnt)
  7034. (t org-file-apps-defaults-gnu)))
  7035. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7036. (defun org-file-remote-p (file)
  7037. "Test whether FILE specifies a location on a remote system.
  7038. Return non-nil if the location is indeed remote.
  7039. For example, the filename \"/user@host:/foo\" specifies a location
  7040. on the system \"/user@host:\"."
  7041. (cond ((fboundp 'file-remote-p)
  7042. (file-remote-p file))
  7043. ((fboundp 'tramp-handle-file-remote-p)
  7044. (tramp-handle-file-remote-p file))
  7045. ((and (boundp 'ange-ftp-name-format)
  7046. (string-match (car ange-ftp-name-format) file))
  7047. t)
  7048. (t nil)))
  7049. ;;;; Refiling
  7050. (defun org-get-org-file ()
  7051. "Read a filename, with default directory `org-directory'."
  7052. (let ((default (or org-default-notes-file remember-data-file)))
  7053. (read-file-name (format "File name [%s]: " default)
  7054. (file-name-as-directory org-directory)
  7055. default)))
  7056. (defun org-notes-order-reversed-p ()
  7057. "Check if the current file should receive notes in reversed order."
  7058. (cond
  7059. ((not org-reverse-note-order) nil)
  7060. ((eq t org-reverse-note-order) t)
  7061. ((not (listp org-reverse-note-order)) nil)
  7062. (t (catch 'exit
  7063. (let ((all org-reverse-note-order)
  7064. entry)
  7065. (while (setq entry (pop all))
  7066. (if (string-match (car entry) buffer-file-name)
  7067. (throw 'exit (cdr entry))))
  7068. nil)))))
  7069. (defvar org-refile-target-table nil
  7070. "The list of refile targets, created by `org-refile'.")
  7071. (defvar org-agenda-new-buffers nil
  7072. "Buffers created to visit agenda files.")
  7073. (defun org-get-refile-targets (&optional default-buffer)
  7074. "Produce a table with refile targets."
  7075. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7076. targets txt re files f desc descre)
  7077. (with-current-buffer (or default-buffer (current-buffer))
  7078. (while (setq entry (pop entries))
  7079. (setq files (car entry) desc (cdr entry))
  7080. (cond
  7081. ((null files) (setq files (list (current-buffer))))
  7082. ((eq files 'org-agenda-files)
  7083. (setq files (org-agenda-files 'unrestricted)))
  7084. ((and (symbolp files) (fboundp files))
  7085. (setq files (funcall files)))
  7086. ((and (symbolp files) (boundp files))
  7087. (setq files (symbol-value files))))
  7088. (if (stringp files) (setq files (list files)))
  7089. (cond
  7090. ((eq (car desc) :tag)
  7091. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7092. ((eq (car desc) :todo)
  7093. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7094. ((eq (car desc) :regexp)
  7095. (setq descre (cdr desc)))
  7096. ((eq (car desc) :level)
  7097. (setq descre (concat "^\\*\\{" (number-to-string
  7098. (if org-odd-levels-only
  7099. (1- (* 2 (cdr desc)))
  7100. (cdr desc)))
  7101. "\\}[ \t]")))
  7102. ((eq (car desc) :maxlevel)
  7103. (setq descre (concat "^\\*\\{1," (number-to-string
  7104. (if org-odd-levels-only
  7105. (1- (* 2 (cdr desc)))
  7106. (cdr desc)))
  7107. "\\}[ \t]")))
  7108. (t (error "Bad refiling target description %s" desc)))
  7109. (while (setq f (pop files))
  7110. (save-excursion
  7111. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7112. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7113. (save-excursion
  7114. (save-restriction
  7115. (widen)
  7116. (goto-char (point-min))
  7117. (while (re-search-forward descre nil t)
  7118. (goto-char (point-at-bol))
  7119. (when (looking-at org-complex-heading-regexp)
  7120. (setq txt (match-string 4)
  7121. re (concat "^" (regexp-quote
  7122. (buffer-substring (match-beginning 1)
  7123. (match-end 4)))))
  7124. (if (match-end 5) (setq re (concat re "[ \t]+"
  7125. (regexp-quote
  7126. (match-string 5)))))
  7127. (setq re (concat re "[ \t]*$"))
  7128. (when org-refile-use-outline-path
  7129. (setq txt (mapconcat 'identity
  7130. (append
  7131. (if (eq org-refile-use-outline-path 'file)
  7132. (list (file-name-nondirectory
  7133. (buffer-file-name (buffer-base-buffer))))
  7134. (if (eq org-refile-use-outline-path 'full-file-path)
  7135. (list (buffer-file-name (buffer-base-buffer)))))
  7136. (org-get-outline-path)
  7137. (list txt))
  7138. "/")))
  7139. (push (list txt f re (point)) targets))
  7140. (goto-char (point-at-eol))))))))
  7141. (nreverse targets))))
  7142. (defun org-get-outline-path ()
  7143. "Return the outline path to the current entry, as a list."
  7144. (let (rtn)
  7145. (save-excursion
  7146. (while (org-up-heading-safe)
  7147. (when (looking-at org-complex-heading-regexp)
  7148. (push (org-match-string-no-properties 4) rtn)))
  7149. rtn)))
  7150. (defvar org-refile-history nil
  7151. "History for refiling operations.")
  7152. (defun org-refile (&optional goto default-buffer)
  7153. "Move the entry at point to another heading.
  7154. The list of target headings is compiled using the information in
  7155. `org-refile-targets', which see. This list is created before each use
  7156. and will therefore always be up-to-date.
  7157. At the target location, the entry is filed as a subitem of the target heading.
  7158. Depending on `org-reverse-note-order', the new subitem will either be the
  7159. first of the last subitem.
  7160. With prefix arg GOTO, the command will only visit the target location,
  7161. not actually move anything.
  7162. With a double prefix `C-c C-c', go to the location where the last refiling
  7163. operation has put the subtree."
  7164. (interactive "P")
  7165. (let* ((cbuf (current-buffer))
  7166. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7167. pos it nbuf file re level reversed)
  7168. (if (equal goto '(16))
  7169. (org-refile-goto-last-stored)
  7170. (when (setq it (org-refile-get-location
  7171. (if goto "Goto: " "Refile to: ") default-buffer))
  7172. (setq file (nth 1 it)
  7173. re (nth 2 it)
  7174. pos (nth 3 it))
  7175. (setq nbuf (or (find-buffer-visiting file)
  7176. (find-file-noselect file)))
  7177. (if goto
  7178. (progn
  7179. (switch-to-buffer nbuf)
  7180. (goto-char pos)
  7181. (org-show-context 'org-goto))
  7182. (org-copy-special)
  7183. (save-excursion
  7184. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7185. (find-file-noselect file))))
  7186. (setq reversed (org-notes-order-reversed-p))
  7187. (save-excursion
  7188. (save-restriction
  7189. (widen)
  7190. (goto-char pos)
  7191. (looking-at outline-regexp)
  7192. (setq level (org-get-valid-level (funcall outline-level) 1))
  7193. (goto-char
  7194. (if reversed
  7195. (outline-next-heading)
  7196. (or (save-excursion (outline-get-next-sibling))
  7197. (org-end-of-subtree t t)
  7198. (point-max))))
  7199. (bookmark-set "org-refile-last-stored")
  7200. (org-paste-subtree level))))
  7201. (org-cut-special)
  7202. (message "Entry refiled to \"%s\"" (car it)))))))
  7203. (defun org-refile-goto-last-stored ()
  7204. "Go to the location where the last refile was stored."
  7205. (interactive)
  7206. (bookmark-jump "org-refile-last-stored")
  7207. (message "This is the location of the last refile"))
  7208. (defun org-refile-get-location (&optional prompt default-buffer)
  7209. "Prompt the user for a refile location, using PROMPT."
  7210. (let ((org-refile-targets org-refile-targets)
  7211. (org-refile-use-outline-path org-refile-use-outline-path))
  7212. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7213. (unless org-refile-target-table
  7214. (error "No refile targets"))
  7215. (let* ((cbuf (current-buffer))
  7216. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7217. (fname (and filename (file-truename filename)))
  7218. (tbl (mapcar
  7219. (lambda (x)
  7220. (if (not (equal fname (file-truename (nth 1 x))))
  7221. (cons (concat (car x) " (" (file-name-nondirectory
  7222. (nth 1 x)) ")")
  7223. (cdr x))
  7224. x))
  7225. org-refile-target-table))
  7226. (completion-ignore-case t))
  7227. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  7228. tbl)))
  7229. ;;;; Dynamic blocks
  7230. (defun org-find-dblock (name)
  7231. "Find the first dynamic block with name NAME in the buffer.
  7232. If not found, stay at current position and return nil."
  7233. (let (pos)
  7234. (save-excursion
  7235. (goto-char (point-min))
  7236. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7237. nil t)
  7238. (match-beginning 0))))
  7239. (if pos (goto-char pos))
  7240. pos))
  7241. (defconst org-dblock-start-re
  7242. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7243. "Matches the startline of a dynamic block, with parameters.")
  7244. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7245. "Matches the end of a dyhamic block.")
  7246. (defun org-create-dblock (plist)
  7247. "Create a dynamic block section, with parameters taken from PLIST.
  7248. PLIST must containe a :name entry which is used as name of the block."
  7249. (unless (bolp) (newline))
  7250. (let ((name (plist-get plist :name)))
  7251. (insert "#+BEGIN: " name)
  7252. (while plist
  7253. (if (eq (car plist) :name)
  7254. (setq plist (cddr plist))
  7255. (insert " " (prin1-to-string (pop plist)))))
  7256. (insert "\n\n#+END:\n")
  7257. (beginning-of-line -2)))
  7258. (defun org-prepare-dblock ()
  7259. "Prepare dynamic block for refresh.
  7260. This empties the block, puts the cursor at the insert position and returns
  7261. the property list including an extra property :name with the block name."
  7262. (unless (looking-at org-dblock-start-re)
  7263. (error "Not at a dynamic block"))
  7264. (let* ((begdel (1+ (match-end 0)))
  7265. (name (org-no-properties (match-string 1)))
  7266. (params (append (list :name name)
  7267. (read (concat "(" (match-string 3) ")")))))
  7268. (unless (re-search-forward org-dblock-end-re nil t)
  7269. (error "Dynamic block not terminated"))
  7270. (setq params
  7271. (append params
  7272. (list :content (buffer-substring
  7273. begdel (match-beginning 0)))))
  7274. (delete-region begdel (match-beginning 0))
  7275. (goto-char begdel)
  7276. (open-line 1)
  7277. params))
  7278. (defun org-map-dblocks (&optional command)
  7279. "Apply COMMAND to all dynamic blocks in the current buffer.
  7280. If COMMAND is not given, use `org-update-dblock'."
  7281. (let ((cmd (or command 'org-update-dblock))
  7282. pos)
  7283. (save-excursion
  7284. (goto-char (point-min))
  7285. (while (re-search-forward org-dblock-start-re nil t)
  7286. (goto-char (setq pos (match-beginning 0)))
  7287. (condition-case nil
  7288. (funcall cmd)
  7289. (error (message "Error during update of dynamic block")))
  7290. (goto-char pos)
  7291. (unless (re-search-forward org-dblock-end-re nil t)
  7292. (error "Dynamic block not terminated"))))))
  7293. (defun org-dblock-update (&optional arg)
  7294. "User command for updating dynamic blocks.
  7295. Update the dynamic block at point. With prefix ARG, update all dynamic
  7296. blocks in the buffer."
  7297. (interactive "P")
  7298. (if arg
  7299. (org-update-all-dblocks)
  7300. (or (looking-at org-dblock-start-re)
  7301. (org-beginning-of-dblock))
  7302. (org-update-dblock)))
  7303. (defun org-update-dblock ()
  7304. "Update the dynamic block at point
  7305. This means to empty the block, parse for parameters and then call
  7306. the correct writing function."
  7307. (save-window-excursion
  7308. (let* ((pos (point))
  7309. (line (org-current-line))
  7310. (params (org-prepare-dblock))
  7311. (name (plist-get params :name))
  7312. (cmd (intern (concat "org-dblock-write:" name))))
  7313. (message "Updating dynamic block `%s' at line %d..." name line)
  7314. (funcall cmd params)
  7315. (message "Updating dynamic block `%s' at line %d...done" name line)
  7316. (goto-char pos))))
  7317. (defun org-beginning-of-dblock ()
  7318. "Find the beginning of the dynamic block at point.
  7319. Error if there is no scuh block at point."
  7320. (let ((pos (point))
  7321. beg)
  7322. (end-of-line 1)
  7323. (if (and (re-search-backward org-dblock-start-re nil t)
  7324. (setq beg (match-beginning 0))
  7325. (re-search-forward org-dblock-end-re nil t)
  7326. (> (match-end 0) pos))
  7327. (goto-char beg)
  7328. (goto-char pos)
  7329. (error "Not in a dynamic block"))))
  7330. (defun org-update-all-dblocks ()
  7331. "Update all dynamic blocks in the buffer.
  7332. This function can be used in a hook."
  7333. (when (org-mode-p)
  7334. (org-map-dblocks 'org-update-dblock)))
  7335. ;;;; Completion
  7336. (defconst org-additional-option-like-keywords
  7337. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7338. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7339. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7340. (defun org-complete (&optional arg)
  7341. "Perform completion on word at point.
  7342. At the beginning of a headline, this completes TODO keywords as given in
  7343. `org-todo-keywords'.
  7344. If the current word is preceded by a backslash, completes the TeX symbols
  7345. that are supported for HTML support.
  7346. If the current word is preceded by \"#+\", completes special words for
  7347. setting file options.
  7348. In the line after \"#+STARTUP:, complete valid keywords.\"
  7349. At all other locations, this simply calls the value of
  7350. `org-completion-fallback-command'."
  7351. (interactive "P")
  7352. (org-without-partial-completion
  7353. (catch 'exit
  7354. (let* ((end (point))
  7355. (beg1 (save-excursion
  7356. (skip-chars-backward (org-re "[:alnum:]_@"))
  7357. (point)))
  7358. (beg (save-excursion
  7359. (skip-chars-backward "a-zA-Z0-9_:$")
  7360. (point)))
  7361. (confirm (lambda (x) (stringp (car x))))
  7362. (searchhead (equal (char-before beg) ?*))
  7363. (tag (and (equal (char-before beg1) ?:)
  7364. (equal (char-after (point-at-bol)) ?*)))
  7365. (prop (and (equal (char-before beg1) ?:)
  7366. (not (equal (char-after (point-at-bol)) ?*))))
  7367. (texp (equal (char-before beg) ?\\))
  7368. (link (equal (char-before beg) ?\[))
  7369. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7370. beg)
  7371. "#+"))
  7372. (startup (string-match "^#\\+STARTUP:.*"
  7373. (buffer-substring (point-at-bol) (point))))
  7374. (completion-ignore-case opt)
  7375. (type nil)
  7376. (tbl nil)
  7377. (table (cond
  7378. (opt
  7379. (setq type :opt)
  7380. (require 'org-exp)
  7381. (append
  7382. (mapcar
  7383. (lambda (x)
  7384. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7385. (cons (match-string 2 x) (match-string 1 x)))
  7386. (org-split-string (org-get-current-options) "\n"))
  7387. (mapcar 'list org-additional-option-like-keywords)))
  7388. (startup
  7389. (setq type :startup)
  7390. org-startup-options)
  7391. (link (append org-link-abbrev-alist-local
  7392. org-link-abbrev-alist))
  7393. (texp
  7394. (setq type :tex)
  7395. org-html-entities)
  7396. ((string-match "\\`\\*+[ \t]+\\'"
  7397. (buffer-substring (point-at-bol) beg))
  7398. (setq type :todo)
  7399. (mapcar 'list org-todo-keywords-1))
  7400. (searchhead
  7401. (setq type :searchhead)
  7402. (save-excursion
  7403. (goto-char (point-min))
  7404. (while (re-search-forward org-todo-line-regexp nil t)
  7405. (push (list
  7406. (org-make-org-heading-search-string
  7407. (match-string 3) t))
  7408. tbl)))
  7409. tbl)
  7410. (tag (setq type :tag beg beg1)
  7411. (or org-tag-alist (org-get-buffer-tags)))
  7412. (prop (setq type :prop beg beg1)
  7413. (mapcar 'list (org-buffer-property-keys nil t t)))
  7414. (t (progn
  7415. (call-interactively org-completion-fallback-command)
  7416. (throw 'exit nil)))))
  7417. (pattern (buffer-substring-no-properties beg end))
  7418. (completion (try-completion pattern table confirm)))
  7419. (cond ((eq completion t)
  7420. (if (not (assoc (upcase pattern) table))
  7421. (message "Already complete")
  7422. (if (and (equal type :opt)
  7423. (not (member (car (assoc (upcase pattern) table))
  7424. org-additional-option-like-keywords)))
  7425. (insert (substring (cdr (assoc (upcase pattern) table))
  7426. (length pattern)))
  7427. (if (memq type '(:tag :prop)) (insert ":")))))
  7428. ((null completion)
  7429. (message "Can't find completion for \"%s\"" pattern)
  7430. (ding))
  7431. ((not (string= pattern completion))
  7432. (delete-region beg end)
  7433. (if (string-match " +$" completion)
  7434. (setq completion (replace-match "" t t completion)))
  7435. (insert completion)
  7436. (if (get-buffer-window "*Completions*")
  7437. (delete-window (get-buffer-window "*Completions*")))
  7438. (if (assoc completion table)
  7439. (if (eq type :todo) (insert " ")
  7440. (if (memq type '(:tag :prop)) (insert ":"))))
  7441. (if (and (equal type :opt) (assoc completion table))
  7442. (message "%s" (substitute-command-keys
  7443. "Press \\[org-complete] again to insert example settings"))))
  7444. (t
  7445. (message "Making completion list...")
  7446. (let ((list (sort (all-completions pattern table confirm)
  7447. 'string<)))
  7448. (with-output-to-temp-buffer "*Completions*"
  7449. (condition-case nil
  7450. ;; Protection needed for XEmacs and emacs 21
  7451. (display-completion-list list pattern)
  7452. (error (display-completion-list list)))))
  7453. (message "Making completion list...%s" "done")))))))
  7454. ;;;; TODO, DEADLINE, Comments
  7455. (defun org-toggle-comment ()
  7456. "Change the COMMENT state of an entry."
  7457. (interactive)
  7458. (save-excursion
  7459. (org-back-to-heading)
  7460. (let (case-fold-search)
  7461. (if (looking-at (concat outline-regexp
  7462. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7463. (replace-match "" t t nil 1)
  7464. (if (looking-at outline-regexp)
  7465. (progn
  7466. (goto-char (match-end 0))
  7467. (insert org-comment-string " ")))))))
  7468. (defvar org-last-todo-state-is-todo nil
  7469. "This is non-nil when the last TODO state change led to a TODO state.
  7470. If the last change removed the TODO tag or switched to DONE, then
  7471. this is nil.")
  7472. (defvar org-setting-tags nil) ; dynamically skiped
  7473. (defun org-parse-local-options (string var)
  7474. "Parse STRING for startup setting relevant for variable VAR."
  7475. (let ((rtn (symbol-value var))
  7476. e opts)
  7477. (save-match-data
  7478. (if (or (not string) (not (string-match "\\S-" string)))
  7479. rtn
  7480. (setq opts (delq nil (mapcar (lambda (x)
  7481. (setq e (assoc x org-startup-options))
  7482. (if (eq (nth 1 e) var) e nil))
  7483. (org-split-string string "[ \t]+"))))
  7484. (if (not opts)
  7485. rtn
  7486. (setq rtn nil)
  7487. (while (setq e (pop opts))
  7488. (if (not (nth 3 e))
  7489. (setq rtn (nth 2 e))
  7490. (if (not (listp rtn)) (setq rtn nil))
  7491. (push (nth 2 e) rtn)))
  7492. rtn)))))
  7493. (defvar org-blocker-hook nil
  7494. "Hook for functions that are allowed to block a state change.
  7495. Each function gets as its single argument a property list, see
  7496. `org-trigger-hook' for more information about this list.
  7497. If any of the functions in this hook returns nil, the state change
  7498. is blocked.")
  7499. (defvar org-trigger-hook nil
  7500. "Hook for functions that are triggered by a state change.
  7501. Each function gets as its single argument a property list with at least
  7502. the following elements:
  7503. (:type type-of-change :position pos-at-entry-start
  7504. :from old-state :to new-state)
  7505. Depending on the type, more properties may be present.
  7506. This mechanism is currently implemented for:
  7507. TODO state changes
  7508. ------------------
  7509. :type todo-state-change
  7510. :from previous state (keyword as a string), or nil
  7511. :to new state (keyword as a string), or nil")
  7512. (defun org-todo (&optional arg)
  7513. "Change the TODO state of an item.
  7514. The state of an item is given by a keyword at the start of the heading,
  7515. like
  7516. *** TODO Write paper
  7517. *** DONE Call mom
  7518. The different keywords are specified in the variable `org-todo-keywords'.
  7519. By default the available states are \"TODO\" and \"DONE\".
  7520. So for this example: when the item starts with TODO, it is changed to DONE.
  7521. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7522. DONE are present, add TODO at the beginning of the heading.
  7523. With C-u prefix arg, use completion to determine the new state.
  7524. With numeric prefix arg, switch to that state.
  7525. For calling through lisp, arg is also interpreted in the following way:
  7526. 'none -> empty state
  7527. \"\"(empty string) -> switch to empty state
  7528. 'done -> switch to DONE
  7529. 'nextset -> switch to the next set of keywords
  7530. 'previousset -> switch to the previous set of keywords
  7531. \"WAITING\" -> switch to the specified keyword, but only if it
  7532. really is a member of `org-todo-keywords'."
  7533. (interactive "P")
  7534. (save-excursion
  7535. (catch 'exit
  7536. (org-back-to-heading)
  7537. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7538. (or (looking-at (concat " +" org-todo-regexp " *"))
  7539. (looking-at " *"))
  7540. (let* ((match-data (match-data))
  7541. (startpos (point-at-bol))
  7542. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7543. (org-log-done org-log-done)
  7544. (org-log-repeat org-log-repeat)
  7545. (org-todo-log-states org-todo-log-states)
  7546. (this (match-string 1))
  7547. (hl-pos (match-beginning 0))
  7548. (head (org-get-todo-sequence-head this))
  7549. (ass (assoc head org-todo-kwd-alist))
  7550. (interpret (nth 1 ass))
  7551. (done-word (nth 3 ass))
  7552. (final-done-word (nth 4 ass))
  7553. (last-state (or this ""))
  7554. (completion-ignore-case t)
  7555. (member (member this org-todo-keywords-1))
  7556. (tail (cdr member))
  7557. (state (cond
  7558. ((and org-todo-key-trigger
  7559. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7560. (and (not arg) org-use-fast-todo-selection
  7561. (not (eq org-use-fast-todo-selection 'prefix)))))
  7562. ;; Use fast selection
  7563. (org-fast-todo-selection))
  7564. ((and (equal arg '(4))
  7565. (or (not org-use-fast-todo-selection)
  7566. (not org-todo-key-trigger)))
  7567. ;; Read a state with completion
  7568. (completing-read "State: " (mapcar (lambda(x) (list x))
  7569. org-todo-keywords-1)
  7570. nil t))
  7571. ((eq arg 'right)
  7572. (if this
  7573. (if tail (car tail) nil)
  7574. (car org-todo-keywords-1)))
  7575. ((eq arg 'left)
  7576. (if (equal member org-todo-keywords-1)
  7577. nil
  7578. (if this
  7579. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7580. org-todo-keywords-1)
  7581. (org-last org-todo-keywords-1))))
  7582. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7583. (setq arg nil))) ; hack to fall back to cycling
  7584. (arg
  7585. ;; user or caller requests a specific state
  7586. (cond
  7587. ((equal arg "") nil)
  7588. ((eq arg 'none) nil)
  7589. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7590. ((eq arg 'nextset)
  7591. (or (car (cdr (member head org-todo-heads)))
  7592. (car org-todo-heads)))
  7593. ((eq arg 'previousset)
  7594. (let ((org-todo-heads (reverse org-todo-heads)))
  7595. (or (car (cdr (member head org-todo-heads)))
  7596. (car org-todo-heads))))
  7597. ((car (member arg org-todo-keywords-1)))
  7598. ((nth (1- (prefix-numeric-value arg))
  7599. org-todo-keywords-1))))
  7600. ((null member) (or head (car org-todo-keywords-1)))
  7601. ((equal this final-done-word) nil) ;; -> make empty
  7602. ((null tail) nil) ;; -> first entry
  7603. ((eq interpret 'sequence)
  7604. (car tail))
  7605. ((memq interpret '(type priority))
  7606. (if (eq this-command last-command)
  7607. (car tail)
  7608. (if (> (length tail) 0)
  7609. (or done-word (car org-done-keywords))
  7610. nil)))
  7611. (t nil)))
  7612. (next (if state (concat " " state " ") " "))
  7613. (change-plist (list :type 'todo-state-change :from this :to state
  7614. :position startpos))
  7615. dolog now-done-p)
  7616. (when org-blocker-hook
  7617. (unless (save-excursion
  7618. (save-match-data
  7619. (run-hook-with-args-until-failure
  7620. 'org-blocker-hook change-plist)))
  7621. (if (interactive-p)
  7622. (error "TODO state change from %s to %s blocked" this state)
  7623. ;; fail silently
  7624. (message "TODO state change from %s to %s blocked" this state)
  7625. (throw 'exit nil))))
  7626. (store-match-data match-data)
  7627. (replace-match next t t)
  7628. (unless (pos-visible-in-window-p hl-pos)
  7629. (message "TODO state changed to %s" (org-trim next)))
  7630. (unless head
  7631. (setq head (org-get-todo-sequence-head state)
  7632. ass (assoc head org-todo-kwd-alist)
  7633. interpret (nth 1 ass)
  7634. done-word (nth 3 ass)
  7635. final-done-word (nth 4 ass)))
  7636. (when (memq arg '(nextset previousset))
  7637. (message "Keyword-Set %d/%d: %s"
  7638. (- (length org-todo-sets) -1
  7639. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7640. (length org-todo-sets)
  7641. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7642. (setq org-last-todo-state-is-todo
  7643. (not (member state org-done-keywords)))
  7644. (setq now-done-p (and (member state org-done-keywords)
  7645. (not (member this org-done-keywords))))
  7646. (and logging (org-local-logging logging))
  7647. (when (and (or org-todo-log-states org-log-done)
  7648. (not (memq arg '(nextset previousset))))
  7649. ;; we need to look at recording a time and note
  7650. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7651. (nth 2 (assoc this org-todo-log-states))))
  7652. (when (and state
  7653. (member state org-not-done-keywords)
  7654. (not (member this org-not-done-keywords)))
  7655. ;; This is now a todo state and was not one before
  7656. ;; If there was a CLOSED time stamp, get rid of it.
  7657. (org-add-planning-info nil nil 'closed))
  7658. (when (and now-done-p org-log-done)
  7659. ;; It is now done, and it was not done before
  7660. (org-add-planning-info 'closed (org-current-time))
  7661. (if (and (not dolog) (eq 'note org-log-done))
  7662. (org-add-log-setup 'done state 'findpos 'note)))
  7663. (when (and state dolog)
  7664. ;; This is a non-nil state, and we need to log it
  7665. (org-add-log-setup 'state state 'findpos dolog)))
  7666. ;; Fixup tag positioning
  7667. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7668. (run-hooks 'org-after-todo-state-change-hook)
  7669. (if (and arg (not (member state org-done-keywords)))
  7670. (setq head (org-get-todo-sequence-head state)))
  7671. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7672. ;; Do we need to trigger a repeat?
  7673. (when now-done-p (org-auto-repeat-maybe state))
  7674. ;; Fixup cursor location if close to the keyword
  7675. (if (and (outline-on-heading-p)
  7676. (not (bolp))
  7677. (save-excursion (beginning-of-line 1)
  7678. (looking-at org-todo-line-regexp))
  7679. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7680. (progn
  7681. (goto-char (or (match-end 2) (match-end 1)))
  7682. (just-one-space)))
  7683. (when org-trigger-hook
  7684. (save-excursion
  7685. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7686. (defun org-local-logging (value)
  7687. "Get logging settings from a property VALUE."
  7688. (let* (words w a)
  7689. ;; directly set the variables, they are already local.
  7690. (setq org-log-done nil
  7691. org-log-repeat nil
  7692. org-todo-log-states nil)
  7693. (setq words (org-split-string value))
  7694. (while (setq w (pop words))
  7695. (cond
  7696. ((setq a (assoc w org-startup-options))
  7697. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7698. (set (nth 1 a) (nth 2 a))))
  7699. ((setq a (org-extract-log-state-settings w))
  7700. (and (member (car a) org-todo-keywords-1)
  7701. (push a org-todo-log-states)))))))
  7702. (defun org-get-todo-sequence-head (kwd)
  7703. "Return the head of the TODO sequence to which KWD belongs.
  7704. If KWD is not set, check if there is a text property remembering the
  7705. right sequence."
  7706. (let (p)
  7707. (cond
  7708. ((not kwd)
  7709. (or (get-text-property (point-at-bol) 'org-todo-head)
  7710. (progn
  7711. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7712. nil (point-at-eol)))
  7713. (get-text-property p 'org-todo-head))))
  7714. ((not (member kwd org-todo-keywords-1))
  7715. (car org-todo-keywords-1))
  7716. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7717. (defun org-fast-todo-selection ()
  7718. "Fast TODO keyword selection with single keys.
  7719. Returns the new TODO keyword, or nil if no state change should occur."
  7720. (let* ((fulltable org-todo-key-alist)
  7721. (done-keywords org-done-keywords) ;; needed for the faces.
  7722. (maxlen (apply 'max (mapcar
  7723. (lambda (x)
  7724. (if (stringp (car x)) (string-width (car x)) 0))
  7725. fulltable)))
  7726. (expert nil)
  7727. (fwidth (+ maxlen 3 1 3))
  7728. (ncol (/ (- (window-width) 4) fwidth))
  7729. tg cnt e c tbl
  7730. groups ingroup)
  7731. (save-window-excursion
  7732. (if expert
  7733. (set-buffer (get-buffer-create " *Org todo*"))
  7734. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7735. (erase-buffer)
  7736. (org-set-local 'org-done-keywords done-keywords)
  7737. (setq tbl fulltable cnt 0)
  7738. (while (setq e (pop tbl))
  7739. (cond
  7740. ((equal e '(:startgroup))
  7741. (push '() groups) (setq ingroup t)
  7742. (when (not (= cnt 0))
  7743. (setq cnt 0)
  7744. (insert "\n"))
  7745. (insert "{ "))
  7746. ((equal e '(:endgroup))
  7747. (setq ingroup nil cnt 0)
  7748. (insert "}\n"))
  7749. (t
  7750. (setq tg (car e) c (cdr e))
  7751. (if ingroup (push tg (car groups)))
  7752. (setq tg (org-add-props tg nil 'face
  7753. (org-get-todo-face tg)))
  7754. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7755. (insert "[" c "] " tg (make-string
  7756. (- fwidth 4 (length tg)) ?\ ))
  7757. (when (= (setq cnt (1+ cnt)) ncol)
  7758. (insert "\n")
  7759. (if ingroup (insert " "))
  7760. (setq cnt 0)))))
  7761. (insert "\n")
  7762. (goto-char (point-min))
  7763. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7764. (fit-window-to-buffer))
  7765. (message "[a-z..]:Set [SPC]:clear")
  7766. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7767. (cond
  7768. ((or (= c ?\C-g)
  7769. (and (= c ?q) (not (rassoc c fulltable))))
  7770. (setq quit-flag t))
  7771. ((= c ?\ ) nil)
  7772. ((setq e (rassoc c fulltable) tg (car e))
  7773. tg)
  7774. (t (setq quit-flag t))))))
  7775. (defun org-entry-is-todo-p ()
  7776. (member (org-get-todo-state) org-not-done-keywords))
  7777. (defun org-entry-is-done-p ()
  7778. (member (org-get-todo-state) org-done-keywords))
  7779. (defun org-get-todo-state ()
  7780. (save-excursion
  7781. (org-back-to-heading t)
  7782. (and (looking-at org-todo-line-regexp)
  7783. (match-end 2)
  7784. (match-string 2))))
  7785. (defun org-at-date-range-p (&optional inactive-ok)
  7786. "Is the cursor inside a date range?"
  7787. (interactive)
  7788. (save-excursion
  7789. (catch 'exit
  7790. (let ((pos (point)))
  7791. (skip-chars-backward "^[<\r\n")
  7792. (skip-chars-backward "<[")
  7793. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7794. (>= (match-end 0) pos)
  7795. (throw 'exit t))
  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. nil)))
  7802. (defun org-get-repeat ()
  7803. "Check if tere is a deadline/schedule with repeater in this entry."
  7804. (save-match-data
  7805. (save-excursion
  7806. (org-back-to-heading t)
  7807. (if (re-search-forward
  7808. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7809. (match-string 1)))))
  7810. (defvar org-last-changed-timestamp)
  7811. (defvar org-log-post-message)
  7812. (defvar org-log-note-purpose)
  7813. (defvar org-log-note-how)
  7814. (defun org-auto-repeat-maybe (done-word)
  7815. "Check if the current headline contains a repeated deadline/schedule.
  7816. If yes, set TODO state back to what it was and change the base date
  7817. of repeating deadline/scheduled time stamps to new date.
  7818. This function is run automatically after each state change to a DONE state."
  7819. ;; last-state is dynamically scoped into this function
  7820. (let* ((repeat (org-get-repeat))
  7821. (aa (assoc last-state org-todo-kwd-alist))
  7822. (interpret (nth 1 aa))
  7823. (head (nth 2 aa))
  7824. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7825. (msg "Entry repeats: ")
  7826. (org-log-done nil)
  7827. (org-todo-log-states nil)
  7828. (nshiftmax 10) (nshift 0)
  7829. re type n what ts mb0 time)
  7830. (when repeat
  7831. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7832. (org-todo (if (eq interpret 'type) last-state head))
  7833. (when org-log-repeat
  7834. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7835. (memq 'org-add-log-note post-command-hook))
  7836. ;; OK, we are already setup for some record
  7837. (if (eq org-log-repeat 'note)
  7838. ;; make sure we take a note, not only a time stamp
  7839. (setq org-log-note-how 'note))
  7840. ;; Set up for taking a record
  7841. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7842. 'findpos org-log-repeat)))
  7843. (org-back-to-heading t)
  7844. (org-add-planning-info nil nil 'closed)
  7845. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7846. org-deadline-time-regexp "\\)\\|\\("
  7847. org-ts-regexp "\\)"))
  7848. (while (re-search-forward
  7849. re (save-excursion (outline-next-heading) (point)) t)
  7850. (setq type (if (match-end 1) org-scheduled-string
  7851. (if (match-end 3) org-deadline-string "Plain:"))
  7852. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7853. mb0 (match-beginning 0))
  7854. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7855. (setq n (string-to-number (match-string 2 ts))
  7856. what (match-string 3 ts))
  7857. (if (equal what "w") (setq n (* n 7) what "d"))
  7858. ;; Preparation, see if we need to modify the start date for the change
  7859. (when (match-end 1)
  7860. (setq time (save-match-data (org-time-string-to-time ts)))
  7861. (cond
  7862. ((equal (match-string 1 ts) ".")
  7863. ;; Shift starting date to today
  7864. (org-timestamp-change
  7865. (- (time-to-days (current-time)) (time-to-days time))
  7866. 'day))
  7867. ((equal (match-string 1 ts) "+")
  7868. (while (or (= nshift 0)
  7869. (<= (time-to-days time) (time-to-days (current-time))))
  7870. (when (= (incf nshift) nshiftmax)
  7871. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7872. (error "Abort")))
  7873. (org-timestamp-change n (cdr (assoc what whata)))
  7874. (org-at-timestamp-p t)
  7875. (setq ts (match-string 1))
  7876. (setq time (save-match-data (org-time-string-to-time ts))))
  7877. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7878. ;; rematch, so that we have everything in place for the real shift
  7879. (org-at-timestamp-p t)
  7880. (setq ts (match-string 1))
  7881. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7882. (org-timestamp-change n (cdr (assoc what whata)))
  7883. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7884. (setq org-log-post-message msg)
  7885. (message "%s" msg))))
  7886. (defun org-show-todo-tree (arg)
  7887. "Make a compact tree which shows all headlines marked with TODO.
  7888. The tree will show the lines where the regexp matches, and all higher
  7889. headlines above the match.
  7890. With a \\[universal-argument] prefix, also show the DONE entries.
  7891. With a numeric prefix N, construct a sparse tree for the Nth element
  7892. of `org-todo-keywords-1'."
  7893. (interactive "P")
  7894. (let ((case-fold-search nil)
  7895. (kwd-re
  7896. (cond ((null arg) org-not-done-regexp)
  7897. ((equal arg '(4))
  7898. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7899. (mapcar 'list org-todo-keywords-1))))
  7900. (concat "\\("
  7901. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7902. "\\)\\>")))
  7903. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7904. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7905. org-todo-keywords-1)))
  7906. (t (error "Invalid prefix argument: %s" arg)))))
  7907. (message "%d TODO entries found"
  7908. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7909. (defun org-deadline (&optional remove)
  7910. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7911. With argument REMOVE, remove any deadline from the item."
  7912. (interactive "P")
  7913. (if remove
  7914. (progn
  7915. (org-remove-timestamp-with-keyword org-deadline-string)
  7916. (message "Item no longer has a deadline."))
  7917. (org-add-planning-info 'deadline nil 'closed)))
  7918. (defun org-schedule (&optional remove)
  7919. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7920. With argument REMOVE, remove any scheduling date from the item."
  7921. (interactive "P")
  7922. (if remove
  7923. (progn
  7924. (org-remove-timestamp-with-keyword org-scheduled-string)
  7925. (message "Item is no longer scheduled."))
  7926. (org-add-planning-info 'scheduled nil 'closed)))
  7927. (defun org-remove-timestamp-with-keyword (keyword)
  7928. "Remove all time stamps with KEYWORD in the current entry."
  7929. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7930. beg)
  7931. (save-excursion
  7932. (org-back-to-heading t)
  7933. (setq beg (point))
  7934. (org-end-of-subtree t t)
  7935. (while (re-search-backward re beg t)
  7936. (replace-match "")
  7937. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7938. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7939. (defun org-add-planning-info (what &optional time &rest remove)
  7940. "Insert new timestamp with keyword in the line directly after the headline.
  7941. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7942. If non is given, the user is prompted for a date.
  7943. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7944. be removed."
  7945. (interactive)
  7946. (let (org-time-was-given org-end-time-was-given ts
  7947. end default-time default-input)
  7948. (when (and (not time) (memq what '(scheduled deadline)))
  7949. ;; Try to get a default date/time from existing timestamp
  7950. (save-excursion
  7951. (org-back-to-heading t)
  7952. (setq end (save-excursion (outline-next-heading) (point)))
  7953. (when (re-search-forward (if (eq what 'scheduled)
  7954. org-scheduled-time-regexp
  7955. org-deadline-time-regexp)
  7956. end t)
  7957. (setq ts (match-string 1)
  7958. default-time
  7959. (apply 'encode-time (org-parse-time-string ts))
  7960. default-input (and ts (org-get-compact-tod ts))))))
  7961. (when what
  7962. ;; If necessary, get the time from the user
  7963. (setq time (or time (org-read-date nil 'to-time nil nil
  7964. default-time default-input))))
  7965. (when (and org-insert-labeled-timestamps-at-point
  7966. (member what '(scheduled deadline)))
  7967. (insert
  7968. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7969. (org-insert-time-stamp time org-time-was-given
  7970. nil nil nil (list org-end-time-was-given))
  7971. (setq what nil))
  7972. (save-excursion
  7973. (save-restriction
  7974. (let (col list elt ts buffer-invisibility-spec)
  7975. (org-back-to-heading t)
  7976. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7977. (goto-char (match-end 1))
  7978. (setq col (current-column))
  7979. (goto-char (match-end 0))
  7980. (if (eobp) (insert "\n") (forward-char 1))
  7981. (if (and (not (looking-at outline-regexp))
  7982. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7983. "[^\r\n]*"))
  7984. (not (equal (match-string 1) org-clock-string)))
  7985. (narrow-to-region (match-beginning 0) (match-end 0))
  7986. (insert-before-markers "\n")
  7987. (backward-char 1)
  7988. (narrow-to-region (point) (point))
  7989. (indent-to-column col))
  7990. ;; Check if we have to remove something.
  7991. (setq list (cons what remove))
  7992. (while list
  7993. (setq elt (pop list))
  7994. (goto-char (point-min))
  7995. (when (or (and (eq elt 'scheduled)
  7996. (re-search-forward org-scheduled-time-regexp nil t))
  7997. (and (eq elt 'deadline)
  7998. (re-search-forward org-deadline-time-regexp nil t))
  7999. (and (eq elt 'closed)
  8000. (re-search-forward org-closed-time-regexp nil t)))
  8001. (replace-match "")
  8002. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8003. (if (looking-at " +") (replace-match ""))))
  8004. (goto-char (point-max))
  8005. (when what
  8006. (insert
  8007. (if (not (equal (char-before) ?\ )) " " "")
  8008. (cond ((eq what 'scheduled) org-scheduled-string)
  8009. ((eq what 'deadline) org-deadline-string)
  8010. ((eq what 'closed) org-closed-string))
  8011. " ")
  8012. (setq ts (org-insert-time-stamp
  8013. time
  8014. (or org-time-was-given
  8015. (and (eq what 'closed) org-log-done-with-time))
  8016. (eq what 'closed)
  8017. nil nil (list org-end-time-was-given)))
  8018. (end-of-line 1))
  8019. (goto-char (point-min))
  8020. (widen)
  8021. (if (looking-at "[ \t]+\r?\n")
  8022. (replace-match ""))
  8023. ts)))))
  8024. (defvar org-log-note-marker (make-marker))
  8025. (defvar org-log-note-purpose nil)
  8026. (defvar org-log-note-state nil)
  8027. (defvar org-log-note-how nil)
  8028. (defvar org-log-note-window-configuration nil)
  8029. (defvar org-log-note-return-to (make-marker))
  8030. (defvar org-log-post-message nil
  8031. "Message to be displayed after a log note has been stored.
  8032. The auto-repeater uses this.")
  8033. (defun org-add-note ()
  8034. "Add a note to the current entry.
  8035. This is done in the same way as adding a state change note."
  8036. (interactive)
  8037. (org-add-log-setup 'note nil t nil))
  8038. (defun org-add-log-setup (&optional purpose state findpos how)
  8039. "Set up the post command hook to take a note.
  8040. If this is about to TODO state change, the new state is expected in STATE.
  8041. When FINDPOS is non-nil, find the correct position for the note in
  8042. the current entry. If not, assume that it can be inserted at point."
  8043. (save-excursion
  8044. (when findpos
  8045. (org-back-to-heading t)
  8046. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8047. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8048. "[^\r\n]*\\)?"))
  8049. (goto-char (match-end 0))
  8050. (unless org-log-states-order-reversed
  8051. (and (= (char-after) ?\n) (forward-char 1))
  8052. (org-skip-over-state-notes)
  8053. (skip-chars-backward " \t\n\r")))
  8054. (move-marker org-log-note-marker (point))
  8055. (setq org-log-note-purpose purpose
  8056. org-log-note-state state
  8057. org-log-note-how how)
  8058. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  8059. (defun org-skip-over-state-notes ()
  8060. "Skip past the list of State notes in an entry."
  8061. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8062. (while (looking-at "[ \t]*- State")
  8063. (condition-case nil
  8064. (org-next-item)
  8065. (error (org-end-of-item)))))
  8066. (defun org-add-log-note (&optional purpose)
  8067. "Pop up a window for taking a note, and add this note later at point."
  8068. (remove-hook 'post-command-hook 'org-add-log-note)
  8069. (setq org-log-note-window-configuration (current-window-configuration))
  8070. (delete-other-windows)
  8071. (move-marker org-log-note-return-to (point))
  8072. (switch-to-buffer (marker-buffer org-log-note-marker))
  8073. (goto-char org-log-note-marker)
  8074. (org-switch-to-buffer-other-window "*Org Note*")
  8075. (erase-buffer)
  8076. (if (memq org-log-note-how '(time state))
  8077. (org-store-log-note)
  8078. (let ((org-inhibit-startup t)) (org-mode))
  8079. (insert (format "# Insert note for %s.
  8080. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8081. (cond
  8082. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8083. ((eq org-log-note-purpose 'done) "closed todo item")
  8084. ((eq org-log-note-purpose 'state)
  8085. (format "state change to \"%s\"" org-log-note-state))
  8086. ((eq org-log-note-purpose 'note)
  8087. "this entry")
  8088. (t (error "This should not happen")))))
  8089. (org-set-local 'org-finish-function 'org-store-log-note)))
  8090. (defvar org-note-abort nil) ; dynamically scoped
  8091. (defun org-store-log-note ()
  8092. "Finish taking a log note, and insert it to where it belongs."
  8093. (let ((txt (buffer-string))
  8094. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8095. lines ind)
  8096. (kill-buffer (current-buffer))
  8097. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8098. (setq txt (replace-match "" t t txt)))
  8099. (if (string-match "\\s-+\\'" txt)
  8100. (setq txt (replace-match "" t t txt)))
  8101. (setq lines (org-split-string txt "\n"))
  8102. (when (and note (string-match "\\S-" note))
  8103. (setq note
  8104. (org-replace-escapes
  8105. note
  8106. (list (cons "%u" (user-login-name))
  8107. (cons "%U" user-full-name)
  8108. (cons "%t" (format-time-string
  8109. (org-time-stamp-format 'long 'inactive)
  8110. (current-time)))
  8111. (cons "%s" (if org-log-note-state
  8112. (concat "\"" org-log-note-state "\"")
  8113. "")))))
  8114. (if lines (setq note (concat note " \\\\")))
  8115. (push note lines))
  8116. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8117. (when lines
  8118. (save-excursion
  8119. (set-buffer (marker-buffer org-log-note-marker))
  8120. (save-excursion
  8121. (goto-char org-log-note-marker)
  8122. (move-marker org-log-note-marker nil)
  8123. (end-of-line 1)
  8124. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8125. (indent-relative nil)
  8126. (insert "- " (pop lines))
  8127. (org-indent-line-function)
  8128. (beginning-of-line 1)
  8129. (looking-at "[ \t]*")
  8130. (setq ind (concat (match-string 0) " "))
  8131. (end-of-line 1)
  8132. (while lines (insert "\n" ind (pop lines)))))))
  8133. (set-window-configuration org-log-note-window-configuration)
  8134. (with-current-buffer (marker-buffer org-log-note-return-to)
  8135. (goto-char org-log-note-return-to))
  8136. (move-marker org-log-note-return-to nil)
  8137. (and org-log-post-message (message "%s" org-log-post-message)))
  8138. (defun org-sparse-tree (&optional arg)
  8139. "Create a sparse tree, prompt for the details.
  8140. This command can create sparse trees. You first need to select the type
  8141. of match used to create the tree:
  8142. t Show entries with a specific TODO keyword.
  8143. T Show entries selected by a tags match.
  8144. p Enter a property name and its value (both with completion on existing
  8145. names/values) and show entries with that property.
  8146. r Show entries matching a regular expression
  8147. d Show deadlines due within `org-deadline-warning-days'."
  8148. (interactive "P")
  8149. (let (ans kwd value)
  8150. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8151. (setq ans (read-char-exclusive))
  8152. (cond
  8153. ((equal ans ?d)
  8154. (call-interactively 'org-check-deadlines))
  8155. ((equal ans ?b)
  8156. (call-interactively 'org-check-before-date))
  8157. ((equal ans ?t)
  8158. (org-show-todo-tree '(4)))
  8159. ((equal ans ?T)
  8160. (call-interactively 'org-tags-sparse-tree))
  8161. ((member ans '(?p ?P))
  8162. (setq kwd (completing-read "Property: "
  8163. (mapcar 'list (org-buffer-property-keys))))
  8164. (setq value (completing-read "Value: "
  8165. (mapcar 'list (org-property-values kwd))))
  8166. (unless (string-match "\\`{.*}\\'" value)
  8167. (setq value (concat "\"" value "\"")))
  8168. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8169. ((member ans '(?r ?R ?/))
  8170. (call-interactively 'org-occur))
  8171. (t (error "No such sparse tree command \"%c\"" ans)))))
  8172. (defvar org-occur-highlights nil
  8173. "List of overlays used for occur matches.")
  8174. (make-variable-buffer-local 'org-occur-highlights)
  8175. (defvar org-occur-parameters nil
  8176. "Parameters of the active org-occur calls.
  8177. This is a list, each call to org-occur pushes as cons cell,
  8178. containing the regular expression and the callback, onto the list.
  8179. The list can contain several entries if `org-occur' has been called
  8180. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8181. will only contain one set of parameters. When the highlights are
  8182. removed (for example with `C-c C-c', or with the next edit (depending
  8183. on `org-remove-highlights-with-change'), this variable is emptied
  8184. as well.")
  8185. (make-variable-buffer-local 'org-occur-parameters)
  8186. (defun org-occur (regexp &optional keep-previous callback)
  8187. "Make a compact tree which shows all matches of REGEXP.
  8188. The tree will show the lines where the regexp matches, and all higher
  8189. headlines above the match. It will also show the heading after the match,
  8190. to make sure editing the matching entry is easy.
  8191. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8192. call to `org-occur' will be kept, to allow stacking of calls to this
  8193. command.
  8194. If CALLBACK is non-nil, it is a function which is called to confirm
  8195. that the match should indeed be shown."
  8196. (interactive "sRegexp: \nP")
  8197. (unless keep-previous
  8198. (org-remove-occur-highlights nil nil t))
  8199. (push (cons regexp callback) org-occur-parameters)
  8200. (let ((cnt 0))
  8201. (save-excursion
  8202. (goto-char (point-min))
  8203. (if (or (not keep-previous) ; do not want to keep
  8204. (not org-occur-highlights)) ; no previous matches
  8205. ;; hide everything
  8206. (org-overview))
  8207. (while (re-search-forward regexp nil t)
  8208. (when (or (not callback)
  8209. (save-match-data (funcall callback)))
  8210. (setq cnt (1+ cnt))
  8211. (when org-highlight-sparse-tree-matches
  8212. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8213. (org-show-context 'occur-tree))))
  8214. (when org-remove-highlights-with-change
  8215. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8216. nil 'local))
  8217. (unless org-sparse-tree-open-archived-trees
  8218. (org-hide-archived-subtrees (point-min) (point-max)))
  8219. (run-hooks 'org-occur-hook)
  8220. (if (interactive-p)
  8221. (message "%d match(es) for regexp %s" cnt regexp))
  8222. cnt))
  8223. (defun org-show-context (&optional key)
  8224. "Make sure point and context and visible.
  8225. How much context is shown depends upon the variables
  8226. `org-show-hierarchy-above', `org-show-following-heading'. and
  8227. `org-show-siblings'."
  8228. (let ((heading-p (org-on-heading-p t))
  8229. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8230. (following-p (org-get-alist-option org-show-following-heading key))
  8231. (entry-p (org-get-alist-option org-show-entry-below key))
  8232. (siblings-p (org-get-alist-option org-show-siblings key)))
  8233. (catch 'exit
  8234. ;; Show heading or entry text
  8235. (if (and heading-p (not entry-p))
  8236. (org-flag-heading nil) ; only show the heading
  8237. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8238. (org-show-hidden-entry))) ; show entire entry
  8239. (when following-p
  8240. ;; Show next sibling, or heading below text
  8241. (save-excursion
  8242. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8243. (org-flag-heading nil))))
  8244. (when siblings-p (org-show-siblings))
  8245. (when hierarchy-p
  8246. ;; show all higher headings, possibly with siblings
  8247. (save-excursion
  8248. (while (and (condition-case nil
  8249. (progn (org-up-heading-all 1) t)
  8250. (error nil))
  8251. (not (bobp)))
  8252. (org-flag-heading nil)
  8253. (when siblings-p (org-show-siblings))))))))
  8254. (defun org-reveal (&optional siblings)
  8255. "Show current entry, hierarchy above it, and the following headline.
  8256. This can be used to show a consistent set of context around locations
  8257. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8258. not t for the search context.
  8259. With optional argument SIBLINGS, on each level of the hierarchy all
  8260. siblings are shown. This repairs the tree structure to what it would
  8261. look like when opened with hierarchical calls to `org-cycle'."
  8262. (interactive "P")
  8263. (let ((org-show-hierarchy-above t)
  8264. (org-show-following-heading t)
  8265. (org-show-siblings (if siblings t org-show-siblings)))
  8266. (org-show-context nil)))
  8267. (defun org-highlight-new-match (beg end)
  8268. "Highlight from BEG to END and mark the highlight is an occur headline."
  8269. (let ((ov (org-make-overlay beg end)))
  8270. (org-overlay-put ov 'face 'secondary-selection)
  8271. (push ov org-occur-highlights)))
  8272. (defun org-remove-occur-highlights (&optional beg end noremove)
  8273. "Remove the occur highlights from the buffer.
  8274. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8275. from the `before-change-functions' in the current buffer."
  8276. (interactive)
  8277. (unless org-inhibit-highlight-removal
  8278. (mapc 'org-delete-overlay org-occur-highlights)
  8279. (setq org-occur-highlights nil)
  8280. (setq org-occur-parameters nil)
  8281. (unless noremove
  8282. (remove-hook 'before-change-functions
  8283. 'org-remove-occur-highlights 'local))))
  8284. ;;;; Priorities
  8285. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8286. "Regular expression matching the priority indicator.")
  8287. (defvar org-remove-priority-next-time nil)
  8288. (defun org-priority-up ()
  8289. "Increase the priority of the current item."
  8290. (interactive)
  8291. (org-priority 'up))
  8292. (defun org-priority-down ()
  8293. "Decrease the priority of the current item."
  8294. (interactive)
  8295. (org-priority 'down))
  8296. (defun org-priority (&optional action)
  8297. "Change the priority of an item by ARG.
  8298. ACTION can be `set', `up', `down', or a character."
  8299. (interactive)
  8300. (setq action (or action 'set))
  8301. (let (current new news have remove)
  8302. (save-excursion
  8303. (org-back-to-heading)
  8304. (if (looking-at org-priority-regexp)
  8305. (setq current (string-to-char (match-string 2))
  8306. have t)
  8307. (setq current org-default-priority))
  8308. (cond
  8309. ((or (eq action 'set) (integerp action))
  8310. (if (integerp action)
  8311. (setq new action)
  8312. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  8313. (setq new (read-char-exclusive)))
  8314. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8315. (= (upcase org-lowest-priority) org-lowest-priority))
  8316. (setq new (upcase new)))
  8317. (cond ((equal new ?\ ) (setq remove t))
  8318. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8319. (error "Priority must be between `%c' and `%c'"
  8320. org-highest-priority org-lowest-priority))))
  8321. ((eq action 'up)
  8322. (if (and (not have) (eq last-command this-command))
  8323. (setq new org-lowest-priority)
  8324. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8325. org-default-priority (1- current)))))
  8326. ((eq action 'down)
  8327. (if (and (not have) (eq last-command this-command))
  8328. (setq new org-highest-priority)
  8329. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8330. org-default-priority (1+ current)))))
  8331. (t (error "Invalid action")))
  8332. (if (or (< (upcase new) org-highest-priority)
  8333. (> (upcase new) org-lowest-priority))
  8334. (setq remove t))
  8335. (setq news (format "%c" new))
  8336. (if have
  8337. (if remove
  8338. (replace-match "" t t nil 1)
  8339. (replace-match news t t nil 2))
  8340. (if remove
  8341. (error "No priority cookie found in line")
  8342. (looking-at org-todo-line-regexp)
  8343. (if (match-end 2)
  8344. (progn
  8345. (goto-char (match-end 2))
  8346. (insert " [#" news "]"))
  8347. (goto-char (match-beginning 3))
  8348. (insert "[#" news "] ")))))
  8349. (org-preserve-lc (org-set-tags nil 'align))
  8350. (if remove
  8351. (message "Priority removed")
  8352. (message "Priority of current item set to %s" news))))
  8353. (defun org-get-priority (s)
  8354. "Find priority cookie and return priority."
  8355. (save-match-data
  8356. (if (not (string-match org-priority-regexp s))
  8357. (* 1000 (- org-lowest-priority org-default-priority))
  8358. (* 1000 (- org-lowest-priority
  8359. (string-to-char (match-string 2 s)))))))
  8360. ;;;; Tags
  8361. (defun org-scan-tags (action matcher &optional todo-only)
  8362. "Scan headline tags with inheritance and produce output ACTION.
  8363. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8364. evaluated, testing if a given set of tags qualifies a headline for
  8365. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8366. are included in the output."
  8367. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8368. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8369. (org-re
  8370. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8371. (props (list 'face nil
  8372. 'done-face 'org-done
  8373. 'undone-face nil
  8374. 'mouse-face 'highlight
  8375. 'org-not-done-regexp org-not-done-regexp
  8376. 'org-todo-regexp org-todo-regexp
  8377. 'keymap org-agenda-keymap
  8378. 'help-echo
  8379. (format "mouse-2 or RET jump to org file %s"
  8380. (abbreviate-file-name
  8381. (or (buffer-file-name (buffer-base-buffer))
  8382. (buffer-name (buffer-base-buffer)))))))
  8383. (case-fold-search nil)
  8384. lspos
  8385. tags tags-list tags-alist (llast 0) rtn level category i txt
  8386. todo marker entry priority)
  8387. (save-excursion
  8388. (goto-char (point-min))
  8389. (when (eq action 'sparse-tree)
  8390. (org-overview)
  8391. (org-remove-occur-highlights))
  8392. (while (re-search-forward re nil t)
  8393. (catch :skip
  8394. (setq todo (if (match-end 1) (match-string 2))
  8395. tags (if (match-end 4) (match-string 4)))
  8396. (goto-char (setq lspos (1+ (match-beginning 0))))
  8397. (setq level (org-reduced-level (funcall outline-level))
  8398. category (org-get-category))
  8399. (setq i llast llast level)
  8400. ;; remove tag lists from same and sublevels
  8401. (while (>= i level)
  8402. (when (setq entry (assoc i tags-alist))
  8403. (setq tags-alist (delete entry tags-alist)))
  8404. (setq i (1- i)))
  8405. ;; add the next tags
  8406. (when tags
  8407. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8408. tags-alist
  8409. (cons (cons level tags) tags-alist)))
  8410. ;; compile tags for current headline
  8411. (setq tags-list
  8412. (if org-use-tag-inheritance
  8413. (apply 'append (mapcar 'cdr tags-alist))
  8414. tags))
  8415. (when (and tags org-use-tag-inheritance
  8416. (not (eq t org-use-tag-inheritance)))
  8417. ;; selective inheritance, remove uninherited ones
  8418. (setcdr (car tags-alist)
  8419. (org-remove-uniherited-tags (cdar tags-alist))))
  8420. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8421. (eval matcher)
  8422. (or (not org-agenda-skip-archived-trees)
  8423. (not (member org-archive-tag tags-list))))
  8424. (and (eq action 'agenda) (org-agenda-skip))
  8425. ;; list this headline
  8426. (if (eq action 'sparse-tree)
  8427. (progn
  8428. (and org-highlight-sparse-tree-matches
  8429. (org-get-heading) (match-end 0)
  8430. (org-highlight-new-match
  8431. (match-beginning 0) (match-beginning 1)))
  8432. (org-show-context 'tags-tree))
  8433. (setq txt (org-format-agenda-item
  8434. ""
  8435. (concat
  8436. (if org-tags-match-list-sublevels
  8437. (make-string (1- level) ?.) "")
  8438. (org-get-heading))
  8439. category tags-list)
  8440. priority (org-get-priority txt))
  8441. (goto-char lspos)
  8442. (setq marker (org-agenda-new-marker))
  8443. (org-add-props txt props
  8444. 'org-marker marker 'org-hd-marker marker 'org-category category
  8445. 'priority priority 'type "tagsmatch")
  8446. (push txt rtn))
  8447. ;; if we are to skip sublevels, jump to end of subtree
  8448. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8449. (when (and (eq action 'sparse-tree)
  8450. (not org-sparse-tree-open-archived-trees))
  8451. (org-hide-archived-subtrees (point-min) (point-max)))
  8452. (nreverse rtn)))
  8453. (defun org-remove-uniherited-tags (tags)
  8454. "Remove all tags that are not inherited from the list TAGS."
  8455. (cond
  8456. ((eq org-use-tag-inheritance t) tags)
  8457. ((not org-use-tag-inheritance) nil)
  8458. ((stringp org-use-tag-inheritance)
  8459. (delq nil (mapcar
  8460. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8461. tags)))
  8462. ((listp org-use-tag-inheritance)
  8463. (org-delete-all org-use-tag-inheritance tags))))
  8464. (defvar todo-only) ;; dynamically scoped
  8465. (defun org-tags-sparse-tree (&optional todo-only match)
  8466. "Create a sparse tree according to tags string MATCH.
  8467. MATCH can contain positive and negative selection of tags, like
  8468. \"+WORK+URGENT-WITHBOSS\".
  8469. If optional argument TODO_ONLY is non-nil, only select lines that are
  8470. also TODO lines."
  8471. (interactive "P")
  8472. (org-prepare-agenda-buffers (list (current-buffer)))
  8473. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8474. (defvar org-cached-props nil)
  8475. (defun org-cached-entry-get (pom property)
  8476. (if (or (eq t org-use-property-inheritance)
  8477. (and (stringp org-use-property-inheritance)
  8478. (string-match org-use-property-inheritance property))
  8479. (and (listp org-use-property-inheritance)
  8480. (member property org-use-property-inheritance)))
  8481. ;; Caching is not possible, check it directly
  8482. (org-entry-get pom property 'inherit)
  8483. ;; Get all properties, so that we can do complicated checks easily
  8484. (cdr (assoc property (or org-cached-props
  8485. (setq org-cached-props
  8486. (org-entry-properties pom)))))))
  8487. (defun org-global-tags-completion-table (&optional files)
  8488. "Return the list of all tags in all agenda buffer/files."
  8489. (save-excursion
  8490. (org-uniquify
  8491. (delq nil
  8492. (apply 'append
  8493. (mapcar
  8494. (lambda (file)
  8495. (set-buffer (find-file-noselect file))
  8496. (append (org-get-buffer-tags)
  8497. (mapcar (lambda (x) (if (stringp (car-safe x))
  8498. (list (car-safe x)) nil))
  8499. org-tag-alist)))
  8500. (if (and files (car files))
  8501. files
  8502. (org-agenda-files))))))))
  8503. (defun org-make-tags-matcher (match)
  8504. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8505. ;; todo-only is scoped dynamically into this function, and the function
  8506. ;; may change it it the matcher asksk for it.
  8507. (unless match
  8508. ;; Get a new match request, with completion
  8509. (let ((org-last-tags-completion-table
  8510. (org-global-tags-completion-table)))
  8511. (setq match (completing-read
  8512. "Match: " 'org-tags-completion-function nil nil nil
  8513. 'org-tags-history))))
  8514. ;; Parse the string and create a lisp form
  8515. (let ((match0 match)
  8516. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  8517. minus tag mm
  8518. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8519. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  8520. (if (string-match "/+" match)
  8521. ;; match contains also a todo-matching request
  8522. (progn
  8523. (setq tagsmatch (substring match 0 (match-beginning 0))
  8524. todomatch (substring match (match-end 0)))
  8525. (if (string-match "^!" todomatch)
  8526. (setq todo-only t todomatch (substring todomatch 1)))
  8527. (if (string-match "^\\s-*$" todomatch)
  8528. (setq todomatch nil)))
  8529. ;; only matching tags
  8530. (setq tagsmatch match todomatch nil))
  8531. ;; Make the tags matcher
  8532. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8533. (setq tagsmatcher t)
  8534. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8535. (while (setq term (pop orterms))
  8536. (while (and (equal (substring term -1) "\\") orterms)
  8537. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8538. (while (string-match re term)
  8539. (setq minus (and (match-end 1)
  8540. (equal (match-string 1 term) "-"))
  8541. tag (match-string 2 term)
  8542. re-p (equal (string-to-char tag) ?{)
  8543. level-p (match-end 3)
  8544. prop-p (match-end 4)
  8545. mm (cond
  8546. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8547. (level-p `(= level ,(string-to-number
  8548. (match-string 3 term))))
  8549. (prop-p
  8550. (setq pn (match-string 4 term)
  8551. pv (match-string 5 term)
  8552. cat-p (equal pn "CATEGORY")
  8553. re-p (equal (string-to-char pv) ?{)
  8554. pv (substring pv 1 -1))
  8555. (if (equal pn "CATEGORY")
  8556. (setq gv '(get-text-property (point) 'org-category))
  8557. (setq gv `(org-cached-entry-get nil ,pn)))
  8558. (if re-p
  8559. `(string-match ,pv (or ,gv ""))
  8560. `(equal ,pv (or ,gv ""))))
  8561. (t `(member ,(downcase tag) tags-list)))
  8562. mm (if minus (list 'not mm) mm)
  8563. term (substring term (match-end 0)))
  8564. (push mm tagsmatcher))
  8565. (push (if (> (length tagsmatcher) 1)
  8566. (cons 'and tagsmatcher)
  8567. (car tagsmatcher))
  8568. orlist)
  8569. (setq tagsmatcher nil))
  8570. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8571. (setq tagsmatcher
  8572. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8573. ;; Make the todo matcher
  8574. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8575. (setq todomatcher t)
  8576. (setq orterms (org-split-string todomatch "|") orlist nil)
  8577. (while (setq term (pop orterms))
  8578. (while (string-match re term)
  8579. (setq minus (and (match-end 1)
  8580. (equal (match-string 1 term) "-"))
  8581. kwd (match-string 2 term)
  8582. re-p (equal (string-to-char kwd) ?{)
  8583. term (substring term (match-end 0))
  8584. mm (if re-p
  8585. `(string-match ,(substring kwd 1 -1) todo)
  8586. (list 'equal 'todo kwd))
  8587. mm (if minus (list 'not mm) mm))
  8588. (push mm todomatcher))
  8589. (push (if (> (length todomatcher) 1)
  8590. (cons 'and todomatcher)
  8591. (car todomatcher))
  8592. orlist)
  8593. (setq todomatcher nil))
  8594. (setq todomatcher (if (> (length orlist) 1)
  8595. (cons 'or orlist) (car orlist))))
  8596. ;; Return the string and lisp forms of the matcher
  8597. (setq matcher (if todomatcher
  8598. (list 'and tagsmatcher todomatcher)
  8599. tagsmatcher))
  8600. (cons match0 matcher)))
  8601. (defun org-match-any-p (re list)
  8602. "Does re match any element of list?"
  8603. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8604. (delq nil list))
  8605. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8606. (defvar org-tags-overlay (org-make-overlay 1 1))
  8607. (org-detach-overlay org-tags-overlay)
  8608. (defun org-get-tags-at (&optional pos)
  8609. "Get a list of all headline tags applicable at POS.
  8610. POS defaults to point. If tags are inherited, the list contains
  8611. the targets in the same sequence as the headlines appear, i.e.
  8612. sthe tags of the current headline come last."
  8613. (interactive)
  8614. (let (tags ltags lastpos parent)
  8615. (save-excursion
  8616. (save-restriction
  8617. (widen)
  8618. (goto-char (or pos (point)))
  8619. (save-match-data
  8620. (condition-case nil
  8621. (progn
  8622. (org-back-to-heading t)
  8623. (while (not (equal lastpos (point)))
  8624. (setq lastpos (point))
  8625. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8626. (setq ltags (org-split-string
  8627. (org-match-string-no-properties 1) ":"))
  8628. (setq tags (append (org-remove-uniherited-tags ltags)
  8629. tags)))
  8630. (or org-use-tag-inheritance (error ""))
  8631. (org-up-heading-all 1)
  8632. (setq parent t)))
  8633. (error nil))))
  8634. tags)))
  8635. (defun org-align-tags-here (to-col)
  8636. ;; Assumes that this is a headline
  8637. (let ((pos (point)) (col (current-column)) tags)
  8638. (beginning-of-line 1)
  8639. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8640. (< pos (match-beginning 2)))
  8641. (progn
  8642. (setq tags (match-string 2))
  8643. (goto-char (match-beginning 1))
  8644. (insert " ")
  8645. (delete-region (point) (1+ (match-end 0)))
  8646. (backward-char 1)
  8647. (move-to-column
  8648. (max (1+ (current-column))
  8649. (1+ col)
  8650. (if (> to-col 0)
  8651. to-col
  8652. (- (abs to-col) (length tags))))
  8653. t)
  8654. (insert tags)
  8655. (move-to-column (min (current-column) col) t))
  8656. (goto-char pos))))
  8657. (defun org-set-tags (&optional arg just-align)
  8658. "Set the tags for the current headline.
  8659. With prefix ARG, realign all tags in headings in the current buffer."
  8660. (interactive "P")
  8661. (let* ((re (concat "^" outline-regexp))
  8662. (current (org-get-tags-string))
  8663. (col (current-column))
  8664. (org-setting-tags t)
  8665. table current-tags inherited-tags ; computed below when needed
  8666. tags p0 c0 c1 rpl)
  8667. (if arg
  8668. (save-excursion
  8669. (goto-char (point-min))
  8670. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8671. (while (re-search-forward re nil t)
  8672. (org-set-tags nil t)
  8673. (end-of-line 1)))
  8674. (message "All tags realigned to column %d" org-tags-column))
  8675. (if just-align
  8676. (setq tags current)
  8677. ;; Get a new set of tags from the user
  8678. (save-excursion
  8679. (setq table (or org-tag-alist (org-get-buffer-tags))
  8680. org-last-tags-completion-table table
  8681. current-tags (org-split-string current ":")
  8682. inherited-tags (nreverse
  8683. (nthcdr (length current-tags)
  8684. (nreverse (org-get-tags-at))))
  8685. tags
  8686. (if (or (eq t org-use-fast-tag-selection)
  8687. (and org-use-fast-tag-selection
  8688. (delq nil (mapcar 'cdr table))))
  8689. (org-fast-tag-selection
  8690. current-tags inherited-tags table
  8691. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8692. (let ((org-add-colon-after-tag-completion t))
  8693. (org-trim
  8694. (org-without-partial-completion
  8695. (completing-read "Tags: " 'org-tags-completion-function
  8696. nil nil current 'org-tags-history)))))))
  8697. (while (string-match "[-+&]+" tags)
  8698. ;; No boolean logic, just a list
  8699. (setq tags (replace-match ":" t t tags))))
  8700. (if (string-match "\\`[\t ]*\\'" tags)
  8701. (setq tags "")
  8702. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8703. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8704. ;; Insert new tags at the correct column
  8705. (beginning-of-line 1)
  8706. (cond
  8707. ((and (equal current "") (equal tags "")))
  8708. ((re-search-forward
  8709. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8710. (point-at-eol) t)
  8711. (if (equal tags "")
  8712. (setq rpl "")
  8713. (goto-char (match-beginning 0))
  8714. (setq c0 (current-column) p0 (point)
  8715. c1 (max (1+ c0) (if (> org-tags-column 0)
  8716. org-tags-column
  8717. (- (- org-tags-column) (length tags))))
  8718. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8719. (replace-match rpl t t)
  8720. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8721. tags)
  8722. (t (error "Tags alignment failed")))
  8723. (move-to-column col)
  8724. (unless just-align
  8725. (run-hooks 'org-after-tags-change-hook)))))
  8726. (defun org-change-tag-in-region (beg end tag off)
  8727. "Add or remove TAG for each entry in the region.
  8728. This works in the agenda, and also in an org-mode buffer."
  8729. (interactive
  8730. (list (region-beginning) (region-end)
  8731. (let ((org-last-tags-completion-table
  8732. (if (org-mode-p)
  8733. (org-get-buffer-tags)
  8734. (org-global-tags-completion-table))))
  8735. (completing-read
  8736. "Tag: " 'org-tags-completion-function nil nil nil
  8737. 'org-tags-history))
  8738. (progn
  8739. (message "[s]et or [r]emove? ")
  8740. (equal (read-char-exclusive) ?r))))
  8741. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8742. (let ((agendap (equal major-mode 'org-agenda-mode))
  8743. l1 l2 m buf pos newhead (cnt 0))
  8744. (goto-char end)
  8745. (setq l2 (1- (org-current-line)))
  8746. (goto-char beg)
  8747. (setq l1 (org-current-line))
  8748. (loop for l from l1 to l2 do
  8749. (goto-line l)
  8750. (setq m (get-text-property (point) 'org-hd-marker))
  8751. (when (or (and (org-mode-p) (org-on-heading-p))
  8752. (and agendap m))
  8753. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8754. pos (if agendap m (point)))
  8755. (with-current-buffer buf
  8756. (save-excursion
  8757. (save-restriction
  8758. (goto-char pos)
  8759. (setq cnt (1+ cnt))
  8760. (org-toggle-tag tag (if off 'off 'on))
  8761. (setq newhead (org-get-heading)))))
  8762. (and agendap (org-agenda-change-all-lines newhead m))))
  8763. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8764. (defun org-tags-completion-function (string predicate &optional flag)
  8765. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8766. (confirm (lambda (x) (stringp (car x)))))
  8767. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8768. (setq s1 (match-string 1 string)
  8769. s2 (match-string 2 string))
  8770. (setq s1 "" s2 string))
  8771. (cond
  8772. ((eq flag nil)
  8773. ;; try completion
  8774. (setq rtn (try-completion s2 ctable confirm))
  8775. (if (stringp rtn)
  8776. (setq rtn
  8777. (concat s1 s2 (substring rtn (length s2))
  8778. (if (and org-add-colon-after-tag-completion
  8779. (assoc rtn ctable))
  8780. ":" ""))))
  8781. rtn)
  8782. ((eq flag t)
  8783. ;; all-completions
  8784. (all-completions s2 ctable confirm)
  8785. )
  8786. ((eq flag 'lambda)
  8787. ;; exact match?
  8788. (assoc s2 ctable)))
  8789. ))
  8790. (defun org-fast-tag-insert (kwd tags face &optional end)
  8791. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8792. (insert (format "%-12s" (concat kwd ":"))
  8793. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8794. (or end "")))
  8795. (defun org-fast-tag-show-exit (flag)
  8796. (save-excursion
  8797. (goto-line 3)
  8798. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8799. (replace-match ""))
  8800. (when flag
  8801. (end-of-line 1)
  8802. (move-to-column (- (window-width) 19) t)
  8803. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8804. (defun org-set-current-tags-overlay (current prefix)
  8805. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8806. (if (featurep 'xemacs)
  8807. (org-overlay-display org-tags-overlay (concat prefix s)
  8808. 'secondary-selection)
  8809. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8810. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8811. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8812. "Fast tag selection with single keys.
  8813. CURRENT is the current list of tags in the headline, INHERITED is the
  8814. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8815. possibly with grouping information. TODO-TABLE is a similar table with
  8816. TODO keywords, should these have keys assigned to them.
  8817. If the keys are nil, a-z are automatically assigned.
  8818. Returns the new tags string, or nil to not change the current settings."
  8819. (let* ((fulltable (append table todo-table))
  8820. (maxlen (apply 'max (mapcar
  8821. (lambda (x)
  8822. (if (stringp (car x)) (string-width (car x)) 0))
  8823. fulltable)))
  8824. (buf (current-buffer))
  8825. (expert (eq org-fast-tag-selection-single-key 'expert))
  8826. (buffer-tags nil)
  8827. (fwidth (+ maxlen 3 1 3))
  8828. (ncol (/ (- (window-width) 4) fwidth))
  8829. (i-face 'org-done)
  8830. (c-face 'org-todo)
  8831. tg cnt e c char c1 c2 ntable tbl rtn
  8832. ov-start ov-end ov-prefix
  8833. (exit-after-next org-fast-tag-selection-single-key)
  8834. (done-keywords org-done-keywords)
  8835. groups ingroup)
  8836. (save-excursion
  8837. (beginning-of-line 1)
  8838. (if (looking-at
  8839. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8840. (setq ov-start (match-beginning 1)
  8841. ov-end (match-end 1)
  8842. ov-prefix "")
  8843. (setq ov-start (1- (point-at-eol))
  8844. ov-end (1+ ov-start))
  8845. (skip-chars-forward "^\n\r")
  8846. (setq ov-prefix
  8847. (concat
  8848. (buffer-substring (1- (point)) (point))
  8849. (if (> (current-column) org-tags-column)
  8850. " "
  8851. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8852. (org-move-overlay org-tags-overlay ov-start ov-end)
  8853. (save-window-excursion
  8854. (if expert
  8855. (set-buffer (get-buffer-create " *Org tags*"))
  8856. (delete-other-windows)
  8857. (split-window-vertically)
  8858. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8859. (erase-buffer)
  8860. (org-set-local 'org-done-keywords done-keywords)
  8861. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8862. (org-fast-tag-insert "Current" current c-face "\n\n")
  8863. (org-fast-tag-show-exit exit-after-next)
  8864. (org-set-current-tags-overlay current ov-prefix)
  8865. (setq tbl fulltable char ?a cnt 0)
  8866. (while (setq e (pop tbl))
  8867. (cond
  8868. ((equal e '(:startgroup))
  8869. (push '() groups) (setq ingroup t)
  8870. (when (not (= cnt 0))
  8871. (setq cnt 0)
  8872. (insert "\n"))
  8873. (insert "{ "))
  8874. ((equal e '(:endgroup))
  8875. (setq ingroup nil cnt 0)
  8876. (insert "}\n"))
  8877. (t
  8878. (setq tg (car e) c2 nil)
  8879. (if (cdr e)
  8880. (setq c (cdr e))
  8881. ;; automatically assign a character.
  8882. (setq c1 (string-to-char
  8883. (downcase (substring
  8884. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8885. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8886. (while (or (rassoc char ntable) (rassoc char table))
  8887. (setq char (1+ char)))
  8888. (setq c2 c1))
  8889. (setq c (or c2 char)))
  8890. (if ingroup (push tg (car groups)))
  8891. (setq tg (org-add-props tg nil 'face
  8892. (cond
  8893. ((not (assoc tg table))
  8894. (org-get-todo-face tg))
  8895. ((member tg current) c-face)
  8896. ((member tg inherited) i-face)
  8897. (t nil))))
  8898. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8899. (insert "[" c "] " tg (make-string
  8900. (- fwidth 4 (length tg)) ?\ ))
  8901. (push (cons tg c) ntable)
  8902. (when (= (setq cnt (1+ cnt)) ncol)
  8903. (insert "\n")
  8904. (if ingroup (insert " "))
  8905. (setq cnt 0)))))
  8906. (setq ntable (nreverse ntable))
  8907. (insert "\n")
  8908. (goto-char (point-min))
  8909. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8910. (fit-window-to-buffer))
  8911. (setq rtn
  8912. (catch 'exit
  8913. (while t
  8914. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8915. (if groups " [!] no groups" " [!]groups")
  8916. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8917. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8918. (cond
  8919. ((= c ?\r) (throw 'exit t))
  8920. ((= c ?!)
  8921. (setq groups (not groups))
  8922. (goto-char (point-min))
  8923. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8924. ((= c ?\C-c)
  8925. (if (not expert)
  8926. (org-fast-tag-show-exit
  8927. (setq exit-after-next (not exit-after-next)))
  8928. (setq expert nil)
  8929. (delete-other-windows)
  8930. (split-window-vertically)
  8931. (org-switch-to-buffer-other-window " *Org tags*")
  8932. (and (fboundp 'fit-window-to-buffer)
  8933. (fit-window-to-buffer))))
  8934. ((or (= c ?\C-g)
  8935. (and (= c ?q) (not (rassoc c ntable))))
  8936. (org-detach-overlay org-tags-overlay)
  8937. (setq quit-flag t))
  8938. ((= c ?\ )
  8939. (setq current nil)
  8940. (if exit-after-next (setq exit-after-next 'now)))
  8941. ((= c ?\t)
  8942. (condition-case nil
  8943. (setq tg (completing-read
  8944. "Tag: "
  8945. (or buffer-tags
  8946. (with-current-buffer buf
  8947. (org-get-buffer-tags)))))
  8948. (quit (setq tg "")))
  8949. (when (string-match "\\S-" tg)
  8950. (add-to-list 'buffer-tags (list tg))
  8951. (if (member tg current)
  8952. (setq current (delete tg current))
  8953. (push tg current)))
  8954. (if exit-after-next (setq exit-after-next 'now)))
  8955. ((setq e (rassoc c todo-table) tg (car e))
  8956. (with-current-buffer buf
  8957. (save-excursion (org-todo tg)))
  8958. (if exit-after-next (setq exit-after-next 'now)))
  8959. ((setq e (rassoc c ntable) tg (car e))
  8960. (if (member tg current)
  8961. (setq current (delete tg current))
  8962. (loop for g in groups do
  8963. (if (member tg g)
  8964. (mapc (lambda (x)
  8965. (setq current (delete x current)))
  8966. g)))
  8967. (push tg current))
  8968. (if exit-after-next (setq exit-after-next 'now))))
  8969. ;; Create a sorted list
  8970. (setq current
  8971. (sort current
  8972. (lambda (a b)
  8973. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8974. (if (eq exit-after-next 'now) (throw 'exit t))
  8975. (goto-char (point-min))
  8976. (beginning-of-line 2)
  8977. (delete-region (point) (point-at-eol))
  8978. (org-fast-tag-insert "Current" current c-face)
  8979. (org-set-current-tags-overlay current ov-prefix)
  8980. (while (re-search-forward
  8981. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8982. (setq tg (match-string 1))
  8983. (add-text-properties
  8984. (match-beginning 1) (match-end 1)
  8985. (list 'face
  8986. (cond
  8987. ((member tg current) c-face)
  8988. ((member tg inherited) i-face)
  8989. (t (get-text-property (match-beginning 1) 'face))))))
  8990. (goto-char (point-min)))))
  8991. (org-detach-overlay org-tags-overlay)
  8992. (if rtn
  8993. (mapconcat 'identity current ":")
  8994. nil))))
  8995. (defun org-get-tags-string ()
  8996. "Get the TAGS string in the current headline."
  8997. (unless (org-on-heading-p t)
  8998. (error "Not on a heading"))
  8999. (save-excursion
  9000. (beginning-of-line 1)
  9001. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9002. (org-match-string-no-properties 1)
  9003. "")))
  9004. (defun org-get-tags ()
  9005. "Get the list of tags specified in the current headline."
  9006. (org-split-string (org-get-tags-string) ":"))
  9007. (defun org-get-buffer-tags ()
  9008. "Get a table of all tags used in the buffer, for completion."
  9009. (let (tags)
  9010. (save-excursion
  9011. (goto-char (point-min))
  9012. (while (re-search-forward
  9013. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9014. (when (equal (char-after (point-at-bol 0)) ?*)
  9015. (mapc (lambda (x) (add-to-list 'tags x))
  9016. (org-split-string (org-match-string-no-properties 1) ":")))))
  9017. (mapcar 'list tags)))
  9018. ;;;; Properties
  9019. ;;; Setting and retrieving properties
  9020. (defconst org-special-properties
  9021. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9022. "TIMESTAMP" "TIMESTAMP_IA")
  9023. "The special properties valid in Org-mode.
  9024. These are properties that are not defined in the property drawer,
  9025. but in some other way.")
  9026. (defconst org-default-properties
  9027. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9028. "LOCATION" "LOGGING" "COLUMNS")
  9029. "Some properties that are used by Org-mode for various purposes.
  9030. Being in this list makes sure that they are offered for completion.")
  9031. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9032. "Regular expression matching the first line of a property drawer.")
  9033. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9034. "Regular expression matching the first line of a property drawer.")
  9035. (defun org-property-action ()
  9036. "Do an action on properties."
  9037. (interactive)
  9038. (let (c)
  9039. (org-at-property-p)
  9040. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9041. (setq c (read-char-exclusive))
  9042. (cond
  9043. ((equal c ?s)
  9044. (call-interactively 'org-set-property))
  9045. ((equal c ?d)
  9046. (call-interactively 'org-delete-property))
  9047. ((equal c ?D)
  9048. (call-interactively 'org-delete-property-globally))
  9049. ((equal c ?c)
  9050. (call-interactively 'org-compute-property-at-point))
  9051. (t (error "No such property action %c" c)))))
  9052. (defun org-at-property-p ()
  9053. "Is the cursor in a property line?"
  9054. ;; FIXME: Does not check if we are actually in the drawer.
  9055. ;; FIXME: also returns true on any drawers.....
  9056. ;; This is used by C-c C-c for property action.
  9057. (save-excursion
  9058. (beginning-of-line 1)
  9059. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9060. (defun org-get-property-block (&optional beg end force)
  9061. "Return the (beg . end) range of the body of the property drawer.
  9062. BEG and END can be beginning and end of subtree, if not given
  9063. they will be found.
  9064. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9065. (catch 'exit
  9066. (save-excursion
  9067. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9068. (end (or end (progn (outline-next-heading) (point)))))
  9069. (goto-char beg)
  9070. (if (re-search-forward org-property-start-re end t)
  9071. (setq beg (1+ (match-end 0)))
  9072. (if force
  9073. (save-excursion
  9074. (org-insert-property-drawer)
  9075. (setq end (progn (outline-next-heading) (point))))
  9076. (throw 'exit nil))
  9077. (goto-char beg)
  9078. (if (re-search-forward org-property-start-re end t)
  9079. (setq beg (1+ (match-end 0)))))
  9080. (if (re-search-forward org-property-end-re end t)
  9081. (setq end (match-beginning 0))
  9082. (or force (throw 'exit nil))
  9083. (goto-char beg)
  9084. (setq end beg)
  9085. (org-indent-line-function)
  9086. (insert ":END:\n"))
  9087. (cons beg end)))))
  9088. (defun org-entry-properties (&optional pom which)
  9089. "Get all properties of the entry at point-or-marker POM.
  9090. This includes the TODO keyword, the tags, time strings for deadline,
  9091. scheduled, and clocking, and any additional properties defined in the
  9092. entry. The return value is an alist, keys may occur multiple times
  9093. if the property key was used several times.
  9094. POM may also be nil, in which case the current entry is used.
  9095. If WHICH is nil or `all', get all properties. If WHICH is
  9096. `special' or `standard', only get that subclass."
  9097. (setq which (or which 'all))
  9098. (org-with-point-at pom
  9099. (let ((clockstr (substring org-clock-string 0 -1))
  9100. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9101. beg end range props sum-props key value string clocksum)
  9102. (save-excursion
  9103. (when (condition-case nil (org-back-to-heading t) (error nil))
  9104. (setq beg (point))
  9105. (setq sum-props (get-text-property (point) 'org-summaries))
  9106. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9107. (outline-next-heading)
  9108. (setq end (point))
  9109. (when (memq which '(all special))
  9110. ;; Get the special properties, like TODO and tags
  9111. (goto-char beg)
  9112. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9113. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9114. (when (looking-at org-priority-regexp)
  9115. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9116. (when (and (setq value (org-get-tags-string))
  9117. (string-match "\\S-" value))
  9118. (push (cons "TAGS" value) props))
  9119. (when (setq value (org-get-tags-at))
  9120. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9121. props))
  9122. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9123. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9124. string (if (equal key clockstr)
  9125. (org-no-properties
  9126. (org-trim
  9127. (buffer-substring
  9128. (match-beginning 3) (goto-char (point-at-eol)))))
  9129. (substring (org-match-string-no-properties 3) 1 -1)))
  9130. (unless key
  9131. (if (= (char-after (match-beginning 3)) ?\[)
  9132. (setq key "TIMESTAMP_IA")
  9133. (setq key "TIMESTAMP")))
  9134. (when (or (equal key clockstr) (not (assoc key props)))
  9135. (push (cons key string) props)))
  9136. )
  9137. (when (memq which '(all standard))
  9138. ;; Get the standard properties, like :PORP: ...
  9139. (setq range (org-get-property-block beg end))
  9140. (when range
  9141. (goto-char (car range))
  9142. (while (re-search-forward
  9143. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9144. (cdr range) t)
  9145. (setq key (org-match-string-no-properties 1)
  9146. value (org-trim (or (org-match-string-no-properties 2) "")))
  9147. (unless (member key excluded)
  9148. (push (cons key (or value "")) props)))))
  9149. (if clocksum
  9150. (push (cons "CLOCKSUM"
  9151. (org-column-number-to-string (/ (float clocksum) 60.)
  9152. 'add_times))
  9153. props))
  9154. (append sum-props (nreverse props)))))))
  9155. (defun org-entry-get (pom property &optional inherit)
  9156. "Get value of PROPERTY for entry at point-or-marker POM.
  9157. If INHERIT is non-nil and the entry does not have the property,
  9158. then also check higher levels of the hierarchy.
  9159. If INHERIT is the symbol `selective', use inheritance only if the setting
  9160. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9161. If the property is present but empty, the return value is the empty string.
  9162. If the property is not present at all, nil is returned."
  9163. (org-with-point-at pom
  9164. (if (and inherit (if (eq inherit 'selective)
  9165. (org-property-inherit-p property)
  9166. t))
  9167. (org-entry-get-with-inheritance property)
  9168. (if (member property org-special-properties)
  9169. ;; We need a special property. Use brute force, get all properties.
  9170. (cdr (assoc property (org-entry-properties nil 'special)))
  9171. (let ((range (org-get-property-block)))
  9172. (if (and range
  9173. (goto-char (car range))
  9174. (re-search-forward
  9175. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9176. (cdr range) t))
  9177. ;; Found the property, return it.
  9178. (if (match-end 1)
  9179. (org-match-string-no-properties 1)
  9180. "")))))))
  9181. (defun org-property-or-variable-value (var &optional inherit)
  9182. "Check if there is a property fixing the value of VAR.
  9183. If yes, return this value. If not, return the current value of the variable."
  9184. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9185. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9186. (read prop)
  9187. (symbol-value var))))
  9188. (defun org-entry-delete (pom property)
  9189. "Delete the property PROPERTY from entry at point-or-marker POM."
  9190. (org-with-point-at pom
  9191. (if (member property org-special-properties)
  9192. nil ; cannot delete these properties.
  9193. (let ((range (org-get-property-block)))
  9194. (if (and range
  9195. (goto-char (car range))
  9196. (re-search-forward
  9197. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9198. (cdr range) t))
  9199. (progn
  9200. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9201. t)
  9202. nil)))))
  9203. ;; Multi-values properties are properties that contain multiple values
  9204. ;; These values are assumed to be single words, separated by whitespace.
  9205. (defun org-entry-add-to-multivalued-property (pom property value)
  9206. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9207. (let* ((old (org-entry-get pom property))
  9208. (values (and old (org-split-string old "[ \t]"))))
  9209. (unless (member value values)
  9210. (setq values (cons value values))
  9211. (org-entry-put pom property
  9212. (mapconcat 'identity values " ")))))
  9213. (defun org-entry-remove-from-multivalued-property (pom property value)
  9214. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9215. (let* ((old (org-entry-get pom property))
  9216. (values (and old (org-split-string old "[ \t]"))))
  9217. (when (member value values)
  9218. (setq values (delete value values))
  9219. (org-entry-put pom property
  9220. (mapconcat 'identity values " ")))))
  9221. (defun org-entry-member-in-multivalued-property (pom property value)
  9222. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9223. (let* ((old (org-entry-get pom property))
  9224. (values (and old (org-split-string old "[ \t]"))))
  9225. (member value values)))
  9226. (defvar org-entry-property-inherited-from (make-marker))
  9227. (defun org-entry-get-with-inheritance (property)
  9228. "Get entry property, and search higher levels if not present."
  9229. (let (tmp)
  9230. (save-excursion
  9231. (save-restriction
  9232. (widen)
  9233. (catch 'ex
  9234. (while t
  9235. (when (setq tmp (org-entry-get nil property))
  9236. (org-back-to-heading t)
  9237. (move-marker org-entry-property-inherited-from (point))
  9238. (throw 'ex tmp))
  9239. (or (org-up-heading-safe) (throw 'ex nil)))))
  9240. (or tmp (cdr (assoc property org-local-properties))
  9241. (cdr (assoc property org-global-properties))))))
  9242. (defun org-entry-put (pom property value)
  9243. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9244. (org-with-point-at pom
  9245. (org-back-to-heading t)
  9246. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9247. range)
  9248. (cond
  9249. ((equal property "TODO")
  9250. (when (and (stringp value) (string-match "\\S-" value)
  9251. (not (member value org-todo-keywords-1)))
  9252. (error "\"%s\" is not a valid TODO state" value))
  9253. (if (or (not value)
  9254. (not (string-match "\\S-" value)))
  9255. (setq value 'none))
  9256. (org-todo value)
  9257. (org-set-tags nil 'align))
  9258. ((equal property "PRIORITY")
  9259. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9260. (string-to-char value) ?\ ))
  9261. (org-set-tags nil 'align))
  9262. ((equal property "SCHEDULED")
  9263. (if (re-search-forward org-scheduled-time-regexp end t)
  9264. (cond
  9265. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9266. ((eq value 'later) (org-timestamp-change 1 'day))
  9267. (t (call-interactively 'org-schedule)))
  9268. (call-interactively 'org-schedule)))
  9269. ((equal property "DEADLINE")
  9270. (if (re-search-forward org-deadline-time-regexp end t)
  9271. (cond
  9272. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9273. ((eq value 'later) (org-timestamp-change 1 'day))
  9274. (t (call-interactively 'org-deadline)))
  9275. (call-interactively 'org-deadline)))
  9276. ((member property org-special-properties)
  9277. (error "The %s property can not yet be set with `org-entry-put'"
  9278. property))
  9279. (t ; a non-special property
  9280. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9281. (setq range (org-get-property-block beg end 'force))
  9282. (goto-char (car range))
  9283. (if (re-search-forward
  9284. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9285. (progn
  9286. (delete-region (match-beginning 1) (match-end 1))
  9287. (goto-char (match-beginning 1)))
  9288. (goto-char (cdr range))
  9289. (insert "\n")
  9290. (backward-char 1)
  9291. (org-indent-line-function)
  9292. (insert ":" property ":"))
  9293. (and value (insert " " value))
  9294. (org-indent-line-function)))))))
  9295. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9296. "Get all property keys in the current buffer.
  9297. With INCLUDE-SPECIALS, also list the special properties that relect things
  9298. like tags and TODO state.
  9299. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9300. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9301. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9302. formats in the current buffer."
  9303. (let (rtn range cfmt cols s p)
  9304. (save-excursion
  9305. (save-restriction
  9306. (widen)
  9307. (goto-char (point-min))
  9308. (while (re-search-forward org-property-start-re nil t)
  9309. (setq range (org-get-property-block))
  9310. (goto-char (car range))
  9311. (while (re-search-forward
  9312. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9313. (cdr range) t)
  9314. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9315. (outline-next-heading))))
  9316. (when include-specials
  9317. (setq rtn (append org-special-properties rtn)))
  9318. (when include-defaults
  9319. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9320. (when include-columns
  9321. (save-excursion
  9322. (save-restriction
  9323. (widen)
  9324. (goto-char (point-min))
  9325. (while (re-search-forward
  9326. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9327. nil t)
  9328. (setq cfmt (match-string 2) s 0)
  9329. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9330. cfmt s)
  9331. (setq s (match-end 0)
  9332. p (match-string 1 cfmt))
  9333. (unless (or (equal p "ITEM")
  9334. (member p org-special-properties))
  9335. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9336. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9337. (defun org-property-values (key)
  9338. "Return a list of all values of property KEY."
  9339. (save-excursion
  9340. (save-restriction
  9341. (widen)
  9342. (goto-char (point-min))
  9343. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9344. values)
  9345. (while (re-search-forward re nil t)
  9346. (add-to-list 'values (org-trim (match-string 1))))
  9347. (delete "" values)))))
  9348. (defun org-insert-property-drawer ()
  9349. "Insert a property drawer into the current entry."
  9350. (interactive)
  9351. (org-back-to-heading t)
  9352. (looking-at outline-regexp)
  9353. (let ((indent (- (match-end 0)(match-beginning 0)))
  9354. (beg (point))
  9355. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9356. end hiddenp)
  9357. (outline-next-heading)
  9358. (setq end (point))
  9359. (goto-char beg)
  9360. (while (re-search-forward re end t))
  9361. (setq hiddenp (org-invisible-p))
  9362. (end-of-line 1)
  9363. (and (equal (char-after) ?\n) (forward-char 1))
  9364. (org-skip-over-state-notes)
  9365. (skip-chars-backward " \t\n\r")
  9366. (if (eq (char-before) ?*) (forward-char 1))
  9367. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9368. (beginning-of-line 0)
  9369. (indent-to-column indent)
  9370. (beginning-of-line 2)
  9371. (indent-to-column indent)
  9372. (beginning-of-line 0)
  9373. (if hiddenp
  9374. (save-excursion
  9375. (org-back-to-heading t)
  9376. (hide-entry))
  9377. (org-flag-drawer t))))
  9378. (defun org-set-property (property value)
  9379. "In the current entry, set PROPERTY to VALUE.
  9380. When called interactively, this will prompt for a property name, offering
  9381. completion on existing and default properties. And then it will prompt
  9382. for a value, offering competion either on allowed values (via an inherited
  9383. xxx_ALL property) or on existing values in other instances of this property
  9384. in the current file."
  9385. (interactive
  9386. (let* ((prop (completing-read
  9387. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  9388. (cur (org-entry-get nil prop))
  9389. (allowed (org-property-get-allowed-values nil prop 'table))
  9390. (existing (mapcar 'list (org-property-values prop)))
  9391. (val (if allowed
  9392. (completing-read "Value: " allowed nil 'req-match)
  9393. (completing-read
  9394. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9395. (concat "[" cur "]") "")
  9396. ": ")
  9397. existing nil nil "" nil cur))))
  9398. (list prop (if (equal val "") cur val))))
  9399. (unless (equal (org-entry-get nil property) value)
  9400. (org-entry-put nil property value)))
  9401. (defun org-delete-property (property)
  9402. "In the current entry, delete PROPERTY."
  9403. (interactive
  9404. (let* ((prop (completing-read
  9405. "Property: " (org-entry-properties nil 'standard))))
  9406. (list prop)))
  9407. (message "Property %s %s" property
  9408. (if (org-entry-delete nil property)
  9409. "deleted"
  9410. "was not present in the entry")))
  9411. (defun org-delete-property-globally (property)
  9412. "Remove PROPERTY globally, from all entries."
  9413. (interactive
  9414. (let* ((prop (completing-read
  9415. "Globally remove property: "
  9416. (mapcar 'list (org-buffer-property-keys)))))
  9417. (list prop)))
  9418. (save-excursion
  9419. (save-restriction
  9420. (widen)
  9421. (goto-char (point-min))
  9422. (let ((cnt 0))
  9423. (while (re-search-forward
  9424. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9425. nil t)
  9426. (setq cnt (1+ cnt))
  9427. (replace-match ""))
  9428. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9429. (defvar org-columns-current-fmt-compiled) ; defined below
  9430. (defun org-compute-property-at-point ()
  9431. "Compute the property at point.
  9432. This looks for an enclosing column format, extracts the operator and
  9433. then applies it to the proerty in the column format's scope."
  9434. (interactive)
  9435. (unless (org-at-property-p)
  9436. (error "Not at a property"))
  9437. (let ((prop (org-match-string-no-properties 2)))
  9438. (org-columns-get-format-and-top-level)
  9439. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9440. (error "No operator defined for property %s" prop))
  9441. (org-columns-compute prop)))
  9442. (defun org-property-get-allowed-values (pom property &optional table)
  9443. "Get allowed values for the property PROPERTY.
  9444. When TABLE is non-nil, return an alist that can directly be used for
  9445. completion."
  9446. (let (vals)
  9447. (cond
  9448. ((equal property "TODO")
  9449. (setq vals (org-with-point-at pom
  9450. (append org-todo-keywords-1 '("")))))
  9451. ((equal property "PRIORITY")
  9452. (let ((n org-lowest-priority))
  9453. (while (>= n org-highest-priority)
  9454. (push (char-to-string n) vals)
  9455. (setq n (1- n)))))
  9456. ((member property org-special-properties))
  9457. (t
  9458. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9459. (when (and vals (string-match "\\S-" vals))
  9460. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9461. (setq vals (mapcar (lambda (x)
  9462. (cond ((stringp x) x)
  9463. ((numberp x) (number-to-string x))
  9464. ((symbolp x) (symbol-name x))
  9465. (t "???")))
  9466. vals)))))
  9467. (if table (mapcar 'list vals) vals)))
  9468. (defun org-property-previous-allowed-value (&optional previous)
  9469. "Switch to the next allowed value for this property."
  9470. (interactive)
  9471. (org-property-next-allowed-value t))
  9472. (defun org-property-next-allowed-value (&optional previous)
  9473. "Switch to the next allowed value for this property."
  9474. (interactive)
  9475. (unless (org-at-property-p)
  9476. (error "Not at a property"))
  9477. (let* ((key (match-string 2))
  9478. (value (match-string 3))
  9479. (allowed (or (org-property-get-allowed-values (point) key)
  9480. (and (member value '("[ ]" "[-]" "[X]"))
  9481. '("[ ]" "[X]"))))
  9482. nval)
  9483. (unless allowed
  9484. (error "Allowed values for this property have not been defined"))
  9485. (if previous (setq allowed (reverse allowed)))
  9486. (if (member value allowed)
  9487. (setq nval (car (cdr (member value allowed)))))
  9488. (setq nval (or nval (car allowed)))
  9489. (if (equal nval value)
  9490. (error "Only one allowed value for this property"))
  9491. (org-at-property-p)
  9492. (replace-match (concat " :" key ": " nval) t t)
  9493. (org-indent-line-function)
  9494. (beginning-of-line 1)
  9495. (skip-chars-forward " \t")))
  9496. (defun org-find-entry-with-id (ident)
  9497. "Locate the entry that contains the ID property with exact value IDENT.
  9498. IDENT can be a string, a symbol or a number, this function will search for
  9499. the string representation of it.
  9500. Return the position where this entry starts, or nil if there is no such entry."
  9501. (let ((id (cond
  9502. ((stringp ident) ident)
  9503. ((symbol-name ident) (symbol-name ident))
  9504. ((numberp ident) (number-to-string ident))
  9505. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9506. (case-fold-search nil))
  9507. (save-excursion
  9508. (save-restriction
  9509. (widen)
  9510. (goto-char (point-min))
  9511. (when (re-search-forward
  9512. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9513. nil t)
  9514. (org-back-to-heading)
  9515. (point))))))
  9516. ;;; Column View
  9517. (defvar org-columns-overlays nil
  9518. "Holds the list of current column overlays.")
  9519. (defvar org-columns-current-fmt nil
  9520. "Local variable, holds the currently active column format.")
  9521. (defvar org-columns-current-fmt-compiled nil
  9522. "Local variable, holds the currently active column format.
  9523. This is the compiled version of the format.")
  9524. (defvar org-columns-current-widths nil
  9525. "Loval variable, holds the currently widths of fields.")
  9526. (defvar org-columns-current-maxwidths nil
  9527. "Loval variable, holds the currently active maximum column widths.")
  9528. (defvar org-columns-begin-marker (make-marker)
  9529. "Points to the position where last a column creation command was called.")
  9530. (defvar org-columns-top-level-marker (make-marker)
  9531. "Points to the position where current columns region starts.")
  9532. (defvar org-columns-map (make-sparse-keymap)
  9533. "The keymap valid in column display.")
  9534. (defun org-columns-content ()
  9535. "Switch to contents view while in columns view."
  9536. (interactive)
  9537. (org-overview)
  9538. (org-content))
  9539. (org-defkey org-columns-map "c" 'org-columns-content)
  9540. (org-defkey org-columns-map "o" 'org-overview)
  9541. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  9542. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  9543. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  9544. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  9545. (org-defkey org-columns-map "v" 'org-columns-show-value)
  9546. (org-defkey org-columns-map "q" 'org-columns-quit)
  9547. (org-defkey org-columns-map "r" 'org-columns-redo)
  9548. (org-defkey org-columns-map "g" 'org-columns-redo)
  9549. (org-defkey org-columns-map [left] 'backward-char)
  9550. (org-defkey org-columns-map "\M-b" 'backward-char)
  9551. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  9552. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  9553. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  9554. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  9555. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  9556. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  9557. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  9558. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  9559. (org-defkey org-columns-map "<" 'org-columns-narrow)
  9560. (org-defkey org-columns-map ">" 'org-columns-widen)
  9561. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  9562. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  9563. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  9564. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  9565. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  9566. '("Column"
  9567. ["Edit property" org-columns-edit-value t]
  9568. ["Next allowed value" org-columns-next-allowed-value t]
  9569. ["Previous allowed value" org-columns-previous-allowed-value t]
  9570. ["Show full value" org-columns-show-value t]
  9571. ["Edit allowed values" org-columns-edit-allowed t]
  9572. "--"
  9573. ["Edit column attributes" org-columns-edit-attributes t]
  9574. ["Increase column width" org-columns-widen t]
  9575. ["Decrease column width" org-columns-narrow t]
  9576. "--"
  9577. ["Move column right" org-columns-move-right t]
  9578. ["Move column left" org-columns-move-left t]
  9579. ["Add column" org-columns-new t]
  9580. ["Delete column" org-columns-delete t]
  9581. "--"
  9582. ["CONTENTS" org-columns-content t]
  9583. ["OVERVIEW" org-overview t]
  9584. ["Refresh columns display" org-columns-redo t]
  9585. "--"
  9586. ["Open link" org-columns-open-link t]
  9587. "--"
  9588. ["Quit" org-columns-quit t]))
  9589. (defun org-columns-new-overlay (beg end &optional string face)
  9590. "Create a new column overlay and add it to the list."
  9591. (let ((ov (org-make-overlay beg end)))
  9592. (org-overlay-put ov 'face (or face 'secondary-selection))
  9593. (org-overlay-display ov string face)
  9594. (push ov org-columns-overlays)
  9595. ov))
  9596. (defun org-columns-display-here (&optional props)
  9597. "Overlay the current line with column display."
  9598. (interactive)
  9599. (let* ((fmt org-columns-current-fmt-compiled)
  9600. (beg (point-at-bol))
  9601. (level-face (save-excursion
  9602. (beginning-of-line 1)
  9603. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  9604. (org-get-level-face 2))))
  9605. (color (list :foreground
  9606. (face-attribute (or level-face 'default) :foreground)))
  9607. props pom property ass width f string ov column val modval)
  9608. ;; Check if the entry is in another buffer.
  9609. (unless props
  9610. (if (eq major-mode 'org-agenda-mode)
  9611. (setq pom (or (get-text-property (point) 'org-hd-marker)
  9612. (get-text-property (point) 'org-marker))
  9613. props (if pom (org-entry-properties pom) nil))
  9614. (setq props (org-entry-properties nil))))
  9615. ;; Walk the format
  9616. (while (setq column (pop fmt))
  9617. (setq property (car column)
  9618. ass (if (equal property "ITEM")
  9619. (cons "ITEM"
  9620. (save-match-data
  9621. (org-no-properties
  9622. (org-remove-tabs
  9623. (buffer-substring-no-properties
  9624. (point-at-bol) (point-at-eol))))))
  9625. (assoc property props))
  9626. width (or (cdr (assoc property org-columns-current-maxwidths))
  9627. (nth 2 column)
  9628. (length property))
  9629. f (format "%%-%d.%ds | " width width)
  9630. val (or (cdr ass) "")
  9631. modval (if (equal property "ITEM")
  9632. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  9633. string (format f (or modval val)))
  9634. ;; Create the overlay
  9635. (org-unmodified
  9636. (setq ov (org-columns-new-overlay
  9637. beg (setq beg (1+ beg)) string
  9638. (list color 'org-column)))
  9639. (org-overlay-put ov 'keymap org-columns-map)
  9640. (org-overlay-put ov 'org-columns-key property)
  9641. (org-overlay-put ov 'org-columns-value (cdr ass))
  9642. (org-overlay-put ov 'org-columns-value-modified modval)
  9643. (org-overlay-put ov 'org-columns-pom pom)
  9644. (org-overlay-put ov 'org-columns-format f))
  9645. (if (or (not (char-after beg))
  9646. (equal (char-after beg) ?\n))
  9647. (let ((inhibit-read-only t))
  9648. (save-excursion
  9649. (goto-char beg)
  9650. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  9651. ;; Make the rest of the line disappear.
  9652. (org-unmodified
  9653. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  9654. (org-overlay-put ov 'invisible t)
  9655. (org-overlay-put ov 'keymap org-columns-map)
  9656. (org-overlay-put ov 'intangible t)
  9657. (push ov org-columns-overlays)
  9658. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  9659. (org-overlay-put ov 'keymap org-columns-map)
  9660. (push ov org-columns-overlays)
  9661. (let ((inhibit-read-only t))
  9662. (put-text-property (max (point-min) (1- (point-at-bol)))
  9663. (min (point-max) (1+ (point-at-eol)))
  9664. 'read-only "Type `e' to edit property")))))
  9665. (defvar org-columns-full-header-line-format nil
  9666. "Fthe full header line format, will be shifted by horizontal scrolling." )
  9667. (defvar org-previous-header-line-format nil
  9668. "The header line format before column view was turned on.")
  9669. (defvar org-columns-inhibit-recalculation nil
  9670. "Inhibit recomputing of columns on column view startup.")
  9671. (defvar header-line-format)
  9672. (defvar org-columns-previous-hscroll 0)
  9673. (defun org-columns-display-here-title ()
  9674. "Overlay the newline before the current line with the table title."
  9675. (interactive)
  9676. (let ((fmt org-columns-current-fmt-compiled)
  9677. string (title "")
  9678. property width f column str widths)
  9679. (while (setq column (pop fmt))
  9680. (setq property (car column)
  9681. str (or (nth 1 column) property)
  9682. width (or (cdr (assoc property org-columns-current-maxwidths))
  9683. (nth 2 column)
  9684. (length str))
  9685. widths (push width widths)
  9686. f (format "%%-%d.%ds | " width width)
  9687. string (format f str)
  9688. title (concat title string)))
  9689. (setq title (concat
  9690. (org-add-props " " nil 'display '(space :align-to 0))
  9691. (org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
  9692. (org-set-local 'org-previous-header-line-format header-line-format)
  9693. (org-set-local 'org-columns-current-widths (nreverse widths))
  9694. (setq org-columns-full-header-line-format title)
  9695. (setq org-columns-previous-hscroll -1)
  9696. ; (org-columns-hscoll-title)
  9697. (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
  9698. (defun org-columns-hscoll-title ()
  9699. "Set the header-line-format so that it scrolls along with the table."
  9700. (sit-for .0001) ; need to force a redisplay to update window-hscroll
  9701. (when (not (= (window-hscroll) org-columns-previous-hscroll))
  9702. (setq header-line-format
  9703. (concat (substring org-columns-full-header-line-format 0 1)
  9704. (substring org-columns-full-header-line-format
  9705. (1+ (window-hscroll))))
  9706. org-columns-previous-hscroll (window-hscroll))
  9707. (force-mode-line-update)))
  9708. (defun org-columns-remove-overlays ()
  9709. "Remove all currently active column overlays."
  9710. (interactive)
  9711. (when (marker-buffer org-columns-begin-marker)
  9712. (with-current-buffer (marker-buffer org-columns-begin-marker)
  9713. (when (local-variable-p 'org-previous-header-line-format)
  9714. (setq header-line-format org-previous-header-line-format)
  9715. (kill-local-variable 'org-previous-header-line-format)
  9716. (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
  9717. (move-marker org-columns-begin-marker nil)
  9718. (move-marker org-columns-top-level-marker nil)
  9719. (org-unmodified
  9720. (mapc 'org-delete-overlay org-columns-overlays)
  9721. (setq org-columns-overlays nil)
  9722. (let ((inhibit-read-only t))
  9723. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  9724. (defun org-columns-cleanup-item (item fmt)
  9725. "Remove from ITEM what is a column in the format FMT."
  9726. (if (not org-complex-heading-regexp)
  9727. item
  9728. (when (string-match org-complex-heading-regexp item)
  9729. (concat
  9730. (org-add-props (concat (match-string 1 item) " ") nil
  9731. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  9732. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  9733. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  9734. " " (match-string 4 item)
  9735. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  9736. (defun org-columns-show-value ()
  9737. "Show the full value of the property."
  9738. (interactive)
  9739. (let ((value (get-char-property (point) 'org-columns-value)))
  9740. (message "Value is: %s" (or value ""))))
  9741. (defun org-columns-quit ()
  9742. "Remove the column overlays and in this way exit column editing."
  9743. (interactive)
  9744. (org-unmodified
  9745. (org-columns-remove-overlays)
  9746. (let ((inhibit-read-only t))
  9747. (remove-text-properties (point-min) (point-max) '(read-only t))))
  9748. (when (eq major-mode 'org-agenda-mode)
  9749. (message
  9750. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  9751. (defun org-columns-check-computed ()
  9752. "Check if this column value is computed.
  9753. If yes, throw an error indicating that changing it does not make sense."
  9754. (let ((val (get-char-property (point) 'org-columns-value)))
  9755. (when (and (stringp val)
  9756. (get-char-property 0 'org-computed val))
  9757. (error "This value is computed from the entry's children"))))
  9758. (defun org-columns-todo (&optional arg)
  9759. "Change the TODO state during column view."
  9760. (interactive "P")
  9761. (org-columns-edit-value "TODO"))
  9762. (defun org-columns-set-tags-or-toggle (&optional arg)
  9763. "Toggle checkbox at point, or set tags for current headline."
  9764. (interactive "P")
  9765. (if (string-match "\\`\\[[ xX-]\\]\\'"
  9766. (get-char-property (point) 'org-columns-value))
  9767. (org-columns-next-allowed-value)
  9768. (org-columns-edit-value "TAGS")))
  9769. (defun org-columns-edit-value (&optional key)
  9770. "Edit the value of the property at point in column view.
  9771. Where possible, use the standard interface for changing this line."
  9772. (interactive)
  9773. (org-columns-check-computed)
  9774. (let* ((external-key key)
  9775. (col (current-column))
  9776. (key (or key (get-char-property (point) 'org-columns-key)))
  9777. (value (get-char-property (point) 'org-columns-value))
  9778. (bol (point-at-bol)) (eol (point-at-eol))
  9779. (pom (or (get-text-property bol 'org-hd-marker)
  9780. (point))) ; keep despite of compiler waring
  9781. (line-overlays
  9782. (delq nil (mapcar (lambda (x)
  9783. (and (eq (overlay-buffer x) (current-buffer))
  9784. (>= (overlay-start x) bol)
  9785. (<= (overlay-start x) eol)
  9786. x))
  9787. org-columns-overlays)))
  9788. nval eval allowed)
  9789. (cond
  9790. ((equal key "CLOCKSUM")
  9791. (error "This special column cannot be edited"))
  9792. ((equal key "ITEM")
  9793. (setq eval '(org-with-point-at pom
  9794. (org-edit-headline))))
  9795. ((equal key "TODO")
  9796. (setq eval '(org-with-point-at pom
  9797. (let ((current-prefix-arg
  9798. (if external-key current-prefix-arg '(4))))
  9799. (call-interactively 'org-todo)))))
  9800. ((equal key "PRIORITY")
  9801. (setq eval '(org-with-point-at pom
  9802. (call-interactively 'org-priority))))
  9803. ((equal key "TAGS")
  9804. (setq eval '(org-with-point-at pom
  9805. (let ((org-fast-tag-selection-single-key
  9806. (if (eq org-fast-tag-selection-single-key 'expert)
  9807. t org-fast-tag-selection-single-key)))
  9808. (call-interactively 'org-set-tags)))))
  9809. ((equal key "DEADLINE")
  9810. (setq eval '(org-with-point-at pom
  9811. (call-interactively 'org-deadline))))
  9812. ((equal key "SCHEDULED")
  9813. (setq eval '(org-with-point-at pom
  9814. (call-interactively 'org-schedule))))
  9815. (t
  9816. (setq allowed (org-property-get-allowed-values pom key 'table))
  9817. (if allowed
  9818. (setq nval (completing-read "Value: " allowed nil t))
  9819. (setq nval (read-string "Edit: " value)))
  9820. (setq nval (org-trim nval))
  9821. (when (not (equal nval value))
  9822. (setq eval '(org-entry-put pom key nval)))))
  9823. (when eval
  9824. (let ((inhibit-read-only t))
  9825. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  9826. (unwind-protect
  9827. (progn
  9828. (setq org-columns-overlays
  9829. (org-delete-all line-overlays org-columns-overlays))
  9830. (mapc 'org-delete-overlay line-overlays)
  9831. (org-columns-eval eval))
  9832. (org-columns-display-here))))
  9833. (move-to-column col)
  9834. (if (and (org-mode-p)
  9835. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9836. (org-columns-update key))))
  9837. (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
  9838. "Edit the current headline, the part without TODO keyword, TAGS."
  9839. (org-back-to-heading)
  9840. (when (looking-at org-todo-line-regexp)
  9841. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  9842. (txt (match-string 3))
  9843. (post "")
  9844. txt2)
  9845. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  9846. (setq post (match-string 0 txt)
  9847. txt (substring txt 0 (match-beginning 0))))
  9848. (setq txt2 (read-string "Edit: " txt))
  9849. (when (not (equal txt txt2))
  9850. (beginning-of-line 1)
  9851. (insert pre txt2 post)
  9852. (delete-region (point) (point-at-eol))
  9853. (org-set-tags nil t)))))
  9854. (defun org-columns-edit-allowed ()
  9855. "Edit the list of allowed values for the current property."
  9856. (interactive)
  9857. (let* ((key (get-char-property (point) 'org-columns-key))
  9858. (key1 (concat key "_ALL"))
  9859. (allowed (org-entry-get (point) key1 t))
  9860. nval)
  9861. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  9862. (setq nval (read-string "Allowed: " allowed))
  9863. (org-entry-put
  9864. (cond ((marker-position org-entry-property-inherited-from)
  9865. org-entry-property-inherited-from)
  9866. ((marker-position org-columns-top-level-marker)
  9867. org-columns-top-level-marker))
  9868. key1 nval)))
  9869. (defun org-columns-eval (form)
  9870. (let (hidep)
  9871. (save-excursion
  9872. (beginning-of-line 1)
  9873. ;; `next-line' is needed here, because it skips invisible line.
  9874. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  9875. (setq hidep (org-on-heading-p 1)))
  9876. (eval form)
  9877. (and hidep (hide-entry))))
  9878. (defun org-columns-previous-allowed-value ()
  9879. "Switch to the previous allowed value for this column."
  9880. (interactive)
  9881. (org-columns-next-allowed-value t))
  9882. (defun org-columns-next-allowed-value (&optional previous)
  9883. "Switch to the next allowed value for this column."
  9884. (interactive)
  9885. (org-columns-check-computed)
  9886. (let* ((col (current-column))
  9887. (key (get-char-property (point) 'org-columns-key))
  9888. (value (get-char-property (point) 'org-columns-value))
  9889. (bol (point-at-bol)) (eol (point-at-eol))
  9890. (pom (or (get-text-property bol 'org-hd-marker)
  9891. (point))) ; keep despite of compiler waring
  9892. (line-overlays
  9893. (delq nil (mapcar (lambda (x)
  9894. (and (eq (overlay-buffer x) (current-buffer))
  9895. (>= (overlay-start x) bol)
  9896. (<= (overlay-start x) eol)
  9897. x))
  9898. org-columns-overlays)))
  9899. (allowed (or (org-property-get-allowed-values pom key)
  9900. (and (memq
  9901. (nth 4 (assoc key org-columns-current-fmt-compiled))
  9902. '(checkbox checkbox-n-of-m checkbox-percent))
  9903. '("[ ]" "[X]"))))
  9904. nval)
  9905. (when (equal key "ITEM")
  9906. (error "Cannot edit item headline from here"))
  9907. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  9908. (error "Allowed values for this property have not been defined"))
  9909. (if (member key '("SCHEDULED" "DEADLINE"))
  9910. (setq nval (if previous 'earlier 'later))
  9911. (if previous (setq allowed (reverse allowed)))
  9912. (if (member value allowed)
  9913. (setq nval (car (cdr (member value allowed)))))
  9914. (setq nval (or nval (car allowed)))
  9915. (if (equal nval value)
  9916. (error "Only one allowed value for this property")))
  9917. (let ((inhibit-read-only t))
  9918. (remove-text-properties (1- bol) eol '(read-only t))
  9919. (unwind-protect
  9920. (progn
  9921. (setq org-columns-overlays
  9922. (org-delete-all line-overlays org-columns-overlays))
  9923. (mapc 'org-delete-overlay line-overlays)
  9924. (org-columns-eval '(org-entry-put pom key nval)))
  9925. (org-columns-display-here)))
  9926. (move-to-column col)
  9927. (if (and (org-mode-p)
  9928. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9929. (org-columns-update key))))
  9930. (defun org-verify-version (task)
  9931. (cond
  9932. ((eq task 'columns)
  9933. (if (or (featurep 'xemacs)
  9934. (< emacs-major-version 22))
  9935. (error "Emacs 22 is required for the columns feature")))))
  9936. (defun org-columns-open-link (&optional arg)
  9937. (interactive "P")
  9938. (let ((value (get-char-property (point) 'org-columns-value)))
  9939. (org-open-link-from-string value arg)))
  9940. (defun org-open-link-from-string (s &optional arg)
  9941. "Open a link in the string S, as if it was in Org-mode."
  9942. (interactive)
  9943. (with-temp-buffer
  9944. (let ((org-inhibit-startup t))
  9945. (org-mode)
  9946. (insert s)
  9947. (goto-char (point-min))
  9948. (org-open-at-point arg))))
  9949. (defun org-columns-get-format-and-top-level ()
  9950. (let (fmt)
  9951. (when (condition-case nil (org-back-to-heading) (error nil))
  9952. (move-marker org-entry-property-inherited-from nil)
  9953. (setq fmt (org-entry-get nil "COLUMNS" t)))
  9954. (setq fmt (or fmt org-columns-default-format))
  9955. (org-set-local 'org-columns-current-fmt fmt)
  9956. (org-columns-compile-format fmt)
  9957. (if (marker-position org-entry-property-inherited-from)
  9958. (move-marker org-columns-top-level-marker
  9959. org-entry-property-inherited-from)
  9960. (move-marker org-columns-top-level-marker (point)))
  9961. fmt))
  9962. (defun org-columns ()
  9963. "Turn on column view on an org-mode file."
  9964. (interactive)
  9965. (org-verify-version 'columns)
  9966. (org-columns-remove-overlays)
  9967. (move-marker org-columns-begin-marker (point))
  9968. (let (beg end fmt cache maxwidths)
  9969. (setq fmt (org-columns-get-format-and-top-level))
  9970. (save-excursion
  9971. (goto-char org-columns-top-level-marker)
  9972. (setq beg (point))
  9973. (unless org-columns-inhibit-recalculation
  9974. (org-columns-compute-all))
  9975. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  9976. (point-max)))
  9977. ;; Get and cache the properties
  9978. (goto-char beg)
  9979. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  9980. (save-excursion
  9981. (save-restriction
  9982. (narrow-to-region beg end)
  9983. (org-clock-sum))))
  9984. (while (re-search-forward (concat "^" outline-regexp) end t)
  9985. (push (cons (org-current-line) (org-entry-properties)) cache))
  9986. (when cache
  9987. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  9988. (org-set-local 'org-columns-current-maxwidths maxwidths)
  9989. (org-columns-display-here-title)
  9990. (mapc (lambda (x)
  9991. (goto-line (car x))
  9992. (org-columns-display-here (cdr x)))
  9993. cache)))))
  9994. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  9995. "Insert a new column, to the left of the current column."
  9996. (interactive)
  9997. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  9998. cell)
  9999. (setq prop (completing-read
  10000. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  10001. nil nil prop))
  10002. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  10003. (setq width (read-string "Column width: " (if width (number-to-string width))))
  10004. (if (string-match "\\S-" width)
  10005. (setq width (string-to-number width))
  10006. (setq width nil))
  10007. (setq fmt (completing-read "Summary [none]: "
  10008. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  10009. nil t))
  10010. (if (string-match "\\S-" fmt)
  10011. (setq fmt (intern fmt))
  10012. (setq fmt nil))
  10013. (if (eq fmt 'none) (setq fmt nil))
  10014. (if editp
  10015. (progn
  10016. (setcar editp prop)
  10017. (setcdr editp (list title width nil fmt)))
  10018. (setq cell (nthcdr (1- (current-column))
  10019. org-columns-current-fmt-compiled))
  10020. (setcdr cell (cons (list prop title width nil fmt)
  10021. (cdr cell))))
  10022. (org-columns-store-format)
  10023. (org-columns-redo)))
  10024. (defun org-columns-delete ()
  10025. "Delete the column at point from columns view."
  10026. (interactive)
  10027. (let* ((n (current-column))
  10028. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  10029. (when (y-or-n-p
  10030. (format "Are you sure you want to remove column \"%s\"? " title))
  10031. (setq org-columns-current-fmt-compiled
  10032. (delq (nth n org-columns-current-fmt-compiled)
  10033. org-columns-current-fmt-compiled))
  10034. (org-columns-store-format)
  10035. (org-columns-redo)
  10036. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  10037. (backward-char 1)))))
  10038. (defun org-columns-edit-attributes ()
  10039. "Edit the attributes of the current column."
  10040. (interactive)
  10041. (let* ((n (current-column))
  10042. (info (nth n org-columns-current-fmt-compiled)))
  10043. (apply 'org-columns-new info)))
  10044. (defun org-columns-widen (arg)
  10045. "Make the column wider by ARG characters."
  10046. (interactive "p")
  10047. (let* ((n (current-column))
  10048. (entry (nth n org-columns-current-fmt-compiled))
  10049. (width (or (nth 2 entry)
  10050. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  10051. (setq width (max 1 (+ width arg)))
  10052. (setcar (nthcdr 2 entry) width)
  10053. (org-columns-store-format)
  10054. (org-columns-redo)))
  10055. (defun org-columns-narrow (arg)
  10056. "Make the column nrrower by ARG characters."
  10057. (interactive "p")
  10058. (org-columns-widen (- arg)))
  10059. (defun org-columns-move-right ()
  10060. "Swap this column with the one to the right."
  10061. (interactive)
  10062. (let* ((n (current-column))
  10063. (cell (nthcdr n org-columns-current-fmt-compiled))
  10064. e)
  10065. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  10066. (error "Cannot shift this column further to the right"))
  10067. (setq e (car cell))
  10068. (setcar cell (car (cdr cell)))
  10069. (setcdr cell (cons e (cdr (cdr cell))))
  10070. (org-columns-store-format)
  10071. (org-columns-redo)
  10072. (forward-char 1)))
  10073. (defun org-columns-move-left ()
  10074. "Swap this column with the one to the left."
  10075. (interactive)
  10076. (let* ((n (current-column)))
  10077. (when (= n 0)
  10078. (error "Cannot shift this column further to the left"))
  10079. (backward-char 1)
  10080. (org-columns-move-right)
  10081. (backward-char 1)))
  10082. (defun org-columns-store-format ()
  10083. "Store the text version of the current columns format in appropriate place.
  10084. This is either in the COLUMNS property of the node starting the current column
  10085. display, or in the #+COLUMNS line of the current buffer."
  10086. (let (fmt (cnt 0))
  10087. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  10088. (org-set-local 'org-columns-current-fmt fmt)
  10089. (if (marker-position org-columns-top-level-marker)
  10090. (save-excursion
  10091. (goto-char org-columns-top-level-marker)
  10092. (if (and (org-at-heading-p)
  10093. (org-entry-get nil "COLUMNS"))
  10094. (org-entry-put nil "COLUMNS" fmt)
  10095. (goto-char (point-min))
  10096. ;; Overwrite all #+COLUMNS lines....
  10097. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  10098. (setq cnt (1+ cnt))
  10099. (replace-match (concat "#+COLUMNS: " fmt) t t))
  10100. (unless (> cnt 0)
  10101. (goto-char (point-min))
  10102. (or (org-on-heading-p t) (outline-next-heading))
  10103. (let ((inhibit-read-only t))
  10104. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  10105. (org-set-local 'org-columns-default-format fmt))))))
  10106. (defvar org-overriding-columns-format nil
  10107. "When set, overrides any other definition.")
  10108. (defvar org-agenda-view-columns-initially nil
  10109. "When set, switch to columns view immediately after creating the agenda.")
  10110. (defun org-agenda-columns ()
  10111. "Turn on column view in the agenda."
  10112. (interactive)
  10113. (org-verify-version 'columns)
  10114. (org-columns-remove-overlays)
  10115. (move-marker org-columns-begin-marker (point))
  10116. (let (fmt cache maxwidths m)
  10117. (cond
  10118. ((and (local-variable-p 'org-overriding-columns-format)
  10119. org-overriding-columns-format)
  10120. (setq fmt org-overriding-columns-format))
  10121. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  10122. (setq fmt (or (org-entry-get m "COLUMNS" t)
  10123. (with-current-buffer (marker-buffer m)
  10124. org-columns-default-format))))
  10125. ((and (boundp 'org-columns-current-fmt)
  10126. (local-variable-p 'org-columns-current-fmt)
  10127. org-columns-current-fmt)
  10128. (setq fmt org-columns-current-fmt))
  10129. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  10130. (setq m (get-text-property m 'org-hd-marker))
  10131. (setq fmt (or (org-entry-get m "COLUMNS" t)
  10132. (with-current-buffer (marker-buffer m)
  10133. org-columns-default-format)))))
  10134. (setq fmt (or fmt org-columns-default-format))
  10135. (org-set-local 'org-columns-current-fmt fmt)
  10136. (org-columns-compile-format fmt)
  10137. (save-excursion
  10138. ;; Get and cache the properties
  10139. (goto-char (point-min))
  10140. (while (not (eobp))
  10141. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  10142. (get-text-property (point) 'org-marker)))
  10143. (push (cons (org-current-line) (org-entry-properties m)) cache))
  10144. (beginning-of-line 2))
  10145. (when cache
  10146. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  10147. (org-set-local 'org-columns-current-maxwidths maxwidths)
  10148. (org-columns-display-here-title)
  10149. (mapc (lambda (x)
  10150. (goto-line (car x))
  10151. (org-columns-display-here (cdr x)))
  10152. cache)))))
  10153. (defun org-columns-get-autowidth-alist (s cache)
  10154. "Derive the maximum column widths from the format and the cache."
  10155. (let ((start 0) rtn)
  10156. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  10157. (push (cons (match-string 1 s) 1) rtn)
  10158. (setq start (match-end 0)))
  10159. (mapc (lambda (x)
  10160. (setcdr x (apply 'max
  10161. (mapcar
  10162. (lambda (y)
  10163. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  10164. cache))))
  10165. rtn)
  10166. rtn))
  10167. (defun org-columns-compute-all ()
  10168. "Compute all columns that have operators defined."
  10169. (org-unmodified
  10170. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  10171. (let ((columns org-columns-current-fmt-compiled) col)
  10172. (while (setq col (pop columns))
  10173. (when (nth 3 col)
  10174. (save-excursion
  10175. (org-columns-compute (car col)))))))
  10176. (defun org-columns-update (property)
  10177. "Recompute PROPERTY, and update the columns display for it."
  10178. (org-columns-compute property)
  10179. (let (fmt val pos)
  10180. (save-excursion
  10181. (mapc (lambda (ov)
  10182. (when (equal (org-overlay-get ov 'org-columns-key) property)
  10183. (setq pos (org-overlay-start ov))
  10184. (goto-char pos)
  10185. (when (setq val (cdr (assoc property
  10186. (get-text-property
  10187. (point-at-bol) 'org-summaries))))
  10188. (setq fmt (org-overlay-get ov 'org-columns-format))
  10189. (org-overlay-put ov 'org-columns-value val)
  10190. (org-overlay-put ov 'display (format fmt val)))))
  10191. org-columns-overlays))))
  10192. (defun org-columns-compute (property)
  10193. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  10194. (interactive)
  10195. (let* ((re (concat "^" outline-regexp))
  10196. (lmax 30) ; Does anyone use deeper levels???
  10197. (lsum (make-vector lmax 0))
  10198. (lflag (make-vector lmax nil))
  10199. (level 0)
  10200. (ass (assoc property org-columns-current-fmt-compiled))
  10201. (format (nth 4 ass))
  10202. (printf (nth 5 ass))
  10203. (beg org-columns-top-level-marker)
  10204. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  10205. (save-excursion
  10206. ;; Find the region to compute
  10207. (goto-char beg)
  10208. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  10209. (goto-char end)
  10210. ;; Walk the tree from the back and do the computations
  10211. (while (re-search-backward re beg t)
  10212. (setq sumpos (match-beginning 0)
  10213. last-level level
  10214. level (org-outline-level)
  10215. val (org-entry-get nil property)
  10216. valflag (and val (string-match "\\S-" val)))
  10217. (cond
  10218. ((< level last-level)
  10219. ;; put the sum of lower levels here as a property
  10220. (setq sum (aref lsum last-level) ; current sum
  10221. flag (aref lflag last-level) ; any valid entries from children?
  10222. str (org-column-number-to-string sum format printf)
  10223. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  10224. useval (if flag str1 (if valflag val ""))
  10225. sum-alist (get-text-property sumpos 'org-summaries))
  10226. (if (assoc property sum-alist)
  10227. (setcdr (assoc property sum-alist) useval)
  10228. (push (cons property useval) sum-alist)
  10229. (org-unmodified
  10230. (add-text-properties sumpos (1+ sumpos)
  10231. (list 'org-summaries sum-alist))))
  10232. (when val
  10233. (org-entry-put nil property (if flag str val)))
  10234. ;; add current to current level accumulator
  10235. (when (or flag valflag)
  10236. (aset lsum level (+ (aref lsum level)
  10237. (if flag sum (org-column-string-to-number
  10238. (if flag str val) format))))
  10239. (aset lflag level t))
  10240. ;; clear accumulators for deeper levels
  10241. (loop for l from (1+ level) to (1- lmax) do
  10242. (aset lsum l 0)
  10243. (aset lflag l nil)))
  10244. ((>= level last-level)
  10245. ;; add what we have here to the accumulator for this level
  10246. (aset lsum level (+ (aref lsum level)
  10247. (org-column-string-to-number (or val "0") format)))
  10248. (and valflag (aset lflag level t)))
  10249. (t (error "This should not happen")))))))
  10250. (defun org-columns-redo ()
  10251. "Construct the column display again."
  10252. (interactive)
  10253. (message "Recomputing columns...")
  10254. (save-excursion
  10255. (if (marker-position org-columns-begin-marker)
  10256. (goto-char org-columns-begin-marker))
  10257. (org-columns-remove-overlays)
  10258. (if (org-mode-p)
  10259. (call-interactively 'org-columns)
  10260. (call-interactively 'org-agenda-columns)))
  10261. (message "Recomputing columns...done"))
  10262. (defun org-columns-not-in-agenda ()
  10263. (if (eq major-mode 'org-agenda-mode)
  10264. (error "This command is only allowed in Org-mode buffers")))
  10265. (defun org-string-to-number (s)
  10266. "Convert string to number, and interpret hh:mm:ss."
  10267. (if (not (string-match ":" s))
  10268. (string-to-number 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. (defun org-column-number-to-string (n fmt &optional printf)
  10274. "Convert a computed column number to a string value, according to FMT."
  10275. (cond
  10276. ((eq fmt 'add_times)
  10277. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  10278. (format "%d:%02d" h m)))
  10279. ((eq fmt 'checkbox)
  10280. (cond ((= n (floor n)) "[X]")
  10281. ((> n 1.) "[-]")
  10282. (t "[ ]")))
  10283. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  10284. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  10285. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  10286. (printf (format printf n))
  10287. ((eq fmt 'currency)
  10288. (format "%.2f" n))
  10289. (t (number-to-string n))))
  10290. (defun org-nofm-to-completion (n m &optional percent)
  10291. (if (not percent)
  10292. (format "[%d/%d]" n m)
  10293. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  10294. (defun org-column-string-to-number (s fmt)
  10295. "Convert a column value to a number that can be used for column computing."
  10296. (cond
  10297. ((string-match ":" s)
  10298. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10299. (while l
  10300. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10301. sum))
  10302. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  10303. (if (equal s "[X]") 1. 0.000001))
  10304. (t (string-to-number s))))
  10305. (defun org-columns-uncompile-format (cfmt)
  10306. "Turn the compiled columns format back into a string representation."
  10307. (let ((rtn "") e s prop title op width fmt printf)
  10308. (while (setq e (pop cfmt))
  10309. (setq prop (car e)
  10310. title (nth 1 e)
  10311. width (nth 2 e)
  10312. op (nth 3 e)
  10313. fmt (nth 4 e)
  10314. printf (nth 5 e))
  10315. (cond
  10316. ((eq fmt 'add_times) (setq op ":"))
  10317. ((eq fmt 'checkbox) (setq op "X"))
  10318. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  10319. ((eq fmt 'checkbox-percent) (setq op "X%"))
  10320. ((eq fmt 'add_numbers) (setq op "+"))
  10321. ((eq fmt 'currency) (setq op "$")))
  10322. (if (and op printf) (setq op (concat op ";" printf)))
  10323. (if (equal title prop) (setq title nil))
  10324. (setq s (concat "%" (if width (number-to-string width))
  10325. prop
  10326. (if title (concat "(" title ")"))
  10327. (if op (concat "{" op "}"))))
  10328. (setq rtn (concat rtn " " s)))
  10329. (org-trim rtn)))
  10330. (defun org-columns-compile-format (fmt)
  10331. "Turn a column format string into an alist of specifications.
  10332. The alist has one entry for each column in the format. The elements of
  10333. that list are:
  10334. property the property
  10335. title the title field for the columns
  10336. width the column width in characters, can be nil for automatic
  10337. operator the operator if any
  10338. format the output format for computed results, derived from operator
  10339. printf a printf format for computed values"
  10340. (let ((start 0) width prop title op f printf)
  10341. (setq org-columns-current-fmt-compiled nil)
  10342. (while (string-match
  10343. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  10344. fmt start)
  10345. (setq start (match-end 0)
  10346. width (match-string 1 fmt)
  10347. prop (match-string 2 fmt)
  10348. title (or (match-string 3 fmt) prop)
  10349. op (match-string 4 fmt)
  10350. f nil
  10351. printf nil)
  10352. (if width (setq width (string-to-number width)))
  10353. (when (and op (string-match ";" op))
  10354. (setq printf (substring op (match-end 0))
  10355. op (substring op 0 (match-beginning 0))))
  10356. (cond
  10357. ((equal op "+") (setq f 'add_numbers))
  10358. ((equal op "$") (setq f 'currency))
  10359. ((equal op ":") (setq f 'add_times))
  10360. ((equal op "X") (setq f 'checkbox))
  10361. ((equal op "X/") (setq f 'checkbox-n-of-m))
  10362. ((equal op "X%") (setq f 'checkbox-percent))
  10363. )
  10364. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  10365. (setq org-columns-current-fmt-compiled
  10366. (nreverse org-columns-current-fmt-compiled))))
  10367. ;;; Dynamic block for Column view
  10368. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  10369. "Get the column view of the current buffer or subtree.
  10370. The first optional argument MAXLEVEL sets the level limit. A
  10371. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  10372. empty rows, an empty row being one where all the column view
  10373. specifiers except ITEM are empty. This function returns a list
  10374. containing the title row and all other rows. Each row is a list
  10375. of fields."
  10376. (save-excursion
  10377. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  10378. (n (length title)) row tbl)
  10379. (goto-char (point-min))
  10380. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  10381. (or (null maxlevel)
  10382. (>= maxlevel
  10383. (if org-odd-levels-only
  10384. (/ (1+ (length (match-string 1))) 2)
  10385. (length (match-string 1))))))
  10386. (when (get-char-property (match-beginning 0) 'org-columns-key)
  10387. (setq row nil)
  10388. (loop for i from 0 to (1- n) do
  10389. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  10390. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  10391. "")
  10392. row))
  10393. (setq row (nreverse row))
  10394. (unless (and skip-empty-rows
  10395. (eq 1 (length (delete "" (delete-dups row)))))
  10396. (push row tbl))))
  10397. (append (list title 'hline) (nreverse tbl)))))
  10398. (defun org-dblock-write:columnview (params)
  10399. "Write the column view table.
  10400. PARAMS is a property list of parameters:
  10401. :width enforce same column widths with <N> specifiers.
  10402. :id the :ID: property of the entry where the columns view
  10403. should be built, as a string. When `local', call locally.
  10404. When `global' call column view with the cursor at the beginning
  10405. of the buffer (usually this means that the whole buffer switches
  10406. to column view).
  10407. :hlines When t, insert a hline before each item. When a number, insert
  10408. a hline before each level <= that number.
  10409. :vlines When t, make each column a colgroup to enforce vertical lines.
  10410. :maxlevel When set to a number, don't capture headlines below this level.
  10411. :skip-empty-rows
  10412. When t, skip rows where all specifiers other than ITEM are empty."
  10413. (let ((pos (move-marker (make-marker) (point)))
  10414. (hlines (plist-get params :hlines))
  10415. (vlines (plist-get params :vlines))
  10416. (maxlevel (plist-get params :maxlevel))
  10417. (skip-empty-rows (plist-get params :skip-empty-rows))
  10418. tbl id idpos nfields tmp)
  10419. (save-excursion
  10420. (save-restriction
  10421. (when (setq id (plist-get params :id))
  10422. (cond ((not id) nil)
  10423. ((eq id 'global) (goto-char (point-min)))
  10424. ((eq id 'local) nil)
  10425. ((setq idpos (org-find-entry-with-id id))
  10426. (goto-char idpos))
  10427. (t (error "Cannot find entry with :ID: %s" id))))
  10428. (org-columns)
  10429. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  10430. (setq nfields (length (car tbl)))
  10431. (org-columns-quit)))
  10432. (goto-char pos)
  10433. (move-marker pos nil)
  10434. (when tbl
  10435. (when (plist-get params :hlines)
  10436. (setq tmp nil)
  10437. (while tbl
  10438. (if (eq (car tbl) 'hline)
  10439. (push (pop tbl) tmp)
  10440. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  10441. (if (and (not (eq (car tmp) 'hline))
  10442. (or (eq hlines t)
  10443. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  10444. (push 'hline tmp)))
  10445. (push (pop tbl) tmp)))
  10446. (setq tbl (nreverse tmp)))
  10447. (when vlines
  10448. (setq tbl (mapcar (lambda (x)
  10449. (if (eq 'hline x) x (cons "" x)))
  10450. tbl))
  10451. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  10452. (setq pos (point))
  10453. (insert (org-listtable-to-string tbl))
  10454. (when (plist-get params :width)
  10455. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  10456. org-columns-current-widths "|")))
  10457. (goto-char pos)
  10458. (org-table-align))))
  10459. (defun org-listtable-to-string (tbl)
  10460. "Convert a listtable TBL to a string that contains the Org-mode table.
  10461. The table still need to be alligned. The resulting string has no leading
  10462. and tailing newline characters."
  10463. (mapconcat
  10464. (lambda (x)
  10465. (cond
  10466. ((listp x)
  10467. (concat "|" (mapconcat 'identity x "|") "|"))
  10468. ((eq x 'hline) "|-|")
  10469. (t (error "Garbage in listtable: %s" x))))
  10470. tbl "\n"))
  10471. (defun org-insert-columns-dblock ()
  10472. "Create a dynamic block capturing a column view table."
  10473. (interactive)
  10474. (let ((defaults '(:name "columnview" :hlines 1))
  10475. (id (completing-read
  10476. "Capture columns (local, global, entry with :ID: property) [local]: "
  10477. (append '(("global") ("local"))
  10478. (mapcar 'list (org-property-values "ID"))))))
  10479. (if (equal id "") (setq id 'local))
  10480. (if (equal id "global") (setq id 'global))
  10481. (setq defaults (append defaults (list :id id)))
  10482. (org-create-dblock defaults)
  10483. (org-update-dblock)))
  10484. ;;;; Timestamps
  10485. (defvar org-last-changed-timestamp nil)
  10486. (defvar org-time-was-given) ; dynamically scoped parameter
  10487. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10488. (defvar org-ts-what) ; dynamically scoped parameter
  10489. (defun org-time-stamp (arg)
  10490. "Prompt for a date/time and insert a time stamp.
  10491. If the user specifies a time like HH:MM, or if this command is called
  10492. with a prefix argument, the time stamp will contain date and time.
  10493. Otherwise, only the date will be included. All parts of a date not
  10494. specified by the user will be filled in from the current date/time.
  10495. So if you press just return without typing anything, the time stamp
  10496. will represent the current date/time. If there is already a timestamp
  10497. at the cursor, it will be modified."
  10498. (interactive "P")
  10499. (let* ((ts nil)
  10500. (default-time
  10501. ;; Default time is either today, or, when entering a range,
  10502. ;; the range start.
  10503. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10504. (save-excursion
  10505. (re-search-backward
  10506. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10507. (- (point) 20) t)))
  10508. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10509. (current-time)))
  10510. (default-input (and ts (org-get-compact-tod ts)))
  10511. org-time-was-given org-end-time-was-given time)
  10512. (cond
  10513. ((and (org-at-timestamp-p)
  10514. (eq last-command 'org-time-stamp)
  10515. (eq this-command 'org-time-stamp))
  10516. (insert "--")
  10517. (setq time (let ((this-command this-command))
  10518. (org-read-date arg 'totime nil nil default-time default-input)))
  10519. (org-insert-time-stamp time (or org-time-was-given arg)))
  10520. ((org-at-timestamp-p)
  10521. (setq time (let ((this-command this-command))
  10522. (org-read-date arg 'totime nil nil default-time default-input)))
  10523. (when (org-at-timestamp-p) ; just to get the match data
  10524. (replace-match "")
  10525. (setq org-last-changed-timestamp
  10526. (org-insert-time-stamp
  10527. time (or org-time-was-given arg)
  10528. nil nil nil (list org-end-time-was-given))))
  10529. (message "Timestamp updated"))
  10530. (t
  10531. (setq time (let ((this-command this-command))
  10532. (org-read-date arg 'totime nil nil default-time default-input)))
  10533. (org-insert-time-stamp time (or org-time-was-given arg)
  10534. nil nil nil (list org-end-time-was-given))))))
  10535. ;; FIXME: can we use this for something else, like computing time differences?
  10536. (defun org-get-compact-tod (s)
  10537. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10538. (let* ((t1 (match-string 1 s))
  10539. (h1 (string-to-number (match-string 2 s)))
  10540. (m1 (string-to-number (match-string 3 s)))
  10541. (t2 (and (match-end 4) (match-string 5 s)))
  10542. (h2 (and t2 (string-to-number (match-string 6 s))))
  10543. (m2 (and t2 (string-to-number (match-string 7 s))))
  10544. dh dm)
  10545. (if (not t2)
  10546. t1
  10547. (setq dh (- h2 h1) dm (- m2 m1))
  10548. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10549. (concat t1 "+" (number-to-string dh)
  10550. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10551. (defun org-time-stamp-inactive (&optional arg)
  10552. "Insert an inactive time stamp.
  10553. An inactive time stamp is enclosed in square brackets instead of angle
  10554. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10555. does not link to the calendar and cannot be changed with the S-cursor keys.
  10556. So these are more for recording a certain time/date."
  10557. (interactive "P")
  10558. (let (org-time-was-given org-end-time-was-given time)
  10559. (setq time (org-read-date arg 'totime))
  10560. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  10561. nil nil (list org-end-time-was-given))))
  10562. (defvar org-date-ovl (org-make-overlay 1 1))
  10563. (org-overlay-put org-date-ovl 'face 'org-warning)
  10564. (org-detach-overlay org-date-ovl)
  10565. (defvar org-ans1) ; dynamically scoped parameter
  10566. (defvar org-ans2) ; dynamically scoped parameter
  10567. (defvar org-plain-time-of-day-regexp) ; defined below
  10568. (defvar org-read-date-overlay nil)
  10569. (defvar org-dcst nil) ; dynamically scoped
  10570. (defun org-read-date (&optional with-time to-time from-string prompt
  10571. default-time default-input)
  10572. "Read a date, possibly a time, and make things smooth for the user.
  10573. The prompt will suggest to enter an ISO date, but you can also enter anything
  10574. which will at least partially be understood by `parse-time-string'.
  10575. Unrecognized parts of the date will default to the current day, month, year,
  10576. hour and minute. If this command is called to replace a timestamp at point,
  10577. of to enter the second timestamp of a range, the default time is taken from the
  10578. existing stamp. For example,
  10579. 3-2-5 --> 2003-02-05
  10580. feb 15 --> currentyear-02-15
  10581. sep 12 9 --> 2009-09-12
  10582. 12:45 --> today 12:45
  10583. 22 sept 0:34 --> currentyear-09-22 0:34
  10584. 12 --> currentyear-currentmonth-12
  10585. Fri --> nearest Friday (today or later)
  10586. etc.
  10587. Furthermore you can specify a relative date by giving, as the *first* thing
  10588. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10589. change in days weeks, months, years.
  10590. With a single plus or minus, the date is relative to today. With a double
  10591. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10592. +4d --> four days from today
  10593. +4 --> same as above
  10594. +2w --> two weeks from today
  10595. ++5 --> five days from default date
  10596. The function understands only English month and weekday abbreviations,
  10597. but this can be configured with the variables `parse-time-months' and
  10598. `parse-time-weekdays'.
  10599. While prompting, a calendar is popped up - you can also select the
  10600. date with the mouse (button 1). The calendar shows a period of three
  10601. months. To scroll it to other months, use the keys `>' and `<'.
  10602. If you don't like the calendar, turn it off with
  10603. \(setq org-read-date-popup-calendar nil)
  10604. With optional argument TO-TIME, the date will immediately be converted
  10605. to an internal time.
  10606. With an optional argument WITH-TIME, the prompt will suggest to also
  10607. insert a time. Note that when WITH-TIME is not set, you can still
  10608. enter a time, and this function will inform the calling routine about
  10609. this change. The calling routine may then choose to change the format
  10610. used to insert the time stamp into the buffer to include the time.
  10611. With optional argument FROM-STRING, read from this string instead from
  10612. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10613. the time/date that is used for everything that is not specified by the
  10614. user."
  10615. (require 'parse-time)
  10616. (let* ((org-time-stamp-rounding-minutes
  10617. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10618. (org-dcst org-display-custom-times)
  10619. (ct (org-current-time))
  10620. (def (or default-time ct))
  10621. (defdecode (decode-time def))
  10622. (dummy (progn
  10623. (when (< (nth 2 defdecode) org-extend-today-until)
  10624. (setcar (nthcdr 2 defdecode) -1)
  10625. (setcar (nthcdr 1 defdecode) 59)
  10626. (setq def (apply 'encode-time defdecode)
  10627. defdecode (decode-time def)))))
  10628. (calendar-move-hook nil)
  10629. (view-diary-entries-initially nil)
  10630. (view-calendar-holidays-initially nil)
  10631. (timestr (format-time-string
  10632. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10633. (prompt (concat (if prompt (concat prompt " ") "")
  10634. (format "Date+time [%s]: " timestr)))
  10635. ans (org-ans0 "") org-ans1 org-ans2 final)
  10636. (cond
  10637. (from-string (setq ans from-string))
  10638. (org-read-date-popup-calendar
  10639. (save-excursion
  10640. (save-window-excursion
  10641. (calendar)
  10642. (calendar-forward-day (- (time-to-days def)
  10643. (calendar-absolute-from-gregorian
  10644. (calendar-current-date))))
  10645. (org-eval-in-calendar nil t)
  10646. (let* ((old-map (current-local-map))
  10647. (map (copy-keymap calendar-mode-map))
  10648. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10649. (org-defkey map (kbd "RET") 'org-calendar-select)
  10650. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10651. 'org-calendar-select-mouse)
  10652. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10653. 'org-calendar-select-mouse)
  10654. (org-defkey minibuffer-local-map [(meta shift left)]
  10655. (lambda () (interactive)
  10656. (org-eval-in-calendar '(calendar-backward-month 1))))
  10657. (org-defkey minibuffer-local-map [(meta shift right)]
  10658. (lambda () (interactive)
  10659. (org-eval-in-calendar '(calendar-forward-month 1))))
  10660. (org-defkey minibuffer-local-map [(meta shift up)]
  10661. (lambda () (interactive)
  10662. (org-eval-in-calendar '(calendar-backward-year 1))))
  10663. (org-defkey minibuffer-local-map [(meta shift down)]
  10664. (lambda () (interactive)
  10665. (org-eval-in-calendar '(calendar-forward-year 1))))
  10666. (org-defkey minibuffer-local-map [(shift up)]
  10667. (lambda () (interactive)
  10668. (org-eval-in-calendar '(calendar-backward-week 1))))
  10669. (org-defkey minibuffer-local-map [(shift down)]
  10670. (lambda () (interactive)
  10671. (org-eval-in-calendar '(calendar-forward-week 1))))
  10672. (org-defkey minibuffer-local-map [(shift left)]
  10673. (lambda () (interactive)
  10674. (org-eval-in-calendar '(calendar-backward-day 1))))
  10675. (org-defkey minibuffer-local-map [(shift right)]
  10676. (lambda () (interactive)
  10677. (org-eval-in-calendar '(calendar-forward-day 1))))
  10678. (org-defkey minibuffer-local-map ">"
  10679. (lambda () (interactive)
  10680. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10681. (org-defkey minibuffer-local-map "<"
  10682. (lambda () (interactive)
  10683. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10684. (unwind-protect
  10685. (progn
  10686. (use-local-map map)
  10687. (add-hook 'post-command-hook 'org-read-date-display)
  10688. (setq org-ans0 (read-string prompt default-input nil nil))
  10689. ;; org-ans0: from prompt
  10690. ;; org-ans1: from mouse click
  10691. ;; org-ans2: from calendar motion
  10692. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10693. (remove-hook 'post-command-hook 'org-read-date-display)
  10694. (use-local-map old-map)
  10695. (when org-read-date-overlay
  10696. (org-delete-overlay org-read-date-overlay)
  10697. (setq org-read-date-overlay nil)))))))
  10698. (t ; Naked prompt only
  10699. (unwind-protect
  10700. (setq ans (read-string prompt default-input nil timestr))
  10701. (when org-read-date-overlay
  10702. (org-delete-overlay org-read-date-overlay)
  10703. (setq org-read-date-overlay nil)))))
  10704. (setq final (org-read-date-analyze ans def defdecode))
  10705. (if to-time
  10706. (apply 'encode-time final)
  10707. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10708. (format "%04d-%02d-%02d %02d:%02d"
  10709. (nth 5 final) (nth 4 final) (nth 3 final)
  10710. (nth 2 final) (nth 1 final))
  10711. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10712. (defvar def)
  10713. (defvar defdecode)
  10714. (defvar with-time)
  10715. (defun org-read-date-display ()
  10716. "Display the currrent date prompt interpretation in the minibuffer."
  10717. (when org-read-date-display-live
  10718. (when org-read-date-overlay
  10719. (org-delete-overlay org-read-date-overlay))
  10720. (let ((p (point)))
  10721. (end-of-line 1)
  10722. (while (not (equal (buffer-substring
  10723. (max (point-min) (- (point) 4)) (point))
  10724. " "))
  10725. (insert " "))
  10726. (goto-char p))
  10727. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10728. " " (or org-ans1 org-ans2)))
  10729. (org-end-time-was-given nil)
  10730. (f (org-read-date-analyze ans def defdecode))
  10731. (fmts (if org-dcst
  10732. org-time-stamp-custom-formats
  10733. org-time-stamp-formats))
  10734. (fmt (if (or with-time
  10735. (and (boundp 'org-time-was-given) org-time-was-given))
  10736. (cdr fmts)
  10737. (car fmts)))
  10738. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10739. (when (and org-end-time-was-given
  10740. (string-match org-plain-time-of-day-regexp txt))
  10741. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10742. org-end-time-was-given
  10743. (substring txt (match-end 0)))))
  10744. (setq org-read-date-overlay
  10745. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  10746. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10747. (defun org-read-date-analyze (ans def defdecode)
  10748. "Analyze the combined answer of the date prompt."
  10749. ;; FIXME: cleanup and comment
  10750. (let (delta deltan deltaw deltadef year month day
  10751. hour minute second wday pm h2 m2 tl wday1
  10752. iso-year iso-weekday iso-week iso-year iso-date)
  10753. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10754. (setq ans (replace-match "" t t ans)
  10755. deltan (car delta)
  10756. deltaw (nth 1 delta)
  10757. deltadef (nth 2 delta)))
  10758. ;; Check if there is an iso week date in there
  10759. ;; If yes, sore the info and ostpone interpreting it until the rest
  10760. ;; of the parsing is done
  10761. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10762. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10763. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10764. iso-week (string-to-number (match-string 2 ans)))
  10765. (setq ans (replace-match "" t t ans)))
  10766. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10767. (when (string-match
  10768. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10769. (setq year (if (match-end 2)
  10770. (string-to-number (match-string 2 ans))
  10771. (string-to-number (format-time-string "%Y")))
  10772. month (string-to-number (match-string 3 ans))
  10773. day (string-to-number (match-string 4 ans)))
  10774. (if (< year 100) (setq year (+ 2000 year)))
  10775. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10776. t nil ans)))
  10777. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10778. ;; If there is a time with am/pm, and *no* time without it, we convert
  10779. ;; so that matching will be successful.
  10780. (loop for i from 1 to 2 do ; twice, for end time as well
  10781. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10782. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10783. (setq hour (string-to-number (match-string 1 ans))
  10784. minute (if (match-end 3)
  10785. (string-to-number (match-string 3 ans))
  10786. 0)
  10787. pm (equal ?p
  10788. (string-to-char (downcase (match-string 4 ans)))))
  10789. (if (and (= hour 12) (not pm))
  10790. (setq hour 0)
  10791. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10792. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10793. t t ans))))
  10794. ;; Check if a time range is given as a duration
  10795. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10796. (setq hour (string-to-number (match-string 1 ans))
  10797. h2 (+ hour (string-to-number (match-string 3 ans)))
  10798. minute (string-to-number (match-string 2 ans))
  10799. m2 (+ minute (if (match-end 5) (string-to-number
  10800. (match-string 5 ans))0)))
  10801. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10802. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10803. t t ans)))
  10804. ;; Check if there is a time range
  10805. (when (boundp 'org-end-time-was-given)
  10806. (setq org-time-was-given nil)
  10807. (when (and (string-match org-plain-time-of-day-regexp ans)
  10808. (match-end 8))
  10809. (setq org-end-time-was-given (match-string 8 ans))
  10810. (setq ans (concat (substring ans 0 (match-beginning 7))
  10811. (substring ans (match-end 7))))))
  10812. (setq tl (parse-time-string ans)
  10813. day (or (nth 3 tl) (nth 3 defdecode))
  10814. month (or (nth 4 tl)
  10815. (if (and org-read-date-prefer-future
  10816. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10817. (1+ (nth 4 defdecode))
  10818. (nth 4 defdecode)))
  10819. year (or (nth 5 tl)
  10820. (if (and org-read-date-prefer-future
  10821. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10822. (1+ (nth 5 defdecode))
  10823. (nth 5 defdecode)))
  10824. hour (or (nth 2 tl) (nth 2 defdecode))
  10825. minute (or (nth 1 tl) (nth 1 defdecode))
  10826. second (or (nth 0 tl) 0)
  10827. wday (nth 6 tl))
  10828. ;; Special date definitions below
  10829. (cond
  10830. (iso-week
  10831. ;; There was an iso week
  10832. (setq year (or iso-year year)
  10833. day (or iso-weekday wday 1)
  10834. wday nil ; to make sure that the trigger below does not match
  10835. iso-date (calendar-gregorian-from-absolute
  10836. (calendar-absolute-from-iso
  10837. (list iso-week day year))))
  10838. ; FIXME: Should we also push ISO weeks into the future?
  10839. ; (when (and org-read-date-prefer-future
  10840. ; (not iso-year)
  10841. ; (< (calendar-absolute-from-gregorian iso-date)
  10842. ; (time-to-days (current-time))))
  10843. ; (setq year (1+ year)
  10844. ; iso-date (calendar-gregorian-from-absolute
  10845. ; (calendar-absolute-from-iso
  10846. ; (list iso-week day year)))))
  10847. (setq month (car iso-date)
  10848. year (nth 2 iso-date)
  10849. day (nth 1 iso-date)))
  10850. (deltan
  10851. (unless deltadef
  10852. (let ((now (decode-time (current-time))))
  10853. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10854. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10855. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10856. ((equal deltaw "m") (setq month (+ month deltan)))
  10857. ((equal deltaw "y") (setq year (+ year deltan)))))
  10858. ((and wday (not (nth 3 tl)))
  10859. ;; Weekday was given, but no day, so pick that day in the week
  10860. ;; on or after the derived date.
  10861. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10862. (unless (equal wday wday1)
  10863. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10864. (if (and (boundp 'org-time-was-given)
  10865. (nth 2 tl))
  10866. (setq org-time-was-given t))
  10867. (if (< year 100) (setq year (+ 2000 year)))
  10868. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10869. (list second minute hour day month year)))
  10870. (defvar parse-time-weekdays)
  10871. (defun org-read-date-get-relative (s today default)
  10872. "Check string S for special relative date string.
  10873. TODAY and DEFAULT are internal times, for today and for a default.
  10874. Return shift list (N what def-flag)
  10875. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10876. N is the number of WHATs to shift.
  10877. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10878. the DEFAULT date rather than TODAY."
  10879. (when (string-match
  10880. (concat
  10881. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  10882. "\\([0-9]+\\)?"
  10883. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10884. "\\([ \t]\\|$\\)") s)
  10885. (let* ((dir (if (match-end 1)
  10886. (string-to-char (substring (match-string 1 s) -1))
  10887. ?+))
  10888. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10889. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10890. (what (if (match-end 3) (match-string 3 s) "d"))
  10891. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10892. (date (if rel default today))
  10893. (wday (nth 6 (decode-time date)))
  10894. delta)
  10895. (if wday1
  10896. (progn
  10897. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10898. (if (= dir ?-) (setq delta (- delta 7)))
  10899. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10900. (list delta "d" rel))
  10901. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10902. (defun org-eval-in-calendar (form &optional keepdate)
  10903. "Eval FORM in the calendar window and return to current window.
  10904. Also, store the cursor date in variable org-ans2."
  10905. (let ((sw (selected-window)))
  10906. (select-window (get-buffer-window "*Calendar*"))
  10907. (eval form)
  10908. (when (and (not keepdate) (calendar-cursor-to-date))
  10909. (let* ((date (calendar-cursor-to-date))
  10910. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10911. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10912. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10913. (select-window sw)))
  10914. ; ;; Update the prompt to show new default date
  10915. ; (save-excursion
  10916. ; (goto-char (point-min))
  10917. ; (when (and org-ans2
  10918. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  10919. ; (get-text-property (match-end 0) 'field))
  10920. ; (let ((inhibit-read-only t))
  10921. ; (replace-match (concat "[" org-ans2 "]") t t)
  10922. ; (add-text-properties (point-min) (1+ (match-end 0))
  10923. ; (text-properties-at (1+ (point-min)))))))))
  10924. (defun org-calendar-select ()
  10925. "Return to `org-read-date' with the date currently selected.
  10926. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10927. (interactive)
  10928. (when (calendar-cursor-to-date)
  10929. (let* ((date (calendar-cursor-to-date))
  10930. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10931. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10932. (if (active-minibuffer-window) (exit-minibuffer))))
  10933. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10934. "Insert a date stamp for the date given by the internal TIME.
  10935. WITH-HM means, use the stamp format that includes the time of the day.
  10936. INACTIVE means use square brackets instead of angular ones, so that the
  10937. stamp will not contribute to the agenda.
  10938. PRE and POST are optional strings to be inserted before and after the
  10939. stamp.
  10940. The command returns the inserted time stamp."
  10941. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10942. stamp)
  10943. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10944. (insert-before-markers (or pre ""))
  10945. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10946. (when (listp extra)
  10947. (setq extra (car extra))
  10948. (if (and (stringp extra)
  10949. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10950. (setq extra (format "-%02d:%02d"
  10951. (string-to-number (match-string 1 extra))
  10952. (string-to-number (match-string 2 extra))))
  10953. (setq extra nil)))
  10954. (when extra
  10955. (backward-char 1)
  10956. (insert-before-markers extra)
  10957. (forward-char 1))
  10958. (insert-before-markers (or post ""))
  10959. stamp))
  10960. (defun org-toggle-time-stamp-overlays ()
  10961. "Toggle the use of custom time stamp formats."
  10962. (interactive)
  10963. (setq org-display-custom-times (not org-display-custom-times))
  10964. (unless org-display-custom-times
  10965. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10966. (while (setq p (next-single-property-change p 'display))
  10967. (if (and (get-text-property p 'display)
  10968. (eq (get-text-property p 'face) 'org-date))
  10969. (remove-text-properties
  10970. p (setq p (next-single-property-change p 'display))
  10971. '(display t))))
  10972. (set-buffer-modified-p bmp)))
  10973. (if (featurep 'xemacs)
  10974. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10975. (org-restart-font-lock)
  10976. (setq org-table-may-need-update t)
  10977. (if org-display-custom-times
  10978. (message "Time stamps are overlayed with custom format")
  10979. (message "Time stamp overlays removed")))
  10980. (defun org-display-custom-time (beg end)
  10981. "Overlay modified time stamp format over timestamp between BED and END."
  10982. (let* ((ts (buffer-substring beg end))
  10983. t1 w1 with-hm tf time str w2 (off 0))
  10984. (save-match-data
  10985. (setq t1 (org-parse-time-string ts t))
  10986. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10987. (setq off (- (match-end 0) (match-beginning 0)))))
  10988. (setq end (- end off))
  10989. (setq w1 (- end beg)
  10990. with-hm (and (nth 1 t1) (nth 2 t1))
  10991. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10992. time (org-fix-decoded-time t1)
  10993. str (org-add-props
  10994. (format-time-string
  10995. (substring tf 1 -1) (apply 'encode-time time))
  10996. nil 'mouse-face 'highlight)
  10997. w2 (length str))
  10998. (if (not (= w2 w1))
  10999. (add-text-properties (1+ beg) (+ 2 beg)
  11000. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11001. (if (featurep 'xemacs)
  11002. (progn
  11003. (put-text-property beg end 'invisible t)
  11004. (put-text-property beg end 'end-glyph (make-glyph str)))
  11005. (put-text-property beg end 'display str))))
  11006. (defun org-translate-time (string)
  11007. "Translate all timestamps in STRING to custom format.
  11008. But do this only if the variable `org-display-custom-times' is set."
  11009. (when org-display-custom-times
  11010. (save-match-data
  11011. (let* ((start 0)
  11012. (re org-ts-regexp-both)
  11013. t1 with-hm inactive tf time str beg end)
  11014. (while (setq start (string-match re string start))
  11015. (setq beg (match-beginning 0)
  11016. end (match-end 0)
  11017. t1 (save-match-data
  11018. (org-parse-time-string (substring string beg end) t))
  11019. with-hm (and (nth 1 t1) (nth 2 t1))
  11020. inactive (equal (substring string beg (1+ beg)) "[")
  11021. tf (funcall (if with-hm 'cdr 'car)
  11022. org-time-stamp-custom-formats)
  11023. time (org-fix-decoded-time t1)
  11024. str (format-time-string
  11025. (concat
  11026. (if inactive "[" "<") (substring tf 1 -1)
  11027. (if inactive "]" ">"))
  11028. (apply 'encode-time time))
  11029. string (replace-match str t t string)
  11030. start (+ start (length str)))))))
  11031. string)
  11032. (defun org-fix-decoded-time (time)
  11033. "Set 0 instead of nil for the first 6 elements of time.
  11034. Don't touch the rest."
  11035. (let ((n 0))
  11036. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11037. (defun org-days-to-time (timestamp-string)
  11038. "Difference between TIMESTAMP-STRING and now in days."
  11039. (- (time-to-days (org-time-string-to-time timestamp-string))
  11040. (time-to-days (current-time))))
  11041. (defun org-deadline-close (timestamp-string &optional ndays)
  11042. "Is the time in TIMESTAMP-STRING close to the current date?"
  11043. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11044. (and (< (org-days-to-time timestamp-string) ndays)
  11045. (not (org-entry-is-done-p))))
  11046. (defun org-get-wdays (ts)
  11047. "Get the deadline lead time appropriate for timestring TS."
  11048. (cond
  11049. ((<= org-deadline-warning-days 0)
  11050. ;; 0 or negative, enforce this value no matter what
  11051. (- org-deadline-warning-days))
  11052. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  11053. ;; lead time is specified.
  11054. (floor (* (string-to-number (match-string 1 ts))
  11055. (cdr (assoc (match-string 2 ts)
  11056. '(("d" . 1) ("w" . 7)
  11057. ("m" . 30.4) ("y" . 365.25)))))))
  11058. ;; go for the default.
  11059. (t org-deadline-warning-days)))
  11060. (defun org-calendar-select-mouse (ev)
  11061. "Return to `org-read-date' with the date currently selected.
  11062. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11063. (interactive "e")
  11064. (mouse-set-point ev)
  11065. (when (calendar-cursor-to-date)
  11066. (let* ((date (calendar-cursor-to-date))
  11067. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11068. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11069. (if (active-minibuffer-window) (exit-minibuffer))))
  11070. (defun org-check-deadlines (ndays)
  11071. "Check if there are any deadlines due or past due.
  11072. A deadline is considered due if it happens within `org-deadline-warning-days'
  11073. days from today's date. If the deadline appears in an entry marked DONE,
  11074. it is not shown. The prefix arg NDAYS can be used to test that many
  11075. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11076. (interactive "P")
  11077. (let* ((org-warn-days
  11078. (cond
  11079. ((equal ndays '(4)) 100000)
  11080. (ndays (prefix-numeric-value ndays))
  11081. (t (abs org-deadline-warning-days))))
  11082. (case-fold-search nil)
  11083. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11084. (callback
  11085. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11086. (message "%d deadlines past-due or due within %d days"
  11087. (org-occur regexp nil callback)
  11088. org-warn-days)))
  11089. (defun org-check-before-date (date)
  11090. "Check if there are deadlines or scheduled entries before DATE."
  11091. (interactive (list (org-read-date)))
  11092. (let ((case-fold-search nil)
  11093. (regexp (concat "\\<\\(" org-deadline-string
  11094. "\\|" org-scheduled-string
  11095. "\\) *<\\([^>]+\\)>"))
  11096. (callback
  11097. (lambda () (time-less-p
  11098. (org-time-string-to-time (match-string 2))
  11099. (org-time-string-to-time date)))))
  11100. (message "%d entries before %s"
  11101. (org-occur regexp nil callback) date)))
  11102. (defun org-evaluate-time-range (&optional to-buffer)
  11103. "Evaluate a time range by computing the difference between start and end.
  11104. Normally the result is just printed in the echo area, but with prefix arg
  11105. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11106. If the time range is actually in a table, the result is inserted into the
  11107. next column.
  11108. For time difference computation, a year is assumed to be exactly 365
  11109. days in order to avoid rounding problems."
  11110. (interactive "P")
  11111. (or
  11112. (org-clock-update-time-maybe)
  11113. (save-excursion
  11114. (unless (org-at-date-range-p t)
  11115. (goto-char (point-at-bol))
  11116. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11117. (if (not (org-at-date-range-p t))
  11118. (error "Not at a time-stamp range, and none found in current line")))
  11119. (let* ((ts1 (match-string 1))
  11120. (ts2 (match-string 2))
  11121. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11122. (match-end (match-end 0))
  11123. (time1 (org-time-string-to-time ts1))
  11124. (time2 (org-time-string-to-time ts2))
  11125. (t1 (time-to-seconds time1))
  11126. (t2 (time-to-seconds time2))
  11127. (diff (abs (- t2 t1)))
  11128. (negative (< (- t2 t1) 0))
  11129. ;; (ys (floor (* 365 24 60 60)))
  11130. (ds (* 24 60 60))
  11131. (hs (* 60 60))
  11132. (fy "%dy %dd %02d:%02d")
  11133. (fy1 "%dy %dd")
  11134. (fd "%dd %02d:%02d")
  11135. (fd1 "%dd")
  11136. (fh "%02d:%02d")
  11137. y d h m align)
  11138. (if havetime
  11139. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11140. y 0
  11141. d (floor (/ diff ds)) diff (mod diff ds)
  11142. h (floor (/ diff hs)) diff (mod diff hs)
  11143. m (floor (/ diff 60)))
  11144. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11145. y 0
  11146. d (floor (+ (/ diff ds) 0.5))
  11147. h 0 m 0))
  11148. (if (not to-buffer)
  11149. (message "%s" (org-make-tdiff-string y d h m))
  11150. (if (org-at-table-p)
  11151. (progn
  11152. (goto-char match-end)
  11153. (setq align t)
  11154. (and (looking-at " *|") (goto-char (match-end 0))))
  11155. (goto-char match-end))
  11156. (if (looking-at
  11157. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11158. (replace-match ""))
  11159. (if negative (insert " -"))
  11160. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11161. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11162. (insert " " (format fh h m))))
  11163. (if align (org-table-align))
  11164. (message "Time difference inserted")))))
  11165. (defun org-make-tdiff-string (y d h m)
  11166. (let ((fmt "")
  11167. (l nil))
  11168. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11169. l (push y l)))
  11170. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11171. l (push d l)))
  11172. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11173. l (push h l)))
  11174. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11175. l (push m l)))
  11176. (apply 'format fmt (nreverse l))))
  11177. (defun org-time-string-to-time (s)
  11178. (apply 'encode-time (org-parse-time-string s)))
  11179. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11180. "Convert a time stamp to an absolute day number.
  11181. If there is a specifyer for a cyclic time stamp, get the closest date to
  11182. DAYNR.
  11183. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  11184. (cond
  11185. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11186. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11187. daynr
  11188. (+ daynr 1000)))
  11189. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11190. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11191. (time-to-days (current-time))) (match-string 0 s)
  11192. prefer show-all))
  11193. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11194. (defun org-days-to-iso-week (days)
  11195. "Return the iso week number."
  11196. (require 'cal-iso)
  11197. (car (calendar-iso-from-absolute days)))
  11198. (defun org-small-year-to-year (year)
  11199. "Convert 2-digit years into 4-digit years.
  11200. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11201. The year 2000 cannot be abbreviated. Any year lager than 99
  11202. is retrned unchanged."
  11203. (if (< year 38)
  11204. (setq year (+ 2000 year))
  11205. (if (< year 100)
  11206. (setq year (+ 1900 year))))
  11207. year)
  11208. (defun org-time-from-absolute (d)
  11209. "Return the time corresponding to date D.
  11210. D may be an absolute day number, or a calendar-type list (month day year)."
  11211. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11212. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11213. (defun org-calendar-holiday ()
  11214. "List of holidays, for Diary display in Org-mode."
  11215. (require 'holidays)
  11216. (let ((hl (funcall
  11217. (if (fboundp 'calendar-check-holidays)
  11218. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11219. (if hl (mapconcat 'identity hl "; "))))
  11220. (defun org-diary-sexp-entry (sexp entry date)
  11221. "Process a SEXP diary ENTRY for DATE."
  11222. (require 'diary-lib)
  11223. (let ((result (if calendar-debug-sexp
  11224. (let ((stack-trace-on-error t))
  11225. (eval (car (read-from-string sexp))))
  11226. (condition-case nil
  11227. (eval (car (read-from-string sexp)))
  11228. (error
  11229. (beep)
  11230. (message "Bad sexp at line %d in %s: %s"
  11231. (org-current-line)
  11232. (buffer-file-name) sexp)
  11233. (sleep-for 2))))))
  11234. (cond ((stringp result) result)
  11235. ((and (consp result)
  11236. (stringp (cdr result))) (cdr result))
  11237. (result entry)
  11238. (t nil))))
  11239. (defun org-diary-to-ical-string (frombuf)
  11240. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11241. This uses the icalendar.el library."
  11242. (let* ((tmpdir (if (featurep 'xemacs)
  11243. (temp-directory)
  11244. temporary-file-directory))
  11245. (tmpfile (make-temp-name
  11246. (expand-file-name "orgics" tmpdir)))
  11247. buf rtn b e)
  11248. (save-excursion
  11249. (set-buffer frombuf)
  11250. (icalendar-export-region (point-min) (point-max) tmpfile)
  11251. (setq buf (find-buffer-visiting tmpfile))
  11252. (set-buffer buf)
  11253. (goto-char (point-min))
  11254. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11255. (setq b (match-beginning 0)))
  11256. (goto-char (point-max))
  11257. (if (re-search-backward "^END:VEVENT" nil t)
  11258. (setq e (match-end 0)))
  11259. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11260. (kill-buffer buf)
  11261. (kill-buffer frombuf)
  11262. (delete-file tmpfile)
  11263. rtn))
  11264. (defun org-closest-date (start current change prefer show-all)
  11265. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11266. When PREFER is `past' return a date that is either CURRENT or past.
  11267. When PREFER is `future', return a date that is either CURRENT or future.
  11268. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  11269. ;; Make the proper lists from the dates
  11270. (catch 'exit
  11271. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11272. dn dw sday cday n1 n2
  11273. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11274. (setq start (org-date-to-gregorian start)
  11275. current (org-date-to-gregorian
  11276. (if show-all
  11277. current
  11278. (time-to-days (current-time))))
  11279. sday (calendar-absolute-from-gregorian start)
  11280. cday (calendar-absolute-from-gregorian current))
  11281. (if (<= cday sday) (throw 'exit sday))
  11282. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11283. (setq dn (string-to-number (match-string 1 change))
  11284. dw (cdr (assoc (match-string 2 change) a1)))
  11285. (error "Invalid change specifyer: %s" change))
  11286. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11287. (cond
  11288. ((eq dw 'day)
  11289. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11290. n2 (+ n1 dn)))
  11291. ((eq dw 'year)
  11292. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11293. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11294. (setq date1 (list m d y1)
  11295. n1 (calendar-absolute-from-gregorian date1)
  11296. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11297. n2 (calendar-absolute-from-gregorian date2)))
  11298. ((eq dw 'month)
  11299. ;; approx number of month between the tow dates
  11300. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11301. ;; How often does dn fit in there?
  11302. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11303. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11304. m (+ m nm)
  11305. ny (floor (/ m 12))
  11306. y (+ y ny)
  11307. m (- m (* ny 12)))
  11308. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11309. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11310. (setq m2 (+ m dn) y2 y)
  11311. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11312. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11313. (while (< n2 cday)
  11314. (setq n1 n2 m m2 y y2)
  11315. (setq m2 (+ m dn) y2 y)
  11316. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11317. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11318. (if show-all
  11319. (cond
  11320. ((eq prefer 'past) n1)
  11321. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11322. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11323. (cond
  11324. ((eq prefer 'past) n1)
  11325. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11326. (t (if (= cday n1) n1 n2)))))))
  11327. (defun org-date-to-gregorian (date)
  11328. "Turn any specification of DATE into a gregorian date for the calendar."
  11329. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11330. ((and (listp date) (= (length date) 3)) date)
  11331. ((stringp date)
  11332. (setq date (org-parse-time-string date))
  11333. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11334. ((listp date)
  11335. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11336. (defun org-parse-time-string (s &optional nodefault)
  11337. "Parse the standard Org-mode time string.
  11338. This should be a lot faster than the normal `parse-time-string'.
  11339. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11340. hour and minute fields will be nil if not given."
  11341. (if (string-match org-ts-regexp0 s)
  11342. (list 0
  11343. (if (or (match-beginning 8) (not nodefault))
  11344. (string-to-number (or (match-string 8 s) "0")))
  11345. (if (or (match-beginning 7) (not nodefault))
  11346. (string-to-number (or (match-string 7 s) "0")))
  11347. (string-to-number (match-string 4 s))
  11348. (string-to-number (match-string 3 s))
  11349. (string-to-number (match-string 2 s))
  11350. nil nil nil)
  11351. (make-list 9 0)))
  11352. (defun org-timestamp-up (&optional arg)
  11353. "Increase the date item at the cursor by one.
  11354. If the cursor is on the year, change the year. If it is on the month or
  11355. the day, change that.
  11356. With prefix ARG, change by that many units."
  11357. (interactive "p")
  11358. (org-timestamp-change (prefix-numeric-value arg)))
  11359. (defun org-timestamp-down (&optional arg)
  11360. "Decrease the date item at the cursor by one.
  11361. If the cursor is on the year, change the year. If it is on the month or
  11362. the day, change that.
  11363. With prefix ARG, change by that many units."
  11364. (interactive "p")
  11365. (org-timestamp-change (- (prefix-numeric-value arg))))
  11366. (defun org-timestamp-up-day (&optional arg)
  11367. "Increase the date in the time stamp by one day.
  11368. With prefix ARG, change that many days."
  11369. (interactive "p")
  11370. (if (and (not (org-at-timestamp-p t))
  11371. (org-on-heading-p))
  11372. (org-todo 'up)
  11373. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11374. (defun org-timestamp-down-day (&optional arg)
  11375. "Decrease the date in the time stamp by one day.
  11376. With prefix ARG, change that many days."
  11377. (interactive "p")
  11378. (if (and (not (org-at-timestamp-p t))
  11379. (org-on-heading-p))
  11380. (org-todo 'down)
  11381. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11382. (defun org-at-timestamp-p (&optional inactive-ok)
  11383. "Determine if the cursor is in or at a timestamp."
  11384. (interactive)
  11385. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11386. (pos (point))
  11387. (ans (or (looking-at tsr)
  11388. (save-excursion
  11389. (skip-chars-backward "^[<\n\r\t")
  11390. (if (> (point) (point-min)) (backward-char 1))
  11391. (and (looking-at tsr)
  11392. (> (- (match-end 0) pos) -1))))))
  11393. (and ans
  11394. (boundp 'org-ts-what)
  11395. (setq org-ts-what
  11396. (cond
  11397. ((= pos (match-beginning 0)) 'bracket)
  11398. ((= pos (1- (match-end 0))) 'bracket)
  11399. ((org-pos-in-match-range pos 2) 'year)
  11400. ((org-pos-in-match-range pos 3) 'month)
  11401. ((org-pos-in-match-range pos 7) 'hour)
  11402. ((org-pos-in-match-range pos 8) 'minute)
  11403. ((or (org-pos-in-match-range pos 4)
  11404. (org-pos-in-match-range pos 5)) 'day)
  11405. ((and (> pos (or (match-end 8) (match-end 5)))
  11406. (< pos (match-end 0)))
  11407. (- pos (or (match-end 8) (match-end 5))))
  11408. (t 'day))))
  11409. ans))
  11410. (defun org-toggle-timestamp-type ()
  11411. "Toggle the type (<active> or [inactive]) of a time stamp."
  11412. (interactive)
  11413. (when (org-at-timestamp-p t)
  11414. (save-excursion
  11415. (goto-char (match-beginning 0))
  11416. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  11417. (goto-char (1- (match-end 0)))
  11418. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  11419. (message "Timestamp is now %sactive"
  11420. (if (equal (char-before) ?>) "in" ""))))
  11421. (defun org-timestamp-change (n &optional what)
  11422. "Change the date in the time stamp at point.
  11423. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11424. `year', `minute', `second'. If WHAT is not given, the cursor position
  11425. in the timestamp determines what will be changed."
  11426. (let ((pos (point))
  11427. with-hm inactive
  11428. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11429. org-ts-what
  11430. extra rem
  11431. ts time time0)
  11432. (if (not (org-at-timestamp-p t))
  11433. (error "Not at a timestamp"))
  11434. (if (and (not what) (eq org-ts-what 'bracket))
  11435. (org-toggle-timestamp-type)
  11436. (if (and (not what) (not (eq org-ts-what 'day))
  11437. org-display-custom-times
  11438. (get-text-property (point) 'display)
  11439. (not (get-text-property (1- (point)) 'display)))
  11440. (setq org-ts-what 'day))
  11441. (setq org-ts-what (or what org-ts-what)
  11442. inactive (= (char-after (match-beginning 0)) ?\[)
  11443. ts (match-string 0))
  11444. (replace-match "")
  11445. (if (string-match
  11446. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11447. ts)
  11448. (setq extra (match-string 1 ts)))
  11449. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11450. (setq with-hm t))
  11451. (setq time0 (org-parse-time-string ts))
  11452. (when (and (eq org-ts-what 'minute)
  11453. (eq current-prefix-arg nil))
  11454. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11455. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11456. (setcar (cdr time0) (+ (nth 1 time0)
  11457. (if (> n 0) (- rem) (- dm rem))))))
  11458. (setq time
  11459. (encode-time (or (car time0) 0)
  11460. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11461. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11462. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11463. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11464. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11465. (nthcdr 6 time0)))
  11466. (when (integerp org-ts-what)
  11467. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11468. (if (eq what 'calendar)
  11469. (let ((cal-date (org-get-date-from-calendar)))
  11470. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11471. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11472. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11473. (setcar time0 (or (car time0) 0))
  11474. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11475. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11476. (setq time (apply 'encode-time time0))))
  11477. (setq org-last-changed-timestamp
  11478. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11479. (org-clock-update-time-maybe)
  11480. (goto-char pos)
  11481. ;; Try to recenter the calendar window, if any
  11482. (if (and org-calendar-follow-timestamp-change
  11483. (get-buffer-window "*Calendar*" t)
  11484. (memq org-ts-what '(day month year)))
  11485. (org-recenter-calendar (time-to-days time))))))
  11486. (defun org-modify-ts-extra (s pos n dm)
  11487. "Change the different parts of the lead-time and repeat fields in timestamp."
  11488. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11489. ng h m new rem)
  11490. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11491. (cond
  11492. ((or (org-pos-in-match-range pos 2)
  11493. (org-pos-in-match-range pos 3))
  11494. (setq m (string-to-number (match-string 3 s))
  11495. h (string-to-number (match-string 2 s)))
  11496. (if (org-pos-in-match-range pos 2)
  11497. (setq h (+ h n))
  11498. (setq n (* dm (org-no-warnings (signum n))))
  11499. (when (not (= 0 (setq rem (% m dm))))
  11500. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11501. (setq m (+ m n)))
  11502. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11503. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11504. (setq h (min 24 (max 0 h)))
  11505. (setq ng 1 new (format "-%02d:%02d" h m)))
  11506. ((org-pos-in-match-range pos 6)
  11507. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11508. ((org-pos-in-match-range pos 5)
  11509. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11510. ((org-pos-in-match-range pos 9)
  11511. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11512. ((org-pos-in-match-range pos 8)
  11513. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11514. (when ng
  11515. (setq s (concat
  11516. (substring s 0 (match-beginning ng))
  11517. new
  11518. (substring s (match-end ng))))))
  11519. s))
  11520. (defun org-recenter-calendar (date)
  11521. "If the calendar is visible, recenter it to DATE."
  11522. (let* ((win (selected-window))
  11523. (cwin (get-buffer-window "*Calendar*" t))
  11524. (calendar-move-hook nil))
  11525. (when cwin
  11526. (select-window cwin)
  11527. (calendar-goto-date (if (listp date) date
  11528. (calendar-gregorian-from-absolute date)))
  11529. (select-window win))))
  11530. (defun org-goto-calendar (&optional arg)
  11531. "Go to the Emacs calendar at the current date.
  11532. If there is a time stamp in the current line, go to that date.
  11533. A prefix ARG can be used to force the current date."
  11534. (interactive "P")
  11535. (let ((tsr org-ts-regexp) diff
  11536. (calendar-move-hook nil)
  11537. (view-calendar-holidays-initially nil)
  11538. (view-diary-entries-initially nil))
  11539. (if (or (org-at-timestamp-p)
  11540. (save-excursion
  11541. (beginning-of-line 1)
  11542. (looking-at (concat ".*" tsr))))
  11543. (let ((d1 (time-to-days (current-time)))
  11544. (d2 (time-to-days
  11545. (org-time-string-to-time (match-string 1)))))
  11546. (setq diff (- d2 d1))))
  11547. (calendar)
  11548. (calendar-goto-today)
  11549. (if (and diff (not arg)) (calendar-forward-day diff))))
  11550. (defun org-get-date-from-calendar ()
  11551. "Return a list (month day year) of date at point in calendar."
  11552. (with-current-buffer "*Calendar*"
  11553. (save-match-data
  11554. (calendar-cursor-to-date))))
  11555. (defun org-date-from-calendar ()
  11556. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11557. If there is already a time stamp at the cursor position, update it."
  11558. (interactive)
  11559. (if (org-at-timestamp-p t)
  11560. (org-timestamp-change 0 'calendar)
  11561. (let ((cal-date (org-get-date-from-calendar)))
  11562. (org-insert-time-stamp
  11563. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11564. (defun org-minutes-to-hours (m)
  11565. "Compute H:MM from a number of minutes."
  11566. (let ((h (/ m 60)))
  11567. (setq m (- m (* 60 h)))
  11568. (format "%d:%02d" h m)))
  11569. ;;;; Agenda files
  11570. (defun org-agenda-files (&optional unrestricted)
  11571. "Get the list of agenda files.
  11572. Optional UNRESTRICTED means return the full list even if a restriction
  11573. is currently in place."
  11574. (let ((files
  11575. (cond
  11576. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11577. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11578. ((listp org-agenda-files) org-agenda-files)
  11579. (t (error "Invalid value of `org-agenda-files'")))))
  11580. (setq files (apply 'append
  11581. (mapcar (lambda (f)
  11582. (if (file-directory-p f)
  11583. (directory-files f t
  11584. org-agenda-file-regexp)
  11585. (list f)))
  11586. files)))
  11587. (if org-agenda-skip-unavailable-files
  11588. (delq nil
  11589. (mapcar (function
  11590. (lambda (file)
  11591. (and (file-readable-p file) file)))
  11592. files))
  11593. files))) ; `org-check-agenda-file' will remove them from the list
  11594. (defun org-edit-agenda-file-list ()
  11595. "Edit the list of agenda files.
  11596. Depending on setup, this either uses customize to edit the variable
  11597. `org-agenda-files', or it visits the file that is holding the list. In the
  11598. latter case, the buffer is set up in a way that saving it automatically kills
  11599. the buffer and restores the previous window configuration."
  11600. (interactive)
  11601. (if (stringp org-agenda-files)
  11602. (let ((cw (current-window-configuration)))
  11603. (find-file org-agenda-files)
  11604. (org-set-local 'org-window-configuration cw)
  11605. (org-add-hook 'after-save-hook
  11606. (lambda ()
  11607. (set-window-configuration
  11608. (prog1 org-window-configuration
  11609. (kill-buffer (current-buffer))))
  11610. (org-install-agenda-files-menu)
  11611. (message "New agenda file list installed"))
  11612. nil 'local)
  11613. (message "%s" (substitute-command-keys
  11614. "Edit list and finish with \\[save-buffer]")))
  11615. (customize-variable 'org-agenda-files)))
  11616. (defun org-store-new-agenda-file-list (list)
  11617. "Set new value for the agenda file list and save it correcly."
  11618. (if (stringp org-agenda-files)
  11619. (let ((f org-agenda-files) b)
  11620. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11621. (with-temp-file f
  11622. (insert (mapconcat 'identity list "\n") "\n")))
  11623. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11624. (setq org-agenda-files list)
  11625. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11626. (defun org-read-agenda-file-list ()
  11627. "Read the list of agenda files from a file."
  11628. (when (stringp org-agenda-files)
  11629. (with-temp-buffer
  11630. (insert-file-contents org-agenda-files)
  11631. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11632. ;;;###autoload
  11633. (defun org-cycle-agenda-files ()
  11634. "Cycle through the files in `org-agenda-files'.
  11635. If the current buffer visits an agenda file, find the next one in the list.
  11636. If the current buffer does not, find the first agenda file."
  11637. (interactive)
  11638. (let* ((fs (org-agenda-files t))
  11639. (files (append fs (list (car fs))))
  11640. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11641. file)
  11642. (unless files (error "No agenda files"))
  11643. (catch 'exit
  11644. (while (setq file (pop files))
  11645. (if (equal (file-truename file) tcf)
  11646. (when (car files)
  11647. (find-file (car files))
  11648. (throw 'exit t))))
  11649. (find-file (car fs)))
  11650. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11651. (defun org-agenda-file-to-front (&optional to-end)
  11652. "Move/add the current file to the top of the agenda file list.
  11653. If the file is not present in the list, it is added to the front. If it is
  11654. present, it is moved there. With optional argument TO-END, add/move to the
  11655. end of the list."
  11656. (interactive "P")
  11657. (let ((org-agenda-skip-unavailable-files nil)
  11658. (file-alist (mapcar (lambda (x)
  11659. (cons (file-truename x) x))
  11660. (org-agenda-files t)))
  11661. (ctf (file-truename buffer-file-name))
  11662. x had)
  11663. (setq x (assoc ctf file-alist) had x)
  11664. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11665. (if to-end
  11666. (setq file-alist (append (delq x file-alist) (list x)))
  11667. (setq file-alist (cons x (delq x file-alist))))
  11668. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11669. (org-install-agenda-files-menu)
  11670. (message "File %s to %s of agenda file list"
  11671. (if had "moved" "added") (if to-end "end" "front"))))
  11672. (defun org-remove-file (&optional file)
  11673. "Remove current file from the list of files in variable `org-agenda-files'.
  11674. These are the files which are being checked for agenda entries.
  11675. Optional argument FILE means, use this file instead of the current."
  11676. (interactive)
  11677. (let* ((org-agenda-skip-unavailable-files nil)
  11678. (file (or file buffer-file-name))
  11679. (true-file (file-truename file))
  11680. (afile (abbreviate-file-name file))
  11681. (files (delq nil (mapcar
  11682. (lambda (x)
  11683. (if (equal true-file
  11684. (file-truename x))
  11685. nil x))
  11686. (org-agenda-files t)))))
  11687. (if (not (= (length files) (length (org-agenda-files t))))
  11688. (progn
  11689. (org-store-new-agenda-file-list files)
  11690. (org-install-agenda-files-menu)
  11691. (message "Removed file: %s" afile))
  11692. (message "File was not in list: %s (not removed)" afile))))
  11693. (defun org-file-menu-entry (file)
  11694. (vector file (list 'find-file file) t))
  11695. (defun org-check-agenda-file (file)
  11696. "Make sure FILE exists. If not, ask user what to do."
  11697. (when (not (file-exists-p file))
  11698. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11699. (abbreviate-file-name file))
  11700. (let ((r (downcase (read-char-exclusive))))
  11701. (cond
  11702. ((equal r ?r)
  11703. (org-remove-file file)
  11704. (throw 'nextfile t))
  11705. (t (error "Abort"))))))
  11706. (defun org-get-agenda-file-buffer (file)
  11707. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11708. it to the list of buffers which might be released later."
  11709. (let ((buf (org-find-base-buffer-visiting file)))
  11710. (if buf
  11711. buf ; just return it
  11712. ;; Make a new buffer and remember it
  11713. (setq buf (find-file-noselect file))
  11714. (if buf (push buf org-agenda-new-buffers))
  11715. buf)))
  11716. (defun org-release-buffers (blist)
  11717. "Release all buffers in list, asking the user for confirmation when needed.
  11718. When a buffer is unmodified, it is just killed. When modified, it is saved
  11719. \(if the user agrees) and then killed."
  11720. (let (buf file)
  11721. (while (setq buf (pop blist))
  11722. (setq file (buffer-file-name buf))
  11723. (when (and (buffer-modified-p buf)
  11724. file
  11725. (y-or-n-p (format "Save file %s? " file)))
  11726. (with-current-buffer buf (save-buffer)))
  11727. (kill-buffer buf))))
  11728. (defun org-prepare-agenda-buffers (files)
  11729. "Create buffers for all agenda files, protect archived trees and comments."
  11730. (interactive)
  11731. (let ((pa '(:org-archived t))
  11732. (pc '(:org-comment t))
  11733. (pall '(:org-archived t :org-comment t))
  11734. (inhibit-read-only t)
  11735. (rea (concat ":" org-archive-tag ":"))
  11736. bmp file re)
  11737. (save-excursion
  11738. (save-restriction
  11739. (while (setq file (pop files))
  11740. (if (bufferp file)
  11741. (set-buffer file)
  11742. (org-check-agenda-file file)
  11743. (set-buffer (org-get-agenda-file-buffer file)))
  11744. (widen)
  11745. (setq bmp (buffer-modified-p))
  11746. (org-refresh-category-properties)
  11747. (setq org-todo-keywords-for-agenda
  11748. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11749. (setq org-done-keywords-for-agenda
  11750. (append org-done-keywords-for-agenda org-done-keywords))
  11751. (save-excursion
  11752. (remove-text-properties (point-min) (point-max) pall)
  11753. (when org-agenda-skip-archived-trees
  11754. (goto-char (point-min))
  11755. (while (re-search-forward rea nil t)
  11756. (if (org-on-heading-p t)
  11757. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11758. (goto-char (point-min))
  11759. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11760. (while (re-search-forward re nil t)
  11761. (add-text-properties
  11762. (match-beginning 0) (org-end-of-subtree t) pc)))
  11763. (set-buffer-modified-p bmp))))))
  11764. ;;;; Embedded LaTeX
  11765. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11766. "Keymap for the minor `org-cdlatex-mode'.")
  11767. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11768. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11769. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11770. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11771. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11772. (defvar org-cdlatex-texmathp-advice-is-done nil
  11773. "Flag remembering if we have applied the advice to texmathp already.")
  11774. (define-minor-mode org-cdlatex-mode
  11775. "Toggle the minor `org-cdlatex-mode'.
  11776. This mode supports entering LaTeX environment and math in LaTeX fragments
  11777. in Org-mode.
  11778. \\{org-cdlatex-mode-map}"
  11779. nil " OCDL" nil
  11780. (when org-cdlatex-mode (require 'cdlatex))
  11781. (unless org-cdlatex-texmathp-advice-is-done
  11782. (setq org-cdlatex-texmathp-advice-is-done t)
  11783. (defadvice texmathp (around org-math-always-on activate)
  11784. "Always return t in org-mode buffers.
  11785. This is because we want to insert math symbols without dollars even outside
  11786. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11787. en embedded LaTeX fragement, let texmathp do its job.
  11788. \\[org-cdlatex-mode-map]"
  11789. (interactive)
  11790. (let (p)
  11791. (cond
  11792. ((not (org-mode-p)) ad-do-it)
  11793. ((eq this-command 'cdlatex-math-symbol)
  11794. (setq ad-return-value t
  11795. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11796. (t
  11797. (let ((p (org-inside-LaTeX-fragment-p)))
  11798. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11799. (setq ad-return-value t
  11800. texmathp-why '("Org-mode embedded math" . 0))
  11801. (if p ad-do-it)))))))))
  11802. (defun turn-on-org-cdlatex ()
  11803. "Unconditionally turn on `org-cdlatex-mode'."
  11804. (org-cdlatex-mode 1))
  11805. (defun org-inside-LaTeX-fragment-p ()
  11806. "Test if point is inside a LaTeX fragment.
  11807. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11808. sequence appearing also before point.
  11809. Even though the matchers for math are configurable, this function assumes
  11810. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11811. delimiters are skipped when they have been removed by customization.
  11812. The return value is nil, or a cons cell with the delimiter and
  11813. and the position of this delimiter.
  11814. This function does a reasonably good job, but can locally be fooled by
  11815. for example currency specifications. For example it will assume being in
  11816. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11817. fragments that are properly closed, but during editing, we have to live
  11818. with the uncertainty caused by missing closing delimiters. This function
  11819. looks only before point, not after."
  11820. (catch 'exit
  11821. (let ((pos (point))
  11822. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11823. (lim (progn
  11824. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11825. (point)))
  11826. dd-on str (start 0) m re)
  11827. (goto-char pos)
  11828. (when dodollar
  11829. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11830. re (nth 1 (assoc "$" org-latex-regexps)))
  11831. (while (string-match re str start)
  11832. (cond
  11833. ((= (match-end 0) (length str))
  11834. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11835. ((= (match-end 0) (- (length str) 5))
  11836. (throw 'exit nil))
  11837. (t (setq start (match-end 0))))))
  11838. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11839. (goto-char pos)
  11840. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11841. (and (match-beginning 2) (throw 'exit nil))
  11842. ;; count $$
  11843. (while (re-search-backward "\\$\\$" lim t)
  11844. (setq dd-on (not dd-on)))
  11845. (goto-char pos)
  11846. (if dd-on (cons "$$" m))))))
  11847. (defun org-try-cdlatex-tab ()
  11848. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11849. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11850. - inside a LaTeX fragment, or
  11851. - after the first word in a line, where an abbreviation expansion could
  11852. insert a LaTeX environment."
  11853. (when org-cdlatex-mode
  11854. (cond
  11855. ((save-excursion
  11856. (skip-chars-backward "a-zA-Z0-9*")
  11857. (skip-chars-backward " \t")
  11858. (bolp))
  11859. (cdlatex-tab) t)
  11860. ((org-inside-LaTeX-fragment-p)
  11861. (cdlatex-tab) t)
  11862. (t nil))))
  11863. (defun org-cdlatex-underscore-caret (&optional arg)
  11864. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11865. Revert to the normal definition outside of these fragments."
  11866. (interactive "P")
  11867. (if (org-inside-LaTeX-fragment-p)
  11868. (call-interactively 'cdlatex-sub-superscript)
  11869. (let (org-cdlatex-mode)
  11870. (call-interactively (key-binding (vector last-input-event))))))
  11871. (defun org-cdlatex-math-modify (&optional arg)
  11872. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11873. Revert to the normal definition outside of these fragments."
  11874. (interactive "P")
  11875. (if (org-inside-LaTeX-fragment-p)
  11876. (call-interactively 'cdlatex-math-modify)
  11877. (let (org-cdlatex-mode)
  11878. (call-interactively (key-binding (vector last-input-event))))))
  11879. (defvar org-latex-fragment-image-overlays nil
  11880. "List of overlays carrying the images of latex fragments.")
  11881. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11882. (defun org-remove-latex-fragment-image-overlays ()
  11883. "Remove all overlays with LaTeX fragment images in current buffer."
  11884. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11885. (setq org-latex-fragment-image-overlays nil))
  11886. (defun org-preview-latex-fragment (&optional subtree)
  11887. "Preview the LaTeX fragment at point, or all locally or globally.
  11888. If the cursor is in a LaTeX fragment, create the image and overlay
  11889. it over the source code. If there is no fragment at point, display
  11890. all fragments in the current text, from one headline to the next. With
  11891. prefix SUBTREE, display all fragments in the current subtree. With a
  11892. double prefix `C-u C-u', or when the cursor is before the first headline,
  11893. display all fragments in the buffer.
  11894. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11895. (interactive "P")
  11896. (org-remove-latex-fragment-image-overlays)
  11897. (save-excursion
  11898. (save-restriction
  11899. (let (beg end at msg)
  11900. (cond
  11901. ((or (equal subtree '(16))
  11902. (not (save-excursion
  11903. (re-search-backward (concat "^" outline-regexp) nil t))))
  11904. (setq beg (point-min) end (point-max)
  11905. msg "Creating images for buffer...%s"))
  11906. ((equal subtree '(4))
  11907. (org-back-to-heading)
  11908. (setq beg (point) end (org-end-of-subtree t)
  11909. msg "Creating images for subtree...%s"))
  11910. (t
  11911. (if (setq at (org-inside-LaTeX-fragment-p))
  11912. (goto-char (max (point-min) (- (cdr at) 2)))
  11913. (org-back-to-heading))
  11914. (setq beg (point) end (progn (outline-next-heading) (point))
  11915. msg (if at "Creating image...%s"
  11916. "Creating images for entry...%s"))))
  11917. (message msg "")
  11918. (narrow-to-region beg end)
  11919. (goto-char beg)
  11920. (org-format-latex
  11921. (concat "ltxpng/" (file-name-sans-extension
  11922. (file-name-nondirectory
  11923. buffer-file-name)))
  11924. default-directory 'overlays msg at 'forbuffer)
  11925. (message msg "done. Use `C-c C-c' to remove images.")))))
  11926. (defvar org-latex-regexps
  11927. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11928. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11929. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11930. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11931. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11932. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11933. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11934. "Regular expressions for matching embedded LaTeX.")
  11935. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11936. "Replace LaTeX fragments with links to an image, and produce images."
  11937. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11938. (let* ((prefixnodir (file-name-nondirectory prefix))
  11939. (absprefix (expand-file-name prefix dir))
  11940. (todir (file-name-directory absprefix))
  11941. (opt org-format-latex-options)
  11942. (matchers (plist-get opt :matchers))
  11943. (re-list org-latex-regexps)
  11944. (cnt 0) txt link beg end re e checkdir
  11945. m n block linkfile movefile ov)
  11946. ;; Check if there are old images files with this prefix, and remove them
  11947. (when (file-directory-p todir)
  11948. (mapc 'delete-file
  11949. (directory-files
  11950. todir 'full
  11951. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11952. ;; Check the different regular expressions
  11953. (while (setq e (pop re-list))
  11954. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11955. block (if (nth 3 e) "\n\n" ""))
  11956. (when (member m matchers)
  11957. (goto-char (point-min))
  11958. (while (re-search-forward re nil t)
  11959. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11960. (setq txt (match-string n)
  11961. beg (match-beginning n) end (match-end n)
  11962. cnt (1+ cnt)
  11963. linkfile (format "%s_%04d.png" prefix cnt)
  11964. movefile (format "%s_%04d.png" absprefix cnt)
  11965. link (concat block "[[file:" linkfile "]]" block))
  11966. (if msg (message msg cnt))
  11967. (goto-char beg)
  11968. (unless checkdir ; make sure the directory exists
  11969. (setq checkdir t)
  11970. (or (file-directory-p todir) (make-directory todir)))
  11971. (org-create-formula-image
  11972. txt movefile opt forbuffer)
  11973. (if overlays
  11974. (progn
  11975. (setq ov (org-make-overlay beg end))
  11976. (if (featurep 'xemacs)
  11977. (progn
  11978. (org-overlay-put ov 'invisible t)
  11979. (org-overlay-put
  11980. ov 'end-glyph
  11981. (make-glyph (vector 'png :file movefile))))
  11982. (org-overlay-put
  11983. ov 'display
  11984. (list 'image :type 'png :file movefile :ascent 'center)))
  11985. (push ov org-latex-fragment-image-overlays)
  11986. (goto-char end))
  11987. (delete-region beg end)
  11988. (insert link))))))))
  11989. ;; This function borrows from Ganesh Swami's latex2png.el
  11990. (defun org-create-formula-image (string tofile options buffer)
  11991. (let* ((tmpdir (if (featurep 'xemacs)
  11992. (temp-directory)
  11993. temporary-file-directory))
  11994. (texfilebase (make-temp-name
  11995. (expand-file-name "orgtex" tmpdir)))
  11996. (texfile (concat texfilebase ".tex"))
  11997. (dvifile (concat texfilebase ".dvi"))
  11998. (pngfile (concat texfilebase ".png"))
  11999. (fnh (face-attribute 'default :height nil))
  12000. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12001. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12002. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12003. "Black"))
  12004. (bg (or (plist-get options (if buffer :background :html-background))
  12005. "Transparent")))
  12006. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12007. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12008. (with-temp-file texfile
  12009. (insert org-format-latex-header
  12010. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12011. (let ((dir default-directory))
  12012. (condition-case nil
  12013. (progn
  12014. (cd tmpdir)
  12015. (call-process "latex" nil nil nil texfile))
  12016. (error nil))
  12017. (cd dir))
  12018. (if (not (file-exists-p dvifile))
  12019. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12020. (call-process "dvipng" nil nil nil
  12021. "-E" "-fg" fg "-bg" bg
  12022. "-D" dpi
  12023. ;;"-x" scale "-y" scale
  12024. "-T" "tight"
  12025. "-o" pngfile
  12026. dvifile)
  12027. (if (not (file-exists-p pngfile))
  12028. (progn (message "Failed to create png file from %s" texfile) nil)
  12029. ;; Use the requested file name and clean up
  12030. (copy-file pngfile tofile 'replace)
  12031. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12032. (delete-file (concat texfilebase e)))
  12033. pngfile))))
  12034. (defun org-dvipng-color (attr)
  12035. "Return an rgb color specification for dvipng."
  12036. (apply 'format "rgb %s %s %s"
  12037. (mapcar 'org-normalize-color
  12038. (color-values (face-attribute 'default attr nil)))))
  12039. (defun org-normalize-color (value)
  12040. "Return string to be used as color value for an RGB component."
  12041. (format "%g" (/ value 65535.0)))
  12042. ;;;; Key bindings
  12043. ;; Make `C-c C-x' a prefix key
  12044. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12045. ;; TAB key with modifiers
  12046. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12047. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12048. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12049. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12050. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12051. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12052. ;; The following line is necessary under Suse GNU/Linux
  12053. (unless (featurep 'xemacs)
  12054. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12055. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12056. (define-key org-mode-map [backtab] 'org-shifttab)
  12057. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12058. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12059. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12060. ;; Cursor keys with modifiers
  12061. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12062. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12063. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12064. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12065. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12066. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12067. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12068. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12069. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12070. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12071. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12072. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12073. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12074. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12075. ;;; Extra keys for tty access.
  12076. ;; We only set them when really needed because otherwise the
  12077. ;; menus don't show the simple keys
  12078. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12079. (not window-system))
  12080. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12081. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12082. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12083. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12084. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12085. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12086. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12087. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12088. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12089. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12090. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12091. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12092. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12093. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12094. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12095. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12096. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12097. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12098. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12099. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12100. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12101. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12102. ;; All the other keys
  12103. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12104. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12105. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  12106. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12107. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12108. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12109. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12110. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12111. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12112. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12113. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12114. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12115. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12116. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12117. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12118. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12119. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12120. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12121. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  12122. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12123. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12124. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12125. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12126. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12127. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12128. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12129. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12130. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12131. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12132. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12133. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12134. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12135. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12136. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12137. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12138. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12139. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12140. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12141. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12142. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12143. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12144. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12145. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12146. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12147. (org-defkey org-mode-map "\C-m" 'org-return)
  12148. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12149. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12150. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12151. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12152. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12153. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  12154. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12155. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12156. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12157. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12158. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  12159. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12160. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12161. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12162. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12163. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12164. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  12165. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12166. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12167. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12168. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12169. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12170. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12171. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12172. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12173. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12174. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12175. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12176. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12177. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12178. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12179. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  12180. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12181. (when (featurep 'xemacs)
  12182. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12183. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12184. (defun org-self-insert-command (N)
  12185. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12186. If the cursor is in a table looking at whitespace, the whitespace is
  12187. overwritten, and the table is not marked as requiring realignment."
  12188. (interactive "p")
  12189. (if (and (org-table-p)
  12190. (progn
  12191. ;; check if we blank the field, and if that triggers align
  12192. (and (featurep 'org-table) org-table-auto-blank-field
  12193. (member last-command
  12194. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12195. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12196. ;; got extra space, this field does not determine column width
  12197. (let (org-table-may-need-update) (org-table-blank-field))
  12198. ;; no extra space, this field may determine column width
  12199. (org-table-blank-field)))
  12200. t)
  12201. (eq N 1)
  12202. (looking-at "[^|\n]* |"))
  12203. (let (org-table-may-need-update)
  12204. (goto-char (1- (match-end 0)))
  12205. (delete-backward-char 1)
  12206. (goto-char (match-beginning 0))
  12207. (self-insert-command N))
  12208. (setq org-table-may-need-update t)
  12209. (self-insert-command N)
  12210. (org-fix-tags-on-the-fly)))
  12211. (defun org-fix-tags-on-the-fly ()
  12212. (when (and (equal (char-after (point-at-bol)) ?*)
  12213. (org-on-heading-p))
  12214. (org-align-tags-here org-tags-column)))
  12215. (defun org-delete-backward-char (N)
  12216. "Like `delete-backward-char', insert whitespace at field end in tables.
  12217. When deleting backwards, in tables this function will insert whitespace in
  12218. front of the next \"|\" separator, to keep the table aligned. The table will
  12219. still be marked for re-alignment if the field did fill the entire column,
  12220. because, in this case the deletion might narrow the column."
  12221. (interactive "p")
  12222. (if (and (org-table-p)
  12223. (eq N 1)
  12224. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12225. (looking-at ".*?|"))
  12226. (let ((pos (point))
  12227. (noalign (looking-at "[^|\n\r]* |"))
  12228. (c org-table-may-need-update))
  12229. (backward-delete-char N)
  12230. (skip-chars-forward "^|")
  12231. (insert " ")
  12232. (goto-char (1- pos))
  12233. ;; noalign: if there were two spaces at the end, this field
  12234. ;; does not determine the width of the column.
  12235. (if noalign (setq org-table-may-need-update c)))
  12236. (backward-delete-char N)
  12237. (org-fix-tags-on-the-fly)))
  12238. (defun org-delete-char (N)
  12239. "Like `delete-char', but insert whitespace at field end in tables.
  12240. When deleting characters, in tables this function will insert whitespace in
  12241. front of the next \"|\" separator, to keep the table aligned. The table will
  12242. still be marked for re-alignment if the field did fill the entire column,
  12243. because, in this case the deletion might narrow the column."
  12244. (interactive "p")
  12245. (if (and (org-table-p)
  12246. (not (bolp))
  12247. (not (= (char-after) ?|))
  12248. (eq N 1))
  12249. (if (looking-at ".*?|")
  12250. (let ((pos (point))
  12251. (noalign (looking-at "[^|\n\r]* |"))
  12252. (c org-table-may-need-update))
  12253. (replace-match (concat
  12254. (substring (match-string 0) 1 -1)
  12255. " |"))
  12256. (goto-char pos)
  12257. ;; noalign: if there were two spaces at the end, this field
  12258. ;; does not determine the width of the column.
  12259. (if noalign (setq org-table-may-need-update c)))
  12260. (delete-char N))
  12261. (delete-char N)
  12262. (org-fix-tags-on-the-fly)))
  12263. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12264. (put 'org-self-insert-command 'delete-selection t)
  12265. (put 'orgtbl-self-insert-command 'delete-selection t)
  12266. (put 'org-delete-char 'delete-selection 'supersede)
  12267. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12268. ;; Make `flyspell-mode' delay after some commands
  12269. (put 'org-self-insert-command 'flyspell-delayed t)
  12270. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12271. (put 'org-delete-char 'flyspell-delayed t)
  12272. (put 'org-delete-backward-char 'flyspell-delayed t)
  12273. ;; Make pabbrev-mode expand after org-mode commands
  12274. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12275. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  12276. ;; How to do this: Measure non-white length of current string
  12277. ;; If equal to column width, we should realign.
  12278. (defun org-remap (map &rest commands)
  12279. "In MAP, remap the functions given in COMMANDS.
  12280. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12281. (let (new old)
  12282. (while commands
  12283. (setq old (pop commands) new (pop commands))
  12284. (if (fboundp 'command-remapping)
  12285. (org-defkey map (vector 'remap old) new)
  12286. (substitute-key-definition old new map global-map)))))
  12287. (when (eq org-enable-table-editor 'optimized)
  12288. ;; If the user wants maximum table support, we need to hijack
  12289. ;; some standard editing functions
  12290. (org-remap org-mode-map
  12291. 'self-insert-command 'org-self-insert-command
  12292. 'delete-char 'org-delete-char
  12293. 'delete-backward-char 'org-delete-backward-char)
  12294. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12295. (defun org-shiftcursor-error ()
  12296. "Throw an error because Shift-Cursor command was applied in wrong context."
  12297. (error "This command is active in special context like tables, headlines or timestamps"))
  12298. (defun org-shifttab (&optional arg)
  12299. "Global visibility cycling or move to previous table field.
  12300. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12301. on context.
  12302. See the individual commands for more information."
  12303. (interactive "P")
  12304. (cond
  12305. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12306. (arg (message "Content view to level: ")
  12307. (org-content (prefix-numeric-value arg))
  12308. (setq org-cycle-global-status 'overview))
  12309. (t (call-interactively 'org-global-cycle))))
  12310. (defun org-shiftmetaleft ()
  12311. "Promote subtree or delete table column.
  12312. Calls `org-promote-subtree', `org-outdent-item',
  12313. or `org-table-delete-column', depending on context.
  12314. See the individual commands for more information."
  12315. (interactive)
  12316. (cond
  12317. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12318. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12319. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12320. (t (org-shiftcursor-error))))
  12321. (defun org-shiftmetaright ()
  12322. "Demote subtree or insert table column.
  12323. Calls `org-demote-subtree', `org-indent-item',
  12324. or `org-table-insert-column', depending on context.
  12325. See the individual commands for more information."
  12326. (interactive)
  12327. (cond
  12328. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12329. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12330. ((org-at-item-p) (call-interactively 'org-indent-item))
  12331. (t (org-shiftcursor-error))))
  12332. (defun org-shiftmetaup (&optional arg)
  12333. "Move subtree up or kill table row.
  12334. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12335. `org-move-item-up' depending on context. See the individual commands
  12336. for more information."
  12337. (interactive "P")
  12338. (cond
  12339. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12340. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12341. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12342. (t (org-shiftcursor-error))))
  12343. (defun org-shiftmetadown (&optional arg)
  12344. "Move subtree down or insert table row.
  12345. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12346. `org-move-item-down', depending on context. See the individual
  12347. commands for more information."
  12348. (interactive "P")
  12349. (cond
  12350. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12351. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12352. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12353. (t (org-shiftcursor-error))))
  12354. (defun org-metaleft (&optional arg)
  12355. "Promote heading or move table column to left.
  12356. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12357. With no specific context, calls the Emacs default `backward-word'.
  12358. See the individual commands for more information."
  12359. (interactive "P")
  12360. (cond
  12361. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12362. ((or (org-on-heading-p) (org-region-active-p))
  12363. (call-interactively 'org-do-promote))
  12364. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12365. (t (call-interactively 'backward-word))))
  12366. (defun org-metaright (&optional arg)
  12367. "Demote subtree or move table column to right.
  12368. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12369. With no specific context, calls the Emacs default `forward-word'.
  12370. See the individual commands for more information."
  12371. (interactive "P")
  12372. (cond
  12373. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12374. ((or (org-on-heading-p) (org-region-active-p))
  12375. (call-interactively 'org-do-demote))
  12376. ((org-at-item-p) (call-interactively 'org-indent-item))
  12377. (t (call-interactively 'forward-word))))
  12378. (defun org-metaup (&optional arg)
  12379. "Move subtree up or move table row up.
  12380. Calls `org-move-subtree-up' or `org-table-move-row' or
  12381. `org-move-item-up', depending on context. See the individual commands
  12382. for more information."
  12383. (interactive "P")
  12384. (cond
  12385. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12386. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12387. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12388. (t (transpose-lines 1) (beginning-of-line -1))))
  12389. (defun org-metadown (&optional arg)
  12390. "Move subtree down or move table row down.
  12391. Calls `org-move-subtree-down' or `org-table-move-row' or
  12392. `org-move-item-down', depending on context. See the individual
  12393. commands for more information."
  12394. (interactive "P")
  12395. (cond
  12396. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12397. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12398. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12399. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12400. (defun org-shiftup (&optional arg)
  12401. "Increase item in timestamp or increase priority of current headline.
  12402. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12403. depending on context. See the individual commands for more information."
  12404. (interactive "P")
  12405. (cond
  12406. ((org-at-timestamp-p t)
  12407. (call-interactively (if org-edit-timestamp-down-means-later
  12408. 'org-timestamp-down 'org-timestamp-up)))
  12409. ((org-on-heading-p) (call-interactively 'org-priority-up))
  12410. ((org-at-item-p) (call-interactively 'org-previous-item))
  12411. ((org-clocktable-try-shift 'up arg))
  12412. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  12413. (defun org-shiftdown (&optional arg)
  12414. "Decrease item in timestamp or decrease priority of current headline.
  12415. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12416. depending on context. See the individual commands for more information."
  12417. (interactive "P")
  12418. (cond
  12419. ((org-at-timestamp-p t)
  12420. (call-interactively (if org-edit-timestamp-down-means-later
  12421. 'org-timestamp-up 'org-timestamp-down)))
  12422. ((org-on-heading-p) (call-interactively 'org-priority-down))
  12423. ((org-clocktable-try-shift 'down arg))
  12424. (t (call-interactively 'org-next-item))))
  12425. (defun org-shiftright (&optional arg)
  12426. "Next TODO keyword or timestamp one day later, depending on context."
  12427. (interactive "P")
  12428. (cond
  12429. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12430. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  12431. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  12432. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  12433. ((org-clocktable-try-shift 'right arg))
  12434. (t (org-shiftcursor-error))))
  12435. (defun org-shiftleft (&optional arg)
  12436. "Previous TODO keyword or timestamp one day earlier, depending on context."
  12437. (interactive "P")
  12438. (cond
  12439. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12440. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  12441. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12442. ((org-at-property-p)
  12443. (call-interactively 'org-property-previous-allowed-value))
  12444. ((org-clocktable-try-shift 'left arg))
  12445. (t (org-shiftcursor-error))))
  12446. (defun org-shiftcontrolright ()
  12447. "Switch to next TODO set."
  12448. (interactive)
  12449. (cond
  12450. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  12451. (t (org-shiftcursor-error))))
  12452. (defun org-shiftcontrolleft ()
  12453. "Switch to previous TODO set."
  12454. (interactive)
  12455. (cond
  12456. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  12457. (t (org-shiftcursor-error))))
  12458. (defun org-ctrl-c-ret ()
  12459. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12460. (interactive)
  12461. (cond
  12462. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12463. (t (call-interactively 'org-insert-heading))))
  12464. (defun org-copy-special ()
  12465. "Copy region in table or copy current subtree.
  12466. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12467. See the individual commands for more information."
  12468. (interactive)
  12469. (call-interactively
  12470. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12471. (defun org-cut-special ()
  12472. "Cut region in table or cut current subtree.
  12473. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12474. See the individual commands for more information."
  12475. (interactive)
  12476. (call-interactively
  12477. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12478. (defun org-paste-special (arg)
  12479. "Paste rectangular region into table, or past subtree relative to level.
  12480. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12481. See the individual commands for more information."
  12482. (interactive "P")
  12483. (if (org-at-table-p)
  12484. (org-table-paste-rectangle)
  12485. (org-paste-subtree arg)))
  12486. (defun org-ctrl-c-ctrl-c (&optional arg)
  12487. "Set tags in headline, or update according to changed information at point.
  12488. This command does many different things, depending on context:
  12489. - If the cursor is in a headline, prompt for tags and insert them
  12490. into the current line, aligned to `org-tags-column'. When called
  12491. with prefix arg, realign all tags in the current buffer.
  12492. - If the cursor is in one of the special #+KEYWORD lines, this
  12493. triggers scanning the buffer for these lines and updating the
  12494. information.
  12495. - If the cursor is inside a table, realign the table. This command
  12496. works even if the automatic table editor has been turned off.
  12497. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12498. the entire table.
  12499. - If the cursor is a the beginning of a dynamic block, update it.
  12500. - If the cursor is inside a table created by the table.el package,
  12501. activate that table.
  12502. - If the current buffer is a remember buffer, close note and file it.
  12503. with a prefix argument, file it without further interaction to the default
  12504. location.
  12505. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12506. links in this buffer.
  12507. - If the cursor is on a numbered item in a plain list, renumber the
  12508. ordered list.
  12509. - If the cursor is on a checkbox, toggle it."
  12510. (interactive "P")
  12511. (let ((org-enable-table-editor t))
  12512. (cond
  12513. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12514. org-occur-highlights
  12515. org-latex-fragment-image-overlays)
  12516. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  12517. (org-remove-occur-highlights)
  12518. (org-remove-latex-fragment-image-overlays)
  12519. (message "Temporary highlights/overlays removed from current buffer"))
  12520. ((and (local-variable-p 'org-finish-function (current-buffer))
  12521. (fboundp org-finish-function))
  12522. (funcall org-finish-function))
  12523. ((org-at-property-p)
  12524. (call-interactively 'org-property-action))
  12525. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12526. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12527. ((org-at-table.el-p)
  12528. (require 'table)
  12529. (beginning-of-line 1)
  12530. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12531. (call-interactively 'table-recognize-table))
  12532. ((org-at-table-p)
  12533. (org-table-maybe-eval-formula)
  12534. (if arg
  12535. (call-interactively 'org-table-recalculate)
  12536. (org-table-maybe-recalculate-line))
  12537. (call-interactively 'org-table-align))
  12538. ((org-at-item-checkbox-p)
  12539. (call-interactively 'org-toggle-checkbox))
  12540. ((org-at-item-p)
  12541. (call-interactively 'org-maybe-renumber-ordered-list))
  12542. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12543. ;; Dynamic block
  12544. (beginning-of-line 1)
  12545. (org-update-dblock))
  12546. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12547. (cond
  12548. ((equal (match-string 1) "TBLFM")
  12549. ;; Recalculate the table before this line
  12550. (save-excursion
  12551. (beginning-of-line 1)
  12552. (skip-chars-backward " \r\n\t")
  12553. (if (org-at-table-p)
  12554. (org-call-with-arg 'org-table-recalculate t))))
  12555. (t
  12556. (call-interactively 'org-mode-restart))))
  12557. (t (error "C-c C-c can do nothing useful at this location.")))))
  12558. (defun org-mode-restart ()
  12559. "Restart Org-mode, to scan again for special lines.
  12560. Also updates the keyword regular expressions."
  12561. (interactive)
  12562. (let ((org-inhibit-startup t)) (org-mode))
  12563. (message "Org-mode restarted to refresh keyword and special line setup"))
  12564. (defun org-kill-note-or-show-branches ()
  12565. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12566. (interactive)
  12567. (if (not org-finish-function)
  12568. (call-interactively 'show-branches)
  12569. (let ((org-note-abort t))
  12570. (funcall org-finish-function))))
  12571. (defun org-return (&optional indent)
  12572. "Goto next table row or insert a newline.
  12573. Calls `org-table-next-row' or `newline', depending on context.
  12574. See the individual commands for more information."
  12575. (interactive)
  12576. (cond
  12577. ((bobp) (if indent (newline-and-indent) (newline)))
  12578. ((and (org-at-heading-p)
  12579. (looking-at
  12580. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12581. (org-show-entry)
  12582. (end-of-line 1)
  12583. (newline))
  12584. ((org-at-table-p)
  12585. (org-table-justify-field-maybe)
  12586. (call-interactively 'org-table-next-row))
  12587. (t (if indent (newline-and-indent) (newline)))))
  12588. (defun org-return-indent ()
  12589. "Goto next table row or insert a newline and indent.
  12590. Calls `org-table-next-row' or `newline-and-indent', depending on
  12591. context. See the individual commands for more information."
  12592. (interactive)
  12593. (org-return t))
  12594. (defun org-ctrl-c-star ()
  12595. "Compute table, or change heading status of lines.
  12596. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  12597. depending on context. This will also turn a plain list item or a normal
  12598. line into a subheading."
  12599. (interactive)
  12600. (cond
  12601. ((org-at-table-p)
  12602. (call-interactively 'org-table-recalculate))
  12603. ((org-region-active-p)
  12604. ;; Convert all lines in region to list items
  12605. (call-interactively 'org-toggle-region-headings))
  12606. ((org-on-heading-p)
  12607. (org-toggle-region-headings (point-at-bol)
  12608. (min (1+ (point-at-eol)) (point-max))))
  12609. ((org-at-item-p)
  12610. ;; Convert to heading
  12611. (let ((level (save-match-data
  12612. (save-excursion
  12613. (condition-case nil
  12614. (progn
  12615. (org-back-to-heading t)
  12616. (funcall outline-level))
  12617. (error 0))))))
  12618. (replace-match
  12619. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12620. (t (org-toggle-region-headings (point-at-bol)
  12621. (min (1+ (point-at-eol)) (point-max))))))
  12622. (defun org-ctrl-c-minus ()
  12623. "Insert separator line in table or modify bullet status of line.
  12624. Also turns a plain line or a region of lines into list items.
  12625. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12626. `org-cycle-list-bullet', depending on context."
  12627. (interactive)
  12628. (cond
  12629. ((org-at-table-p)
  12630. (call-interactively 'org-table-insert-hline))
  12631. ((org-on-heading-p)
  12632. ;; Convert to item
  12633. (save-excursion
  12634. (beginning-of-line 1)
  12635. (if (looking-at "\\*+ ")
  12636. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  12637. ((org-region-active-p)
  12638. ;; Convert all lines in region to list items
  12639. (call-interactively 'org-toggle-region-items))
  12640. ((org-in-item-p)
  12641. (call-interactively 'org-cycle-list-bullet))
  12642. (t (org-toggle-region-items (point-at-bol)
  12643. (min (1+ (point-at-eol)) (point-max))))))
  12644. (defun org-toggle-region-items (beg end)
  12645. "Convert all lines in region to list items.
  12646. If the first line is already an item, convert all list items in the region
  12647. to normal lines."
  12648. (interactive "r")
  12649. (let (l2 l)
  12650. (save-excursion
  12651. (goto-char end)
  12652. (setq l2 (org-current-line))
  12653. (goto-char beg)
  12654. (beginning-of-line 1)
  12655. (setq l (1- (org-current-line)))
  12656. (if (org-at-item-p)
  12657. ;; We already have items, de-itemize
  12658. (while (< (setq l (1+ l)) l2)
  12659. (when (org-at-item-p)
  12660. (goto-char (match-beginning 2))
  12661. (delete-region (match-beginning 2) (match-end 2))
  12662. (and (looking-at "[ \t]+") (replace-match "")))
  12663. (beginning-of-line 2))
  12664. (while (< (setq l (1+ l)) l2)
  12665. (unless (org-at-item-p)
  12666. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12667. (replace-match "\\1- \\2")))
  12668. (beginning-of-line 2))))))
  12669. (defun org-toggle-region-headings (beg end)
  12670. "Convert all lines in region to list items.
  12671. If the first line is already an item, convert all list items in the region
  12672. to normal lines."
  12673. (interactive "r")
  12674. (let (l2 l)
  12675. (save-excursion
  12676. (goto-char end)
  12677. (setq l2 (org-current-line))
  12678. (goto-char beg)
  12679. (beginning-of-line 1)
  12680. (setq l (1- (org-current-line)))
  12681. (if (org-on-heading-p)
  12682. ;; We already have headlines, de-star them
  12683. (while (< (setq l (1+ l)) l2)
  12684. (when (org-on-heading-p t)
  12685. (and (looking-at outline-regexp) (replace-match "")))
  12686. (beginning-of-line 2))
  12687. (let* ((stars (save-excursion
  12688. (re-search-backward org-complex-heading-regexp nil t)
  12689. (or (match-string 1) "*")))
  12690. (add-stars (if org-odd-levels-only "**" "*"))
  12691. (rpl (concat stars add-stars " \\2")))
  12692. (while (< (setq l (1+ l)) l2)
  12693. (unless (org-on-heading-p)
  12694. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12695. (replace-match rpl)))
  12696. (beginning-of-line 2)))))))
  12697. (defun org-meta-return (&optional arg)
  12698. "Insert a new heading or wrap a region in a table.
  12699. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12700. See the individual commands for more information."
  12701. (interactive "P")
  12702. (cond
  12703. ((org-at-table-p)
  12704. (call-interactively 'org-table-wrap-region))
  12705. (t (call-interactively 'org-insert-heading))))
  12706. ;;; Menu entries
  12707. ;; Define the Org-mode menus
  12708. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12709. '("Tbl"
  12710. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  12711. ["Next Field" org-cycle (org-at-table-p)]
  12712. ["Previous Field" org-shifttab (org-at-table-p)]
  12713. ["Next Row" org-return (org-at-table-p)]
  12714. "--"
  12715. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12716. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12717. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12718. "--"
  12719. ("Column"
  12720. ["Move Column Left" org-metaleft (org-at-table-p)]
  12721. ["Move Column Right" org-metaright (org-at-table-p)]
  12722. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12723. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12724. ("Row"
  12725. ["Move Row Up" org-metaup (org-at-table-p)]
  12726. ["Move Row Down" org-metadown (org-at-table-p)]
  12727. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12728. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12729. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12730. "--"
  12731. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12732. ("Rectangle"
  12733. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12734. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12735. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12736. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12737. "--"
  12738. ("Calculate"
  12739. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12740. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12741. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  12742. "--"
  12743. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12744. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12745. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12746. "--"
  12747. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12748. "--"
  12749. ["Sum Column/Rectangle" org-table-sum
  12750. (or (org-at-table-p) (org-region-active-p))]
  12751. ["Which Column?" org-table-current-column (org-at-table-p)])
  12752. ["Debug Formulas"
  12753. org-table-toggle-formula-debugger
  12754. :style toggle :selected org-table-formula-debug]
  12755. ["Show Col/Row Numbers"
  12756. org-table-toggle-coordinate-overlays
  12757. :style toggle :selected org-table-overlay-coordinates]
  12758. "--"
  12759. ["Create" org-table-create (and (not (org-at-table-p))
  12760. org-enable-table-editor)]
  12761. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12762. ["Import from File" org-table-import (not (org-at-table-p))]
  12763. ["Export to File" org-table-export (org-at-table-p)]
  12764. "--"
  12765. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12766. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12767. '("Org"
  12768. ("Show/Hide"
  12769. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  12770. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  12771. ["Sparse Tree" org-occur t]
  12772. ["Reveal Context" org-reveal t]
  12773. ["Show All" show-all t]
  12774. "--"
  12775. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12776. "--"
  12777. ["New Heading" org-insert-heading t]
  12778. ("Navigate Headings"
  12779. ["Up" outline-up-heading t]
  12780. ["Next" outline-next-visible-heading t]
  12781. ["Previous" outline-previous-visible-heading t]
  12782. ["Next Same Level" outline-forward-same-level t]
  12783. ["Previous Same Level" outline-backward-same-level t]
  12784. "--"
  12785. ["Jump" org-goto t])
  12786. ("Edit Structure"
  12787. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12788. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12789. "--"
  12790. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12791. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12792. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12793. "--"
  12794. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12795. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12796. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12797. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12798. "--"
  12799. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12800. "--"
  12801. ["Convert to odd levels" org-convert-to-odd-levels t]
  12802. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12803. ("Editing"
  12804. ["Emphasis..." org-emphasize t])
  12805. ("Archive"
  12806. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12807. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12808. ; :active t :keys "C-u C-c C-x C-a"]
  12809. ["Sparse trees open ARCHIVE trees"
  12810. (setq org-sparse-tree-open-archived-trees
  12811. (not org-sparse-tree-open-archived-trees))
  12812. :style toggle :selected org-sparse-tree-open-archived-trees]
  12813. ["Cycling opens ARCHIVE trees"
  12814. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12815. :style toggle :selected org-cycle-open-archived-trees]
  12816. ["Agenda includes ARCHIVE trees"
  12817. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  12818. :style toggle :selected (not org-agenda-skip-archived-trees)]
  12819. "--"
  12820. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12821. ; ["Check and Move Children" (org-archive-subtree '(4))
  12822. ; :active t :keys "C-u C-c C-x C-s"]
  12823. )
  12824. "--"
  12825. ("TODO Lists"
  12826. ["TODO/DONE/-" org-todo t]
  12827. ("Select keyword"
  12828. ["Next keyword" org-shiftright (org-on-heading-p)]
  12829. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12830. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12831. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12832. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12833. ["Show TODO Tree" org-show-todo-tree t]
  12834. ["Global TODO list" org-todo-list t]
  12835. "--"
  12836. ["Set Priority" org-priority t]
  12837. ["Priority Up" org-shiftup t]
  12838. ["Priority Down" org-shiftdown t])
  12839. ("TAGS and Properties"
  12840. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  12841. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12842. "--"
  12843. ["Set property" 'org-set-property t]
  12844. ["Column view of properties" org-columns t]
  12845. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12846. ("Dates and Scheduling"
  12847. ["Timestamp" org-time-stamp t]
  12848. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12849. ("Change Date"
  12850. ["1 Day Later" org-shiftright t]
  12851. ["1 Day Earlier" org-shiftleft t]
  12852. ["1 ... Later" org-shiftup t]
  12853. ["1 ... Earlier" org-shiftdown t])
  12854. ["Compute Time Range" org-evaluate-time-range t]
  12855. ["Schedule Item" org-schedule t]
  12856. ["Deadline" org-deadline t]
  12857. "--"
  12858. ["Custom time format" org-toggle-time-stamp-overlays
  12859. :style radio :selected org-display-custom-times]
  12860. "--"
  12861. ["Goto Calendar" org-goto-calendar t]
  12862. ["Date from Calendar" org-date-from-calendar t])
  12863. ("Logging work"
  12864. ["Clock in" org-clock-in t]
  12865. ["Clock out" org-clock-out t]
  12866. ["Clock cancel" org-clock-cancel t]
  12867. ["Goto running clock" org-clock-goto t]
  12868. ["Display times" org-clock-display t]
  12869. ["Create clock table" org-clock-report t]
  12870. "--"
  12871. ["Record DONE time"
  12872. (progn (setq org-log-done (not org-log-done))
  12873. (message "Switching to %s will %s record a timestamp"
  12874. (car org-done-keywords)
  12875. (if org-log-done "automatically" "not")))
  12876. :style toggle :selected org-log-done])
  12877. "--"
  12878. ["Agenda Command..." org-agenda t]
  12879. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12880. ("File List for Agenda")
  12881. ("Special views current file"
  12882. ["TODO Tree" org-show-todo-tree t]
  12883. ["Check Deadlines" org-check-deadlines t]
  12884. ["Timeline" org-timeline t]
  12885. ["Tags Tree" org-tags-sparse-tree t])
  12886. "--"
  12887. ("Hyperlinks"
  12888. ["Store Link (Global)" org-store-link t]
  12889. ["Insert Link" org-insert-link t]
  12890. ["Follow Link" org-open-at-point t]
  12891. "--"
  12892. ["Next link" org-next-link t]
  12893. ["Previous link" org-previous-link t]
  12894. "--"
  12895. ["Descriptive Links"
  12896. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12897. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  12898. ["Literal Links"
  12899. (progn
  12900. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12901. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  12902. "--"
  12903. ["Export/Publish..." org-export t]
  12904. ("LaTeX"
  12905. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12906. :selected org-cdlatex-mode]
  12907. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12908. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12909. ["Modify math symbol" org-cdlatex-math-modify
  12910. (org-inside-LaTeX-fragment-p)]
  12911. ["Export LaTeX fragments as images"
  12912. (if (featurep 'org-exp)
  12913. (setq org-export-with-LaTeX-fragments
  12914. (not org-export-with-LaTeX-fragments))
  12915. (require 'org-exp))
  12916. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12917. org-export-with-LaTeX-fragments)])
  12918. "--"
  12919. ("Documentation"
  12920. ["Show Version" org-version t]
  12921. ["Info Documentation" org-info t])
  12922. ("Customize"
  12923. ["Browse Org Group" org-customize t]
  12924. "--"
  12925. ["Expand This Menu" org-create-customize-menu
  12926. (fboundp 'customize-menu-create)])
  12927. "--"
  12928. ["Refresh setup" org-mode-restart t]
  12929. ))
  12930. (defun org-info (&optional node)
  12931. "Read documentation for Org-mode in the info system.
  12932. With optional NODE, go directly to that node."
  12933. (interactive)
  12934. (info (format "(org)%s" (or node ""))))
  12935. (defun org-install-agenda-files-menu ()
  12936. (let ((bl (buffer-list)))
  12937. (save-excursion
  12938. (while bl
  12939. (set-buffer (pop bl))
  12940. (if (org-mode-p) (setq bl nil)))
  12941. (when (org-mode-p)
  12942. (easy-menu-change
  12943. '("Org") "File List for Agenda"
  12944. (append
  12945. (list
  12946. ["Edit File List" (org-edit-agenda-file-list) t]
  12947. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12948. ["Remove Current File from List" org-remove-file t]
  12949. ["Cycle through agenda files" org-cycle-agenda-files t]
  12950. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12951. "--")
  12952. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12953. ;;;; Documentation
  12954. (defun org-customize ()
  12955. "Call the customize function with org as argument."
  12956. (interactive)
  12957. (customize-browse 'org))
  12958. (defun org-create-customize-menu ()
  12959. "Create a full customization menu for Org-mode, insert it into the menu."
  12960. (interactive)
  12961. (if (fboundp 'customize-menu-create)
  12962. (progn
  12963. (easy-menu-change
  12964. '("Org") "Customize"
  12965. `(["Browse Org group" org-customize t]
  12966. "--"
  12967. ,(customize-menu-create 'org)
  12968. ["Set" Custom-set t]
  12969. ["Save" Custom-save t]
  12970. ["Reset to Current" Custom-reset-current t]
  12971. ["Reset to Saved" Custom-reset-saved t]
  12972. ["Reset to Standard Settings" Custom-reset-standard t]))
  12973. (message "\"Org\"-menu now contains full customization menu"))
  12974. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12975. ;;;; Miscellaneous stuff
  12976. ;;; Generally useful functions
  12977. (defun org-force-self-insert (N)
  12978. "Needed to enforce self-insert under remapping."
  12979. (interactive "p")
  12980. (self-insert-command N))
  12981. (defun org-string-width (s)
  12982. "Compute width of string, ignoring invisible characters.
  12983. This ignores character with invisibility property `org-link', and also
  12984. characters with property `org-cwidth', because these will become invisible
  12985. upon the next fontification round."
  12986. (let (b l)
  12987. (when (or (eq t buffer-invisibility-spec)
  12988. (assq 'org-link buffer-invisibility-spec))
  12989. (while (setq b (text-property-any 0 (length s)
  12990. 'invisible 'org-link s))
  12991. (setq s (concat (substring s 0 b)
  12992. (substring s (or (next-single-property-change
  12993. b 'invisible s) (length s)))))))
  12994. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12995. (setq s (concat (substring s 0 b)
  12996. (substring s (or (next-single-property-change
  12997. b 'org-cwidth s) (length s))))))
  12998. (setq l (string-width s) b -1)
  12999. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13000. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13001. l))
  13002. (defun org-trim (s)
  13003. "Remove whitespace at beginning and end of string."
  13004. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13005. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13006. s)
  13007. (defun org-wrap (string &optional width lines)
  13008. "Wrap string to either a number of lines, or a width in characters.
  13009. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13010. that costs. If there is a word longer than WIDTH, the text is actually
  13011. wrapped to the length of that word.
  13012. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13013. many lines, whatever width that takes.
  13014. The return value is a list of lines, without newlines at the end."
  13015. (let* ((words (org-split-string string "[ \t\n]+"))
  13016. (maxword (apply 'max (mapcar 'org-string-width words)))
  13017. w ll)
  13018. (cond (width
  13019. (org-do-wrap words (max maxword width)))
  13020. (lines
  13021. (setq w maxword)
  13022. (setq ll (org-do-wrap words maxword))
  13023. (if (<= (length ll) lines)
  13024. ll
  13025. (setq ll words)
  13026. (while (> (length ll) lines)
  13027. (setq w (1+ w))
  13028. (setq ll (org-do-wrap words w)))
  13029. ll))
  13030. (t (error "Cannot wrap this")))))
  13031. (defun org-do-wrap (words width)
  13032. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13033. (let (lines line)
  13034. (while words
  13035. (setq line (pop words))
  13036. (while (and words (< (+ (length line) (length (car words))) width))
  13037. (setq line (concat line " " (pop words))))
  13038. (setq lines (push line lines)))
  13039. (nreverse lines)))
  13040. (defun org-split-string (string &optional separators)
  13041. "Splits STRING into substrings at SEPARATORS.
  13042. No empty strings are returned if there are matches at the beginning
  13043. and end of string."
  13044. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13045. (start 0)
  13046. notfirst
  13047. (list nil))
  13048. (while (and (string-match rexp string
  13049. (if (and notfirst
  13050. (= start (match-beginning 0))
  13051. (< start (length string)))
  13052. (1+ start) start))
  13053. (< (match-beginning 0) (length string)))
  13054. (setq notfirst t)
  13055. (or (eq (match-beginning 0) 0)
  13056. (and (eq (match-beginning 0) (match-end 0))
  13057. (eq (match-beginning 0) start))
  13058. (setq list
  13059. (cons (substring string start (match-beginning 0))
  13060. list)))
  13061. (setq start (match-end 0)))
  13062. (or (eq start (length string))
  13063. (setq list
  13064. (cons (substring string start)
  13065. list)))
  13066. (nreverse list)))
  13067. (defun org-context ()
  13068. "Return a list of contexts of the current cursor position.
  13069. If several contexts apply, all are returned.
  13070. Each context entry is a list with a symbol naming the context, and
  13071. two positions indicating start and end of the context. Possible
  13072. contexts are:
  13073. :headline anywhere in a headline
  13074. :headline-stars on the leading stars in a headline
  13075. :todo-keyword on a TODO keyword (including DONE) in a headline
  13076. :tags on the TAGS in a headline
  13077. :priority on the priority cookie in a headline
  13078. :item on the first line of a plain list item
  13079. :item-bullet on the bullet/number of a plain list item
  13080. :checkbox on the checkbox in a plain list item
  13081. :table in an org-mode table
  13082. :table-special on a special filed in a table
  13083. :table-table in a table.el table
  13084. :link on a hyperlink
  13085. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13086. :target on a <<target>>
  13087. :radio-target on a <<<radio-target>>>
  13088. :latex-fragment on a LaTeX fragment
  13089. :latex-preview on a LaTeX fragment with overlayed preview image
  13090. This function expects the position to be visible because it uses font-lock
  13091. faces as a help to recognize the following contexts: :table-special, :link,
  13092. and :keyword."
  13093. (let* ((f (get-text-property (point) 'face))
  13094. (faces (if (listp f) f (list f)))
  13095. (p (point)) clist o)
  13096. ;; First the large context
  13097. (cond
  13098. ((org-on-heading-p t)
  13099. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13100. (when (progn
  13101. (beginning-of-line 1)
  13102. (looking-at org-todo-line-tags-regexp))
  13103. (push (org-point-in-group p 1 :headline-stars) clist)
  13104. (push (org-point-in-group p 2 :todo-keyword) clist)
  13105. (push (org-point-in-group p 4 :tags) clist))
  13106. (goto-char p)
  13107. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13108. (if (looking-at "\\[#[A-Z0-9]\\]")
  13109. (push (org-point-in-group p 0 :priority) clist)))
  13110. ((org-at-item-p)
  13111. (push (org-point-in-group p 2 :item-bullet) clist)
  13112. (push (list :item (point-at-bol)
  13113. (save-excursion (org-end-of-item) (point)))
  13114. clist)
  13115. (and (org-at-item-checkbox-p)
  13116. (push (org-point-in-group p 0 :checkbox) clist)))
  13117. ((org-at-table-p)
  13118. (push (list :table (org-table-begin) (org-table-end)) clist)
  13119. (if (memq 'org-formula faces)
  13120. (push (list :table-special
  13121. (previous-single-property-change p 'face)
  13122. (next-single-property-change p 'face)) clist)))
  13123. ((org-at-table-p 'any)
  13124. (push (list :table-table) clist)))
  13125. (goto-char p)
  13126. ;; Now the small context
  13127. (cond
  13128. ((org-at-timestamp-p)
  13129. (push (org-point-in-group p 0 :timestamp) clist))
  13130. ((memq 'org-link faces)
  13131. (push (list :link
  13132. (previous-single-property-change p 'face)
  13133. (next-single-property-change p 'face)) clist))
  13134. ((memq 'org-special-keyword faces)
  13135. (push (list :keyword
  13136. (previous-single-property-change p 'face)
  13137. (next-single-property-change p 'face)) clist))
  13138. ((org-on-target-p)
  13139. (push (org-point-in-group p 0 :target) clist)
  13140. (goto-char (1- (match-beginning 0)))
  13141. (if (looking-at org-radio-target-regexp)
  13142. (push (org-point-in-group p 0 :radio-target) clist))
  13143. (goto-char p))
  13144. ((setq o (car (delq nil
  13145. (mapcar
  13146. (lambda (x)
  13147. (if (memq x org-latex-fragment-image-overlays) x))
  13148. (org-overlays-at (point))))))
  13149. (push (list :latex-fragment
  13150. (org-overlay-start o) (org-overlay-end o)) clist)
  13151. (push (list :latex-preview
  13152. (org-overlay-start o) (org-overlay-end o)) clist))
  13153. ((org-inside-LaTeX-fragment-p)
  13154. ;; FIXME: positions wrong.
  13155. (push (list :latex-fragment (point) (point)) clist)))
  13156. (setq clist (nreverse (delq nil clist)))
  13157. clist))
  13158. ;; FIXME: Compare with at-regexp-p Do we need both?
  13159. (defun org-in-regexp (re &optional nlines visually)
  13160. "Check if point is inside a match of regexp.
  13161. Normally only the current line is checked, but you can include NLINES extra
  13162. lines both before and after point into the search.
  13163. If VISUALLY is set, require that the cursor is not after the match but
  13164. really on, so that the block visually is on the match."
  13165. (catch 'exit
  13166. (let ((pos (point))
  13167. (eol (point-at-eol (+ 1 (or nlines 0))))
  13168. (inc (if visually 1 0)))
  13169. (save-excursion
  13170. (beginning-of-line (- 1 (or nlines 0)))
  13171. (while (re-search-forward re eol t)
  13172. (if (and (<= (match-beginning 0) pos)
  13173. (>= (+ inc (match-end 0)) pos))
  13174. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13175. (defun org-at-regexp-p (regexp)
  13176. "Is point inside a match of REGEXP in the current line?"
  13177. (catch 'exit
  13178. (save-excursion
  13179. (let ((pos (point)) (end (point-at-eol)))
  13180. (beginning-of-line 1)
  13181. (while (re-search-forward regexp end t)
  13182. (if (and (<= (match-beginning 0) pos)
  13183. (>= (match-end 0) pos))
  13184. (throw 'exit t)))
  13185. nil))))
  13186. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13187. "Call `multi-occur' with buffers for all agenda files."
  13188. (interactive "sOrg-files matching: \np")
  13189. (let* ((files (org-agenda-files))
  13190. (tnames (mapcar 'file-truename files))
  13191. (extra org-agenda-text-search-extra-files)
  13192. f)
  13193. (while (setq f (pop extra))
  13194. (unless (member (file-truename f) tnames)
  13195. (add-to-list 'files f 'append)
  13196. (add-to-list 'tnames (file-truename f) 'append)))
  13197. (multi-occur
  13198. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13199. regexp)))
  13200. (if (boundp 'occur-mode-find-occurrence-hook)
  13201. ;; Emacs 23
  13202. (add-hook 'occur-mode-find-occurrence-hook
  13203. (lambda ()
  13204. (when (org-mode-p)
  13205. (org-reveal))))
  13206. ;; Emacs 22
  13207. (defadvice occur-mode-goto-occurrence
  13208. (after org-occur-reveal activate)
  13209. (and (org-mode-p) (org-reveal)))
  13210. (defadvice occur-mode-goto-occurrence-other-window
  13211. (after org-occur-reveal activate)
  13212. (and (org-mode-p) (org-reveal)))
  13213. (defadvice occur-mode-display-occurrence
  13214. (after org-occur-reveal activate)
  13215. (when (org-mode-p)
  13216. (let ((pos (occur-mode-find-occurrence)))
  13217. (with-current-buffer (marker-buffer pos)
  13218. (save-excursion
  13219. (goto-char pos)
  13220. (org-reveal)))))))
  13221. (defun org-uniquify (list)
  13222. "Remove duplicate elements from LIST."
  13223. (let (res)
  13224. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13225. res))
  13226. (defun org-delete-all (elts list)
  13227. "Remove all elements in ELTS from LIST."
  13228. (while elts
  13229. (setq list (delete (pop elts) list)))
  13230. list)
  13231. (defun org-back-over-empty-lines ()
  13232. "Move backwards over witespace, to the beginning of the first empty line.
  13233. Returns the number of empty lines passed."
  13234. (let ((pos (point)))
  13235. (skip-chars-backward " \t\n\r")
  13236. (beginning-of-line 2)
  13237. (goto-char (min (point) pos))
  13238. (count-lines (point) pos)))
  13239. (defun org-skip-whitespace ()
  13240. (skip-chars-forward " \t\n\r"))
  13241. (defun org-point-in-group (point group &optional context)
  13242. "Check if POINT is in match-group GROUP.
  13243. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13244. match. If the match group does ot exist or point is not inside it,
  13245. return nil."
  13246. (and (match-beginning group)
  13247. (>= point (match-beginning group))
  13248. (<= point (match-end group))
  13249. (if context
  13250. (list context (match-beginning group) (match-end group))
  13251. t)))
  13252. (defun org-switch-to-buffer-other-window (&rest args)
  13253. "Switch to buffer in a second window on the current frame.
  13254. In particular, do not allow pop-up frames."
  13255. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13256. special-display-function)
  13257. (apply 'switch-to-buffer-other-window args)))
  13258. (defun org-combine-plists (&rest plists)
  13259. "Create a single property list from all plists in PLISTS.
  13260. The process starts by copying the first list, and then setting properties
  13261. from the other lists. Settings in the last list are the most significant
  13262. ones and overrule settings in the other lists."
  13263. (let ((rtn (copy-sequence (pop plists)))
  13264. p v ls)
  13265. (while plists
  13266. (setq ls (pop plists))
  13267. (while ls
  13268. (setq p (pop ls) v (pop ls))
  13269. (setq rtn (plist-put rtn p v))))
  13270. rtn))
  13271. (defun org-move-line-down (arg)
  13272. "Move the current line down. With prefix argument, move it past ARG lines."
  13273. (interactive "p")
  13274. (let ((col (current-column))
  13275. beg end pos)
  13276. (beginning-of-line 1) (setq beg (point))
  13277. (beginning-of-line 2) (setq end (point))
  13278. (beginning-of-line (+ 1 arg))
  13279. (setq pos (move-marker (make-marker) (point)))
  13280. (insert (delete-and-extract-region beg end))
  13281. (goto-char pos)
  13282. (move-to-column col)))
  13283. (defun org-move-line-up (arg)
  13284. "Move the current line up. With prefix argument, move it past ARG lines."
  13285. (interactive "p")
  13286. (let ((col (current-column))
  13287. beg end pos)
  13288. (beginning-of-line 1) (setq beg (point))
  13289. (beginning-of-line 2) (setq end (point))
  13290. (beginning-of-line (- arg))
  13291. (setq pos (move-marker (make-marker) (point)))
  13292. (insert (delete-and-extract-region beg end))
  13293. (goto-char pos)
  13294. (move-to-column col)))
  13295. (defun org-replace-escapes (string table)
  13296. "Replace %-escapes in STRING with values in TABLE.
  13297. TABLE is an association list with keys like \"%a\" and string values.
  13298. The sequences in STRING may contain normal field width and padding information,
  13299. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13300. so values can contain further %-escapes if they are define later in TABLE."
  13301. (let ((case-fold-search nil)
  13302. e re rpl)
  13303. (while (setq e (pop table))
  13304. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13305. (while (string-match re string)
  13306. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13307. (cdr e)))
  13308. (setq string (replace-match rpl t t string))))
  13309. string))
  13310. (defun org-sublist (list start end)
  13311. "Return a section of LIST, from START to END.
  13312. Counting starts at 1."
  13313. (let (rtn (c start))
  13314. (setq list (nthcdr (1- start) list))
  13315. (while (and list (<= c end))
  13316. (push (pop list) rtn)
  13317. (setq c (1+ c)))
  13318. (nreverse rtn)))
  13319. (defun org-find-base-buffer-visiting (file)
  13320. "Like `find-buffer-visiting' but alway return the base buffer and
  13321. not an indirect buffer."
  13322. (let ((buf (find-buffer-visiting file)))
  13323. (if buf
  13324. (or (buffer-base-buffer buf) buf)
  13325. nil)))
  13326. (defun org-image-file-name-regexp ()
  13327. "Return regexp matching the file names of images."
  13328. (if (fboundp 'image-file-name-regexp)
  13329. (image-file-name-regexp)
  13330. (let ((image-file-name-extensions
  13331. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13332. "xbm" "xpm" "pbm" "pgm" "ppm")))
  13333. (concat "\\."
  13334. (regexp-opt (nconc (mapcar 'upcase
  13335. image-file-name-extensions)
  13336. image-file-name-extensions)
  13337. t)
  13338. "\\'"))))
  13339. (defun org-file-image-p (file)
  13340. "Return non-nil if FILE is an image."
  13341. (save-match-data
  13342. (string-match (org-image-file-name-regexp) file)))
  13343. ;;; Paragraph filling stuff.
  13344. ;; We want this to be just right, so use the full arsenal.
  13345. (defun org-indent-line-function ()
  13346. "Indent line like previous, but further if previous was headline or item."
  13347. (interactive)
  13348. (let* ((pos (point))
  13349. (itemp (org-at-item-p))
  13350. column bpos bcol tpos tcol bullet btype bullet-type)
  13351. ;; Find the previous relevant line
  13352. (beginning-of-line 1)
  13353. (cond
  13354. ((looking-at "#") (setq column 0))
  13355. ((looking-at "\\*+ ") (setq column 0))
  13356. (t
  13357. (beginning-of-line 0)
  13358. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13359. (beginning-of-line 0))
  13360. (cond
  13361. ((looking-at "\\*+[ \t]+")
  13362. (goto-char (match-end 0))
  13363. (setq column (current-column)))
  13364. ((org-in-item-p)
  13365. (org-beginning-of-item)
  13366. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13367. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  13368. (setq bpos (match-beginning 1) tpos (match-end 0)
  13369. bcol (progn (goto-char bpos) (current-column))
  13370. tcol (progn (goto-char tpos) (current-column))
  13371. bullet (match-string 1)
  13372. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13373. (if (not itemp)
  13374. (setq column tcol)
  13375. (goto-char pos)
  13376. (beginning-of-line 1)
  13377. (if (looking-at "\\S-")
  13378. (progn
  13379. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13380. (setq bullet (match-string 1)
  13381. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13382. (setq column (if (equal btype bullet-type) bcol tcol)))
  13383. (setq column (org-get-indentation)))))
  13384. (t (setq column (org-get-indentation))))))
  13385. (goto-char pos)
  13386. (if (<= (current-column) (current-indentation))
  13387. (indent-line-to column)
  13388. (save-excursion (indent-line-to column)))
  13389. (setq column (current-column))
  13390. (beginning-of-line 1)
  13391. (if (looking-at
  13392. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13393. (replace-match (concat "\\1" (format org-property-format
  13394. (match-string 2) (match-string 3)))
  13395. t nil))
  13396. (move-to-column column)))
  13397. (defun org-set-autofill-regexps ()
  13398. (interactive)
  13399. ;; In the paragraph separator we include headlines, because filling
  13400. ;; text in a line directly attached to a headline would otherwise
  13401. ;; fill the headline as well.
  13402. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13403. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13404. ;; The paragraph starter includes hand-formatted lists.
  13405. (org-set-local 'paragraph-start
  13406. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13407. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13408. ;; But only if the user has not turned off tables or fixed-width regions
  13409. (org-set-local
  13410. 'auto-fill-inhibit-regexp
  13411. (concat "\\*+ \\|#\\+"
  13412. "\\|[ \t]*" org-keyword-time-regexp
  13413. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13414. (concat
  13415. "\\|[ \t]*["
  13416. (if org-enable-table-editor "|" "")
  13417. (if org-enable-fixed-width-editor ":" "")
  13418. "]"))))
  13419. ;; We use our own fill-paragraph function, to make sure that tables
  13420. ;; and fixed-width regions are not wrapped. That function will pass
  13421. ;; through to `fill-paragraph' when appropriate.
  13422. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13423. ; Adaptive filling: To get full control, first make sure that
  13424. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13425. (org-set-local 'adaptive-fill-regexp "\000")
  13426. (org-set-local 'adaptive-fill-function
  13427. 'org-adaptive-fill-function)
  13428. (org-set-local
  13429. 'align-mode-rules-list
  13430. '((org-in-buffer-settings
  13431. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13432. (modes . '(org-mode))))))
  13433. (defun org-fill-paragraph (&optional justify)
  13434. "Re-align a table, pass through to fill-paragraph if no table."
  13435. (let ((table-p (org-at-table-p))
  13436. (table.el-p (org-at-table.el-p)))
  13437. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13438. (save-excursion (goto-char (point-at-bol))
  13439. (looking-at outline-regexp)))
  13440. t) ; skip headlines
  13441. (table.el-p t) ; skip table.el tables
  13442. (table-p (org-table-align) t) ; align org-mode tables
  13443. (t nil)))) ; call paragraph-fill
  13444. ;; For reference, this is the default value of adaptive-fill-regexp
  13445. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13446. (defun org-adaptive-fill-function ()
  13447. "Return a fill prefix for org-mode files.
  13448. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13449. work correctly."
  13450. (cond ((looking-at "#[ \t]+")
  13451. (match-string 0))
  13452. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  13453. (save-excursion
  13454. (goto-char (match-end 0))
  13455. (make-string (current-column) ?\ )))
  13456. (t nil)))
  13457. ;;; Other stuff.
  13458. (defun org-toggle-fixed-width-section (arg)
  13459. "Toggle the fixed-width export.
  13460. If there is no active region, the QUOTE keyword at the current headline is
  13461. inserted or removed. When present, it causes the text between this headline
  13462. and the next to be exported as fixed-width text, and unmodified.
  13463. If there is an active region, this command adds or removes a colon as the
  13464. first character of this line. If the first character of a line is a colon,
  13465. this line is also exported in fixed-width font."
  13466. (interactive "P")
  13467. (let* ((cc 0)
  13468. (regionp (org-region-active-p))
  13469. (beg (if regionp (region-beginning) (point)))
  13470. (end (if regionp (region-end)))
  13471. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13472. (case-fold-search nil)
  13473. (re "[ \t]*\\(:\\)")
  13474. off)
  13475. (if regionp
  13476. (save-excursion
  13477. (goto-char beg)
  13478. (setq cc (current-column))
  13479. (beginning-of-line 1)
  13480. (setq off (looking-at re))
  13481. (while (> nlines 0)
  13482. (setq nlines (1- nlines))
  13483. (beginning-of-line 1)
  13484. (cond
  13485. (arg
  13486. (move-to-column cc t)
  13487. (insert ":\n")
  13488. (forward-line -1))
  13489. ((and off (looking-at re))
  13490. (replace-match "" t t nil 1))
  13491. ((not off) (move-to-column cc t) (insert ":")))
  13492. (forward-line 1)))
  13493. (save-excursion
  13494. (org-back-to-heading)
  13495. (if (looking-at (concat outline-regexp
  13496. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13497. (replace-match "" t t nil 1)
  13498. (if (looking-at outline-regexp)
  13499. (progn
  13500. (goto-char (match-end 0))
  13501. (insert org-quote-string " "))))))))
  13502. ;;;; Functions extending outline functionality
  13503. (defun org-beginning-of-line (&optional arg)
  13504. "Go to the beginning of the current line. If that is invisible, continue
  13505. to a visible line beginning. This makes the function of C-a more intuitive.
  13506. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13507. first attempt, and only move to after the tags when the cursor is already
  13508. beyond the end of the headline."
  13509. (interactive "P")
  13510. (let ((pos (point)))
  13511. (beginning-of-line 1)
  13512. (if (bobp)
  13513. nil
  13514. (backward-char 1)
  13515. (if (org-invisible-p)
  13516. (while (and (not (bobp)) (org-invisible-p))
  13517. (backward-char 1)
  13518. (beginning-of-line 1))
  13519. (forward-char 1)))
  13520. (when org-special-ctrl-a/e
  13521. (cond
  13522. ((and (looking-at org-todo-line-regexp)
  13523. (= (char-after (match-end 1)) ?\ ))
  13524. (goto-char
  13525. (if (eq org-special-ctrl-a/e t)
  13526. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  13527. ((= pos (point)) (match-beginning 3))
  13528. (t (point)))
  13529. (cond ((> pos (point)) (point))
  13530. ((not (eq last-command this-command)) (point))
  13531. (t (match-beginning 3))))))
  13532. ((org-at-item-p)
  13533. (goto-char
  13534. (if (eq org-special-ctrl-a/e t)
  13535. (cond ((> pos (match-end 4)) (match-end 4))
  13536. ((= pos (point)) (match-end 4))
  13537. (t (point)))
  13538. (cond ((> pos (point)) (point))
  13539. ((not (eq last-command this-command)) (point))
  13540. (t (match-end 4))))))))))
  13541. (defun org-end-of-line (&optional arg)
  13542. "Go to the end of the line.
  13543. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13544. first attempt, and only move to after the tags when the cursor is already
  13545. beyond the end of the headline."
  13546. (interactive "P")
  13547. (if (or (not org-special-ctrl-a/e)
  13548. (not (org-on-heading-p)))
  13549. (end-of-line arg)
  13550. (let ((pos (point)))
  13551. (beginning-of-line 1)
  13552. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13553. (if (eq org-special-ctrl-a/e t)
  13554. (if (or (< pos (match-beginning 1))
  13555. (= pos (match-end 0)))
  13556. (goto-char (match-beginning 1))
  13557. (goto-char (match-end 0)))
  13558. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13559. (goto-char (match-end 0))
  13560. (goto-char (match-beginning 1))))
  13561. (end-of-line arg)))))
  13562. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13563. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13564. (defun org-kill-line (&optional arg)
  13565. "Kill line, to tags or end of line."
  13566. (interactive "P")
  13567. (cond
  13568. ((or (not org-special-ctrl-k)
  13569. (bolp)
  13570. (not (org-on-heading-p)))
  13571. (call-interactively 'kill-line))
  13572. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13573. (kill-region (point) (match-beginning 1))
  13574. (org-set-tags nil t))
  13575. (t (kill-region (point) (point-at-eol)))))
  13576. (define-key org-mode-map "\C-k" 'org-kill-line)
  13577. (defun org-invisible-p ()
  13578. "Check if point is at a character currently not visible."
  13579. ;; Early versions of noutline don't have `outline-invisible-p'.
  13580. (if (fboundp 'outline-invisible-p)
  13581. (outline-invisible-p)
  13582. (get-char-property (point) 'invisible)))
  13583. (defun org-invisible-p2 ()
  13584. "Check if point is at a character currently not visible."
  13585. (save-excursion
  13586. (if (and (eolp) (not (bobp))) (backward-char 1))
  13587. ;; Early versions of noutline don't have `outline-invisible-p'.
  13588. (if (fboundp 'outline-invisible-p)
  13589. (outline-invisible-p)
  13590. (get-char-property (point) 'invisible))))
  13591. (defalias 'org-back-to-heading 'outline-back-to-heading)
  13592. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13593. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13594. (defun org-at-heading-or-item-p ()
  13595. (or (org-on-heading-p) (org-at-item-p)))
  13596. (defun org-on-target-p ()
  13597. (or (org-in-regexp org-radio-target-regexp)
  13598. (org-in-regexp org-target-regexp)))
  13599. (defun org-up-heading-all (arg)
  13600. "Move to the heading line of which the present line is a subheading.
  13601. This function considers both visible and invisible heading lines.
  13602. With argument, move up ARG levels."
  13603. (if (fboundp 'outline-up-heading-all)
  13604. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13605. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13606. (defun org-up-heading-safe ()
  13607. "Move to the heading line of which the present line is a subheading.
  13608. This version will not throw an error. It will return the level of the
  13609. headline found, or nil if no higher level is found."
  13610. (let ((pos (point)) start-level level
  13611. (re (concat "^" outline-regexp)))
  13612. (catch 'exit
  13613. (outline-back-to-heading t)
  13614. (setq start-level (funcall outline-level))
  13615. (if (equal start-level 1) (throw 'exit nil))
  13616. (while (re-search-backward re nil t)
  13617. (setq level (funcall outline-level))
  13618. (if (< level start-level) (throw 'exit level)))
  13619. nil)))
  13620. (defun org-first-sibling-p ()
  13621. "Is this heading the first child of its parents?"
  13622. (interactive)
  13623. (let ((re (concat "^" outline-regexp))
  13624. level l)
  13625. (unless (org-at-heading-p t)
  13626. (error "Not at a heading"))
  13627. (setq level (funcall outline-level))
  13628. (save-excursion
  13629. (if (not (re-search-backward re nil t))
  13630. t
  13631. (setq l (funcall outline-level))
  13632. (< l level)))))
  13633. (defun org-goto-sibling (&optional previous)
  13634. "Goto the next sibling, even if it is invisible.
  13635. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13636. when a sibling was found. When none is found, return nil and don't
  13637. move point."
  13638. (let ((fun (if previous 're-search-backward 're-search-forward))
  13639. (pos (point))
  13640. (re (concat "^" outline-regexp))
  13641. level l)
  13642. (when (condition-case nil (org-back-to-heading t) (error nil))
  13643. (setq level (funcall outline-level))
  13644. (catch 'exit
  13645. (or previous (forward-char 1))
  13646. (while (funcall fun re nil t)
  13647. (setq l (funcall outline-level))
  13648. (when (< l level) (goto-char pos) (throw 'exit nil))
  13649. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13650. (goto-char pos)
  13651. nil))))
  13652. (defun org-show-siblings ()
  13653. "Show all siblings of the current headline."
  13654. (save-excursion
  13655. (while (org-goto-sibling) (org-flag-heading nil)))
  13656. (save-excursion
  13657. (while (org-goto-sibling 'previous)
  13658. (org-flag-heading nil))))
  13659. (defun org-show-hidden-entry ()
  13660. "Show an entry where even the heading is hidden."
  13661. (save-excursion
  13662. (org-show-entry)))
  13663. (defun org-flag-heading (flag &optional entry)
  13664. "Flag the current heading. FLAG non-nil means make invisible.
  13665. When ENTRY is non-nil, show the entire entry."
  13666. (save-excursion
  13667. (org-back-to-heading t)
  13668. ;; Check if we should show the entire entry
  13669. (if entry
  13670. (progn
  13671. (org-show-entry)
  13672. (save-excursion
  13673. (and (outline-next-heading)
  13674. (org-flag-heading nil))))
  13675. (outline-flag-region (max (point-min) (1- (point)))
  13676. (save-excursion (outline-end-of-heading) (point))
  13677. flag))))
  13678. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13679. ;; This is an exact copy of the original function, but it uses
  13680. ;; `org-back-to-heading', to make it work also in invisible
  13681. ;; trees. And is uses an invisible-OK argument.
  13682. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13683. (org-back-to-heading invisible-OK)
  13684. (let ((first t)
  13685. (level (funcall outline-level)))
  13686. (while (and (not (eobp))
  13687. (or first (> (funcall outline-level) level)))
  13688. (setq first nil)
  13689. (outline-next-heading))
  13690. (unless to-heading
  13691. (if (memq (preceding-char) '(?\n ?\^M))
  13692. (progn
  13693. ;; Go to end of line before heading
  13694. (forward-char -1)
  13695. (if (memq (preceding-char) '(?\n ?\^M))
  13696. ;; leave blank line before heading
  13697. (forward-char -1))))))
  13698. (point))
  13699. (defun org-show-subtree ()
  13700. "Show everything after this heading at deeper levels."
  13701. (outline-flag-region
  13702. (point)
  13703. (save-excursion
  13704. (outline-end-of-subtree) (outline-next-heading) (point))
  13705. nil))
  13706. (defun org-show-entry ()
  13707. "Show the body directly following this heading.
  13708. Show the heading too, if it is currently invisible."
  13709. (interactive)
  13710. (save-excursion
  13711. (condition-case nil
  13712. (progn
  13713. (org-back-to-heading t)
  13714. (outline-flag-region
  13715. (max (point-min) (1- (point)))
  13716. (save-excursion
  13717. (re-search-forward
  13718. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13719. (or (match-beginning 1) (point-max)))
  13720. nil))
  13721. (error nil))))
  13722. (defun org-make-options-regexp (kwds)
  13723. "Make a regular expression for keyword lines."
  13724. (concat
  13725. "^"
  13726. "#?[ \t]*\\+\\("
  13727. (mapconcat 'regexp-quote kwds "\\|")
  13728. "\\):[ \t]*"
  13729. "\\(.+\\)"))
  13730. ;; Make isearch reveal the necessary context
  13731. (defun org-isearch-end ()
  13732. "Reveal context after isearch exits."
  13733. (when isearch-success ; only if search was successful
  13734. (if (featurep 'xemacs)
  13735. ;; Under XEmacs, the hook is run in the correct place,
  13736. ;; we directly show the context.
  13737. (org-show-context 'isearch)
  13738. ;; In Emacs the hook runs *before* restoring the overlays.
  13739. ;; So we have to use a one-time post-command-hook to do this.
  13740. ;; (Emacs 22 has a special variable, see function `org-mode')
  13741. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13742. isearch-mode-end-hook-quit)
  13743. ;; Only when the isearch was not quitted.
  13744. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13745. 'append 'local)))))
  13746. (defun org-isearch-post-command ()
  13747. "Remove self from hook, and show context."
  13748. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13749. (org-show-context 'isearch))
  13750. ;;;; Integration with and fixes for other packages
  13751. ;;; Imenu support
  13752. (defvar org-imenu-markers nil
  13753. "All markers currently used by Imenu.")
  13754. (make-variable-buffer-local 'org-imenu-markers)
  13755. (defun org-imenu-new-marker (&optional pos)
  13756. "Return a new marker for use by Imenu, and remember the marker."
  13757. (let ((m (make-marker)))
  13758. (move-marker m (or pos (point)))
  13759. (push m org-imenu-markers)
  13760. m))
  13761. (defun org-imenu-get-tree ()
  13762. "Produce the index for Imenu."
  13763. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13764. (setq org-imenu-markers nil)
  13765. (let* ((n org-imenu-depth)
  13766. (re (concat "^" outline-regexp))
  13767. (subs (make-vector (1+ n) nil))
  13768. (last-level 0)
  13769. m tree level head)
  13770. (save-excursion
  13771. (save-restriction
  13772. (widen)
  13773. (goto-char (point-max))
  13774. (while (re-search-backward re nil t)
  13775. (setq level (org-reduced-level (funcall outline-level)))
  13776. (when (<= level n)
  13777. (looking-at org-complex-heading-regexp)
  13778. (setq head (org-match-string-no-properties 4)
  13779. m (org-imenu-new-marker))
  13780. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13781. (if (>= level last-level)
  13782. (push (cons head m) (aref subs level))
  13783. (push (cons head (aref subs (1+ level))) (aref subs level))
  13784. (loop for i from (1+ level) to n do (aset subs i nil)))
  13785. (setq last-level level)))))
  13786. (aref subs 1)))
  13787. (eval-after-load "imenu"
  13788. '(progn
  13789. (add-hook 'imenu-after-jump-hook
  13790. (lambda () (org-show-context 'org-goto)))))
  13791. ;; Speedbar support
  13792. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13793. "Overlay marking the agenda restriction line in speedbar.")
  13794. (org-overlay-put org-speedbar-restriction-lock-overlay
  13795. 'face 'org-agenda-restriction-lock)
  13796. (org-overlay-put org-speedbar-restriction-lock-overlay
  13797. 'help-echo "Agendas are currently limited to this item.")
  13798. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13799. (defun org-speedbar-set-agenda-restriction ()
  13800. "Restrict future agenda commands to the location at point in speedbar.
  13801. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13802. (interactive)
  13803. (require 'org-agenda)
  13804. (let (p m tp np dir txt w)
  13805. (cond
  13806. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13807. 'org-imenu t))
  13808. (setq m (get-text-property p 'org-imenu-marker))
  13809. (save-excursion
  13810. (save-restriction
  13811. (set-buffer (marker-buffer m))
  13812. (goto-char m)
  13813. (org-agenda-set-restriction-lock 'subtree))))
  13814. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13815. 'speedbar-function 'speedbar-find-file))
  13816. (setq tp (previous-single-property-change
  13817. (1+ p) 'speedbar-function)
  13818. np (next-single-property-change
  13819. tp 'speedbar-function)
  13820. dir (speedbar-line-directory)
  13821. txt (buffer-substring-no-properties (or tp (point-min))
  13822. (or np (point-max))))
  13823. (save-excursion
  13824. (save-restriction
  13825. (set-buffer (find-file-noselect
  13826. (let ((default-directory dir))
  13827. (expand-file-name txt))))
  13828. (unless (org-mode-p)
  13829. (error "Cannot restrict to non-Org-mode file"))
  13830. (org-agenda-set-restriction-lock 'file))))
  13831. (t (error "Don't know how to restrict Org-mode's agenda")))
  13832. (org-move-overlay org-speedbar-restriction-lock-overlay
  13833. (point-at-bol) (point-at-eol))
  13834. (setq current-prefix-arg nil)
  13835. (org-agenda-maybe-redo)))
  13836. (eval-after-load "speedbar"
  13837. '(progn
  13838. (speedbar-add-supported-extension ".org")
  13839. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13840. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13841. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13842. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13843. (add-hook 'speedbar-visiting-tag-hook
  13844. (lambda () (org-show-context 'org-goto)))))
  13845. ;;; Fixes and Hacks for problems with other packages
  13846. ;; Make flyspell not check words in links, to not mess up our keymap
  13847. (defun org-mode-flyspell-verify ()
  13848. "Don't let flyspell put overlays at active buttons."
  13849. (not (get-text-property (point) 'keymap)))
  13850. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13851. (eval-after-load "bookmark"
  13852. '(if (boundp 'bookmark-after-jump-hook)
  13853. ;; We can use the hook
  13854. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13855. ;; Hook not available, use advice
  13856. (defadvice bookmark-jump (after org-make-visible activate)
  13857. "Make the position visible."
  13858. (org-bookmark-jump-unhide))))
  13859. (defun org-bookmark-jump-unhide ()
  13860. "Unhide the current position, to show the bookmark location."
  13861. (and (org-mode-p)
  13862. (or (org-invisible-p)
  13863. (save-excursion (goto-char (max (point-min) (1- (point))))
  13864. (org-invisible-p)))
  13865. (org-show-context 'bookmark-jump)))
  13866. ;; Make session.el ignore our circular variable
  13867. (eval-after-load "session"
  13868. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13869. ;;;; Experimental code
  13870. (defun org-closed-in-range ()
  13871. "Sparse tree of items closed in a certain time range.
  13872. Still experimental, may disappear in the future."
  13873. (interactive)
  13874. ;; Get the time interval from the user.
  13875. (let* ((time1 (time-to-seconds
  13876. (org-read-date nil 'to-time nil "Starting date: ")))
  13877. (time2 (time-to-seconds
  13878. (org-read-date nil 'to-time nil "End date:")))
  13879. ;; callback function
  13880. (callback (lambda ()
  13881. (let ((time
  13882. (time-to-seconds
  13883. (apply 'encode-time
  13884. (org-parse-time-string
  13885. (match-string 1))))))
  13886. ;; check if time in interval
  13887. (and (>= time time1) (<= time time2))))))
  13888. ;; make tree, check each match with the callback
  13889. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13890. ;;;; Finish up
  13891. (provide 'org)
  13892. (run-hooks 'org-load-hook)
  13893. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13894. ;;; org.el ends here
  13895. (defun org-plist-delete (plist property)
  13896. "Delete PROPERTY from PLIST.
  13897. This is in contrast to merely setting it to 0."
  13898. (let (p)
  13899. (while plist
  13900. (if (not (eq property (car plist)))
  13901. (setq p (plist-put p (car plist) (nth 1 plist))))
  13902. (setq plist (cddr plist)))
  13903. p))