org.el 568 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277
  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, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.21
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.21"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  163. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  164. (const :tag "C eval: Include command output as text" org-eval)
  165. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  166. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  167. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  168. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  169. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  170. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  171. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  172. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  173. (const :tag "C registry: A registry for Org links" org-registry)
  174. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  175. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  176. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  177. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  178. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  179. (defcustom org-support-shift-select nil
  180. "Non-nil means, make shift-cursor commands select text when possible.
  181. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  182. selecting a region, or enlarge thusly regions started in this way.
  183. In Org-mode, in special contexts, these same keys are used for other
  184. purposes, important enough to compete with shift selection. Org tries
  185. to balance these needs by supporting `shift-select-mode' outside these
  186. special contexts, under control of this variable.
  187. The default of this variable is nil, to avoid confusing behavior. Shifted
  188. cursor keys will then execute Org commands in the following contexts:
  189. - on a headline, changing TODO state (left/right) and priority (up/down)
  190. - on a time stamp, changing the time
  191. - in a plain list item, changing the bullet type
  192. - in a property definition line, switching between allowed values
  193. - in the BEGIN line of a clock table (changing the time block).
  194. Outside these contexts, the commands will throw an error.
  195. When this variable is t and the cursor is not in a special context,
  196. Org-mode will support shift-selection for making and enlarging regions.
  197. To make this more effective, the bullet cycling will no longer happen
  198. anywhere in an item line, but only if the cursor is exactly on the bullet.
  199. If you set this variable to the symbol `always', then the keys
  200. will not be special in headlines, property lines, and item lines, to make
  201. shift selection work there as well. If this is what you want, you can
  202. use the following alternative commands: `C-c C-t' and `C-c ,' to
  203. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  204. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  205. edited by hand or in column view.
  206. However, when the cursor is on a timestamp, shift-cursor commands
  207. will still edit the time stamp - this is just too good to give up.
  208. XEmacs user should have this variable set to nil, because shift-select-mode
  209. is Emacs 23 only."
  210. :group 'org
  211. :type '(choice
  212. (const :tag "Never" nil)
  213. (const :tag "When outside special context" t)
  214. (const :tag "Everywhere except timestamps" always)))
  215. (defgroup org-startup nil
  216. "Options concerning startup of Org-mode."
  217. :tag "Org Startup"
  218. :group 'org)
  219. (defcustom org-startup-folded t
  220. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  221. This can also be configured on a per-file basis by adding one of
  222. the following lines anywhere in the buffer:
  223. #+STARTUP: fold
  224. #+STARTUP: nofold
  225. #+STARTUP: content"
  226. :group 'org-startup
  227. :type '(choice
  228. (const :tag "nofold: show all" nil)
  229. (const :tag "fold: overview" t)
  230. (const :tag "content: all headlines" content)))
  231. (defcustom org-startup-truncated t
  232. "Non-nil means, entering Org-mode will set `truncate-lines'.
  233. This is useful since some lines containing links can be very long and
  234. uninteresting. Also tables look terrible when wrapped."
  235. :group 'org-startup
  236. :type 'boolean)
  237. (defcustom org-startup-align-all-tables nil
  238. "Non-nil means, align all tables when visiting a file.
  239. This is useful when the column width in tables is forced with <N> cookies
  240. in table fields. Such tables will look correct only after the first re-align.
  241. This can also be configured on a per-file basis by adding one of
  242. the following lines anywhere in the buffer:
  243. #+STARTUP: align
  244. #+STARTUP: noalign"
  245. :group 'org-startup
  246. :type 'boolean)
  247. (defcustom org-insert-mode-line-in-empty-file nil
  248. "Non-nil means insert the first line setting Org-mode in empty files.
  249. When the function `org-mode' is called interactively in an empty file, this
  250. normally means that the file name does not automatically trigger Org-mode.
  251. To ensure that the file will always be in Org-mode in the future, a
  252. line enforcing Org-mode will be inserted into the buffer, if this option
  253. has been set."
  254. :group 'org-startup
  255. :type 'boolean)
  256. (defcustom org-replace-disputed-keys nil
  257. "Non-nil means use alternative key bindings for some keys.
  258. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  259. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  260. If you want to use Org-mode together with one of these other modes,
  261. or more generally if you would like to move some Org-mode commands to
  262. other keys, set this variable and configure the keys with the variable
  263. `org-disputed-keys'.
  264. This option is only relevant at load-time of Org-mode, and must be set
  265. *before* org.el is loaded. Changing it requires a restart of Emacs to
  266. become effective."
  267. :group 'org-startup
  268. :type 'boolean)
  269. (defcustom org-use-extra-keys nil
  270. "Non-nil means use extra key sequence definitions for certain
  271. commands. This happens automatically if you run XEmacs or if
  272. window-system is nil. This variable lets you do the same
  273. manually. You must set it before loading org.
  274. Example: on Carbon Emacs 22 running graphically, with an external
  275. keyboard on a Powerbook, the default way of setting M-left might
  276. not work for either Alt or ESC. Setting this variable will make
  277. it work for ESC."
  278. :group 'org-startup
  279. :type 'boolean)
  280. (if (fboundp 'defvaralias)
  281. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  282. (defcustom org-disputed-keys
  283. '(([(shift up)] . [(meta p)])
  284. ([(shift down)] . [(meta n)])
  285. ([(shift left)] . [(meta -)])
  286. ([(shift right)] . [(meta +)])
  287. ([(control shift right)] . [(meta shift +)])
  288. ([(control shift left)] . [(meta shift -)]))
  289. "Keys for which Org-mode and other modes compete.
  290. This is an alist, cars are the default keys, second element specifies
  291. the alternative to use when `org-replace-disputed-keys' is t.
  292. Keys can be specified in any syntax supported by `define-key'.
  293. The value of this option takes effect only at Org-mode's startup,
  294. therefore you'll have to restart Emacs to apply it after changing."
  295. :group 'org-startup
  296. :type 'alist)
  297. (defun org-key (key)
  298. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  299. Or return the original if not disputed."
  300. (if org-replace-disputed-keys
  301. (let* ((nkey (key-description key))
  302. (x (org-find-if (lambda (x)
  303. (equal (key-description (car x)) nkey))
  304. org-disputed-keys)))
  305. (if x (cdr x) key))
  306. key))
  307. (defun org-find-if (predicate seq)
  308. (catch 'exit
  309. (while seq
  310. (if (funcall predicate (car seq))
  311. (throw 'exit (car seq))
  312. (pop seq)))))
  313. (defun org-defkey (keymap key def)
  314. "Define a key, possibly translated, as returned by `org-key'."
  315. (define-key keymap (org-key key) def))
  316. (defcustom org-ellipsis nil
  317. "The ellipsis to use in the Org-mode outline.
  318. When nil, just use the standard three dots. When a string, use that instead,
  319. When a face, use the standard 3 dots, but with the specified face.
  320. The change affects only Org-mode (which will then use its own display table).
  321. Changing this requires executing `M-x org-mode' in a buffer to become
  322. effective."
  323. :group 'org-startup
  324. :type '(choice (const :tag "Default" nil)
  325. (face :tag "Face" :value org-warning)
  326. (string :tag "String" :value "...#")))
  327. (defvar org-display-table nil
  328. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  329. (defgroup org-keywords nil
  330. "Keywords in Org-mode."
  331. :tag "Org Keywords"
  332. :group 'org)
  333. (defcustom org-deadline-string "DEADLINE:"
  334. "String to mark deadline entries.
  335. A deadline is this string, followed by a time stamp. Should be a word,
  336. terminated by a colon. You can insert a schedule keyword and
  337. a timestamp with \\[org-deadline].
  338. Changes become only effective after restarting Emacs."
  339. :group 'org-keywords
  340. :type 'string)
  341. (defcustom org-scheduled-string "SCHEDULED:"
  342. "String to mark scheduled TODO entries.
  343. A schedule is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-schedule].
  346. Changes become only effective after restarting Emacs."
  347. :group 'org-keywords
  348. :type 'string)
  349. (defcustom org-closed-string "CLOSED:"
  350. "String used as the prefix for timestamps logging closing a TODO entry."
  351. :group 'org-keywords
  352. :type 'string)
  353. (defcustom org-clock-string "CLOCK:"
  354. "String used as prefix for timestamps clocking work hours on an item."
  355. :group 'org-keywords
  356. :type 'string)
  357. (defcustom org-comment-string "COMMENT"
  358. "Entries starting with this keyword will never be exported.
  359. An entry can be toggled between COMMENT and normal with
  360. \\[org-toggle-comment].
  361. Changes become only effective after restarting Emacs."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-quote-string "QUOTE"
  365. "Entries starting with this keyword will be exported in fixed-width font.
  366. Quoting applies only to the text in the entry following the headline, and does
  367. not extend beyond the next headline, even if that is lower level.
  368. An entry can be toggled between QUOTE and normal with
  369. \\[org-toggle-fixed-width-section]."
  370. :group 'org-keywords
  371. :type 'string)
  372. (defconst org-repeat-re
  373. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  374. "Regular expression for specifying repeated events.
  375. After a match, group 1 contains the repeat expression.")
  376. (defgroup org-structure nil
  377. "Options concerning the general structure of Org-mode files."
  378. :tag "Org Structure"
  379. :group 'org)
  380. (defgroup org-reveal-location nil
  381. "Options about how to make context of a location visible."
  382. :tag "Org Reveal Location"
  383. :group 'org-structure)
  384. (defconst org-context-choice
  385. '(choice
  386. (const :tag "Always" t)
  387. (const :tag "Never" nil)
  388. (repeat :greedy t :tag "Individual contexts"
  389. (cons
  390. (choice :tag "Context"
  391. (const agenda)
  392. (const org-goto)
  393. (const occur-tree)
  394. (const tags-tree)
  395. (const link-search)
  396. (const mark-goto)
  397. (const bookmark-jump)
  398. (const isearch)
  399. (const default))
  400. (boolean))))
  401. "Contexts for the reveal options.")
  402. (defcustom org-show-hierarchy-above '((default . t))
  403. "Non-nil means, show full hierarchy when revealing a location.
  404. Org-mode often shows locations in an org-mode file which might have
  405. been invisible before. When this is set, the hierarchy of headings
  406. above the exposed location is shown.
  407. Turning this off for example for sparse trees makes them very compact.
  408. Instead of t, this can also be an alist specifying this option for different
  409. contexts. Valid contexts are
  410. agenda when exposing an entry from the agenda
  411. org-goto when using the command `org-goto' on key C-c C-j
  412. occur-tree when using the command `org-occur' on key C-c /
  413. tags-tree when constructing a sparse tree based on tags matches
  414. link-search when exposing search matches associated with a link
  415. mark-goto when exposing the jump goal of a mark
  416. bookmark-jump when exposing a bookmark location
  417. isearch when exiting from an incremental search
  418. default default for all contexts not set explicitly"
  419. :group 'org-reveal-location
  420. :type org-context-choice)
  421. (defcustom org-show-following-heading '((default . nil))
  422. "Non-nil means, show following heading when revealing a location.
  423. Org-mode often shows locations in an org-mode file which might have
  424. been invisible before. When this is set, the heading following the
  425. match is shown.
  426. Turning this off for example for sparse trees makes them very compact,
  427. but makes it harder to edit the location of the match. In such a case,
  428. use the command \\[org-reveal] to show more context.
  429. Instead of t, this can also be an alist specifying this option for different
  430. contexts. See `org-show-hierarchy-above' for valid contexts."
  431. :group 'org-reveal-location
  432. :type org-context-choice)
  433. (defcustom org-show-siblings '((default . nil) (isearch t))
  434. "Non-nil means, show all sibling heading when revealing a location.
  435. Org-mode often shows locations in an org-mode file which might have
  436. been invisible before. When this is set, the sibling of the current entry
  437. heading are all made visible. If `org-show-hierarchy-above' is t,
  438. the same happens on each level of the hierarchy above the current entry.
  439. By default this is on for the isearch context, off for all other contexts.
  440. Turning this off for example for sparse trees makes them very compact,
  441. but makes it harder to edit the location of the match. In such a case,
  442. use the command \\[org-reveal] to show more context.
  443. Instead of t, this can also be an alist specifying this option for different
  444. contexts. See `org-show-hierarchy-above' for valid contexts."
  445. :group 'org-reveal-location
  446. :type org-context-choice)
  447. (defcustom org-show-entry-below '((default . nil))
  448. "Non-nil means, show the entry below a headline when revealing a location.
  449. Org-mode often shows locations in an org-mode file which might have
  450. been invisible before. When this is set, the text below the headline that is
  451. exposed is also shown.
  452. By default this is off for all contexts.
  453. Instead of t, this can also be an alist specifying this option for different
  454. contexts. See `org-show-hierarchy-above' for valid contexts."
  455. :group 'org-reveal-location
  456. :type org-context-choice)
  457. (defcustom org-indirect-buffer-display 'other-window
  458. "How should indirect tree buffers be displayed?
  459. This applies to indirect buffers created with the commands
  460. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  461. Valid values are:
  462. current-window Display in the current window
  463. other-window Just display in another window.
  464. dedicated-frame Create one new frame, and re-use it each time.
  465. new-frame Make a new frame each time. Note that in this case
  466. previously-made indirect buffers are kept, and you need to
  467. kill these buffers yourself."
  468. :group 'org-structure
  469. :group 'org-agenda-windows
  470. :type '(choice
  471. (const :tag "In current window" current-window)
  472. (const :tag "In current frame, other window" other-window)
  473. (const :tag "Each time a new frame" new-frame)
  474. (const :tag "One dedicated frame" dedicated-frame)))
  475. (defgroup org-cycle nil
  476. "Options concerning visibility cycling in Org-mode."
  477. :tag "Org Cycle"
  478. :group 'org-structure)
  479. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  480. "Names of drawers. Drawers are not opened by cycling on the headline above.
  481. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  482. this:
  483. :DRAWERNAME:
  484. .....
  485. :END:
  486. The drawer \"PROPERTIES\" is special for capturing properties through
  487. the property API.
  488. Drawers can be defined on the per-file basis with a line like:
  489. #+DRAWERS: HIDDEN STATE PROPERTIES"
  490. :group 'org-structure
  491. :type '(repeat (string :tag "Drawer Name")))
  492. (defcustom org-cycle-global-at-bob nil
  493. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  494. This makes it possible to do global cycling without having to use S-TAB or
  495. C-u TAB. For this special case to work, the first line of the buffer
  496. must not be a headline - it may be empty ot some other text. When used in
  497. this way, `org-cycle-hook' is disables temporarily, to make sure the
  498. cursor stays at the beginning of the buffer.
  499. When this option is nil, don't do anything special at the beginning
  500. of the buffer."
  501. :group 'org-cycle
  502. :type 'boolean)
  503. (defcustom org-cycle-emulate-tab t
  504. "Where should `org-cycle' emulate TAB.
  505. nil Never
  506. white Only in completely white lines
  507. whitestart Only at the beginning of lines, before the first non-white char
  508. t Everywhere except in headlines
  509. exc-hl-bol Everywhere except at the start of a headline
  510. If TAB is used in a place where it does not emulate TAB, the current subtree
  511. visibility is cycled."
  512. :group 'org-cycle
  513. :type '(choice (const :tag "Never" nil)
  514. (const :tag "Only in completely white lines" white)
  515. (const :tag "Before first char in a line" whitestart)
  516. (const :tag "Everywhere except in headlines" t)
  517. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  518. ))
  519. (defcustom org-cycle-separator-lines 2
  520. "Number of empty lines needed to keep an empty line between collapsed trees.
  521. If you leave an empty line between the end of a subtree and the following
  522. headline, this empty line is hidden when the subtree is folded.
  523. Org-mode will leave (exactly) one empty line visible if the number of
  524. empty lines is equal or larger to the number given in this variable.
  525. So the default 2 means, at least 2 empty lines after the end of a subtree
  526. are needed to produce free space between a collapsed subtree and the
  527. following headline.
  528. Special case: when 0, never leave empty lines in collapsed view."
  529. :group 'org-cycle
  530. :type 'integer)
  531. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  532. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  533. org-cycle-hide-drawers
  534. org-cycle-show-empty-lines
  535. org-optimize-window-after-visibility-change)
  536. "Hook that is run after `org-cycle' has changed the buffer visibility.
  537. The function(s) in this hook must accept a single argument which indicates
  538. the new state that was set by the most recent `org-cycle' command. The
  539. argument is a symbol. After a global state change, it can have the values
  540. `overview', `content', or `all'. After a local state change, it can have
  541. the values `folded', `children', or `subtree'."
  542. :group 'org-cycle
  543. :type 'hook)
  544. (defgroup org-edit-structure nil
  545. "Options concerning structure editing in Org-mode."
  546. :tag "Org Edit Structure"
  547. :group 'org-structure)
  548. (defcustom org-odd-levels-only nil
  549. "Non-nil means, skip even levels and only use odd levels for the outline.
  550. This has the effect that two stars are being added/taken away in
  551. promotion/demotion commands. It also influences how levels are
  552. handled by the exporters.
  553. Changing it requires restart of `font-lock-mode' to become effective
  554. for fontification also in regions already fontified.
  555. You may also set this on a per-file basis by adding one of the following
  556. lines to the buffer:
  557. #+STARTUP: odd
  558. #+STARTUP: oddeven"
  559. :group 'org-edit-structure
  560. :group 'org-font-lock
  561. :type 'boolean)
  562. (defcustom org-adapt-indentation t
  563. "Non-nil means, adapt indentation when promoting and demoting.
  564. When this is set and the *entire* text in an entry is indented, the
  565. indentation is increased by one space in a demotion command, and
  566. decreased by one in a promotion command. If any line in the entry
  567. body starts at column 0, indentation is not changed at all."
  568. :group 'org-edit-structure
  569. :type 'boolean)
  570. (defcustom org-special-ctrl-a/e nil
  571. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  572. When t, `C-a' will bring back the cursor to the beginning of the
  573. headline text, i.e. after the stars and after a possible TODO keyword.
  574. In an item, this will be the position after the bullet.
  575. When the cursor is already at that position, another `C-a' will bring
  576. it to the beginning of the line.
  577. `C-e' will jump to the end of the headline, ignoring the presence of tags
  578. in the headline. A second `C-e' will then jump to the true end of the
  579. line, after any tags.
  580. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  581. and only a directly following, identical keypress will bring the cursor
  582. to the special positions."
  583. :group 'org-edit-structure
  584. :type '(choice
  585. (const :tag "off" nil)
  586. (const :tag "after bullet first" t)
  587. (const :tag "border first" reversed)))
  588. (if (fboundp 'defvaralias)
  589. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  590. (defcustom org-special-ctrl-k nil
  591. "Non-nil means `C-k' will behave specially in headlines.
  592. When nil, `C-k' will call the default `kill-line' command.
  593. When t, the following will happen while the cursor is in the headline:
  594. - When the cursor is at the beginning of a headline, kill the entire
  595. line and possible the folded subtree below the line.
  596. - When in the middle of the headline text, kill the headline up to the tags.
  597. - When after the headline text, kill the tags."
  598. :group 'org-edit-structure
  599. :type 'boolean)
  600. (defcustom org-yank-folded-subtrees t
  601. "Non-nil means, when yanking subtrees, fold them.
  602. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  603. it starts with a heading and all other headings in it are either children
  604. or siblings, then fold all the subtrees. However, do this only if no
  605. text after the yank would be swallowed into a folded tree by this action."
  606. :group 'org-edit-structure
  607. :type 'boolean)
  608. (defcustom org-yank-adjusted-subtrees nil
  609. "Non-nil means, when yanking subtrees, adjust the level.
  610. With this setting, `org-paste-subtree' is used to insert the subtree, see
  611. this function for details."
  612. :group 'org-edit-structure
  613. :type 'boolean)
  614. (defcustom org-M-RET-may-split-line '((default . t))
  615. "Non-nil means, M-RET will split the line at the cursor position.
  616. When nil, it will go to the end of the line before making a
  617. new line.
  618. You may also set this option in a different way for different
  619. contexts. Valid contexts are:
  620. headline when creating a new headline
  621. item when creating a new item
  622. table in a table field
  623. default the value to be used for all contexts not explicitly
  624. customized"
  625. :group 'org-structure
  626. :group 'org-table
  627. :type '(choice
  628. (const :tag "Always" t)
  629. (const :tag "Never" nil)
  630. (repeat :greedy t :tag "Individual contexts"
  631. (cons
  632. (choice :tag "Context"
  633. (const headline)
  634. (const item)
  635. (const table)
  636. (const default))
  637. (boolean)))))
  638. (defcustom org-insert-heading-respect-content nil
  639. "Non-nil means, insert new headings after the current subtree.
  640. When nil, the new heading is created directly after the current line.
  641. The commands \\[org-insert-heading-respect-content] and
  642. \\[org-insert-todo-heading-respect-content] turn this variable on
  643. for the duration of the command."
  644. :group 'org-structure
  645. :type 'boolean)
  646. (defcustom org-blank-before-new-entry '((heading . auto)
  647. (plain-list-item . auto))
  648. "Should `org-insert-heading' leave a blank line before new heading/item?
  649. The value is an alist, with `heading' and `plain-list-item' as car,
  650. and a boolean flag as cdr."
  651. :group 'org-edit-structure
  652. :type '(list
  653. (cons (const heading)
  654. (choice (const :tag "Never" nil)
  655. (const :tag "Always" t)
  656. (const :tag "Auto" auto)))
  657. (cons (const plain-list-item)
  658. (choice (const :tag "Never" nil)
  659. (const :tag "Always" t)
  660. (const :tag "Auto" auto)))))
  661. (defcustom org-insert-heading-hook nil
  662. "Hook being run after inserting a new heading."
  663. :group 'org-edit-structure
  664. :type 'hook)
  665. (defcustom org-enable-fixed-width-editor t
  666. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  667. This currently only means, they are never auto-wrapped.
  668. When nil, such lines will be treated like ordinary lines.
  669. See also the QUOTE keyword."
  670. :group 'org-edit-structure
  671. :type 'boolean)
  672. (defcustom org-edit-src-region-extra nil
  673. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  674. For examples see the function `org-edit-src-find-region-and-lang'.
  675. The regular expression identifying the begin marker should end with a newline,
  676. and the regexp marking the end line should start with a newline, to make sure
  677. there are kept outside the narrowed region."
  678. :group 'org-edit-structure
  679. :type '(repeat
  680. (list
  681. (regexp :tag "begin regexp")
  682. (regexp :tag "end regexp")
  683. (choice :tag "language"
  684. (string :tag "specify")
  685. (integer :tag "from match group")
  686. (const :tag "from `lang' element")
  687. (const :tag "from `style' element")))))
  688. (defcustom org-coderef-label-format "(ref:%s)"
  689. "The default coderef format.
  690. This format string will be used to search for coderef labels in literal
  691. examples (EXAMPLE and SRC blocks). The format can be overwritten
  692. an individual literal example with the -f option, like
  693. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  694. ...
  695. #+END_SRC
  696. If you want to use this for HTML export, make sure that the format does
  697. not introduce special font-locking, and avoid the HTML special
  698. characters `<', `>', and `&'. The reason for this restriction is that
  699. the labels are searched for only after htmlize has done its job."
  700. :group 'org-edit-structure ; FIXME this is not in the right group
  701. :type 'string)
  702. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  703. "The mode that should be used to edit fixed-width regions.
  704. These are the regions where each line starts with a colon."
  705. :group 'org-edit-structure
  706. :type '(choice
  707. (const artist-mode)
  708. (const picture-mode)
  709. (const fundamental-mode)
  710. (function :tag "Other (specify)")))
  711. (defcustom org-goto-auto-isearch t
  712. "Non-nil means, typing characters in org-goto starts incremental search."
  713. :group 'org-edit-structure
  714. :type 'boolean)
  715. (defgroup org-sparse-trees nil
  716. "Options concerning sparse trees in Org-mode."
  717. :tag "Org Sparse Trees"
  718. :group 'org-structure)
  719. (defcustom org-highlight-sparse-tree-matches t
  720. "Non-nil means, highlight all matches that define a sparse tree.
  721. The highlights will automatically disappear the next time the buffer is
  722. changed by an edit command."
  723. :group 'org-sparse-trees
  724. :type 'boolean)
  725. (defcustom org-remove-highlights-with-change t
  726. "Non-nil means, any change to the buffer will remove temporary highlights.
  727. Such highlights are created by `org-occur' and `org-clock-display'.
  728. When nil, `C-c C-c needs to be used to get rid of the highlights.
  729. The highlights created by `org-preview-latex-fragment' always need
  730. `C-c C-c' to be removed."
  731. :group 'org-sparse-trees
  732. :group 'org-time
  733. :type 'boolean)
  734. (defcustom org-occur-hook '(org-first-headline-recenter)
  735. "Hook that is run after `org-occur' has constructed a sparse tree.
  736. This can be used to recenter the window to show as much of the structure
  737. as possible."
  738. :group 'org-sparse-trees
  739. :type 'hook)
  740. (defgroup org-imenu-and-speedbar nil
  741. "Options concerning imenu and speedbar in Org-mode."
  742. :tag "Org Imenu and Speedbar"
  743. :group 'org-structure)
  744. (defcustom org-imenu-depth 2
  745. "The maximum level for Imenu access to Org-mode headlines.
  746. This also applied for speedbar access."
  747. :group 'org-imenu-and-speedbar
  748. :type 'number)
  749. (defgroup org-table nil
  750. "Options concerning tables in Org-mode."
  751. :tag "Org Table"
  752. :group 'org)
  753. (defcustom org-enable-table-editor 'optimized
  754. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  755. When nil, such lines will be treated like ordinary lines.
  756. When equal to the symbol `optimized', the table editor will be optimized to
  757. do the following:
  758. - Automatic overwrite mode in front of whitespace in table fields.
  759. This makes the structure of the table stay in tact as long as the edited
  760. field does not exceed the column width.
  761. - Minimize the number of realigns. Normally, the table is aligned each time
  762. TAB or RET are pressed to move to another field. With optimization this
  763. happens only if changes to a field might have changed the column width.
  764. Optimization requires replacing the functions `self-insert-command',
  765. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  766. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  767. very good at guessing when a re-align will be necessary, but you can always
  768. force one with \\[org-ctrl-c-ctrl-c].
  769. If you would like to use the optimized version in Org-mode, but the
  770. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  771. This variable can be used to turn on and off the table editor during a session,
  772. but in order to toggle optimization, a restart is required.
  773. See also the variable `org-table-auto-blank-field'."
  774. :group 'org-table
  775. :type '(choice
  776. (const :tag "off" nil)
  777. (const :tag "on" t)
  778. (const :tag "on, optimized" optimized)))
  779. (defcustom org-table-tab-recognizes-table.el t
  780. "Non-nil means, TAB will automatically notice a table.el table.
  781. When it sees such a table, it moves point into it and - if necessary -
  782. calls `table-recognize-table'."
  783. :group 'org-table-editing
  784. :type 'boolean)
  785. (defgroup org-link nil
  786. "Options concerning links in Org-mode."
  787. :tag "Org Link"
  788. :group 'org)
  789. (defvar org-link-abbrev-alist-local nil
  790. "Buffer-local version of `org-link-abbrev-alist', which see.
  791. The value of this is taken from the #+LINK lines.")
  792. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  793. (defcustom org-link-abbrev-alist nil
  794. "Alist of link abbreviations.
  795. The car of each element is a string, to be replaced at the start of a link.
  796. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  797. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  798. [[linkkey:tag][description]]
  799. If REPLACE is a string, the tag will simply be appended to create the link.
  800. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  801. the placeholder \"%h\" will cause a url-encoded version of the tag to
  802. be inserted at that point (see the function `url-hexify-string').
  803. REPLACE may also be a function that will be called with the tag as the
  804. only argument to create the link, which should be returned as a string.
  805. See the manual for examples."
  806. :group 'org-link
  807. :type '(repeat
  808. (cons
  809. (string :tag "Protocol")
  810. (choice
  811. (string :tag "Format")
  812. (function)))))
  813. (defcustom org-descriptive-links t
  814. "Non-nil means, hide link part and only show description of bracket links.
  815. Bracket links are like [[link][description]]. This variable sets the initial
  816. state in new org-mode buffers. The setting can then be toggled on a
  817. per-buffer basis from the Org->Hyperlinks menu."
  818. :group 'org-link
  819. :type 'boolean)
  820. (defcustom org-link-file-path-type 'adaptive
  821. "How the path name in file links should be stored.
  822. Valid values are:
  823. relative Relative to the current directory, i.e. the directory of the file
  824. into which the link is being inserted.
  825. absolute Absolute path, if possible with ~ for home directory.
  826. noabbrev Absolute path, no abbreviation of home directory.
  827. adaptive Use relative path for files in the current directory and sub-
  828. directories of it. For other files, use an absolute path."
  829. :group 'org-link
  830. :type '(choice
  831. (const relative)
  832. (const absolute)
  833. (const noabbrev)
  834. (const adaptive)))
  835. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  836. "Types of links that should be activated in Org-mode files.
  837. This is a list of symbols, each leading to the activation of a certain link
  838. type. In principle, it does not hurt to turn on most link types - there may
  839. be a small gain when turning off unused link types. The types are:
  840. bracket The recommended [[link][description]] or [[link]] links with hiding.
  841. angular Links in angular brackets that may contain whitespace like
  842. <bbdb:Carsten Dominik>.
  843. plain Plain links in normal text, no whitespace, like http://google.com.
  844. radio Text that is matched by a radio target, see manual for details.
  845. tag Tag settings in a headline (link to tag search).
  846. date Time stamps (link to calendar).
  847. footnote Footnote labels.
  848. Changing this variable requires a restart of Emacs to become effective."
  849. :group 'org-link
  850. :type '(set :greedy t
  851. (const :tag "Double bracket links (new style)" bracket)
  852. (const :tag "Angular bracket links (old style)" angular)
  853. (const :tag "Plain text links" plain)
  854. (const :tag "Radio target matches" radio)
  855. (const :tag "Tags" tag)
  856. (const :tag "Timestamps" date)
  857. (const :tag "Footnotes" footnote)))
  858. (defcustom org-make-link-description-function nil
  859. "Function to use to generate link descriptions from links. If
  860. nil the link location will be used. This function must take two
  861. parameters; the first is the link and the second the description
  862. org-insert-link has generated, and should return the description
  863. to use."
  864. :group 'org-link
  865. :type 'function)
  866. (defgroup org-link-store nil
  867. "Options concerning storing links in Org-mode."
  868. :tag "Org Store Link"
  869. :group 'org-link)
  870. (defcustom org-email-link-description-format "Email %c: %.30s"
  871. "Format of the description part of a link to an email or usenet message.
  872. The following %-escapes will be replaced by corresponding information:
  873. %F full \"From\" field
  874. %f name, taken from \"From\" field, address if no name
  875. %T full \"To\" field
  876. %t first name in \"To\" field, address if no name
  877. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  878. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  879. %s subject
  880. %m message-id.
  881. You may use normal field width specification between the % and the letter.
  882. This is for example useful to limit the length of the subject.
  883. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  884. :group 'org-link-store
  885. :type 'string)
  886. (defcustom org-from-is-user-regexp
  887. (let (r1 r2)
  888. (when (and user-mail-address (not (string= user-mail-address "")))
  889. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  890. (when (and user-full-name (not (string= user-full-name "")))
  891. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  892. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  893. "Regexp matched against the \"From:\" header of an email or usenet message.
  894. It should match if the message is from the user him/herself."
  895. :group 'org-link-store
  896. :type 'regexp)
  897. (defcustom org-link-to-org-use-id 'create-if-interactive
  898. "Non-nil means, storing a link to an Org file will use entry IDs.
  899. Note that before this variable is even considered, org-id must be loaded,
  900. to please customize `org-modules' and turn it on.
  901. The variable can have the following values:
  902. t Create an ID if needed to make a link to the current entry.
  903. create-if-interactive
  904. If `org-store-link' is called directly (interactively, as a user
  905. command), do create an ID to support the link. But when doing the
  906. job for remember, only use the ID if it already exists. The
  907. purpose of this setting is to avoid proliferation of unwanted
  908. IDs, just because you happen to be in an Org file when you
  909. call `org-remember' that automatically and preemptively
  910. creates a link. If you do want to get an ID link in a remember
  911. template to an entry not having an ID, create it first by
  912. explicitly creating a link to it, using `C-c C-l' first.
  913. use-existing
  914. Use existing ID, do not create one.
  915. nil Never use an ID to make a link, instead link using a text search for
  916. the headline text."
  917. :group 'org-link-store
  918. :type '(choice
  919. (const :tag "Create ID to make link" t)
  920. (const :tag "Create if string link interactively"
  921. 'create-if-interactive)
  922. (const :tag "Only use existing" 'use-existing)
  923. (const :tag "Do not use ID to create link" nil)))
  924. (defcustom org-context-in-file-links t
  925. "Non-nil means, file links from `org-store-link' contain context.
  926. A search string will be added to the file name with :: as separator and
  927. used to find the context when the link is activated by the command
  928. `org-open-at-point'.
  929. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  930. negates this setting for the duration of the command."
  931. :group 'org-link-store
  932. :type 'boolean)
  933. (defcustom org-keep-stored-link-after-insertion nil
  934. "Non-nil means, keep link in list for entire session.
  935. The command `org-store-link' adds a link pointing to the current
  936. location to an internal list. These links accumulate during a session.
  937. The command `org-insert-link' can be used to insert links into any
  938. Org-mode file (offering completion for all stored links). When this
  939. option is nil, every link which has been inserted once using \\[org-insert-link]
  940. will be removed from the list, to make completing the unused links
  941. more efficient."
  942. :group 'org-link-store
  943. :type 'boolean)
  944. (defgroup org-link-follow nil
  945. "Options concerning following links in Org-mode."
  946. :tag "Org Follow Link"
  947. :group 'org-link)
  948. (defcustom org-link-translation-function nil
  949. "Function to translate links with different syntax to Org syntax.
  950. This can be used to translate links created for example by the Planner
  951. or emacs-wiki packages to Org syntax.
  952. The function must accept two parameters, a TYPE containing the link
  953. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  954. which is everything after the link protocol. It should return a cons
  955. with possibly modified values of type and path.
  956. Org contains a function for this, so if you set this variable to
  957. `org-translate-link-from-planner', you should be able follow many
  958. links created by planner."
  959. :group 'org-link-follow
  960. :type 'function)
  961. (defcustom org-follow-link-hook nil
  962. "Hook that is run after a link has been followed."
  963. :group 'org-link-follow
  964. :type 'hook)
  965. (defcustom org-tab-follows-link nil
  966. "Non-nil means, on links TAB will follow the link.
  967. Needs to be set before org.el is loaded."
  968. :group 'org-link-follow
  969. :type 'boolean)
  970. (defcustom org-return-follows-link t
  971. "Non-nil means, on links RET will follow the link.
  972. Needs to be set before org.el is loaded."
  973. :group 'org-link-follow
  974. :type 'boolean)
  975. (defcustom org-mouse-1-follows-link
  976. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  977. "Non-nil means, mouse-1 on a link will follow the link.
  978. A longer mouse click will still set point. Does not work on XEmacs.
  979. Needs to be set before org.el is loaded."
  980. :group 'org-link-follow
  981. :type 'boolean)
  982. (defcustom org-mark-ring-length 4
  983. "Number of different positions to be recorded in the ring
  984. Changing this requires a restart of Emacs to work correctly."
  985. :group 'org-link-follow
  986. :type 'integer)
  987. (defcustom org-link-frame-setup
  988. '((vm . vm-visit-folder-other-frame)
  989. (gnus . gnus-other-frame)
  990. (file . find-file-other-window))
  991. "Setup the frame configuration for following links.
  992. When following a link with Emacs, it may often be useful to display
  993. this link in another window or frame. This variable can be used to
  994. set this up for the different types of links.
  995. For VM, use any of
  996. `vm-visit-folder'
  997. `vm-visit-folder-other-frame'
  998. For Gnus, use any of
  999. `gnus'
  1000. `gnus-other-frame'
  1001. `org-gnus-no-new-news'
  1002. For FILE, use any of
  1003. `find-file'
  1004. `find-file-other-window'
  1005. `find-file-other-frame'
  1006. For the calendar, use the variable `calendar-setup'.
  1007. For BBDB, it is currently only possible to display the matches in
  1008. another window."
  1009. :group 'org-link-follow
  1010. :type '(list
  1011. (cons (const vm)
  1012. (choice
  1013. (const vm-visit-folder)
  1014. (const vm-visit-folder-other-window)
  1015. (const vm-visit-folder-other-frame)))
  1016. (cons (const gnus)
  1017. (choice
  1018. (const gnus)
  1019. (const gnus-other-frame)
  1020. (const org-gnus-no-new-news)))
  1021. (cons (const file)
  1022. (choice
  1023. (const find-file)
  1024. (const find-file-other-window)
  1025. (const find-file-other-frame)))))
  1026. (defcustom org-display-internal-link-with-indirect-buffer nil
  1027. "Non-nil means, use indirect buffer to display infile links.
  1028. Activating internal links (from one location in a file to another location
  1029. in the same file) normally just jumps to the location. When the link is
  1030. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1031. another window. When this option is set, the other window actually displays
  1032. an indirect buffer clone of the current buffer, to avoid any visibility
  1033. changes to the current buffer."
  1034. :group 'org-link-follow
  1035. :type 'boolean)
  1036. (defcustom org-open-non-existing-files nil
  1037. "Non-nil means, `org-open-file' will open non-existing files.
  1038. When nil, an error will be generated."
  1039. :group 'org-link-follow
  1040. :type 'boolean)
  1041. (defcustom org-open-directory-means-index-dot-org nil
  1042. "Non-nil means, a link to a directory really means to index.org.
  1043. When nil, following a directory link will run dired or open a finder/explorer
  1044. window on that directory."
  1045. :group 'org-link-follow
  1046. :type 'boolean)
  1047. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1048. "Function and arguments to call for following mailto links.
  1049. This is a list with the first element being a lisp function, and the
  1050. remaining elements being arguments to the function. In string arguments,
  1051. %a will be replaced by the address, and %s will be replaced by the subject
  1052. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1053. :group 'org-link-follow
  1054. :type '(choice
  1055. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1056. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1057. (const :tag "message-mail" (message-mail "%a" "%s"))
  1058. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1059. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1060. "Non-nil means, ask for confirmation before executing shell links.
  1061. Shell links can be dangerous: just think about a link
  1062. [[shell:rm -rf ~/*][Google Search]]
  1063. This link would show up in your Org-mode document as \"Google Search\",
  1064. but really it would remove your entire home directory.
  1065. Therefore we advise against setting this variable to nil.
  1066. Just change it to `y-or-n-p' of you want to confirm with a
  1067. single keystroke rather than having to type \"yes\"."
  1068. :group 'org-link-follow
  1069. :type '(choice
  1070. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1071. (const :tag "with y-or-n (faster)" y-or-n-p)
  1072. (const :tag "no confirmation (dangerous)" nil)))
  1073. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1074. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1075. Elisp links can be dangerous: just think about a link
  1076. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1077. This link would show up in your Org-mode document as \"Google Search\",
  1078. but really it would remove your entire home directory.
  1079. Therefore we advise against setting this variable to nil.
  1080. Just change it to `y-or-n-p' of you want to confirm with a
  1081. single keystroke rather than having to type \"yes\"."
  1082. :group 'org-link-follow
  1083. :type '(choice
  1084. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1085. (const :tag "with y-or-n (faster)" y-or-n-p)
  1086. (const :tag "no confirmation (dangerous)" nil)))
  1087. (defconst org-file-apps-defaults-gnu
  1088. '((remote . emacs)
  1089. (system . mailcap)
  1090. (t . mailcap))
  1091. "Default file applications on a UNIX or GNU/Linux system.
  1092. See `org-file-apps'.")
  1093. (defconst org-file-apps-defaults-macosx
  1094. '((remote . emacs)
  1095. (t . "open %s")
  1096. (system . "open %s")
  1097. ("ps.gz" . "gv %s")
  1098. ("eps.gz" . "gv %s")
  1099. ("dvi" . "xdvi %s")
  1100. ("fig" . "xfig %s"))
  1101. "Default file applications on a MacOS X system.
  1102. The system \"open\" is known as a default, but we use X11 applications
  1103. for some files for which the OS does not have a good default.
  1104. See `org-file-apps'.")
  1105. (defconst org-file-apps-defaults-windowsnt
  1106. (list
  1107. '(remote . emacs)
  1108. (cons t
  1109. (list (if (featurep 'xemacs)
  1110. 'mswindows-shell-execute
  1111. 'w32-shell-execute)
  1112. "open" 'file))
  1113. (cons 'system
  1114. (list (if (featurep 'xemacs)
  1115. 'mswindows-shell-execute
  1116. 'w32-shell-execute)
  1117. "open" 'file)))
  1118. "Default file applications on a Windows NT system.
  1119. The system \"open\" is used for most files.
  1120. See `org-file-apps'.")
  1121. (defcustom org-file-apps
  1122. '(
  1123. (auto-mode . emacs)
  1124. ("\\.x?html?\\'" . default)
  1125. ("\\.pdf\\'" . default)
  1126. )
  1127. "External applications for opening `file:path' items in a document.
  1128. Org-mode uses system defaults for different file types, but
  1129. you can use this variable to set the application for a given file
  1130. extension. The entries in this list are cons cells where the car identifies
  1131. files and the cdr the corresponding command. Possible values for the
  1132. file identifier are
  1133. \"regex\" Regular expression matched against the file name. For backward
  1134. compatibility, this can also be a string with only alphanumeric
  1135. characters, which is then interpreted as an extension.
  1136. `directory' Matches a directory
  1137. `remote' Matches a remote file, accessible through tramp or efs.
  1138. Remote files most likely should be visited through Emacs
  1139. because external applications cannot handle such paths.
  1140. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1141. so all files Emacs knows how to handle. Using this with
  1142. command `emacs' will open most files in Emacs. Beware that this
  1143. will also open html files inside Emacs, unless you add
  1144. (\"html\" . default) to the list as well.
  1145. t Default for files not matched by any of the other options.
  1146. `system' The system command to open files, like `open' on Windows
  1147. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1148. that will be selected if you call `C-c C-o' with a double
  1149. `C-u C-u' prefix.
  1150. Possible values for the command are:
  1151. `emacs' The file will be visited by the current Emacs process.
  1152. `default' Use the default application for this file type, which is the
  1153. association for t in the list, most likely in the system-specific
  1154. part.
  1155. This can be used to overrule an unwanted setting in the
  1156. system-specific variable.
  1157. `system' Use the system command for opening files, like \"open\".
  1158. This command is specified by the entry whose car is `system'.
  1159. Most likely, the system-specific version of this variable
  1160. does define this command, but you can overrule/replace it
  1161. here.
  1162. string A command to be executed by a shell; %s will be replaced
  1163. by the path to the file.
  1164. sexp A Lisp form which will be evaluated. The file path will
  1165. be available in the Lisp variable `file'.
  1166. For more examples, see the system specific constants
  1167. `org-file-apps-defaults-macosx'
  1168. `org-file-apps-defaults-windowsnt'
  1169. `org-file-apps-defaults-gnu'."
  1170. :group 'org-link-follow
  1171. :type '(repeat
  1172. (cons (choice :value ""
  1173. (string :tag "Extension")
  1174. (const :tag "System command to open files" system)
  1175. (const :tag "Default for unrecognized files" t)
  1176. (const :tag "Remote file" remote)
  1177. (const :tag "Links to a directory" directory)
  1178. (const :tag "Any files that have Emacs modes"
  1179. auto-mode))
  1180. (choice :value ""
  1181. (const :tag "Visit with Emacs" emacs)
  1182. (const :tag "Use default" default)
  1183. (const :tag "Use the system command" system)
  1184. (string :tag "Command")
  1185. (sexp :tag "Lisp form")))))
  1186. (defgroup org-refile nil
  1187. "Options concerning refiling entries in Org-mode."
  1188. :tag "Org Refile"
  1189. :group 'org)
  1190. (defcustom org-directory "~/org"
  1191. "Directory with org files.
  1192. This directory will be used as default to prompt for org files.
  1193. Used by the hooks for remember.el."
  1194. :group 'org-refile
  1195. :group 'org-remember
  1196. :type 'directory)
  1197. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1198. "Default target for storing notes.
  1199. Used by the hooks for remember.el. This can be a string, or nil to mean
  1200. the value of `remember-data-file'.
  1201. You can set this on a per-template basis with the variable
  1202. `org-remember-templates'."
  1203. :group 'org-refile
  1204. :group 'org-remember
  1205. :type '(choice
  1206. (const :tag "Default from remember-data-file" nil)
  1207. file))
  1208. (defcustom org-goto-interface 'outline
  1209. "The default interface to be used for `org-goto'.
  1210. Allowed values are:
  1211. outline The interface shows an outline of the relevant file
  1212. and the correct heading is found by moving through
  1213. the outline or by searching with incremental search.
  1214. outline-path-completion Headlines in the current buffer are offered via
  1215. completion. This is the interface also used by
  1216. the refile command."
  1217. :group 'org-refile
  1218. :type '(choice
  1219. (const :tag "Outline" outline)
  1220. (const :tag "Outline-path-completion" outline-path-completion)))
  1221. (defcustom org-goto-max-level 5
  1222. "Maximum level to be considered when running org-goto with refile interface."
  1223. :group 'org-refile
  1224. :type 'number)
  1225. (defcustom org-reverse-note-order nil
  1226. "Non-nil means, store new notes at the beginning of a file or entry.
  1227. When nil, new notes will be filed to the end of a file or entry.
  1228. This can also be a list with cons cells of regular expressions that
  1229. are matched against file names, and values."
  1230. :group 'org-remember
  1231. :group 'org-refile
  1232. :type '(choice
  1233. (const :tag "Reverse always" t)
  1234. (const :tag "Reverse never" nil)
  1235. (repeat :tag "By file name regexp"
  1236. (cons regexp boolean))))
  1237. (defcustom org-refile-targets nil
  1238. "Targets for refiling entries with \\[org-refile].
  1239. This is list of cons cells. Each cell contains:
  1240. - a specification of the files to be considered, either a list of files,
  1241. or a symbol whose function or variable value will be used to retrieve
  1242. a file name or a list of file names. If you use `org-agenda-files' for
  1243. that, all agenda files will be scanned for targets. Nil means, consider
  1244. headings in the current buffer.
  1245. - A specification of how to select find candidate refile targets. This
  1246. may be any of
  1247. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1248. This tag has to be present in all target headlines, inheritance will
  1249. not be considered.
  1250. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1251. todo keyword.
  1252. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1253. headlines that are refiling targets.
  1254. - a cons cell (:level . N). Any headline of level N is considered a target.
  1255. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1256. When this variable is nil, all top-level headlines in the current buffer
  1257. are used, equivalent to the value `((nil . (:level . 1))'."
  1258. :group 'org-refile
  1259. :type '(repeat
  1260. (cons
  1261. (choice :value org-agenda-files
  1262. (const :tag "All agenda files" org-agenda-files)
  1263. (const :tag "Current buffer" nil)
  1264. (function) (variable) (file))
  1265. (choice :tag "Identify target headline by"
  1266. (cons :tag "Specific tag" (const :value :tag) (string))
  1267. (cons :tag "TODO keyword" (const :value :todo) (string))
  1268. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1269. (cons :tag "Level number" (const :value :level) (integer))
  1270. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1271. (defcustom org-refile-use-outline-path nil
  1272. "Non-nil means, provide refile targets as paths.
  1273. So a level 3 headline will be available as level1/level2/level3.
  1274. When the value is `file', also include the file name (without directory)
  1275. into the path. When `full-file-path', include the full file path."
  1276. :group 'org-refile
  1277. :type '(choice
  1278. (const :tag "Not" nil)
  1279. (const :tag "Yes" t)
  1280. (const :tag "Start with file name" file)
  1281. (const :tag "Start with full file path" full-file-path)))
  1282. (defcustom org-outline-path-complete-in-steps t
  1283. "Non-nil means, complete the outline path in hierarchical steps.
  1284. When Org-mode uses the refile interface to select an outline path
  1285. \(see variable `org-refile-use-outline-path'), the completion of
  1286. the path can be done is a single go, or if can be done in steps down
  1287. the headline hierarchy. Going in steps is probably the best if you
  1288. do not use a special completion package like `ido' or `icicles'.
  1289. However, when using these packages, going in one step can be very
  1290. fast, while still showing the whole path to the entry."
  1291. :group 'org-refile
  1292. :type 'boolean)
  1293. (defgroup org-todo nil
  1294. "Options concerning TODO items in Org-mode."
  1295. :tag "Org TODO"
  1296. :group 'org)
  1297. (defgroup org-progress nil
  1298. "Options concerning Progress logging in Org-mode."
  1299. :tag "Org Progress"
  1300. :group 'org-time)
  1301. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1302. "List of TODO entry keyword sequences and their interpretation.
  1303. \\<org-mode-map>This is a list of sequences.
  1304. Each sequence starts with a symbol, either `sequence' or `type',
  1305. indicating if the keywords should be interpreted as a sequence of
  1306. action steps, or as different types of TODO items. The first
  1307. keywords are states requiring action - these states will select a headline
  1308. for inclusion into the global TODO list Org-mode produces. If one of
  1309. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1310. signify that no further action is necessary. If \"|\" is not found,
  1311. the last keyword is treated as the only DONE state of the sequence.
  1312. The command \\[org-todo] cycles an entry through these states, and one
  1313. additional state where no keyword is present. For details about this
  1314. cycling, see the manual.
  1315. TODO keywords and interpretation can also be set on a per-file basis with
  1316. the special #+SEQ_TODO and #+TYP_TODO lines.
  1317. Each keyword can optionally specify a character for fast state selection
  1318. \(in combination with the variable `org-use-fast-todo-selection')
  1319. and specifiers for state change logging, using the same syntax
  1320. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1321. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1322. indicates to record a time stamp each time this state is selected.
  1323. Each keyword may also specify if a timestamp or a note should be
  1324. recorded when entering or leaving the state, by adding additional
  1325. characters in the parenthesis after the keyword. This looks like this:
  1326. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1327. record only the time of the state change. With X and Y being either
  1328. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1329. Y when leaving the state if and only if the *target* state does not
  1330. define X. You may omit any of the fast-selection key or X or /Y,
  1331. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1332. For backward compatibility, this variable may also be just a list
  1333. of keywords - in this case the interpretation (sequence or type) will be
  1334. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1335. :group 'org-todo
  1336. :group 'org-keywords
  1337. :type '(choice
  1338. (repeat :tag "Old syntax, just keywords"
  1339. (string :tag "Keyword"))
  1340. (repeat :tag "New syntax"
  1341. (cons
  1342. (choice
  1343. :tag "Interpretation"
  1344. (const :tag "Sequence (cycling hits every state)" sequence)
  1345. (const :tag "Type (cycling directly to DONE)" type))
  1346. (repeat
  1347. (string :tag "Keyword"))))))
  1348. (defvar org-todo-keywords-1 nil
  1349. "All TODO and DONE keywords active in a buffer.")
  1350. (make-variable-buffer-local 'org-todo-keywords-1)
  1351. (defvar org-todo-keywords-for-agenda nil)
  1352. (defvar org-done-keywords-for-agenda nil)
  1353. (defvar org-todo-keyword-alist-for-agenda nil)
  1354. (defvar org-tag-alist-for-agenda nil)
  1355. (defvar org-agenda-contributing-files nil)
  1356. (defvar org-not-done-keywords nil)
  1357. (make-variable-buffer-local 'org-not-done-keywords)
  1358. (defvar org-done-keywords nil)
  1359. (make-variable-buffer-local 'org-done-keywords)
  1360. (defvar org-todo-heads nil)
  1361. (make-variable-buffer-local 'org-todo-heads)
  1362. (defvar org-todo-sets nil)
  1363. (make-variable-buffer-local 'org-todo-sets)
  1364. (defvar org-todo-log-states nil)
  1365. (make-variable-buffer-local 'org-todo-log-states)
  1366. (defvar org-todo-kwd-alist nil)
  1367. (make-variable-buffer-local 'org-todo-kwd-alist)
  1368. (defvar org-todo-key-alist nil)
  1369. (make-variable-buffer-local 'org-todo-key-alist)
  1370. (defvar org-todo-key-trigger nil)
  1371. (make-variable-buffer-local 'org-todo-key-trigger)
  1372. (defcustom org-todo-interpretation 'sequence
  1373. "Controls how TODO keywords are interpreted.
  1374. This variable is in principle obsolete and is only used for
  1375. backward compatibility, if the interpretation of todo keywords is
  1376. not given already in `org-todo-keywords'. See that variable for
  1377. more information."
  1378. :group 'org-todo
  1379. :group 'org-keywords
  1380. :type '(choice (const sequence)
  1381. (const type)))
  1382. (defcustom org-use-fast-todo-selection t
  1383. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1384. This variable describes if and under what circumstances the cycling
  1385. mechanism for TODO keywords will be replaced by a single-key, direct
  1386. selection scheme.
  1387. When nil, fast selection is never used.
  1388. When the symbol `prefix', it will be used when `org-todo' is called with
  1389. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1390. in an agenda buffer.
  1391. When t, fast selection is used by default. In this case, the prefix
  1392. argument forces cycling instead.
  1393. In all cases, the special interface is only used if access keys have actually
  1394. been assigned by the user, i.e. if keywords in the configuration are followed
  1395. by a letter in parenthesis, like TODO(t)."
  1396. :group 'org-todo
  1397. :type '(choice
  1398. (const :tag "Never" nil)
  1399. (const :tag "By default" t)
  1400. (const :tag "Only with C-u C-c C-t" prefix)))
  1401. (defcustom org-provide-todo-statistics t
  1402. "Non-nil means, update todo statistics after insert and toggle.
  1403. When this is set, todo statistics is updated in the parent of the current
  1404. entry each time a todo state is changed."
  1405. :group 'org-todo
  1406. :type 'boolean)
  1407. (defcustom org-after-todo-state-change-hook nil
  1408. "Hook which is run after the state of a TODO item was changed.
  1409. The new state (a string with a TODO keyword, or nil) is available in the
  1410. Lisp variable `state'."
  1411. :group 'org-todo
  1412. :type 'hook)
  1413. (defvar org-blocker-hook nil
  1414. "Hook for functions that are allowed to block a state change.
  1415. Each function gets as its single argument a property list, see
  1416. `org-trigger-hook' for more information about this list.
  1417. If any of the functions in this hook returns nil, the state change
  1418. is blocked.")
  1419. (defvar org-trigger-hook nil
  1420. "Hook for functions that are triggered by a state change.
  1421. Each function gets as its single argument a property list with at least
  1422. the following elements:
  1423. (:type type-of-change :position pos-at-entry-start
  1424. :from old-state :to new-state)
  1425. Depending on the type, more properties may be present.
  1426. This mechanism is currently implemented for:
  1427. TODO state changes
  1428. ------------------
  1429. :type todo-state-change
  1430. :from previous state (keyword as a string), or nil, or a symbol
  1431. 'todo' or 'done', to indicate the general type of state.
  1432. :to new state, like in :from")
  1433. (defcustom org-enforce-todo-dependencies nil
  1434. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1435. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1436. be blocked if any prior sibling is not yet done.
  1437. This variable needs to be set before org.el is loaded, and you need to
  1438. restart Emacs after a change to make the change effective. The only way
  1439. to change is while Emacs is running is through the customize interface."
  1440. :set (lambda (var val)
  1441. (set var val)
  1442. (if val
  1443. (add-hook 'org-blocker-hook
  1444. 'org-block-todo-from-children-or-siblings)
  1445. (remove-hook 'org-blocker-hook
  1446. 'org-block-todo-from-children-or-siblings)))
  1447. :group 'org-todo
  1448. :type 'boolean)
  1449. (defcustom org-enforce-todo-checkbox-dependencies nil
  1450. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1451. When this is nil, checkboxes have no influence on switching TODO states.
  1452. When non-nil, you first need to check off all check boxes before the TODO
  1453. entry can be switched to DONE.
  1454. This variable needs to be set before org.el is loaded, and you need to
  1455. restart Emacs after a change to make the change effective. The only way
  1456. to change is while Emacs is running is through the customize interface."
  1457. :set (lambda (var val)
  1458. (set var val)
  1459. (if val
  1460. (add-hook 'org-blocker-hook
  1461. 'org-block-todo-from-checkboxes)
  1462. (remove-hook 'org-blocker-hook
  1463. 'org-block-todo-from-checkboxes)))
  1464. :group 'org-todo
  1465. :type 'boolean)
  1466. (defcustom org-todo-state-tags-triggers nil
  1467. "Tag changes that should be triggered by TODO state changes.
  1468. This is a list. Each entry is
  1469. (state-change (tag . flag) .......)
  1470. State-change can be a string with a state, and empty string to indicate the
  1471. state that has no TODO keyword, or it can be one of the symbols `todo'
  1472. or `done', meaning any not-done or done state, respectively."
  1473. :group 'org-todo
  1474. :group 'org-tags
  1475. :type '(repeat
  1476. (cons (choice :tag "When changing to"
  1477. (const :tag "Not-done state" todo)
  1478. (const :tag "Done state" done)
  1479. (string :tag "State"))
  1480. (repeat
  1481. (cons :tag "Tag action"
  1482. (string :tag "Tag")
  1483. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1484. (defcustom org-log-done nil
  1485. "Information to record when a task moves to the DONE state.
  1486. Possible values are:
  1487. nil Don't add anything, just change the keyword
  1488. time Add a time stamp to the task
  1489. note Prompt a closing note and add it with template `org-log-note-headings'
  1490. This option can also be set with on a per-file-basis with
  1491. #+STARTUP: nologdone
  1492. #+STARTUP: logdone
  1493. #+STARTUP: lognotedone
  1494. You can have local logging settings for a subtree by setting the LOGGING
  1495. property to one or more of these keywords."
  1496. :group 'org-todo
  1497. :group 'org-progress
  1498. :type '(choice
  1499. (const :tag "No logging" nil)
  1500. (const :tag "Record CLOSED timestamp" time)
  1501. (const :tag "Record CLOSED timestamp with closing note." note)))
  1502. ;; Normalize old uses of org-log-done.
  1503. (cond
  1504. ((eq org-log-done t) (setq org-log-done 'time))
  1505. ((and (listp org-log-done) (memq 'done org-log-done))
  1506. (setq org-log-done 'note)))
  1507. (defcustom org-log-note-clock-out nil
  1508. "Non-nil means, record a note when clocking out of an item.
  1509. This can also be configured on a per-file basis by adding one of
  1510. the following lines anywhere in the buffer:
  1511. #+STARTUP: lognoteclock-out
  1512. #+STARTUP: nolognoteclock-out"
  1513. :group 'org-todo
  1514. :group 'org-progress
  1515. :type 'boolean)
  1516. (defcustom org-log-done-with-time t
  1517. "Non-nil means, the CLOSED time stamp will contain date and time.
  1518. When nil, only the date will be recorded."
  1519. :group 'org-progress
  1520. :type 'boolean)
  1521. (defcustom org-log-note-headings
  1522. '((done . "CLOSING NOTE %t")
  1523. (state . "State %-12s %t")
  1524. (note . "Note taken on %t")
  1525. (clock-out . ""))
  1526. "Headings for notes added to entries.
  1527. The value is an alist, with the car being a symbol indicating the note
  1528. context, and the cdr is the heading to be used. The heading may also be the
  1529. empty string.
  1530. %t in the heading will be replaced by a time stamp.
  1531. %s will be replaced by the new TODO state, in double quotes.
  1532. %u will be replaced by the user name.
  1533. %U will be replaced by the full user name."
  1534. :group 'org-todo
  1535. :group 'org-progress
  1536. :type '(list :greedy t
  1537. (cons (const :tag "Heading when closing an item" done) string)
  1538. (cons (const :tag
  1539. "Heading when changing todo state (todo sequence only)"
  1540. state) string)
  1541. (cons (const :tag "Heading when just taking a note" note) string)
  1542. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1543. (unless (assq 'note org-log-note-headings)
  1544. (push '(note . "%t") org-log-note-headings))
  1545. (defcustom org-log-state-notes-insert-after-drawers nil
  1546. "Non-nil means, insert state change notes after any drawers in entry.
  1547. Only the drawers that *immediately* follow the headline and the
  1548. deadline/scheduled line are skipped.
  1549. When nil, insert notes right after the heading and perhaps the line
  1550. with deadline/scheduling if present."
  1551. :group 'org-todo
  1552. :group 'org-progress
  1553. :type 'boolean)
  1554. (defcustom org-log-states-order-reversed t
  1555. "Non-nil means, the latest state change note will be directly after heading.
  1556. When nil, the notes will be orderer according to time."
  1557. :group 'org-todo
  1558. :group 'org-progress
  1559. :type 'boolean)
  1560. (defcustom org-log-repeat 'time
  1561. "Non-nil means, record moving through the DONE state when triggering repeat.
  1562. An auto-repeating tasks is immediately switched back to TODO when marked
  1563. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1564. the TODO keyword definition, or recording a closing note by setting
  1565. `org-log-done', there will be no record of the task moving through DONE.
  1566. This variable forces taking a note anyway. Possible values are:
  1567. nil Don't force a record
  1568. time Record a time stamp
  1569. note Record a note
  1570. This option can also be set with on a per-file-basis with
  1571. #+STARTUP: logrepeat
  1572. #+STARTUP: lognoterepeat
  1573. #+STARTUP: nologrepeat
  1574. You can have local logging settings for a subtree by setting the LOGGING
  1575. property to one or more of these keywords."
  1576. :group 'org-todo
  1577. :group 'org-progress
  1578. :type '(choice
  1579. (const :tag "Don't force a record" nil)
  1580. (const :tag "Force recording the DONE state" time)
  1581. (const :tag "Force recording a note with the DONE state" note)))
  1582. (defgroup org-priorities nil
  1583. "Priorities in Org-mode."
  1584. :tag "Org Priorities"
  1585. :group 'org-todo)
  1586. (defcustom org-highest-priority ?A
  1587. "The highest priority of TODO items. A character like ?A, ?B etc.
  1588. Must have a smaller ASCII number than `org-lowest-priority'."
  1589. :group 'org-priorities
  1590. :type 'character)
  1591. (defcustom org-lowest-priority ?C
  1592. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1593. Must have a larger ASCII number than `org-highest-priority'."
  1594. :group 'org-priorities
  1595. :type 'character)
  1596. (defcustom org-default-priority ?B
  1597. "The default priority of TODO items.
  1598. This is the priority an item get if no explicit priority is given."
  1599. :group 'org-priorities
  1600. :type 'character)
  1601. (defcustom org-priority-start-cycle-with-default t
  1602. "Non-nil means, start with default priority when starting to cycle.
  1603. When this is nil, the first step in the cycle will be (depending on the
  1604. command used) one higher or lower that the default priority."
  1605. :group 'org-priorities
  1606. :type 'boolean)
  1607. (defgroup org-time nil
  1608. "Options concerning time stamps and deadlines in Org-mode."
  1609. :tag "Org Time"
  1610. :group 'org)
  1611. (defcustom org-insert-labeled-timestamps-at-point nil
  1612. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1613. When nil, these labeled time stamps are forces into the second line of an
  1614. entry, just after the headline. When scheduling from the global TODO list,
  1615. the time stamp will always be forced into the second line."
  1616. :group 'org-time
  1617. :type 'boolean)
  1618. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1619. "Formats for `format-time-string' which are used for time stamps.
  1620. It is not recommended to change this constant.")
  1621. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1622. "Number of minutes to round time stamps to.
  1623. These are two values, the first applies when first creating a time stamp.
  1624. The second applies when changing it with the commands `S-up' and `S-down'.
  1625. When changing the time stamp, this means that it will change in steps
  1626. of N minutes, as given by the second value.
  1627. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1628. numbers should be factors of 60, so for example 5, 10, 15.
  1629. When this is larger than 1, you can still force an exact time-stamp by using
  1630. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1631. and by using a prefix arg to `S-up/down' to specify the exact number
  1632. of minutes to shift."
  1633. :group 'org-time
  1634. :get '(lambda (var) ; Make sure all entries have 5 elements
  1635. (if (integerp (default-value var))
  1636. (list (default-value var) 5)
  1637. (default-value var)))
  1638. :type '(list
  1639. (integer :tag "when inserting times")
  1640. (integer :tag "when modifying times")))
  1641. ;; Normalize old customizations of this variable.
  1642. (when (integerp org-time-stamp-rounding-minutes)
  1643. (setq org-time-stamp-rounding-minutes
  1644. (list org-time-stamp-rounding-minutes
  1645. org-time-stamp-rounding-minutes)))
  1646. (defcustom org-display-custom-times nil
  1647. "Non-nil means, overlay custom formats over all time stamps.
  1648. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1649. To turn this on on a per-file basis, insert anywhere in the file:
  1650. #+STARTUP: customtime"
  1651. :group 'org-time
  1652. :set 'set-default
  1653. :type 'sexp)
  1654. (make-variable-buffer-local 'org-display-custom-times)
  1655. (defcustom org-time-stamp-custom-formats
  1656. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1657. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1658. These are overlayed over the default ISO format if the variable
  1659. `org-display-custom-times' is set. Time like %H:%M should be at the
  1660. end of the second format."
  1661. :group 'org-time
  1662. :type 'sexp)
  1663. (defun org-time-stamp-format (&optional long inactive)
  1664. "Get the right format for a time string."
  1665. (let ((f (if long (cdr org-time-stamp-formats)
  1666. (car org-time-stamp-formats))))
  1667. (if inactive
  1668. (concat "[" (substring f 1 -1) "]")
  1669. f)))
  1670. (defcustom org-time-clocksum-format "%d:%02d"
  1671. "The format string used when creating CLOCKSUM lines, or when
  1672. org-mode generates a time duration."
  1673. :group 'org-time
  1674. :type 'string)
  1675. (defcustom org-deadline-warning-days 14
  1676. "No. of days before expiration during which a deadline becomes active.
  1677. This variable governs the display in sparse trees and in the agenda.
  1678. When 0 or negative, it means use this number (the absolute value of it)
  1679. even if a deadline has a different individual lead time specified."
  1680. :group 'org-time
  1681. :group 'org-agenda-daily/weekly
  1682. :type 'number)
  1683. (defcustom org-read-date-prefer-future t
  1684. "Non-nil means, assume future for incomplete date input from user.
  1685. This affects the following situations:
  1686. 1. The user gives a day, but no month.
  1687. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1688. read this as the third of *next* month. However, if you enter \"17\",
  1689. it will be considered as *this* month.
  1690. 2. The user gives a month but not a year.
  1691. For example, if it is april and you enter \"feb 2\", this will be read
  1692. as feb 2, *next* year. \"May 5\", however, will be this year.
  1693. Currently this does not work for ISO week specifications.
  1694. When this option is nil, the current month and year will always be used
  1695. as defaults."
  1696. :group 'org-time
  1697. :type 'boolean)
  1698. (defcustom org-read-date-display-live t
  1699. "Non-nil means, display current interpretation of date prompt live.
  1700. This display will be in an overlay, in the minibuffer."
  1701. :group 'org-time
  1702. :type 'boolean)
  1703. (defcustom org-read-date-popup-calendar t
  1704. "Non-nil means, pop up a calendar when prompting for a date.
  1705. In the calendar, the date can be selected with mouse-1. However, the
  1706. minibuffer will also be active, and you can simply enter the date as well.
  1707. When nil, only the minibuffer will be available."
  1708. :group 'org-time
  1709. :type 'boolean)
  1710. (if (fboundp 'defvaralias)
  1711. (defvaralias 'org-popup-calendar-for-date-prompt
  1712. 'org-read-date-popup-calendar))
  1713. (defcustom org-extend-today-until 0
  1714. "The hour when your day really ends. Must be an integer.
  1715. This has influence for the following applications:
  1716. - When switching the agenda to \"today\". It it is still earlier than
  1717. the time given here, the day recognized as TODAY is actually yesterday.
  1718. - When a date is read from the user and it is still before the time given
  1719. here, the current date and time will be assumed to be yesterday, 23:59.
  1720. Also, timestamps inserted in remember templates follow this rule.
  1721. IMPORTANT: This is a feature whose implementation is and likely will
  1722. remain incomplete. Really, it is only here because past midnight seems to
  1723. be the favorite working time of John Wiegley :-)"
  1724. :group 'org-time
  1725. :type 'number)
  1726. (defcustom org-edit-timestamp-down-means-later nil
  1727. "Non-nil means, S-down will increase the time in a time stamp.
  1728. When nil, S-up will increase."
  1729. :group 'org-time
  1730. :type 'boolean)
  1731. (defcustom org-calendar-follow-timestamp-change t
  1732. "Non-nil means, make the calendar window follow timestamp changes.
  1733. When a timestamp is modified and the calendar window is visible, it will be
  1734. moved to the new date."
  1735. :group 'org-time
  1736. :type 'boolean)
  1737. (defgroup org-tags nil
  1738. "Options concerning tags in Org-mode."
  1739. :tag "Org Tags"
  1740. :group 'org)
  1741. (defcustom org-tag-alist nil
  1742. "List of tags allowed in Org-mode files.
  1743. When this list is nil, Org-mode will base TAG input on what is already in the
  1744. buffer.
  1745. The value of this variable is an alist, the car of each entry must be a
  1746. keyword as a string, the cdr may be a character that is used to select
  1747. that tag through the fast-tag-selection interface.
  1748. See the manual for details."
  1749. :group 'org-tags
  1750. :type '(repeat
  1751. (choice
  1752. (cons (string :tag "Tag name")
  1753. (character :tag "Access char"))
  1754. (const :tag "Start radio group" (:startgroup))
  1755. (const :tag "End radio group" (:endgroup)))))
  1756. (defvar org-file-tags nil
  1757. "List of tags that can be inherited by all entries in the file.
  1758. The tags will be inherited if the variable `org-use-tag-inheritance'
  1759. says they should be.
  1760. This variable is populated from #+TAG lines.")
  1761. (defcustom org-use-fast-tag-selection 'auto
  1762. "Non-nil means, use fast tag selection scheme.
  1763. This is a special interface to select and deselect tags with single keys.
  1764. When nil, fast selection is never used.
  1765. When the symbol `auto', fast selection is used if and only if selection
  1766. characters for tags have been configured, either through the variable
  1767. `org-tag-alist' or through a #+TAGS line in the buffer.
  1768. When t, fast selection is always used and selection keys are assigned
  1769. automatically if necessary."
  1770. :group 'org-tags
  1771. :type '(choice
  1772. (const :tag "Always" t)
  1773. (const :tag "Never" nil)
  1774. (const :tag "When selection characters are configured" 'auto)))
  1775. (defcustom org-fast-tag-selection-single-key nil
  1776. "Non-nil means, fast tag selection exits after first change.
  1777. When nil, you have to press RET to exit it.
  1778. During fast tag selection, you can toggle this flag with `C-c'.
  1779. This variable can also have the value `expert'. In this case, the window
  1780. displaying the tags menu is not even shown, until you press C-c again."
  1781. :group 'org-tags
  1782. :type '(choice
  1783. (const :tag "No" nil)
  1784. (const :tag "Yes" t)
  1785. (const :tag "Expert" expert)))
  1786. (defvar org-fast-tag-selection-include-todo nil
  1787. "Non-nil means, fast tags selection interface will also offer TODO states.
  1788. This is an undocumented feature, you should not rely on it.")
  1789. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1790. "The column to which tags should be indented in a headline.
  1791. If this number is positive, it specifies the column. If it is negative,
  1792. it means that the tags should be flushright to that column. For example,
  1793. -80 works well for a normal 80 character screen."
  1794. :group 'org-tags
  1795. :type 'integer)
  1796. (defcustom org-auto-align-tags t
  1797. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1798. These operations change the length of a headline and therefore shift
  1799. the tags around. With this options turned on, after each such operation
  1800. the tags are again aligned to `org-tags-column'."
  1801. :group 'org-tags
  1802. :type 'boolean)
  1803. (defcustom org-use-tag-inheritance t
  1804. "Non-nil means, tags in levels apply also for sublevels.
  1805. When nil, only the tags directly given in a specific line apply there.
  1806. This may also be a list of tags that should be inherited, or a regexp that
  1807. matches tags that should be inherited. Additional control is possible
  1808. with the variable `org-tags-exclude-from-inheritance' which gives an
  1809. explicit list of tags to be excluded from inheritance., even if the value of
  1810. `org-use-tag-inheritance' would select it for inheritance.
  1811. If this option is t, a match early-on in a tree can lead to a large
  1812. number of matches in the subtree when constructing the agenda or creating
  1813. a sparse tree. If you only want to see the first match in a tree during
  1814. a search, check out the variable `org-tags-match-list-sublevels'."
  1815. :group 'org-tags
  1816. :type '(choice
  1817. (const :tag "Not" nil)
  1818. (const :tag "Always" t)
  1819. (repeat :tag "Specific tags" (string :tag "Tag"))
  1820. (regexp :tag "Tags matched by regexp")))
  1821. (defcustom org-tags-exclude-from-inheritance nil
  1822. "List of tags that should never be inherited.
  1823. This is a way to exclude a few tags from inheritance. For way to do
  1824. the opposite, to actively allow inheritance for selected tags,
  1825. see the variable `org-use-tag-inheritance'."
  1826. :group 'org-tags
  1827. :type '(repeat (string :tag "Tag")))
  1828. (defun org-tag-inherit-p (tag)
  1829. "Check if TAG is one that should be inherited."
  1830. (cond
  1831. ((member tag org-tags-exclude-from-inheritance) nil)
  1832. ((eq org-use-tag-inheritance t) t)
  1833. ((not org-use-tag-inheritance) nil)
  1834. ((stringp org-use-tag-inheritance)
  1835. (string-match org-use-tag-inheritance tag))
  1836. ((listp org-use-tag-inheritance)
  1837. (member tag org-use-tag-inheritance))
  1838. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1839. (defcustom org-tags-match-list-sublevels t
  1840. "Non-nil means list also sublevels of headlines matching tag search.
  1841. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1842. the sublevels of a headline matching a tag search often also match
  1843. the same search. Listing all of them can create very long lists.
  1844. Setting this variable to nil causes subtrees of a match to be skipped.
  1845. This option is off by default, because inheritance in on. If you turn
  1846. inheritance off, you very likely want to turn this option on.
  1847. As a special case, if the tag search is restricted to TODO items, the
  1848. value of this variable is ignored and sublevels are always checked, to
  1849. make sure all corresponding TODO items find their way into the list.
  1850. This variable is semi-obsolete and probably should always be true. It
  1851. is better to limit inheritance to certain tags using the variables
  1852. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1853. :group 'org-tags
  1854. :type 'boolean)
  1855. (defvar org-tags-history nil
  1856. "History of minibuffer reads for tags.")
  1857. (defvar org-last-tags-completion-table nil
  1858. "The last used completion table for tags.")
  1859. (defvar org-after-tags-change-hook nil
  1860. "Hook that is run after the tags in a line have changed.")
  1861. (defgroup org-properties nil
  1862. "Options concerning properties in Org-mode."
  1863. :tag "Org Properties"
  1864. :group 'org)
  1865. (defcustom org-property-format "%-10s %s"
  1866. "How property key/value pairs should be formatted by `indent-line'.
  1867. When `indent-line' hits a property definition, it will format the line
  1868. according to this format, mainly to make sure that the values are
  1869. lined-up with respect to each other."
  1870. :group 'org-properties
  1871. :type 'string)
  1872. (defcustom org-use-property-inheritance nil
  1873. "Non-nil means, properties apply also for sublevels.
  1874. This setting is chiefly used during property searches. Turning it on can
  1875. cause significant overhead when doing a search, which is why it is not
  1876. on by default.
  1877. When nil, only the properties directly given in the current entry count.
  1878. When t, every property is inherited. The value may also be a list of
  1879. properties that should have inheritance, or a regular expression matching
  1880. properties that should be inherited.
  1881. However, note that some special properties use inheritance under special
  1882. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1883. and the properties ending in \"_ALL\" when they are used as descriptor
  1884. for valid values of a property.
  1885. Note for programmers:
  1886. When querying an entry with `org-entry-get', you can control if inheritance
  1887. should be used. By default, `org-entry-get' looks only at the local
  1888. properties. You can request inheritance by setting the inherit argument
  1889. to t (to force inheritance) or to `selective' (to respect the setting
  1890. in this variable)."
  1891. :group 'org-properties
  1892. :type '(choice
  1893. (const :tag "Not" nil)
  1894. (const :tag "Always" t)
  1895. (repeat :tag "Specific properties" (string :tag "Property"))
  1896. (regexp :tag "Properties matched by regexp")))
  1897. (defun org-property-inherit-p (property)
  1898. "Check if PROPERTY is one that should be inherited."
  1899. (cond
  1900. ((eq org-use-property-inheritance t) t)
  1901. ((not org-use-property-inheritance) nil)
  1902. ((stringp org-use-property-inheritance)
  1903. (string-match org-use-property-inheritance property))
  1904. ((listp org-use-property-inheritance)
  1905. (member property org-use-property-inheritance))
  1906. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1907. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1908. "The default column format, if no other format has been defined.
  1909. This variable can be set on the per-file basis by inserting a line
  1910. #+COLUMNS: %25ITEM ....."
  1911. :group 'org-properties
  1912. :type 'string)
  1913. (defcustom org-columns-ellipses ".."
  1914. "The ellipses to be used when a field in column view is truncated.
  1915. When this is the empty string, as many characters as possible are shown,
  1916. but then there will be no visual indication that the field has been truncated.
  1917. When this is a string of length N, the last N characters of a truncated
  1918. field are replaced by this string. If the column is narrower than the
  1919. ellipses string, only part of the ellipses string will be shown."
  1920. :group 'org-properties
  1921. :type 'string)
  1922. (defcustom org-columns-modify-value-for-display-function nil
  1923. "Function that modifies values for display in column view.
  1924. For example, it can be used to cut out a certain part from a time stamp.
  1925. The function must take 2 arguments:
  1926. column-title The title of the column (*not* the property name)
  1927. value The value that should be modified.
  1928. The function should return the value that should be displayed,
  1929. or nil if the normal value should be used."
  1930. :group 'org-properties
  1931. :type 'function)
  1932. (defcustom org-effort-property "Effort"
  1933. "The property that is being used to keep track of effort estimates.
  1934. Effort estimates given in this property need to have the format H:MM."
  1935. :group 'org-properties
  1936. :group 'org-progress
  1937. :type '(string :tag "Property"))
  1938. (defconst org-global-properties-fixed
  1939. '(("VISIBILITY_ALL" . "folded children content all"))
  1940. "List of property/value pairs that can be inherited by any entry.
  1941. These are fixed values, for the preset properties.")
  1942. (defcustom org-global-properties nil
  1943. "List of property/value pairs that can be inherited by any entry.
  1944. You can set buffer-local values for the same purpose in the variable
  1945. `org-file-properties' this by adding lines like
  1946. #+PROPERTY: NAME VALUE"
  1947. :group 'org-properties
  1948. :type '(repeat
  1949. (cons (string :tag "Property")
  1950. (string :tag "Value"))))
  1951. (defvar org-file-properties nil
  1952. "List of property/value pairs that can be inherited by any entry.
  1953. Valid for the current buffer.
  1954. This variable is populated from #+PROPERTY lines.")
  1955. (make-variable-buffer-local 'org-file-properties)
  1956. (defgroup org-agenda nil
  1957. "Options concerning agenda views in Org-mode."
  1958. :tag "Org Agenda"
  1959. :group 'org)
  1960. (defvar org-category nil
  1961. "Variable used by org files to set a category for agenda display.
  1962. Such files should use a file variable to set it, for example
  1963. # -*- mode: org; org-category: \"ELisp\"
  1964. or contain a special line
  1965. #+CATEGORY: ELisp
  1966. If the file does not specify a category, then file's base name
  1967. is used instead.")
  1968. (make-variable-buffer-local 'org-category)
  1969. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1970. (defcustom org-agenda-files nil
  1971. "The files to be used for agenda display.
  1972. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1973. \\[org-remove-file]. You can also use customize to edit the list.
  1974. If an entry is a directory, all files in that directory that are matched by
  1975. `org-agenda-file-regexp' will be part of the file list.
  1976. If the value of the variable is not a list but a single file name, then
  1977. the list of agenda files is actually stored and maintained in that file, one
  1978. agenda file per line."
  1979. :group 'org-agenda
  1980. :type '(choice
  1981. (repeat :tag "List of files and directories" file)
  1982. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1983. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1984. "Regular expression to match files for `org-agenda-files'.
  1985. If any element in the list in that variable contains a directory instead
  1986. of a normal file, all files in that directory that are matched by this
  1987. regular expression will be included."
  1988. :group 'org-agenda
  1989. :type 'regexp)
  1990. (defcustom org-agenda-text-search-extra-files nil
  1991. "List of extra files to be searched by text search commands.
  1992. These files will be search in addition to the agenda files by the
  1993. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1994. Note that these files will only be searched for text search commands,
  1995. not for the other agenda views like todo lists, tag searches or the weekly
  1996. agenda. This variable is intended to list notes and possibly archive files
  1997. that should also be searched by these two commands.
  1998. In fact, if the first element in the list is the symbol `agenda-archives',
  1999. than all archive files of all agenda files will be added to the search
  2000. scope."
  2001. :group 'org-agenda
  2002. :type '(set :greedy t
  2003. (const :tag "Agenda Archives" agenda-archives)
  2004. (repeat :inline t (file))))
  2005. (if (fboundp 'defvaralias)
  2006. (defvaralias 'org-agenda-multi-occur-extra-files
  2007. 'org-agenda-text-search-extra-files))
  2008. (defcustom org-agenda-skip-unavailable-files nil
  2009. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2010. A nil value means to remove them, after a query, from the list."
  2011. :group 'org-agenda
  2012. :type 'boolean)
  2013. (defcustom org-calendar-to-agenda-key [?c]
  2014. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2015. The command `org-calendar-goto-agenda' will be bound to this key. The
  2016. default is the character `c' because then `c' can be used to switch back and
  2017. forth between agenda and calendar."
  2018. :group 'org-agenda
  2019. :type 'sexp)
  2020. (defcustom org-calendar-agenda-action-key [?k]
  2021. "The key to be installed in `calendar-mode-map' for agenda-action.
  2022. The command `org-agenda-action' will be bound to this key. The
  2023. default is the character `k' because we use the same key in the agenda."
  2024. :group 'org-agenda
  2025. :type 'sexp)
  2026. (eval-after-load "calendar"
  2027. '(progn
  2028. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2029. 'org-calendar-goto-agenda)
  2030. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2031. 'org-agenda-action)))
  2032. (defgroup org-latex nil
  2033. "Options for embedding LaTeX code into Org-mode."
  2034. :tag "Org LaTeX"
  2035. :group 'org)
  2036. (defcustom org-format-latex-options
  2037. '(:foreground default :background default :scale 1.0
  2038. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2039. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2040. "Options for creating images from LaTeX fragments.
  2041. This is a property list with the following properties:
  2042. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2043. `default' means use the foreground of the default face.
  2044. :background the background color, or \"Transparent\".
  2045. `default' means use the background of the default face.
  2046. :scale a scaling factor for the size of the images.
  2047. :html-foreground, :html-background, :html-scale
  2048. the same numbers for HTML export.
  2049. :matchers a list indicating which matchers should be used to
  2050. find LaTeX fragments. Valid members of this list are:
  2051. \"begin\" find environments
  2052. \"$1\" find single characters surrounded by $.$
  2053. \"$\" find math expressions surrounded by $...$
  2054. \"$$\" find math expressions surrounded by $$....$$
  2055. \"\\(\" find math expressions surrounded by \\(...\\)
  2056. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2057. :group 'org-latex
  2058. :type 'plist)
  2059. (defcustom org-format-latex-header "\\documentclass{article}
  2060. \\usepackage{fullpage} % do not remove
  2061. \\usepackage{amssymb}
  2062. \\usepackage[usenames]{color}
  2063. \\usepackage{amsmath}
  2064. \\usepackage{latexsym}
  2065. \\usepackage[mathscr]{eucal}
  2066. \\pagestyle{empty} % do not remove"
  2067. "The document header used for processing LaTeX fragments."
  2068. :group 'org-latex
  2069. :type 'string)
  2070. (defgroup org-font-lock nil
  2071. "Font-lock settings for highlighting in Org-mode."
  2072. :tag "Org Font Lock"
  2073. :group 'org)
  2074. (defcustom org-level-color-stars-only nil
  2075. "Non-nil means fontify only the stars in each headline.
  2076. When nil, the entire headline is fontified.
  2077. Changing it requires restart of `font-lock-mode' to become effective
  2078. also in regions already fontified."
  2079. :group 'org-font-lock
  2080. :type 'boolean)
  2081. (defcustom org-hide-leading-stars nil
  2082. "Non-nil means, hide the first N-1 stars in a headline.
  2083. This works by using the face `org-hide' for these stars. This
  2084. face is white for a light background, and black for a dark
  2085. background. You may have to customize the face `org-hide' to
  2086. make this work.
  2087. Changing it requires restart of `font-lock-mode' to become effective
  2088. also in regions already fontified.
  2089. You may also set this on a per-file basis by adding one of the following
  2090. lines to the buffer:
  2091. #+STARTUP: hidestars
  2092. #+STARTUP: showstars"
  2093. :group 'org-font-lock
  2094. :type 'boolean)
  2095. (defcustom org-fontify-done-headline nil
  2096. "Non-nil means, change the face of a headline if it is marked DONE.
  2097. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2098. When this is non-nil, the headline after the keyword is set to the
  2099. `org-headline-done' as an additional indication."
  2100. :group 'org-font-lock
  2101. :type 'boolean)
  2102. (defcustom org-fontify-emphasized-text t
  2103. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2104. Changing this variable requires a restart of Emacs to take effect."
  2105. :group 'org-font-lock
  2106. :type 'boolean)
  2107. (defcustom org-highlight-latex-fragments-and-specials nil
  2108. "Non-nil means, fontify what is treated specially by the exporters."
  2109. :group 'org-font-lock
  2110. :type 'boolean)
  2111. (defcustom org-hide-emphasis-markers nil
  2112. "Non-nil mean font-lock should hide the emphasis marker characters."
  2113. :group 'org-font-lock
  2114. :type 'boolean)
  2115. (defvar org-emph-re nil
  2116. "Regular expression for matching emphasis.")
  2117. (defvar org-verbatim-re nil
  2118. "Regular expression for matching verbatim text.")
  2119. (defvar org-emphasis-regexp-components) ; defined just below
  2120. (defvar org-emphasis-alist) ; defined just below
  2121. (defun org-set-emph-re (var val)
  2122. "Set variable and compute the emphasis regular expression."
  2123. (set var val)
  2124. (when (and (boundp 'org-emphasis-alist)
  2125. (boundp 'org-emphasis-regexp-components)
  2126. org-emphasis-alist org-emphasis-regexp-components)
  2127. (let* ((e org-emphasis-regexp-components)
  2128. (pre (car e))
  2129. (post (nth 1 e))
  2130. (border (nth 2 e))
  2131. (body (nth 3 e))
  2132. (nl (nth 4 e))
  2133. (body1 (concat body "*?"))
  2134. (markers (mapconcat 'car org-emphasis-alist ""))
  2135. (vmarkers (mapconcat
  2136. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2137. org-emphasis-alist "")))
  2138. ;; make sure special characters appear at the right position in the class
  2139. (if (string-match "\\^" markers)
  2140. (setq markers (concat (replace-match "" t t markers) "^")))
  2141. (if (string-match "-" markers)
  2142. (setq markers (concat (replace-match "" t t markers) "-")))
  2143. (if (string-match "\\^" vmarkers)
  2144. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2145. (if (string-match "-" vmarkers)
  2146. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2147. (if (> nl 0)
  2148. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2149. (int-to-string nl) "\\}")))
  2150. ;; Make the regexp
  2151. (setq org-emph-re
  2152. (concat "\\([" pre "]\\|^\\)"
  2153. "\\("
  2154. "\\([" markers "]\\)"
  2155. "\\("
  2156. "[^" border "]\\|"
  2157. "[^" border "]"
  2158. body1
  2159. "[^" border "]"
  2160. "\\)"
  2161. "\\3\\)"
  2162. "\\([" post "]\\|$\\)"))
  2163. (setq org-verbatim-re
  2164. (concat "\\([" pre "]\\|^\\)"
  2165. "\\("
  2166. "\\([" vmarkers "]\\)"
  2167. "\\("
  2168. "[^" border "]\\|"
  2169. "[^" border "]"
  2170. body1
  2171. "[^" border "]"
  2172. "\\)"
  2173. "\\3\\)"
  2174. "\\([" post "]\\|$\\)")))))
  2175. (defcustom org-emphasis-regexp-components
  2176. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2177. "Components used to build the regular expression for emphasis.
  2178. This is a list with 6 entries. Terminology: In an emphasis string
  2179. like \" *strong word* \", we call the initial space PREMATCH, the final
  2180. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2181. and \"trong wor\" is the body. The different components in this variable
  2182. specify what is allowed/forbidden in each part:
  2183. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2184. post Chars allowed as postmatch. End of line will be allowed too.
  2185. border The chars *forbidden* as border characters.
  2186. body-regexp A regexp like \".\" to match a body character. Don't use
  2187. non-shy groups here, and don't allow newline here.
  2188. newline The maximum number of newlines allowed in an emphasis exp.
  2189. Use customize to modify this, or restart Emacs after changing it."
  2190. :group 'org-font-lock
  2191. :set 'org-set-emph-re
  2192. :type '(list
  2193. (sexp :tag "Allowed chars in pre ")
  2194. (sexp :tag "Allowed chars in post ")
  2195. (sexp :tag "Forbidden chars in border ")
  2196. (sexp :tag "Regexp for body ")
  2197. (integer :tag "number of newlines allowed")
  2198. (option (boolean :tag "Please ignore this button"))))
  2199. (defcustom org-emphasis-alist
  2200. `(("*" bold "<b>" "</b>")
  2201. ("/" italic "<i>" "</i>")
  2202. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2203. ("=" org-code "<code>" "</code>" verbatim)
  2204. ("~" org-verbatim "<code>" "</code>" verbatim)
  2205. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2206. "<del>" "</del>")
  2207. )
  2208. "Special syntax for emphasized text.
  2209. Text starting and ending with a special character will be emphasized, for
  2210. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2211. characters, the face to be used by font-lock for highlighting in Org-mode
  2212. Emacs buffers, and the HTML tags to be used for this.
  2213. Use customize to modify this, or restart Emacs after changing it."
  2214. :group 'org-font-lock
  2215. :set 'org-set-emph-re
  2216. :type '(repeat
  2217. (list
  2218. (string :tag "Marker character")
  2219. (choice
  2220. (face :tag "Font-lock-face")
  2221. (plist :tag "Face property list"))
  2222. (string :tag "HTML start tag")
  2223. (string :tag "HTML end tag")
  2224. (option (const verbatim)))))
  2225. ;;; Miscellaneous options
  2226. (defgroup org-completion nil
  2227. "Completion in Org-mode."
  2228. :tag "Org Completion"
  2229. :group 'org)
  2230. (defcustom org-completion-use-ido nil
  2231. "Non-nil means, use ido completion wherever possible.
  2232. Note that `ido-mode' must be active for this variable to be relevant.
  2233. If you decide to turn this variable on, you might well want to turn off
  2234. `org-outline-path-complete-in-steps'."
  2235. :group 'org-completion
  2236. :type 'boolean)
  2237. (defcustom org-completion-fallback-command 'hippie-expand
  2238. "The expansion command called by \\[org-complete] in normal context.
  2239. Normal means, no org-mode-specific context."
  2240. :group 'org-completion
  2241. :type 'function)
  2242. ;;; Functions and variables from ther packages
  2243. ;; Declared here to avoid compiler warnings
  2244. ;; XEmacs only
  2245. (defvar outline-mode-menu-heading)
  2246. (defvar outline-mode-menu-show)
  2247. (defvar outline-mode-menu-hide)
  2248. (defvar zmacs-regions) ; XEmacs regions
  2249. ;; Emacs only
  2250. (defvar mark-active)
  2251. ;; Various packages
  2252. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2253. (declare-function calendar-forward-day "cal-move" (arg))
  2254. (declare-function calendar-goto-date "cal-move" (date))
  2255. (declare-function calendar-goto-today "cal-move" ())
  2256. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2257. (defvar calc-embedded-close-formula)
  2258. (defvar calc-embedded-open-formula)
  2259. (declare-function cdlatex-tab "ext:cdlatex" ())
  2260. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2261. (defvar font-lock-unfontify-region-function)
  2262. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2263. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2264. (defvar iswitchb-temp-buflist)
  2265. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2266. (defvar org-agenda-tags-todo-honor-ignore-options)
  2267. (declare-function org-agenda-skip "org-agenda" ())
  2268. (declare-function org-format-agenda-item "org-agenda"
  2269. (extra txt &optional category tags dotime noprefix remove-re))
  2270. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2271. (declare-function org-agenda-change-all-lines "org-agenda"
  2272. (newhead hdmarker &optional fixface just-this))
  2273. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2274. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2275. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2276. (beg end))
  2277. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2278. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2279. "org-agenda" (&optional end))
  2280. (declare-function parse-time-string "parse-time" (string))
  2281. (declare-function remember "remember" (&optional initial))
  2282. (declare-function remember-buffer-desc "remember" ())
  2283. (declare-function remember-finalize "remember" ())
  2284. (defvar remember-save-after-remembering)
  2285. (defvar remember-data-file)
  2286. (defvar remember-register)
  2287. (defvar remember-buffer)
  2288. (defvar remember-handler-functions)
  2289. (defvar remember-annotation-functions)
  2290. (defvar texmathp-why)
  2291. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2292. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2293. (defvar w3m-current-url)
  2294. (defvar w3m-current-title)
  2295. (defvar org-latex-regexps)
  2296. ;;; Autoload and prepare some org modules
  2297. ;; Some table stuff that needs to be defined here, because it is used
  2298. ;; by the functions setting up org-mode or checking for table context.
  2299. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2300. "Detects an org-type or table-type table.")
  2301. (defconst org-table-line-regexp "^[ \t]*|"
  2302. "Detects an org-type table line.")
  2303. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2304. "Detects an org-type table line.")
  2305. (defconst org-table-hline-regexp "^[ \t]*|-"
  2306. "Detects an org-type table hline.")
  2307. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2308. "Detects a table-type table hline.")
  2309. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2310. "Searching from within a table (any type) this finds the first line
  2311. outside the table.")
  2312. ;; Autoload the functions in org-table.el that are needed by functions here.
  2313. (eval-and-compile
  2314. (org-autoload "org-table"
  2315. '(org-table-align org-table-begin org-table-blank-field
  2316. org-table-convert org-table-convert-region org-table-copy-down
  2317. org-table-copy-region org-table-create
  2318. org-table-create-or-convert-from-region
  2319. org-table-create-with-table.el org-table-current-dline
  2320. org-table-cut-region org-table-delete-column org-table-edit-field
  2321. org-table-edit-formulas org-table-end org-table-eval-formula
  2322. org-table-export org-table-field-info
  2323. org-table-get-stored-formulas org-table-goto-column
  2324. org-table-hline-and-move org-table-import org-table-insert-column
  2325. org-table-insert-hline org-table-insert-row org-table-iterate
  2326. org-table-justify-field-maybe org-table-kill-row
  2327. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2328. org-table-move-column org-table-move-column-left
  2329. org-table-move-column-right org-table-move-row
  2330. org-table-move-row-down org-table-move-row-up
  2331. org-table-next-field org-table-next-row org-table-paste-rectangle
  2332. org-table-previous-field org-table-recalculate
  2333. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2334. org-table-toggle-coordinate-overlays
  2335. org-table-toggle-formula-debugger org-table-wrap-region
  2336. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2337. (defun org-at-table-p (&optional table-type)
  2338. "Return t if the cursor is inside an org-type table.
  2339. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2340. (if org-enable-table-editor
  2341. (save-excursion
  2342. (beginning-of-line 1)
  2343. (looking-at (if table-type org-table-any-line-regexp
  2344. org-table-line-regexp)))
  2345. nil))
  2346. (defsubst org-table-p () (org-at-table-p))
  2347. (defun org-at-table.el-p ()
  2348. "Return t if and only if we are at a table.el table."
  2349. (and (org-at-table-p 'any)
  2350. (save-excursion
  2351. (goto-char (org-table-begin 'any))
  2352. (looking-at org-table1-hline-regexp))))
  2353. (defun org-table-recognize-table.el ()
  2354. "If there is a table.el table nearby, recognize it and move into it."
  2355. (if org-table-tab-recognizes-table.el
  2356. (if (org-at-table.el-p)
  2357. (progn
  2358. (beginning-of-line 1)
  2359. (if (looking-at org-table-dataline-regexp)
  2360. nil
  2361. (if (looking-at org-table1-hline-regexp)
  2362. (progn
  2363. (beginning-of-line 2)
  2364. (if (looking-at org-table-any-border-regexp)
  2365. (beginning-of-line -1)))))
  2366. (if (re-search-forward "|" (org-table-end t) t)
  2367. (progn
  2368. (require 'table)
  2369. (if (table--at-cell-p (point))
  2370. t
  2371. (message "recognizing table.el table...")
  2372. (table-recognize-table)
  2373. (message "recognizing table.el table...done")))
  2374. (error "This should not happen..."))
  2375. t)
  2376. nil)
  2377. nil))
  2378. (defun org-at-table-hline-p ()
  2379. "Return t if the cursor is inside a hline in a table."
  2380. (if org-enable-table-editor
  2381. (save-excursion
  2382. (beginning-of-line 1)
  2383. (looking-at org-table-hline-regexp))
  2384. nil))
  2385. (defvar org-table-clean-did-remove-column nil)
  2386. (defun org-table-map-tables (function)
  2387. "Apply FUNCTION to the start of all tables in the buffer."
  2388. (save-excursion
  2389. (save-restriction
  2390. (widen)
  2391. (goto-char (point-min))
  2392. (while (re-search-forward org-table-any-line-regexp nil t)
  2393. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2394. (beginning-of-line 1)
  2395. (if (looking-at org-table-line-regexp)
  2396. (save-excursion (funcall function)))
  2397. (re-search-forward org-table-any-border-regexp nil 1))))
  2398. (message "Mapping tables: done"))
  2399. ;; Declare and autoload functions from org-exp.el
  2400. (declare-function org-default-export-plist "org-exp")
  2401. (declare-function org-infile-export-plist "org-exp")
  2402. (declare-function org-get-current-options "org-exp")
  2403. (eval-and-compile
  2404. (org-autoload "org-exp"
  2405. '(org-export org-export-as-ascii org-export-visible
  2406. org-insert-export-options-template org-export-as-html-and-open
  2407. org-export-as-html-batch org-export-as-html-to-buffer
  2408. org-replace-region-by-html org-export-region-as-html
  2409. org-export-as-html org-export-icalendar-this-file
  2410. org-export-icalendar-all-agenda-files
  2411. org-table-clean-before-export
  2412. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2413. ;; Declare and autoload functions from org-agenda.el
  2414. (eval-and-compile
  2415. (org-autoload "org-agenda"
  2416. '(org-agenda org-agenda-list org-search-view
  2417. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2418. org-diary org-agenda-to-appt
  2419. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2420. ;; Autoload org-remember
  2421. (eval-and-compile
  2422. (org-autoload "org-remember"
  2423. '(org-remember-insinuate org-remember-annotation
  2424. org-remember-apply-template org-remember org-remember-handler)))
  2425. ;; Autoload org-clock.el
  2426. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2427. (beg end))
  2428. (declare-function org-clock-update-mode-line "org-clock" ())
  2429. (defvar org-clock-start-time)
  2430. (defvar org-clock-marker (make-marker)
  2431. "Marker recording the last clock-in.")
  2432. (eval-and-compile
  2433. (org-autoload
  2434. "org-clock"
  2435. '(org-clock-in org-clock-out org-clock-cancel
  2436. org-clock-goto org-clock-sum org-clock-display
  2437. org-clock-remove-overlays org-clock-report
  2438. org-clocktable-shift org-dblock-write:clocktable
  2439. org-get-clocktable)))
  2440. (defun org-clock-update-time-maybe ()
  2441. "If this is a CLOCK line, update it and return t.
  2442. Otherwise, return nil."
  2443. (interactive)
  2444. (save-excursion
  2445. (beginning-of-line 1)
  2446. (skip-chars-forward " \t")
  2447. (when (looking-at org-clock-string)
  2448. (let ((re (concat "[ \t]*" org-clock-string
  2449. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2450. "\\([ \t]*=>.*\\)?\\)?"))
  2451. ts te h m s neg)
  2452. (cond
  2453. ((not (looking-at re))
  2454. nil)
  2455. ((not (match-end 2))
  2456. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2457. (> org-clock-marker (point))
  2458. (<= org-clock-marker (point-at-eol)))
  2459. ;; The clock is running here
  2460. (setq org-clock-start-time
  2461. (apply 'encode-time
  2462. (org-parse-time-string (match-string 1))))
  2463. (org-clock-update-mode-line)))
  2464. (t
  2465. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2466. (end-of-line 1)
  2467. (setq ts (match-string 1)
  2468. te (match-string 3))
  2469. (setq s (- (time-to-seconds
  2470. (apply 'encode-time (org-parse-time-string te)))
  2471. (time-to-seconds
  2472. (apply 'encode-time (org-parse-time-string ts))))
  2473. neg (< s 0)
  2474. s (abs s)
  2475. h (floor (/ s 3600))
  2476. s (- s (* 3600 h))
  2477. m (floor (/ s 60))
  2478. s (- s (* 60 s)))
  2479. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2480. t))))))
  2481. (defun org-check-running-clock ()
  2482. "Check if the current buffer contains the running clock.
  2483. If yes, offer to stop it and to save the buffer with the changes."
  2484. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2485. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2486. (buffer-name))))
  2487. (org-clock-out)
  2488. (when (y-or-n-p "Save changed buffer?")
  2489. (save-buffer))))
  2490. (defun org-clocktable-try-shift (dir n)
  2491. "Check if this line starts a clock table, if yes, shift the time block."
  2492. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2493. (org-clocktable-shift dir n)))
  2494. ;; Autoload org-timer.el
  2495. ;(declare-function org-timer "org-timer")
  2496. (eval-and-compile
  2497. (org-autoload
  2498. "org-timer"
  2499. '(org-timer-start org-timer org-timer-item
  2500. org-timer-change-times-in-region)))
  2501. ;; Autoload archiving code
  2502. ;; The stuff that is needed for cycling and tags has to be defined here.
  2503. (defgroup org-archive nil
  2504. "Options concerning archiving in Org-mode."
  2505. :tag "Org Archive"
  2506. :group 'org-structure)
  2507. (defcustom org-archive-location "%s_archive::"
  2508. "The location where subtrees should be archived.
  2509. The value of this variable is a string, consisting of two parts,
  2510. separated by a double-colon. The first part is a filename and
  2511. the second part is a headline.
  2512. When the filename is omitted, archiving happens in the same file.
  2513. %s in the filename will be replaced by the current file
  2514. name (without the directory part). Archiving to a different file
  2515. is useful to keep archived entries from contributing to the
  2516. Org-mode Agenda.
  2517. The archived entries will be filed as subtrees of the specified
  2518. headline. When the headline is omitted, the subtrees are simply
  2519. filed away at the end of the file, as top-level entries. Also in
  2520. the heading you can use %s to represent the file name, this can be
  2521. useful when using the same archive for a number of different files.
  2522. Here are a few examples:
  2523. \"%s_archive::\"
  2524. If the current file is Projects.org, archive in file
  2525. Projects.org_archive, as top-level trees. This is the default.
  2526. \"::* Archived Tasks\"
  2527. Archive in the current file, under the top-level headline
  2528. \"* Archived Tasks\".
  2529. \"~/org/archive.org::\"
  2530. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2531. \"~/org/archive.org::From %s\"
  2532. Archive in file ~/org/archive.org (absolute path), und headlines
  2533. \"From FILENAME\" where file name is the current file name.
  2534. \"basement::** Finished Tasks\"
  2535. Archive in file ./basement (relative path), as level 3 trees
  2536. below the level 2 heading \"** Finished Tasks\".
  2537. You may set this option on a per-file basis by adding to the buffer a
  2538. line like
  2539. #+ARCHIVE: basement::** Finished Tasks
  2540. You may also define it locally for a subtree by setting an ARCHIVE property
  2541. in the entry. If such a property is found in an entry, or anywhere up
  2542. the hierarchy, it will be used."
  2543. :group 'org-archive
  2544. :type 'string)
  2545. (defcustom org-archive-tag "ARCHIVE"
  2546. "The tag that marks a subtree as archived.
  2547. An archived subtree does not open during visibility cycling, and does
  2548. not contribute to the agenda listings.
  2549. After changing this, font-lock must be restarted in the relevant buffers to
  2550. get the proper fontification."
  2551. :group 'org-archive
  2552. :group 'org-keywords
  2553. :type 'string)
  2554. (defcustom org-agenda-skip-archived-trees t
  2555. "Non-nil means, the agenda will skip any items located in archived trees.
  2556. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2557. variable is no longer recommended, you should leave it at the value t.
  2558. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2559. :group 'org-archive
  2560. :group 'org-agenda-skip
  2561. :type 'boolean)
  2562. (defcustom org-cycle-open-archived-trees nil
  2563. "Non-nil means, `org-cycle' will open archived trees.
  2564. An archived tree is a tree marked with the tag ARCHIVE.
  2565. When nil, archived trees will stay folded. You can still open them with
  2566. normal outline commands like `show-all', but not with the cycling commands."
  2567. :group 'org-archive
  2568. :group 'org-cycle
  2569. :type 'boolean)
  2570. (defcustom org-sparse-tree-open-archived-trees nil
  2571. "Non-nil means sparse tree construction shows matches in archived trees.
  2572. When nil, matches in these trees are highlighted, but the trees are kept in
  2573. collapsed state."
  2574. :group 'org-archive
  2575. :group 'org-sparse-trees
  2576. :type 'boolean)
  2577. (defun org-cycle-hide-archived-subtrees (state)
  2578. "Re-hide all archived subtrees after a visibility state change."
  2579. (when (and (not org-cycle-open-archived-trees)
  2580. (not (memq state '(overview folded))))
  2581. (save-excursion
  2582. (let* ((globalp (memq state '(contents all)))
  2583. (beg (if globalp (point-min) (point)))
  2584. (end (if globalp (point-max) (org-end-of-subtree t))))
  2585. (org-hide-archived-subtrees beg end)
  2586. (goto-char beg)
  2587. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2588. (message "%s" (substitute-command-keys
  2589. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2590. (defun org-force-cycle-archived ()
  2591. "Cycle subtree even if it is archived."
  2592. (interactive)
  2593. (setq this-command 'org-cycle)
  2594. (let ((org-cycle-open-archived-trees t))
  2595. (call-interactively 'org-cycle)))
  2596. (defun org-hide-archived-subtrees (beg end)
  2597. "Re-hide all archived subtrees after a visibility state change."
  2598. (save-excursion
  2599. (let* ((re (concat ":" org-archive-tag ":")))
  2600. (goto-char beg)
  2601. (while (re-search-forward re end t)
  2602. (and (org-on-heading-p) (hide-subtree))
  2603. (org-end-of-subtree t)))))
  2604. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2605. (eval-and-compile
  2606. (org-autoload "org-archive"
  2607. '(org-add-archive-files org-archive-subtree
  2608. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2609. ;; Autoload Column View Code
  2610. (declare-function org-columns-number-to-string "org-colview")
  2611. (declare-function org-columns-get-format-and-top-level "org-colview")
  2612. (declare-function org-columns-compute "org-colview")
  2613. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2614. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2615. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2616. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2617. ;; Autoload ID code
  2618. (declare-function org-id-store-link "org-id")
  2619. (org-autoload "org-id"
  2620. '(org-id-get-create org-id-new org-id-copy org-id-get
  2621. org-id-get-with-outline-path-completion
  2622. org-id-get-with-outline-drilling
  2623. org-id-goto org-id-find org-id-store-link))
  2624. ;;; Variables for pre-computed regular expressions, all buffer local
  2625. (defvar org-drawer-regexp nil
  2626. "Matches first line of a hidden block.")
  2627. (make-variable-buffer-local 'org-drawer-regexp)
  2628. (defvar org-todo-regexp nil
  2629. "Matches any of the TODO state keywords.")
  2630. (make-variable-buffer-local 'org-todo-regexp)
  2631. (defvar org-not-done-regexp nil
  2632. "Matches any of the TODO state keywords except the last one.")
  2633. (make-variable-buffer-local 'org-not-done-regexp)
  2634. (defvar org-todo-line-regexp nil
  2635. "Matches a headline and puts TODO state into group 2 if present.")
  2636. (make-variable-buffer-local 'org-todo-line-regexp)
  2637. (defvar org-complex-heading-regexp nil
  2638. "Matches a headline and puts everything into groups:
  2639. group 1: the stars
  2640. group 2: The todo keyword, maybe
  2641. group 3: Priority cookie
  2642. group 4: True headline
  2643. group 5: Tags")
  2644. (make-variable-buffer-local 'org-complex-heading-regexp)
  2645. (defvar org-todo-line-tags-regexp nil
  2646. "Matches a headline and puts TODO state into group 2 if present.
  2647. Also put tags into group 4 if tags are present.")
  2648. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2649. (defvar org-nl-done-regexp nil
  2650. "Matches newline followed by a headline with the DONE keyword.")
  2651. (make-variable-buffer-local 'org-nl-done-regexp)
  2652. (defvar org-looking-at-done-regexp nil
  2653. "Matches the DONE keyword a point.")
  2654. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2655. (defvar org-ds-keyword-length 12
  2656. "Maximum length of the Deadline and SCHEDULED keywords.")
  2657. (make-variable-buffer-local 'org-ds-keyword-length)
  2658. (defvar org-deadline-regexp nil
  2659. "Matches the DEADLINE keyword.")
  2660. (make-variable-buffer-local 'org-deadline-regexp)
  2661. (defvar org-deadline-time-regexp nil
  2662. "Matches the DEADLINE keyword together with a time stamp.")
  2663. (make-variable-buffer-local 'org-deadline-time-regexp)
  2664. (defvar org-deadline-line-regexp nil
  2665. "Matches the DEADLINE keyword and the rest of the line.")
  2666. (make-variable-buffer-local 'org-deadline-line-regexp)
  2667. (defvar org-scheduled-regexp nil
  2668. "Matches the SCHEDULED keyword.")
  2669. (make-variable-buffer-local 'org-scheduled-regexp)
  2670. (defvar org-scheduled-time-regexp nil
  2671. "Matches the SCHEDULED keyword together with a time stamp.")
  2672. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2673. (defvar org-closed-time-regexp nil
  2674. "Matches the CLOSED keyword together with a time stamp.")
  2675. (make-variable-buffer-local 'org-closed-time-regexp)
  2676. (defvar org-keyword-time-regexp nil
  2677. "Matches any of the 4 keywords, together with the time stamp.")
  2678. (make-variable-buffer-local 'org-keyword-time-regexp)
  2679. (defvar org-keyword-time-not-clock-regexp nil
  2680. "Matches any of the 3 keywords, together with the time stamp.")
  2681. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2682. (defvar org-maybe-keyword-time-regexp nil
  2683. "Matches a timestamp, possibly preceeded by a keyword.")
  2684. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2685. (defvar org-planning-or-clock-line-re nil
  2686. "Matches a line with planning or clock info.")
  2687. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2688. (defconst org-plain-time-of-day-regexp
  2689. (concat
  2690. "\\(\\<[012]?[0-9]"
  2691. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2692. "\\(--?"
  2693. "\\(\\<[012]?[0-9]"
  2694. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2695. "\\)?")
  2696. "Regular expression to match a plain time or time range.
  2697. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2698. groups carry important information:
  2699. 0 the full match
  2700. 1 the first time, range or not
  2701. 8 the second time, if it is a range.")
  2702. (defconst org-plain-time-extension-regexp
  2703. (concat
  2704. "\\(\\<[012]?[0-9]"
  2705. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2706. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2707. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2708. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2709. groups carry important information:
  2710. 0 the full match
  2711. 7 hours of duration
  2712. 9 minutes of duration")
  2713. (defconst org-stamp-time-of-day-regexp
  2714. (concat
  2715. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2716. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2717. "\\(--?"
  2718. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2719. "Regular expression to match a timestamp time or time range.
  2720. After a match, the following groups carry important information:
  2721. 0 the full match
  2722. 1 date plus weekday, for backreferencing to make sure both times on same day
  2723. 2 the first time, range or not
  2724. 4 the second time, if it is a range.")
  2725. (defconst org-startup-options
  2726. '(("fold" org-startup-folded t)
  2727. ("overview" org-startup-folded t)
  2728. ("nofold" org-startup-folded nil)
  2729. ("showall" org-startup-folded nil)
  2730. ("content" org-startup-folded content)
  2731. ("hidestars" org-hide-leading-stars t)
  2732. ("showstars" org-hide-leading-stars nil)
  2733. ("odd" org-odd-levels-only t)
  2734. ("oddeven" org-odd-levels-only nil)
  2735. ("align" org-startup-align-all-tables t)
  2736. ("noalign" org-startup-align-all-tables nil)
  2737. ("customtime" org-display-custom-times t)
  2738. ("logdone" org-log-done time)
  2739. ("lognotedone" org-log-done note)
  2740. ("nologdone" org-log-done nil)
  2741. ("lognoteclock-out" org-log-note-clock-out t)
  2742. ("nolognoteclock-out" org-log-note-clock-out nil)
  2743. ("logrepeat" org-log-repeat state)
  2744. ("lognoterepeat" org-log-repeat note)
  2745. ("nologrepeat" org-log-repeat nil)
  2746. ("fninline" org-footnote-define-inline t)
  2747. ("nofninline" org-footnote-define-inline nil)
  2748. ("fnlocal" org-footnote-section nil)
  2749. ("fnauto" org-footnote-auto-label t)
  2750. ("fnprompt" org-footnote-auto-label nil)
  2751. ("fnconfirm" org-footnote-auto-label confirm)
  2752. ("fnplain" org-footnote-auto-label plain)
  2753. ("constcgs" constants-unit-system cgs)
  2754. ("constSI" constants-unit-system SI))
  2755. "Variable associated with STARTUP options for org-mode.
  2756. Each element is a list of three items: The startup options as written
  2757. in the #+STARTUP line, the corresponding variable, and the value to
  2758. set this variable to if the option is found. An optional forth element PUSH
  2759. means to push this value onto the list in the variable.")
  2760. (defun org-set-regexps-and-options ()
  2761. "Precompute regular expressions for current buffer."
  2762. (when (org-mode-p)
  2763. (org-set-local 'org-todo-kwd-alist nil)
  2764. (org-set-local 'org-todo-key-alist nil)
  2765. (org-set-local 'org-todo-key-trigger nil)
  2766. (org-set-local 'org-todo-keywords-1 nil)
  2767. (org-set-local 'org-done-keywords nil)
  2768. (org-set-local 'org-todo-heads nil)
  2769. (org-set-local 'org-todo-sets nil)
  2770. (org-set-local 'org-todo-log-states nil)
  2771. (org-set-local 'org-file-properties nil)
  2772. (org-set-local 'org-file-tags nil)
  2773. (let ((re (org-make-options-regexp
  2774. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2775. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2776. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2777. (splitre "[ \t]+")
  2778. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2779. tail sep kws1 prio props ftags drawers
  2780. ext-setup-or-nil setup-contents (start 0))
  2781. (save-excursion
  2782. (save-restriction
  2783. (widen)
  2784. (goto-char (point-min))
  2785. (while (or (and ext-setup-or-nil
  2786. (string-match re ext-setup-or-nil start)
  2787. (setq start (match-end 0)))
  2788. (and (setq ext-setup-or-nil nil start 0)
  2789. (re-search-forward re nil t)))
  2790. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2791. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2792. (cond
  2793. ((equal key "CATEGORY")
  2794. (if (string-match "[ \t]+$" value)
  2795. (setq value (replace-match "" t t value)))
  2796. (setq cat value))
  2797. ((member key '("SEQ_TODO" "TODO"))
  2798. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2799. ((equal key "TYP_TODO")
  2800. (push (cons 'type (org-split-string value splitre)) kwds))
  2801. ((equal key "TAGS")
  2802. (setq tags (append tags (org-split-string value splitre))))
  2803. ((equal key "COLUMNS")
  2804. (org-set-local 'org-columns-default-format value))
  2805. ((equal key "LINK")
  2806. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2807. (push (cons (match-string 1 value)
  2808. (org-trim (match-string 2 value)))
  2809. links)))
  2810. ((equal key "PRIORITIES")
  2811. (setq prio (org-split-string value " +")))
  2812. ((equal key "PROPERTY")
  2813. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2814. (push (cons (match-string 1 value) (match-string 2 value))
  2815. props)))
  2816. ((equal key "FILETAGS")
  2817. (when (string-match "\\S-" value)
  2818. (setq ftags
  2819. (append
  2820. ftags
  2821. (apply 'append
  2822. (mapcar (lambda (x) (org-split-string x ":"))
  2823. (org-split-string value)))))))
  2824. ((equal key "DRAWERS")
  2825. (setq drawers (org-split-string value splitre)))
  2826. ((equal key "CONSTANTS")
  2827. (setq const (append const (org-split-string value splitre))))
  2828. ((equal key "STARTUP")
  2829. (let ((opts (org-split-string value splitre))
  2830. l var val)
  2831. (while (setq l (pop opts))
  2832. (when (setq l (assoc l org-startup-options))
  2833. (setq var (nth 1 l) val (nth 2 l))
  2834. (if (not (nth 3 l))
  2835. (set (make-local-variable var) val)
  2836. (if (not (listp (symbol-value var)))
  2837. (set (make-local-variable var) nil))
  2838. (set (make-local-variable var) (symbol-value var))
  2839. (add-to-list var val))))))
  2840. ((equal key "ARCHIVE")
  2841. (string-match " *$" value)
  2842. (setq arch (replace-match "" t t value))
  2843. (remove-text-properties 0 (length arch)
  2844. '(face t fontified t) arch))
  2845. ((equal key "SETUPFILE")
  2846. (setq setup-contents (org-file-contents
  2847. (expand-file-name
  2848. (org-remove-double-quotes value))
  2849. 'noerror))
  2850. (if (not ext-setup-or-nil)
  2851. (setq ext-setup-or-nil setup-contents start 0)
  2852. (setq ext-setup-or-nil
  2853. (concat (substring ext-setup-or-nil 0 start)
  2854. "\n" setup-contents "\n"
  2855. (substring ext-setup-or-nil start)))))
  2856. ))))
  2857. (when cat
  2858. (org-set-local 'org-category (intern cat))
  2859. (push (cons "CATEGORY" cat) props))
  2860. (when prio
  2861. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2862. (setq prio (mapcar 'string-to-char prio))
  2863. (org-set-local 'org-highest-priority (nth 0 prio))
  2864. (org-set-local 'org-lowest-priority (nth 1 prio))
  2865. (org-set-local 'org-default-priority (nth 2 prio)))
  2866. (and props (org-set-local 'org-file-properties (nreverse props)))
  2867. (and ftags (org-set-local 'org-file-tags ftags))
  2868. (and drawers (org-set-local 'org-drawers drawers))
  2869. (and arch (org-set-local 'org-archive-location arch))
  2870. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2871. ;; Process the TODO keywords
  2872. (unless kwds
  2873. ;; Use the global values as if they had been given locally.
  2874. (setq kwds (default-value 'org-todo-keywords))
  2875. (if (stringp (car kwds))
  2876. (setq kwds (list (cons org-todo-interpretation
  2877. (default-value 'org-todo-keywords)))))
  2878. (setq kwds (reverse kwds)))
  2879. (setq kwds (nreverse kwds))
  2880. (let (inter kws kw)
  2881. (while (setq kws (pop kwds))
  2882. (setq inter (pop kws) sep (member "|" kws)
  2883. kws0 (delete "|" (copy-sequence kws))
  2884. kwsa nil
  2885. kws1 (mapcar
  2886. (lambda (x)
  2887. ;; 1 2
  2888. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2889. (progn
  2890. (setq kw (match-string 1 x)
  2891. key (and (match-end 2) (match-string 2 x))
  2892. log (org-extract-log-state-settings x))
  2893. (push (cons kw (and key (string-to-char key))) kwsa)
  2894. (and log (push log org-todo-log-states))
  2895. kw)
  2896. (error "Invalid TODO keyword %s" x)))
  2897. kws0)
  2898. kwsa (if kwsa (append '((:startgroup))
  2899. (nreverse kwsa)
  2900. '((:endgroup))))
  2901. hw (car kws1)
  2902. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2903. tail (list inter hw (car dws) (org-last dws)))
  2904. (add-to-list 'org-todo-heads hw 'append)
  2905. (push kws1 org-todo-sets)
  2906. (setq org-done-keywords (append org-done-keywords dws nil))
  2907. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2908. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2909. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2910. (setq org-todo-sets (nreverse org-todo-sets)
  2911. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2912. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2913. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2914. ;; Process the constants
  2915. (when const
  2916. (let (e cst)
  2917. (while (setq e (pop const))
  2918. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2919. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2920. (setq org-table-formula-constants-local cst)))
  2921. ;; Process the tags.
  2922. (when tags
  2923. (let (e tgs)
  2924. (while (setq e (pop tags))
  2925. (cond
  2926. ((equal e "{") (push '(:startgroup) tgs))
  2927. ((equal e "}") (push '(:endgroup) tgs))
  2928. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2929. (push (cons (match-string 1 e)
  2930. (string-to-char (match-string 2 e)))
  2931. tgs))
  2932. (t (push (list e) tgs))))
  2933. (org-set-local 'org-tag-alist nil)
  2934. (while (setq e (pop tgs))
  2935. (or (and (stringp (car e))
  2936. (assoc (car e) org-tag-alist))
  2937. (push e org-tag-alist)))))
  2938. ;; Compute the regular expressions and other local variables
  2939. (if (not org-done-keywords)
  2940. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2941. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2942. (length org-scheduled-string)
  2943. (length org-clock-string)
  2944. (length org-closed-string)))
  2945. org-drawer-regexp
  2946. (concat "^[ \t]*:\\("
  2947. (mapconcat 'regexp-quote org-drawers "\\|")
  2948. "\\):[ \t]*$")
  2949. org-not-done-keywords
  2950. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2951. org-todo-regexp
  2952. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2953. "\\|") "\\)\\>")
  2954. org-not-done-regexp
  2955. (concat "\\<\\("
  2956. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2957. "\\)\\>")
  2958. org-todo-line-regexp
  2959. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2960. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2961. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2962. org-complex-heading-regexp
  2963. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2964. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2965. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2966. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2967. org-nl-done-regexp
  2968. (concat "\n\\*+[ \t]+"
  2969. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2970. "\\)" "\\>")
  2971. org-todo-line-tags-regexp
  2972. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2973. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2974. (org-re
  2975. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2976. org-looking-at-done-regexp
  2977. (concat "^" "\\(?:"
  2978. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2979. "\\>")
  2980. org-deadline-regexp (concat "\\<" org-deadline-string)
  2981. org-deadline-time-regexp
  2982. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2983. org-deadline-line-regexp
  2984. (concat "\\<\\(" org-deadline-string "\\).*")
  2985. org-scheduled-regexp
  2986. (concat "\\<" org-scheduled-string)
  2987. org-scheduled-time-regexp
  2988. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2989. org-closed-time-regexp
  2990. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2991. org-keyword-time-regexp
  2992. (concat "\\<\\(" org-scheduled-string
  2993. "\\|" org-deadline-string
  2994. "\\|" org-closed-string
  2995. "\\|" org-clock-string "\\)"
  2996. " *[[<]\\([^]>]+\\)[]>]")
  2997. org-keyword-time-not-clock-regexp
  2998. (concat "\\<\\(" org-scheduled-string
  2999. "\\|" org-deadline-string
  3000. "\\|" org-closed-string
  3001. "\\)"
  3002. " *[[<]\\([^]>]+\\)[]>]")
  3003. org-maybe-keyword-time-regexp
  3004. (concat "\\(\\<\\(" org-scheduled-string
  3005. "\\|" org-deadline-string
  3006. "\\|" org-closed-string
  3007. "\\|" org-clock-string "\\)\\)?"
  3008. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3009. org-planning-or-clock-line-re
  3010. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3011. "\\|" org-deadline-string
  3012. "\\|" org-closed-string "\\|" org-clock-string
  3013. "\\)\\>\\)")
  3014. )
  3015. (org-compute-latex-and-specials-regexp)
  3016. (org-set-font-lock-defaults))))
  3017. (defun org-file-contents (file &optional noerror)
  3018. "Return the contents of FILE, as a string."
  3019. (if (or (not file)
  3020. (not (file-readable-p file)))
  3021. (if noerror
  3022. (progn
  3023. (message "Cannot read file %s" file)
  3024. (ding) (sit-for 2)
  3025. "")
  3026. (error "Cannot read file %s" file))
  3027. (with-temp-buffer
  3028. (insert-file-contents file)
  3029. (buffer-string))))
  3030. (defun org-extract-log-state-settings (x)
  3031. "Extract the log state setting from a TODO keyword string.
  3032. This will extract info from a string like \"WAIT(w@/!)\"."
  3033. (let (kw key log1 log2)
  3034. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3035. (setq kw (match-string 1 x)
  3036. key (and (match-end 2) (match-string 2 x))
  3037. log1 (and (match-end 3) (match-string 3 x))
  3038. log2 (and (match-end 4) (match-string 4 x)))
  3039. (and (or log1 log2)
  3040. (list kw
  3041. (and log1 (if (equal log1 "!") 'time 'note))
  3042. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3043. (defun org-remove-keyword-keys (list)
  3044. "Remove a pair of parenthesis at the end of each string in LIST."
  3045. (mapcar (lambda (x)
  3046. (if (string-match "(.*)$" x)
  3047. (substring x 0 (match-beginning 0))
  3048. x))
  3049. list))
  3050. ;; FIXME: this could be done much better, using second characters etc.
  3051. (defun org-assign-fast-keys (alist)
  3052. "Assign fast keys to a keyword-key alist.
  3053. Respect keys that are already there."
  3054. (let (new e k c c1 c2 (char ?a))
  3055. (while (setq e (pop alist))
  3056. (cond
  3057. ((equal e '(:startgroup)) (push e new))
  3058. ((equal e '(:endgroup)) (push e new))
  3059. (t
  3060. (setq k (car e) c2 nil)
  3061. (if (cdr e)
  3062. (setq c (cdr e))
  3063. ;; automatically assign a character.
  3064. (setq c1 (string-to-char
  3065. (downcase (substring
  3066. k (if (= (string-to-char k) ?@) 1 0)))))
  3067. (if (or (rassoc c1 new) (rassoc c1 alist))
  3068. (while (or (rassoc char new) (rassoc char alist))
  3069. (setq char (1+ char)))
  3070. (setq c2 c1))
  3071. (setq c (or c2 char)))
  3072. (push (cons k c) new))))
  3073. (nreverse new)))
  3074. ;;; Some variables used in various places
  3075. (defvar org-window-configuration nil
  3076. "Used in various places to store a window configuration.")
  3077. (defvar org-finish-function nil
  3078. "Function to be called when `C-c C-c' is used.
  3079. This is for getting out of special buffers like remember.")
  3080. ;; FIXME: Occasionally check by commenting these, to make sure
  3081. ;; no other functions uses these, forgetting to let-bind them.
  3082. (defvar entry)
  3083. (defvar state)
  3084. (defvar last-state)
  3085. (defvar date)
  3086. (defvar description)
  3087. ;; Defined somewhere in this file, but used before definition.
  3088. (defvar org-html-entities)
  3089. (defvar org-struct-menu)
  3090. (defvar org-org-menu)
  3091. (defvar org-tbl-menu)
  3092. (defvar org-agenda-keymap)
  3093. ;;;; Define the Org-mode
  3094. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3095. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
  3096. ;; We use a before-change function to check if a table might need
  3097. ;; an update.
  3098. (defvar org-table-may-need-update t
  3099. "Indicates that a table might need an update.
  3100. This variable is set by `org-before-change-function'.
  3101. `org-table-align' sets it back to nil.")
  3102. (defun org-before-change-function (beg end)
  3103. "Every change indicates that a table might need an update."
  3104. (setq org-table-may-need-update t))
  3105. (defvar org-mode-map)
  3106. (defvar org-mode-hook nil
  3107. "Mode hook for Org-mode, run after the mode was turned on.")
  3108. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3109. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3110. (defvar org-table-buffer-is-an nil)
  3111. (defconst org-outline-regexp "\\*+ ")
  3112. ;;;###autoload
  3113. (define-derived-mode org-mode outline-mode "Org"
  3114. "Outline-based notes management and organizer, alias
  3115. \"Carsten's outline-mode for keeping track of everything.\"
  3116. Org-mode develops organizational tasks around a NOTES file which
  3117. contains information about projects as plain text. Org-mode is
  3118. implemented on top of outline-mode, which is ideal to keep the content
  3119. of large files well structured. It supports ToDo items, deadlines and
  3120. time stamps, which magically appear in the diary listing of the Emacs
  3121. calendar. Tables are easily created with a built-in table editor.
  3122. Plain text URL-like links connect to websites, emails (VM), Usenet
  3123. messages (Gnus), BBDB entries, and any files related to the project.
  3124. For printing and sharing of notes, an Org-mode file (or a part of it)
  3125. can be exported as a structured ASCII or HTML file.
  3126. The following commands are available:
  3127. \\{org-mode-map}"
  3128. ;; Get rid of Outline menus, they are not needed
  3129. ;; Need to do this here because define-derived-mode sets up
  3130. ;; the keymap so late. Still, it is a waste to call this each time
  3131. ;; we switch another buffer into org-mode.
  3132. (if (featurep 'xemacs)
  3133. (when (boundp 'outline-mode-menu-heading)
  3134. ;; Assume this is Greg's port, it used easymenu
  3135. (easy-menu-remove outline-mode-menu-heading)
  3136. (easy-menu-remove outline-mode-menu-show)
  3137. (easy-menu-remove outline-mode-menu-hide))
  3138. (define-key org-mode-map [menu-bar headings] 'undefined)
  3139. (define-key org-mode-map [menu-bar hide] 'undefined)
  3140. (define-key org-mode-map [menu-bar show] 'undefined))
  3141. (org-load-modules-maybe)
  3142. (easy-menu-add org-org-menu)
  3143. (easy-menu-add org-tbl-menu)
  3144. (org-install-agenda-files-menu)
  3145. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3146. (org-add-to-invisibility-spec '(org-cwidth))
  3147. (when (featurep 'xemacs)
  3148. (org-set-local 'line-move-ignore-invisible t))
  3149. (org-set-local 'outline-regexp org-outline-regexp)
  3150. (org-set-local 'outline-level 'org-outline-level)
  3151. (when (and org-ellipsis
  3152. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3153. (fboundp 'make-glyph-code))
  3154. (unless org-display-table
  3155. (setq org-display-table (make-display-table)))
  3156. (set-display-table-slot
  3157. org-display-table 4
  3158. (vconcat (mapcar
  3159. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3160. org-ellipsis)))
  3161. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3162. (setq buffer-display-table org-display-table))
  3163. (org-set-regexps-and-options)
  3164. (when (and org-tag-faces (not org-tags-special-faces-re))
  3165. ;; tag faces set outside customize.... force initialization.
  3166. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3167. ;; Calc embedded
  3168. (org-set-local 'calc-embedded-open-mode "# ")
  3169. (modify-syntax-entry ?# "<")
  3170. (modify-syntax-entry ?@ "w")
  3171. (if org-startup-truncated (setq truncate-lines t))
  3172. (org-set-local 'font-lock-unfontify-region-function
  3173. 'org-unfontify-region)
  3174. ;; Activate before-change-function
  3175. (org-set-local 'org-table-may-need-update t)
  3176. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3177. 'local)
  3178. ;; Check for running clock before killing a buffer
  3179. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3180. ;; Paragraphs and auto-filling
  3181. (org-set-autofill-regexps)
  3182. (setq indent-line-function 'org-indent-line-function)
  3183. (org-update-radio-target-regexp)
  3184. ;; Make sure dependence stuff works reliably, even for users who set it
  3185. ;; too late :-(
  3186. (if org-enforce-todo-dependencies
  3187. (add-hook 'org-blocker-hook
  3188. 'org-block-todo-from-children-or-siblings)
  3189. (remove-hook 'org-blocker-hook
  3190. 'org-block-todo-from-children-or-siblings))
  3191. (if org-enforce-todo-checkbox-dependencies
  3192. (add-hook 'org-blocker-hook
  3193. 'org-block-todo-from-checkboxes)
  3194. (remove-hook 'org-blocker-hook
  3195. 'org-block-todo-from-checkboxes))
  3196. ;; Comment characters
  3197. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3198. (org-set-local 'comment-padding " ")
  3199. ;; Align options lines
  3200. (org-set-local
  3201. 'align-mode-rules-list
  3202. '((org-in-buffer-settings
  3203. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3204. (modes . '(org-mode)))))
  3205. ;; Imenu
  3206. (org-set-local 'imenu-create-index-function
  3207. 'org-imenu-get-tree)
  3208. ;; Make isearch reveal context
  3209. (if (or (featurep 'xemacs)
  3210. (not (boundp 'outline-isearch-open-invisible-function)))
  3211. ;; Emacs 21 and XEmacs make use of the hook
  3212. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3213. ;; Emacs 22 deals with this through a special variable
  3214. (org-set-local 'outline-isearch-open-invisible-function
  3215. (lambda (&rest ignore) (org-show-context 'isearch))))
  3216. ;; If empty file that did not turn on org-mode automatically, make it to.
  3217. (if (and org-insert-mode-line-in-empty-file
  3218. (interactive-p)
  3219. (= (point-min) (point-max)))
  3220. (insert "# -*- mode: org -*-\n\n"))
  3221. (unless org-inhibit-startup
  3222. (when org-startup-align-all-tables
  3223. (let ((bmp (buffer-modified-p)))
  3224. (org-table-map-tables 'org-table-align)
  3225. (set-buffer-modified-p bmp)))
  3226. (org-set-startup-visibility)))
  3227. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3228. (defun org-current-time ()
  3229. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3230. (if (> (car org-time-stamp-rounding-minutes) 1)
  3231. (let ((r (car org-time-stamp-rounding-minutes))
  3232. (time (decode-time)))
  3233. (apply 'encode-time
  3234. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3235. (nthcdr 2 time))))
  3236. (current-time)))
  3237. ;;;; Font-Lock stuff, including the activators
  3238. (defvar org-mouse-map (make-sparse-keymap))
  3239. (org-defkey org-mouse-map
  3240. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3241. (org-defkey org-mouse-map
  3242. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3243. (when org-mouse-1-follows-link
  3244. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3245. (when org-tab-follows-link
  3246. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3247. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3248. (when org-return-follows-link
  3249. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3250. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3251. (require 'font-lock)
  3252. (defconst org-non-link-chars "]\t\n\r<>")
  3253. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3254. "shell" "elisp"))
  3255. (defvar org-link-types-re nil
  3256. "Matches a link that has a url-like prefix like \"http:\"")
  3257. (defvar org-link-re-with-space nil
  3258. "Matches a link with spaces, optional angular brackets around it.")
  3259. (defvar org-link-re-with-space2 nil
  3260. "Matches a link with spaces, optional angular brackets around it.")
  3261. (defvar org-link-re-with-space3 nil
  3262. "Matches a link with spaces, only for internal part in bracket links.")
  3263. (defvar org-angle-link-re nil
  3264. "Matches link with angular brackets, spaces are allowed.")
  3265. (defvar org-plain-link-re nil
  3266. "Matches plain link, without spaces.")
  3267. (defvar org-bracket-link-regexp nil
  3268. "Matches a link in double brackets.")
  3269. (defvar org-bracket-link-analytic-regexp nil
  3270. "Regular expression used to analyze links.
  3271. Here is what the match groups contain after a match:
  3272. 1: http:
  3273. 2: http
  3274. 3: path
  3275. 4: [desc]
  3276. 5: desc")
  3277. (defvar org-bracket-link-analytic-regexp++ nil
  3278. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3279. (defvar org-any-link-re nil
  3280. "Regular expression matching any link.")
  3281. (defun org-make-link-regexps ()
  3282. "Update the link regular expressions.
  3283. This should be called after the variable `org-link-types' has changed."
  3284. (setq org-link-types-re
  3285. (concat
  3286. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3287. org-link-re-with-space
  3288. (concat
  3289. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3290. "\\([^" org-non-link-chars " ]"
  3291. "[^" org-non-link-chars "]*"
  3292. "[^" org-non-link-chars " ]\\)>?")
  3293. org-link-re-with-space2
  3294. (concat
  3295. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3296. "\\([^" org-non-link-chars " ]"
  3297. "[^\t\n\r]*"
  3298. "[^" org-non-link-chars " ]\\)>?")
  3299. org-link-re-with-space3
  3300. (concat
  3301. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3302. "\\([^" org-non-link-chars " ]"
  3303. "[^\t\n\r]*\\)")
  3304. org-angle-link-re
  3305. (concat
  3306. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3307. "\\([^" org-non-link-chars " ]"
  3308. "[^" org-non-link-chars "]*"
  3309. "\\)>")
  3310. org-plain-link-re
  3311. (concat
  3312. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3313. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3314. org-bracket-link-regexp
  3315. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3316. org-bracket-link-analytic-regexp
  3317. (concat
  3318. "\\[\\["
  3319. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3320. "\\([^]]+\\)"
  3321. "\\]"
  3322. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3323. "\\]")
  3324. org-bracket-link-analytic-regexp++
  3325. (concat
  3326. "\\[\\["
  3327. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3328. "\\([^]]+\\)"
  3329. "\\]"
  3330. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3331. "\\]")
  3332. org-any-link-re
  3333. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3334. org-angle-link-re "\\)\\|\\("
  3335. org-plain-link-re "\\)")))
  3336. (org-make-link-regexps)
  3337. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3338. "Regular expression for fast time stamp matching.")
  3339. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3340. "Regular expression for fast time stamp matching.")
  3341. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3342. "Regular expression matching time strings for analysis.
  3343. This one does not require the space after the date, so it can be used
  3344. on a string that terminates immediately after the date.")
  3345. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3346. "Regular expression matching time strings for analysis.")
  3347. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3348. "Regular expression matching time stamps, with groups.")
  3349. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3350. "Regular expression matching time stamps (also [..]), with groups.")
  3351. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3352. "Regular expression matching a time stamp range.")
  3353. (defconst org-tr-regexp-both
  3354. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3355. "Regular expression matching a time stamp range.")
  3356. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3357. org-ts-regexp "\\)?")
  3358. "Regular expression matching a time stamp or time stamp range.")
  3359. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3360. org-ts-regexp-both "\\)?")
  3361. "Regular expression matching a time stamp or time stamp range.
  3362. The time stamps may be either active or inactive.")
  3363. (defvar org-emph-face nil)
  3364. (defun org-do-emphasis-faces (limit)
  3365. "Run through the buffer and add overlays to links."
  3366. (let (rtn)
  3367. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3368. (if (not (= (char-after (match-beginning 3))
  3369. (char-after (match-beginning 4))))
  3370. (progn
  3371. (setq rtn t)
  3372. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3373. 'face
  3374. (nth 1 (assoc (match-string 3)
  3375. org-emphasis-alist)))
  3376. (add-text-properties (match-beginning 2) (match-end 2)
  3377. '(font-lock-multiline t))
  3378. (when org-hide-emphasis-markers
  3379. (add-text-properties (match-end 4) (match-beginning 5)
  3380. '(invisible org-link))
  3381. (add-text-properties (match-beginning 3) (match-end 3)
  3382. '(invisible org-link)))))
  3383. (backward-char 1))
  3384. rtn))
  3385. (defun org-emphasize (&optional char)
  3386. "Insert or change an emphasis, i.e. a font like bold or italic.
  3387. If there is an active region, change that region to a new emphasis.
  3388. If there is no region, just insert the marker characters and position
  3389. the cursor between them.
  3390. CHAR should be either the marker character, or the first character of the
  3391. HTML tag associated with that emphasis. If CHAR is a space, the means
  3392. to remove the emphasis of the selected region.
  3393. If char is not given (for example in an interactive call) it
  3394. will be prompted for."
  3395. (interactive)
  3396. (let ((eal org-emphasis-alist) e det
  3397. (erc org-emphasis-regexp-components)
  3398. (prompt "")
  3399. (string "") beg end move tag c s)
  3400. (if (org-region-active-p)
  3401. (setq beg (region-beginning) end (region-end)
  3402. string (buffer-substring beg end))
  3403. (setq move t))
  3404. (while (setq e (pop eal))
  3405. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3406. c (aref tag 0))
  3407. (push (cons c (string-to-char (car e))) det)
  3408. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3409. (substring tag 1)))))
  3410. (setq det (nreverse det))
  3411. (unless char
  3412. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3413. (setq char (read-char-exclusive)))
  3414. (setq char (or (cdr (assoc char det)) char))
  3415. (if (equal char ?\ )
  3416. (setq s "" move nil)
  3417. (unless (assoc (char-to-string char) org-emphasis-alist)
  3418. (error "No such emphasis marker: \"%c\"" char))
  3419. (setq s (char-to-string char)))
  3420. (while (and (> (length string) 1)
  3421. (equal (substring string 0 1) (substring string -1))
  3422. (assoc (substring string 0 1) org-emphasis-alist))
  3423. (setq string (substring string 1 -1)))
  3424. (setq string (concat s string s))
  3425. (if beg (delete-region beg end))
  3426. (unless (or (bolp)
  3427. (string-match (concat "[" (nth 0 erc) "\n]")
  3428. (char-to-string (char-before (point)))))
  3429. (insert " "))
  3430. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3431. (char-to-string (char-after (point))))
  3432. (insert " ") (backward-char 1))
  3433. (insert string)
  3434. (and move (backward-char 1))))
  3435. (defconst org-nonsticky-props
  3436. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3437. (defun org-activate-plain-links (limit)
  3438. "Run through the buffer and add overlays to links."
  3439. (catch 'exit
  3440. (let (f)
  3441. (while (re-search-forward org-plain-link-re limit t)
  3442. (setq f (get-text-property (match-beginning 0) 'face))
  3443. (if (or (eq f 'org-tag)
  3444. (and (listp f) (memq 'org-tag f)))
  3445. nil
  3446. (add-text-properties (match-beginning 0) (match-end 0)
  3447. (list 'mouse-face 'highlight
  3448. 'rear-nonsticky org-nonsticky-props
  3449. 'keymap org-mouse-map
  3450. ))
  3451. (throw 'exit t))))))
  3452. (defun org-activate-code (limit)
  3453. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3454. (progn
  3455. (remove-text-properties (match-beginning 0) (match-end 0)
  3456. '(display t invisible t intangible t))
  3457. t)))
  3458. (defun org-activate-angle-links (limit)
  3459. "Run through the buffer and add overlays to links."
  3460. (if (re-search-forward org-angle-link-re limit t)
  3461. (progn
  3462. (add-text-properties (match-beginning 0) (match-end 0)
  3463. (list 'mouse-face 'highlight
  3464. 'rear-nonsticky org-nonsticky-props
  3465. 'keymap org-mouse-map
  3466. ))
  3467. t)))
  3468. (defun org-activate-footnote-links (limit)
  3469. "Run through the buffer and add overlays to links."
  3470. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3471. limit t)
  3472. (progn
  3473. (add-text-properties (match-beginning 2) (match-end 2)
  3474. (list 'mouse-face 'highlight
  3475. 'rear-nonsticky org-nonsticky-props
  3476. 'keymap org-mouse-map
  3477. 'help-echo
  3478. (if (= (point-at-bol) (match-beginning 2))
  3479. "Footnote definition"
  3480. "Footnote reference")
  3481. ))
  3482. t)))
  3483. (defun org-activate-bracket-links (limit)
  3484. "Run through the buffer and add overlays to bracketed links."
  3485. (if (re-search-forward org-bracket-link-regexp limit t)
  3486. (let* ((help (concat "LINK: "
  3487. (org-match-string-no-properties 1)))
  3488. ;; FIXME: above we should remove the escapes.
  3489. ;; but that requires another match, protecting match data,
  3490. ;; a lot of overhead for font-lock.
  3491. (ip (org-maybe-intangible
  3492. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3493. 'keymap org-mouse-map 'mouse-face 'highlight
  3494. 'font-lock-multiline t 'help-echo help)))
  3495. (vp (list 'rear-nonsticky org-nonsticky-props
  3496. 'keymap org-mouse-map 'mouse-face 'highlight
  3497. ' font-lock-multiline t 'help-echo help)))
  3498. ;; We need to remove the invisible property here. Table narrowing
  3499. ;; may have made some of this invisible.
  3500. (remove-text-properties (match-beginning 0) (match-end 0)
  3501. '(invisible nil))
  3502. (if (match-end 3)
  3503. (progn
  3504. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3505. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3506. (add-text-properties (match-end 3) (match-end 0) ip))
  3507. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3508. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3509. (add-text-properties (match-end 1) (match-end 0) ip))
  3510. t)))
  3511. (defun org-activate-dates (limit)
  3512. "Run through the buffer and add overlays to dates."
  3513. (if (re-search-forward org-tsr-regexp-both limit t)
  3514. (progn
  3515. (add-text-properties (match-beginning 0) (match-end 0)
  3516. (list 'mouse-face 'highlight
  3517. 'rear-nonsticky org-nonsticky-props
  3518. 'keymap org-mouse-map))
  3519. (when org-display-custom-times
  3520. (if (match-end 3)
  3521. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3522. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3523. t)))
  3524. (defvar org-target-link-regexp nil
  3525. "Regular expression matching radio targets in plain text.")
  3526. (make-variable-buffer-local 'org-target-link-regexp)
  3527. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3528. "Regular expression matching a link target.")
  3529. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3530. "Regular expression matching a radio target.")
  3531. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3532. "Regular expression matching any target.")
  3533. (defun org-activate-target-links (limit)
  3534. "Run through the buffer and add overlays to target matches."
  3535. (when org-target-link-regexp
  3536. (let ((case-fold-search t))
  3537. (if (re-search-forward org-target-link-regexp limit t)
  3538. (progn
  3539. (add-text-properties (match-beginning 0) (match-end 0)
  3540. (list 'mouse-face 'highlight
  3541. 'rear-nonsticky org-nonsticky-props
  3542. 'keymap org-mouse-map
  3543. 'help-echo "Radio target link"
  3544. 'org-linked-text t))
  3545. t)))))
  3546. (defun org-update-radio-target-regexp ()
  3547. "Find all radio targets in this file and update the regular expression."
  3548. (interactive)
  3549. (when (memq 'radio org-activate-links)
  3550. (setq org-target-link-regexp
  3551. (org-make-target-link-regexp (org-all-targets 'radio)))
  3552. (org-restart-font-lock)))
  3553. (defun org-hide-wide-columns (limit)
  3554. (let (s e)
  3555. (setq s (text-property-any (point) (or limit (point-max))
  3556. 'org-cwidth t))
  3557. (when s
  3558. (setq e (next-single-property-change s 'org-cwidth))
  3559. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3560. (goto-char e)
  3561. t)))
  3562. (defvar org-latex-and-specials-regexp nil
  3563. "Regular expression for highlighting export special stuff.")
  3564. (defvar org-match-substring-regexp)
  3565. (defvar org-match-substring-with-braces-regexp)
  3566. (defvar org-export-html-special-string-regexps)
  3567. (defun org-compute-latex-and-specials-regexp ()
  3568. "Compute regular expression for stuff treated specially by exporters."
  3569. (if (not org-highlight-latex-fragments-and-specials)
  3570. (org-set-local 'org-latex-and-specials-regexp nil)
  3571. (require 'org-exp)
  3572. (let*
  3573. ((matchers (plist-get org-format-latex-options :matchers))
  3574. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3575. org-latex-regexps)))
  3576. (options (org-combine-plists (org-default-export-plist)
  3577. (org-infile-export-plist)))
  3578. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3579. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3580. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3581. (org-export-html-expand (plist-get options :expand-quoted-html))
  3582. (org-export-with-special-strings (plist-get options :special-strings))
  3583. (re-sub
  3584. (cond
  3585. ((equal org-export-with-sub-superscripts '{})
  3586. (list org-match-substring-with-braces-regexp))
  3587. (org-export-with-sub-superscripts
  3588. (list org-match-substring-regexp))
  3589. (t nil)))
  3590. (re-latex
  3591. (if org-export-with-LaTeX-fragments
  3592. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3593. (re-macros
  3594. (if org-export-with-TeX-macros
  3595. (list (concat "\\\\"
  3596. (regexp-opt
  3597. (append (mapcar 'car org-html-entities)
  3598. (if (boundp 'org-latex-entities)
  3599. org-latex-entities nil))
  3600. 'words))) ; FIXME
  3601. ))
  3602. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3603. (re-special (if org-export-with-special-strings
  3604. (mapcar (lambda (x) (car x))
  3605. org-export-html-special-string-regexps)))
  3606. (re-rest
  3607. (delq nil
  3608. (list
  3609. (if org-export-html-expand "@<[^>\n]+>")
  3610. ))))
  3611. (org-set-local
  3612. 'org-latex-and-specials-regexp
  3613. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3614. re-rest) "\\|")))))
  3615. (defun org-do-latex-and-special-faces (limit)
  3616. "Run through the buffer and add overlays to links."
  3617. (when org-latex-and-specials-regexp
  3618. (let (rtn d)
  3619. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3620. limit t))
  3621. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3622. 'face))
  3623. '(org-code org-verbatim underline)))
  3624. (progn
  3625. (setq rtn t
  3626. d (cond ((member (char-after (1+ (match-beginning 0)))
  3627. '(?_ ?^)) 1)
  3628. (t 0)))
  3629. (font-lock-prepend-text-property
  3630. (+ d (match-beginning 0)) (match-end 0)
  3631. 'face 'org-latex-and-export-specials)
  3632. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3633. '(font-lock-multiline t)))))
  3634. rtn)))
  3635. (defun org-restart-font-lock ()
  3636. "Restart font-lock-mode, to force refontification."
  3637. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3638. (font-lock-mode -1)
  3639. (font-lock-mode 1)))
  3640. (defun org-all-targets (&optional radio)
  3641. "Return a list of all targets in this file.
  3642. With optional argument RADIO, only find radio targets."
  3643. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3644. rtn)
  3645. (save-excursion
  3646. (goto-char (point-min))
  3647. (while (re-search-forward re nil t)
  3648. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3649. rtn)))
  3650. (defun org-make-target-link-regexp (targets)
  3651. "Make regular expression matching all strings in TARGETS.
  3652. The regular expression finds the targets also if there is a line break
  3653. between words."
  3654. (and targets
  3655. (concat
  3656. "\\<\\("
  3657. (mapconcat
  3658. (lambda (x)
  3659. (while (string-match " +" x)
  3660. (setq x (replace-match "\\s-+" t t x)))
  3661. x)
  3662. targets
  3663. "\\|")
  3664. "\\)\\>")))
  3665. (defun org-activate-tags (limit)
  3666. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3667. (progn
  3668. (add-text-properties (match-beginning 1) (match-end 1)
  3669. (list 'mouse-face 'highlight
  3670. 'rear-nonsticky org-nonsticky-props
  3671. 'keymap org-mouse-map))
  3672. t)))
  3673. (defun org-outline-level ()
  3674. (save-excursion
  3675. (looking-at outline-regexp)
  3676. (if (match-beginning 1)
  3677. (+ (org-get-string-indentation (match-string 1)) 1000)
  3678. (1- (- (match-end 0) (match-beginning 0))))))
  3679. (defvar org-font-lock-keywords nil)
  3680. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3681. "Regular expression matching a property line.")
  3682. (defvar org-font-lock-hook nil
  3683. "Functions to be called for special font lock stuff.")
  3684. (defun org-font-lock-hook (limit)
  3685. (run-hook-with-args 'org-font-lock-hook limit))
  3686. (defun org-set-font-lock-defaults ()
  3687. (let* ((em org-fontify-emphasized-text)
  3688. (lk org-activate-links)
  3689. (org-font-lock-extra-keywords
  3690. (list
  3691. ;; Call the hook
  3692. '(org-font-lock-hook)
  3693. ;; Headlines
  3694. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3695. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3696. ;; Table lines
  3697. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3698. (1 'org-table t))
  3699. ;; Table internals
  3700. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3701. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3702. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3703. ;; Drawers
  3704. (list org-drawer-regexp '(0 'org-special-keyword t))
  3705. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3706. ;; Properties
  3707. (list org-property-re
  3708. '(1 'org-special-keyword t)
  3709. '(3 'org-property-value t))
  3710. (if org-format-transports-properties-p
  3711. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3712. ;; Links
  3713. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3714. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3715. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3716. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3717. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3718. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3719. (if (memq 'footnote lk) '(org-activate-footnote-links
  3720. (2 'org-footnote t)))
  3721. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3722. '(org-hide-wide-columns (0 nil append))
  3723. ;; TODO lines
  3724. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3725. '(1 (org-get-todo-face 1) t))
  3726. ;; DONE
  3727. (if org-fontify-done-headline
  3728. (list (concat "^[*]+ +\\<\\("
  3729. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3730. "\\)\\(.*\\)")
  3731. '(2 'org-headline-done t))
  3732. nil)
  3733. ;; Priorities
  3734. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3735. ;; Tags
  3736. '(org-font-lock-add-tag-faces)
  3737. ;; Special keywords
  3738. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3739. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3740. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3741. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3742. ;; Emphasis
  3743. (if em
  3744. (if (featurep 'xemacs)
  3745. '(org-do-emphasis-faces (0 nil append))
  3746. '(org-do-emphasis-faces)))
  3747. ;; Checkboxes
  3748. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3749. 2 'bold prepend)
  3750. (if org-provide-checkbox-statistics
  3751. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3752. (0 (org-get-checkbox-statistics-face) t)))
  3753. ;; Description list items
  3754. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3755. 2 'bold prepend)
  3756. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3757. '(1 'org-archived prepend))
  3758. ;; Specials
  3759. '(org-do-latex-and-special-faces)
  3760. ;; Code
  3761. '(org-activate-code (1 'org-code t))
  3762. ;; COMMENT
  3763. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3764. "\\|" org-quote-string "\\)\\>")
  3765. '(1 'org-special-keyword t))
  3766. '("^#.*" (0 'font-lock-comment-face t))
  3767. )))
  3768. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3769. ;; Now set the full font-lock-keywords
  3770. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3771. (org-set-local 'font-lock-defaults
  3772. '(org-font-lock-keywords t nil nil backward-paragraph))
  3773. (kill-local-variable 'font-lock-keywords) nil))
  3774. (defvar org-m nil)
  3775. (defvar org-l nil)
  3776. (defvar org-f nil)
  3777. (defun org-get-level-face (n)
  3778. "Get the right face for match N in font-lock matching of headlines."
  3779. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3780. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3781. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3782. (cond
  3783. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3784. ((eq n 2) org-f)
  3785. (t (if org-level-color-stars-only nil org-f))))
  3786. (defun org-get-todo-face (kwd)
  3787. "Get the right face for a TODO keyword KWD.
  3788. If KWD is a number, get the corresponding match group."
  3789. (if (numberp kwd) (setq kwd (match-string kwd)))
  3790. (or (cdr (assoc kwd org-todo-keyword-faces))
  3791. (and (member kwd org-done-keywords) 'org-done)
  3792. 'org-todo))
  3793. (defun org-font-lock-add-tag-faces (limit)
  3794. "Add the special tag faces."
  3795. (when (and org-tag-faces org-tags-special-faces-re)
  3796. (while (re-search-forward org-tags-special-faces-re limit t)
  3797. (add-text-properties (match-beginning 1) (match-end 1)
  3798. (list 'face (org-get-tag-face 1)
  3799. 'font-lock-fontified t))
  3800. (backward-char 1))))
  3801. (defun org-get-tag-face (kwd)
  3802. "Get the right face for a TODO keyword KWD.
  3803. If KWD is a number, get the corresponding match group."
  3804. (if (numberp kwd) (setq kwd (match-string kwd)))
  3805. (or (cdr (assoc kwd org-tag-faces))
  3806. 'org-tag))
  3807. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3808. "Remove fontification and activation overlays from links."
  3809. (font-lock-default-unfontify-region beg end)
  3810. (let* ((buffer-undo-list t)
  3811. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3812. (inhibit-modification-hooks t)
  3813. deactivate-mark buffer-file-name buffer-file-truename)
  3814. (remove-text-properties beg end
  3815. '(mouse-face t keymap t org-linked-text t
  3816. invisible t intangible t))))
  3817. ;;;; Visibility cycling, including org-goto and indirect buffer
  3818. ;;; Cycling
  3819. (defvar org-cycle-global-status nil)
  3820. (make-variable-buffer-local 'org-cycle-global-status)
  3821. (defvar org-cycle-subtree-status nil)
  3822. (make-variable-buffer-local 'org-cycle-subtree-status)
  3823. ;;;###autoload
  3824. (defun org-cycle (&optional arg)
  3825. "Visibility cycling for Org-mode.
  3826. - When this function is called with a prefix argument, rotate the entire
  3827. buffer through 3 states (global cycling)
  3828. 1. OVERVIEW: Show only top-level headlines.
  3829. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3830. 3. SHOW ALL: Show everything.
  3831. When called with two C-u C-u prefixes, switch to the startup visibility,
  3832. determined by the variable `org-startup-folded', and by any VISIBILITY
  3833. properties in the buffer.
  3834. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3835. including drawers.
  3836. - When point is at the beginning of a headline, rotate the subtree started
  3837. by this line through 3 different states (local cycling)
  3838. 1. FOLDED: Only the main headline is shown.
  3839. 2. CHILDREN: The main headline and the direct children are shown.
  3840. From this state, you can move to one of the children
  3841. and zoom in further.
  3842. 3. SUBTREE: Show the entire subtree, including body text.
  3843. - When there is a numeric prefix, go up to a heading with level ARG, do
  3844. a `show-subtree' and return to the previous cursor position. If ARG
  3845. is negative, go up that many levels.
  3846. - When point is not at the beginning of a headline, execute the global
  3847. binding for TAB, which is re-indenting the line. See the option
  3848. `org-cycle-emulate-tab' for details.
  3849. - Special case: if point is at the beginning of the buffer and there is
  3850. no headline in line 1, this function will act as if called with prefix arg.
  3851. But only if also the variable `org-cycle-global-at-bob' is t."
  3852. (interactive "P")
  3853. (org-load-modules-maybe)
  3854. (let* ((outline-regexp
  3855. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3856. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3857. outline-regexp))
  3858. (bob-special (and org-cycle-global-at-bob (bobp)
  3859. (not (looking-at outline-regexp))))
  3860. (org-cycle-hook
  3861. (if bob-special
  3862. (delq 'org-optimize-window-after-visibility-change
  3863. (copy-sequence org-cycle-hook))
  3864. org-cycle-hook))
  3865. (pos (point)))
  3866. (if (or bob-special (equal arg '(4)))
  3867. ;; special case: use global cycling
  3868. (setq arg t))
  3869. (cond
  3870. ((equal arg '(16))
  3871. (org-set-startup-visibility)
  3872. (message "Startup visibility, plus VISIBILITY properties"))
  3873. ((equal arg '(64))
  3874. (show-all)
  3875. (message "Entire buffer visible, including drawers"))
  3876. ((org-at-table-p 'any)
  3877. ;; Enter the table or move to the next field in the table
  3878. (or (org-table-recognize-table.el)
  3879. (progn
  3880. (if arg (org-table-edit-field t)
  3881. (org-table-justify-field-maybe)
  3882. (call-interactively 'org-table-next-field)))))
  3883. ((eq arg t) ;; Global cycling
  3884. (cond
  3885. ((and (eq last-command this-command)
  3886. (eq org-cycle-global-status 'overview))
  3887. ;; We just created the overview - now do table of contents
  3888. ;; This can be slow in very large buffers, so indicate action
  3889. (message "CONTENTS...")
  3890. (org-content)
  3891. (message "CONTENTS...done")
  3892. (setq org-cycle-global-status 'contents)
  3893. (run-hook-with-args 'org-cycle-hook 'contents))
  3894. ((and (eq last-command this-command)
  3895. (eq org-cycle-global-status 'contents))
  3896. ;; We just showed the table of contents - now show everything
  3897. (show-all)
  3898. (message "SHOW ALL")
  3899. (setq org-cycle-global-status 'all)
  3900. (run-hook-with-args 'org-cycle-hook 'all))
  3901. (t
  3902. ;; Default action: go to overview
  3903. (org-overview)
  3904. (message "OVERVIEW")
  3905. (setq org-cycle-global-status 'overview)
  3906. (run-hook-with-args 'org-cycle-hook 'overview))))
  3907. ((and org-drawers org-drawer-regexp
  3908. (save-excursion
  3909. (beginning-of-line 1)
  3910. (looking-at org-drawer-regexp)))
  3911. ;; Toggle block visibility
  3912. (org-flag-drawer
  3913. (not (get-char-property (match-end 0) 'invisible))))
  3914. ((integerp arg)
  3915. ;; Show-subtree, ARG levels up from here.
  3916. (save-excursion
  3917. (org-back-to-heading)
  3918. (outline-up-heading (if (< arg 0) (- arg)
  3919. (- (funcall outline-level) arg)))
  3920. (org-show-subtree)))
  3921. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3922. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3923. ;; At a heading: rotate between three different views
  3924. (org-back-to-heading)
  3925. (let ((goal-column 0) eoh eol eos)
  3926. ;; First, some boundaries
  3927. (save-excursion
  3928. (org-back-to-heading)
  3929. (save-excursion
  3930. (beginning-of-line 2)
  3931. (while (and (not (eobp)) ;; this is like `next-line'
  3932. (get-char-property (1- (point)) 'invisible))
  3933. (beginning-of-line 2)) (setq eol (point)))
  3934. (outline-end-of-heading) (setq eoh (point))
  3935. (org-end-of-subtree t)
  3936. (unless (eobp)
  3937. (skip-chars-forward " \t\n")
  3938. (beginning-of-line 1) ; in case this is an item
  3939. )
  3940. (setq eos (1- (point))))
  3941. ;; Find out what to do next and set `this-command'
  3942. (cond
  3943. ((= eos eoh)
  3944. ;; Nothing is hidden behind this heading
  3945. (message "EMPTY ENTRY")
  3946. (setq org-cycle-subtree-status nil)
  3947. (save-excursion
  3948. (goto-char eos)
  3949. (outline-next-heading)
  3950. (if (org-invisible-p) (org-flag-heading nil))))
  3951. ((or (>= eol eos)
  3952. (not (string-match "\\S-" (buffer-substring eol eos))))
  3953. ;; Entire subtree is hidden in one line: open it
  3954. (org-show-entry)
  3955. (show-children)
  3956. (message "CHILDREN")
  3957. (save-excursion
  3958. (goto-char eos)
  3959. (outline-next-heading)
  3960. (if (org-invisible-p) (org-flag-heading nil)))
  3961. (setq org-cycle-subtree-status 'children)
  3962. (run-hook-with-args 'org-cycle-hook 'children))
  3963. ((and (eq last-command this-command)
  3964. (eq org-cycle-subtree-status 'children))
  3965. ;; We just showed the children, now show everything.
  3966. (org-show-subtree)
  3967. (message "SUBTREE")
  3968. (setq org-cycle-subtree-status 'subtree)
  3969. (run-hook-with-args 'org-cycle-hook 'subtree))
  3970. (t
  3971. ;; Default action: hide the subtree.
  3972. (hide-subtree)
  3973. (message "FOLDED")
  3974. (setq org-cycle-subtree-status 'folded)
  3975. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3976. ;; TAB emulation and template completion
  3977. (buffer-read-only (org-back-to-heading))
  3978. ((org-try-structure-completion))
  3979. ((org-try-cdlatex-tab))
  3980. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3981. (or (not (bolp))
  3982. (not (looking-at outline-regexp))))
  3983. (call-interactively (global-key-binding "\t")))
  3984. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3985. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3986. (or (and (eq org-cycle-emulate-tab 'white)
  3987. (= (match-end 0) (point-at-eol)))
  3988. (and (eq org-cycle-emulate-tab 'whitestart)
  3989. (>= (match-end 0) pos))))
  3990. t
  3991. (eq org-cycle-emulate-tab t))
  3992. (call-interactively (global-key-binding "\t")))
  3993. (t (save-excursion
  3994. (org-back-to-heading)
  3995. (org-cycle))))))
  3996. ;;;###autoload
  3997. (defun org-global-cycle (&optional arg)
  3998. "Cycle the global visibility. For details see `org-cycle'.
  3999. With C-u prefix arg, switch to startup visibility.
  4000. With a numeric prefix, show all headlines up to that level."
  4001. (interactive "P")
  4002. (let ((org-cycle-include-plain-lists
  4003. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4004. (cond
  4005. ((integerp arg)
  4006. (show-all)
  4007. (hide-sublevels arg)
  4008. (setq org-cycle-global-status 'contents))
  4009. ((equal arg '(4))
  4010. (org-set-startup-visibility)
  4011. (message "Startup visibility, plus VISIBILITY properties."))
  4012. (t
  4013. (org-cycle '(4))))))
  4014. (defun org-set-startup-visibility ()
  4015. "Set the visibility required by startup options and properties."
  4016. (cond
  4017. ((eq org-startup-folded t)
  4018. (org-cycle '(4)))
  4019. ((eq org-startup-folded 'content)
  4020. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4021. (org-cycle '(4)) (org-cycle '(4)))))
  4022. (org-set-visibility-according-to-property 'no-cleanup)
  4023. (org-cycle-hide-archived-subtrees 'all)
  4024. (org-cycle-hide-drawers 'all)
  4025. (org-cycle-show-empty-lines 'all))
  4026. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4027. "Switch subtree visibilities according to :VISIBILITY: property."
  4028. (interactive)
  4029. (let (org-show-entry-below state)
  4030. (save-excursion
  4031. (goto-char (point-min))
  4032. (while (re-search-forward
  4033. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4034. nil t)
  4035. (setq state (match-string 1))
  4036. (save-excursion
  4037. (org-back-to-heading t)
  4038. (hide-subtree)
  4039. (org-reveal)
  4040. (cond
  4041. ((equal state '("fold" "folded"))
  4042. (hide-subtree))
  4043. ((equal state "children")
  4044. (org-show-hidden-entry)
  4045. (show-children))
  4046. ((equal state "content")
  4047. (save-excursion
  4048. (save-restriction
  4049. (org-narrow-to-subtree)
  4050. (org-content))))
  4051. ((member state '("all" "showall"))
  4052. (show-subtree)))))
  4053. (unless no-cleanup
  4054. (org-cycle-hide-archived-subtrees 'all)
  4055. (org-cycle-hide-drawers 'all)
  4056. (org-cycle-show-empty-lines 'all)))))
  4057. (defun org-overview ()
  4058. "Switch to overview mode, showing only top-level headlines.
  4059. Really, this shows all headlines with level equal or greater than the level
  4060. of the first headline in the buffer. This is important, because if the
  4061. first headline is not level one, then (hide-sublevels 1) gives confusing
  4062. results."
  4063. (interactive)
  4064. (let ((level (save-excursion
  4065. (goto-char (point-min))
  4066. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4067. (progn
  4068. (goto-char (match-beginning 0))
  4069. (funcall outline-level))))))
  4070. (and level (hide-sublevels level))))
  4071. (defun org-content (&optional arg)
  4072. "Show all headlines in the buffer, like a table of contents.
  4073. With numerical argument N, show content up to level N."
  4074. (interactive "P")
  4075. (save-excursion
  4076. ;; Visit all headings and show their offspring
  4077. (and (integerp arg) (org-overview))
  4078. (goto-char (point-max))
  4079. (catch 'exit
  4080. (while (and (progn (condition-case nil
  4081. (outline-previous-visible-heading 1)
  4082. (error (goto-char (point-min))))
  4083. t)
  4084. (looking-at outline-regexp))
  4085. (if (integerp arg)
  4086. (show-children (1- arg))
  4087. (show-branches))
  4088. (if (bobp) (throw 'exit nil))))))
  4089. (defun org-optimize-window-after-visibility-change (state)
  4090. "Adjust the window after a change in outline visibility.
  4091. This function is the default value of the hook `org-cycle-hook'."
  4092. (when (get-buffer-window (current-buffer))
  4093. (cond
  4094. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4095. ; ((eq state 'overview) (org-beginning-of-line))
  4096. ((eq state 'content) nil)
  4097. ((eq state 'all) nil)
  4098. ((eq state 'folded) nil)
  4099. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4100. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4101. (defun org-compact-display-after-subtree-move ()
  4102. "Show a compacter version of the tree of the entry's parent."
  4103. (save-excursion
  4104. (if (org-up-heading-safe)
  4105. (progn
  4106. (hide-subtree)
  4107. (show-entry)
  4108. (show-children)
  4109. (org-cycle-show-empty-lines 'children)
  4110. (org-cycle-hide-drawers 'children))
  4111. (org-overview))))
  4112. (defun org-cycle-show-empty-lines (state)
  4113. "Show empty lines above all visible headlines.
  4114. The region to be covered depends on STATE when called through
  4115. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4116. entire buffer covered. Note that an empty line is only shown if there
  4117. are at least `org-cycle-separator-lines' empty lines before the headline."
  4118. (when (> org-cycle-separator-lines 0)
  4119. (save-excursion
  4120. (let* ((n org-cycle-separator-lines)
  4121. (re (cond
  4122. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4123. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4124. (t (let ((ns (number-to-string (- n 2))))
  4125. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4126. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4127. beg end)
  4128. (cond
  4129. ((memq state '(overview contents t))
  4130. (setq beg (point-min) end (point-max)))
  4131. ((memq state '(children folded))
  4132. (setq beg (point) end (progn (org-end-of-subtree t t)
  4133. (beginning-of-line 2)
  4134. (point)))))
  4135. (when beg
  4136. (goto-char beg)
  4137. (while (re-search-forward re end t)
  4138. (if (not (get-char-property (match-end 1) 'invisible))
  4139. (outline-flag-region
  4140. (match-beginning 1) (match-end 1) nil)))))))
  4141. ;; Never hide empty lines at the end of the file.
  4142. (save-excursion
  4143. (goto-char (point-max))
  4144. (outline-previous-heading)
  4145. (outline-end-of-heading)
  4146. (if (and (looking-at "[ \t\n]+")
  4147. (= (match-end 0) (point-max)))
  4148. (outline-flag-region (point) (match-end 0) nil))))
  4149. (defun org-show-empty-lines-in-parent ()
  4150. "Move to the parent and re-show empty lines before visible headlines."
  4151. (save-excursion
  4152. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4153. (org-cycle-show-empty-lines context))))
  4154. (defun org-cycle-hide-drawers (state)
  4155. "Re-hide all drawers after a visibility state change."
  4156. (when (and (org-mode-p)
  4157. (not (memq state '(overview folded))))
  4158. (save-excursion
  4159. (let* ((globalp (memq state '(contents all)))
  4160. (beg (if globalp (point-min) (point)))
  4161. (end (if globalp (point-max) (org-end-of-subtree t))))
  4162. (goto-char beg)
  4163. (while (re-search-forward org-drawer-regexp end t)
  4164. (org-flag-drawer t))))))
  4165. (defun org-flag-drawer (flag)
  4166. (save-excursion
  4167. (beginning-of-line 1)
  4168. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4169. (let ((b (match-end 0))
  4170. (outline-regexp org-outline-regexp))
  4171. (if (re-search-forward
  4172. "^[ \t]*:END:"
  4173. (save-excursion (outline-next-heading) (point)) t)
  4174. (outline-flag-region b (point-at-eol) flag)
  4175. (error ":END: line missing"))))))
  4176. (defun org-subtree-end-visible-p ()
  4177. "Is the end of the current subtree visible?"
  4178. (pos-visible-in-window-p
  4179. (save-excursion (org-end-of-subtree t) (point))))
  4180. (defun org-first-headline-recenter (&optional N)
  4181. "Move cursor to the first headline and recenter the headline.
  4182. Optional argument N means, put the headline into the Nth line of the window."
  4183. (goto-char (point-min))
  4184. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4185. (beginning-of-line)
  4186. (recenter (prefix-numeric-value N))))
  4187. ;;; Org-goto
  4188. (defvar org-goto-window-configuration nil)
  4189. (defvar org-goto-marker nil)
  4190. (defvar org-goto-map
  4191. (let ((map (make-sparse-keymap)))
  4192. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4193. (while (setq cmd (pop cmds))
  4194. (substitute-key-definition cmd cmd map global-map)))
  4195. (suppress-keymap map)
  4196. (org-defkey map "\C-m" 'org-goto-ret)
  4197. (org-defkey map [(return)] 'org-goto-ret)
  4198. (org-defkey map [(left)] 'org-goto-left)
  4199. (org-defkey map [(right)] 'org-goto-right)
  4200. (org-defkey map [(control ?g)] 'org-goto-quit)
  4201. (org-defkey map "\C-i" 'org-cycle)
  4202. (org-defkey map [(tab)] 'org-cycle)
  4203. (org-defkey map [(down)] 'outline-next-visible-heading)
  4204. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4205. (if org-goto-auto-isearch
  4206. (if (fboundp 'define-key-after)
  4207. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4208. nil)
  4209. (org-defkey map "q" 'org-goto-quit)
  4210. (org-defkey map "n" 'outline-next-visible-heading)
  4211. (org-defkey map "p" 'outline-previous-visible-heading)
  4212. (org-defkey map "f" 'outline-forward-same-level)
  4213. (org-defkey map "b" 'outline-backward-same-level)
  4214. (org-defkey map "u" 'outline-up-heading))
  4215. (org-defkey map "/" 'org-occur)
  4216. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4217. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4218. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4219. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4220. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4221. map))
  4222. (defconst org-goto-help
  4223. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4224. RET=jump to location [Q]uit and return to previous location
  4225. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4226. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4227. ;; FIXME: Docstring doe not mention both interfaces
  4228. (defun org-goto (&optional alternative-interface)
  4229. "Look up a different location in the current file, keeping current visibility.
  4230. When you want look-up or go to a different location in a document, the
  4231. fastest way is often to fold the entire buffer and then dive into the tree.
  4232. This method has the disadvantage, that the previous location will be folded,
  4233. which may not be what you want.
  4234. This command works around this by showing a copy of the current buffer
  4235. in an indirect buffer, in overview mode. You can dive into the tree in
  4236. that copy, use org-occur and incremental search to find a location.
  4237. When pressing RET or `Q', the command returns to the original buffer in
  4238. which the visibility is still unchanged. After RET is will also jump to
  4239. the location selected in the indirect buffer and expose the
  4240. the headline hierarchy above."
  4241. (interactive "P")
  4242. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4243. (org-refile-use-outline-path t)
  4244. (interface
  4245. (if (not alternative-interface)
  4246. org-goto-interface
  4247. (if (eq org-goto-interface 'outline)
  4248. 'outline-path-completion
  4249. 'outline)))
  4250. (org-goto-start-pos (point))
  4251. (selected-point
  4252. (if (eq interface 'outline)
  4253. (car (org-get-location (current-buffer) org-goto-help))
  4254. (nth 3 (org-refile-get-location "Goto: ")))))
  4255. (if selected-point
  4256. (progn
  4257. (org-mark-ring-push org-goto-start-pos)
  4258. (goto-char selected-point)
  4259. (if (or (org-invisible-p) (org-invisible-p2))
  4260. (org-show-context 'org-goto)))
  4261. (message "Quit"))))
  4262. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4263. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4264. (defvar org-goto-local-auto-isearch-map) ; defined below
  4265. (defun org-get-location (buf help)
  4266. "Let the user select a location in the Org-mode buffer BUF.
  4267. This function uses a recursive edit. It returns the selected position
  4268. or nil."
  4269. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4270. (isearch-hide-immediately nil)
  4271. (isearch-search-fun-function
  4272. (lambda () 'org-goto-local-search-headings))
  4273. (org-goto-selected-point org-goto-exit-command))
  4274. (save-excursion
  4275. (save-window-excursion
  4276. (delete-other-windows)
  4277. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4278. (switch-to-buffer
  4279. (condition-case nil
  4280. (make-indirect-buffer (current-buffer) "*org-goto*")
  4281. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4282. (with-output-to-temp-buffer "*Help*"
  4283. (princ help))
  4284. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4285. (setq buffer-read-only nil)
  4286. (let ((org-startup-truncated t)
  4287. (org-startup-folded nil)
  4288. (org-startup-align-all-tables nil))
  4289. (org-mode)
  4290. (org-overview))
  4291. (setq buffer-read-only t)
  4292. (if (and (boundp 'org-goto-start-pos)
  4293. (integer-or-marker-p org-goto-start-pos))
  4294. (let ((org-show-hierarchy-above t)
  4295. (org-show-siblings t)
  4296. (org-show-following-heading t))
  4297. (goto-char org-goto-start-pos)
  4298. (and (org-invisible-p) (org-show-context)))
  4299. (goto-char (point-min)))
  4300. (org-beginning-of-line)
  4301. (message "Select location and press RET")
  4302. (use-local-map org-goto-map)
  4303. (recursive-edit)
  4304. ))
  4305. (kill-buffer "*org-goto*")
  4306. (cons org-goto-selected-point org-goto-exit-command)))
  4307. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4308. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4309. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4310. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4311. (defun org-goto-local-search-headings (string bound noerror)
  4312. "Search and make sure that any matches are in headlines."
  4313. (catch 'return
  4314. (while (if isearch-forward
  4315. (search-forward string bound noerror)
  4316. (search-backward string bound noerror))
  4317. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4318. (and (member :headline context)
  4319. (not (member :tags context))))
  4320. (throw 'return (point))))))
  4321. (defun org-goto-local-auto-isearch ()
  4322. "Start isearch."
  4323. (interactive)
  4324. (goto-char (point-min))
  4325. (let ((keys (this-command-keys)))
  4326. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4327. (isearch-mode t)
  4328. (isearch-process-search-char (string-to-char keys)))))
  4329. (defun org-goto-ret (&optional arg)
  4330. "Finish `org-goto' by going to the new location."
  4331. (interactive "P")
  4332. (setq org-goto-selected-point (point)
  4333. org-goto-exit-command 'return)
  4334. (throw 'exit nil))
  4335. (defun org-goto-left ()
  4336. "Finish `org-goto' by going to the new location."
  4337. (interactive)
  4338. (if (org-on-heading-p)
  4339. (progn
  4340. (beginning-of-line 1)
  4341. (setq org-goto-selected-point (point)
  4342. org-goto-exit-command 'left)
  4343. (throw 'exit nil))
  4344. (error "Not on a heading")))
  4345. (defun org-goto-right ()
  4346. "Finish `org-goto' by going to the new location."
  4347. (interactive)
  4348. (if (org-on-heading-p)
  4349. (progn
  4350. (setq org-goto-selected-point (point)
  4351. org-goto-exit-command 'right)
  4352. (throw 'exit nil))
  4353. (error "Not on a heading")))
  4354. (defun org-goto-quit ()
  4355. "Finish `org-goto' without cursor motion."
  4356. (interactive)
  4357. (setq org-goto-selected-point nil)
  4358. (setq org-goto-exit-command 'quit)
  4359. (throw 'exit nil))
  4360. ;;; Indirect buffer display of subtrees
  4361. (defvar org-indirect-dedicated-frame nil
  4362. "This is the frame being used for indirect tree display.")
  4363. (defvar org-last-indirect-buffer nil)
  4364. (defun org-tree-to-indirect-buffer (&optional arg)
  4365. "Create indirect buffer and narrow it to current subtree.
  4366. With numerical prefix ARG, go up to this level and then take that tree.
  4367. If ARG is negative, go up that many levels.
  4368. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4369. indirect buffer previously made with this command, to avoid proliferation of
  4370. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4371. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4372. is kept so that you can work with several indirect buffers at the same time.
  4373. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4374. requests that a new frame be made for the new buffer, so that the dedicated
  4375. frame is not changed."
  4376. (interactive "P")
  4377. (let ((cbuf (current-buffer))
  4378. (cwin (selected-window))
  4379. (pos (point))
  4380. beg end level heading ibuf)
  4381. (save-excursion
  4382. (org-back-to-heading t)
  4383. (when (numberp arg)
  4384. (setq level (org-outline-level))
  4385. (if (< arg 0) (setq arg (+ level arg)))
  4386. (while (> (setq level (org-outline-level)) arg)
  4387. (outline-up-heading 1 t)))
  4388. (setq beg (point)
  4389. heading (org-get-heading))
  4390. (org-end-of-subtree t) (setq end (point)))
  4391. (if (and (buffer-live-p org-last-indirect-buffer)
  4392. (not (eq org-indirect-buffer-display 'new-frame))
  4393. (not arg))
  4394. (kill-buffer org-last-indirect-buffer))
  4395. (setq ibuf (org-get-indirect-buffer cbuf)
  4396. org-last-indirect-buffer ibuf)
  4397. (cond
  4398. ((or (eq org-indirect-buffer-display 'new-frame)
  4399. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4400. (select-frame (make-frame))
  4401. (delete-other-windows)
  4402. (switch-to-buffer ibuf)
  4403. (org-set-frame-title heading))
  4404. ((eq org-indirect-buffer-display 'dedicated-frame)
  4405. (raise-frame
  4406. (select-frame (or (and org-indirect-dedicated-frame
  4407. (frame-live-p org-indirect-dedicated-frame)
  4408. org-indirect-dedicated-frame)
  4409. (setq org-indirect-dedicated-frame (make-frame)))))
  4410. (delete-other-windows)
  4411. (switch-to-buffer ibuf)
  4412. (org-set-frame-title (concat "Indirect: " heading)))
  4413. ((eq org-indirect-buffer-display 'current-window)
  4414. (switch-to-buffer ibuf))
  4415. ((eq org-indirect-buffer-display 'other-window)
  4416. (pop-to-buffer ibuf))
  4417. (t (error "Invalid value.")))
  4418. (if (featurep 'xemacs)
  4419. (save-excursion (org-mode) (turn-on-font-lock)))
  4420. (narrow-to-region beg end)
  4421. (show-all)
  4422. (goto-char pos)
  4423. (and (window-live-p cwin) (select-window cwin))))
  4424. (defun org-get-indirect-buffer (&optional buffer)
  4425. (setq buffer (or buffer (current-buffer)))
  4426. (let ((n 1) (base (buffer-name buffer)) bname)
  4427. (while (buffer-live-p
  4428. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4429. (setq n (1+ n)))
  4430. (condition-case nil
  4431. (make-indirect-buffer buffer bname 'clone)
  4432. (error (make-indirect-buffer buffer bname)))))
  4433. (defun org-set-frame-title (title)
  4434. "Set the title of the current frame to the string TITLE."
  4435. ;; FIXME: how to name a single frame in XEmacs???
  4436. (unless (featurep 'xemacs)
  4437. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4438. ;;;; Structure editing
  4439. ;;; Inserting headlines
  4440. (defun org-previous-line-empty-p ()
  4441. (save-excursion
  4442. (and (not (bobp))
  4443. (or (beginning-of-line 0) t)
  4444. (save-match-data
  4445. (looking-at "[ \t]*$")))))
  4446. (defun org-insert-heading (&optional force-heading)
  4447. "Insert a new heading or item with same depth at point.
  4448. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4449. If point is at the beginning of a headline, insert a sibling before the
  4450. current headline. If point is not at the beginning, do not split the line,
  4451. but create the new headline after the current line."
  4452. (interactive "P")
  4453. (if (= (buffer-size) 0)
  4454. (insert "\n* ")
  4455. (when (or force-heading (not (org-insert-item)))
  4456. (let* ((empty-line-p nil)
  4457. (head (save-excursion
  4458. (condition-case nil
  4459. (progn
  4460. (org-back-to-heading)
  4461. (setq empty-line-p (org-previous-line-empty-p))
  4462. (match-string 0))
  4463. (error "*"))))
  4464. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4465. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4466. pos hide-previous previous-pos)
  4467. (cond
  4468. ((and (org-on-heading-p) (bolp)
  4469. (or (bobp)
  4470. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4471. ;; insert before the current line
  4472. (open-line (if blank 2 1)))
  4473. ((and (bolp)
  4474. (or (bobp)
  4475. (save-excursion
  4476. (backward-char 1) (not (org-invisible-p)))))
  4477. ;; insert right here
  4478. nil)
  4479. (t
  4480. ;; somewhere in the line
  4481. (save-excursion
  4482. (setq previous-pos (point-at-bol))
  4483. (end-of-line)
  4484. (setq hide-previous (org-invisible-p)))
  4485. (and org-insert-heading-respect-content (org-show-subtree))
  4486. (let ((split
  4487. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4488. (save-excursion
  4489. (let ((p (point)))
  4490. (goto-char (point-at-bol))
  4491. (and (looking-at org-complex-heading-regexp)
  4492. (> p (match-beginning 4)))))))
  4493. tags pos)
  4494. (cond
  4495. (org-insert-heading-respect-content
  4496. (org-end-of-subtree nil t)
  4497. (or (bolp) (newline))
  4498. (or (org-previous-line-empty-p)
  4499. (and blank (newline)))
  4500. (open-line 1))
  4501. ((org-on-heading-p)
  4502. (when hide-previous
  4503. (show-children)
  4504. (org-show-entry))
  4505. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4506. (setq tags (and (match-end 2) (match-string 2)))
  4507. (and (match-end 1)
  4508. (delete-region (match-beginning 1) (match-end 1)))
  4509. (setq pos (point-at-bol))
  4510. (or split (end-of-line 1))
  4511. (delete-horizontal-space)
  4512. (newline (if blank 2 1))
  4513. (when tags
  4514. (save-excursion
  4515. (goto-char pos)
  4516. (end-of-line 1)
  4517. (insert " " tags)
  4518. (org-set-tags nil 'align))))
  4519. (t
  4520. (or split (end-of-line 1))
  4521. (newline (if blank 2 1)))))))
  4522. (insert head) (just-one-space)
  4523. (setq pos (point))
  4524. (end-of-line 1)
  4525. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4526. (when (and org-insert-heading-respect-content hide-previous)
  4527. (save-excursion
  4528. (goto-char previous-pos)
  4529. (hide-subtree)))
  4530. (run-hooks 'org-insert-heading-hook)))))
  4531. (defun org-get-heading (&optional no-tags)
  4532. "Return the heading of the current entry, without the stars."
  4533. (save-excursion
  4534. (org-back-to-heading t)
  4535. (if (looking-at
  4536. (if no-tags
  4537. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4538. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4539. (match-string 1) "")))
  4540. (defun org-heading-components ()
  4541. "Return the components of the current heading.
  4542. This is a list with the following elements:
  4543. - the level as an integer
  4544. - the reduced level, different if `org-odd-levels-only' is set.
  4545. - the TODO keyword, or nil
  4546. - the priority character, like ?A, or nil if no priority is given
  4547. - the headline text itself, or the tags string if no headline text
  4548. - the tags string, or nil."
  4549. (save-excursion
  4550. (org-back-to-heading t)
  4551. (if (looking-at org-complex-heading-regexp)
  4552. (list (length (match-string 1))
  4553. (org-reduced-level (length (match-string 1)))
  4554. (org-match-string-no-properties 2)
  4555. (and (match-end 3) (aref (match-string 3) 2))
  4556. (org-match-string-no-properties 4)
  4557. (org-match-string-no-properties 5)))))
  4558. (defun org-insert-heading-after-current ()
  4559. "Insert a new heading with same level as current, after current subtree."
  4560. (interactive)
  4561. (org-back-to-heading)
  4562. (org-insert-heading)
  4563. (org-move-subtree-down)
  4564. (end-of-line 1))
  4565. (defun org-insert-heading-respect-content ()
  4566. (interactive)
  4567. (let ((org-insert-heading-respect-content t))
  4568. (org-insert-heading t)))
  4569. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4570. (interactive "P")
  4571. (let ((org-insert-heading-respect-content t))
  4572. (org-insert-todo-heading force-state t)))
  4573. (defun org-insert-todo-heading (arg &optional force-heading)
  4574. "Insert a new heading with the same level and TODO state as current heading.
  4575. If the heading has no TODO state, or if the state is DONE, use the first
  4576. state (TODO by default). Also with prefix arg, force first state."
  4577. (interactive "P")
  4578. (when (or force-heading (not (org-insert-item 'checkbox)))
  4579. (org-insert-heading force-heading)
  4580. (save-excursion
  4581. (org-back-to-heading)
  4582. (outline-previous-heading)
  4583. (looking-at org-todo-line-regexp))
  4584. (if (or arg
  4585. (not (match-beginning 2))
  4586. (member (match-string 2) org-done-keywords))
  4587. (insert (car org-todo-keywords-1) " ")
  4588. (insert (match-string 2) " "))
  4589. (when org-provide-todo-statistics
  4590. (org-update-parent-todo-statistics))))
  4591. (defun org-insert-subheading (arg)
  4592. "Insert a new subheading and demote it.
  4593. Works for outline headings and for plain lists alike."
  4594. (interactive "P")
  4595. (org-insert-heading arg)
  4596. (cond
  4597. ((org-on-heading-p) (org-do-demote))
  4598. ((org-at-item-p) (org-indent-item 1))))
  4599. (defun org-insert-todo-subheading (arg)
  4600. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4601. Works for outline headings and for plain lists alike."
  4602. (interactive "P")
  4603. (org-insert-todo-heading arg)
  4604. (cond
  4605. ((org-on-heading-p) (org-do-demote))
  4606. ((org-at-item-p) (org-indent-item 1))))
  4607. ;;; Promotion and Demotion
  4608. (defun org-promote-subtree ()
  4609. "Promote the entire subtree.
  4610. See also `org-promote'."
  4611. (interactive)
  4612. (save-excursion
  4613. (org-map-tree 'org-promote))
  4614. (org-fix-position-after-promote))
  4615. (defun org-demote-subtree ()
  4616. "Demote the entire subtree. See `org-demote'.
  4617. See also `org-promote'."
  4618. (interactive)
  4619. (save-excursion
  4620. (org-map-tree 'org-demote))
  4621. (org-fix-position-after-promote))
  4622. (defun org-do-promote ()
  4623. "Promote the current heading higher up the tree.
  4624. If the region is active in `transient-mark-mode', promote all headings
  4625. in the region."
  4626. (interactive)
  4627. (save-excursion
  4628. (if (org-region-active-p)
  4629. (org-map-region 'org-promote (region-beginning) (region-end))
  4630. (org-promote)))
  4631. (org-fix-position-after-promote))
  4632. (defun org-do-demote ()
  4633. "Demote the current heading lower down the tree.
  4634. If the region is active in `transient-mark-mode', demote all headings
  4635. in the region."
  4636. (interactive)
  4637. (save-excursion
  4638. (if (org-region-active-p)
  4639. (org-map-region 'org-demote (region-beginning) (region-end))
  4640. (org-demote)))
  4641. (org-fix-position-after-promote))
  4642. (defun org-fix-position-after-promote ()
  4643. "Make sure that after pro/demotion cursor position is right."
  4644. (let ((pos (point)))
  4645. (when (save-excursion
  4646. (beginning-of-line 1)
  4647. (looking-at org-todo-line-regexp)
  4648. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4649. (cond ((eobp) (insert " "))
  4650. ((eolp) (insert " "))
  4651. ((equal (char-after) ?\ ) (forward-char 1))))))
  4652. (defun org-reduced-level (l)
  4653. "Compute the effective level of a heading.
  4654. This takes into account the setting of `org-odd-levels-only'."
  4655. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4656. (defun org-get-valid-level (level &optional change)
  4657. "Rectify a level change under the influence of `org-odd-levels-only'
  4658. LEVEL is a current level, CHANGE is by how much the level should be
  4659. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4660. even level numbers will become the next higher odd number."
  4661. (if org-odd-levels-only
  4662. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4663. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4664. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4665. (max 1 (+ level change))))
  4666. (if (boundp 'define-obsolete-function-alias)
  4667. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4668. (define-obsolete-function-alias 'org-get-legal-level
  4669. 'org-get-valid-level)
  4670. (define-obsolete-function-alias 'org-get-legal-level
  4671. 'org-get-valid-level "23.1")))
  4672. (defun org-promote ()
  4673. "Promote the current heading higher up the tree.
  4674. If the region is active in `transient-mark-mode', promote all headings
  4675. in the region."
  4676. (org-back-to-heading t)
  4677. (let* ((level (save-match-data (funcall outline-level)))
  4678. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4679. (diff (abs (- level (length up-head) -1))))
  4680. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4681. (replace-match up-head nil t)
  4682. ;; Fixup tag positioning
  4683. (and org-auto-align-tags (org-set-tags nil t))
  4684. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4685. (defun org-demote ()
  4686. "Demote the current heading lower down the tree.
  4687. If the region is active in `transient-mark-mode', demote all headings
  4688. in the region."
  4689. (org-back-to-heading t)
  4690. (let* ((level (save-match-data (funcall outline-level)))
  4691. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4692. (diff (abs (- level (length down-head) -1))))
  4693. (replace-match down-head nil t)
  4694. ;; Fixup tag positioning
  4695. (and org-auto-align-tags (org-set-tags nil t))
  4696. (if org-adapt-indentation (org-fixup-indentation diff))))
  4697. (defun org-map-tree (fun)
  4698. "Call FUN for every heading underneath the current one."
  4699. (org-back-to-heading)
  4700. (let ((level (funcall outline-level)))
  4701. (save-excursion
  4702. (funcall fun)
  4703. (while (and (progn
  4704. (outline-next-heading)
  4705. (> (funcall outline-level) level))
  4706. (not (eobp)))
  4707. (funcall fun)))))
  4708. (defun org-map-region (fun beg end)
  4709. "Call FUN for every heading between BEG and END."
  4710. (let ((org-ignore-region t))
  4711. (save-excursion
  4712. (setq end (copy-marker end))
  4713. (goto-char beg)
  4714. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4715. (< (point) end))
  4716. (funcall fun))
  4717. (while (and (progn
  4718. (outline-next-heading)
  4719. (< (point) end))
  4720. (not (eobp)))
  4721. (funcall fun)))))
  4722. (defun org-fixup-indentation (diff)
  4723. "Change the indentation in the current entry by DIFF
  4724. However, if any line in the current entry has no indentation, or if it
  4725. would end up with no indentation after the change, nothing at all is done."
  4726. (save-excursion
  4727. (let ((end (save-excursion (outline-next-heading)
  4728. (point-marker)))
  4729. (prohibit (if (> diff 0)
  4730. "^\\S-"
  4731. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4732. col)
  4733. (unless (save-excursion (end-of-line 1)
  4734. (re-search-forward prohibit end t))
  4735. (while (and (< (point) end)
  4736. (re-search-forward "^[ \t]+" end t))
  4737. (goto-char (match-end 0))
  4738. (setq col (current-column))
  4739. (if (< diff 0) (replace-match ""))
  4740. (org-indent-to-column (+ diff col))))
  4741. (move-marker end nil))))
  4742. (defun org-convert-to-odd-levels ()
  4743. "Convert an org-mode file with all levels allowed to one with odd levels.
  4744. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4745. level 5 etc."
  4746. (interactive)
  4747. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4748. (let ((org-odd-levels-only nil) n)
  4749. (save-excursion
  4750. (goto-char (point-min))
  4751. (while (re-search-forward "^\\*\\*+ " nil t)
  4752. (setq n (- (length (match-string 0)) 2))
  4753. (while (>= (setq n (1- n)) 0)
  4754. (org-demote))
  4755. (end-of-line 1))))))
  4756. (defun org-convert-to-oddeven-levels ()
  4757. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4758. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4759. section with an even level, conversion would destroy the structure of the file. An error
  4760. is signaled in this case."
  4761. (interactive)
  4762. (goto-char (point-min))
  4763. ;; First check if there are no even levels
  4764. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4765. (org-show-context t)
  4766. (error "Not all levels are odd in this file. Conversion not possible."))
  4767. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4768. (let ((org-odd-levels-only nil) n)
  4769. (save-excursion
  4770. (goto-char (point-min))
  4771. (while (re-search-forward "^\\*\\*+ " nil t)
  4772. (setq n (/ (1- (length (match-string 0))) 2))
  4773. (while (>= (setq n (1- n)) 0)
  4774. (org-promote))
  4775. (end-of-line 1))))))
  4776. (defun org-tr-level (n)
  4777. "Make N odd if required."
  4778. (if org-odd-levels-only (1+ (/ n 2)) n))
  4779. ;;; Vertical tree motion, cutting and pasting of subtrees
  4780. (defun org-move-subtree-up (&optional arg)
  4781. "Move the current subtree up past ARG headlines of the same level."
  4782. (interactive "p")
  4783. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4784. (defun org-move-subtree-down (&optional arg)
  4785. "Move the current subtree down past ARG headlines of the same level."
  4786. (interactive "p")
  4787. (setq arg (prefix-numeric-value arg))
  4788. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4789. 'outline-get-last-sibling))
  4790. (ins-point (make-marker))
  4791. (cnt (abs arg))
  4792. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4793. ;; Select the tree
  4794. (org-back-to-heading)
  4795. (setq beg0 (point))
  4796. (save-excursion
  4797. (setq ne-beg (org-back-over-empty-lines))
  4798. (setq beg (point)))
  4799. (save-match-data
  4800. (save-excursion (outline-end-of-heading)
  4801. (setq folded (org-invisible-p)))
  4802. (outline-end-of-subtree))
  4803. (outline-next-heading)
  4804. (setq ne-end (org-back-over-empty-lines))
  4805. (setq end (point))
  4806. (goto-char beg0)
  4807. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4808. ;; include less whitespace
  4809. (save-excursion
  4810. (goto-char beg)
  4811. (forward-line (- ne-beg ne-end))
  4812. (setq beg (point))))
  4813. ;; Find insertion point, with error handling
  4814. (while (> cnt 0)
  4815. (or (and (funcall movfunc) (looking-at outline-regexp))
  4816. (progn (goto-char beg0)
  4817. (error "Cannot move past superior level or buffer limit")))
  4818. (setq cnt (1- cnt)))
  4819. (if (> arg 0)
  4820. ;; Moving forward - still need to move over subtree
  4821. (progn (org-end-of-subtree t t)
  4822. (save-excursion
  4823. (org-back-over-empty-lines)
  4824. (or (bolp) (newline)))))
  4825. (setq ne-ins (org-back-over-empty-lines))
  4826. (move-marker ins-point (point))
  4827. (setq txt (buffer-substring beg end))
  4828. (org-save-markers-in-region beg end)
  4829. (delete-region beg end)
  4830. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4831. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4832. (let ((bbb (point)))
  4833. (insert-before-markers txt)
  4834. (org-reinstall-markers-in-region bbb)
  4835. (move-marker ins-point bbb))
  4836. (or (bolp) (insert "\n"))
  4837. (setq ins-end (point))
  4838. (goto-char ins-point)
  4839. (org-skip-whitespace)
  4840. (when (and (< arg 0)
  4841. (org-first-sibling-p)
  4842. (> ne-ins ne-beg))
  4843. ;; Move whitespace back to beginning
  4844. (save-excursion
  4845. (goto-char ins-end)
  4846. (let ((kill-whole-line t))
  4847. (kill-line (- ne-ins ne-beg)) (point)))
  4848. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4849. (move-marker ins-point nil)
  4850. (org-compact-display-after-subtree-move)
  4851. (org-show-empty-lines-in-parent)
  4852. (unless folded
  4853. (org-show-entry)
  4854. (show-children)
  4855. (org-cycle-hide-drawers 'children))))
  4856. (defvar org-subtree-clip ""
  4857. "Clipboard for cut and paste of subtrees.
  4858. This is actually only a copy of the kill, because we use the normal kill
  4859. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4860. (defvar org-subtree-clip-folded nil
  4861. "Was the last copied subtree folded?
  4862. This is used to fold the tree back after pasting.")
  4863. (defun org-cut-subtree (&optional n)
  4864. "Cut the current subtree into the clipboard.
  4865. With prefix arg N, cut this many sequential subtrees.
  4866. This is a short-hand for marking the subtree and then cutting it."
  4867. (interactive "p")
  4868. (org-copy-subtree n 'cut))
  4869. (defun org-copy-subtree (&optional n cut force-store-markers)
  4870. "Cut the current subtree into the clipboard.
  4871. With prefix arg N, cut this many sequential subtrees.
  4872. This is a short-hand for marking the subtree and then copying it.
  4873. If CUT is non-nil, actually cut the subtree.
  4874. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4875. of some markers in the region, even if CUT is non-nil. This is
  4876. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4877. (interactive "p")
  4878. (let (beg end folded (beg0 (point)))
  4879. (if (interactive-p)
  4880. (org-back-to-heading nil) ; take what looks like a subtree
  4881. (org-back-to-heading t)) ; take what is really there
  4882. (org-back-over-empty-lines)
  4883. (setq beg (point))
  4884. (skip-chars-forward " \t\r\n")
  4885. (save-match-data
  4886. (save-excursion (outline-end-of-heading)
  4887. (setq folded (org-invisible-p)))
  4888. (condition-case nil
  4889. (outline-forward-same-level (1- n))
  4890. (error nil))
  4891. (org-end-of-subtree t t))
  4892. (org-back-over-empty-lines)
  4893. (setq end (point))
  4894. (goto-char beg0)
  4895. (when (> end beg)
  4896. (setq org-subtree-clip-folded folded)
  4897. (when (or cut force-store-markers)
  4898. (org-save-markers-in-region beg end))
  4899. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4900. (setq org-subtree-clip (current-kill 0))
  4901. (message "%s: Subtree(s) with %d characters"
  4902. (if cut "Cut" "Copied")
  4903. (length org-subtree-clip)))))
  4904. (defun org-paste-subtree (&optional level tree for-yank)
  4905. "Paste the clipboard as a subtree, with modification of headline level.
  4906. The entire subtree is promoted or demoted in order to match a new headline
  4907. level.
  4908. If the cursor is at the beginning of a headline, the same level as
  4909. that headline is used to paste the tree
  4910. If not, the new level is derived from the *visible* headings
  4911. before and after the insertion point, and taken to be the inferior headline
  4912. level of the two. So if the previous visible heading is level 3 and the
  4913. next is level 4 (or vice versa), level 4 will be used for insertion.
  4914. This makes sure that the subtree remains an independent subtree and does
  4915. not swallow low level entries.
  4916. You can also force a different level, either by using a numeric prefix
  4917. argument, or by inserting the heading marker by hand. For example, if the
  4918. cursor is after \"*****\", then the tree will be shifted to level 5.
  4919. If optional TREE is given, use this text instead of the kill ring.
  4920. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4921. move back over whitespace before inserting, and move point to the end of
  4922. the inserted text when done."
  4923. (interactive "P")
  4924. (unless (org-kill-is-subtree-p tree)
  4925. (error "%s"
  4926. (substitute-command-keys
  4927. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4928. (let* ((visp (not (org-invisible-p)))
  4929. (txt (or tree (and kill-ring (current-kill 0))))
  4930. (^re (concat "^\\(" outline-regexp "\\)"))
  4931. (re (concat "\\(" outline-regexp "\\)"))
  4932. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4933. (old-level (if (string-match ^re txt)
  4934. (- (match-end 0) (match-beginning 0) 1)
  4935. -1))
  4936. (force-level (cond (level (prefix-numeric-value level))
  4937. ((and (looking-at "[ \t]*$")
  4938. (string-match
  4939. ^re_ (buffer-substring
  4940. (point-at-bol) (point))))
  4941. (- (match-end 1) (match-beginning 1)))
  4942. ((and (bolp)
  4943. (looking-at org-outline-regexp))
  4944. (- (match-end 0) (point) 1))
  4945. (t nil)))
  4946. (previous-level (save-excursion
  4947. (condition-case nil
  4948. (progn
  4949. (outline-previous-visible-heading 1)
  4950. (if (looking-at re)
  4951. (- (match-end 0) (match-beginning 0) 1)
  4952. 1))
  4953. (error 1))))
  4954. (next-level (save-excursion
  4955. (condition-case nil
  4956. (progn
  4957. (or (looking-at outline-regexp)
  4958. (outline-next-visible-heading 1))
  4959. (if (looking-at re)
  4960. (- (match-end 0) (match-beginning 0) 1)
  4961. 1))
  4962. (error 1))))
  4963. (new-level (or force-level (max previous-level next-level)))
  4964. (shift (if (or (= old-level -1)
  4965. (= new-level -1)
  4966. (= old-level new-level))
  4967. 0
  4968. (- new-level old-level)))
  4969. (delta (if (> shift 0) -1 1))
  4970. (func (if (> shift 0) 'org-demote 'org-promote))
  4971. (org-odd-levels-only nil)
  4972. beg end newend)
  4973. ;; Remove the forced level indicator
  4974. (if force-level
  4975. (delete-region (point-at-bol) (point)))
  4976. ;; Paste
  4977. (beginning-of-line 1)
  4978. (unless for-yank (org-back-over-empty-lines))
  4979. (setq beg (point))
  4980. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4981. (insert-before-markers txt)
  4982. (unless (string-match "\n\\'" txt) (insert "\n"))
  4983. (setq newend (point))
  4984. (org-reinstall-markers-in-region beg)
  4985. (setq end (point))
  4986. (goto-char beg)
  4987. (skip-chars-forward " \t\n\r")
  4988. (setq beg (point))
  4989. (if (and (org-invisible-p) visp)
  4990. (save-excursion (outline-show-heading)))
  4991. ;; Shift if necessary
  4992. (unless (= shift 0)
  4993. (save-restriction
  4994. (narrow-to-region beg end)
  4995. (while (not (= shift 0))
  4996. (org-map-region func (point-min) (point-max))
  4997. (setq shift (+ delta shift)))
  4998. (goto-char (point-min))
  4999. (setq newend (point-max))))
  5000. (when (or (interactive-p) for-yank)
  5001. (message "Clipboard pasted as level %d subtree" new-level))
  5002. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5003. kill-ring
  5004. (eq org-subtree-clip (current-kill 0))
  5005. org-subtree-clip-folded)
  5006. ;; The tree was folded before it was killed/copied
  5007. (hide-subtree))
  5008. (and for-yank (goto-char newend))))
  5009. (defun org-kill-is-subtree-p (&optional txt)
  5010. "Check if the current kill is an outline subtree, or a set of trees.
  5011. Returns nil if kill does not start with a headline, or if the first
  5012. headline level is not the largest headline level in the tree.
  5013. So this will actually accept several entries of equal levels as well,
  5014. which is OK for `org-paste-subtree'.
  5015. If optional TXT is given, check this string instead of the current kill."
  5016. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5017. (start-level (and kill
  5018. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5019. org-outline-regexp "\\)")
  5020. kill)
  5021. (- (match-end 2) (match-beginning 2) 1)))
  5022. (re (concat "^" org-outline-regexp))
  5023. (start (1+ (or (match-beginning 2) -1))))
  5024. (if (not start-level)
  5025. (progn
  5026. nil) ;; does not even start with a heading
  5027. (catch 'exit
  5028. (while (setq start (string-match re kill (1+ start)))
  5029. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5030. (throw 'exit nil)))
  5031. t))))
  5032. (defvar org-markers-to-move nil
  5033. "Markers that should be moved with a cut-and-paste operation.
  5034. Those markers are stored together with their positions relative to
  5035. the start of the region.")
  5036. (defun org-save-markers-in-region (beg end)
  5037. "Check markers in region.
  5038. If these markers are between BEG and END, record their position relative
  5039. to BEG, so that after moving the block of text, we can put the markers back
  5040. into place.
  5041. This function gets called just before an entry or tree gets cut from the
  5042. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5043. called immediately, to move the markers with the entries."
  5044. (setq org-markers-to-move nil)
  5045. (when (featurep 'org-clock)
  5046. (org-clock-save-markers-for-cut-and-paste beg end))
  5047. (when (featurep 'org-agenda)
  5048. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5049. (defun org-check-and-save-marker (marker beg end)
  5050. "Check if MARKER is between BEG and END.
  5051. If yes, remember the marker and the distance to BEG."
  5052. (when (and (marker-buffer marker)
  5053. (equal (marker-buffer marker) (current-buffer)))
  5054. (if (and (>= marker beg) (< marker end))
  5055. (push (cons marker (- marker beg)) org-markers-to-move))))
  5056. (defun org-reinstall-markers-in-region (beg)
  5057. "Move all remembered markers to their position relative to BEG."
  5058. (mapc (lambda (x)
  5059. (move-marker (car x) (+ beg (cdr x))))
  5060. org-markers-to-move)
  5061. (setq org-markers-to-move nil))
  5062. (defun org-narrow-to-subtree ()
  5063. "Narrow buffer to the current subtree."
  5064. (interactive)
  5065. (save-excursion
  5066. (save-match-data
  5067. (narrow-to-region
  5068. (progn (org-back-to-heading) (point))
  5069. (progn (org-end-of-subtree t) (point))))))
  5070. ;;; Outline Sorting
  5071. (defun org-sort (with-case)
  5072. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5073. Optional argument WITH-CASE means sort case-sensitively."
  5074. (interactive "P")
  5075. (if (org-at-table-p)
  5076. (org-call-with-arg 'org-table-sort-lines with-case)
  5077. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5078. (defun org-sort-remove-invisible (s)
  5079. (remove-text-properties 0 (length s) org-rm-props s)
  5080. (while (string-match org-bracket-link-regexp s)
  5081. (setq s (replace-match (if (match-end 2)
  5082. (match-string 3 s)
  5083. (match-string 1 s)) t t s)))
  5084. s)
  5085. (defvar org-priority-regexp) ; defined later in the file
  5086. (defun org-sort-entries-or-items
  5087. (&optional with-case sorting-type getkey-func compare-func property)
  5088. "Sort entries on a certain level of an outline tree.
  5089. If there is an active region, the entries in the region are sorted.
  5090. Else, if the cursor is before the first entry, sort the top-level items.
  5091. Else, the children of the entry at point are sorted.
  5092. Sorting can be alphabetically, numerically, and by date/time as given by
  5093. the first time stamp in the entry. The command prompts for the sorting
  5094. type unless it has been given to the function through the SORTING-TYPE
  5095. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5096. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5097. called with point at the beginning of the record. It must return either
  5098. a string or a number that should serve as the sorting key for that record.
  5099. Comparing entries ignores case by default. However, with an optional argument
  5100. WITH-CASE, the sorting considers case as well."
  5101. (interactive "P")
  5102. (let ((case-func (if with-case 'identity 'downcase))
  5103. start beg end stars re re2
  5104. txt what tmp plain-list-p)
  5105. ;; Find beginning and end of region to sort
  5106. (cond
  5107. ((org-region-active-p)
  5108. ;; we will sort the region
  5109. (setq end (region-end)
  5110. what "region")
  5111. (goto-char (region-beginning))
  5112. (if (not (org-on-heading-p)) (outline-next-heading))
  5113. (setq start (point)))
  5114. ((org-at-item-p)
  5115. ;; we will sort this plain list
  5116. (org-beginning-of-item-list) (setq start (point))
  5117. (org-end-of-item-list) (setq end (point))
  5118. (goto-char start)
  5119. (setq plain-list-p t
  5120. what "plain list"))
  5121. ((or (org-on-heading-p)
  5122. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5123. ;; we will sort the children of the current headline
  5124. (org-back-to-heading)
  5125. (setq start (point)
  5126. end (progn (org-end-of-subtree t t)
  5127. (org-back-over-empty-lines)
  5128. (point))
  5129. what "children")
  5130. (goto-char start)
  5131. (show-subtree)
  5132. (outline-next-heading))
  5133. (t
  5134. ;; we will sort the top-level entries in this file
  5135. (goto-char (point-min))
  5136. (or (org-on-heading-p) (outline-next-heading))
  5137. (setq start (point) end (point-max) what "top-level")
  5138. (goto-char start)
  5139. (show-all)))
  5140. (setq beg (point))
  5141. (if (>= beg end) (error "Nothing to sort"))
  5142. (unless plain-list-p
  5143. (looking-at "\\(\\*+\\)")
  5144. (setq stars (match-string 1)
  5145. re (concat "^" (regexp-quote stars) " +")
  5146. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5147. txt (buffer-substring beg end))
  5148. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5149. (if (and (not (equal stars "*")) (string-match re2 txt))
  5150. (error "Region to sort contains a level above the first entry")))
  5151. (unless sorting-type
  5152. (message
  5153. (if plain-list-p
  5154. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5155. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5156. what)
  5157. (setq sorting-type (read-char-exclusive))
  5158. (and (= (downcase sorting-type) ?f)
  5159. (setq getkey-func
  5160. (org-ido-completing-read "Sort using function: "
  5161. obarray 'fboundp t nil nil))
  5162. (setq getkey-func (intern getkey-func)))
  5163. (and (= (downcase sorting-type) ?r)
  5164. (setq property
  5165. (org-ido-completing-read "Property: "
  5166. (mapcar 'list (org-buffer-property-keys t))
  5167. nil t))))
  5168. (message "Sorting entries...")
  5169. (save-restriction
  5170. (narrow-to-region start end)
  5171. (let ((dcst (downcase sorting-type))
  5172. (now (current-time)))
  5173. (sort-subr
  5174. (/= dcst sorting-type)
  5175. ;; This function moves to the beginning character of the "record" to
  5176. ;; be sorted.
  5177. (if plain-list-p
  5178. (lambda nil
  5179. (if (org-at-item-p) t (goto-char (point-max))))
  5180. (lambda nil
  5181. (if (re-search-forward re nil t)
  5182. (goto-char (match-beginning 0))
  5183. (goto-char (point-max)))))
  5184. ;; This function moves to the last character of the "record" being
  5185. ;; sorted.
  5186. (if plain-list-p
  5187. 'org-end-of-item
  5188. (lambda nil
  5189. (save-match-data
  5190. (condition-case nil
  5191. (outline-forward-same-level 1)
  5192. (error
  5193. (goto-char (point-max)))))))
  5194. ;; This function returns the value that gets sorted against.
  5195. (if plain-list-p
  5196. (lambda nil
  5197. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5198. (cond
  5199. ((= dcst ?n)
  5200. (string-to-number (buffer-substring (match-end 0)
  5201. (point-at-eol))))
  5202. ((= dcst ?a)
  5203. (buffer-substring (match-end 0) (point-at-eol)))
  5204. ((= dcst ?t)
  5205. (if (re-search-forward org-ts-regexp
  5206. (point-at-eol) t)
  5207. (org-time-string-to-time (match-string 0))
  5208. now))
  5209. ((= dcst ?f)
  5210. (if getkey-func
  5211. (progn
  5212. (setq tmp (funcall getkey-func))
  5213. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5214. tmp)
  5215. (error "Invalid key function `%s'" getkey-func)))
  5216. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5217. (lambda nil
  5218. (cond
  5219. ((= dcst ?n)
  5220. (if (looking-at org-complex-heading-regexp)
  5221. (string-to-number (match-string 4))
  5222. nil))
  5223. ((= dcst ?a)
  5224. (if (looking-at org-complex-heading-regexp)
  5225. (funcall case-func (match-string 4))
  5226. nil))
  5227. ((= dcst ?t)
  5228. (if (re-search-forward org-ts-regexp
  5229. (save-excursion
  5230. (forward-line 2)
  5231. (point)) t)
  5232. (org-time-string-to-time (match-string 0))
  5233. now))
  5234. ((= dcst ?p)
  5235. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5236. (string-to-char (match-string 2))
  5237. org-default-priority))
  5238. ((= dcst ?r)
  5239. (or (org-entry-get nil property) ""))
  5240. ((= dcst ?o)
  5241. (if (looking-at org-complex-heading-regexp)
  5242. (- 9999 (length (member (match-string 2)
  5243. org-todo-keywords-1)))))
  5244. ((= dcst ?f)
  5245. (if getkey-func
  5246. (progn
  5247. (setq tmp (funcall getkey-func))
  5248. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5249. tmp)
  5250. (error "Invalid key function `%s'" getkey-func)))
  5251. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5252. nil
  5253. (cond
  5254. ((= dcst ?a) 'string<)
  5255. ((= dcst ?t) 'time-less-p)
  5256. ((= dcst ?f) compare-func)
  5257. (t nil)))))
  5258. (message "Sorting entries...done")))
  5259. (defun org-do-sort (table what &optional with-case sorting-type)
  5260. "Sort TABLE of WHAT according to SORTING-TYPE.
  5261. The user will be prompted for the SORTING-TYPE if the call to this
  5262. function does not specify it. WHAT is only for the prompt, to indicate
  5263. what is being sorted. The sorting key will be extracted from
  5264. the car of the elements of the table.
  5265. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5266. (unless sorting-type
  5267. (message
  5268. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5269. what)
  5270. (setq sorting-type (read-char-exclusive)))
  5271. (let ((dcst (downcase sorting-type))
  5272. extractfun comparefun)
  5273. ;; Define the appropriate functions
  5274. (cond
  5275. ((= dcst ?n)
  5276. (setq extractfun 'string-to-number
  5277. comparefun (if (= dcst sorting-type) '< '>)))
  5278. ((= dcst ?a)
  5279. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5280. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5281. comparefun (if (= dcst sorting-type)
  5282. 'string<
  5283. (lambda (a b) (and (not (string< a b))
  5284. (not (string= a b)))))))
  5285. ((= dcst ?t)
  5286. (setq extractfun
  5287. (lambda (x)
  5288. (if (string-match org-ts-regexp x)
  5289. (time-to-seconds
  5290. (org-time-string-to-time (match-string 0 x)))
  5291. 0))
  5292. comparefun (if (= dcst sorting-type) '< '>)))
  5293. (t (error "Invalid sorting type `%c'" sorting-type)))
  5294. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5295. table)
  5296. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5297. ;;; Editing source examples
  5298. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5299. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5300. (defvar org-edit-src-force-single-line nil)
  5301. (defvar org-edit-src-from-org-mode nil)
  5302. (defvar org-edit-src-picture nil)
  5303. (define-minor-mode org-exit-edit-mode
  5304. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5305. (defun org-edit-src-code ()
  5306. "Edit the source code example at point.
  5307. An indirect buffer is created, and that buffer is then narrowed to the
  5308. example at point and switched to the correct language mode. When done,
  5309. exit by killing the buffer with \\[org-edit-src-exit]."
  5310. (interactive)
  5311. (let ((line (org-current-line))
  5312. (case-fold-search t)
  5313. (msg (substitute-command-keys
  5314. "Edit, then exit with C-c ' (C-c and single quote)"))
  5315. (info (org-edit-src-find-region-and-lang))
  5316. (org-mode-p (eq major-mode 'org-mode))
  5317. beg end lang lang-f single lfmt)
  5318. (if (not info)
  5319. nil
  5320. (setq beg (nth 0 info)
  5321. end (nth 1 info)
  5322. lang (nth 2 info)
  5323. single (nth 3 info)
  5324. lfmt (nth 4 info)
  5325. lang-f (intern (concat lang "-mode")))
  5326. (unless (functionp lang-f)
  5327. (error "No such language mode: %s" lang-f))
  5328. (goto-line line)
  5329. (if (get-buffer "*Org Edit Src Example*")
  5330. (kill-buffer "*Org Edit Src Example*"))
  5331. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5332. "*Org Edit Src Example*"))
  5333. (narrow-to-region beg end)
  5334. (remove-text-properties beg end '(display nil invisible nil
  5335. intangible nil))
  5336. (let ((org-inhibit-startup t))
  5337. (funcall lang-f))
  5338. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5339. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5340. (when lfmt
  5341. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5342. (when org-mode-p
  5343. (goto-char (point-min))
  5344. (while (re-search-forward "^," nil t)
  5345. (replace-match "")))
  5346. (goto-line line)
  5347. (org-exit-edit-mode)
  5348. (org-set-local 'header-line-format msg)
  5349. (message "%s" msg)
  5350. t)))
  5351. (defun org-edit-fixed-width-region ()
  5352. "Edit the fixed-width ascii drawing at point.
  5353. This must be a region where each line starts with a colon followed by
  5354. a space character.
  5355. An indirect buffer is created, and that buffer is then narrowed to the
  5356. example at point and switched to artist-mode. When done,
  5357. exit by killing the buffer with \\[org-edit-src-exit]."
  5358. (interactive)
  5359. (let ((line (org-current-line))
  5360. (case-fold-search t)
  5361. (msg (substitute-command-keys
  5362. "Edit, then exit with C-c ' (C-c and single quote)"))
  5363. (org-mode-p (eq major-mode 'org-mode))
  5364. beg end)
  5365. (beginning-of-line 1)
  5366. (if (looking-at "[ \t]*[^:\n \t]")
  5367. nil
  5368. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5369. (setq beg (point) end beg)
  5370. (save-excursion
  5371. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5372. (setq beg (point-at-bol 2))
  5373. (setq beg (point))))
  5374. (save-excursion
  5375. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5376. (setq end (1- (match-beginning 0)))
  5377. (setq end (point))))
  5378. (goto-line line))
  5379. (if (get-buffer "*Org Edit Picture*")
  5380. (kill-buffer "*Org Edit Picture*"))
  5381. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5382. "*Org Edit Picture*"))
  5383. (narrow-to-region beg end)
  5384. (remove-text-properties beg end '(display nil invisible nil
  5385. intangible nil))
  5386. (when (fboundp 'font-lock-unfontify-region)
  5387. (font-lock-unfontify-region (point-min) (point-max)))
  5388. (cond
  5389. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5390. (fundamental-mode)
  5391. (artist-mode 1))
  5392. (t (funcall org-edit-fixed-width-region-mode)))
  5393. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5394. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5395. (set (make-local-variable 'org-edit-src-picture) t)
  5396. (goto-char (point-min))
  5397. (while (re-search-forward "^[ \t]*: ?" nil t)
  5398. (replace-match ""))
  5399. (goto-line line)
  5400. (org-exit-edit-mode)
  5401. (org-set-local 'header-line-format msg)
  5402. (message "%s" msg)
  5403. t)))
  5404. (defun org-edit-src-find-region-and-lang ()
  5405. "Find the region and language for a local edit.
  5406. Return a list with beginning and end of the region, a string representing
  5407. the language, a switch telling of the content should be in a single line."
  5408. (let ((re-list
  5409. (append
  5410. org-edit-src-region-extra
  5411. '(
  5412. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5413. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5414. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5415. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5416. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5417. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5418. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5419. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5420. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5421. ("^#\\+html:" "\n" "html" single-line)
  5422. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5423. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5424. ("^#\\+latex:" "\n" "latex" single-line)
  5425. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5426. ("^#\\+ascii:" "\n" "ascii" single-line)
  5427. )))
  5428. (pos (point))
  5429. re1 re2 single beg end lang lfmt match-re1)
  5430. (catch 'exit
  5431. (while (setq entry (pop re-list))
  5432. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5433. single (nth 3 entry))
  5434. (save-excursion
  5435. (if (or (looking-at re1)
  5436. (re-search-backward re1 nil t))
  5437. (progn
  5438. (setq match-re1 (match-string 0))
  5439. (setq beg (match-end 0)
  5440. lang (org-edit-src-get-lang lang)
  5441. lfmt (org-edit-src-get-label-format match-re1))
  5442. (if (and (re-search-forward re2 nil t)
  5443. (>= (match-end 0) pos))
  5444. (throw 'exit (list beg (match-beginning 0)
  5445. lang single lfmt))))
  5446. (if (or (looking-at re2)
  5447. (re-search-forward re2 nil t))
  5448. (progn
  5449. (setq end (match-beginning 0))
  5450. (if (and (re-search-backward re1 nil t)
  5451. (<= (match-beginning 0) pos))
  5452. (progn
  5453. (setq lfmt (org-edit-src-get-label-format
  5454. (match-string 0)))
  5455. (throw 'exit
  5456. (list (match-end 0) end
  5457. (org-edit-src-get-lang lang)
  5458. single lfmt))))))))))))
  5459. (defun org-edit-src-get-lang (lang)
  5460. "Extract the src language."
  5461. (let ((m (match-string 0)))
  5462. (cond
  5463. ((stringp lang) lang)
  5464. ((integerp lang) (match-string lang))
  5465. ((and (eq lang 'lang)
  5466. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5467. (match-string 1 m))
  5468. ((and (eq lang 'style)
  5469. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5470. (match-string 1 m))
  5471. (t "fundamental"))))
  5472. (defun org-edit-src-get-label-format (s)
  5473. "Extract the label format."
  5474. (save-match-data
  5475. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5476. (match-string 1 s))))
  5477. (defun org-edit-src-exit ()
  5478. "Exit special edit and protect problematic lines."
  5479. (interactive)
  5480. (unless (buffer-base-buffer (current-buffer))
  5481. (error "This is not an indirect buffer, something is wrong..."))
  5482. (unless (> (point-min) 1)
  5483. (error "This buffer is not narrowed, something is wrong..."))
  5484. (goto-char (point-min))
  5485. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5486. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5487. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5488. (goto-char (point-min))
  5489. (while (re-search-forward "\n" nil t)
  5490. (replace-match " "))
  5491. (goto-char (point-min))
  5492. (if (looking-at "\\s-*") (replace-match " "))
  5493. (if (re-search-forward "\\s-+\\'" nil t)
  5494. (replace-match "")))
  5495. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5496. (goto-char (point-min))
  5497. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5498. (replace-match ",\\1"))
  5499. (when font-lock-mode
  5500. (font-lock-unfontify-region (point-min) (point-max)))
  5501. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5502. (when (org-bound-and-true-p org-edit-src-picture)
  5503. (untabify (point-min) (point-max))
  5504. (goto-char (point-min))
  5505. (while (re-search-forward "^" nil t)
  5506. (replace-match ": "))
  5507. (when font-lock-mode
  5508. (font-lock-unfontify-region (point-min) (point-max)))
  5509. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5510. (kill-buffer (current-buffer))
  5511. (and (org-mode-p) (org-restart-font-lock)))
  5512. ;;; The orgstruct minor mode
  5513. ;; Define a minor mode which can be used in other modes in order to
  5514. ;; integrate the org-mode structure editing commands.
  5515. ;; This is really a hack, because the org-mode structure commands use
  5516. ;; keys which normally belong to the major mode. Here is how it
  5517. ;; works: The minor mode defines all the keys necessary to operate the
  5518. ;; structure commands, but wraps the commands into a function which
  5519. ;; tests if the cursor is currently at a headline or a plain list
  5520. ;; item. If that is the case, the structure command is used,
  5521. ;; temporarily setting many Org-mode variables like regular
  5522. ;; expressions for filling etc. However, when any of those keys is
  5523. ;; used at a different location, function uses `key-binding' to look
  5524. ;; up if the key has an associated command in another currently active
  5525. ;; keymap (minor modes, major mode, global), and executes that
  5526. ;; command. There might be problems if any of the keys is otherwise
  5527. ;; used as a prefix key.
  5528. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5529. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5530. ;; addresses this by checking explicitly for both bindings.
  5531. (defvar orgstruct-mode-map (make-sparse-keymap)
  5532. "Keymap for the minor `orgstruct-mode'.")
  5533. (defvar org-local-vars nil
  5534. "List of local variables, for use by `orgstruct-mode'")
  5535. ;;;###autoload
  5536. (define-minor-mode orgstruct-mode
  5537. "Toggle the minor more `orgstruct-mode'.
  5538. This mode is for using Org-mode structure commands in other modes.
  5539. The following key behave as if Org-mode was active, if the cursor
  5540. is on a headline, or on a plain list item (both in the definition
  5541. of Org-mode).
  5542. M-up Move entry/item up
  5543. M-down Move entry/item down
  5544. M-left Promote
  5545. M-right Demote
  5546. M-S-up Move entry/item up
  5547. M-S-down Move entry/item down
  5548. M-S-left Promote subtree
  5549. M-S-right Demote subtree
  5550. M-q Fill paragraph and items like in Org-mode
  5551. C-c ^ Sort entries
  5552. C-c - Cycle list bullet
  5553. TAB Cycle item visibility
  5554. M-RET Insert new heading/item
  5555. S-M-RET Insert new TODO heading / Checkbox item
  5556. C-c C-c Set tags / toggle checkbox"
  5557. nil " OrgStruct" nil
  5558. (org-load-modules-maybe)
  5559. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5560. ;;;###autoload
  5561. (defun turn-on-orgstruct ()
  5562. "Unconditionally turn on `orgstruct-mode'."
  5563. (orgstruct-mode 1))
  5564. ;;;###autoload
  5565. (defun turn-on-orgstruct++ ()
  5566. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5567. In addition to setting orgstruct-mode, this also exports all indentation and
  5568. autofilling variables from org-mode into the buffer. Note that turning
  5569. off orgstruct-mode will *not* remove these additional settings."
  5570. (orgstruct-mode 1)
  5571. (let (var val)
  5572. (mapc
  5573. (lambda (x)
  5574. (when (string-match
  5575. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5576. (symbol-name (car x)))
  5577. (setq var (car x) val (nth 1 x))
  5578. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5579. org-local-vars)))
  5580. (defun orgstruct-error ()
  5581. "Error when there is no default binding for a structure key."
  5582. (interactive)
  5583. (error "This key has no function outside structure elements"))
  5584. (defun orgstruct-setup ()
  5585. "Setup orgstruct keymaps."
  5586. (let ((nfunc 0)
  5587. (bindings
  5588. (list
  5589. '([(meta up)] org-metaup)
  5590. '([(meta down)] org-metadown)
  5591. '([(meta left)] org-metaleft)
  5592. '([(meta right)] org-metaright)
  5593. '([(meta shift up)] org-shiftmetaup)
  5594. '([(meta shift down)] org-shiftmetadown)
  5595. '([(meta shift left)] org-shiftmetaleft)
  5596. '([(meta shift right)] org-shiftmetaright)
  5597. '([(shift up)] org-shiftup)
  5598. '([(shift down)] org-shiftdown)
  5599. '([(shift left)] org-shiftleft)
  5600. '([(shift right)] org-shiftright)
  5601. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5602. '("\M-q" fill-paragraph)
  5603. '("\C-c^" org-sort)
  5604. '("\C-c-" org-cycle-list-bullet)))
  5605. elt key fun cmd)
  5606. (while (setq elt (pop bindings))
  5607. (setq nfunc (1+ nfunc))
  5608. (setq key (org-key (car elt))
  5609. fun (nth 1 elt)
  5610. cmd (orgstruct-make-binding fun nfunc key))
  5611. (org-defkey orgstruct-mode-map key cmd))
  5612. ;; Special treatment needed for TAB and RET
  5613. (org-defkey orgstruct-mode-map [(tab)]
  5614. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5615. (org-defkey orgstruct-mode-map "\C-i"
  5616. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5617. (org-defkey orgstruct-mode-map "\M-\C-m"
  5618. (orgstruct-make-binding 'org-insert-heading 105
  5619. "\M-\C-m" [(meta return)]))
  5620. (org-defkey orgstruct-mode-map [(meta return)]
  5621. (orgstruct-make-binding 'org-insert-heading 106
  5622. [(meta return)] "\M-\C-m"))
  5623. (org-defkey orgstruct-mode-map [(shift meta return)]
  5624. (orgstruct-make-binding 'org-insert-todo-heading 107
  5625. [(meta return)] "\M-\C-m"))
  5626. (unless org-local-vars
  5627. (setq org-local-vars (org-get-local-variables)))
  5628. t))
  5629. (defun orgstruct-make-binding (fun n &rest keys)
  5630. "Create a function for binding in the structure minor mode.
  5631. FUN is the command to call inside a table. N is used to create a unique
  5632. command name. KEYS are keys that should be checked in for a command
  5633. to execute outside of tables."
  5634. (eval
  5635. (list 'defun
  5636. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5637. '(arg)
  5638. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5639. "Outside of structure, run the binding of `"
  5640. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5641. "'.")
  5642. '(interactive "p")
  5643. (list 'if
  5644. '(org-context-p 'headline 'item)
  5645. (list 'org-run-like-in-org-mode (list 'quote fun))
  5646. (list 'let '(orgstruct-mode)
  5647. (list 'call-interactively
  5648. (append '(or)
  5649. (mapcar (lambda (k)
  5650. (list 'key-binding k))
  5651. keys)
  5652. '('orgstruct-error))))))))
  5653. (defun org-context-p (&rest contexts)
  5654. "Check if local context is any of CONTEXTS.
  5655. Possible values in the list of contexts are `table', `headline', and `item'."
  5656. (let ((pos (point)))
  5657. (goto-char (point-at-bol))
  5658. (prog1 (or (and (memq 'table contexts)
  5659. (looking-at "[ \t]*|"))
  5660. (and (memq 'headline contexts)
  5661. ;;????????? (looking-at "\\*+"))
  5662. (looking-at outline-regexp))
  5663. (and (memq 'item contexts)
  5664. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5665. (goto-char pos))))
  5666. (defun org-get-local-variables ()
  5667. "Return a list of all local variables in an org-mode buffer."
  5668. (let (varlist)
  5669. (with-current-buffer (get-buffer-create "*Org tmp*")
  5670. (erase-buffer)
  5671. (org-mode)
  5672. (setq varlist (buffer-local-variables)))
  5673. (kill-buffer "*Org tmp*")
  5674. (delq nil
  5675. (mapcar
  5676. (lambda (x)
  5677. (setq x
  5678. (if (symbolp x)
  5679. (list x)
  5680. (list (car x) (list 'quote (cdr x)))))
  5681. (if (string-match
  5682. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5683. (symbol-name (car x)))
  5684. x nil))
  5685. varlist))))
  5686. ;;;###autoload
  5687. (defun org-run-like-in-org-mode (cmd)
  5688. (org-load-modules-maybe)
  5689. (unless org-local-vars
  5690. (setq org-local-vars (org-get-local-variables)))
  5691. (eval (list 'let org-local-vars
  5692. (list 'call-interactively (list 'quote cmd)))))
  5693. ;;;; Archiving
  5694. (defun org-get-category (&optional pos)
  5695. "Get the category applying to position POS."
  5696. (get-text-property (or pos (point)) 'org-category))
  5697. (defun org-refresh-category-properties ()
  5698. "Refresh category text properties in the buffer."
  5699. (let ((def-cat (cond
  5700. ((null org-category)
  5701. (if buffer-file-name
  5702. (file-name-sans-extension
  5703. (file-name-nondirectory buffer-file-name))
  5704. "???"))
  5705. ((symbolp org-category) (symbol-name org-category))
  5706. (t org-category)))
  5707. beg end cat pos optionp)
  5708. (org-unmodified
  5709. (save-excursion
  5710. (save-restriction
  5711. (widen)
  5712. (goto-char (point-min))
  5713. (put-text-property (point) (point-max) 'org-category def-cat)
  5714. (while (re-search-forward
  5715. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5716. (setq pos (match-end 0)
  5717. optionp (equal (char-after (match-beginning 0)) ?#)
  5718. cat (org-trim (match-string 2)))
  5719. (if optionp
  5720. (setq beg (point-at-bol) end (point-max))
  5721. (org-back-to-heading t)
  5722. (setq beg (point) end (org-end-of-subtree t t)))
  5723. (put-text-property beg end 'org-category cat)
  5724. (goto-char pos)))))))
  5725. ;;;; Link Stuff
  5726. ;;; Link abbreviations
  5727. (defun org-link-expand-abbrev (link)
  5728. "Apply replacements as defined in `org-link-abbrev-alist."
  5729. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5730. (let* ((key (match-string 1 link))
  5731. (as (or (assoc key org-link-abbrev-alist-local)
  5732. (assoc key org-link-abbrev-alist)))
  5733. (tag (and (match-end 2) (match-string 3 link)))
  5734. rpl)
  5735. (if (not as)
  5736. link
  5737. (setq rpl (cdr as))
  5738. (cond
  5739. ((symbolp rpl) (funcall rpl tag))
  5740. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5741. ((string-match "%h" rpl)
  5742. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5743. (t (concat rpl tag)))))
  5744. link))
  5745. ;;; Storing and inserting links
  5746. (defvar org-insert-link-history nil
  5747. "Minibuffer history for links inserted with `org-insert-link'.")
  5748. (defvar org-stored-links nil
  5749. "Contains the links stored with `org-store-link'.")
  5750. (defvar org-store-link-plist nil
  5751. "Plist with info about the most recently link created with `org-store-link'.")
  5752. (defvar org-link-protocols nil
  5753. "Link protocols added to Org-mode using `org-add-link-type'.")
  5754. (defvar org-store-link-functions nil
  5755. "List of functions that are called to create and store a link.
  5756. Each function will be called in turn until one returns a non-nil
  5757. value. Each function should check if it is responsible for creating
  5758. this link (for example by looking at the major mode).
  5759. If not, it must exit and return nil.
  5760. If yes, it should return a non-nil value after a calling
  5761. `org-store-link-props' with a list of properties and values.
  5762. Special properties are:
  5763. :type The link prefix. like \"http\". This must be given.
  5764. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5765. This is obligatory as well.
  5766. :description Optional default description for the second pair
  5767. of brackets in an Org-mode link. The user can still change
  5768. this when inserting this link into an Org-mode buffer.
  5769. In addition to these, any additional properties can be specified
  5770. and then used in remember templates.")
  5771. (defun org-add-link-type (type &optional follow export)
  5772. "Add TYPE to the list of `org-link-types'.
  5773. Re-compute all regular expressions depending on `org-link-types'
  5774. FOLLOW and EXPORT are two functions.
  5775. FOLLOW should take the link path as the single argument and do whatever
  5776. is necessary to follow the link, for example find a file or display
  5777. a mail message.
  5778. EXPORT should format the link path for export to one of the export formats.
  5779. It should be a function accepting three arguments:
  5780. path the path of the link, the text after the prefix (like \"http:\")
  5781. desc the description of the link, if any, nil if there was no description
  5782. format the export format, a symbol like `html' or `latex'.
  5783. The function may use the FORMAT information to return different values
  5784. depending on the format. The return value will be put literally into
  5785. the exported file.
  5786. Org-mode has a built-in default for exporting links. If you are happy with
  5787. this default, there is no need to define an export function for the link
  5788. type. For a simple example of an export function, see `org-bbdb.el'."
  5789. (add-to-list 'org-link-types type t)
  5790. (org-make-link-regexps)
  5791. (if (assoc type org-link-protocols)
  5792. (setcdr (assoc type org-link-protocols) (list follow export))
  5793. (push (list type follow export) org-link-protocols)))
  5794. ;;;###autoload
  5795. (defun org-store-link (arg)
  5796. "\\<org-mode-map>Store an org-link to the current location.
  5797. This link is added to `org-stored-links' and can later be inserted
  5798. into an org-buffer with \\[org-insert-link].
  5799. For some link types, a prefix arg is interpreted:
  5800. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5801. For file links, arg negates `org-context-in-file-links'."
  5802. (interactive "P")
  5803. (org-load-modules-maybe)
  5804. (setq org-store-link-plist nil) ; reset
  5805. (let (link cpltxt desc description search txt)
  5806. (cond
  5807. ((run-hook-with-args-until-success 'org-store-link-functions)
  5808. (setq link (plist-get org-store-link-plist :link)
  5809. desc (or (plist-get org-store-link-plist :description) link)))
  5810. ((equal (buffer-name) "*Org Edit Src Example*")
  5811. (let (label gc)
  5812. (while (or (not label)
  5813. (save-excursion
  5814. (save-restriction
  5815. (widen)
  5816. (goto-char (point-min))
  5817. (re-search-forward
  5818. (regexp-quote (format org-coderef-label-format label))
  5819. nil t))))
  5820. (when label (message "Label exists already") (sit-for 2))
  5821. (setq label (read-string "Code line label: " label)))
  5822. (end-of-line 1)
  5823. (setq link (format org-coderef-label-format label))
  5824. (setq gc (- 79 (length link)))
  5825. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5826. (insert link)
  5827. (setq link (concat "(" label ")") desc nil)))
  5828. ((eq major-mode 'calendar-mode)
  5829. (let ((cd (calendar-cursor-to-date)))
  5830. (setq link
  5831. (format-time-string
  5832. (car org-time-stamp-formats)
  5833. (apply 'encode-time
  5834. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5835. nil nil nil))))
  5836. (org-store-link-props :type "calendar" :date cd)))
  5837. ((eq major-mode 'w3-mode)
  5838. (setq cpltxt (url-view-url t)
  5839. link (org-make-link cpltxt))
  5840. (org-store-link-props :type "w3" :url (url-view-url t)))
  5841. ((eq major-mode 'w3m-mode)
  5842. (setq cpltxt (or w3m-current-title w3m-current-url)
  5843. link (org-make-link w3m-current-url))
  5844. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5845. ((setq search (run-hook-with-args-until-success
  5846. 'org-create-file-search-functions))
  5847. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5848. "::" search))
  5849. (setq cpltxt (or description link)))
  5850. ((eq major-mode 'image-mode)
  5851. (setq cpltxt (concat "file:"
  5852. (abbreviate-file-name buffer-file-name))
  5853. link (org-make-link cpltxt))
  5854. (org-store-link-props :type "image" :file buffer-file-name))
  5855. ((eq major-mode 'dired-mode)
  5856. ;; link to the file in the current line
  5857. (setq cpltxt (concat "file:"
  5858. (abbreviate-file-name
  5859. (expand-file-name
  5860. (dired-get-filename nil t))))
  5861. link (org-make-link cpltxt)))
  5862. ((and buffer-file-name (org-mode-p))
  5863. (cond
  5864. ((org-in-regexp "<<\\(.*?\\)>>")
  5865. (setq cpltxt
  5866. (concat "file:"
  5867. (abbreviate-file-name buffer-file-name)
  5868. "::" (match-string 1))
  5869. link (org-make-link cpltxt)))
  5870. ((and (featurep 'org-id)
  5871. (or (eq org-link-to-org-use-id t)
  5872. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5873. (interactive-p))
  5874. (and org-link-to-org-use-id
  5875. (condition-case nil
  5876. (org-entry-get nil "ID")
  5877. (error nil)))))
  5878. ;; We can make a link using the ID.
  5879. (setq link (condition-case nil
  5880. (prog1 (org-id-store-link)
  5881. (setq desc (plist-get org-store-link-plist
  5882. :description)))
  5883. (error
  5884. ;; probably before first headline, link to file only
  5885. (concat "file:"
  5886. (abbreviate-file-name buffer-file-name))))))
  5887. (t
  5888. ;; Just link to current headline
  5889. (setq cpltxt (concat "file:"
  5890. (abbreviate-file-name buffer-file-name)))
  5891. ;; Add a context search string
  5892. (when (org-xor org-context-in-file-links arg)
  5893. (setq txt (cond
  5894. ((org-on-heading-p) nil)
  5895. ((org-region-active-p)
  5896. (buffer-substring (region-beginning) (region-end)))
  5897. (t nil)))
  5898. (when (or (null txt) (string-match "\\S-" txt))
  5899. (setq cpltxt
  5900. (concat cpltxt "::"
  5901. (condition-case nil
  5902. (org-make-org-heading-search-string txt)
  5903. (error "")))
  5904. desc "NONE")))
  5905. (if (string-match "::\\'" cpltxt)
  5906. (setq cpltxt (substring cpltxt 0 -2)))
  5907. (setq link (org-make-link cpltxt)))))
  5908. ((buffer-file-name (buffer-base-buffer))
  5909. ;; Just link to this file here.
  5910. (setq cpltxt (concat "file:"
  5911. (abbreviate-file-name
  5912. (buffer-file-name (buffer-base-buffer)))))
  5913. ;; Add a context string
  5914. (when (org-xor org-context-in-file-links arg)
  5915. (setq txt (if (org-region-active-p)
  5916. (buffer-substring (region-beginning) (region-end))
  5917. (buffer-substring (point-at-bol) (point-at-eol))))
  5918. ;; Only use search option if there is some text.
  5919. (when (string-match "\\S-" txt)
  5920. (setq cpltxt
  5921. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5922. desc "NONE")))
  5923. (setq link (org-make-link cpltxt)))
  5924. ((interactive-p)
  5925. (error "Cannot link to a buffer which is not visiting a file"))
  5926. (t (setq link nil)))
  5927. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5928. (setq link (or link cpltxt)
  5929. desc (or desc cpltxt))
  5930. (if (equal desc "NONE") (setq desc nil))
  5931. (if (and (interactive-p) link)
  5932. (progn
  5933. (setq org-stored-links
  5934. (cons (list link desc) org-stored-links))
  5935. (message "Stored: %s" (or desc link)))
  5936. (and link (org-make-link-string link desc)))))
  5937. (defun org-store-link-props (&rest plist)
  5938. "Store link properties, extract names and addresses."
  5939. (let (x adr)
  5940. (when (setq x (plist-get plist :from))
  5941. (setq adr (mail-extract-address-components x))
  5942. (setq plist (plist-put plist :fromname (car adr)))
  5943. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5944. (when (setq x (plist-get plist :to))
  5945. (setq adr (mail-extract-address-components x))
  5946. (setq plist (plist-put plist :toname (car adr)))
  5947. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5948. (let ((from (plist-get plist :from))
  5949. (to (plist-get plist :to)))
  5950. (when (and from to org-from-is-user-regexp)
  5951. (setq plist
  5952. (plist-put plist :fromto
  5953. (if (string-match org-from-is-user-regexp from)
  5954. (concat "to %t")
  5955. (concat "from %f"))))))
  5956. (setq org-store-link-plist plist))
  5957. (defun org-add-link-props (&rest plist)
  5958. "Add these properties to the link property list."
  5959. (let (key value)
  5960. (while plist
  5961. (setq key (pop plist) value (pop plist))
  5962. (setq org-store-link-plist
  5963. (plist-put org-store-link-plist key value)))))
  5964. (defun org-email-link-description (&optional fmt)
  5965. "Return the description part of an email link.
  5966. This takes information from `org-store-link-plist' and formats it
  5967. according to FMT (default from `org-email-link-description-format')."
  5968. (setq fmt (or fmt org-email-link-description-format))
  5969. (let* ((p org-store-link-plist)
  5970. (to (plist-get p :toaddress))
  5971. (from (plist-get p :fromaddress))
  5972. (table
  5973. (list
  5974. (cons "%c" (plist-get p :fromto))
  5975. (cons "%F" (plist-get p :from))
  5976. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5977. (cons "%T" (plist-get p :to))
  5978. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5979. (cons "%s" (plist-get p :subject))
  5980. (cons "%m" (plist-get p :message-id)))))
  5981. (when (string-match "%c" fmt)
  5982. ;; Check if the user wrote this message
  5983. (if (and org-from-is-user-regexp from to
  5984. (save-match-data (string-match org-from-is-user-regexp from)))
  5985. (setq fmt (replace-match "to %t" t t fmt))
  5986. (setq fmt (replace-match "from %f" t t fmt))))
  5987. (org-replace-escapes fmt table)))
  5988. (defun org-make-org-heading-search-string (&optional string heading)
  5989. "Make search string for STRING or current headline."
  5990. (interactive)
  5991. (let ((s (or string (org-get-heading))))
  5992. (unless (and string (not heading))
  5993. ;; We are using a headline, clean up garbage in there.
  5994. (if (string-match org-todo-regexp s)
  5995. (setq s (replace-match "" t t s)))
  5996. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5997. (setq s (replace-match "" t t s)))
  5998. (setq s (org-trim s))
  5999. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6000. org-comment-string "\\)") s)
  6001. (setq s (replace-match "" t t s)))
  6002. (while (string-match org-ts-regexp s)
  6003. (setq s (replace-match "" t t s))))
  6004. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6005. (setq s (replace-match " " t t s)))
  6006. (or string (setq s (concat "*" s))) ; Add * for headlines
  6007. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6008. (defun org-make-link (&rest strings)
  6009. "Concatenate STRINGS."
  6010. (apply 'concat strings))
  6011. (defun org-make-link-string (link &optional description)
  6012. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6013. (unless (string-match "\\S-" link)
  6014. (error "Empty link"))
  6015. (when (stringp description)
  6016. ;; Remove brackets from the description, they are fatal.
  6017. (while (string-match "\\[" description)
  6018. (setq description (replace-match "{" t t description)))
  6019. (while (string-match "\\]" description)
  6020. (setq description (replace-match "}" t t description))))
  6021. (when (equal (org-link-escape link) description)
  6022. ;; No description needed, it is identical
  6023. (setq description nil))
  6024. (when (and (not description)
  6025. (not (equal link (org-link-escape link))))
  6026. (setq description (org-extract-attributes link)))
  6027. (concat "[[" (org-link-escape link) "]"
  6028. (if description (concat "[" description "]") "")
  6029. "]"))
  6030. (defconst org-link-escape-chars
  6031. '((?\ . "%20")
  6032. (?\[ . "%5B")
  6033. (?\] . "%5D")
  6034. (?\340 . "%E0") ; `a
  6035. (?\342 . "%E2") ; ^a
  6036. (?\347 . "%E7") ; ,c
  6037. (?\350 . "%E8") ; `e
  6038. (?\351 . "%E9") ; 'e
  6039. (?\352 . "%EA") ; ^e
  6040. (?\356 . "%EE") ; ^i
  6041. (?\364 . "%F4") ; ^o
  6042. (?\371 . "%F9") ; `u
  6043. (?\373 . "%FB") ; ^u
  6044. (?\; . "%3B")
  6045. (?? . "%3F")
  6046. (?= . "%3D")
  6047. (?+ . "%2B")
  6048. )
  6049. "Association list of escapes for some characters problematic in links.
  6050. This is the list that is used for internal purposes.")
  6051. (defconst org-link-escape-chars-browser
  6052. '((?\ . "%20")) ; 32 for the SPC char
  6053. "Association list of escapes for some characters problematic in links.
  6054. This is the list that is used before handing over to the browser.")
  6055. (defun org-link-escape (text &optional table)
  6056. "Escape characters in TEXT that are problematic for links."
  6057. (setq table (or table org-link-escape-chars))
  6058. (when text
  6059. (let ((re (mapconcat (lambda (x) (regexp-quote
  6060. (char-to-string (car x))))
  6061. table "\\|")))
  6062. (while (string-match re text)
  6063. (setq text
  6064. (replace-match
  6065. (cdr (assoc (string-to-char (match-string 0 text))
  6066. table))
  6067. t t text)))
  6068. text)))
  6069. (defun org-link-unescape (text &optional table)
  6070. "Reverse the action of `org-link-escape'."
  6071. (setq table (or table org-link-escape-chars))
  6072. (when text
  6073. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6074. table "\\|")))
  6075. (while (string-match re text)
  6076. (setq text
  6077. (replace-match
  6078. (char-to-string (car (rassoc (match-string 0 text) table)))
  6079. t t text)))
  6080. text)))
  6081. (defun org-xor (a b)
  6082. "Exclusive or."
  6083. (if a (not b) b))
  6084. (defun org-fixup-message-id-for-http (s)
  6085. "Replace special characters in a message id, so it can be used in an http query."
  6086. (while (string-match "<" s)
  6087. (setq s (replace-match "%3C" t t s)))
  6088. (while (string-match ">" s)
  6089. (setq s (replace-match "%3E" t t s)))
  6090. (while (string-match "@" s)
  6091. (setq s (replace-match "%40" t t s)))
  6092. s)
  6093. ;;;###autoload
  6094. (defun org-insert-link-global ()
  6095. "Insert a link like Org-mode does.
  6096. This command can be called in any mode to insert a link in Org-mode syntax."
  6097. (interactive)
  6098. (org-load-modules-maybe)
  6099. (org-run-like-in-org-mode 'org-insert-link))
  6100. (defun org-insert-link (&optional complete-file link-location)
  6101. "Insert a link. At the prompt, enter the link.
  6102. Completion can be used to insert any of the link protocol prefixes like
  6103. http or ftp in use.
  6104. The history can be used to select a link previously stored with
  6105. `org-store-link'. When the empty string is entered (i.e. if you just
  6106. press RET at the prompt), the link defaults to the most recently
  6107. stored link. As SPC triggers completion in the minibuffer, you need to
  6108. use M-SPC or C-q SPC to force the insertion of a space character.
  6109. You will also be prompted for a description, and if one is given, it will
  6110. be displayed in the buffer instead of the link.
  6111. If there is already a link at point, this command will allow you to edit link
  6112. and description parts.
  6113. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6114. be selected using completion. The path to the file will be relative to the
  6115. current directory if the file is in the current directory or a subdirectory.
  6116. Otherwise, the link will be the absolute path as completed in the minibuffer
  6117. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6118. option `org-link-file-path-type'.
  6119. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6120. the current directory or below.
  6121. With three \\[universal-argument] prefixes, negate the meaning of
  6122. `org-keep-stored-link-after-insertion'.
  6123. If `org-make-link-description-function' is non-nil, this function will be
  6124. called with the link target, and the result will be the default
  6125. link description.
  6126. If the LINK-LOCATION parameter is non-nil, this value will be
  6127. used as the link location instead of reading one interactively."
  6128. (interactive "P")
  6129. (let* ((wcf (current-window-configuration))
  6130. (region (if (org-region-active-p)
  6131. (buffer-substring (region-beginning) (region-end))))
  6132. (remove (and region (list (region-beginning) (region-end))))
  6133. (desc region)
  6134. tmphist ; byte-compile incorrectly complains about this
  6135. (link link-location)
  6136. entry file)
  6137. (cond
  6138. (link-location) ; specified by arg, just use it.
  6139. ((org-in-regexp org-bracket-link-regexp 1)
  6140. ;; We do have a link at point, and we are going to edit it.
  6141. (setq remove (list (match-beginning 0) (match-end 0)))
  6142. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6143. (setq link (read-string "Link: "
  6144. (org-link-unescape
  6145. (org-match-string-no-properties 1)))))
  6146. ((or (org-in-regexp org-angle-link-re)
  6147. (org-in-regexp org-plain-link-re))
  6148. ;; Convert to bracket link
  6149. (setq remove (list (match-beginning 0) (match-end 0))
  6150. link (read-string "Link: "
  6151. (org-remove-angle-brackets (match-string 0)))))
  6152. ((member complete-file '((4) (16)))
  6153. ;; Completing read for file names.
  6154. (setq file (read-file-name "File: "))
  6155. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6156. (pwd1 (file-name-as-directory (abbreviate-file-name
  6157. (expand-file-name ".")))))
  6158. (cond
  6159. ((equal complete-file '(16))
  6160. (setq link (org-make-link
  6161. "file:"
  6162. (abbreviate-file-name (expand-file-name file)))))
  6163. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6164. (setq link (org-make-link "file:" (match-string 1 file))))
  6165. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6166. (expand-file-name file))
  6167. (setq link (org-make-link
  6168. "file:" (match-string 1 (expand-file-name file)))))
  6169. (t (setq link (org-make-link "file:" file))))))
  6170. (t
  6171. ;; Read link, with completion for stored links.
  6172. (with-output-to-temp-buffer "*Org Links*"
  6173. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6174. (when org-stored-links
  6175. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6176. (princ (mapconcat
  6177. (lambda (x)
  6178. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6179. (reverse org-stored-links) "\n"))))
  6180. (let ((cw (selected-window)))
  6181. (select-window (get-buffer-window "*Org Links*"))
  6182. (setq truncate-lines t)
  6183. (org-fit-window-to-buffer)
  6184. (select-window cw))
  6185. ;; Fake a link history, containing the stored links.
  6186. (setq tmphist (append (mapcar 'car org-stored-links)
  6187. org-insert-link-history))
  6188. (unwind-protect
  6189. (setq link
  6190. (let ((org-completion-use-ido nil))
  6191. (org-completing-read
  6192. "Link: "
  6193. (append
  6194. (mapcar (lambda (x) (list (concat (car x) ":")))
  6195. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6196. (mapcar (lambda (x) (list (concat x ":")))
  6197. org-link-types))
  6198. nil nil nil
  6199. 'tmphist
  6200. (or (car (car org-stored-links))))))
  6201. (set-window-configuration wcf)
  6202. (kill-buffer "*Org Links*"))
  6203. (setq entry (assoc link org-stored-links))
  6204. (or entry (push link org-insert-link-history))
  6205. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6206. (not org-keep-stored-link-after-insertion))
  6207. (setq org-stored-links (delq (assoc link org-stored-links)
  6208. org-stored-links)))
  6209. (setq desc (or desc (nth 1 entry)))))
  6210. (if (string-match org-plain-link-re link)
  6211. ;; URL-like link, normalize the use of angular brackets.
  6212. (setq link (org-make-link (org-remove-angle-brackets link))))
  6213. ;; Check if we are linking to the current file with a search option
  6214. ;; If yes, simplify the link by using only the search option.
  6215. (when (and buffer-file-name
  6216. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6217. (let* ((path (match-string 1 link))
  6218. (case-fold-search nil)
  6219. (search (match-string 2 link)))
  6220. (save-match-data
  6221. (if (equal (file-truename buffer-file-name) (file-truename path))
  6222. ;; We are linking to this same file, with a search option
  6223. (setq link search)))))
  6224. ;; Check if we can/should use a relative path. If yes, simplify the link
  6225. (when (string-match "^file:\\(.*\\)" link)
  6226. (let* ((path (match-string 1 link))
  6227. (origpath path)
  6228. (case-fold-search nil))
  6229. (cond
  6230. ((or (eq org-link-file-path-type 'absolute)
  6231. (equal complete-file '(16)))
  6232. (setq path (abbreviate-file-name (expand-file-name path))))
  6233. ((eq org-link-file-path-type 'noabbrev)
  6234. (setq path (expand-file-name path)))
  6235. ((eq org-link-file-path-type 'relative)
  6236. (setq path (file-relative-name path)))
  6237. (t
  6238. (save-match-data
  6239. (if (string-match (concat "^" (regexp-quote
  6240. (file-name-as-directory
  6241. (expand-file-name "."))))
  6242. (expand-file-name path))
  6243. ;; We are linking a file with relative path name.
  6244. (setq path (substring (expand-file-name path)
  6245. (match-end 0)))
  6246. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6247. (setq link (concat "file:" path))
  6248. (if (equal desc origpath)
  6249. (setq desc path))))
  6250. (if org-make-link-description-function
  6251. (setq desc (funcall org-make-link-description-function link desc)))
  6252. (setq desc (read-string "Description: " desc))
  6253. (unless (string-match "\\S-" desc) (setq desc nil))
  6254. (if remove (apply 'delete-region remove))
  6255. (insert (org-make-link-string link desc))))
  6256. (defun org-completing-read (&rest args)
  6257. "Completing-read with SPACE being a normal character."
  6258. (let ((minibuffer-local-completion-map
  6259. (copy-keymap minibuffer-local-completion-map)))
  6260. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6261. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6262. (apply 'org-ido-completing-read args)))
  6263. (defun org-ido-completing-read (&rest args)
  6264. "Completing-read using `ido-mode' speedups if available"
  6265. (if (and org-completion-use-ido
  6266. (fboundp 'ido-completing-read)
  6267. (boundp 'ido-mode) ido-mode
  6268. (listp (second args)))
  6269. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6270. (apply 'completing-read args)))
  6271. (defun org-extract-attributes (s)
  6272. "Extract the attributes cookie from a string and set as text property."
  6273. (let (a attr (start 0) key value)
  6274. (save-match-data
  6275. (when (string-match "{{\\([^}]+\\)}}$" s)
  6276. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6277. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6278. (setq key (match-string 1 a) value (match-string 2 a)
  6279. start (match-end 0)
  6280. attr (plist-put attr (intern key) value))))
  6281. (org-add-props s nil 'org-attr attr))
  6282. s))
  6283. (defun org-attributes-to-string (plist)
  6284. "Format a property list into an HTML attribute list."
  6285. (let ((s "") key value)
  6286. (while plist
  6287. (setq key (pop plist) value (pop plist))
  6288. (and value
  6289. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6290. s))
  6291. ;;; Opening/following a link
  6292. (defvar org-link-search-failed nil)
  6293. (defun org-next-link ()
  6294. "Move forward to the next link.
  6295. If the link is in hidden text, expose it."
  6296. (interactive)
  6297. (when (and org-link-search-failed (eq this-command last-command))
  6298. (goto-char (point-min))
  6299. (message "Link search wrapped back to beginning of buffer"))
  6300. (setq org-link-search-failed nil)
  6301. (let* ((pos (point))
  6302. (ct (org-context))
  6303. (a (assoc :link ct)))
  6304. (if a (goto-char (nth 2 a)))
  6305. (if (re-search-forward org-any-link-re nil t)
  6306. (progn
  6307. (goto-char (match-beginning 0))
  6308. (if (org-invisible-p) (org-show-context)))
  6309. (goto-char pos)
  6310. (setq org-link-search-failed t)
  6311. (error "No further link found"))))
  6312. (defun org-previous-link ()
  6313. "Move backward to the previous link.
  6314. If the link is in hidden text, expose it."
  6315. (interactive)
  6316. (when (and org-link-search-failed (eq this-command last-command))
  6317. (goto-char (point-max))
  6318. (message "Link search wrapped back to end of buffer"))
  6319. (setq org-link-search-failed nil)
  6320. (let* ((pos (point))
  6321. (ct (org-context))
  6322. (a (assoc :link ct)))
  6323. (if a (goto-char (nth 1 a)))
  6324. (if (re-search-backward org-any-link-re nil t)
  6325. (progn
  6326. (goto-char (match-beginning 0))
  6327. (if (org-invisible-p) (org-show-context)))
  6328. (goto-char pos)
  6329. (setq org-link-search-failed t)
  6330. (error "No further link found"))))
  6331. (defun org-translate-link (s)
  6332. "Translate a link string if a translation function has been defined."
  6333. (if (and org-link-translation-function
  6334. (fboundp org-link-translation-function)
  6335. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6336. (progn
  6337. (setq s (funcall org-link-translation-function
  6338. (match-string 1) (match-string 2)))
  6339. (concat (car s) ":" (cdr s)))
  6340. s))
  6341. (defun org-translate-link-from-planner (type path)
  6342. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6343. This is still an experimental function, your mileage may vary."
  6344. (cond
  6345. ((member type '("http" "https" "news" "ftp"))
  6346. ;; standard Internet links are the same.
  6347. nil)
  6348. ((and (equal type "irc") (string-match "^//" path))
  6349. ;; Planner has two / at the beginning of an irc link, we have 1.
  6350. ;; We should have zero, actually....
  6351. (setq path (substring path 1)))
  6352. ((and (equal type "lisp") (string-match "^/" path))
  6353. ;; Planner has a slash, we do not.
  6354. (setq type "elisp" path (substring path 1)))
  6355. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6356. ;; A typical message link. Planner has the id after the fina slash,
  6357. ;; we separate it with a hash mark
  6358. (setq path (concat (match-string 1 path) "#"
  6359. (org-remove-angle-brackets (match-string 2 path)))))
  6360. )
  6361. (cons type path))
  6362. (defun org-find-file-at-mouse (ev)
  6363. "Open file link or URL at mouse."
  6364. (interactive "e")
  6365. (mouse-set-point ev)
  6366. (org-open-at-point 'in-emacs))
  6367. (defun org-open-at-mouse (ev)
  6368. "Open file link or URL at mouse."
  6369. (interactive "e")
  6370. (mouse-set-point ev)
  6371. (if (eq major-mode 'org-agenda-mode)
  6372. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6373. (org-open-at-point))
  6374. (defvar org-window-config-before-follow-link nil
  6375. "The window configuration before following a link.
  6376. This is saved in case the need arises to restore it.")
  6377. (defvar org-open-link-marker (make-marker)
  6378. "Marker pointing to the location where `org-open-at-point; was called.")
  6379. ;;;###autoload
  6380. (defun org-open-at-point-global ()
  6381. "Follow a link like Org-mode does.
  6382. This command can be called in any mode to follow a link that has
  6383. Org-mode syntax."
  6384. (interactive)
  6385. (org-run-like-in-org-mode 'org-open-at-point))
  6386. ;;;###autoload
  6387. (defun org-open-link-from-string (s &optional arg)
  6388. "Open a link in the string S, as if it was in Org-mode."
  6389. (interactive "sLink: \nP")
  6390. (with-temp-buffer
  6391. (let ((org-inhibit-startup t))
  6392. (org-mode)
  6393. (insert s)
  6394. (goto-char (point-min))
  6395. (org-open-at-point arg))))
  6396. (defun org-open-at-point (&optional in-emacs)
  6397. "Open link at or after point.
  6398. If there is no link at point, this function will search forward up to
  6399. the end of the current subtree.
  6400. Normally, files will be opened by an appropriate application. If the
  6401. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6402. With a double prefix argument, try to open outside of Emacs, in the
  6403. application the system uses for this file type."
  6404. (interactive "P")
  6405. (org-load-modules-maybe)
  6406. (move-marker org-open-link-marker (point))
  6407. (setq org-window-config-before-follow-link (current-window-configuration))
  6408. (org-remove-occur-highlights nil nil t)
  6409. (cond
  6410. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6411. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6412. (org-footnote-action))
  6413. (t
  6414. (let (type path link line search (pos (point)))
  6415. (catch 'match
  6416. (save-excursion
  6417. (skip-chars-forward "^]\n\r")
  6418. (when (org-in-regexp org-bracket-link-regexp)
  6419. (setq link (org-extract-attributes
  6420. (org-link-unescape (org-match-string-no-properties 1))))
  6421. (while (string-match " *\n *" link)
  6422. (setq link (replace-match " " t t link)))
  6423. (setq link (org-link-expand-abbrev link))
  6424. (cond
  6425. ((or (file-name-absolute-p link)
  6426. (string-match "^\\.\\.?/" link))
  6427. (setq type "file" path link))
  6428. ((string-match org-link-re-with-space3 link)
  6429. (setq type (match-string 1 link) path (match-string 2 link)))
  6430. (t (setq type "thisfile" path link)))
  6431. (throw 'match t)))
  6432. (when (get-text-property (point) 'org-linked-text)
  6433. (setq type "thisfile"
  6434. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6435. (1+ (point)) (point))
  6436. path (buffer-substring
  6437. (previous-single-property-change pos 'org-linked-text)
  6438. (next-single-property-change pos 'org-linked-text)))
  6439. (throw 'match t))
  6440. (save-excursion
  6441. (when (or (org-in-regexp org-angle-link-re)
  6442. (org-in-regexp org-plain-link-re))
  6443. (setq type (match-string 1) path (match-string 2))
  6444. (throw 'match t)))
  6445. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6446. (setq type "tree-match"
  6447. path (match-string 1))
  6448. (throw 'match t))
  6449. (save-excursion
  6450. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6451. (setq type "tags"
  6452. path (match-string 1))
  6453. (while (string-match ":" path)
  6454. (setq path (replace-match "+" t t path)))
  6455. (throw 'match t))))
  6456. (unless path
  6457. (error "No link found"))
  6458. ;; Remove any trailing spaces in path
  6459. (if (string-match " +\\'" path)
  6460. (setq path (replace-match "" t t path)))
  6461. (if (and org-link-translation-function
  6462. (fboundp org-link-translation-function))
  6463. ;; Check if we need to translate the link
  6464. (let ((tmp (funcall org-link-translation-function type path)))
  6465. (setq type (car tmp) path (cdr tmp))))
  6466. (cond
  6467. ((assoc type org-link-protocols)
  6468. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6469. ((equal type "mailto")
  6470. (let ((cmd (car org-link-mailto-program))
  6471. (args (cdr org-link-mailto-program)) args1
  6472. (address path) (subject "") a)
  6473. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6474. (setq address (match-string 1 path)
  6475. subject (org-link-escape (match-string 2 path))))
  6476. (while args
  6477. (cond
  6478. ((not (stringp (car args))) (push (pop args) args1))
  6479. (t (setq a (pop args))
  6480. (if (string-match "%a" a)
  6481. (setq a (replace-match address t t a)))
  6482. (if (string-match "%s" a)
  6483. (setq a (replace-match subject t t a)))
  6484. (push a args1))))
  6485. (apply cmd (nreverse args1))))
  6486. ((member type '("http" "https" "ftp" "news"))
  6487. (browse-url (concat type ":" (org-link-escape
  6488. path org-link-escape-chars-browser))))
  6489. ((member type '("message"))
  6490. (browse-url (concat type ":" path)))
  6491. ((string= type "tags")
  6492. (org-tags-view in-emacs path))
  6493. ((string= type "thisfile")
  6494. (if in-emacs
  6495. (switch-to-buffer-other-window
  6496. (org-get-buffer-for-internal-link (current-buffer)))
  6497. (org-mark-ring-push))
  6498. (let ((cmd `(org-link-search
  6499. ,path
  6500. ,(cond ((equal in-emacs '(4)) 'occur)
  6501. ((equal in-emacs '(16)) 'org-occur)
  6502. (t nil))
  6503. ,pos)))
  6504. (condition-case nil (eval cmd)
  6505. (error (progn (widen) (eval cmd))))))
  6506. ((string= type "tree-match")
  6507. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6508. ((string= type "file")
  6509. (if (string-match "::\\([0-9]+\\)\\'" path)
  6510. (setq line (string-to-number (match-string 1 path))
  6511. path (substring path 0 (match-beginning 0)))
  6512. (if (string-match "::\\(.+\\)\\'" path)
  6513. (setq search (match-string 1 path)
  6514. path (substring path 0 (match-beginning 0)))))
  6515. (if (string-match "[*?{]" (file-name-nondirectory path))
  6516. (dired path)
  6517. (org-open-file path in-emacs line search)))
  6518. ((string= type "news")
  6519. (require 'org-gnus)
  6520. (org-gnus-follow-link path))
  6521. ((string= type "shell")
  6522. (let ((cmd path))
  6523. (if (or (not org-confirm-shell-link-function)
  6524. (funcall org-confirm-shell-link-function
  6525. (format "Execute \"%s\" in shell? "
  6526. (org-add-props cmd nil
  6527. 'face 'org-warning))))
  6528. (progn
  6529. (message "Executing %s" cmd)
  6530. (shell-command cmd))
  6531. (error "Abort"))))
  6532. ((string= type "elisp")
  6533. (let ((cmd path))
  6534. (if (or (not org-confirm-elisp-link-function)
  6535. (funcall org-confirm-elisp-link-function
  6536. (format "Execute \"%s\" as elisp? "
  6537. (org-add-props cmd nil
  6538. 'face 'org-warning))))
  6539. (message "%s => %s" cmd
  6540. (if (equal (string-to-char cmd) ?\()
  6541. (eval (read cmd))
  6542. (call-interactively (read cmd))))
  6543. (error "Abort"))))
  6544. (t
  6545. (browse-url-at-point))))))
  6546. (move-marker org-open-link-marker nil)
  6547. (run-hook-with-args 'org-follow-link-hook))
  6548. ;;;; Time estimates
  6549. (defun org-get-effort (&optional pom)
  6550. "Get the effort estimate for the current entry."
  6551. (org-entry-get pom org-effort-property))
  6552. ;;; File search
  6553. (defvar org-create-file-search-functions nil
  6554. "List of functions to construct the right search string for a file link.
  6555. These functions are called in turn with point at the location to
  6556. which the link should point.
  6557. A function in the hook should first test if it would like to
  6558. handle this file type, for example by checking the major-mode or
  6559. the file extension. If it decides not to handle this file, it
  6560. should just return nil to give other functions a chance. If it
  6561. does handle the file, it must return the search string to be used
  6562. when following the link. The search string will be part of the
  6563. file link, given after a double colon, and `org-open-at-point'
  6564. will automatically search for it. If special measures must be
  6565. taken to make the search successful, another function should be
  6566. added to the companion hook `org-execute-file-search-functions',
  6567. which see.
  6568. A function in this hook may also use `setq' to set the variable
  6569. `description' to provide a suggestion for the descriptive text to
  6570. be used for this link when it gets inserted into an Org-mode
  6571. buffer with \\[org-insert-link].")
  6572. (defvar org-execute-file-search-functions nil
  6573. "List of functions to execute a file search triggered by a link.
  6574. Functions added to this hook must accept a single argument, the
  6575. search string that was part of the file link, the part after the
  6576. double colon. The function must first check if it would like to
  6577. handle this search, for example by checking the major-mode or the
  6578. file extension. If it decides not to handle this search, it
  6579. should just return nil to give other functions a chance. If it
  6580. does handle the search, it must return a non-nil value to keep
  6581. other functions from trying.
  6582. Each function can access the current prefix argument through the
  6583. variable `current-prefix-argument'. Note that a single prefix is
  6584. used to force opening a link in Emacs, so it may be good to only
  6585. use a numeric or double prefix to guide the search function.
  6586. In case this is needed, a function in this hook can also restore
  6587. the window configuration before `org-open-at-point' was called using:
  6588. (set-window-configuration org-window-config-before-follow-link)")
  6589. (defun org-link-search (s &optional type avoid-pos)
  6590. "Search for a link search option.
  6591. If S is surrounded by forward slashes, it is interpreted as a
  6592. regular expression. In org-mode files, this will create an `org-occur'
  6593. sparse tree. In ordinary files, `occur' will be used to list matches.
  6594. If the current buffer is in `dired-mode', grep will be used to search
  6595. in all files. If AVOID-POS is given, ignore matches near that position."
  6596. (let ((case-fold-search t)
  6597. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6598. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6599. (append '(("") (" ") ("\t") ("\n"))
  6600. org-emphasis-alist)
  6601. "\\|") "\\)"))
  6602. (pos (point))
  6603. (pre nil) (post nil)
  6604. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6605. (cond
  6606. ;; First check if there are any special
  6607. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6608. ;; Now try the builtin stuff
  6609. ((save-excursion
  6610. (goto-char (point-min))
  6611. (and
  6612. (re-search-forward
  6613. (concat "<<" (regexp-quote s0) ">>") nil t)
  6614. (setq type 'dedicated
  6615. pos (match-beginning 0))))
  6616. ;; There is an exact target for this
  6617. (goto-char pos))
  6618. ((and (string-match "^(\\(.*\\))$" s0)
  6619. (save-excursion
  6620. (goto-char (point-min))
  6621. (and
  6622. (re-search-forward
  6623. (concat "[^[]" (regexp-quote
  6624. (format org-coderef-label-format
  6625. (match-string 1 s0))))
  6626. nil t)
  6627. (setq type 'dedicated
  6628. pos (1+ (match-beginning 0))))))
  6629. ;; There is a coderef target for this
  6630. (goto-char pos))
  6631. ((string-match "^/\\(.*\\)/$" s)
  6632. ;; A regular expression
  6633. (cond
  6634. ((org-mode-p)
  6635. (org-occur (match-string 1 s)))
  6636. ;;((eq major-mode 'dired-mode)
  6637. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6638. (t (org-do-occur (match-string 1 s)))))
  6639. (t
  6640. ;; A normal search strings
  6641. (when (equal (string-to-char s) ?*)
  6642. ;; Anchor on headlines, post may include tags.
  6643. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6644. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6645. s (substring s 1)))
  6646. (remove-text-properties
  6647. 0 (length s)
  6648. '(face nil mouse-face nil keymap nil fontified nil) s)
  6649. ;; Make a series of regular expressions to find a match
  6650. (setq words (org-split-string s "[ \n\r\t]+")
  6651. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6652. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6653. "\\)" markers)
  6654. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6655. re2a (concat "[ \t\r\n]" re2a_)
  6656. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6657. re4 (concat "[^a-zA-Z_]" re4_)
  6658. re1 (concat pre re2 post)
  6659. re3 (concat pre (if pre re4_ re4) post)
  6660. re5 (concat pre ".*" re4)
  6661. re2 (concat pre re2)
  6662. re2a (concat pre (if pre re2a_ re2a))
  6663. re4 (concat pre (if pre re4_ re4))
  6664. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6665. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6666. re5 "\\)"
  6667. ))
  6668. (cond
  6669. ((eq type 'org-occur) (org-occur reall))
  6670. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6671. (t (goto-char (point-min))
  6672. (setq type 'fuzzy)
  6673. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6674. (org-search-not-self 1 re1 nil t)
  6675. (org-search-not-self 1 re2 nil t)
  6676. (org-search-not-self 1 re2a nil t)
  6677. (org-search-not-self 1 re3 nil t)
  6678. (org-search-not-self 1 re4 nil t)
  6679. (org-search-not-self 1 re5 nil t)
  6680. )
  6681. (goto-char (match-beginning 1))
  6682. (goto-char pos)
  6683. (error "No match")))))
  6684. (t
  6685. ;; Normal string-search
  6686. (goto-char (point-min))
  6687. (if (search-forward s nil t)
  6688. (goto-char (match-beginning 0))
  6689. (error "No match"))))
  6690. (and (org-mode-p) (org-show-context 'link-search))
  6691. type))
  6692. (defun org-search-not-self (group &rest args)
  6693. "Execute `re-search-forward', but only accept matches that do not
  6694. enclose the position of `org-open-link-marker'."
  6695. (let ((m org-open-link-marker))
  6696. (catch 'exit
  6697. (while (apply 're-search-forward args)
  6698. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6699. (goto-char (match-end group))
  6700. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6701. (> (match-beginning 0) (marker-position m))
  6702. (< (match-end 0) (marker-position m)))
  6703. (save-match-data
  6704. (or (not (org-in-regexp
  6705. org-bracket-link-analytic-regexp 1))
  6706. (not (match-end 4)) ; no description
  6707. (and (<= (match-beginning 4) (point))
  6708. (>= (match-end 4) (point))))))
  6709. (throw 'exit (point))))))))
  6710. (defun org-get-buffer-for-internal-link (buffer)
  6711. "Return a buffer to be used for displaying the link target of internal links."
  6712. (cond
  6713. ((not org-display-internal-link-with-indirect-buffer)
  6714. buffer)
  6715. ((string-match "(Clone)$" (buffer-name buffer))
  6716. (message "Buffer is already a clone, not making another one")
  6717. ;; we also do not modify visibility in this case
  6718. buffer)
  6719. (t ; make a new indirect buffer for displaying the link
  6720. (let* ((bn (buffer-name buffer))
  6721. (ibn (concat bn "(Clone)"))
  6722. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6723. (with-current-buffer ib (org-overview))
  6724. ib))))
  6725. (defun org-do-occur (regexp &optional cleanup)
  6726. "Call the Emacs command `occur'.
  6727. If CLEANUP is non-nil, remove the printout of the regular expression
  6728. in the *Occur* buffer. This is useful if the regex is long and not useful
  6729. to read."
  6730. (occur regexp)
  6731. (when cleanup
  6732. (let ((cwin (selected-window)) win beg end)
  6733. (when (setq win (get-buffer-window "*Occur*"))
  6734. (select-window win))
  6735. (goto-char (point-min))
  6736. (when (re-search-forward "match[a-z]+" nil t)
  6737. (setq beg (match-end 0))
  6738. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6739. (setq end (1- (match-beginning 0)))))
  6740. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6741. (goto-char (point-min))
  6742. (select-window cwin))))
  6743. ;;; The mark ring for links jumps
  6744. (defvar org-mark-ring nil
  6745. "Mark ring for positions before jumps in Org-mode.")
  6746. (defvar org-mark-ring-last-goto nil
  6747. "Last position in the mark ring used to go back.")
  6748. ;; Fill and close the ring
  6749. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6750. (loop for i from 1 to org-mark-ring-length do
  6751. (push (make-marker) org-mark-ring))
  6752. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6753. org-mark-ring)
  6754. (defun org-mark-ring-push (&optional pos buffer)
  6755. "Put the current position or POS into the mark ring and rotate it."
  6756. (interactive)
  6757. (setq pos (or pos (point)))
  6758. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6759. (move-marker (car org-mark-ring)
  6760. (or pos (point))
  6761. (or buffer (current-buffer)))
  6762. (message "%s"
  6763. (substitute-command-keys
  6764. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6765. (defun org-mark-ring-goto (&optional n)
  6766. "Jump to the previous position in the mark ring.
  6767. With prefix arg N, jump back that many stored positions. When
  6768. called several times in succession, walk through the entire ring.
  6769. Org-mode commands jumping to a different position in the current file,
  6770. or to another Org-mode file, automatically push the old position
  6771. onto the ring."
  6772. (interactive "p")
  6773. (let (p m)
  6774. (if (eq last-command this-command)
  6775. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6776. (setq p org-mark-ring))
  6777. (setq org-mark-ring-last-goto p)
  6778. (setq m (car p))
  6779. (switch-to-buffer (marker-buffer m))
  6780. (goto-char m)
  6781. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6782. (defun org-remove-angle-brackets (s)
  6783. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6784. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6785. s)
  6786. (defun org-add-angle-brackets (s)
  6787. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6788. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6789. s)
  6790. (defun org-remove-double-quotes (s)
  6791. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6792. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6793. s)
  6794. ;;; Following specific links
  6795. (defun org-follow-timestamp-link ()
  6796. (cond
  6797. ((org-at-date-range-p t)
  6798. (let ((org-agenda-start-on-weekday)
  6799. (t1 (match-string 1))
  6800. (t2 (match-string 2)))
  6801. (setq t1 (time-to-days (org-time-string-to-time t1))
  6802. t2 (time-to-days (org-time-string-to-time t2)))
  6803. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6804. ((org-at-timestamp-p t)
  6805. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6806. (substring (match-string 1) 0 10)))
  6807. 1))
  6808. (t (error "This should not happen"))))
  6809. ;;; Following file links
  6810. (defvar org-wait nil)
  6811. (defun org-open-file (path &optional in-emacs line search)
  6812. "Open the file at PATH.
  6813. First, this expands any special file name abbreviations. Then the
  6814. configuration variable `org-file-apps' is checked if it contains an
  6815. entry for this file type, and if yes, the corresponding command is launched.
  6816. If no application is found, Emacs simply visits the file.
  6817. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6818. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6819. and o use an external application to visit the file.
  6820. Optional LINE specifies a line to go to, optional SEARCH a string to
  6821. search for. If LINE or SEARCH is given, the file will always be
  6822. opened in Emacs.
  6823. If the file does not exist, an error is thrown."
  6824. (setq in-emacs (or in-emacs line search))
  6825. (let* ((file (if (equal path "")
  6826. buffer-file-name
  6827. (substitute-in-file-name (expand-file-name path))))
  6828. (apps (append org-file-apps (org-default-apps)))
  6829. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6830. (dirp (if remp nil (file-directory-p file)))
  6831. (file (if (and dirp org-open-directory-means-index-dot-org)
  6832. (concat (file-name-as-directory file) "index.org")
  6833. file))
  6834. (a-m-a-p (assq 'auto-mode apps))
  6835. (dfile (downcase file))
  6836. (old-buffer (current-buffer))
  6837. (old-pos (point))
  6838. (old-mode major-mode)
  6839. ext cmd)
  6840. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6841. (setq ext (match-string 1 dfile))
  6842. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6843. (setq ext (match-string 1 dfile))))
  6844. (cond
  6845. ((equal in-emacs '(16))
  6846. (setq cmd (cdr (assoc 'system apps))))
  6847. (in-emacs (setq cmd 'emacs))
  6848. (t
  6849. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6850. (and dirp (cdr (assoc 'directory apps)))
  6851. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6852. 'string-match)
  6853. (cdr (assoc ext apps))
  6854. (cdr (assoc t apps))))))
  6855. (when (eq cmd 'system)
  6856. (setq cmd (cdr (assoc 'system apps))))
  6857. (when (eq cmd 'default)
  6858. (setq cmd (cdr (assoc t apps))))
  6859. (when (eq cmd 'mailcap)
  6860. (require 'mailcap)
  6861. (mailcap-parse-mailcaps)
  6862. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6863. (command (mailcap-mime-info mime-type)))
  6864. (if (stringp command)
  6865. (setq cmd command)
  6866. (setq cmd 'emacs))))
  6867. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6868. (not (file-exists-p file))
  6869. (not org-open-non-existing-files))
  6870. (error "No such file: %s" file))
  6871. (cond
  6872. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6873. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6874. (while (string-match "['\"]%s['\"]" cmd)
  6875. (setq cmd (replace-match "%s" t t cmd)))
  6876. (while (string-match "%s" cmd)
  6877. (setq cmd (replace-match
  6878. (save-match-data
  6879. (shell-quote-argument
  6880. (convert-standard-filename file)))
  6881. t t cmd)))
  6882. (save-window-excursion
  6883. (start-process-shell-command cmd nil cmd)
  6884. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6885. ))
  6886. ((or (stringp cmd)
  6887. (eq cmd 'emacs))
  6888. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6889. (widen)
  6890. (if line (goto-line line)
  6891. (if search (org-link-search search))))
  6892. ((consp cmd)
  6893. (let ((file (convert-standard-filename file)))
  6894. (eval cmd)))
  6895. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6896. (and (org-mode-p) (eq old-mode 'org-mode)
  6897. (or (not (equal old-buffer (current-buffer)))
  6898. (not (equal old-pos (point))))
  6899. (org-mark-ring-push old-pos old-buffer))))
  6900. (defun org-default-apps ()
  6901. "Return the default applications for this operating system."
  6902. (cond
  6903. ((eq system-type 'darwin)
  6904. org-file-apps-defaults-macosx)
  6905. ((eq system-type 'windows-nt)
  6906. org-file-apps-defaults-windowsnt)
  6907. (t org-file-apps-defaults-gnu)))
  6908. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6909. "Convert extensions to regular expressions in the cars of LIST.
  6910. Also, weed out any non-string entries, because the return value is used
  6911. only for regexp matching.
  6912. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6913. point to the symbol `emacs', indicating that the file should
  6914. be opened in Emacs."
  6915. (append
  6916. (delq nil
  6917. (mapcar (lambda (x)
  6918. (if (not (stringp (car x)))
  6919. nil
  6920. (if (string-match "\\W" (car x))
  6921. x
  6922. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6923. list))
  6924. (if add-auto-mode
  6925. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6926. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6927. (defun org-file-remote-p (file)
  6928. "Test whether FILE specifies a location on a remote system.
  6929. Return non-nil if the location is indeed remote.
  6930. For example, the filename \"/user@host:/foo\" specifies a location
  6931. on the system \"/user@host:\"."
  6932. (cond ((fboundp 'file-remote-p)
  6933. (file-remote-p file))
  6934. ((fboundp 'tramp-handle-file-remote-p)
  6935. (tramp-handle-file-remote-p file))
  6936. ((and (boundp 'ange-ftp-name-format)
  6937. (string-match (car ange-ftp-name-format) file))
  6938. t)
  6939. (t nil)))
  6940. ;;;; Refiling
  6941. (defun org-get-org-file ()
  6942. "Read a filename, with default directory `org-directory'."
  6943. (let ((default (or org-default-notes-file remember-data-file)))
  6944. (read-file-name (format "File name [%s]: " default)
  6945. (file-name-as-directory org-directory)
  6946. default)))
  6947. (defun org-notes-order-reversed-p ()
  6948. "Check if the current file should receive notes in reversed order."
  6949. (cond
  6950. ((not org-reverse-note-order) nil)
  6951. ((eq t org-reverse-note-order) t)
  6952. ((not (listp org-reverse-note-order)) nil)
  6953. (t (catch 'exit
  6954. (let ((all org-reverse-note-order)
  6955. entry)
  6956. (while (setq entry (pop all))
  6957. (if (string-match (car entry) buffer-file-name)
  6958. (throw 'exit (cdr entry))))
  6959. nil)))))
  6960. (defvar org-refile-target-table nil
  6961. "The list of refile targets, created by `org-refile'.")
  6962. (defvar org-agenda-new-buffers nil
  6963. "Buffers created to visit agenda files.")
  6964. (defun org-get-refile-targets (&optional default-buffer)
  6965. "Produce a table with refile targets."
  6966. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6967. targets txt re files f desc descre fast-path-p level)
  6968. (message "Getting targets...")
  6969. (with-current-buffer (or default-buffer (current-buffer))
  6970. (while (setq entry (pop entries))
  6971. (setq files (car entry) desc (cdr entry))
  6972. (setq fast-path-p nil)
  6973. (cond
  6974. ((null files) (setq files (list (current-buffer))))
  6975. ((eq files 'org-agenda-files)
  6976. (setq files (org-agenda-files 'unrestricted)))
  6977. ((and (symbolp files) (fboundp files))
  6978. (setq files (funcall files)))
  6979. ((and (symbolp files) (boundp files))
  6980. (setq files (symbol-value files))))
  6981. (if (stringp files) (setq files (list files)))
  6982. (cond
  6983. ((eq (car desc) :tag)
  6984. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6985. ((eq (car desc) :todo)
  6986. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6987. ((eq (car desc) :regexp)
  6988. (setq descre (cdr desc)))
  6989. ((eq (car desc) :level)
  6990. (setq descre (concat "^\\*\\{" (number-to-string
  6991. (if org-odd-levels-only
  6992. (1- (* 2 (cdr desc)))
  6993. (cdr desc)))
  6994. "\\}[ \t]")))
  6995. ((eq (car desc) :maxlevel)
  6996. (setq fast-path-p t)
  6997. (setq descre (concat "^\\*\\{1," (number-to-string
  6998. (if org-odd-levels-only
  6999. (1- (* 2 (cdr desc)))
  7000. (cdr desc)))
  7001. "\\}[ \t]")))
  7002. (t (error "Bad refiling target description %s" desc)))
  7003. (while (setq f (pop files))
  7004. (save-excursion
  7005. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7006. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7007. (setq f (expand-file-name f))
  7008. (save-excursion
  7009. (save-restriction
  7010. (widen)
  7011. (goto-char (point-min))
  7012. (while (re-search-forward descre nil t)
  7013. (goto-char (point-at-bol))
  7014. (when (looking-at org-complex-heading-regexp)
  7015. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7016. txt (org-link-display-format (match-string 4))
  7017. re (concat "^" (regexp-quote
  7018. (buffer-substring (match-beginning 1)
  7019. (match-end 4)))))
  7020. (if (match-end 5) (setq re (concat re "[ \t]+"
  7021. (regexp-quote
  7022. (match-string 5)))))
  7023. (setq re (concat re "[ \t]*$"))
  7024. (when org-refile-use-outline-path
  7025. (setq txt (mapconcat 'org-protect-slash
  7026. (append
  7027. (if (eq org-refile-use-outline-path 'file)
  7028. (list (file-name-nondirectory
  7029. (buffer-file-name (buffer-base-buffer))))
  7030. (if (eq org-refile-use-outline-path 'full-file-path)
  7031. (list (buffer-file-name (buffer-base-buffer)))))
  7032. (org-get-outline-path fast-path-p level txt)
  7033. (list txt))
  7034. "/")))
  7035. (push (list txt f re (point)) targets))
  7036. (goto-char (point-at-eol))))))))
  7037. (message "Getting targets...done")
  7038. (nreverse targets))))
  7039. (defun org-protect-slash (s)
  7040. (while (string-match "/" s)
  7041. (setq s (replace-match "\\" t t s)))
  7042. s)
  7043. (defvar org-olpa (make-vector 20 nil))
  7044. (defun org-get-outline-path (&optional fastp level heading)
  7045. "Return the outline path to the current entry, as a list."
  7046. (if fastp
  7047. (progn
  7048. (if (> level 19)
  7049. (error "Outline path failure, more than 19 levels."))
  7050. (loop for i from level upto 19 do
  7051. (aset org-olpa i nil))
  7052. (prog1
  7053. (delq nil (append org-olpa nil))
  7054. (aset org-olpa level heading)))
  7055. (let (rtn)
  7056. (save-excursion
  7057. (while (org-up-heading-safe)
  7058. (when (looking-at org-complex-heading-regexp)
  7059. (push (org-match-string-no-properties 4) rtn)))
  7060. rtn))))
  7061. (defvar org-refile-history nil
  7062. "History for refiling operations.")
  7063. (defun org-refile (&optional goto default-buffer)
  7064. "Move the entry at point to another heading.
  7065. The list of target headings is compiled using the information in
  7066. `org-refile-targets', which see. This list is created before each use
  7067. and will therefore always be up-to-date.
  7068. At the target location, the entry is filed as a subitem of the target heading.
  7069. Depending on `org-reverse-note-order', the new subitem will either be the
  7070. first or the last subitem.
  7071. If there is an active region, all entries in that region will be moved.
  7072. However, the region must fulfil the requirement that the first heading
  7073. is the first one sets the top-level of the moved text - at most siblings
  7074. below it are allowed.
  7075. With prefix arg GOTO, the command will only visit the target location,
  7076. not actually move anything.
  7077. With a double prefix `C-u C-u', go to the location where the last refiling
  7078. operation has put the subtree."
  7079. (interactive "P")
  7080. (let* ((cbuf (current-buffer))
  7081. (regionp (org-region-active-p))
  7082. (region-start (and regionp (region-beginning)))
  7083. (region-end (and regionp (region-end)))
  7084. (region-length (and regionp (- region-end region-start)))
  7085. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7086. pos it nbuf file re level reversed)
  7087. (when regionp (goto-char region-start)
  7088. (unless (org-kill-is-subtree-p
  7089. (buffer-substring region-start region-end))
  7090. (error "The region is not a (sequence of) subtree(s)")))
  7091. (if (equal goto '(16))
  7092. (org-refile-goto-last-stored)
  7093. (when (setq it (org-refile-get-location
  7094. (if goto "Goto: " "Refile to: ") default-buffer))
  7095. (setq file (nth 1 it)
  7096. re (nth 2 it)
  7097. pos (nth 3 it))
  7098. (if (and (equal (buffer-file-name) file)
  7099. (if regionp
  7100. (and (>= pos region-start)
  7101. (<= pos region-end))
  7102. (and (>= pos (point))
  7103. (< pos (save-excursion
  7104. (org-end-of-subtree t t))))))
  7105. (error "Cannot refile to position inside the tree or region"))
  7106. (setq nbuf (or (find-buffer-visiting file)
  7107. (find-file-noselect file)))
  7108. (if goto
  7109. (progn
  7110. (switch-to-buffer nbuf)
  7111. (goto-char pos)
  7112. (org-show-context 'org-goto))
  7113. (if regionp
  7114. (progn
  7115. (kill-new (buffer-substring region-start region-end))
  7116. (org-save-markers-in-region region-start region-end))
  7117. (org-copy-subtree 1 nil t))
  7118. (save-excursion
  7119. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7120. (find-file-noselect file))))
  7121. (setq reversed (org-notes-order-reversed-p))
  7122. (save-excursion
  7123. (save-restriction
  7124. (widen)
  7125. (goto-char pos)
  7126. (looking-at outline-regexp)
  7127. (setq level (org-get-valid-level (funcall outline-level) 1))
  7128. (goto-char
  7129. (if reversed
  7130. (or (outline-next-heading) (point-max))
  7131. (or (save-excursion (outline-get-next-sibling))
  7132. (org-end-of-subtree t t)
  7133. (point-max))))
  7134. (if (not (bolp)) (newline))
  7135. (bookmark-set "org-refile-last-stored")
  7136. (org-paste-subtree level))))
  7137. (if regionp
  7138. (delete-region (point) (+ (point) region-length))
  7139. (org-cut-subtree))
  7140. (setq org-markers-to-move nil)
  7141. (message "Refiled to \"%s\"" (car it)))))))
  7142. (defun org-refile-goto-last-stored ()
  7143. "Go to the location where the last refile was stored."
  7144. (interactive)
  7145. (bookmark-jump "org-refile-last-stored")
  7146. (message "This is the location of the last refile"))
  7147. (defun org-refile-get-location (&optional prompt default-buffer)
  7148. "Prompt the user for a refile location, using PROMPT."
  7149. (let ((org-refile-targets org-refile-targets)
  7150. (org-refile-use-outline-path org-refile-use-outline-path))
  7151. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7152. (unless org-refile-target-table
  7153. (error "No refile targets"))
  7154. (let* ((cbuf (current-buffer))
  7155. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7156. (cfunc (if (and org-refile-use-outline-path
  7157. org-outline-path-complete-in-steps)
  7158. 'org-olpath-completing-read
  7159. 'org-ido-completing-read))
  7160. (extra (if org-refile-use-outline-path "/" ""))
  7161. (filename (and cfn (expand-file-name cfn)))
  7162. (tbl (mapcar
  7163. (lambda (x)
  7164. (if (not (equal filename (nth 1 x)))
  7165. (cons (concat (car x) extra " ("
  7166. (file-name-nondirectory (nth 1 x)) ")")
  7167. (cdr x))
  7168. (cons (concat (car x) extra) (cdr x))))
  7169. org-refile-target-table))
  7170. (completion-ignore-case t))
  7171. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7172. tbl)))
  7173. (defun org-olpath-completing-read (prompt collection &rest args)
  7174. "Read an outline path like a file name."
  7175. (let ((thetable collection))
  7176. (apply
  7177. 'org-ido-completing-read prompt
  7178. (lambda (string predicate &optional flag)
  7179. (let (rtn r f (l (length string)))
  7180. (cond
  7181. ((eq flag nil)
  7182. ;; try completion
  7183. (try-completion string thetable))
  7184. ((eq flag t)
  7185. ;; all-completions
  7186. (setq rtn (all-completions string thetable predicate))
  7187. (mapcar
  7188. (lambda (x)
  7189. (setq r (substring x l))
  7190. (if (string-match " ([^)]*)$" x)
  7191. (setq f (match-string 0 x))
  7192. (setq f ""))
  7193. (if (string-match "/" r)
  7194. (concat string (substring r 0 (match-end 0)) f)
  7195. x))
  7196. rtn))
  7197. ((eq flag 'lambda)
  7198. ;; exact match?
  7199. (assoc string thetable)))
  7200. ))
  7201. args)))
  7202. ;;;; Dynamic blocks
  7203. (defun org-find-dblock (name)
  7204. "Find the first dynamic block with name NAME in the buffer.
  7205. If not found, stay at current position and return nil."
  7206. (let (pos)
  7207. (save-excursion
  7208. (goto-char (point-min))
  7209. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7210. nil t)
  7211. (match-beginning 0))))
  7212. (if pos (goto-char pos))
  7213. pos))
  7214. (defconst org-dblock-start-re
  7215. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7216. "Matches the startline of a dynamic block, with parameters.")
  7217. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7218. "Matches the end of a dynamic block.")
  7219. (defun org-create-dblock (plist)
  7220. "Create a dynamic block section, with parameters taken from PLIST.
  7221. PLIST must contain a :name entry which is used as name of the block."
  7222. (unless (bolp) (newline))
  7223. (let ((name (plist-get plist :name)))
  7224. (insert "#+BEGIN: " name)
  7225. (while plist
  7226. (if (eq (car plist) :name)
  7227. (setq plist (cddr plist))
  7228. (insert " " (prin1-to-string (pop plist)))))
  7229. (insert "\n\n#+END:\n")
  7230. (beginning-of-line -2)))
  7231. (defun org-prepare-dblock ()
  7232. "Prepare dynamic block for refresh.
  7233. This empties the block, puts the cursor at the insert position and returns
  7234. the property list including an extra property :name with the block name."
  7235. (unless (looking-at org-dblock-start-re)
  7236. (error "Not at a dynamic block"))
  7237. (let* ((begdel (1+ (match-end 0)))
  7238. (name (org-no-properties (match-string 1)))
  7239. (params (append (list :name name)
  7240. (read (concat "(" (match-string 3) ")")))))
  7241. (unless (re-search-forward org-dblock-end-re nil t)
  7242. (error "Dynamic block not terminated"))
  7243. (setq params
  7244. (append params
  7245. (list :content (buffer-substring
  7246. begdel (match-beginning 0)))))
  7247. (delete-region begdel (match-beginning 0))
  7248. (goto-char begdel)
  7249. (open-line 1)
  7250. params))
  7251. (defun org-map-dblocks (&optional command)
  7252. "Apply COMMAND to all dynamic blocks in the current buffer.
  7253. If COMMAND is not given, use `org-update-dblock'."
  7254. (let ((cmd (or command 'org-update-dblock))
  7255. pos)
  7256. (save-excursion
  7257. (goto-char (point-min))
  7258. (while (re-search-forward org-dblock-start-re nil t)
  7259. (goto-char (setq pos (match-beginning 0)))
  7260. (condition-case nil
  7261. (funcall cmd)
  7262. (error (message "Error during update of dynamic block")))
  7263. (goto-char pos)
  7264. (unless (re-search-forward org-dblock-end-re nil t)
  7265. (error "Dynamic block not terminated"))))))
  7266. (defun org-dblock-update (&optional arg)
  7267. "User command for updating dynamic blocks.
  7268. Update the dynamic block at point. With prefix ARG, update all dynamic
  7269. blocks in the buffer."
  7270. (interactive "P")
  7271. (if arg
  7272. (org-update-all-dblocks)
  7273. (or (looking-at org-dblock-start-re)
  7274. (org-beginning-of-dblock))
  7275. (org-update-dblock)))
  7276. (defun org-update-dblock ()
  7277. "Update the dynamic block at point
  7278. This means to empty the block, parse for parameters and then call
  7279. the correct writing function."
  7280. (save-window-excursion
  7281. (let* ((pos (point))
  7282. (line (org-current-line))
  7283. (params (org-prepare-dblock))
  7284. (name (plist-get params :name))
  7285. (cmd (intern (concat "org-dblock-write:" name))))
  7286. (message "Updating dynamic block `%s' at line %d..." name line)
  7287. (funcall cmd params)
  7288. (message "Updating dynamic block `%s' at line %d...done" name line)
  7289. (goto-char pos))))
  7290. (defun org-beginning-of-dblock ()
  7291. "Find the beginning of the dynamic block at point.
  7292. Error if there is no such block at point."
  7293. (let ((pos (point))
  7294. beg)
  7295. (end-of-line 1)
  7296. (if (and (re-search-backward org-dblock-start-re nil t)
  7297. (setq beg (match-beginning 0))
  7298. (re-search-forward org-dblock-end-re nil t)
  7299. (> (match-end 0) pos))
  7300. (goto-char beg)
  7301. (goto-char pos)
  7302. (error "Not in a dynamic block"))))
  7303. (defun org-update-all-dblocks ()
  7304. "Update all dynamic blocks in the buffer.
  7305. This function can be used in a hook."
  7306. (when (org-mode-p)
  7307. (org-map-dblocks 'org-update-dblock)))
  7308. ;;;; Completion
  7309. (defconst org-additional-option-like-keywords
  7310. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7311. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7312. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7313. "BEGIN_QUOTE" "END_QUOTE"
  7314. "BEGIN_VERSE" "END_VERSE"
  7315. "BEGIN_SRC" "END_SRC"
  7316. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7317. (defcustom org-structure-template-alist
  7318. '(
  7319. ("s" "#+begin_src ?\n\n#+end_src"
  7320. "<src lang=\"?\">\n\n</src>")
  7321. ("e" "#+begin_example\n?\n#+end_example"
  7322. "<example>\n?\n</example>")
  7323. ("q" "#+begin_quote\n?\n#+end_quote"
  7324. "<quote>\n?\n</quote>")
  7325. ("v" "#+begin_verse\n?\n#+end_verse"
  7326. "<verse>\n?\n/verse>")
  7327. ("l" "#+begin_latex\n?\n#+end_latex"
  7328. "<literal style=\"latex\">\n?\n</literal>")
  7329. ("L" "#+latex: "
  7330. "<literal style=\"latex\">?</literal>")
  7331. ("h" "#+begin_html\n?\n#+end_html"
  7332. "<literal style=\"html\">\n?\n</literal>")
  7333. ("H" "#+html: "
  7334. "<literal style=\"html\">?</literal>")
  7335. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7336. ("A" "#+ascii: ")
  7337. ("i" "#+include %file ?"
  7338. "<include file=%file markup=\"?\">")
  7339. )
  7340. "Structure completion elements.
  7341. This is a list of abbreviation keys and values. The value gets inserted
  7342. it you type @samp{.} followed by the key and then the completion key,
  7343. usually `M-TAB'. %file will be replaced by a file name after prompting
  7344. for the file using completion.
  7345. There are two templates for each key, the first uses the original Org syntax,
  7346. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7347. the default when the /org-mtags.el/ module has been loaded. See also the
  7348. variable `org-mtags-prefer-muse-templates'.
  7349. This is an experimental feature, it is undecided if it is going to stay in."
  7350. :group 'org-completion
  7351. :type '(repeat
  7352. (string :tag "Key")
  7353. (string :tag "Template")
  7354. (string :tag "Muse Template")))
  7355. (defun org-try-structure-completion ()
  7356. "Try to complete a structure template before point.
  7357. This looks for strings like \"<e\" on an otherwise empty line and
  7358. expands them."
  7359. (let ((l (buffer-substring (point-at-bol) (point)))
  7360. a)
  7361. (when (and (looking-at "[ \t]*$")
  7362. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7363. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7364. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7365. (match-beginning 1)) a)
  7366. t)))
  7367. (defun org-complete-expand-structure-template (start cell)
  7368. "Expand a structure template."
  7369. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7370. (rpl (nth (if musep 2 1) cell)))
  7371. (delete-region start (point))
  7372. (when (string-match "\\`#\\+" rpl)
  7373. (cond
  7374. ((bolp))
  7375. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7376. (delete-region (point-at-bol) (point)))
  7377. (t (newline))))
  7378. (setq start (point))
  7379. (if (string-match "%file" rpl)
  7380. (setq rpl (replace-match
  7381. (concat
  7382. "\""
  7383. (save-match-data
  7384. (abbreviate-file-name (read-file-name "Include file: ")))
  7385. "\"")
  7386. t t rpl)))
  7387. (insert rpl)
  7388. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7389. (defun org-complete (&optional arg)
  7390. "Perform completion on word at point.
  7391. At the beginning of a headline, this completes TODO keywords as given in
  7392. `org-todo-keywords'.
  7393. If the current word is preceded by a backslash, completes the TeX symbols
  7394. that are supported for HTML support.
  7395. If the current word is preceded by \"#+\", completes special words for
  7396. setting file options.
  7397. In the line after \"#+STARTUP:, complete valid keywords.\"
  7398. At all other locations, this simply calls the value of
  7399. `org-completion-fallback-command'."
  7400. (interactive "P")
  7401. (org-without-partial-completion
  7402. (catch 'exit
  7403. (let* ((a nil)
  7404. (end (point))
  7405. (beg1 (save-excursion
  7406. (skip-chars-backward (org-re "[:alnum:]_@"))
  7407. (point)))
  7408. (beg (save-excursion
  7409. (skip-chars-backward "a-zA-Z0-9_:$")
  7410. (point)))
  7411. (confirm (lambda (x) (stringp (car x))))
  7412. (searchhead (equal (char-before beg) ?*))
  7413. (struct
  7414. (when (and (member (char-before beg1) '(?. ?<))
  7415. (setq a (assoc (buffer-substring beg1 (point))
  7416. org-structure-template-alist)))
  7417. (org-complete-expand-structure-template (1- beg1) a)
  7418. (throw 'exit t)))
  7419. (tag (and (equal (char-before beg1) ?:)
  7420. (equal (char-after (point-at-bol)) ?*)))
  7421. (prop (and (equal (char-before beg1) ?:)
  7422. (not (equal (char-after (point-at-bol)) ?*))))
  7423. (texp (equal (char-before beg) ?\\))
  7424. (link (equal (char-before beg) ?\[))
  7425. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7426. beg)
  7427. "#+"))
  7428. (startup (string-match "^#\\+STARTUP:.*"
  7429. (buffer-substring (point-at-bol) (point))))
  7430. (completion-ignore-case opt)
  7431. (type nil)
  7432. (tbl nil)
  7433. (table (cond
  7434. (opt
  7435. (setq type :opt)
  7436. (require 'org-exp)
  7437. (append
  7438. (mapcar
  7439. (lambda (x)
  7440. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7441. (cons (match-string 2 x) (match-string 1 x)))
  7442. (org-split-string (org-get-current-options) "\n"))
  7443. (mapcar 'list org-additional-option-like-keywords)))
  7444. (startup
  7445. (setq type :startup)
  7446. org-startup-options)
  7447. (link (append org-link-abbrev-alist-local
  7448. org-link-abbrev-alist))
  7449. (texp
  7450. (setq type :tex)
  7451. org-html-entities)
  7452. ((string-match "\\`\\*+[ \t]+\\'"
  7453. (buffer-substring (point-at-bol) beg))
  7454. (setq type :todo)
  7455. (mapcar 'list org-todo-keywords-1))
  7456. (searchhead
  7457. (setq type :searchhead)
  7458. (save-excursion
  7459. (goto-char (point-min))
  7460. (while (re-search-forward org-todo-line-regexp nil t)
  7461. (push (list
  7462. (org-make-org-heading-search-string
  7463. (match-string 3) t))
  7464. tbl)))
  7465. tbl)
  7466. (tag (setq type :tag beg beg1)
  7467. (or org-tag-alist (org-get-buffer-tags)))
  7468. (prop (setq type :prop beg beg1)
  7469. (mapcar 'list (org-buffer-property-keys nil t t)))
  7470. (t (progn
  7471. (call-interactively org-completion-fallback-command)
  7472. (throw 'exit nil)))))
  7473. (pattern (buffer-substring-no-properties beg end))
  7474. (completion (try-completion pattern table confirm)))
  7475. (cond ((eq completion t)
  7476. (if (not (assoc (upcase pattern) table))
  7477. (message "Already complete")
  7478. (if (and (equal type :opt)
  7479. (not (member (car (assoc (upcase pattern) table))
  7480. org-additional-option-like-keywords)))
  7481. (insert (substring (cdr (assoc (upcase pattern) table))
  7482. (length pattern)))
  7483. (if (memq type '(:tag :prop)) (insert ":")))))
  7484. ((null completion)
  7485. (message "Can't find completion for \"%s\"" pattern)
  7486. (ding))
  7487. ((not (string= pattern completion))
  7488. (delete-region beg end)
  7489. (if (string-match " +$" completion)
  7490. (setq completion (replace-match "" t t completion)))
  7491. (insert completion)
  7492. (if (get-buffer-window "*Completions*")
  7493. (delete-window (get-buffer-window "*Completions*")))
  7494. (if (assoc completion table)
  7495. (if (eq type :todo) (insert " ")
  7496. (if (memq type '(:tag :prop)) (insert ":"))))
  7497. (if (and (equal type :opt) (assoc completion table))
  7498. (message "%s" (substitute-command-keys
  7499. "Press \\[org-complete] again to insert example settings"))))
  7500. (t
  7501. (message "Making completion list...")
  7502. (let ((list (sort (all-completions pattern table confirm)
  7503. 'string<)))
  7504. (with-output-to-temp-buffer "*Completions*"
  7505. (condition-case nil
  7506. ;; Protection needed for XEmacs and emacs 21
  7507. (display-completion-list list pattern)
  7508. (error (display-completion-list list)))))
  7509. (message "Making completion list...%s" "done")))))))
  7510. ;;;; TODO, DEADLINE, Comments
  7511. (defun org-toggle-comment ()
  7512. "Change the COMMENT state of an entry."
  7513. (interactive)
  7514. (save-excursion
  7515. (org-back-to-heading)
  7516. (let (case-fold-search)
  7517. (if (looking-at (concat outline-regexp
  7518. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7519. (replace-match "" t t nil 1)
  7520. (if (looking-at outline-regexp)
  7521. (progn
  7522. (goto-char (match-end 0))
  7523. (insert org-comment-string " ")))))))
  7524. (defvar org-last-todo-state-is-todo nil
  7525. "This is non-nil when the last TODO state change led to a TODO state.
  7526. If the last change removed the TODO tag or switched to DONE, then
  7527. this is nil.")
  7528. (defvar org-setting-tags nil) ; dynamically skipped
  7529. (defun org-parse-local-options (string var)
  7530. "Parse STRING for startup setting relevant for variable VAR."
  7531. (let ((rtn (symbol-value var))
  7532. e opts)
  7533. (save-match-data
  7534. (if (or (not string) (not (string-match "\\S-" string)))
  7535. rtn
  7536. (setq opts (delq nil (mapcar (lambda (x)
  7537. (setq e (assoc x org-startup-options))
  7538. (if (eq (nth 1 e) var) e nil))
  7539. (org-split-string string "[ \t]+"))))
  7540. (if (not opts)
  7541. rtn
  7542. (setq rtn nil)
  7543. (while (setq e (pop opts))
  7544. (if (not (nth 3 e))
  7545. (setq rtn (nth 2 e))
  7546. (if (not (listp rtn)) (setq rtn nil))
  7547. (push (nth 2 e) rtn)))
  7548. rtn)))))
  7549. (defvar org-agenda-headline-snapshot-before-repeat)
  7550. (defun org-todo (&optional arg)
  7551. "Change the TODO state of an item.
  7552. The state of an item is given by a keyword at the start of the heading,
  7553. like
  7554. *** TODO Write paper
  7555. *** DONE Call mom
  7556. The different keywords are specified in the variable `org-todo-keywords'.
  7557. By default the available states are \"TODO\" and \"DONE\".
  7558. So for this example: when the item starts with TODO, it is changed to DONE.
  7559. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7560. DONE are present, add TODO at the beginning of the heading.
  7561. With C-u prefix arg, use completion to determine the new state.
  7562. With numeric prefix arg, switch to that state.
  7563. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7564. With a tripple C-u prefix, circumvent any state blocking.
  7565. For calling through lisp, arg is also interpreted in the following way:
  7566. 'none -> empty state
  7567. \"\"(empty string) -> switch to empty state
  7568. 'done -> switch to DONE
  7569. 'nextset -> switch to the next set of keywords
  7570. 'previousset -> switch to the previous set of keywords
  7571. \"WAITING\" -> switch to the specified keyword, but only if it
  7572. really is a member of `org-todo-keywords'."
  7573. (interactive "P")
  7574. (if (equal arg '(16)) (setq arg 'nextset))
  7575. (let ((org-blocker-hook org-blocker-hook))
  7576. (when (equal arg '(64))
  7577. (setq arg nil org-blocker-hook nil))
  7578. (save-excursion
  7579. (catch 'exit
  7580. (org-back-to-heading)
  7581. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7582. (or (looking-at (concat " +" org-todo-regexp " *"))
  7583. (looking-at " *"))
  7584. (let* ((match-data (match-data))
  7585. (startpos (point-at-bol))
  7586. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7587. (org-log-done org-log-done)
  7588. (org-log-repeat org-log-repeat)
  7589. (org-todo-log-states org-todo-log-states)
  7590. (this (match-string 1))
  7591. (hl-pos (match-beginning 0))
  7592. (head (org-get-todo-sequence-head this))
  7593. (ass (assoc head org-todo-kwd-alist))
  7594. (interpret (nth 1 ass))
  7595. (done-word (nth 3 ass))
  7596. (final-done-word (nth 4 ass))
  7597. (last-state (or this ""))
  7598. (completion-ignore-case t)
  7599. (member (member this org-todo-keywords-1))
  7600. (tail (cdr member))
  7601. (state (cond
  7602. ((and org-todo-key-trigger
  7603. (or (and (equal arg '(4))
  7604. (eq org-use-fast-todo-selection 'prefix))
  7605. (and (not arg) org-use-fast-todo-selection
  7606. (not (eq org-use-fast-todo-selection
  7607. 'prefix)))))
  7608. ;; Use fast selection
  7609. (org-fast-todo-selection))
  7610. ((and (equal arg '(4))
  7611. (or (not org-use-fast-todo-selection)
  7612. (not org-todo-key-trigger)))
  7613. ;; Read a state with completion
  7614. (org-ido-completing-read
  7615. "State: " (mapcar (lambda(x) (list x))
  7616. org-todo-keywords-1)
  7617. nil t))
  7618. ((eq arg 'right)
  7619. (if this
  7620. (if tail (car tail) nil)
  7621. (car org-todo-keywords-1)))
  7622. ((eq arg 'left)
  7623. (if (equal member org-todo-keywords-1)
  7624. nil
  7625. (if this
  7626. (nth (- (length org-todo-keywords-1)
  7627. (length tail) 2)
  7628. org-todo-keywords-1)
  7629. (org-last org-todo-keywords-1))))
  7630. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7631. (setq arg nil))) ; hack to fall back to cycling
  7632. (arg
  7633. ;; user or caller requests a specific state
  7634. (cond
  7635. ((equal arg "") nil)
  7636. ((eq arg 'none) nil)
  7637. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7638. ((eq arg 'nextset)
  7639. (or (car (cdr (member head org-todo-heads)))
  7640. (car org-todo-heads)))
  7641. ((eq arg 'previousset)
  7642. (let ((org-todo-heads (reverse org-todo-heads)))
  7643. (or (car (cdr (member head org-todo-heads)))
  7644. (car org-todo-heads))))
  7645. ((car (member arg org-todo-keywords-1)))
  7646. ((nth (1- (prefix-numeric-value arg))
  7647. org-todo-keywords-1))))
  7648. ((null member) (or head (car org-todo-keywords-1)))
  7649. ((equal this final-done-word) nil) ;; -> make empty
  7650. ((null tail) nil) ;; -> first entry
  7651. ((eq interpret 'sequence)
  7652. (car tail))
  7653. ((memq interpret '(type priority))
  7654. (if (eq this-command last-command)
  7655. (car tail)
  7656. (if (> (length tail) 0)
  7657. (or done-word (car org-done-keywords))
  7658. nil)))
  7659. (t nil)))
  7660. (next (if state (concat " " state " ") " "))
  7661. (change-plist (list :type 'todo-state-change :from this :to state
  7662. :position startpos))
  7663. dolog now-done-p)
  7664. (when org-blocker-hook
  7665. (setq org-last-todo-state-is-todo
  7666. (not (member this org-done-keywords)))
  7667. (unless (save-excursion
  7668. (save-match-data
  7669. (run-hook-with-args-until-failure
  7670. 'org-blocker-hook change-plist)))
  7671. (if (interactive-p)
  7672. (error "TODO state change from %s to %s blocked" this state)
  7673. ;; fail silently
  7674. (message "TODO state change from %s to %s blocked" this state)
  7675. (throw 'exit nil))))
  7676. (store-match-data match-data)
  7677. (replace-match next t t)
  7678. (unless (pos-visible-in-window-p hl-pos)
  7679. (message "TODO state changed to %s" (org-trim next)))
  7680. (unless head
  7681. (setq head (org-get-todo-sequence-head state)
  7682. ass (assoc head org-todo-kwd-alist)
  7683. interpret (nth 1 ass)
  7684. done-word (nth 3 ass)
  7685. final-done-word (nth 4 ass)))
  7686. (when (memq arg '(nextset previousset))
  7687. (message "Keyword-Set %d/%d: %s"
  7688. (- (length org-todo-sets) -1
  7689. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7690. (length org-todo-sets)
  7691. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7692. (setq org-last-todo-state-is-todo
  7693. (not (member state org-done-keywords)))
  7694. (setq now-done-p (and (member state org-done-keywords)
  7695. (not (member this org-done-keywords))))
  7696. (and logging (org-local-logging logging))
  7697. (when (and (or org-todo-log-states org-log-done)
  7698. (not (memq arg '(nextset previousset))))
  7699. ;; we need to look at recording a time and note
  7700. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7701. (nth 2 (assoc this org-todo-log-states))))
  7702. (when (and state
  7703. (member state org-not-done-keywords)
  7704. (not (member this org-not-done-keywords)))
  7705. ;; This is now a todo state and was not one before
  7706. ;; If there was a CLOSED time stamp, get rid of it.
  7707. (org-add-planning-info nil nil 'closed))
  7708. (when (and now-done-p org-log-done)
  7709. ;; It is now done, and it was not done before
  7710. (org-add-planning-info 'closed (org-current-time))
  7711. (if (and (not dolog) (eq 'note org-log-done))
  7712. (org-add-log-setup 'done state 'findpos 'note)))
  7713. (when (and state dolog)
  7714. ;; This is a non-nil state, and we need to log it
  7715. (org-add-log-setup 'state state 'findpos dolog)))
  7716. ;; Fixup tag positioning
  7717. (org-todo-trigger-tag-changes state)
  7718. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7719. (when org-provide-todo-statistics
  7720. (org-update-parent-todo-statistics))
  7721. (run-hooks 'org-after-todo-state-change-hook)
  7722. (if (and arg (not (member state org-done-keywords)))
  7723. (setq head (org-get-todo-sequence-head state)))
  7724. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7725. ;; Do we need to trigger a repeat?
  7726. (when now-done-p
  7727. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7728. ;; This is for the agenda, take a snapshot of the headline.
  7729. (save-match-data
  7730. (setq org-agenda-headline-snapshot-before-repeat
  7731. (org-get-heading))))
  7732. (org-auto-repeat-maybe state))
  7733. ;; Fixup cursor location if close to the keyword
  7734. (if (and (outline-on-heading-p)
  7735. (not (bolp))
  7736. (save-excursion (beginning-of-line 1)
  7737. (looking-at org-todo-line-regexp))
  7738. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7739. (progn
  7740. (goto-char (or (match-end 2) (match-end 1)))
  7741. (just-one-space)))
  7742. (when org-trigger-hook
  7743. (save-excursion
  7744. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7745. (defun org-block-todo-from-children-or-siblings (change-plist)
  7746. "Block turning an entry into a TODO, using the hierarchy.
  7747. This checks whether the current task should be blocked from state
  7748. changes. Such blocking occurs when:
  7749. 1. The task has children which are not all in a completed state.
  7750. 2. A task has a parent with the property :ORDERED:, and there
  7751. are siblings prior to the current task with incomplete
  7752. status."
  7753. (catch 'dont-block
  7754. ;; If this is not a todo state change, or if this entry is already DONE,
  7755. ;; do not block
  7756. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7757. (member (plist-get change-plist :from)
  7758. (cons 'done org-done-keywords))
  7759. (member (plist-get change-plist :to)
  7760. (cons 'todo org-not-done-keywords)))
  7761. (throw 'dont-block t))
  7762. ;; If this task has children, and any are undone, it's blocked
  7763. (save-excursion
  7764. (org-back-to-heading t)
  7765. (let ((this-level (funcall outline-level)))
  7766. (outline-next-heading)
  7767. (let ((child-level (funcall outline-level)))
  7768. (while (and (not (eobp))
  7769. (> child-level this-level))
  7770. ;; this todo has children, check whether they are all
  7771. ;; completed
  7772. (if (and (not (org-entry-is-done-p))
  7773. (org-entry-is-todo-p))
  7774. (throw 'dont-block nil))
  7775. (outline-next-heading)
  7776. (setq child-level (funcall outline-level))))))
  7777. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7778. ;; any previous siblings are undone, it's blocked
  7779. (save-excursion
  7780. (org-back-to-heading t)
  7781. (when (save-excursion
  7782. (ignore-errors
  7783. (outline-up-heading 1)
  7784. (org-entry-get (point) "ORDERED")))
  7785. (let* ((this-level (funcall outline-level))
  7786. (current-level this-level))
  7787. (while (and (not (bobp))
  7788. (= current-level this-level))
  7789. (outline-previous-heading)
  7790. (setq current-level (funcall outline-level))
  7791. (if (= current-level this-level)
  7792. ;; this todo has children, check whether they are all
  7793. ;; completed
  7794. (if (and (not (org-entry-is-done-p))
  7795. (org-entry-is-todo-p))
  7796. (throw 'dont-block nil)))))))
  7797. t)) ; don't block
  7798. (defun org-toggle-ordered-property ()
  7799. "Toggle the ORDERED property of the current entry."
  7800. (interactive)
  7801. (save-excursion
  7802. (org-back-to-heading)
  7803. (if (org-entry-get nil "ORDERED")
  7804. (progn
  7805. (org-delete-property "ORDERED")
  7806. (message "Subtasks can be completed in arbitrary order or parallel"))
  7807. (org-entry-put nil "ORDERED" "t")
  7808. (message "Subtasks must be completed in sequence"))))
  7809. (defun org-block-todo-from-checkboxes (change-plist)
  7810. "Block turning an entry into a TODO, using checkboxes.
  7811. This checks whether the current task should be blocked from state
  7812. changes because there are uncheckd boxes in this entry."
  7813. (catch 'dont-block
  7814. ;; If this is not a todo state change, or if this entry is already DONE,
  7815. ;; do not block
  7816. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7817. (member (plist-get change-plist :from)
  7818. (cons 'done org-done-keywords))
  7819. (member (plist-get change-plist :to)
  7820. (cons 'todo org-not-done-keywords)))
  7821. (throw 'dont-block t))
  7822. ;; If this task has checkboxes that are not checked, it's blocked
  7823. (save-excursion
  7824. (org-back-to-heading t)
  7825. (let ((beg (point)) end)
  7826. (outline-next-heading)
  7827. (setq end (point))
  7828. (goto-char beg)
  7829. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7830. end t)
  7831. (throw 'dont-block nil))))
  7832. t)) ; do not block
  7833. (defun org-update-parent-todo-statistics ()
  7834. "Update any statistics cookie in the parent of the current headline."
  7835. (interactive)
  7836. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7837. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7838. (catch 'exit
  7839. (save-excursion
  7840. (setq level (org-up-heading-safe))
  7841. (unless level
  7842. (throw 'exit nil))
  7843. (while (re-search-forward box-re (point-at-eol) t)
  7844. (setq is-percent (match-end 2))
  7845. (save-match-data
  7846. (unless (outline-next-heading) (throw 'exit nil))
  7847. (while (looking-at org-todo-line-regexp)
  7848. (setq kwd (match-string 2))
  7849. (and kwd (setq cnt-all (1+ cnt-all)))
  7850. (and (member kwd org-done-keywords)
  7851. (setq cnt-done (1+ cnt-done)))
  7852. (condition-case nil
  7853. (org-forward-same-level 1)
  7854. (error (end-of-line 1)))))
  7855. (replace-match
  7856. (if is-percent
  7857. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7858. (format "[%d/%d]" cnt-done cnt-all))))
  7859. (run-hook-with-args 'org-after-todo-statistics-hook
  7860. cnt-done (- cnt-all cnt-done))))))
  7861. (defvar org-after-todo-statistics-hook nil
  7862. "Hook that is called after a TODO statistics cookie has been updated.
  7863. Each function is called with two arguments: the number of not-done entries
  7864. and the number of done entries.
  7865. For example, the following function, when added to this hook, will switch
  7866. an entry to DONE when all children are done, and back to TODO when new
  7867. entries are set to a TODO status. Note that this hook is only called
  7868. when there is a statistics cookie in the headline!
  7869. (defun org-summary-todo (n-done n-not-done)
  7870. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7871. (let (org-log-done org-log-states) ; turn off logging
  7872. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7873. ")
  7874. (defun org-todo-trigger-tag-changes (state)
  7875. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7876. (let ((l org-todo-state-tags-triggers)
  7877. changes)
  7878. (when (or (not state) (equal state ""))
  7879. (setq changes (append changes (cdr (assoc "" l)))))
  7880. (when (and (stringp state) (> (length state) 0))
  7881. (setq changes (append changes (cdr (assoc state l)))))
  7882. (when (member state org-not-done-keywords)
  7883. (setq changes (append changes (cdr (assoc 'todo l)))))
  7884. (when (member state org-done-keywords)
  7885. (setq changes (append changes (cdr (assoc 'done l)))))
  7886. (dolist (c changes)
  7887. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7888. (defun org-local-logging (value)
  7889. "Get logging settings from a property VALUE."
  7890. (let* (words w a)
  7891. ;; directly set the variables, they are already local.
  7892. (setq org-log-done nil
  7893. org-log-repeat nil
  7894. org-todo-log-states nil)
  7895. (setq words (org-split-string value))
  7896. (while (setq w (pop words))
  7897. (cond
  7898. ((setq a (assoc w org-startup-options))
  7899. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7900. (set (nth 1 a) (nth 2 a))))
  7901. ((setq a (org-extract-log-state-settings w))
  7902. (and (member (car a) org-todo-keywords-1)
  7903. (push a org-todo-log-states)))))))
  7904. (defun org-get-todo-sequence-head (kwd)
  7905. "Return the head of the TODO sequence to which KWD belongs.
  7906. If KWD is not set, check if there is a text property remembering the
  7907. right sequence."
  7908. (let (p)
  7909. (cond
  7910. ((not kwd)
  7911. (or (get-text-property (point-at-bol) 'org-todo-head)
  7912. (progn
  7913. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7914. nil (point-at-eol)))
  7915. (get-text-property p 'org-todo-head))))
  7916. ((not (member kwd org-todo-keywords-1))
  7917. (car org-todo-keywords-1))
  7918. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7919. (defun org-fast-todo-selection ()
  7920. "Fast TODO keyword selection with single keys.
  7921. Returns the new TODO keyword, or nil if no state change should occur."
  7922. (let* ((fulltable org-todo-key-alist)
  7923. (done-keywords org-done-keywords) ;; needed for the faces.
  7924. (maxlen (apply 'max (mapcar
  7925. (lambda (x)
  7926. (if (stringp (car x)) (string-width (car x)) 0))
  7927. fulltable)))
  7928. (expert nil)
  7929. (fwidth (+ maxlen 3 1 3))
  7930. (ncol (/ (- (window-width) 4) fwidth))
  7931. tg cnt e c tbl
  7932. groups ingroup)
  7933. (save-excursion
  7934. (save-window-excursion
  7935. (if expert
  7936. (set-buffer (get-buffer-create " *Org todo*"))
  7937. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7938. (erase-buffer)
  7939. (org-set-local 'org-done-keywords done-keywords)
  7940. (setq tbl fulltable cnt 0)
  7941. (while (setq e (pop tbl))
  7942. (cond
  7943. ((equal e '(:startgroup))
  7944. (push '() groups) (setq ingroup t)
  7945. (when (not (= cnt 0))
  7946. (setq cnt 0)
  7947. (insert "\n"))
  7948. (insert "{ "))
  7949. ((equal e '(:endgroup))
  7950. (setq ingroup nil cnt 0)
  7951. (insert "}\n"))
  7952. (t
  7953. (setq tg (car e) c (cdr e))
  7954. (if ingroup (push tg (car groups)))
  7955. (setq tg (org-add-props tg nil 'face
  7956. (org-get-todo-face tg)))
  7957. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7958. (insert "[" c "] " tg (make-string
  7959. (- fwidth 4 (length tg)) ?\ ))
  7960. (when (= (setq cnt (1+ cnt)) ncol)
  7961. (insert "\n")
  7962. (if ingroup (insert " "))
  7963. (setq cnt 0)))))
  7964. (insert "\n")
  7965. (goto-char (point-min))
  7966. (if (not expert) (org-fit-window-to-buffer))
  7967. (message "[a-z..]:Set [SPC]:clear")
  7968. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7969. (cond
  7970. ((or (= c ?\C-g)
  7971. (and (= c ?q) (not (rassoc c fulltable))))
  7972. (setq quit-flag t))
  7973. ((= c ?\ ) nil)
  7974. ((setq e (rassoc c fulltable) tg (car e))
  7975. tg)
  7976. (t (setq quit-flag t)))))))
  7977. (defun org-entry-is-todo-p ()
  7978. (member (org-get-todo-state) org-not-done-keywords))
  7979. (defun org-entry-is-done-p ()
  7980. (member (org-get-todo-state) org-done-keywords))
  7981. (defun org-get-todo-state ()
  7982. (save-excursion
  7983. (org-back-to-heading t)
  7984. (and (looking-at org-todo-line-regexp)
  7985. (match-end 2)
  7986. (match-string 2))))
  7987. (defun org-at-date-range-p (&optional inactive-ok)
  7988. "Is the cursor inside a date range?"
  7989. (interactive)
  7990. (save-excursion
  7991. (catch 'exit
  7992. (let ((pos (point)))
  7993. (skip-chars-backward "^[<\r\n")
  7994. (skip-chars-backward "<[")
  7995. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7996. (>= (match-end 0) pos)
  7997. (throw 'exit t))
  7998. (skip-chars-backward "^<[\r\n")
  7999. (skip-chars-backward "<[")
  8000. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8001. (>= (match-end 0) pos)
  8002. (throw 'exit t)))
  8003. nil)))
  8004. (defun org-get-repeat ()
  8005. "Check if there is a deadline/schedule with repeater in this entry."
  8006. (save-match-data
  8007. (save-excursion
  8008. (org-back-to-heading t)
  8009. (if (re-search-forward
  8010. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8011. (match-string 1)))))
  8012. (defvar org-last-changed-timestamp)
  8013. (defvar org-last-inserted-timestamp)
  8014. (defvar org-log-post-message)
  8015. (defvar org-log-note-purpose)
  8016. (defvar org-log-note-how)
  8017. (defvar org-log-note-extra)
  8018. (defun org-auto-repeat-maybe (done-word)
  8019. "Check if the current headline contains a repeated deadline/schedule.
  8020. If yes, set TODO state back to what it was and change the base date
  8021. of repeating deadline/scheduled time stamps to new date.
  8022. This function is run automatically after each state change to a DONE state."
  8023. ;; last-state is dynamically scoped into this function
  8024. (let* ((repeat (org-get-repeat))
  8025. (aa (assoc last-state org-todo-kwd-alist))
  8026. (interpret (nth 1 aa))
  8027. (head (nth 2 aa))
  8028. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8029. (msg "Entry repeats: ")
  8030. (org-log-done nil)
  8031. (org-todo-log-states nil)
  8032. (nshiftmax 10) (nshift 0)
  8033. re type n what ts time)
  8034. (when repeat
  8035. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8036. (org-todo (if (eq interpret 'type) last-state head))
  8037. (when org-log-repeat
  8038. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8039. (memq 'org-add-log-note post-command-hook))
  8040. ;; OK, we are already setup for some record
  8041. (if (eq org-log-repeat 'note)
  8042. ;; make sure we take a note, not only a time stamp
  8043. (setq org-log-note-how 'note))
  8044. ;; Set up for taking a record
  8045. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8046. 'findpos org-log-repeat)))
  8047. (org-back-to-heading t)
  8048. (org-add-planning-info nil nil 'closed)
  8049. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8050. org-deadline-time-regexp "\\)\\|\\("
  8051. org-ts-regexp "\\)"))
  8052. (while (re-search-forward
  8053. re (save-excursion (outline-next-heading) (point)) t)
  8054. (setq type (if (match-end 1) org-scheduled-string
  8055. (if (match-end 3) org-deadline-string "Plain:"))
  8056. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8057. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8058. (setq n (string-to-number (match-string 2 ts))
  8059. what (match-string 3 ts))
  8060. (if (equal what "w") (setq n (* n 7) what "d"))
  8061. ;; Preparation, see if we need to modify the start date for the change
  8062. (when (match-end 1)
  8063. (setq time (save-match-data (org-time-string-to-time ts)))
  8064. (cond
  8065. ((equal (match-string 1 ts) ".")
  8066. ;; Shift starting date to today
  8067. (org-timestamp-change
  8068. (- (time-to-days (current-time)) (time-to-days time))
  8069. 'day))
  8070. ((equal (match-string 1 ts) "+")
  8071. (while (or (= nshift 0)
  8072. (<= (time-to-days time) (time-to-days (current-time))))
  8073. (when (= (incf nshift) nshiftmax)
  8074. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8075. (error "Abort")))
  8076. (org-timestamp-change n (cdr (assoc what whata)))
  8077. (org-at-timestamp-p t)
  8078. (setq ts (match-string 1))
  8079. (setq time (save-match-data (org-time-string-to-time ts))))
  8080. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8081. ;; rematch, so that we have everything in place for the real shift
  8082. (org-at-timestamp-p t)
  8083. (setq ts (match-string 1))
  8084. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8085. (org-timestamp-change n (cdr (assoc what whata)))
  8086. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8087. (setq org-log-post-message msg)
  8088. (message "%s" msg))))
  8089. (defun org-show-todo-tree (arg)
  8090. "Make a compact tree which shows all headlines marked with TODO.
  8091. The tree will show the lines where the regexp matches, and all higher
  8092. headlines above the match.
  8093. With a \\[universal-argument] prefix, also show the DONE entries.
  8094. With a numeric prefix N, construct a sparse tree for the Nth element
  8095. of `org-todo-keywords-1'."
  8096. (interactive "P")
  8097. (let ((case-fold-search nil)
  8098. (kwd-re
  8099. (cond ((null arg) org-not-done-regexp)
  8100. ((equal arg '(4))
  8101. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8102. (mapcar 'list org-todo-keywords-1))))
  8103. (concat "\\("
  8104. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8105. "\\)\\>")))
  8106. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8107. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8108. org-todo-keywords-1)))
  8109. (t (error "Invalid prefix argument: %s" arg)))))
  8110. (message "%d TODO entries found"
  8111. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8112. (defun org-deadline (&optional remove time)
  8113. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8114. With argument REMOVE, remove any deadline from the item.
  8115. When TIME is set, it should be an internal time specification, and the
  8116. scheduling will use the corresponding date."
  8117. (interactive "P")
  8118. (if remove
  8119. (progn
  8120. (org-remove-timestamp-with-keyword org-deadline-string)
  8121. (message "Item no longer has a deadline."))
  8122. (if (org-get-repeat)
  8123. (error "Cannot change deadline on task with repeater, please do that by hand")
  8124. (org-add-planning-info 'deadline time 'closed)
  8125. (message "Deadline on %s" org-last-inserted-timestamp))))
  8126. (defun org-schedule (&optional remove time)
  8127. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8128. With argument REMOVE, remove any scheduling date from the item.
  8129. When TIME is set, it should be an internal time specification, and the
  8130. scheduling will use the corresponding date."
  8131. (interactive "P")
  8132. (if remove
  8133. (progn
  8134. (org-remove-timestamp-with-keyword org-scheduled-string)
  8135. (message "Item is no longer scheduled."))
  8136. (if (org-get-repeat)
  8137. (error "Cannot reschedule task with repeater, please do that by hand")
  8138. (org-add-planning-info 'scheduled time 'closed)
  8139. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8140. (defun org-remove-timestamp-with-keyword (keyword)
  8141. "Remove all time stamps with KEYWORD in the current entry."
  8142. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8143. beg)
  8144. (save-excursion
  8145. (org-back-to-heading t)
  8146. (setq beg (point))
  8147. (org-end-of-subtree t t)
  8148. (while (re-search-backward re beg t)
  8149. (replace-match "")
  8150. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8151. (equal (char-before) ?\ ))
  8152. (backward-delete-char 1)
  8153. (if (string-match "^[ \t]*$" (buffer-substring
  8154. (point-at-bol) (point-at-eol)))
  8155. (delete-region (point-at-bol)
  8156. (min (point-max) (1+ (point-at-eol))))))))))
  8157. (defun org-add-planning-info (what &optional time &rest remove)
  8158. "Insert new timestamp with keyword in the line directly after the headline.
  8159. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8160. If non is given, the user is prompted for a date.
  8161. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8162. be removed."
  8163. (interactive)
  8164. (let (org-time-was-given org-end-time-was-given ts
  8165. end default-time default-input)
  8166. (when (and (not time) (memq what '(scheduled deadline)))
  8167. ;; Try to get a default date/time from existing timestamp
  8168. (save-excursion
  8169. (org-back-to-heading t)
  8170. (setq end (save-excursion (outline-next-heading) (point)))
  8171. (when (re-search-forward (if (eq what 'scheduled)
  8172. org-scheduled-time-regexp
  8173. org-deadline-time-regexp)
  8174. end t)
  8175. (setq ts (match-string 1)
  8176. default-time
  8177. (apply 'encode-time (org-parse-time-string ts))
  8178. default-input (and ts (org-get-compact-tod ts))))))
  8179. (when what
  8180. ;; If necessary, get the time from the user
  8181. (setq time (or time (org-read-date nil 'to-time nil nil
  8182. default-time default-input))))
  8183. (when (and org-insert-labeled-timestamps-at-point
  8184. (member what '(scheduled deadline)))
  8185. (insert
  8186. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8187. (org-insert-time-stamp time org-time-was-given
  8188. nil nil nil (list org-end-time-was-given))
  8189. (setq what nil))
  8190. (save-excursion
  8191. (save-restriction
  8192. (let (col list elt ts buffer-invisibility-spec)
  8193. (org-back-to-heading t)
  8194. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8195. (goto-char (match-end 1))
  8196. (setq col (current-column))
  8197. (goto-char (match-end 0))
  8198. (if (eobp) (insert "\n") (forward-char 1))
  8199. (if (and (not (looking-at outline-regexp))
  8200. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8201. "[^\r\n]*"))
  8202. (not (equal (match-string 1) org-clock-string)))
  8203. (narrow-to-region (match-beginning 0) (match-end 0))
  8204. (insert-before-markers "\n")
  8205. (backward-char 1)
  8206. (narrow-to-region (point) (point))
  8207. (and org-adapt-indentation (org-indent-to-column col)))
  8208. ;; Check if we have to remove something.
  8209. (setq list (cons what remove))
  8210. (while list
  8211. (setq elt (pop list))
  8212. (goto-char (point-min))
  8213. (when (or (and (eq elt 'scheduled)
  8214. (re-search-forward org-scheduled-time-regexp nil t))
  8215. (and (eq elt 'deadline)
  8216. (re-search-forward org-deadline-time-regexp nil t))
  8217. (and (eq elt 'closed)
  8218. (re-search-forward org-closed-time-regexp nil t)))
  8219. (replace-match "")
  8220. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8221. (if (looking-at " +") (replace-match ""))))
  8222. (goto-char (point-max))
  8223. (when what
  8224. (insert
  8225. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8226. (cond ((eq what 'scheduled) org-scheduled-string)
  8227. ((eq what 'deadline) org-deadline-string)
  8228. ((eq what 'closed) org-closed-string))
  8229. " ")
  8230. (setq ts (org-insert-time-stamp
  8231. time
  8232. (or org-time-was-given
  8233. (and (eq what 'closed) org-log-done-with-time))
  8234. (eq what 'closed)
  8235. nil nil (list org-end-time-was-given)))
  8236. (end-of-line 1))
  8237. (goto-char (point-min))
  8238. (widen)
  8239. (if (and (looking-at "[ \t]+\n")
  8240. (equal (char-before) ?\n))
  8241. (delete-region (1- (point)) (point-at-eol)))
  8242. ts)))))
  8243. (defvar org-log-note-marker (make-marker))
  8244. (defvar org-log-note-purpose nil)
  8245. (defvar org-log-note-state nil)
  8246. (defvar org-log-note-how nil)
  8247. (defvar org-log-note-extra nil)
  8248. (defvar org-log-note-window-configuration nil)
  8249. (defvar org-log-note-return-to (make-marker))
  8250. (defvar org-log-post-message nil
  8251. "Message to be displayed after a log note has been stored.
  8252. The auto-repeater uses this.")
  8253. (defun org-add-note ()
  8254. "Add a note to the current entry.
  8255. This is done in the same way as adding a state change note."
  8256. (interactive)
  8257. (org-add-log-setup 'note nil 'findpos nil))
  8258. (defvar org-property-end-re)
  8259. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  8260. "Set up the post command hook to take a note.
  8261. If this is about to TODO state change, the new state is expected in STATE.
  8262. When FINDPOS is non-nil, find the correct position for the note in
  8263. the current entry. If not, assume that it can be inserted at point.
  8264. HOW is an indicator what kind of note should be created.
  8265. EXTRA is additional text that will be inserted into the notes buffer."
  8266. (save-restriction
  8267. (save-excursion
  8268. (when findpos
  8269. (org-back-to-heading t)
  8270. (narrow-to-region (point) (save-excursion
  8271. (outline-next-heading) (point)))
  8272. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8273. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8274. "[^\r\n]*\\)?"))
  8275. (goto-char (match-end 0))
  8276. (when (and org-log-state-notes-insert-after-drawers
  8277. (save-excursion
  8278. (forward-line) (looking-at org-drawer-regexp)))
  8279. (progn (forward-line)
  8280. (while (looking-at org-drawer-regexp)
  8281. (goto-char (match-end 0))
  8282. (re-search-forward org-property-end-re (point-max) t)
  8283. (forward-line))
  8284. (forward-line -1)))
  8285. (unless org-log-states-order-reversed
  8286. (and (= (char-after) ?\n) (forward-char 1))
  8287. (org-skip-over-state-notes)
  8288. (skip-chars-backward " \t\n\r")))
  8289. (move-marker org-log-note-marker (point))
  8290. (setq org-log-note-purpose purpose
  8291. org-log-note-state state
  8292. org-log-note-how how
  8293. org-log-note-extra extra)
  8294. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  8295. (defun org-skip-over-state-notes ()
  8296. "Skip past the list of State notes in an entry."
  8297. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8298. (while (looking-at "[ \t]*- State")
  8299. (condition-case nil
  8300. (org-next-item)
  8301. (error (org-end-of-item)))))
  8302. (defun org-add-log-note (&optional purpose)
  8303. "Pop up a window for taking a note, and add this note later at point."
  8304. (remove-hook 'post-command-hook 'org-add-log-note)
  8305. (setq org-log-note-window-configuration (current-window-configuration))
  8306. (delete-other-windows)
  8307. (move-marker org-log-note-return-to (point))
  8308. (switch-to-buffer (marker-buffer org-log-note-marker))
  8309. (goto-char org-log-note-marker)
  8310. (org-switch-to-buffer-other-window "*Org Note*")
  8311. (erase-buffer)
  8312. (if (memq org-log-note-how '(time state))
  8313. (let (current-prefix-arg) (org-store-log-note))
  8314. (let ((org-inhibit-startup t)) (org-mode))
  8315. (insert (format "# Insert note for %s.
  8316. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8317. (cond
  8318. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8319. ((eq org-log-note-purpose 'done) "closed todo item")
  8320. ((eq org-log-note-purpose 'state)
  8321. (format "state change to \"%s\"" org-log-note-state))
  8322. ((eq org-log-note-purpose 'note)
  8323. "this entry")
  8324. (t (error "This should not happen")))))
  8325. (if org-log-note-extra (insert org-log-note-extra))
  8326. (org-set-local 'org-finish-function 'org-store-log-note)))
  8327. (defvar org-note-abort nil) ; dynamically scoped
  8328. (defun org-store-log-note ()
  8329. "Finish taking a log note, and insert it to where it belongs."
  8330. (let ((txt (buffer-string))
  8331. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8332. lines ind)
  8333. (kill-buffer (current-buffer))
  8334. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8335. (setq txt (replace-match "" t t txt)))
  8336. (if (string-match "\\s-+\\'" txt)
  8337. (setq txt (replace-match "" t t txt)))
  8338. (setq lines (org-split-string txt "\n"))
  8339. (when (and note (string-match "\\S-" note))
  8340. (setq note
  8341. (org-replace-escapes
  8342. note
  8343. (list (cons "%u" (user-login-name))
  8344. (cons "%U" user-full-name)
  8345. (cons "%t" (format-time-string
  8346. (org-time-stamp-format 'long 'inactive)
  8347. (current-time)))
  8348. (cons "%s" (if org-log-note-state
  8349. (concat "\"" org-log-note-state "\"")
  8350. "")))))
  8351. (if lines (setq note (concat note " \\\\")))
  8352. (push note lines))
  8353. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8354. (when lines
  8355. (save-excursion
  8356. (set-buffer (marker-buffer org-log-note-marker))
  8357. (save-excursion
  8358. (goto-char org-log-note-marker)
  8359. (move-marker org-log-note-marker nil)
  8360. (end-of-line 1)
  8361. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8362. (indent-relative nil)
  8363. (insert "- " (pop lines))
  8364. (org-indent-line-function)
  8365. (beginning-of-line 1)
  8366. (looking-at "[ \t]*")
  8367. (setq ind (concat (match-string 0) " "))
  8368. (end-of-line 1)
  8369. (while lines (insert "\n" ind (pop lines)))))))
  8370. (set-window-configuration org-log-note-window-configuration)
  8371. (with-current-buffer (marker-buffer org-log-note-return-to)
  8372. (goto-char org-log-note-return-to))
  8373. (move-marker org-log-note-return-to nil)
  8374. (and org-log-post-message (message "%s" org-log-post-message)))
  8375. (defun org-sparse-tree (&optional arg)
  8376. "Create a sparse tree, prompt for the details.
  8377. This command can create sparse trees. You first need to select the type
  8378. of match used to create the tree:
  8379. t Show entries with a specific TODO keyword.
  8380. T Show entries selected by a tags match.
  8381. p Enter a property name and its value (both with completion on existing
  8382. names/values) and show entries with that property.
  8383. r Show entries matching a regular expression
  8384. d Show deadlines due within `org-deadline-warning-days'."
  8385. (interactive "P")
  8386. (let (ans kwd value)
  8387. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8388. (setq ans (read-char-exclusive))
  8389. (cond
  8390. ((equal ans ?d)
  8391. (call-interactively 'org-check-deadlines))
  8392. ((equal ans ?b)
  8393. (call-interactively 'org-check-before-date))
  8394. ((equal ans ?t)
  8395. (org-show-todo-tree '(4)))
  8396. ((equal ans ?T)
  8397. (call-interactively 'org-tags-sparse-tree))
  8398. ((member ans '(?p ?P))
  8399. (setq kwd (org-ido-completing-read "Property: "
  8400. (mapcar 'list (org-buffer-property-keys))))
  8401. (setq value (org-ido-completing-read "Value: "
  8402. (mapcar 'list (org-property-values kwd))))
  8403. (unless (string-match "\\`{.*}\\'" value)
  8404. (setq value (concat "\"" value "\"")))
  8405. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8406. ((member ans '(?r ?R ?/))
  8407. (call-interactively 'org-occur))
  8408. (t (error "No such sparse tree command \"%c\"" ans)))))
  8409. (defvar org-occur-highlights nil
  8410. "List of overlays used for occur matches.")
  8411. (make-variable-buffer-local 'org-occur-highlights)
  8412. (defvar org-occur-parameters nil
  8413. "Parameters of the active org-occur calls.
  8414. This is a list, each call to org-occur pushes as cons cell,
  8415. containing the regular expression and the callback, onto the list.
  8416. The list can contain several entries if `org-occur' has been called
  8417. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8418. will only contain one set of parameters. When the highlights are
  8419. removed (for example with `C-c C-c', or with the next edit (depending
  8420. on `org-remove-highlights-with-change'), this variable is emptied
  8421. as well.")
  8422. (make-variable-buffer-local 'org-occur-parameters)
  8423. (defun org-occur (regexp &optional keep-previous callback)
  8424. "Make a compact tree which shows all matches of REGEXP.
  8425. The tree will show the lines where the regexp matches, and all higher
  8426. headlines above the match. It will also show the heading after the match,
  8427. to make sure editing the matching entry is easy.
  8428. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8429. call to `org-occur' will be kept, to allow stacking of calls to this
  8430. command.
  8431. If CALLBACK is non-nil, it is a function which is called to confirm
  8432. that the match should indeed be shown."
  8433. (interactive "sRegexp: \nP")
  8434. (unless keep-previous
  8435. (org-remove-occur-highlights nil nil t))
  8436. (push (cons regexp callback) org-occur-parameters)
  8437. (let ((cnt 0))
  8438. (save-excursion
  8439. (goto-char (point-min))
  8440. (if (or (not keep-previous) ; do not want to keep
  8441. (not org-occur-highlights)) ; no previous matches
  8442. ;; hide everything
  8443. (org-overview))
  8444. (while (re-search-forward regexp nil t)
  8445. (when (or (not callback)
  8446. (save-match-data (funcall callback)))
  8447. (setq cnt (1+ cnt))
  8448. (when org-highlight-sparse-tree-matches
  8449. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8450. (org-show-context 'occur-tree))))
  8451. (when org-remove-highlights-with-change
  8452. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8453. nil 'local))
  8454. (unless org-sparse-tree-open-archived-trees
  8455. (org-hide-archived-subtrees (point-min) (point-max)))
  8456. (run-hooks 'org-occur-hook)
  8457. (if (interactive-p)
  8458. (message "%d match(es) for regexp %s" cnt regexp))
  8459. cnt))
  8460. (defun org-show-context (&optional key)
  8461. "Make sure point and context and visible.
  8462. How much context is shown depends upon the variables
  8463. `org-show-hierarchy-above', `org-show-following-heading'. and
  8464. `org-show-siblings'."
  8465. (let ((heading-p (org-on-heading-p t))
  8466. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8467. (following-p (org-get-alist-option org-show-following-heading key))
  8468. (entry-p (org-get-alist-option org-show-entry-below key))
  8469. (siblings-p (org-get-alist-option org-show-siblings key)))
  8470. (catch 'exit
  8471. ;; Show heading or entry text
  8472. (if (and heading-p (not entry-p))
  8473. (org-flag-heading nil) ; only show the heading
  8474. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8475. (org-show-hidden-entry))) ; show entire entry
  8476. (when following-p
  8477. ;; Show next sibling, or heading below text
  8478. (save-excursion
  8479. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8480. (org-flag-heading nil))))
  8481. (when siblings-p (org-show-siblings))
  8482. (when hierarchy-p
  8483. ;; show all higher headings, possibly with siblings
  8484. (save-excursion
  8485. (while (and (condition-case nil
  8486. (progn (org-up-heading-all 1) t)
  8487. (error nil))
  8488. (not (bobp)))
  8489. (org-flag-heading nil)
  8490. (when siblings-p (org-show-siblings))))))))
  8491. (defun org-reveal (&optional siblings)
  8492. "Show current entry, hierarchy above it, and the following headline.
  8493. This can be used to show a consistent set of context around locations
  8494. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8495. not t for the search context.
  8496. With optional argument SIBLINGS, on each level of the hierarchy all
  8497. siblings are shown. This repairs the tree structure to what it would
  8498. look like when opened with hierarchical calls to `org-cycle'."
  8499. (interactive "P")
  8500. (let ((org-show-hierarchy-above t)
  8501. (org-show-following-heading t)
  8502. (org-show-siblings (if siblings t org-show-siblings)))
  8503. (org-show-context nil)))
  8504. (defun org-highlight-new-match (beg end)
  8505. "Highlight from BEG to END and mark the highlight is an occur headline."
  8506. (let ((ov (org-make-overlay beg end)))
  8507. (org-overlay-put ov 'face 'secondary-selection)
  8508. (push ov org-occur-highlights)))
  8509. (defun org-remove-occur-highlights (&optional beg end noremove)
  8510. "Remove the occur highlights from the buffer.
  8511. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8512. from the `before-change-functions' in the current buffer."
  8513. (interactive)
  8514. (unless org-inhibit-highlight-removal
  8515. (mapc 'org-delete-overlay org-occur-highlights)
  8516. (setq org-occur-highlights nil)
  8517. (setq org-occur-parameters nil)
  8518. (unless noremove
  8519. (remove-hook 'before-change-functions
  8520. 'org-remove-occur-highlights 'local))))
  8521. ;;;; Priorities
  8522. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8523. "Regular expression matching the priority indicator.")
  8524. (defvar org-remove-priority-next-time nil)
  8525. (defun org-priority-up ()
  8526. "Increase the priority of the current item."
  8527. (interactive)
  8528. (org-priority 'up))
  8529. (defun org-priority-down ()
  8530. "Decrease the priority of the current item."
  8531. (interactive)
  8532. (org-priority 'down))
  8533. (defun org-priority (&optional action)
  8534. "Change the priority of an item by ARG.
  8535. ACTION can be `set', `up', `down', or a character."
  8536. (interactive)
  8537. (setq action (or action 'set))
  8538. (let (current new news have remove)
  8539. (save-excursion
  8540. (org-back-to-heading)
  8541. (if (looking-at org-priority-regexp)
  8542. (setq current (string-to-char (match-string 2))
  8543. have t)
  8544. (setq current org-default-priority))
  8545. (cond
  8546. ((or (eq action 'set)
  8547. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8548. (if (not (eq action 'set))
  8549. (setq new action)
  8550. (message "Priority %c-%c, SPC to remove: "
  8551. org-highest-priority org-lowest-priority)
  8552. (setq new (read-char-exclusive)))
  8553. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8554. (= (upcase org-lowest-priority) org-lowest-priority))
  8555. (setq new (upcase new)))
  8556. (cond ((equal new ?\ ) (setq remove t))
  8557. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8558. (error "Priority must be between `%c' and `%c'"
  8559. org-highest-priority org-lowest-priority))))
  8560. ((eq action 'up)
  8561. (if (and (not have) (eq last-command this-command))
  8562. (setq new org-lowest-priority)
  8563. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8564. org-default-priority (1- current)))))
  8565. ((eq action 'down)
  8566. (if (and (not have) (eq last-command this-command))
  8567. (setq new org-highest-priority)
  8568. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8569. org-default-priority (1+ current)))))
  8570. (t (error "Invalid action")))
  8571. (if (or (< (upcase new) org-highest-priority)
  8572. (> (upcase new) org-lowest-priority))
  8573. (setq remove t))
  8574. (setq news (format "%c" new))
  8575. (if have
  8576. (if remove
  8577. (replace-match "" t t nil 1)
  8578. (replace-match news t t nil 2))
  8579. (if remove
  8580. (error "No priority cookie found in line")
  8581. (looking-at org-todo-line-regexp)
  8582. (if (match-end 2)
  8583. (progn
  8584. (goto-char (match-end 2))
  8585. (insert " [#" news "]"))
  8586. (goto-char (match-beginning 3))
  8587. (insert "[#" news "] ")))))
  8588. (org-preserve-lc (org-set-tags nil 'align))
  8589. (if remove
  8590. (message "Priority removed")
  8591. (message "Priority of current item set to %s" news))))
  8592. (defun org-get-priority (s)
  8593. "Find priority cookie and return priority."
  8594. (save-match-data
  8595. (if (not (string-match org-priority-regexp s))
  8596. (* 1000 (- org-lowest-priority org-default-priority))
  8597. (* 1000 (- org-lowest-priority
  8598. (string-to-char (match-string 2 s)))))))
  8599. ;;;; Tags
  8600. (defvar org-agenda-archives-mode)
  8601. (defun org-scan-tags (action matcher &optional todo-only)
  8602. "Scan headline tags with inheritance and produce output ACTION.
  8603. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8604. or `agenda' to produce an entry list for an agenda view. It can also be
  8605. a Lisp form or a function that should be called at each matched headline, in
  8606. this case the return value is a list of all return values from these calls.
  8607. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8608. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8609. only lines with a TODO keyword are included in the output."
  8610. (require 'org-agenda)
  8611. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8612. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8613. (org-re
  8614. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8615. (props (list 'face 'default
  8616. 'done-face 'org-done
  8617. 'undone-face 'default
  8618. 'mouse-face 'highlight
  8619. 'org-not-done-regexp org-not-done-regexp
  8620. 'org-todo-regexp org-todo-regexp
  8621. 'keymap org-agenda-keymap
  8622. 'help-echo
  8623. (format "mouse-2 or RET jump to org file %s"
  8624. (abbreviate-file-name
  8625. (or (buffer-file-name (buffer-base-buffer))
  8626. (buffer-name (buffer-base-buffer)))))))
  8627. (case-fold-search nil)
  8628. lspos tags tags-list
  8629. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8630. (llast 0) rtn rtn1 level category i txt
  8631. todo marker entry priority)
  8632. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8633. (setq action (list 'lambda nil action)))
  8634. (save-excursion
  8635. (goto-char (point-min))
  8636. (when (eq action 'sparse-tree)
  8637. (org-overview)
  8638. (org-remove-occur-highlights))
  8639. (while (re-search-forward re nil t)
  8640. (catch :skip
  8641. (setq todo (if (match-end 1) (match-string 2))
  8642. tags (if (match-end 4) (match-string 4)))
  8643. (goto-char (setq lspos (1+ (match-beginning 0))))
  8644. (setq level (org-reduced-level (funcall outline-level))
  8645. category (org-get-category))
  8646. (setq i llast llast level)
  8647. ;; remove tag lists from same and sublevels
  8648. (while (>= i level)
  8649. (when (setq entry (assoc i tags-alist))
  8650. (setq tags-alist (delete entry tags-alist)))
  8651. (setq i (1- i)))
  8652. ;; add the next tags
  8653. (when tags
  8654. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8655. tags-alist
  8656. (cons (cons level tags) tags-alist)))
  8657. ;; compile tags for current headline
  8658. (setq tags-list
  8659. (if org-use-tag-inheritance
  8660. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8661. tags))
  8662. (when org-use-tag-inheritance
  8663. (setcdr (car tags-alist)
  8664. (mapcar (lambda (x)
  8665. (setq x (copy-sequence x))
  8666. (org-add-prop-inherited x))
  8667. (cdar tags-alist))))
  8668. (when (and tags org-use-tag-inheritance
  8669. (not (eq t org-use-tag-inheritance)))
  8670. ;; selective inheritance, remove uninherited ones
  8671. (setcdr (car tags-alist)
  8672. (org-remove-uniherited-tags (cdar tags-alist))))
  8673. (when (and (or (not todo-only)
  8674. (and (member todo org-not-done-keywords)
  8675. (or (not org-agenda-tags-todo-honor-ignore-options)
  8676. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8677. (let ((case-fold-search t)) (eval matcher))
  8678. (or
  8679. (not (member org-archive-tag tags-list))
  8680. ;; we have an archive tag, should we use this anyway?
  8681. (or (not org-agenda-skip-archived-trees)
  8682. (and (eq action 'agenda) org-agenda-archives-mode))))
  8683. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8684. ;; select this headline
  8685. (cond
  8686. ((eq action 'sparse-tree)
  8687. (and org-highlight-sparse-tree-matches
  8688. (org-get-heading) (match-end 0)
  8689. (org-highlight-new-match
  8690. (match-beginning 0) (match-beginning 1)))
  8691. (org-show-context 'tags-tree))
  8692. ((eq action 'agenda)
  8693. (setq txt (org-format-agenda-item
  8694. ""
  8695. (concat
  8696. (if org-tags-match-list-sublevels
  8697. (make-string (1- level) ?.) "")
  8698. (org-get-heading))
  8699. category (org-get-tags-at))
  8700. priority (org-get-priority txt))
  8701. (goto-char lspos)
  8702. (setq marker (org-agenda-new-marker))
  8703. (org-add-props txt props
  8704. 'org-marker marker 'org-hd-marker marker 'org-category category
  8705. 'priority priority 'type "tagsmatch")
  8706. (push txt rtn))
  8707. ((functionp action)
  8708. (save-excursion
  8709. (setq rtn1 (funcall action))
  8710. (push rtn1 rtn))
  8711. (goto-char (point-at-eol)))
  8712. (t (error "Invalid action")))
  8713. ;; if we are to skip sublevels, jump to end of subtree
  8714. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8715. (when (and (eq action 'sparse-tree)
  8716. (not org-sparse-tree-open-archived-trees))
  8717. (org-hide-archived-subtrees (point-min) (point-max)))
  8718. (nreverse rtn)))
  8719. (defun org-remove-uniherited-tags (tags)
  8720. "Remove all tags that are not inherited from the list TAGS."
  8721. (cond
  8722. ((eq org-use-tag-inheritance t)
  8723. (if org-tags-exclude-from-inheritance
  8724. (org-delete-all org-tags-exclude-from-inheritance tags)
  8725. tags))
  8726. ((not org-use-tag-inheritance) nil)
  8727. ((stringp org-use-tag-inheritance)
  8728. (delq nil (mapcar
  8729. (lambda (x)
  8730. (if (and (string-match org-use-tag-inheritance x)
  8731. (not (member x org-tags-exclude-from-inheritance)))
  8732. x nil))
  8733. tags)))
  8734. ((listp org-use-tag-inheritance)
  8735. (delq nil (mapcar
  8736. (lambda (x)
  8737. (if (member x org-use-tag-inheritance) x nil))
  8738. tags)))))
  8739. (defvar todo-only) ;; dynamically scoped
  8740. (defun org-tags-sparse-tree (&optional todo-only match)
  8741. "Create a sparse tree according to tags string MATCH.
  8742. MATCH can contain positive and negative selection of tags, like
  8743. \"+WORK+URGENT-WITHBOSS\".
  8744. If optional argument TODO-ONLY is non-nil, only select lines that are
  8745. also TODO lines."
  8746. (interactive "P")
  8747. (org-prepare-agenda-buffers (list (current-buffer)))
  8748. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8749. (defvar org-cached-props nil)
  8750. (defun org-cached-entry-get (pom property)
  8751. (if (or (eq t org-use-property-inheritance)
  8752. (and (stringp org-use-property-inheritance)
  8753. (string-match org-use-property-inheritance property))
  8754. (and (listp org-use-property-inheritance)
  8755. (member property org-use-property-inheritance)))
  8756. ;; Caching is not possible, check it directly
  8757. (org-entry-get pom property 'inherit)
  8758. ;; Get all properties, so that we can do complicated checks easily
  8759. (cdr (assoc property (or org-cached-props
  8760. (setq org-cached-props
  8761. (org-entry-properties pom)))))))
  8762. (defun org-global-tags-completion-table (&optional files)
  8763. "Return the list of all tags in all agenda buffer/files."
  8764. (save-excursion
  8765. (org-uniquify
  8766. (delq nil
  8767. (apply 'append
  8768. (mapcar
  8769. (lambda (file)
  8770. (set-buffer (find-file-noselect file))
  8771. (append (org-get-buffer-tags)
  8772. (mapcar (lambda (x) (if (stringp (car-safe x))
  8773. (list (car-safe x)) nil))
  8774. org-tag-alist)))
  8775. (if (and files (car files))
  8776. files
  8777. (org-agenda-files))))))))
  8778. (defun org-make-tags-matcher (match)
  8779. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8780. ;; todo-only is scoped dynamically into this function, and the function
  8781. ;; may change it if the matcher asks for it.
  8782. (unless match
  8783. ;; Get a new match request, with completion
  8784. (let ((org-last-tags-completion-table
  8785. (org-global-tags-completion-table)))
  8786. (setq match (org-completing-read
  8787. "Match: " 'org-tags-completion-function nil nil nil
  8788. 'org-tags-history))))
  8789. ;; Parse the string and create a lisp form
  8790. (let ((match0 match)
  8791. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8792. minus tag mm
  8793. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8794. orterms term orlist re-p str-p level-p level-op time-p
  8795. prop-p pn pv po cat-p gv rest)
  8796. (if (string-match "/+" match)
  8797. ;; match contains also a todo-matching request
  8798. (progn
  8799. (setq tagsmatch (substring match 0 (match-beginning 0))
  8800. todomatch (substring match (match-end 0)))
  8801. (if (string-match "^!" todomatch)
  8802. (setq todo-only t todomatch (substring todomatch 1)))
  8803. (if (string-match "^\\s-*$" todomatch)
  8804. (setq todomatch nil)))
  8805. ;; only matching tags
  8806. (setq tagsmatch match todomatch nil))
  8807. ;; Make the tags matcher
  8808. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8809. (setq tagsmatcher t)
  8810. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8811. (while (setq term (pop orterms))
  8812. (while (and (equal (substring term -1) "\\") orterms)
  8813. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8814. (while (string-match re term)
  8815. (setq rest (substring term (match-end 0))
  8816. minus (and (match-end 1)
  8817. (equal (match-string 1 term) "-"))
  8818. tag (match-string 2 term)
  8819. re-p (equal (string-to-char tag) ?{)
  8820. level-p (match-end 4)
  8821. prop-p (match-end 5)
  8822. mm (cond
  8823. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8824. (level-p
  8825. (setq level-op (org-op-to-function (match-string 3 term)))
  8826. `(,level-op level ,(string-to-number
  8827. (match-string 4 term))))
  8828. (prop-p
  8829. (setq pn (match-string 5 term)
  8830. po (match-string 6 term)
  8831. pv (match-string 7 term)
  8832. cat-p (equal pn "CATEGORY")
  8833. re-p (equal (string-to-char pv) ?{)
  8834. str-p (equal (string-to-char pv) ?\")
  8835. time-p (save-match-data
  8836. (string-match "^\"[[<].*[]>]\"$" pv))
  8837. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8838. (if time-p (setq pv (org-matcher-time pv)))
  8839. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8840. (cond
  8841. ((equal pn "CATEGORY")
  8842. (setq gv '(get-text-property (point) 'org-category)))
  8843. ((equal pn "TODO")
  8844. (setq gv 'todo))
  8845. (t
  8846. (setq gv `(org-cached-entry-get nil ,pn))))
  8847. (if re-p
  8848. (if (eq po 'org<>)
  8849. `(not (string-match ,pv (or ,gv "")))
  8850. `(string-match ,pv (or ,gv "")))
  8851. (if str-p
  8852. `(,po (or ,gv "") ,pv)
  8853. `(,po (string-to-number (or ,gv ""))
  8854. ,(string-to-number pv) ))))
  8855. (t `(member ,(downcase tag) tags-list)))
  8856. mm (if minus (list 'not mm) mm)
  8857. term rest)
  8858. (push mm tagsmatcher))
  8859. (push (if (> (length tagsmatcher) 1)
  8860. (cons 'and tagsmatcher)
  8861. (car tagsmatcher))
  8862. orlist)
  8863. (setq tagsmatcher nil))
  8864. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8865. (setq tagsmatcher
  8866. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8867. ;; Make the todo matcher
  8868. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8869. (setq todomatcher t)
  8870. (setq orterms (org-split-string todomatch "|") orlist nil)
  8871. (while (setq term (pop orterms))
  8872. (while (string-match re term)
  8873. (setq minus (and (match-end 1)
  8874. (equal (match-string 1 term) "-"))
  8875. kwd (match-string 2 term)
  8876. re-p (equal (string-to-char kwd) ?{)
  8877. term (substring term (match-end 0))
  8878. mm (if re-p
  8879. `(string-match ,(substring kwd 1 -1) todo)
  8880. (list 'equal 'todo kwd))
  8881. mm (if minus (list 'not mm) mm))
  8882. (push mm todomatcher))
  8883. (push (if (> (length todomatcher) 1)
  8884. (cons 'and todomatcher)
  8885. (car todomatcher))
  8886. orlist)
  8887. (setq todomatcher nil))
  8888. (setq todomatcher (if (> (length orlist) 1)
  8889. (cons 'or orlist) (car orlist))))
  8890. ;; Return the string and lisp forms of the matcher
  8891. (setq matcher (if todomatcher
  8892. (list 'and tagsmatcher todomatcher)
  8893. tagsmatcher))
  8894. (cons match0 matcher)))
  8895. (defun org-op-to-function (op &optional stringp)
  8896. "Turn an operator into the appropriate function."
  8897. (setq op
  8898. (cond
  8899. ((equal op "<" ) '(< string< org-time<))
  8900. ((equal op ">" ) '(> org-string> org-time>))
  8901. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8902. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8903. ((member op '("=" "==")) '(= string= org-time=))
  8904. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8905. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8906. (defun org<> (a b) (not (= a b)))
  8907. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8908. (defun org-string>= (a b) (not (string< a b)))
  8909. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8910. (defun org-string<> (a b) (not (string= a b)))
  8911. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  8912. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  8913. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  8914. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  8915. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  8916. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  8917. (defun org-2ft (s)
  8918. "Convert S to a floating point time.
  8919. If S is already a number, just return it. If it is a string, parse
  8920. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  8921. (cond
  8922. ((numberp s) s)
  8923. ((stringp s)
  8924. (condition-case nil
  8925. (float-time (apply 'encode-time (org-parse-time-string s)))
  8926. (error 0.)))
  8927. (t 0.)))
  8928. (defun org-time-today ()
  8929. "Time in seconds today at 0:00.
  8930. Returns the float number of seconds since the beginning of the
  8931. epoch to the beginning of today (00:00)."
  8932. (float-time (apply 'encode-time
  8933. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8934. (defun org-matcher-time (s)
  8935. "Interpret a time comparison value."
  8936. (save-match-data
  8937. (cond
  8938. ((string= s "<now>") (float-time))
  8939. ((string= s "<today>") (org-time-today))
  8940. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8941. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8942. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8943. (+ (org-time-today)
  8944. (* (string-to-number (match-string 1 s))
  8945. (cdr (assoc (match-string 2 s)
  8946. '(("d" . 86400.0) ("w" . 604800.0)
  8947. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8948. (t (org-2ft s)))))
  8949. (defun org-match-any-p (re list)
  8950. "Does re match any element of list?"
  8951. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8952. (delq nil list))
  8953. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8954. (defvar org-tags-overlay (org-make-overlay 1 1))
  8955. (org-detach-overlay org-tags-overlay)
  8956. (defun org-get-local-tags-at (&optional pos)
  8957. "Get a list of tags defined in the current headline."
  8958. (org-get-tags-at pos 'local))
  8959. (defun org-get-local-tags ()
  8960. "Get a list of tags defined in the current headline."
  8961. (org-get-tags-at nil 'local))
  8962. (defun org-get-tags-at (&optional pos local)
  8963. "Get a list of all headline tags applicable at POS.
  8964. POS defaults to point. If tags are inherited, the list contains
  8965. the targets in the same sequence as the headlines appear, i.e.
  8966. the tags of the current headline come last.
  8967. When LOCAL is non-nil, only return tags from the current headline,
  8968. ignore inherited ones."
  8969. (interactive)
  8970. (let (tags ltags lastpos parent)
  8971. (save-excursion
  8972. (save-restriction
  8973. (widen)
  8974. (goto-char (or pos (point)))
  8975. (save-match-data
  8976. (catch 'done
  8977. (condition-case nil
  8978. (progn
  8979. (org-back-to-heading t)
  8980. (while (not (equal lastpos (point)))
  8981. (setq lastpos (point))
  8982. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8983. (setq ltags (org-split-string
  8984. (org-match-string-no-properties 1) ":"))
  8985. (when parent
  8986. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8987. (setq tags (append
  8988. (if parent
  8989. (org-remove-uniherited-tags ltags)
  8990. ltags)
  8991. tags)))
  8992. (or org-use-tag-inheritance (throw 'done t))
  8993. (if local (throw 'done t))
  8994. (org-up-heading-all 1)
  8995. (setq parent t)))
  8996. (error nil)))))
  8997. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8998. (defun org-add-prop-inherited (s)
  8999. (add-text-properties 0 (length s) '(inherited t) s)
  9000. s)
  9001. (defun org-toggle-tag (tag &optional onoff)
  9002. "Toggle the tag TAG for the current line.
  9003. If ONOFF is `on' or `off', don't toggle but set to this state."
  9004. (let (res current)
  9005. (save-excursion
  9006. (org-back-to-heading t)
  9007. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9008. (point-at-eol) t)
  9009. (progn
  9010. (setq current (match-string 1))
  9011. (replace-match ""))
  9012. (setq current ""))
  9013. (setq current (nreverse (org-split-string current ":")))
  9014. (cond
  9015. ((eq onoff 'on)
  9016. (setq res t)
  9017. (or (member tag current) (push tag current)))
  9018. ((eq onoff 'off)
  9019. (or (not (member tag current)) (setq current (delete tag current))))
  9020. (t (if (member tag current)
  9021. (setq current (delete tag current))
  9022. (setq res t)
  9023. (push tag current))))
  9024. (end-of-line 1)
  9025. (if current
  9026. (progn
  9027. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9028. (org-set-tags nil t))
  9029. (delete-horizontal-space))
  9030. (run-hooks 'org-after-tags-change-hook))
  9031. res))
  9032. (defun org-align-tags-here (to-col)
  9033. ;; Assumes that this is a headline
  9034. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9035. (beginning-of-line 1)
  9036. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9037. (< pos (match-beginning 2)))
  9038. (progn
  9039. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9040. (goto-char (match-beginning 1))
  9041. (insert " ")
  9042. (delete-region (point) (1+ (match-beginning 2)))
  9043. (setq ncol (max (1+ (current-column))
  9044. (1+ col)
  9045. (if (> to-col 0)
  9046. to-col
  9047. (- (abs to-col) tags-l))))
  9048. (setq p (point))
  9049. (insert (make-string (- ncol (current-column)) ?\ ))
  9050. (setq ncol (current-column))
  9051. (when indent-tabs-mode (tabify p (point-at-eol)))
  9052. (org-move-to-column (min ncol col) t))
  9053. (goto-char pos))))
  9054. (defun org-set-tags-command (&optional arg just-align)
  9055. "Call the set-tags command for the current entry."
  9056. (interactive "P")
  9057. (if (org-on-heading-p)
  9058. (org-set-tags arg just-align)
  9059. (save-excursion
  9060. (org-back-to-heading t)
  9061. (org-set-tags arg just-align))))
  9062. (defun org-set-tags (&optional arg just-align)
  9063. "Set the tags for the current headline.
  9064. With prefix ARG, realign all tags in headings in the current buffer."
  9065. (interactive "P")
  9066. (let* ((re (concat "^" outline-regexp))
  9067. (current (org-get-tags-string))
  9068. (col (current-column))
  9069. (org-setting-tags t)
  9070. table current-tags inherited-tags ; computed below when needed
  9071. tags p0 c0 c1 rpl)
  9072. (if arg
  9073. (save-excursion
  9074. (goto-char (point-min))
  9075. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9076. (while (re-search-forward re nil t)
  9077. (org-set-tags nil t)
  9078. (end-of-line 1)))
  9079. (message "All tags realigned to column %d" org-tags-column))
  9080. (if just-align
  9081. (setq tags current)
  9082. ;; Get a new set of tags from the user
  9083. (save-excursion
  9084. (setq table (or org-tag-alist (org-get-buffer-tags))
  9085. org-last-tags-completion-table table
  9086. current-tags (org-split-string current ":")
  9087. inherited-tags (nreverse
  9088. (nthcdr (length current-tags)
  9089. (nreverse (org-get-tags-at))))
  9090. tags
  9091. (if (or (eq t org-use-fast-tag-selection)
  9092. (and org-use-fast-tag-selection
  9093. (delq nil (mapcar 'cdr table))))
  9094. (org-fast-tag-selection
  9095. current-tags inherited-tags table
  9096. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9097. (let ((org-add-colon-after-tag-completion t))
  9098. (org-trim
  9099. (org-without-partial-completion
  9100. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9101. nil nil current 'org-tags-history)))))))
  9102. (while (string-match "[-+&]+" tags)
  9103. ;; No boolean logic, just a list
  9104. (setq tags (replace-match ":" t t tags))))
  9105. (if (string-match "\\`[\t ]*\\'" tags)
  9106. (setq tags "")
  9107. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9108. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9109. ;; Insert new tags at the correct column
  9110. (beginning-of-line 1)
  9111. (cond
  9112. ((and (equal current "") (equal tags "")))
  9113. ((re-search-forward
  9114. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9115. (point-at-eol) t)
  9116. (if (equal tags "")
  9117. (setq rpl "")
  9118. (goto-char (match-beginning 0))
  9119. (setq c0 (current-column) p0 (point)
  9120. c1 (max (1+ c0) (if (> org-tags-column 0)
  9121. org-tags-column
  9122. (- (- org-tags-column) (length tags))))
  9123. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9124. (replace-match rpl t t)
  9125. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9126. tags)
  9127. (t (error "Tags alignment failed")))
  9128. (org-move-to-column col)
  9129. (unless just-align
  9130. (run-hooks 'org-after-tags-change-hook)))))
  9131. (defun org-change-tag-in-region (beg end tag off)
  9132. "Add or remove TAG for each entry in the region.
  9133. This works in the agenda, and also in an org-mode buffer."
  9134. (interactive
  9135. (list (region-beginning) (region-end)
  9136. (let ((org-last-tags-completion-table
  9137. (if (org-mode-p)
  9138. (org-get-buffer-tags)
  9139. (org-global-tags-completion-table))))
  9140. (org-ido-completing-read
  9141. "Tag: " 'org-tags-completion-function nil nil nil
  9142. 'org-tags-history))
  9143. (progn
  9144. (message "[s]et or [r]emove? ")
  9145. (equal (read-char-exclusive) ?r))))
  9146. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9147. (let ((agendap (equal major-mode 'org-agenda-mode))
  9148. l1 l2 m buf pos newhead (cnt 0))
  9149. (goto-char end)
  9150. (setq l2 (1- (org-current-line)))
  9151. (goto-char beg)
  9152. (setq l1 (org-current-line))
  9153. (loop for l from l1 to l2 do
  9154. (goto-line l)
  9155. (setq m (get-text-property (point) 'org-hd-marker))
  9156. (when (or (and (org-mode-p) (org-on-heading-p))
  9157. (and agendap m))
  9158. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9159. pos (if agendap m (point)))
  9160. (with-current-buffer buf
  9161. (save-excursion
  9162. (save-restriction
  9163. (goto-char pos)
  9164. (setq cnt (1+ cnt))
  9165. (org-toggle-tag tag (if off 'off 'on))
  9166. (setq newhead (org-get-heading)))))
  9167. (and agendap (org-agenda-change-all-lines newhead m))))
  9168. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9169. (defun org-tags-completion-function (string predicate &optional flag)
  9170. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9171. (confirm (lambda (x) (stringp (car x)))))
  9172. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9173. (setq s1 (match-string 1 string)
  9174. s2 (match-string 2 string))
  9175. (setq s1 "" s2 string))
  9176. (cond
  9177. ((eq flag nil)
  9178. ;; try completion
  9179. (setq rtn (try-completion s2 ctable confirm))
  9180. (if (stringp rtn)
  9181. (setq rtn
  9182. (concat s1 s2 (substring rtn (length s2))
  9183. (if (and org-add-colon-after-tag-completion
  9184. (assoc rtn ctable))
  9185. ":" ""))))
  9186. rtn)
  9187. ((eq flag t)
  9188. ;; all-completions
  9189. (all-completions s2 ctable confirm)
  9190. )
  9191. ((eq flag 'lambda)
  9192. ;; exact match?
  9193. (assoc s2 ctable)))
  9194. ))
  9195. (defun org-fast-tag-insert (kwd tags face &optional end)
  9196. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9197. (insert (format "%-12s" (concat kwd ":"))
  9198. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9199. (or end "")))
  9200. (defun org-fast-tag-show-exit (flag)
  9201. (save-excursion
  9202. (goto-line 3)
  9203. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9204. (replace-match ""))
  9205. (when flag
  9206. (end-of-line 1)
  9207. (org-move-to-column (- (window-width) 19) t)
  9208. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9209. (defun org-set-current-tags-overlay (current prefix)
  9210. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9211. (if (featurep 'xemacs)
  9212. (org-overlay-display org-tags-overlay (concat prefix s)
  9213. 'secondary-selection)
  9214. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9215. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9216. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9217. "Fast tag selection with single keys.
  9218. CURRENT is the current list of tags in the headline, INHERITED is the
  9219. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9220. possibly with grouping information. TODO-TABLE is a similar table with
  9221. TODO keywords, should these have keys assigned to them.
  9222. If the keys are nil, a-z are automatically assigned.
  9223. Returns the new tags string, or nil to not change the current settings."
  9224. (let* ((fulltable (append table todo-table))
  9225. (maxlen (apply 'max (mapcar
  9226. (lambda (x)
  9227. (if (stringp (car x)) (string-width (car x)) 0))
  9228. fulltable)))
  9229. (buf (current-buffer))
  9230. (expert (eq org-fast-tag-selection-single-key 'expert))
  9231. (buffer-tags nil)
  9232. (fwidth (+ maxlen 3 1 3))
  9233. (ncol (/ (- (window-width) 4) fwidth))
  9234. (i-face 'org-done)
  9235. (c-face 'org-todo)
  9236. tg cnt e c char c1 c2 ntable tbl rtn
  9237. ov-start ov-end ov-prefix
  9238. (exit-after-next org-fast-tag-selection-single-key)
  9239. (done-keywords org-done-keywords)
  9240. groups ingroup)
  9241. (save-excursion
  9242. (beginning-of-line 1)
  9243. (if (looking-at
  9244. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9245. (setq ov-start (match-beginning 1)
  9246. ov-end (match-end 1)
  9247. ov-prefix "")
  9248. (setq ov-start (1- (point-at-eol))
  9249. ov-end (1+ ov-start))
  9250. (skip-chars-forward "^\n\r")
  9251. (setq ov-prefix
  9252. (concat
  9253. (buffer-substring (1- (point)) (point))
  9254. (if (> (current-column) org-tags-column)
  9255. " "
  9256. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9257. (org-move-overlay org-tags-overlay ov-start ov-end)
  9258. (save-window-excursion
  9259. (if expert
  9260. (set-buffer (get-buffer-create " *Org tags*"))
  9261. (delete-other-windows)
  9262. (split-window-vertically)
  9263. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9264. (erase-buffer)
  9265. (org-set-local 'org-done-keywords done-keywords)
  9266. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9267. (org-fast-tag-insert "Current" current c-face "\n\n")
  9268. (org-fast-tag-show-exit exit-after-next)
  9269. (org-set-current-tags-overlay current ov-prefix)
  9270. (setq tbl fulltable char ?a cnt 0)
  9271. (while (setq e (pop tbl))
  9272. (cond
  9273. ((equal e '(:startgroup))
  9274. (push '() groups) (setq ingroup t)
  9275. (when (not (= cnt 0))
  9276. (setq cnt 0)
  9277. (insert "\n"))
  9278. (insert "{ "))
  9279. ((equal e '(:endgroup))
  9280. (setq ingroup nil cnt 0)
  9281. (insert "}\n"))
  9282. (t
  9283. (setq tg (car e) c2 nil)
  9284. (if (cdr e)
  9285. (setq c (cdr e))
  9286. ;; automatically assign a character.
  9287. (setq c1 (string-to-char
  9288. (downcase (substring
  9289. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9290. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9291. (while (or (rassoc char ntable) (rassoc char table))
  9292. (setq char (1+ char)))
  9293. (setq c2 c1))
  9294. (setq c (or c2 char)))
  9295. (if ingroup (push tg (car groups)))
  9296. (setq tg (org-add-props tg nil 'face
  9297. (cond
  9298. ((not (assoc tg table))
  9299. (org-get-todo-face tg))
  9300. ((member tg current) c-face)
  9301. ((member tg inherited) i-face)
  9302. (t nil))))
  9303. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9304. (insert "[" c "] " tg (make-string
  9305. (- fwidth 4 (length tg)) ?\ ))
  9306. (push (cons tg c) ntable)
  9307. (when (= (setq cnt (1+ cnt)) ncol)
  9308. (insert "\n")
  9309. (if ingroup (insert " "))
  9310. (setq cnt 0)))))
  9311. (setq ntable (nreverse ntable))
  9312. (insert "\n")
  9313. (goto-char (point-min))
  9314. (if (not expert) (org-fit-window-to-buffer))
  9315. (setq rtn
  9316. (catch 'exit
  9317. (while t
  9318. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9319. (if groups " [!] no groups" " [!]groups")
  9320. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9321. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9322. (cond
  9323. ((= c ?\r) (throw 'exit t))
  9324. ((= c ?!)
  9325. (setq groups (not groups))
  9326. (goto-char (point-min))
  9327. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9328. ((= c ?\C-c)
  9329. (if (not expert)
  9330. (org-fast-tag-show-exit
  9331. (setq exit-after-next (not exit-after-next)))
  9332. (setq expert nil)
  9333. (delete-other-windows)
  9334. (split-window-vertically)
  9335. (org-switch-to-buffer-other-window " *Org tags*")
  9336. (org-fit-window-to-buffer)))
  9337. ((or (= c ?\C-g)
  9338. (and (= c ?q) (not (rassoc c ntable))))
  9339. (org-detach-overlay org-tags-overlay)
  9340. (setq quit-flag t))
  9341. ((= c ?\ )
  9342. (setq current nil)
  9343. (if exit-after-next (setq exit-after-next 'now)))
  9344. ((= c ?\t)
  9345. (condition-case nil
  9346. (setq tg (org-ido-completing-read
  9347. "Tag: "
  9348. (or buffer-tags
  9349. (with-current-buffer buf
  9350. (org-get-buffer-tags)))))
  9351. (quit (setq tg "")))
  9352. (when (string-match "\\S-" tg)
  9353. (add-to-list 'buffer-tags (list tg))
  9354. (if (member tg current)
  9355. (setq current (delete tg current))
  9356. (push tg current)))
  9357. (if exit-after-next (setq exit-after-next 'now)))
  9358. ((setq e (rassoc c todo-table) tg (car e))
  9359. (with-current-buffer buf
  9360. (save-excursion (org-todo tg)))
  9361. (if exit-after-next (setq exit-after-next 'now)))
  9362. ((setq e (rassoc c ntable) tg (car e))
  9363. (if (member tg current)
  9364. (setq current (delete tg current))
  9365. (loop for g in groups do
  9366. (if (member tg g)
  9367. (mapc (lambda (x)
  9368. (setq current (delete x current)))
  9369. g)))
  9370. (push tg current))
  9371. (if exit-after-next (setq exit-after-next 'now))))
  9372. ;; Create a sorted list
  9373. (setq current
  9374. (sort current
  9375. (lambda (a b)
  9376. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9377. (if (eq exit-after-next 'now) (throw 'exit t))
  9378. (goto-char (point-min))
  9379. (beginning-of-line 2)
  9380. (delete-region (point) (point-at-eol))
  9381. (org-fast-tag-insert "Current" current c-face)
  9382. (org-set-current-tags-overlay current ov-prefix)
  9383. (while (re-search-forward
  9384. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9385. (setq tg (match-string 1))
  9386. (add-text-properties
  9387. (match-beginning 1) (match-end 1)
  9388. (list 'face
  9389. (cond
  9390. ((member tg current) c-face)
  9391. ((member tg inherited) i-face)
  9392. (t (get-text-property (match-beginning 1) 'face))))))
  9393. (goto-char (point-min)))))
  9394. (org-detach-overlay org-tags-overlay)
  9395. (if rtn
  9396. (mapconcat 'identity current ":")
  9397. nil))))
  9398. (defun org-get-tags-string ()
  9399. "Get the TAGS string in the current headline."
  9400. (unless (org-on-heading-p t)
  9401. (error "Not on a heading"))
  9402. (save-excursion
  9403. (beginning-of-line 1)
  9404. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9405. (org-match-string-no-properties 1)
  9406. "")))
  9407. (defun org-get-tags ()
  9408. "Get the list of tags specified in the current headline."
  9409. (org-split-string (org-get-tags-string) ":"))
  9410. (defun org-get-buffer-tags ()
  9411. "Get a table of all tags used in the buffer, for completion."
  9412. (let (tags)
  9413. (save-excursion
  9414. (goto-char (point-min))
  9415. (while (re-search-forward
  9416. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9417. (when (equal (char-after (point-at-bol 0)) ?*)
  9418. (mapc (lambda (x) (add-to-list 'tags x))
  9419. (org-split-string (org-match-string-no-properties 1) ":")))))
  9420. (mapcar 'list tags)))
  9421. ;;;; The mapping API
  9422. ;;;###autoload
  9423. (defun org-map-entries (func &optional match scope &rest skip)
  9424. "Call FUNC at each headline selected by MATCH in SCOPE.
  9425. FUNC is a function or a lisp form. The function will be called without
  9426. arguments, with the cursor positioned at the beginning of the headline.
  9427. The return values of all calls to the function will be collected and
  9428. returned as a list.
  9429. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9430. Only headlines that are matched by this query will be considered during
  9431. the iteration. When MATCH is nil or t, all headlines will be
  9432. visited by the iteration.
  9433. SCOPE determines the scope of this command. It can be any of:
  9434. nil The current buffer, respecting the restriction if any
  9435. tree The subtree started with the entry at point
  9436. file The current buffer, without restriction
  9437. file-with-archives
  9438. The current buffer, and any archives associated with it
  9439. agenda All agenda files
  9440. agenda-with-archives
  9441. All agenda files with any archive files associated with them
  9442. \(file1 file2 ...)
  9443. If this is a list, all files in the list will be scanned
  9444. The remaining args are treated as settings for the skipping facilities of
  9445. the scanner. The following items can be given here:
  9446. archive skip trees with the archive tag.
  9447. comment skip trees with the COMMENT keyword
  9448. function or Emacs Lisp form:
  9449. will be used as value for `org-agenda-skip-function', so whenever
  9450. the the function returns t, FUNC will not be called for that
  9451. entry and search will continue from the point where the
  9452. function leaves it."
  9453. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9454. (org-agenda-skip-archived-trees (memq 'archive skip))
  9455. (org-agenda-skip-comment-trees (memq 'comment skip))
  9456. (org-agenda-skip-function
  9457. (car (org-delete-all '(comment archive) skip)))
  9458. (org-tags-match-list-sublevels t)
  9459. matcher file res
  9460. org-todo-keywords-for-agenda
  9461. org-done-keywords-for-agenda
  9462. org-todo-keyword-alist-for-agenda
  9463. org-tag-alist-for-agenda)
  9464. (cond
  9465. ((eq match t) (setq matcher t))
  9466. ((eq match nil) (setq matcher t))
  9467. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9468. (save-excursion
  9469. (save-restriction
  9470. (when (eq scope 'tree)
  9471. (org-back-to-heading t)
  9472. (org-narrow-to-subtree)
  9473. (setq scope nil))
  9474. (if (not scope)
  9475. (progn
  9476. (org-prepare-agenda-buffers
  9477. (list (buffer-file-name (current-buffer))))
  9478. (setq res (org-scan-tags func matcher)))
  9479. ;; Get the right scope
  9480. (cond
  9481. ((and scope (listp scope) (symbolp (car scope)))
  9482. (setq scope (eval scope)))
  9483. ((eq scope 'agenda)
  9484. (setq scope (org-agenda-files t)))
  9485. ((eq scope 'agenda-with-archives)
  9486. (setq scope (org-agenda-files t))
  9487. (setq scope (org-add-archive-files scope)))
  9488. ((eq scope 'file)
  9489. (setq scope (list (buffer-file-name))))
  9490. ((eq scope 'file-with-archives)
  9491. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9492. (org-prepare-agenda-buffers scope)
  9493. (while (setq file (pop scope))
  9494. (with-current-buffer (org-find-base-buffer-visiting file)
  9495. (save-excursion
  9496. (save-restriction
  9497. (widen)
  9498. (goto-char (point-min))
  9499. (setq res (append res (org-scan-tags func matcher))))))))))
  9500. res))
  9501. ;;;; Properties
  9502. ;;; Setting and retrieving properties
  9503. (defconst org-special-properties
  9504. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9505. "TIMESTAMP" "TIMESTAMP_IA")
  9506. "The special properties valid in Org-mode.
  9507. These are properties that are not defined in the property drawer,
  9508. but in some other way.")
  9509. (defconst org-default-properties
  9510. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9511. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9512. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9513. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9514. "Some properties that are used by Org-mode for various purposes.
  9515. Being in this list makes sure that they are offered for completion.")
  9516. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9517. "Regular expression matching the first line of a property drawer.")
  9518. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9519. "Regular expression matching the first line of a property drawer.")
  9520. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9521. "Regular expression matching the first line of a property drawer.")
  9522. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9523. "Regular expression matching the first line of a property drawer.")
  9524. (defconst org-property-drawer-re
  9525. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9526. org-property-end-re "\\)\n?")
  9527. "Matches an entire property drawer.")
  9528. (defconst org-clock-drawer-re
  9529. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9530. org-property-end-re "\\)\n?")
  9531. "Matches an entire clock drawer.")
  9532. (defun org-property-action ()
  9533. "Do an action on properties."
  9534. (interactive)
  9535. (let (c)
  9536. (org-at-property-p)
  9537. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9538. (setq c (read-char-exclusive))
  9539. (cond
  9540. ((equal c ?s)
  9541. (call-interactively 'org-set-property))
  9542. ((equal c ?d)
  9543. (call-interactively 'org-delete-property))
  9544. ((equal c ?D)
  9545. (call-interactively 'org-delete-property-globally))
  9546. ((equal c ?c)
  9547. (call-interactively 'org-compute-property-at-point))
  9548. (t (error "No such property action %c" c)))))
  9549. (defun org-at-property-p ()
  9550. "Is the cursor in a property line?"
  9551. ;; FIXME: Does not check if we are actually in the drawer.
  9552. ;; FIXME: also returns true on any drawers.....
  9553. ;; This is used by C-c C-c for property action.
  9554. (save-excursion
  9555. (beginning-of-line 1)
  9556. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9557. (defun org-get-property-block (&optional beg end force)
  9558. "Return the (beg . end) range of the body of the property drawer.
  9559. BEG and END can be beginning and end of subtree, if not given
  9560. they will be found.
  9561. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9562. (catch 'exit
  9563. (save-excursion
  9564. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9565. (end (or end (progn (outline-next-heading) (point)))))
  9566. (goto-char beg)
  9567. (if (re-search-forward org-property-start-re end t)
  9568. (setq beg (1+ (match-end 0)))
  9569. (if force
  9570. (save-excursion
  9571. (org-insert-property-drawer)
  9572. (setq end (progn (outline-next-heading) (point))))
  9573. (throw 'exit nil))
  9574. (goto-char beg)
  9575. (if (re-search-forward org-property-start-re end t)
  9576. (setq beg (1+ (match-end 0)))))
  9577. (if (re-search-forward org-property-end-re end t)
  9578. (setq end (match-beginning 0))
  9579. (or force (throw 'exit nil))
  9580. (goto-char beg)
  9581. (setq end beg)
  9582. (org-indent-line-function)
  9583. (insert ":END:\n"))
  9584. (cons beg end)))))
  9585. (defun org-entry-properties (&optional pom which)
  9586. "Get all properties of the entry at point-or-marker POM.
  9587. This includes the TODO keyword, the tags, time strings for deadline,
  9588. scheduled, and clocking, and any additional properties defined in the
  9589. entry. The return value is an alist, keys may occur multiple times
  9590. if the property key was used several times.
  9591. POM may also be nil, in which case the current entry is used.
  9592. If WHICH is nil or `all', get all properties. If WHICH is
  9593. `special' or `standard', only get that subclass."
  9594. (setq which (or which 'all))
  9595. (org-with-point-at pom
  9596. (let ((clockstr (substring org-clock-string 0 -1))
  9597. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9598. beg end range props sum-props key value string clocksum)
  9599. (save-excursion
  9600. (when (condition-case nil
  9601. (and (org-mode-p) (org-back-to-heading t))
  9602. (error nil))
  9603. (setq beg (point))
  9604. (setq sum-props (get-text-property (point) 'org-summaries))
  9605. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9606. (outline-next-heading)
  9607. (setq end (point))
  9608. (when (memq which '(all special))
  9609. ;; Get the special properties, like TODO and tags
  9610. (goto-char beg)
  9611. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9612. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9613. (when (looking-at org-priority-regexp)
  9614. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9615. (when (and (setq value (org-get-tags-string))
  9616. (string-match "\\S-" value))
  9617. (push (cons "TAGS" value) props))
  9618. (when (setq value (org-get-tags-at))
  9619. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9620. props))
  9621. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9622. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9623. string (if (equal key clockstr)
  9624. (org-no-properties
  9625. (org-trim
  9626. (buffer-substring
  9627. (match-beginning 3) (goto-char (point-at-eol)))))
  9628. (substring (org-match-string-no-properties 3) 1 -1)))
  9629. (unless key
  9630. (if (= (char-after (match-beginning 3)) ?\[)
  9631. (setq key "TIMESTAMP_IA")
  9632. (setq key "TIMESTAMP")))
  9633. (when (or (equal key clockstr) (not (assoc key props)))
  9634. (push (cons key string) props)))
  9635. )
  9636. (when (memq which '(all standard))
  9637. ;; Get the standard properties, like :PORP: ...
  9638. (setq range (org-get-property-block beg end))
  9639. (when range
  9640. (goto-char (car range))
  9641. (while (re-search-forward
  9642. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9643. (cdr range) t)
  9644. (setq key (org-match-string-no-properties 1)
  9645. value (org-trim (or (org-match-string-no-properties 2) "")))
  9646. (unless (member key excluded)
  9647. (push (cons key (or value "")) props)))))
  9648. (if clocksum
  9649. (push (cons "CLOCKSUM"
  9650. (org-columns-number-to-string (/ (float clocksum) 60.)
  9651. 'add_times))
  9652. props))
  9653. (unless (assoc "CATEGORY" props)
  9654. (setq value (or (org-get-category)
  9655. (progn (org-refresh-category-properties)
  9656. (org-get-category))))
  9657. (push (cons "CATEGORY" value) props))
  9658. (append sum-props (nreverse props)))))))
  9659. (defun org-entry-get (pom property &optional inherit)
  9660. "Get value of PROPERTY for entry at point-or-marker POM.
  9661. If INHERIT is non-nil and the entry does not have the property,
  9662. then also check higher levels of the hierarchy.
  9663. If INHERIT is the symbol `selective', use inheritance only if the setting
  9664. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9665. If the property is present but empty, the return value is the empty string.
  9666. If the property is not present at all, nil is returned."
  9667. (org-with-point-at pom
  9668. (if (and inherit (if (eq inherit 'selective)
  9669. (org-property-inherit-p property)
  9670. t))
  9671. (org-entry-get-with-inheritance property)
  9672. (if (member property org-special-properties)
  9673. ;; We need a special property. Use brute force, get all properties.
  9674. (cdr (assoc property (org-entry-properties nil 'special)))
  9675. (let ((range (org-get-property-block)))
  9676. (if (and range
  9677. (goto-char (car range))
  9678. (re-search-forward
  9679. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9680. (cdr range) t))
  9681. ;; Found the property, return it.
  9682. (if (match-end 1)
  9683. (org-match-string-no-properties 1)
  9684. "")))))))
  9685. (defun org-property-or-variable-value (var &optional inherit)
  9686. "Check if there is a property fixing the value of VAR.
  9687. If yes, return this value. If not, return the current value of the variable."
  9688. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9689. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9690. (read prop)
  9691. (symbol-value var))))
  9692. (defun org-entry-delete (pom property)
  9693. "Delete the property PROPERTY from entry at point-or-marker POM."
  9694. (org-with-point-at pom
  9695. (if (member property org-special-properties)
  9696. nil ; cannot delete these properties.
  9697. (let ((range (org-get-property-block)))
  9698. (if (and range
  9699. (goto-char (car range))
  9700. (re-search-forward
  9701. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9702. (cdr range) t))
  9703. (progn
  9704. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9705. t)
  9706. nil)))))
  9707. ;; Multi-values properties are properties that contain multiple values
  9708. ;; These values are assumed to be single words, separated by whitespace.
  9709. (defun org-entry-add-to-multivalued-property (pom property value)
  9710. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9711. (let* ((old (org-entry-get pom property))
  9712. (values (and old (org-split-string old "[ \t]"))))
  9713. (setq value (org-entry-protect-space value))
  9714. (unless (member value values)
  9715. (setq values (cons value values))
  9716. (org-entry-put pom property
  9717. (mapconcat 'identity values " ")))))
  9718. (defun org-entry-remove-from-multivalued-property (pom property value)
  9719. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9720. (let* ((old (org-entry-get pom property))
  9721. (values (and old (org-split-string old "[ \t]"))))
  9722. (setq value (org-entry-protect-space value))
  9723. (when (member value values)
  9724. (setq values (delete value values))
  9725. (org-entry-put pom property
  9726. (mapconcat 'identity values " ")))))
  9727. (defun org-entry-member-in-multivalued-property (pom property value)
  9728. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9729. (let* ((old (org-entry-get pom property))
  9730. (values (and old (org-split-string old "[ \t]"))))
  9731. (setq value (org-entry-protect-space value))
  9732. (member value values)))
  9733. (defun org-entry-get-multivalued-property (pom property)
  9734. "Return a list of values in a multivalued property."
  9735. (let* ((value (org-entry-get pom property))
  9736. (values (and value (org-split-string value "[ \t]"))))
  9737. (mapcar 'org-entry-restore-space values)))
  9738. (defun org-entry-put-multivalued-property (pom property &rest values)
  9739. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9740. VALUES should be a list of strings. Spaces will be protected."
  9741. (org-entry-put pom property
  9742. (mapconcat 'org-entry-protect-space values " "))
  9743. (let* ((value (org-entry-get pom property))
  9744. (values (and value (org-split-string value "[ \t]"))))
  9745. (mapcar 'org-entry-restore-space values)))
  9746. (defun org-entry-protect-space (s)
  9747. "Protect spaces and newline in string S."
  9748. (while (string-match " " s)
  9749. (setq s (replace-match "%20" t t s)))
  9750. (while (string-match "\n" s)
  9751. (setq s (replace-match "%0A" t t s)))
  9752. s)
  9753. (defun org-entry-restore-space (s)
  9754. "Restore spaces and newline in string S."
  9755. (while (string-match "%20" s)
  9756. (setq s (replace-match " " t t s)))
  9757. (while (string-match "%0A" s)
  9758. (setq s (replace-match "\n" t t s)))
  9759. s)
  9760. (defvar org-entry-property-inherited-from (make-marker)
  9761. "Marker pointing to the entry from where a property was inherited.
  9762. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9763. location of the entry where the inheritance search matched. If there was
  9764. no match, the marker will point nowhere.
  9765. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9766. is set.")
  9767. (defun org-entry-get-with-inheritance (property)
  9768. "Get entry property, and search higher levels if not present."
  9769. (move-marker org-entry-property-inherited-from nil)
  9770. (let (tmp)
  9771. (save-excursion
  9772. (save-restriction
  9773. (widen)
  9774. (catch 'ex
  9775. (while t
  9776. (when (setq tmp (org-entry-get nil property))
  9777. (org-back-to-heading t)
  9778. (move-marker org-entry-property-inherited-from (point))
  9779. (throw 'ex tmp))
  9780. (or (org-up-heading-safe) (throw 'ex nil)))))
  9781. (or tmp
  9782. (cdr (assoc property org-file-properties))
  9783. (cdr (assoc property org-global-properties))
  9784. (cdr (assoc property org-global-properties-fixed))))))
  9785. (defun org-entry-put (pom property value)
  9786. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9787. (org-with-point-at pom
  9788. (org-back-to-heading t)
  9789. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9790. range)
  9791. (cond
  9792. ((equal property "TODO")
  9793. (when (and (stringp value) (string-match "\\S-" value)
  9794. (not (member value org-todo-keywords-1)))
  9795. (error "\"%s\" is not a valid TODO state" value))
  9796. (if (or (not value)
  9797. (not (string-match "\\S-" value)))
  9798. (setq value 'none))
  9799. (org-todo value)
  9800. (org-set-tags nil 'align))
  9801. ((equal property "PRIORITY")
  9802. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9803. (string-to-char value) ?\ ))
  9804. (org-set-tags nil 'align))
  9805. ((equal property "SCHEDULED")
  9806. (if (re-search-forward org-scheduled-time-regexp end t)
  9807. (cond
  9808. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9809. ((eq value 'later) (org-timestamp-change 1 'day))
  9810. (t (call-interactively 'org-schedule)))
  9811. (call-interactively 'org-schedule)))
  9812. ((equal property "DEADLINE")
  9813. (if (re-search-forward org-deadline-time-regexp end t)
  9814. (cond
  9815. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9816. ((eq value 'later) (org-timestamp-change 1 'day))
  9817. (t (call-interactively 'org-deadline)))
  9818. (call-interactively 'org-deadline)))
  9819. ((member property org-special-properties)
  9820. (error "The %s property can not yet be set with `org-entry-put'"
  9821. property))
  9822. (t ; a non-special property
  9823. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9824. (setq range (org-get-property-block beg end 'force))
  9825. (goto-char (car range))
  9826. (if (re-search-forward
  9827. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9828. (progn
  9829. (delete-region (match-beginning 1) (match-end 1))
  9830. (goto-char (match-beginning 1)))
  9831. (goto-char (cdr range))
  9832. (insert "\n")
  9833. (backward-char 1)
  9834. (org-indent-line-function)
  9835. (insert ":" property ":"))
  9836. (and value (insert " " value))
  9837. (org-indent-line-function)))))))
  9838. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9839. "Get all property keys in the current buffer.
  9840. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9841. like tags and TODO state.
  9842. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9843. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9844. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9845. formats in the current buffer."
  9846. (let (rtn range cfmt s p)
  9847. (save-excursion
  9848. (save-restriction
  9849. (widen)
  9850. (goto-char (point-min))
  9851. (while (re-search-forward org-property-start-re nil t)
  9852. (setq range (org-get-property-block))
  9853. (goto-char (car range))
  9854. (while (re-search-forward
  9855. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9856. (cdr range) t)
  9857. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9858. (outline-next-heading))))
  9859. (when include-specials
  9860. (setq rtn (append org-special-properties rtn)))
  9861. (when include-defaults
  9862. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9863. (when include-columns
  9864. (save-excursion
  9865. (save-restriction
  9866. (widen)
  9867. (goto-char (point-min))
  9868. (while (re-search-forward
  9869. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9870. nil t)
  9871. (setq cfmt (match-string 2) s 0)
  9872. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9873. cfmt s)
  9874. (setq s (match-end 0)
  9875. p (match-string 1 cfmt))
  9876. (unless (or (equal p "ITEM")
  9877. (member p org-special-properties))
  9878. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9879. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9880. (defun org-property-values (key)
  9881. "Return a list of all values of property KEY."
  9882. (save-excursion
  9883. (save-restriction
  9884. (widen)
  9885. (goto-char (point-min))
  9886. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9887. values)
  9888. (while (re-search-forward re nil t)
  9889. (add-to-list 'values (org-trim (match-string 1))))
  9890. (delete "" values)))))
  9891. (defun org-insert-property-drawer ()
  9892. "Insert a property drawer into the current entry."
  9893. (interactive)
  9894. (org-back-to-heading t)
  9895. (looking-at outline-regexp)
  9896. (let ((indent (- (match-end 0)(match-beginning 0)))
  9897. (beg (point))
  9898. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9899. end hiddenp)
  9900. (outline-next-heading)
  9901. (setq end (point))
  9902. (goto-char beg)
  9903. (while (re-search-forward re end t))
  9904. (setq hiddenp (org-invisible-p))
  9905. (end-of-line 1)
  9906. (and (equal (char-after) ?\n) (forward-char 1))
  9907. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9908. (beginning-of-line 2))
  9909. (org-skip-over-state-notes)
  9910. (skip-chars-backward " \t\n\r")
  9911. (if (eq (char-before) ?*) (forward-char 1))
  9912. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9913. (beginning-of-line 0)
  9914. (org-indent-to-column indent)
  9915. (beginning-of-line 2)
  9916. (org-indent-to-column indent)
  9917. (beginning-of-line 0)
  9918. (if hiddenp
  9919. (save-excursion
  9920. (org-back-to-heading t)
  9921. (hide-entry))
  9922. (org-flag-drawer t))))
  9923. (defun org-set-property (property value)
  9924. "In the current entry, set PROPERTY to VALUE.
  9925. When called interactively, this will prompt for a property name, offering
  9926. completion on existing and default properties. And then it will prompt
  9927. for a value, offering completion either on allowed values (via an inherited
  9928. xxx_ALL property) or on existing values in other instances of this property
  9929. in the current file."
  9930. (interactive
  9931. (let* ((completion-ignore-case t)
  9932. (keys (org-buffer-property-keys nil t t))
  9933. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9934. (prop (if (member prop0 keys)
  9935. prop0
  9936. (or (cdr (assoc (downcase prop0)
  9937. (mapcar (lambda (x) (cons (downcase x) x))
  9938. keys)))
  9939. prop0)))
  9940. (cur (org-entry-get nil prop))
  9941. (allowed (org-property-get-allowed-values nil prop 'table))
  9942. (existing (mapcar 'list (org-property-values prop)))
  9943. (val (if allowed
  9944. (org-completing-read "Value: " allowed nil 'req-match)
  9945. (org-completing-read
  9946. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9947. (concat "[" cur "]") "")
  9948. ": ")
  9949. existing nil nil "" nil cur))))
  9950. (list prop (if (equal val "") cur val))))
  9951. (unless (equal (org-entry-get nil property) value)
  9952. (org-entry-put nil property value)))
  9953. (defun org-delete-property (property)
  9954. "In the current entry, delete PROPERTY."
  9955. (interactive
  9956. (let* ((completion-ignore-case t)
  9957. (prop (org-ido-completing-read
  9958. "Property: " (org-entry-properties nil 'standard))))
  9959. (list prop)))
  9960. (message "Property %s %s" property
  9961. (if (org-entry-delete nil property)
  9962. "deleted"
  9963. "was not present in the entry")))
  9964. (defun org-delete-property-globally (property)
  9965. "Remove PROPERTY globally, from all entries."
  9966. (interactive
  9967. (let* ((completion-ignore-case t)
  9968. (prop (org-ido-completing-read
  9969. "Globally remove property: "
  9970. (mapcar 'list (org-buffer-property-keys)))))
  9971. (list prop)))
  9972. (save-excursion
  9973. (save-restriction
  9974. (widen)
  9975. (goto-char (point-min))
  9976. (let ((cnt 0))
  9977. (while (re-search-forward
  9978. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9979. nil t)
  9980. (setq cnt (1+ cnt))
  9981. (replace-match ""))
  9982. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9983. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9984. (defun org-compute-property-at-point ()
  9985. "Compute the property at point.
  9986. This looks for an enclosing column format, extracts the operator and
  9987. then applies it to the property in the column format's scope."
  9988. (interactive)
  9989. (unless (org-at-property-p)
  9990. (error "Not at a property"))
  9991. (let ((prop (org-match-string-no-properties 2)))
  9992. (org-columns-get-format-and-top-level)
  9993. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9994. (error "No operator defined for property %s" prop))
  9995. (org-columns-compute prop)))
  9996. (defun org-property-get-allowed-values (pom property &optional table)
  9997. "Get allowed values for the property PROPERTY.
  9998. When TABLE is non-nil, return an alist that can directly be used for
  9999. completion."
  10000. (let (vals)
  10001. (cond
  10002. ((equal property "TODO")
  10003. (setq vals (org-with-point-at pom
  10004. (append org-todo-keywords-1 '("")))))
  10005. ((equal property "PRIORITY")
  10006. (let ((n org-lowest-priority))
  10007. (while (>= n org-highest-priority)
  10008. (push (char-to-string n) vals)
  10009. (setq n (1- n)))))
  10010. ((member property org-special-properties))
  10011. (t
  10012. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10013. (when (and vals (string-match "\\S-" vals))
  10014. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10015. (setq vals (mapcar (lambda (x)
  10016. (cond ((stringp x) x)
  10017. ((numberp x) (number-to-string x))
  10018. ((symbolp x) (symbol-name x))
  10019. (t "???")))
  10020. vals)))))
  10021. (if table (mapcar 'list vals) vals)))
  10022. (defun org-property-previous-allowed-value (&optional previous)
  10023. "Switch to the next allowed value for this property."
  10024. (interactive)
  10025. (org-property-next-allowed-value t))
  10026. (defun org-property-next-allowed-value (&optional previous)
  10027. "Switch to the next allowed value for this property."
  10028. (interactive)
  10029. (unless (org-at-property-p)
  10030. (error "Not at a property"))
  10031. (let* ((key (match-string 2))
  10032. (value (match-string 3))
  10033. (allowed (or (org-property-get-allowed-values (point) key)
  10034. (and (member value '("[ ]" "[-]" "[X]"))
  10035. '("[ ]" "[X]"))))
  10036. nval)
  10037. (unless allowed
  10038. (error "Allowed values for this property have not been defined"))
  10039. (if previous (setq allowed (reverse allowed)))
  10040. (if (member value allowed)
  10041. (setq nval (car (cdr (member value allowed)))))
  10042. (setq nval (or nval (car allowed)))
  10043. (if (equal nval value)
  10044. (error "Only one allowed value for this property"))
  10045. (org-at-property-p)
  10046. (replace-match (concat " :" key ": " nval) t t)
  10047. (org-indent-line-function)
  10048. (beginning-of-line 1)
  10049. (skip-chars-forward " \t")))
  10050. (defun org-find-entry-with-id (ident)
  10051. "Locate the entry that contains the ID property with exact value IDENT.
  10052. IDENT can be a string, a symbol or a number, this function will search for
  10053. the string representation of it.
  10054. Return the position where this entry starts, or nil if there is no such entry."
  10055. (interactive "sID: ")
  10056. (let ((id (cond
  10057. ((stringp ident) ident)
  10058. ((symbol-name ident) (symbol-name ident))
  10059. ((numberp ident) (number-to-string ident))
  10060. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10061. (case-fold-search nil))
  10062. (save-excursion
  10063. (save-restriction
  10064. (widen)
  10065. (goto-char (point-min))
  10066. (when (re-search-forward
  10067. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10068. nil t)
  10069. (org-back-to-heading)
  10070. (point))))))
  10071. ;;;; Timestamps
  10072. (defvar org-last-changed-timestamp nil)
  10073. (defvar org-last-inserted-timestamp nil
  10074. "The last time stamp inserted with `org-insert-time-stamp'.")
  10075. (defvar org-time-was-given) ; dynamically scoped parameter
  10076. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10077. (defvar org-ts-what) ; dynamically scoped parameter
  10078. (defun org-time-stamp (arg &optional inactive)
  10079. "Prompt for a date/time and insert a time stamp.
  10080. If the user specifies a time like HH:MM, or if this command is called
  10081. with a prefix argument, the time stamp will contain date and time.
  10082. Otherwise, only the date will be included. All parts of a date not
  10083. specified by the user will be filled in from the current date/time.
  10084. So if you press just return without typing anything, the time stamp
  10085. will represent the current date/time. If there is already a timestamp
  10086. at the cursor, it will be modified."
  10087. (interactive "P")
  10088. (let* ((ts nil)
  10089. (default-time
  10090. ;; Default time is either today, or, when entering a range,
  10091. ;; the range start.
  10092. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10093. (save-excursion
  10094. (re-search-backward
  10095. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10096. (- (point) 20) t)))
  10097. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10098. (current-time)))
  10099. (default-input (and ts (org-get-compact-tod ts)))
  10100. org-time-was-given org-end-time-was-given time)
  10101. (cond
  10102. ((and (org-at-timestamp-p t)
  10103. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10104. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10105. (insert "--")
  10106. (setq time (let ((this-command this-command))
  10107. (org-read-date arg 'totime nil nil
  10108. default-time default-input)))
  10109. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10110. ((org-at-timestamp-p t)
  10111. (setq time (let ((this-command this-command))
  10112. (org-read-date arg 'totime nil nil default-time default-input)))
  10113. (when (org-at-timestamp-p t) ; just to get the match data
  10114. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10115. (replace-match "")
  10116. (setq org-last-changed-timestamp
  10117. (org-insert-time-stamp
  10118. time (or org-time-was-given arg)
  10119. inactive nil nil (list org-end-time-was-given))))
  10120. (message "Timestamp updated"))
  10121. (t
  10122. (setq time (let ((this-command this-command))
  10123. (org-read-date arg 'totime nil nil default-time default-input)))
  10124. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10125. nil nil (list org-end-time-was-given))))))
  10126. ;; FIXME: can we use this for something else, like computing time differences?
  10127. (defun org-get-compact-tod (s)
  10128. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10129. (let* ((t1 (match-string 1 s))
  10130. (h1 (string-to-number (match-string 2 s)))
  10131. (m1 (string-to-number (match-string 3 s)))
  10132. (t2 (and (match-end 4) (match-string 5 s)))
  10133. (h2 (and t2 (string-to-number (match-string 6 s))))
  10134. (m2 (and t2 (string-to-number (match-string 7 s))))
  10135. dh dm)
  10136. (if (not t2)
  10137. t1
  10138. (setq dh (- h2 h1) dm (- m2 m1))
  10139. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10140. (concat t1 "+" (number-to-string dh)
  10141. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10142. (defun org-time-stamp-inactive (&optional arg)
  10143. "Insert an inactive time stamp.
  10144. An inactive time stamp is enclosed in square brackets instead of angle
  10145. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10146. does not link to the calendar and cannot be changed with the S-cursor keys.
  10147. So these are more for recording a certain time/date."
  10148. (interactive "P")
  10149. (org-time-stamp arg 'inactive))
  10150. (defvar org-date-ovl (org-make-overlay 1 1))
  10151. (org-overlay-put org-date-ovl 'face 'org-warning)
  10152. (org-detach-overlay org-date-ovl)
  10153. (defvar org-ans1) ; dynamically scoped parameter
  10154. (defvar org-ans2) ; dynamically scoped parameter
  10155. (defvar org-plain-time-of-day-regexp) ; defined below
  10156. (defvar org-overriding-default-time nil) ; dynamically scoped
  10157. (defvar org-read-date-overlay nil)
  10158. (defvar org-dcst nil) ; dynamically scoped
  10159. (defun org-read-date (&optional with-time to-time from-string prompt
  10160. default-time default-input)
  10161. "Read a date, possibly a time, and make things smooth for the user.
  10162. The prompt will suggest to enter an ISO date, but you can also enter anything
  10163. which will at least partially be understood by `parse-time-string'.
  10164. Unrecognized parts of the date will default to the current day, month, year,
  10165. hour and minute. If this command is called to replace a timestamp at point,
  10166. of to enter the second timestamp of a range, the default time is taken from the
  10167. existing stamp. For example,
  10168. 3-2-5 --> 2003-02-05
  10169. feb 15 --> currentyear-02-15
  10170. sep 12 9 --> 2009-09-12
  10171. 12:45 --> today 12:45
  10172. 22 sept 0:34 --> currentyear-09-22 0:34
  10173. 12 --> currentyear-currentmonth-12
  10174. Fri --> nearest Friday (today or later)
  10175. etc.
  10176. Furthermore you can specify a relative date by giving, as the *first* thing
  10177. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10178. change in days weeks, months, years.
  10179. With a single plus or minus, the date is relative to today. With a double
  10180. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10181. +4d --> four days from today
  10182. +4 --> same as above
  10183. +2w --> two weeks from today
  10184. ++5 --> five days from default date
  10185. The function understands only English month and weekday abbreviations,
  10186. but this can be configured with the variables `parse-time-months' and
  10187. `parse-time-weekdays'.
  10188. While prompting, a calendar is popped up - you can also select the
  10189. date with the mouse (button 1). The calendar shows a period of three
  10190. months. To scroll it to other months, use the keys `>' and `<'.
  10191. If you don't like the calendar, turn it off with
  10192. \(setq org-read-date-popup-calendar nil)
  10193. With optional argument TO-TIME, the date will immediately be converted
  10194. to an internal time.
  10195. With an optional argument WITH-TIME, the prompt will suggest to also
  10196. insert a time. Note that when WITH-TIME is not set, you can still
  10197. enter a time, and this function will inform the calling routine about
  10198. this change. The calling routine may then choose to change the format
  10199. used to insert the time stamp into the buffer to include the time.
  10200. With optional argument FROM-STRING, read from this string instead from
  10201. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10202. the time/date that is used for everything that is not specified by the
  10203. user."
  10204. (require 'parse-time)
  10205. (let* ((org-time-stamp-rounding-minutes
  10206. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10207. (org-dcst org-display-custom-times)
  10208. (ct (org-current-time))
  10209. (def (or org-overriding-default-time default-time ct))
  10210. (defdecode (decode-time def))
  10211. (dummy (progn
  10212. (when (< (nth 2 defdecode) org-extend-today-until)
  10213. (setcar (nthcdr 2 defdecode) -1)
  10214. (setcar (nthcdr 1 defdecode) 59)
  10215. (setq def (apply 'encode-time defdecode)
  10216. defdecode (decode-time def)))))
  10217. (calendar-move-hook nil)
  10218. (calendar-view-diary-initially-flag nil)
  10219. (view-diary-entries-initially nil)
  10220. (calendar-view-holidays-initially-flag nil)
  10221. (view-calendar-holidays-initially nil)
  10222. (timestr (format-time-string
  10223. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10224. (prompt (concat (if prompt (concat prompt " ") "")
  10225. (format "Date+time [%s]: " timestr)))
  10226. ans (org-ans0 "") org-ans1 org-ans2 final)
  10227. (cond
  10228. (from-string (setq ans from-string))
  10229. (org-read-date-popup-calendar
  10230. (save-excursion
  10231. (save-window-excursion
  10232. (calendar)
  10233. (calendar-forward-day (- (time-to-days def)
  10234. (calendar-absolute-from-gregorian
  10235. (calendar-current-date))))
  10236. (org-eval-in-calendar nil t)
  10237. (let* ((old-map (current-local-map))
  10238. (map (copy-keymap calendar-mode-map))
  10239. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10240. (org-defkey map (kbd "RET") 'org-calendar-select)
  10241. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10242. 'org-calendar-select-mouse)
  10243. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10244. 'org-calendar-select-mouse)
  10245. (org-defkey minibuffer-local-map [(meta shift left)]
  10246. (lambda () (interactive)
  10247. (org-eval-in-calendar '(calendar-backward-month 1))))
  10248. (org-defkey minibuffer-local-map [(meta shift right)]
  10249. (lambda () (interactive)
  10250. (org-eval-in-calendar '(calendar-forward-month 1))))
  10251. (org-defkey minibuffer-local-map [(meta shift up)]
  10252. (lambda () (interactive)
  10253. (org-eval-in-calendar '(calendar-backward-year 1))))
  10254. (org-defkey minibuffer-local-map [(meta shift down)]
  10255. (lambda () (interactive)
  10256. (org-eval-in-calendar '(calendar-forward-year 1))))
  10257. (org-defkey minibuffer-local-map [(shift up)]
  10258. (lambda () (interactive)
  10259. (org-eval-in-calendar '(calendar-backward-week 1))))
  10260. (org-defkey minibuffer-local-map [(shift down)]
  10261. (lambda () (interactive)
  10262. (org-eval-in-calendar '(calendar-forward-week 1))))
  10263. (org-defkey minibuffer-local-map [(shift left)]
  10264. (lambda () (interactive)
  10265. (org-eval-in-calendar '(calendar-backward-day 1))))
  10266. (org-defkey minibuffer-local-map [(shift right)]
  10267. (lambda () (interactive)
  10268. (org-eval-in-calendar '(calendar-forward-day 1))))
  10269. (org-defkey minibuffer-local-map ">"
  10270. (lambda () (interactive)
  10271. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10272. (org-defkey minibuffer-local-map "<"
  10273. (lambda () (interactive)
  10274. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10275. (unwind-protect
  10276. (progn
  10277. (use-local-map map)
  10278. (add-hook 'post-command-hook 'org-read-date-display)
  10279. (setq org-ans0 (read-string prompt default-input nil nil))
  10280. ;; org-ans0: from prompt
  10281. ;; org-ans1: from mouse click
  10282. ;; org-ans2: from calendar motion
  10283. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10284. (remove-hook 'post-command-hook 'org-read-date-display)
  10285. (use-local-map old-map)
  10286. (when org-read-date-overlay
  10287. (org-delete-overlay org-read-date-overlay)
  10288. (setq org-read-date-overlay nil)))))))
  10289. (t ; Naked prompt only
  10290. (unwind-protect
  10291. (setq ans (read-string prompt default-input nil timestr))
  10292. (when org-read-date-overlay
  10293. (org-delete-overlay org-read-date-overlay)
  10294. (setq org-read-date-overlay nil)))))
  10295. (setq final (org-read-date-analyze ans def defdecode))
  10296. (if to-time
  10297. (apply 'encode-time final)
  10298. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10299. (format "%04d-%02d-%02d %02d:%02d"
  10300. (nth 5 final) (nth 4 final) (nth 3 final)
  10301. (nth 2 final) (nth 1 final))
  10302. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10303. (defvar def)
  10304. (defvar defdecode)
  10305. (defvar with-time)
  10306. (defun org-read-date-display ()
  10307. "Display the current date prompt interpretation in the minibuffer."
  10308. (when org-read-date-display-live
  10309. (when org-read-date-overlay
  10310. (org-delete-overlay org-read-date-overlay))
  10311. (let ((p (point)))
  10312. (end-of-line 1)
  10313. (while (not (equal (buffer-substring
  10314. (max (point-min) (- (point) 4)) (point))
  10315. " "))
  10316. (insert " "))
  10317. (goto-char p))
  10318. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10319. " " (or org-ans1 org-ans2)))
  10320. (org-end-time-was-given nil)
  10321. (f (org-read-date-analyze ans def defdecode))
  10322. (fmts (if org-dcst
  10323. org-time-stamp-custom-formats
  10324. org-time-stamp-formats))
  10325. (fmt (if (or with-time
  10326. (and (boundp 'org-time-was-given) org-time-was-given))
  10327. (cdr fmts)
  10328. (car fmts)))
  10329. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10330. (when (and org-end-time-was-given
  10331. (string-match org-plain-time-of-day-regexp txt))
  10332. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10333. org-end-time-was-given
  10334. (substring txt (match-end 0)))))
  10335. (setq org-read-date-overlay
  10336. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10337. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10338. (defun org-read-date-analyze (ans def defdecode)
  10339. "Analyse the combined answer of the date prompt."
  10340. ;; FIXME: cleanup and comment
  10341. (let (delta deltan deltaw deltadef year month day
  10342. hour minute second wday pm h2 m2 tl wday1
  10343. iso-year iso-weekday iso-week iso-year iso-date)
  10344. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10345. (setq ans "+0"))
  10346. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10347. (setq ans (replace-match "" t t ans)
  10348. deltan (car delta)
  10349. deltaw (nth 1 delta)
  10350. deltadef (nth 2 delta)))
  10351. ;; Check if there is an iso week date in there
  10352. ;; If yes, sore the info and postpone interpreting it until the rest
  10353. ;; of the parsing is done
  10354. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10355. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10356. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10357. iso-week (string-to-number (match-string 2 ans)))
  10358. (setq ans (replace-match "" t t ans)))
  10359. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10360. (when (string-match
  10361. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10362. (setq year (if (match-end 2)
  10363. (string-to-number (match-string 2 ans))
  10364. (string-to-number (format-time-string "%Y")))
  10365. month (string-to-number (match-string 3 ans))
  10366. day (string-to-number (match-string 4 ans)))
  10367. (if (< year 100) (setq year (+ 2000 year)))
  10368. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10369. t nil ans)))
  10370. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10371. ;; If there is a time with am/pm, and *no* time without it, we convert
  10372. ;; so that matching will be successful.
  10373. (loop for i from 1 to 2 do ; twice, for end time as well
  10374. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10375. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10376. (setq hour (string-to-number (match-string 1 ans))
  10377. minute (if (match-end 3)
  10378. (string-to-number (match-string 3 ans))
  10379. 0)
  10380. pm (equal ?p
  10381. (string-to-char (downcase (match-string 4 ans)))))
  10382. (if (and (= hour 12) (not pm))
  10383. (setq hour 0)
  10384. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10385. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10386. t t ans))))
  10387. ;; Check if a time range is given as a duration
  10388. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10389. (setq hour (string-to-number (match-string 1 ans))
  10390. h2 (+ hour (string-to-number (match-string 3 ans)))
  10391. minute (string-to-number (match-string 2 ans))
  10392. m2 (+ minute (if (match-end 5) (string-to-number
  10393. (match-string 5 ans))0)))
  10394. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10395. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10396. t t ans)))
  10397. ;; Check if there is a time range
  10398. (when (boundp 'org-end-time-was-given)
  10399. (setq org-time-was-given nil)
  10400. (when (and (string-match org-plain-time-of-day-regexp ans)
  10401. (match-end 8))
  10402. (setq org-end-time-was-given (match-string 8 ans))
  10403. (setq ans (concat (substring ans 0 (match-beginning 7))
  10404. (substring ans (match-end 7))))))
  10405. (setq tl (parse-time-string ans)
  10406. day (or (nth 3 tl) (nth 3 defdecode))
  10407. month (or (nth 4 tl)
  10408. (if (and org-read-date-prefer-future
  10409. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10410. (1+ (nth 4 defdecode))
  10411. (nth 4 defdecode)))
  10412. year (or (nth 5 tl)
  10413. (if (and org-read-date-prefer-future
  10414. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10415. (1+ (nth 5 defdecode))
  10416. (nth 5 defdecode)))
  10417. hour (or (nth 2 tl) (nth 2 defdecode))
  10418. minute (or (nth 1 tl) (nth 1 defdecode))
  10419. second (or (nth 0 tl) 0)
  10420. wday (nth 6 tl))
  10421. ;; Special date definitions below
  10422. (cond
  10423. (iso-week
  10424. ;; There was an iso week
  10425. (setq year (or iso-year year)
  10426. day (or iso-weekday wday 1)
  10427. wday nil ; to make sure that the trigger below does not match
  10428. iso-date (calendar-gregorian-from-absolute
  10429. (calendar-absolute-from-iso
  10430. (list iso-week day year))))
  10431. ; FIXME: Should we also push ISO weeks into the future?
  10432. ; (when (and org-read-date-prefer-future
  10433. ; (not iso-year)
  10434. ; (< (calendar-absolute-from-gregorian iso-date)
  10435. ; (time-to-days (current-time))))
  10436. ; (setq year (1+ year)
  10437. ; iso-date (calendar-gregorian-from-absolute
  10438. ; (calendar-absolute-from-iso
  10439. ; (list iso-week day year)))))
  10440. (setq month (car iso-date)
  10441. year (nth 2 iso-date)
  10442. day (nth 1 iso-date)))
  10443. (deltan
  10444. (unless deltadef
  10445. (let ((now (decode-time (current-time))))
  10446. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10447. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10448. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10449. ((equal deltaw "m") (setq month (+ month deltan)))
  10450. ((equal deltaw "y") (setq year (+ year deltan)))))
  10451. ((and wday (not (nth 3 tl)))
  10452. ;; Weekday was given, but no day, so pick that day in the week
  10453. ;; on or after the derived date.
  10454. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10455. (unless (equal wday wday1)
  10456. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10457. (if (and (boundp 'org-time-was-given)
  10458. (nth 2 tl))
  10459. (setq org-time-was-given t))
  10460. (if (< year 100) (setq year (+ 2000 year)))
  10461. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10462. (list second minute hour day month year)))
  10463. (defvar parse-time-weekdays)
  10464. (defun org-read-date-get-relative (s today default)
  10465. "Check string S for special relative date string.
  10466. TODAY and DEFAULT are internal times, for today and for a default.
  10467. Return shift list (N what def-flag)
  10468. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10469. N is the number of WHATs to shift.
  10470. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10471. the DEFAULT date rather than TODAY."
  10472. (when (and
  10473. (string-match
  10474. (concat
  10475. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10476. "\\([0-9]+\\)?"
  10477. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10478. "\\([ \t]\\|$\\)") s)
  10479. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10480. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10481. (string-to-char (substring (match-string 1 s) -1))
  10482. ?+))
  10483. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10484. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10485. (what (if (match-end 3) (match-string 3 s) "d"))
  10486. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10487. (date (if rel default today))
  10488. (wday (nth 6 (decode-time date)))
  10489. delta)
  10490. (if wday1
  10491. (progn
  10492. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10493. (if (= dir ?-) (setq delta (- delta 7)))
  10494. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10495. (list delta "d" rel))
  10496. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10497. (defun org-eval-in-calendar (form &optional keepdate)
  10498. "Eval FORM in the calendar window and return to current window.
  10499. Also, store the cursor date in variable org-ans2."
  10500. (let ((sw (selected-window)))
  10501. (select-window (get-buffer-window "*Calendar*"))
  10502. (eval form)
  10503. (when (and (not keepdate) (calendar-cursor-to-date))
  10504. (let* ((date (calendar-cursor-to-date))
  10505. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10506. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10507. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10508. (select-window sw)))
  10509. (defun org-calendar-select ()
  10510. "Return to `org-read-date' with the date currently selected.
  10511. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10512. (interactive)
  10513. (when (calendar-cursor-to-date)
  10514. (let* ((date (calendar-cursor-to-date))
  10515. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10516. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10517. (if (active-minibuffer-window) (exit-minibuffer))))
  10518. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10519. "Insert a date stamp for the date given by the internal TIME.
  10520. WITH-HM means, use the stamp format that includes the time of the day.
  10521. INACTIVE means use square brackets instead of angular ones, so that the
  10522. stamp will not contribute to the agenda.
  10523. PRE and POST are optional strings to be inserted before and after the
  10524. stamp.
  10525. The command returns the inserted time stamp."
  10526. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10527. stamp)
  10528. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10529. (insert-before-markers (or pre ""))
  10530. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10531. (when (listp extra)
  10532. (setq extra (car extra))
  10533. (if (and (stringp extra)
  10534. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10535. (setq extra (format "-%02d:%02d"
  10536. (string-to-number (match-string 1 extra))
  10537. (string-to-number (match-string 2 extra))))
  10538. (setq extra nil)))
  10539. (when extra
  10540. (backward-char 1)
  10541. (insert-before-markers extra)
  10542. (forward-char 1))
  10543. (insert-before-markers (or post ""))
  10544. (setq org-last-inserted-timestamp stamp)))
  10545. (defun org-toggle-time-stamp-overlays ()
  10546. "Toggle the use of custom time stamp formats."
  10547. (interactive)
  10548. (setq org-display-custom-times (not org-display-custom-times))
  10549. (unless org-display-custom-times
  10550. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10551. (while (setq p (next-single-property-change p 'display))
  10552. (if (and (get-text-property p 'display)
  10553. (eq (get-text-property p 'face) 'org-date))
  10554. (remove-text-properties
  10555. p (setq p (next-single-property-change p 'display))
  10556. '(display t))))
  10557. (set-buffer-modified-p bmp)))
  10558. (if (featurep 'xemacs)
  10559. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10560. (org-restart-font-lock)
  10561. (setq org-table-may-need-update t)
  10562. (if org-display-custom-times
  10563. (message "Time stamps are overlayed with custom format")
  10564. (message "Time stamp overlays removed")))
  10565. (defun org-display-custom-time (beg end)
  10566. "Overlay modified time stamp format over timestamp between BEG and END."
  10567. (let* ((ts (buffer-substring beg end))
  10568. t1 w1 with-hm tf time str w2 (off 0))
  10569. (save-match-data
  10570. (setq t1 (org-parse-time-string ts t))
  10571. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10572. (setq off (- (match-end 0) (match-beginning 0)))))
  10573. (setq end (- end off))
  10574. (setq w1 (- end beg)
  10575. with-hm (and (nth 1 t1) (nth 2 t1))
  10576. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10577. time (org-fix-decoded-time t1)
  10578. str (org-add-props
  10579. (format-time-string
  10580. (substring tf 1 -1) (apply 'encode-time time))
  10581. nil 'mouse-face 'highlight)
  10582. w2 (length str))
  10583. (if (not (= w2 w1))
  10584. (add-text-properties (1+ beg) (+ 2 beg)
  10585. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10586. (if (featurep 'xemacs)
  10587. (progn
  10588. (put-text-property beg end 'invisible t)
  10589. (put-text-property beg end 'end-glyph (make-glyph str)))
  10590. (put-text-property beg end 'display str))))
  10591. (defun org-translate-time (string)
  10592. "Translate all timestamps in STRING to custom format.
  10593. But do this only if the variable `org-display-custom-times' is set."
  10594. (when org-display-custom-times
  10595. (save-match-data
  10596. (let* ((start 0)
  10597. (re org-ts-regexp-both)
  10598. t1 with-hm inactive tf time str beg end)
  10599. (while (setq start (string-match re string start))
  10600. (setq beg (match-beginning 0)
  10601. end (match-end 0)
  10602. t1 (save-match-data
  10603. (org-parse-time-string (substring string beg end) t))
  10604. with-hm (and (nth 1 t1) (nth 2 t1))
  10605. inactive (equal (substring string beg (1+ beg)) "[")
  10606. tf (funcall (if with-hm 'cdr 'car)
  10607. org-time-stamp-custom-formats)
  10608. time (org-fix-decoded-time t1)
  10609. str (format-time-string
  10610. (concat
  10611. (if inactive "[" "<") (substring tf 1 -1)
  10612. (if inactive "]" ">"))
  10613. (apply 'encode-time time))
  10614. string (replace-match str t t string)
  10615. start (+ start (length str)))))))
  10616. string)
  10617. (defun org-fix-decoded-time (time)
  10618. "Set 0 instead of nil for the first 6 elements of time.
  10619. Don't touch the rest."
  10620. (let ((n 0))
  10621. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10622. (defun org-days-to-time (timestamp-string)
  10623. "Difference between TIMESTAMP-STRING and now in days."
  10624. (- (time-to-days (org-time-string-to-time timestamp-string))
  10625. (time-to-days (current-time))))
  10626. (defun org-deadline-close (timestamp-string &optional ndays)
  10627. "Is the time in TIMESTAMP-STRING close to the current date?"
  10628. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10629. (and (< (org-days-to-time timestamp-string) ndays)
  10630. (not (org-entry-is-done-p))))
  10631. (defun org-get-wdays (ts)
  10632. "Get the deadline lead time appropriate for timestring TS."
  10633. (cond
  10634. ((<= org-deadline-warning-days 0)
  10635. ;; 0 or negative, enforce this value no matter what
  10636. (- org-deadline-warning-days))
  10637. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10638. ;; lead time is specified.
  10639. (floor (* (string-to-number (match-string 1 ts))
  10640. (cdr (assoc (match-string 2 ts)
  10641. '(("d" . 1) ("w" . 7)
  10642. ("m" . 30.4) ("y" . 365.25)))))))
  10643. ;; go for the default.
  10644. (t org-deadline-warning-days)))
  10645. (defun org-calendar-select-mouse (ev)
  10646. "Return to `org-read-date' with the date currently selected.
  10647. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10648. (interactive "e")
  10649. (mouse-set-point ev)
  10650. (when (calendar-cursor-to-date)
  10651. (let* ((date (calendar-cursor-to-date))
  10652. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10653. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10654. (if (active-minibuffer-window) (exit-minibuffer))))
  10655. (defun org-check-deadlines (ndays)
  10656. "Check if there are any deadlines due or past due.
  10657. A deadline is considered due if it happens within `org-deadline-warning-days'
  10658. days from today's date. If the deadline appears in an entry marked DONE,
  10659. it is not shown. The prefix arg NDAYS can be used to test that many
  10660. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10661. (interactive "P")
  10662. (let* ((org-warn-days
  10663. (cond
  10664. ((equal ndays '(4)) 100000)
  10665. (ndays (prefix-numeric-value ndays))
  10666. (t (abs org-deadline-warning-days))))
  10667. (case-fold-search nil)
  10668. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10669. (callback
  10670. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10671. (message "%d deadlines past-due or due within %d days"
  10672. (org-occur regexp nil callback)
  10673. org-warn-days)))
  10674. (defun org-check-before-date (date)
  10675. "Check if there are deadlines or scheduled entries before DATE."
  10676. (interactive (list (org-read-date)))
  10677. (let ((case-fold-search nil)
  10678. (regexp (concat "\\<\\(" org-deadline-string
  10679. "\\|" org-scheduled-string
  10680. "\\) *<\\([^>]+\\)>"))
  10681. (callback
  10682. (lambda () (time-less-p
  10683. (org-time-string-to-time (match-string 2))
  10684. (org-time-string-to-time date)))))
  10685. (message "%d entries before %s"
  10686. (org-occur regexp nil callback) date)))
  10687. (defun org-evaluate-time-range (&optional to-buffer)
  10688. "Evaluate a time range by computing the difference between start and end.
  10689. Normally the result is just printed in the echo area, but with prefix arg
  10690. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10691. If the time range is actually in a table, the result is inserted into the
  10692. next column.
  10693. For time difference computation, a year is assumed to be exactly 365
  10694. days in order to avoid rounding problems."
  10695. (interactive "P")
  10696. (or
  10697. (org-clock-update-time-maybe)
  10698. (save-excursion
  10699. (unless (org-at-date-range-p t)
  10700. (goto-char (point-at-bol))
  10701. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10702. (if (not (org-at-date-range-p t))
  10703. (error "Not at a time-stamp range, and none found in current line")))
  10704. (let* ((ts1 (match-string 1))
  10705. (ts2 (match-string 2))
  10706. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10707. (match-end (match-end 0))
  10708. (time1 (org-time-string-to-time ts1))
  10709. (time2 (org-time-string-to-time ts2))
  10710. (t1 (time-to-seconds time1))
  10711. (t2 (time-to-seconds time2))
  10712. (diff (abs (- t2 t1)))
  10713. (negative (< (- t2 t1) 0))
  10714. ;; (ys (floor (* 365 24 60 60)))
  10715. (ds (* 24 60 60))
  10716. (hs (* 60 60))
  10717. (fy "%dy %dd %02d:%02d")
  10718. (fy1 "%dy %dd")
  10719. (fd "%dd %02d:%02d")
  10720. (fd1 "%dd")
  10721. (fh "%02d:%02d")
  10722. y d h m align)
  10723. (if havetime
  10724. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10725. y 0
  10726. d (floor (/ diff ds)) diff (mod diff ds)
  10727. h (floor (/ diff hs)) diff (mod diff hs)
  10728. m (floor (/ diff 60)))
  10729. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10730. y 0
  10731. d (floor (+ (/ diff ds) 0.5))
  10732. h 0 m 0))
  10733. (if (not to-buffer)
  10734. (message "%s" (org-make-tdiff-string y d h m))
  10735. (if (org-at-table-p)
  10736. (progn
  10737. (goto-char match-end)
  10738. (setq align t)
  10739. (and (looking-at " *|") (goto-char (match-end 0))))
  10740. (goto-char match-end))
  10741. (if (looking-at
  10742. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10743. (replace-match ""))
  10744. (if negative (insert " -"))
  10745. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10746. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10747. (insert " " (format fh h m))))
  10748. (if align (org-table-align))
  10749. (message "Time difference inserted")))))
  10750. (defun org-make-tdiff-string (y d h m)
  10751. (let ((fmt "")
  10752. (l nil))
  10753. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10754. l (push y l)))
  10755. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10756. l (push d l)))
  10757. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10758. l (push h l)))
  10759. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10760. l (push m l)))
  10761. (apply 'format fmt (nreverse l))))
  10762. (defun org-time-string-to-time (s)
  10763. (apply 'encode-time (org-parse-time-string s)))
  10764. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10765. "Convert a time stamp to an absolute day number.
  10766. If there is a specifyer for a cyclic time stamp, get the closest date to
  10767. DAYNR.
  10768. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10769. (cond
  10770. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10771. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10772. daynr
  10773. (+ daynr 1000)))
  10774. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10775. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10776. (time-to-days (current-time))) (match-string 0 s)
  10777. prefer show-all))
  10778. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10779. (defun org-days-to-iso-week (days)
  10780. "Return the iso week number."
  10781. (require 'cal-iso)
  10782. (car (calendar-iso-from-absolute days)))
  10783. (defun org-small-year-to-year (year)
  10784. "Convert 2-digit years into 4-digit years.
  10785. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10786. The year 2000 cannot be abbreviated. Any year larger than 99
  10787. is returned unchanged."
  10788. (if (< year 38)
  10789. (setq year (+ 2000 year))
  10790. (if (< year 100)
  10791. (setq year (+ 1900 year))))
  10792. year)
  10793. (defun org-time-from-absolute (d)
  10794. "Return the time corresponding to date D.
  10795. D may be an absolute day number, or a calendar-type list (month day year)."
  10796. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10797. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10798. (defun org-calendar-holiday ()
  10799. "List of holidays, for Diary display in Org-mode."
  10800. (require 'holidays)
  10801. (let ((hl (funcall
  10802. (if (fboundp 'calendar-check-holidays)
  10803. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10804. (if hl (mapconcat 'identity hl "; "))))
  10805. (defun org-diary-sexp-entry (sexp entry date)
  10806. "Process a SEXP diary ENTRY for DATE."
  10807. (require 'diary-lib)
  10808. (let ((result (if calendar-debug-sexp
  10809. (let ((stack-trace-on-error t))
  10810. (eval (car (read-from-string sexp))))
  10811. (condition-case nil
  10812. (eval (car (read-from-string sexp)))
  10813. (error
  10814. (beep)
  10815. (message "Bad sexp at line %d in %s: %s"
  10816. (org-current-line)
  10817. (buffer-file-name) sexp)
  10818. (sleep-for 2))))))
  10819. (cond ((stringp result) result)
  10820. ((and (consp result)
  10821. (stringp (cdr result))) (cdr result))
  10822. (result entry)
  10823. (t nil))))
  10824. (defun org-diary-to-ical-string (frombuf)
  10825. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10826. This uses the icalendar.el library."
  10827. (let* ((tmpdir (if (featurep 'xemacs)
  10828. (temp-directory)
  10829. temporary-file-directory))
  10830. (tmpfile (make-temp-name
  10831. (expand-file-name "orgics" tmpdir)))
  10832. buf rtn b e)
  10833. (save-excursion
  10834. (set-buffer frombuf)
  10835. (icalendar-export-region (point-min) (point-max) tmpfile)
  10836. (setq buf (find-buffer-visiting tmpfile))
  10837. (set-buffer buf)
  10838. (goto-char (point-min))
  10839. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10840. (setq b (match-beginning 0)))
  10841. (goto-char (point-max))
  10842. (if (re-search-backward "^END:VEVENT" nil t)
  10843. (setq e (match-end 0)))
  10844. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10845. (kill-buffer buf)
  10846. (delete-file tmpfile)
  10847. rtn))
  10848. (defun org-closest-date (start current change prefer show-all)
  10849. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10850. When PREFER is `past' return a date that is either CURRENT or past.
  10851. When PREFER is `future', return a date that is either CURRENT or future.
  10852. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10853. ;; Make the proper lists from the dates
  10854. (catch 'exit
  10855. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10856. dn dw sday cday n1 n2 n0
  10857. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10858. (setq start (org-date-to-gregorian start)
  10859. current (org-date-to-gregorian
  10860. (if show-all
  10861. current
  10862. (time-to-days (current-time))))
  10863. sday (calendar-absolute-from-gregorian start)
  10864. cday (calendar-absolute-from-gregorian current))
  10865. (if (<= cday sday) (throw 'exit sday))
  10866. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10867. (setq dn (string-to-number (match-string 1 change))
  10868. dw (cdr (assoc (match-string 2 change) a1)))
  10869. (error "Invalid change specifyer: %s" change))
  10870. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10871. (cond
  10872. ((eq dw 'day)
  10873. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10874. n2 (+ n1 dn)))
  10875. ((eq dw 'year)
  10876. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10877. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10878. (setq date1 (list m d y1)
  10879. n1 (calendar-absolute-from-gregorian date1)
  10880. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10881. n2 (calendar-absolute-from-gregorian date2)))
  10882. ((eq dw 'month)
  10883. ;; approx number of month between the two dates
  10884. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10885. ;; How often does dn fit in there?
  10886. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10887. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10888. m (+ m nm)
  10889. ny (floor (/ m 12))
  10890. y (+ y ny)
  10891. m (- m (* ny 12)))
  10892. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10893. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10894. (setq m2 (+ m dn) y2 y)
  10895. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10896. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10897. (while (<= n2 cday)
  10898. (setq n1 n2 m m2 y y2)
  10899. (setq m2 (+ m dn) y2 y)
  10900. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10901. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10902. ;; Make sure n1 is the earlier date
  10903. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  10904. (if show-all
  10905. (cond
  10906. ((eq prefer 'past) n1)
  10907. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10908. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10909. (cond
  10910. ((eq prefer 'past) n1)
  10911. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10912. (t (if (= cday n1) n1 n2)))))))
  10913. (defun org-date-to-gregorian (date)
  10914. "Turn any specification of DATE into a gregorian date for the calendar."
  10915. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10916. ((and (listp date) (= (length date) 3)) date)
  10917. ((stringp date)
  10918. (setq date (org-parse-time-string date))
  10919. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10920. ((listp date)
  10921. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10922. (defun org-parse-time-string (s &optional nodefault)
  10923. "Parse the standard Org-mode time string.
  10924. This should be a lot faster than the normal `parse-time-string'.
  10925. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10926. hour and minute fields will be nil if not given."
  10927. (if (string-match org-ts-regexp0 s)
  10928. (list 0
  10929. (if (or (match-beginning 8) (not nodefault))
  10930. (string-to-number (or (match-string 8 s) "0")))
  10931. (if (or (match-beginning 7) (not nodefault))
  10932. (string-to-number (or (match-string 7 s) "0")))
  10933. (string-to-number (match-string 4 s))
  10934. (string-to-number (match-string 3 s))
  10935. (string-to-number (match-string 2 s))
  10936. nil nil nil)
  10937. (make-list 9 0)))
  10938. (defun org-timestamp-up (&optional arg)
  10939. "Increase the date item at the cursor by one.
  10940. If the cursor is on the year, change the year. If it is on the month or
  10941. the day, change that.
  10942. With prefix ARG, change by that many units."
  10943. (interactive "p")
  10944. (org-timestamp-change (prefix-numeric-value arg)))
  10945. (defun org-timestamp-down (&optional arg)
  10946. "Decrease the date item at the cursor by one.
  10947. If the cursor is on the year, change the year. If it is on the month or
  10948. the day, change that.
  10949. With prefix ARG, change by that many units."
  10950. (interactive "p")
  10951. (org-timestamp-change (- (prefix-numeric-value arg))))
  10952. (defun org-timestamp-up-day (&optional arg)
  10953. "Increase the date in the time stamp by one day.
  10954. With prefix ARG, change that many days."
  10955. (interactive "p")
  10956. (if (and (not (org-at-timestamp-p t))
  10957. (org-on-heading-p))
  10958. (org-todo 'up)
  10959. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10960. (defun org-timestamp-down-day (&optional arg)
  10961. "Decrease the date in the time stamp by one day.
  10962. With prefix ARG, change that many days."
  10963. (interactive "p")
  10964. (if (and (not (org-at-timestamp-p t))
  10965. (org-on-heading-p))
  10966. (org-todo 'down)
  10967. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10968. (defun org-at-timestamp-p (&optional inactive-ok)
  10969. "Determine if the cursor is in or at a timestamp."
  10970. (interactive)
  10971. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10972. (pos (point))
  10973. (ans (or (looking-at tsr)
  10974. (save-excursion
  10975. (skip-chars-backward "^[<\n\r\t")
  10976. (if (> (point) (point-min)) (backward-char 1))
  10977. (and (looking-at tsr)
  10978. (> (- (match-end 0) pos) -1))))))
  10979. (and ans
  10980. (boundp 'org-ts-what)
  10981. (setq org-ts-what
  10982. (cond
  10983. ((= pos (match-beginning 0)) 'bracket)
  10984. ((= pos (1- (match-end 0))) 'bracket)
  10985. ((org-pos-in-match-range pos 2) 'year)
  10986. ((org-pos-in-match-range pos 3) 'month)
  10987. ((org-pos-in-match-range pos 7) 'hour)
  10988. ((org-pos-in-match-range pos 8) 'minute)
  10989. ((or (org-pos-in-match-range pos 4)
  10990. (org-pos-in-match-range pos 5)) 'day)
  10991. ((and (> pos (or (match-end 8) (match-end 5)))
  10992. (< pos (match-end 0)))
  10993. (- pos (or (match-end 8) (match-end 5))))
  10994. (t 'day))))
  10995. ans))
  10996. (defun org-toggle-timestamp-type ()
  10997. "Toggle the type (<active> or [inactive]) of a time stamp."
  10998. (interactive)
  10999. (when (org-at-timestamp-p t)
  11000. (let ((beg (match-beginning 0)) (end (match-end 0))
  11001. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11002. (save-excursion
  11003. (goto-char beg)
  11004. (while (re-search-forward "[][<>]" end t)
  11005. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11006. t t)))
  11007. (message "Timestamp is now %sactive"
  11008. (if (equal (char-after beg) ?<) "" "in")))))
  11009. (defun org-timestamp-change (n &optional what)
  11010. "Change the date in the time stamp at point.
  11011. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11012. `year', `minute', `second'. If WHAT is not given, the cursor position
  11013. in the timestamp determines what will be changed."
  11014. (let ((pos (point))
  11015. with-hm inactive
  11016. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11017. org-ts-what
  11018. extra rem
  11019. ts time time0)
  11020. (if (not (org-at-timestamp-p t))
  11021. (error "Not at a timestamp"))
  11022. (if (and (not what) (eq org-ts-what 'bracket))
  11023. (org-toggle-timestamp-type)
  11024. (if (and (not what) (not (eq org-ts-what 'day))
  11025. org-display-custom-times
  11026. (get-text-property (point) 'display)
  11027. (not (get-text-property (1- (point)) 'display)))
  11028. (setq org-ts-what 'day))
  11029. (setq org-ts-what (or what org-ts-what)
  11030. inactive (= (char-after (match-beginning 0)) ?\[)
  11031. ts (match-string 0))
  11032. (replace-match "")
  11033. (if (string-match
  11034. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11035. ts)
  11036. (setq extra (match-string 1 ts)))
  11037. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11038. (setq with-hm t))
  11039. (setq time0 (org-parse-time-string ts))
  11040. (when (and (eq org-ts-what 'minute)
  11041. (eq current-prefix-arg nil))
  11042. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11043. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11044. (setcar (cdr time0) (+ (nth 1 time0)
  11045. (if (> n 0) (- rem) (- dm rem))))))
  11046. (setq time
  11047. (encode-time (or (car time0) 0)
  11048. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11049. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11050. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11051. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11052. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11053. (nthcdr 6 time0)))
  11054. (when (integerp org-ts-what)
  11055. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11056. (if (eq what 'calendar)
  11057. (let ((cal-date (org-get-date-from-calendar)))
  11058. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11059. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11060. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11061. (setcar time0 (or (car time0) 0))
  11062. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11063. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11064. (setq time (apply 'encode-time time0))))
  11065. (setq org-last-changed-timestamp
  11066. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11067. (org-clock-update-time-maybe)
  11068. (goto-char pos)
  11069. ;; Try to recenter the calendar window, if any
  11070. (if (and org-calendar-follow-timestamp-change
  11071. (get-buffer-window "*Calendar*" t)
  11072. (memq org-ts-what '(day month year)))
  11073. (org-recenter-calendar (time-to-days time))))))
  11074. (defun org-modify-ts-extra (s pos n dm)
  11075. "Change the different parts of the lead-time and repeat fields in timestamp."
  11076. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11077. ng h m new rem)
  11078. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11079. (cond
  11080. ((or (org-pos-in-match-range pos 2)
  11081. (org-pos-in-match-range pos 3))
  11082. (setq m (string-to-number (match-string 3 s))
  11083. h (string-to-number (match-string 2 s)))
  11084. (if (org-pos-in-match-range pos 2)
  11085. (setq h (+ h n))
  11086. (setq n (* dm (org-no-warnings (signum n))))
  11087. (when (not (= 0 (setq rem (% m dm))))
  11088. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11089. (setq m (+ m n)))
  11090. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11091. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11092. (setq h (min 24 (max 0 h)))
  11093. (setq ng 1 new (format "-%02d:%02d" h m)))
  11094. ((org-pos-in-match-range pos 6)
  11095. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11096. ((org-pos-in-match-range pos 5)
  11097. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11098. ((org-pos-in-match-range pos 9)
  11099. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11100. ((org-pos-in-match-range pos 8)
  11101. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11102. (when ng
  11103. (setq s (concat
  11104. (substring s 0 (match-beginning ng))
  11105. new
  11106. (substring s (match-end ng))))))
  11107. s))
  11108. (defun org-recenter-calendar (date)
  11109. "If the calendar is visible, recenter it to DATE."
  11110. (let* ((win (selected-window))
  11111. (cwin (get-buffer-window "*Calendar*" t))
  11112. (calendar-move-hook nil))
  11113. (when cwin
  11114. (select-window cwin)
  11115. (calendar-goto-date (if (listp date) date
  11116. (calendar-gregorian-from-absolute date)))
  11117. (select-window win))))
  11118. (defun org-goto-calendar (&optional arg)
  11119. "Go to the Emacs calendar at the current date.
  11120. If there is a time stamp in the current line, go to that date.
  11121. A prefix ARG can be used to force the current date."
  11122. (interactive "P")
  11123. (let ((tsr org-ts-regexp) diff
  11124. (calendar-move-hook nil)
  11125. (calendar-view-holidays-initially-flag nil)
  11126. (view-calendar-holidays-initially nil)
  11127. (calendar-view-diary-initially-flag nil)
  11128. (view-diary-entries-initially nil))
  11129. (if (or (org-at-timestamp-p)
  11130. (save-excursion
  11131. (beginning-of-line 1)
  11132. (looking-at (concat ".*" tsr))))
  11133. (let ((d1 (time-to-days (current-time)))
  11134. (d2 (time-to-days
  11135. (org-time-string-to-time (match-string 1)))))
  11136. (setq diff (- d2 d1))))
  11137. (calendar)
  11138. (calendar-goto-today)
  11139. (if (and diff (not arg)) (calendar-forward-day diff))))
  11140. (defun org-get-date-from-calendar ()
  11141. "Return a list (month day year) of date at point in calendar."
  11142. (with-current-buffer "*Calendar*"
  11143. (save-match-data
  11144. (calendar-cursor-to-date))))
  11145. (defun org-date-from-calendar ()
  11146. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11147. If there is already a time stamp at the cursor position, update it."
  11148. (interactive)
  11149. (if (org-at-timestamp-p t)
  11150. (org-timestamp-change 0 'calendar)
  11151. (let ((cal-date (org-get-date-from-calendar)))
  11152. (org-insert-time-stamp
  11153. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11154. (defun org-minutes-to-hh:mm-string (m)
  11155. "Compute H:MM from a number of minutes."
  11156. (let ((h (/ m 60)))
  11157. (setq m (- m (* 60 h)))
  11158. (format org-time-clocksum-format h m)))
  11159. (defun org-hh:mm-string-to-minutes (s)
  11160. "Convert a string H:MM to a number of minutes."
  11161. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11162. (+ (* (string-to-number (match-string 1 s)) 60)
  11163. (string-to-number (match-string 2 s)))
  11164. 0))
  11165. ;;;; Agenda files
  11166. ;;;###autoload
  11167. (defun org-iswitchb (&optional arg)
  11168. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11169. With a prefix argument, restrict available to files.
  11170. With two prefix arguments, restrict available buffers to agenda files.
  11171. Due to some yet unresolved reason, the global function
  11172. `iswitchb-mode' needs to be active for this function to work."
  11173. (interactive "P")
  11174. (require 'iswitchb)
  11175. (let ((enabled iswitchb-mode) blist)
  11176. (or enabled (iswitchb-mode 1))
  11177. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11178. ((equal arg '(16)) (org-buffer-list 'agenda))
  11179. (t (org-buffer-list))))
  11180. (unwind-protect
  11181. (let ((iswitchb-make-buflist-hook
  11182. (lambda ()
  11183. (setq iswitchb-temp-buflist
  11184. (mapcar 'buffer-name blist)))))
  11185. (switch-to-buffer
  11186. (iswitchb-read-buffer
  11187. "Switch-to: " nil t))
  11188. (or enabled (iswitchb-mode -1))))))
  11189. ;;;###autoload
  11190. (defun org-ido-switchb (&optional arg)
  11191. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11192. With a prefix argument, restrict available to files.
  11193. With two prefix arguments, restrict available buffers to agenda files."
  11194. (interactive "P")
  11195. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11196. ((equal arg '(16)) (org-buffer-list 'agenda))
  11197. (t (org-buffer-list)))))
  11198. (switch-to-buffer
  11199. (org-ido-completing-read "Org buffer: "
  11200. (mapcar 'buffer-name blist)
  11201. nil t))))
  11202. (defun org-buffer-list (&optional predicate exclude-tmp)
  11203. "Return a list of Org buffers.
  11204. PREDICATE can be `export', `files' or `agenda'.
  11205. export restrict the list to Export buffers.
  11206. files restrict the list to buffers visiting Org files.
  11207. agenda restrict the list to buffers visiting agenda files.
  11208. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11209. (let* ((bfn nil)
  11210. (agenda-files (and (eq predicate 'agenda)
  11211. (mapcar 'file-truename (org-agenda-files t))))
  11212. (filter
  11213. (cond
  11214. ((eq predicate 'files)
  11215. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11216. ((eq predicate 'export)
  11217. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11218. ((eq predicate 'agenda)
  11219. (lambda (b)
  11220. (with-current-buffer b
  11221. (and (eq major-mode 'org-mode)
  11222. (setq bfn (buffer-file-name b))
  11223. (member (file-truename bfn) agenda-files)))))
  11224. (t (lambda (b) (with-current-buffer b
  11225. (or (eq major-mode 'org-mode)
  11226. (string-match "\*Org .*Export"
  11227. (buffer-name b)))))))))
  11228. (delq nil
  11229. (mapcar
  11230. (lambda(b)
  11231. (if (and (funcall filter b)
  11232. (or (not exclude-tmp)
  11233. (not (string-match "tmp" (buffer-name b)))))
  11234. b
  11235. nil))
  11236. (buffer-list)))))
  11237. (defun org-agenda-files (&optional unrestricted archives)
  11238. "Get the list of agenda files.
  11239. Optional UNRESTRICTED means return the full list even if a restriction
  11240. is currently in place.
  11241. When ARCHIVES is t, include all archive files hat are really being
  11242. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11243. `org-agenda-archives-mode' is t."
  11244. (let ((files
  11245. (cond
  11246. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11247. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11248. ((listp org-agenda-files) org-agenda-files)
  11249. (t (error "Invalid value of `org-agenda-files'")))))
  11250. (setq files (apply 'append
  11251. (mapcar (lambda (f)
  11252. (if (file-directory-p f)
  11253. (directory-files
  11254. f t org-agenda-file-regexp)
  11255. (list f)))
  11256. files)))
  11257. (when org-agenda-skip-unavailable-files
  11258. (setq files (delq nil
  11259. (mapcar (function
  11260. (lambda (file)
  11261. (and (file-readable-p file) file)))
  11262. files))))
  11263. (when (or (eq archives t)
  11264. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11265. (setq files (org-add-archive-files files)))
  11266. files))
  11267. (defun org-edit-agenda-file-list ()
  11268. "Edit the list of agenda files.
  11269. Depending on setup, this either uses customize to edit the variable
  11270. `org-agenda-files', or it visits the file that is holding the list. In the
  11271. latter case, the buffer is set up in a way that saving it automatically kills
  11272. the buffer and restores the previous window configuration."
  11273. (interactive)
  11274. (if (stringp org-agenda-files)
  11275. (let ((cw (current-window-configuration)))
  11276. (find-file org-agenda-files)
  11277. (org-set-local 'org-window-configuration cw)
  11278. (org-add-hook 'after-save-hook
  11279. (lambda ()
  11280. (set-window-configuration
  11281. (prog1 org-window-configuration
  11282. (kill-buffer (current-buffer))))
  11283. (org-install-agenda-files-menu)
  11284. (message "New agenda file list installed"))
  11285. nil 'local)
  11286. (message "%s" (substitute-command-keys
  11287. "Edit list and finish with \\[save-buffer]")))
  11288. (customize-variable 'org-agenda-files)))
  11289. (defun org-store-new-agenda-file-list (list)
  11290. "Set new value for the agenda file list and save it correctly."
  11291. (if (stringp org-agenda-files)
  11292. (let ((f org-agenda-files) b)
  11293. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11294. (with-temp-file f
  11295. (insert (mapconcat 'identity list "\n") "\n")))
  11296. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11297. (setq org-agenda-files list)
  11298. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11299. (defun org-read-agenda-file-list ()
  11300. "Read the list of agenda files from a file."
  11301. (when (file-directory-p org-agenda-files)
  11302. (error "`org-agenda-files' cannot be a single directory"))
  11303. (when (stringp org-agenda-files)
  11304. (with-temp-buffer
  11305. (insert-file-contents org-agenda-files)
  11306. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11307. ;;;###autoload
  11308. (defun org-cycle-agenda-files ()
  11309. "Cycle through the files in `org-agenda-files'.
  11310. If the current buffer visits an agenda file, find the next one in the list.
  11311. If the current buffer does not, find the first agenda file."
  11312. (interactive)
  11313. (let* ((fs (org-agenda-files t))
  11314. (files (append fs (list (car fs))))
  11315. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11316. file)
  11317. (unless files (error "No agenda files"))
  11318. (catch 'exit
  11319. (while (setq file (pop files))
  11320. (if (equal (file-truename file) tcf)
  11321. (when (car files)
  11322. (find-file (car files))
  11323. (throw 'exit t))))
  11324. (find-file (car fs)))
  11325. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11326. (defun org-agenda-file-to-front (&optional to-end)
  11327. "Move/add the current file to the top of the agenda file list.
  11328. If the file is not present in the list, it is added to the front. If it is
  11329. present, it is moved there. With optional argument TO-END, add/move to the
  11330. end of the list."
  11331. (interactive "P")
  11332. (let ((org-agenda-skip-unavailable-files nil)
  11333. (file-alist (mapcar (lambda (x)
  11334. (cons (file-truename x) x))
  11335. (org-agenda-files t)))
  11336. (ctf (file-truename buffer-file-name))
  11337. x had)
  11338. (setq x (assoc ctf file-alist) had x)
  11339. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11340. (if to-end
  11341. (setq file-alist (append (delq x file-alist) (list x)))
  11342. (setq file-alist (cons x (delq x file-alist))))
  11343. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11344. (org-install-agenda-files-menu)
  11345. (message "File %s to %s of agenda file list"
  11346. (if had "moved" "added") (if to-end "end" "front"))))
  11347. (defun org-remove-file (&optional file)
  11348. "Remove current file from the list of files in variable `org-agenda-files'.
  11349. These are the files which are being checked for agenda entries.
  11350. Optional argument FILE means, use this file instead of the current."
  11351. (interactive)
  11352. (let* ((org-agenda-skip-unavailable-files nil)
  11353. (file (or file buffer-file-name))
  11354. (true-file (file-truename file))
  11355. (afile (abbreviate-file-name file))
  11356. (files (delq nil (mapcar
  11357. (lambda (x)
  11358. (if (equal true-file
  11359. (file-truename x))
  11360. nil x))
  11361. (org-agenda-files t)))))
  11362. (if (not (= (length files) (length (org-agenda-files t))))
  11363. (progn
  11364. (org-store-new-agenda-file-list files)
  11365. (org-install-agenda-files-menu)
  11366. (message "Removed file: %s" afile))
  11367. (message "File was not in list: %s (not removed)" afile))))
  11368. (defun org-file-menu-entry (file)
  11369. (vector file (list 'find-file file) t))
  11370. (defun org-check-agenda-file (file)
  11371. "Make sure FILE exists. If not, ask user what to do."
  11372. (when (not (file-exists-p file))
  11373. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11374. (abbreviate-file-name file))
  11375. (let ((r (downcase (read-char-exclusive))))
  11376. (cond
  11377. ((equal r ?r)
  11378. (org-remove-file file)
  11379. (throw 'nextfile t))
  11380. (t (error "Abort"))))))
  11381. (defun org-get-agenda-file-buffer (file)
  11382. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11383. it to the list of buffers which might be released later."
  11384. (let ((buf (org-find-base-buffer-visiting file)))
  11385. (if buf
  11386. buf ; just return it
  11387. ;; Make a new buffer and remember it
  11388. (setq buf (find-file-noselect file))
  11389. (if buf (push buf org-agenda-new-buffers))
  11390. buf)))
  11391. (defun org-release-buffers (blist)
  11392. "Release all buffers in list, asking the user for confirmation when needed.
  11393. When a buffer is unmodified, it is just killed. When modified, it is saved
  11394. \(if the user agrees) and then killed."
  11395. (let (buf file)
  11396. (while (setq buf (pop blist))
  11397. (setq file (buffer-file-name buf))
  11398. (when (and (buffer-modified-p buf)
  11399. file
  11400. (y-or-n-p (format "Save file %s? " file)))
  11401. (with-current-buffer buf (save-buffer)))
  11402. (kill-buffer buf))))
  11403. (defun org-prepare-agenda-buffers (files)
  11404. "Create buffers for all agenda files, protect archived trees and comments."
  11405. (interactive)
  11406. (let ((pa '(:org-archived t))
  11407. (pc '(:org-comment t))
  11408. (pall '(:org-archived t :org-comment t))
  11409. (inhibit-read-only t)
  11410. (rea (concat ":" org-archive-tag ":"))
  11411. bmp file re)
  11412. (save-excursion
  11413. (save-restriction
  11414. (while (setq file (pop files))
  11415. (if (bufferp file)
  11416. (set-buffer file)
  11417. (org-check-agenda-file file)
  11418. (set-buffer (org-get-agenda-file-buffer file)))
  11419. (widen)
  11420. (setq bmp (buffer-modified-p))
  11421. (org-refresh-category-properties)
  11422. (setq org-todo-keywords-for-agenda
  11423. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11424. (setq org-done-keywords-for-agenda
  11425. (append org-done-keywords-for-agenda org-done-keywords))
  11426. (setq org-todo-keyword-alist-for-agenda
  11427. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11428. (setq org-tag-alist-for-agenda
  11429. (append org-tag-alist-for-agenda org-tag-alist))
  11430. (save-excursion
  11431. (remove-text-properties (point-min) (point-max) pall)
  11432. (when org-agenda-skip-archived-trees
  11433. (goto-char (point-min))
  11434. (while (re-search-forward rea nil t)
  11435. (if (org-on-heading-p t)
  11436. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11437. (goto-char (point-min))
  11438. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11439. (while (re-search-forward re nil t)
  11440. (add-text-properties
  11441. (match-beginning 0) (org-end-of-subtree t) pc)))
  11442. (set-buffer-modified-p bmp))))
  11443. (setq org-todo-keyword-alist-for-agenda
  11444. (org-uniquify org-todo-keyword-alist-for-agenda)
  11445. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11446. ;;;; Embedded LaTeX
  11447. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11448. "Keymap for the minor `org-cdlatex-mode'.")
  11449. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11450. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11451. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11452. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11453. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11454. (defvar org-cdlatex-texmathp-advice-is-done nil
  11455. "Flag remembering if we have applied the advice to texmathp already.")
  11456. (define-minor-mode org-cdlatex-mode
  11457. "Toggle the minor `org-cdlatex-mode'.
  11458. This mode supports entering LaTeX environment and math in LaTeX fragments
  11459. in Org-mode.
  11460. \\{org-cdlatex-mode-map}"
  11461. nil " OCDL" nil
  11462. (when org-cdlatex-mode (require 'cdlatex))
  11463. (unless org-cdlatex-texmathp-advice-is-done
  11464. (setq org-cdlatex-texmathp-advice-is-done t)
  11465. (defadvice texmathp (around org-math-always-on activate)
  11466. "Always return t in org-mode buffers.
  11467. This is because we want to insert math symbols without dollars even outside
  11468. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11469. an embedded LaTeX fragment, let texmathp do its job.
  11470. \\[org-cdlatex-mode-map]"
  11471. (interactive)
  11472. (let (p)
  11473. (cond
  11474. ((not (org-mode-p)) ad-do-it)
  11475. ((eq this-command 'cdlatex-math-symbol)
  11476. (setq ad-return-value t
  11477. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11478. (t
  11479. (let ((p (org-inside-LaTeX-fragment-p)))
  11480. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11481. (setq ad-return-value t
  11482. texmathp-why '("Org-mode embedded math" . 0))
  11483. (if p ad-do-it)))))))))
  11484. (defun turn-on-org-cdlatex ()
  11485. "Unconditionally turn on `org-cdlatex-mode'."
  11486. (org-cdlatex-mode 1))
  11487. (defun org-inside-LaTeX-fragment-p ()
  11488. "Test if point is inside a LaTeX fragment.
  11489. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11490. sequence appearing also before point.
  11491. Even though the matchers for math are configurable, this function assumes
  11492. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11493. delimiters are skipped when they have been removed by customization.
  11494. The return value is nil, or a cons cell with the delimiter and
  11495. and the position of this delimiter.
  11496. This function does a reasonably good job, but can locally be fooled by
  11497. for example currency specifications. For example it will assume being in
  11498. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11499. fragments that are properly closed, but during editing, we have to live
  11500. with the uncertainty caused by missing closing delimiters. This function
  11501. looks only before point, not after."
  11502. (catch 'exit
  11503. (let ((pos (point))
  11504. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11505. (lim (progn
  11506. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11507. (point)))
  11508. dd-on str (start 0) m re)
  11509. (goto-char pos)
  11510. (when dodollar
  11511. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11512. re (nth 1 (assoc "$" org-latex-regexps)))
  11513. (while (string-match re str start)
  11514. (cond
  11515. ((= (match-end 0) (length str))
  11516. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11517. ((= (match-end 0) (- (length str) 5))
  11518. (throw 'exit nil))
  11519. (t (setq start (match-end 0))))))
  11520. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11521. (goto-char pos)
  11522. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11523. (and (match-beginning 2) (throw 'exit nil))
  11524. ;; count $$
  11525. (while (re-search-backward "\\$\\$" lim t)
  11526. (setq dd-on (not dd-on)))
  11527. (goto-char pos)
  11528. (if dd-on (cons "$$" m))))))
  11529. (defun org-try-cdlatex-tab ()
  11530. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11531. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11532. - inside a LaTeX fragment, or
  11533. - after the first word in a line, where an abbreviation expansion could
  11534. insert a LaTeX environment."
  11535. (when org-cdlatex-mode
  11536. (cond
  11537. ((save-excursion
  11538. (skip-chars-backward "a-zA-Z0-9*")
  11539. (skip-chars-backward " \t")
  11540. (bolp))
  11541. (cdlatex-tab) t)
  11542. ((org-inside-LaTeX-fragment-p)
  11543. (cdlatex-tab) t)
  11544. (t nil))))
  11545. (defun org-cdlatex-underscore-caret (&optional arg)
  11546. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11547. Revert to the normal definition outside of these fragments."
  11548. (interactive "P")
  11549. (if (org-inside-LaTeX-fragment-p)
  11550. (call-interactively 'cdlatex-sub-superscript)
  11551. (let (org-cdlatex-mode)
  11552. (call-interactively (key-binding (vector last-input-event))))))
  11553. (defun org-cdlatex-math-modify (&optional arg)
  11554. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11555. Revert to the normal definition outside of these fragments."
  11556. (interactive "P")
  11557. (if (org-inside-LaTeX-fragment-p)
  11558. (call-interactively 'cdlatex-math-modify)
  11559. (let (org-cdlatex-mode)
  11560. (call-interactively (key-binding (vector last-input-event))))))
  11561. (defvar org-latex-fragment-image-overlays nil
  11562. "List of overlays carrying the images of latex fragments.")
  11563. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11564. (defun org-remove-latex-fragment-image-overlays ()
  11565. "Remove all overlays with LaTeX fragment images in current buffer."
  11566. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11567. (setq org-latex-fragment-image-overlays nil))
  11568. (defun org-preview-latex-fragment (&optional subtree)
  11569. "Preview the LaTeX fragment at point, or all locally or globally.
  11570. If the cursor is in a LaTeX fragment, create the image and overlay
  11571. it over the source code. If there is no fragment at point, display
  11572. all fragments in the current text, from one headline to the next. With
  11573. prefix SUBTREE, display all fragments in the current subtree. With a
  11574. double prefix `C-u C-u', or when the cursor is before the first headline,
  11575. display all fragments in the buffer.
  11576. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11577. (interactive "P")
  11578. (org-remove-latex-fragment-image-overlays)
  11579. (save-excursion
  11580. (save-restriction
  11581. (let (beg end at msg)
  11582. (cond
  11583. ((or (equal subtree '(16))
  11584. (not (save-excursion
  11585. (re-search-backward (concat "^" outline-regexp) nil t))))
  11586. (setq beg (point-min) end (point-max)
  11587. msg "Creating images for buffer...%s"))
  11588. ((equal subtree '(4))
  11589. (org-back-to-heading)
  11590. (setq beg (point) end (org-end-of-subtree t)
  11591. msg "Creating images for subtree...%s"))
  11592. (t
  11593. (if (setq at (org-inside-LaTeX-fragment-p))
  11594. (goto-char (max (point-min) (- (cdr at) 2)))
  11595. (org-back-to-heading))
  11596. (setq beg (point) end (progn (outline-next-heading) (point))
  11597. msg (if at "Creating image...%s"
  11598. "Creating images for entry...%s"))))
  11599. (message msg "")
  11600. (narrow-to-region beg end)
  11601. (goto-char beg)
  11602. (org-format-latex
  11603. (concat "ltxpng/" (file-name-sans-extension
  11604. (file-name-nondirectory
  11605. buffer-file-name)))
  11606. default-directory 'overlays msg at 'forbuffer)
  11607. (message msg "done. Use `C-c C-c' to remove images.")))))
  11608. (defvar org-latex-regexps
  11609. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11610. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11611. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11612. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11613. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11614. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11615. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11616. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11617. "Regular expressions for matching embedded LaTeX.")
  11618. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11619. "Replace LaTeX fragments with links to an image, and produce images."
  11620. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11621. (let* ((prefixnodir (file-name-nondirectory prefix))
  11622. (absprefix (expand-file-name prefix dir))
  11623. (todir (file-name-directory absprefix))
  11624. (opt org-format-latex-options)
  11625. (matchers (plist-get opt :matchers))
  11626. (re-list org-latex-regexps)
  11627. (cnt 0) txt link beg end re e checkdir
  11628. m n block linkfile movefile ov)
  11629. ;; Check if there are old images files with this prefix, and remove them
  11630. (when (file-directory-p todir)
  11631. (mapc 'delete-file
  11632. (directory-files
  11633. todir 'full
  11634. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11635. ;; Check the different regular expressions
  11636. (while (setq e (pop re-list))
  11637. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11638. block (if (nth 3 e) "\n\n" ""))
  11639. (when (member m matchers)
  11640. (goto-char (point-min))
  11641. (while (re-search-forward re nil t)
  11642. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11643. (setq txt (match-string n)
  11644. beg (match-beginning n) end (match-end n)
  11645. cnt (1+ cnt)
  11646. linkfile (format "%s_%04d.png" prefix cnt)
  11647. movefile (format "%s_%04d.png" absprefix cnt)
  11648. link (concat block "[[file:" linkfile "]]" block))
  11649. (if msg (message msg cnt))
  11650. (goto-char beg)
  11651. (unless checkdir ; make sure the directory exists
  11652. (setq checkdir t)
  11653. (or (file-directory-p todir) (make-directory todir)))
  11654. (org-create-formula-image
  11655. txt movefile opt forbuffer)
  11656. (if overlays
  11657. (progn
  11658. (setq ov (org-make-overlay beg end))
  11659. (if (featurep 'xemacs)
  11660. (progn
  11661. (org-overlay-put ov 'invisible t)
  11662. (org-overlay-put
  11663. ov 'end-glyph
  11664. (make-glyph (vector 'png :file movefile))))
  11665. (org-overlay-put
  11666. ov 'display
  11667. (list 'image :type 'png :file movefile :ascent 'center)))
  11668. (push ov org-latex-fragment-image-overlays)
  11669. (goto-char end))
  11670. (delete-region beg end)
  11671. (insert link))))))))
  11672. ;; This function borrows from Ganesh Swami's latex2png.el
  11673. (defun org-create-formula-image (string tofile options buffer)
  11674. (let* ((tmpdir (if (featurep 'xemacs)
  11675. (temp-directory)
  11676. temporary-file-directory))
  11677. (texfilebase (make-temp-name
  11678. (expand-file-name "orgtex" tmpdir)))
  11679. (texfile (concat texfilebase ".tex"))
  11680. (dvifile (concat texfilebase ".dvi"))
  11681. (pngfile (concat texfilebase ".png"))
  11682. (fnh (if (featurep 'xemacs)
  11683. (font-height (get-face-font 'default))
  11684. (face-attribute 'default :height nil)))
  11685. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11686. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11687. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11688. "Black"))
  11689. (bg (or (plist-get options (if buffer :background :html-background))
  11690. "Transparent")))
  11691. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11692. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11693. (with-temp-file texfile
  11694. (insert org-format-latex-header
  11695. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11696. (let ((dir default-directory))
  11697. (condition-case nil
  11698. (progn
  11699. (cd tmpdir)
  11700. (call-process "latex" nil nil nil texfile))
  11701. (error nil))
  11702. (cd dir))
  11703. (if (not (file-exists-p dvifile))
  11704. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11705. (condition-case nil
  11706. (call-process "dvipng" nil nil nil
  11707. "-E" "-fg" fg "-bg" bg
  11708. "-D" dpi
  11709. ;;"-x" scale "-y" scale
  11710. "-T" "tight"
  11711. "-o" pngfile
  11712. dvifile)
  11713. (error nil))
  11714. (if (not (file-exists-p pngfile))
  11715. (progn (message "Failed to create png file from %s" texfile) nil)
  11716. ;; Use the requested file name and clean up
  11717. (copy-file pngfile tofile 'replace)
  11718. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11719. (delete-file (concat texfilebase e)))
  11720. pngfile))))
  11721. (defun org-dvipng-color (attr)
  11722. "Return an rgb color specification for dvipng."
  11723. (apply 'format "rgb %s %s %s"
  11724. (mapcar 'org-normalize-color
  11725. (color-values (face-attribute 'default attr nil)))))
  11726. (defun org-normalize-color (value)
  11727. "Return string to be used as color value for an RGB component."
  11728. (format "%g" (/ value 65535.0)))
  11729. ;;;; Key bindings
  11730. ;; Make `C-c C-x' a prefix key
  11731. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11732. ;; TAB key with modifiers
  11733. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11734. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11735. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11736. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11737. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11738. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11739. ;; The following line is necessary under Suse GNU/Linux
  11740. (unless (featurep 'xemacs)
  11741. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11742. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11743. (define-key org-mode-map [backtab] 'org-shifttab)
  11744. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11745. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11746. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11747. ;; Cursor keys with modifiers
  11748. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11749. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11750. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11751. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11752. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11753. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11754. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11755. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11756. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11757. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11758. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11759. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11760. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11761. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11762. ;;; Extra keys for tty access.
  11763. ;; We only set them when really needed because otherwise the
  11764. ;; menus don't show the simple keys
  11765. (when (or org-use-extra-keys
  11766. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11767. (not window-system))
  11768. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11769. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11770. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11771. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11772. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11773. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11774. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11775. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11776. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11777. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11778. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11779. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11780. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11781. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11782. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11783. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11784. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11785. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11786. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11787. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11788. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11789. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11790. ;; All the other keys
  11791. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11792. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11793. (if (boundp 'narrow-map)
  11794. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11795. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11796. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11797. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11798. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11799. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11800. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11801. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11802. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11803. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11804. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11805. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11806. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11807. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11808. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11809. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11810. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11811. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11812. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11813. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11814. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11815. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11816. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11817. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11818. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11819. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11820. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11821. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11822. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11823. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11824. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11825. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11826. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11827. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11828. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11829. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11830. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11831. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11832. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11833. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11834. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11835. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11836. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11837. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11838. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11839. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11840. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11841. (org-defkey org-mode-map "\C-m" 'org-return)
  11842. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11843. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11844. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11845. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11846. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11847. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11848. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11849. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11850. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11851. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11852. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11853. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11854. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11855. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11856. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11857. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11858. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  11859. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11860. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11861. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11862. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11863. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11864. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11865. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11866. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11867. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11868. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11869. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11870. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11871. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11872. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11873. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11874. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  11875. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11876. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11877. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11878. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11879. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  11880. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11881. (when (featurep 'xemacs)
  11882. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11883. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11884. (defun org-self-insert-command (N)
  11885. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11886. If the cursor is in a table looking at whitespace, the whitespace is
  11887. overwritten, and the table is not marked as requiring realignment."
  11888. (interactive "p")
  11889. (if (and (org-table-p)
  11890. (progn
  11891. ;; check if we blank the field, and if that triggers align
  11892. (and (featurep 'org-table) org-table-auto-blank-field
  11893. (member last-command
  11894. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11895. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11896. ;; got extra space, this field does not determine column width
  11897. (let (org-table-may-need-update) (org-table-blank-field))
  11898. ;; no extra space, this field may determine column width
  11899. (org-table-blank-field)))
  11900. t)
  11901. (eq N 1)
  11902. (looking-at "[^|\n]* |"))
  11903. (let (org-table-may-need-update)
  11904. (goto-char (1- (match-end 0)))
  11905. (delete-backward-char 1)
  11906. (goto-char (match-beginning 0))
  11907. (self-insert-command N))
  11908. (setq org-table-may-need-update t)
  11909. (self-insert-command N)
  11910. (org-fix-tags-on-the-fly)))
  11911. (defun org-fix-tags-on-the-fly ()
  11912. (when (and (equal (char-after (point-at-bol)) ?*)
  11913. (org-on-heading-p))
  11914. (org-align-tags-here org-tags-column)))
  11915. (defun org-delete-backward-char (N)
  11916. "Like `delete-backward-char', insert whitespace at field end in tables.
  11917. When deleting backwards, in tables this function will insert whitespace in
  11918. front of the next \"|\" separator, to keep the table aligned. The table will
  11919. still be marked for re-alignment if the field did fill the entire column,
  11920. because, in this case the deletion might narrow the column."
  11921. (interactive "p")
  11922. (if (and (org-table-p)
  11923. (eq N 1)
  11924. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11925. (looking-at ".*?|"))
  11926. (let ((pos (point))
  11927. (noalign (looking-at "[^|\n\r]* |"))
  11928. (c org-table-may-need-update))
  11929. (backward-delete-char N)
  11930. (skip-chars-forward "^|")
  11931. (insert " ")
  11932. (goto-char (1- pos))
  11933. ;; noalign: if there were two spaces at the end, this field
  11934. ;; does not determine the width of the column.
  11935. (if noalign (setq org-table-may-need-update c)))
  11936. (backward-delete-char N)
  11937. (org-fix-tags-on-the-fly)))
  11938. (defun org-delete-char (N)
  11939. "Like `delete-char', but insert whitespace at field end in tables.
  11940. When deleting characters, in tables this function will insert whitespace in
  11941. front of the next \"|\" separator, to keep the table aligned. The table will
  11942. still be marked for re-alignment if the field did fill the entire column,
  11943. because, in this case the deletion might narrow the column."
  11944. (interactive "p")
  11945. (if (and (org-table-p)
  11946. (not (bolp))
  11947. (not (= (char-after) ?|))
  11948. (eq N 1))
  11949. (if (looking-at ".*?|")
  11950. (let ((pos (point))
  11951. (noalign (looking-at "[^|\n\r]* |"))
  11952. (c org-table-may-need-update))
  11953. (replace-match (concat
  11954. (substring (match-string 0) 1 -1)
  11955. " |"))
  11956. (goto-char pos)
  11957. ;; noalign: if there were two spaces at the end, this field
  11958. ;; does not determine the width of the column.
  11959. (if noalign (setq org-table-may-need-update c)))
  11960. (delete-char N))
  11961. (delete-char N)
  11962. (org-fix-tags-on-the-fly)))
  11963. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11964. (put 'org-self-insert-command 'delete-selection t)
  11965. (put 'orgtbl-self-insert-command 'delete-selection t)
  11966. (put 'org-delete-char 'delete-selection 'supersede)
  11967. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11968. (put 'org-yank 'delete-selection 'yank)
  11969. ;; Make `flyspell-mode' delay after some commands
  11970. (put 'org-self-insert-command 'flyspell-delayed t)
  11971. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11972. (put 'org-delete-char 'flyspell-delayed t)
  11973. (put 'org-delete-backward-char 'flyspell-delayed t)
  11974. ;; Make pabbrev-mode expand after org-mode commands
  11975. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11976. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11977. ;; How to do this: Measure non-white length of current string
  11978. ;; If equal to column width, we should realign.
  11979. (defun org-remap (map &rest commands)
  11980. "In MAP, remap the functions given in COMMANDS.
  11981. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11982. (let (new old)
  11983. (while commands
  11984. (setq old (pop commands) new (pop commands))
  11985. (if (fboundp 'command-remapping)
  11986. (org-defkey map (vector 'remap old) new)
  11987. (substitute-key-definition old new map global-map)))))
  11988. (when (eq org-enable-table-editor 'optimized)
  11989. ;; If the user wants maximum table support, we need to hijack
  11990. ;; some standard editing functions
  11991. (org-remap org-mode-map
  11992. 'self-insert-command 'org-self-insert-command
  11993. 'delete-char 'org-delete-char
  11994. 'delete-backward-char 'org-delete-backward-char)
  11995. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11996. (defun org-modifier-cursor-error ()
  11997. "Throw an error, a modified cursor command was applied in wrong context."
  11998. (error "This command is active in special context like tables, headlines or items"))
  11999. (defun org-shiftselect-error ()
  12000. "Throw an error because Shift-Cursor command was applied in wrong context."
  12001. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12002. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12003. (error "This command works only in special context like headlines or timestamps.")))
  12004. (defun org-call-for-shift-select (cmd)
  12005. (let ((this-command-keys-shift-translated t))
  12006. (call-interactively cmd)))
  12007. (defun org-shifttab (&optional arg)
  12008. "Global visibility cycling or move to previous table field.
  12009. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12010. on context.
  12011. See the individual commands for more information."
  12012. (interactive "P")
  12013. (cond
  12014. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12015. ((integerp arg)
  12016. (message "Content view to level: %d" arg)
  12017. (org-content (prefix-numeric-value arg))
  12018. (setq org-cycle-global-status 'overview))
  12019. (t (call-interactively 'org-global-cycle))))
  12020. (defun org-shiftmetaleft ()
  12021. "Promote subtree or delete table column.
  12022. Calls `org-promote-subtree', `org-outdent-item',
  12023. or `org-table-delete-column', depending on context.
  12024. See the individual commands for more information."
  12025. (interactive)
  12026. (cond
  12027. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12028. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12029. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12030. (t (org-modifier-cursor-error))))
  12031. (defun org-shiftmetaright ()
  12032. "Demote subtree or insert table column.
  12033. Calls `org-demote-subtree', `org-indent-item',
  12034. or `org-table-insert-column', depending on context.
  12035. See the individual commands for more information."
  12036. (interactive)
  12037. (cond
  12038. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12039. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12040. ((org-at-item-p) (call-interactively 'org-indent-item))
  12041. (t (org-modifier-cursor-error))))
  12042. (defun org-shiftmetaup (&optional arg)
  12043. "Move subtree up or kill table row.
  12044. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12045. `org-move-item-up' depending on context. See the individual commands
  12046. for more information."
  12047. (interactive "P")
  12048. (cond
  12049. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12050. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12051. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12052. (t (org-modifier-cursor-error))))
  12053. (defun org-shiftmetadown (&optional arg)
  12054. "Move subtree down or insert table row.
  12055. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12056. `org-move-item-down', depending on context. See the individual
  12057. commands for more information."
  12058. (interactive "P")
  12059. (cond
  12060. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12061. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12062. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12063. (t (org-modifier-cursor-error))))
  12064. (defun org-metaleft (&optional arg)
  12065. "Promote heading or move table column to left.
  12066. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12067. With no specific context, calls the Emacs default `backward-word'.
  12068. See the individual commands for more information."
  12069. (interactive "P")
  12070. (cond
  12071. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12072. ((or (org-on-heading-p) (org-region-active-p))
  12073. (call-interactively 'org-do-promote))
  12074. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12075. (t (call-interactively 'backward-word))))
  12076. (defun org-metaright (&optional arg)
  12077. "Demote subtree or move table column to right.
  12078. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12079. With no specific context, calls the Emacs default `forward-word'.
  12080. See the individual commands for more information."
  12081. (interactive "P")
  12082. (cond
  12083. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12084. ((or (org-on-heading-p) (org-region-active-p))
  12085. (call-interactively 'org-do-demote))
  12086. ((org-at-item-p) (call-interactively 'org-indent-item))
  12087. (t (call-interactively 'forward-word))))
  12088. (defun org-metaup (&optional arg)
  12089. "Move subtree up or move table row up.
  12090. Calls `org-move-subtree-up' or `org-table-move-row' or
  12091. `org-move-item-up', depending on context. See the individual commands
  12092. for more information."
  12093. (interactive "P")
  12094. (cond
  12095. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12096. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12097. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12098. (t (transpose-lines 1) (beginning-of-line -1))))
  12099. (defun org-metadown (&optional arg)
  12100. "Move subtree down or move table row down.
  12101. Calls `org-move-subtree-down' or `org-table-move-row' or
  12102. `org-move-item-down', depending on context. See the individual
  12103. commands for more information."
  12104. (interactive "P")
  12105. (cond
  12106. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12107. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12108. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12109. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12110. (defun org-shiftup (&optional arg)
  12111. "Increase item in timestamp or increase priority of current headline.
  12112. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12113. depending on context. See the individual commands for more information."
  12114. (interactive "P")
  12115. (cond
  12116. ((and org-support-shift-select (org-region-active-p))
  12117. (org-call-for-shift-select 'previous-line))
  12118. ((org-at-timestamp-p t)
  12119. (call-interactively (if org-edit-timestamp-down-means-later
  12120. 'org-timestamp-down 'org-timestamp-up)))
  12121. ((and (not (eq org-support-shift-select 'always))
  12122. (org-on-heading-p))
  12123. (call-interactively 'org-priority-up))
  12124. ((and (not org-support-shift-select) (org-at-item-p))
  12125. (call-interactively 'org-previous-item))
  12126. ((org-clocktable-try-shift 'up arg))
  12127. (org-support-shift-select
  12128. (org-call-for-shift-select 'previous-line))
  12129. (t (org-shiftselect-error))))
  12130. (defun org-shiftdown (&optional arg)
  12131. "Decrease item in timestamp or decrease priority of current headline.
  12132. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12133. depending on context. See the individual commands for more information."
  12134. (interactive "P")
  12135. (cond
  12136. ((and org-support-shift-select (org-region-active-p))
  12137. (org-call-for-shift-select 'next-line))
  12138. ((org-at-timestamp-p t)
  12139. (call-interactively (if org-edit-timestamp-down-means-later
  12140. 'org-timestamp-up 'org-timestamp-down)))
  12141. ((and (not (eq org-support-shift-select 'always))
  12142. (org-on-heading-p))
  12143. (call-interactively 'org-priority-down))
  12144. ((and (not org-support-shift-select) (org-at-item-p))
  12145. (call-interactively 'org-next-item))
  12146. ((org-clocktable-try-shift 'down arg))
  12147. (org-support-shift-select
  12148. (org-call-for-shift-select 'next-line))
  12149. (t (org-shiftselect-error))))
  12150. (defun org-shiftright (&optional arg)
  12151. "Cycle the thing at point or in the current line, depending on context.
  12152. Depending on context, this does one of the following:
  12153. - switch a timestamp at point one day into the future
  12154. - on a headline, switch to the next TODO keyword.
  12155. - on an item, switch entire list to the next bullet type
  12156. - on a property line, switch to the next allowed value
  12157. - on a clocktable definition line, move time block into the future"
  12158. (interactive "P")
  12159. (cond
  12160. ((and org-support-shift-select (org-region-active-p))
  12161. (org-call-for-shift-select 'forward-char))
  12162. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12163. ((and (not (eq org-support-shift-select 'always))
  12164. (org-on-heading-p))
  12165. (org-call-with-arg 'org-todo 'right))
  12166. ((or (and org-support-shift-select
  12167. (not (eq org-support-shift-select 'always))
  12168. (org-at-item-bullet-p))
  12169. (and (not org-support-shift-select) (org-at-item-p)))
  12170. (org-call-with-arg 'org-cycle-list-bullet nil))
  12171. ((and (not (eq org-support-shift-select 'always))
  12172. (org-at-property-p))
  12173. (call-interactively 'org-property-next-allowed-value))
  12174. ((org-clocktable-try-shift 'right arg))
  12175. (org-support-shift-select
  12176. (org-call-for-shift-select 'forward-char))
  12177. (t (org-shiftselect-error))))
  12178. (defun org-shiftleft (&optional arg)
  12179. "Cycle the thing at point or in the current line, depending on context.
  12180. Depending on context, this does one of the following:
  12181. - switch a timestamp at point one day into the past
  12182. - on a headline, switch to the previous TODO keyword.
  12183. - on an item, switch entire list to the previous bullet type
  12184. - on a property line, switch to the previous allowed value
  12185. - on a clocktable definition line, move time block into the past"
  12186. (interactive "P")
  12187. (cond
  12188. ((and org-support-shift-select (org-region-active-p))
  12189. (org-call-for-shift-select 'backward-char))
  12190. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12191. ((and (not (eq org-support-shift-select 'always))
  12192. (org-on-heading-p))
  12193. (org-call-with-arg 'org-todo 'left))
  12194. ((or (and org-support-shift-select
  12195. (not (eq org-support-shift-select 'always))
  12196. (org-at-item-bullet-p))
  12197. (and (not org-support-shift-select) (org-at-item-p)))
  12198. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12199. ((and (not (eq org-support-shift-select 'always))
  12200. (org-at-property-p))
  12201. (call-interactively 'org-property-previous-allowed-value))
  12202. ((org-clocktable-try-shift 'left arg))
  12203. (org-support-shift-select
  12204. (org-call-for-shift-select 'backward-char))
  12205. (t (org-shiftselect-error))))
  12206. (defun org-shiftcontrolright ()
  12207. "Switch to next TODO set."
  12208. (interactive)
  12209. (cond
  12210. ((and org-support-shift-select (org-region-active-p))
  12211. (org-call-for-shift-select 'forward-word))
  12212. ((and (not (eq org-support-shift-select 'always))
  12213. (org-on-heading-p))
  12214. (org-call-with-arg 'org-todo 'nextset))
  12215. (org-support-shift-select
  12216. (org-call-for-shift-select 'forward-word))
  12217. (t (org-shiftselect-error))))
  12218. (defun org-shiftcontrolleft ()
  12219. "Switch to previous TODO set."
  12220. (interactive)
  12221. (cond
  12222. ((and org-support-shift-select (org-region-active-p))
  12223. (org-call-for-shift-select 'backward-word))
  12224. ((and (not (eq org-support-shift-select 'always))
  12225. (org-on-heading-p))
  12226. (org-call-with-arg 'org-todo 'previousset))
  12227. (org-support-shift-select
  12228. (org-call-for-shift-select 'backward-word))
  12229. (t (org-shiftselect-error))))
  12230. (defun org-ctrl-c-ret ()
  12231. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12232. (interactive)
  12233. (cond
  12234. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12235. (t (call-interactively 'org-insert-heading))))
  12236. (defun org-copy-special ()
  12237. "Copy region in table or copy current subtree.
  12238. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12239. See the individual commands for more information."
  12240. (interactive)
  12241. (call-interactively
  12242. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12243. (defun org-cut-special ()
  12244. "Cut region in table or cut current subtree.
  12245. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12246. See the individual commands for more information."
  12247. (interactive)
  12248. (call-interactively
  12249. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12250. (defun org-paste-special (arg)
  12251. "Paste rectangular region into table, or past subtree relative to level.
  12252. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12253. See the individual commands for more information."
  12254. (interactive "P")
  12255. (if (org-at-table-p)
  12256. (org-table-paste-rectangle)
  12257. (org-paste-subtree arg)))
  12258. (defun org-edit-special ()
  12259. "Call a special editor for the stuff at point.
  12260. When at a table, call the formula editor with `org-table-edit-formulas'.
  12261. When at the first line of an src example, call `org-edit-src-code'.
  12262. When in an #+include line, visit the include file. Otherwise call
  12263. `ffap' to visit the file at point."
  12264. (interactive)
  12265. (cond
  12266. ((org-at-table-p)
  12267. (call-interactively 'org-table-edit-formulas))
  12268. ((save-excursion
  12269. (beginning-of-line 1)
  12270. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12271. (find-file (org-trim (match-string 1))))
  12272. ((org-edit-src-code))
  12273. ((org-edit-fixed-width-region))
  12274. (t (call-interactively 'ffap))))
  12275. (defun org-ctrl-c-ctrl-c (&optional arg)
  12276. "Set tags in headline, or update according to changed information at point.
  12277. This command does many different things, depending on context:
  12278. - If the cursor is in a headline, prompt for tags and insert them
  12279. into the current line, aligned to `org-tags-column'. When called
  12280. with prefix arg, realign all tags in the current buffer.
  12281. - If the cursor is in one of the special #+KEYWORD lines, this
  12282. triggers scanning the buffer for these lines and updating the
  12283. information.
  12284. - If the cursor is inside a table, realign the table. This command
  12285. works even if the automatic table editor has been turned off.
  12286. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12287. the entire table.
  12288. - If the cursor is at a footnote reference or definition, jump to
  12289. the corresponding definition or references, respectively.
  12290. - If the cursor is a the beginning of a dynamic block, update it.
  12291. - If the cursor is inside a table created by the table.el package,
  12292. activate that table.
  12293. - If the current buffer is a remember buffer, close note and file
  12294. it. A prefix argument of 1 files to the default location
  12295. without further interaction. A prefix argument of 2 files to
  12296. the currently clocking task.
  12297. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12298. links in this buffer.
  12299. - If the cursor is on a numbered item in a plain list, renumber the
  12300. ordered list.
  12301. - If the cursor is on a checkbox, toggle it."
  12302. (interactive "P")
  12303. (let ((org-enable-table-editor t))
  12304. (cond
  12305. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12306. org-occur-highlights
  12307. org-latex-fragment-image-overlays)
  12308. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12309. (org-remove-occur-highlights)
  12310. (org-remove-latex-fragment-image-overlays)
  12311. (message "Temporary highlights/overlays removed from current buffer"))
  12312. ((and (local-variable-p 'org-finish-function (current-buffer))
  12313. (fboundp org-finish-function))
  12314. (funcall org-finish-function))
  12315. ((org-at-property-p)
  12316. (call-interactively 'org-property-action))
  12317. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12318. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12319. ((org-at-table.el-p)
  12320. (require 'table)
  12321. (beginning-of-line 1)
  12322. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12323. (call-interactively 'table-recognize-table))
  12324. ((org-at-table-p)
  12325. (org-table-maybe-eval-formula)
  12326. (if arg
  12327. (call-interactively 'org-table-recalculate)
  12328. (org-table-maybe-recalculate-line))
  12329. (call-interactively 'org-table-align))
  12330. ((or (org-footnote-at-reference-p)
  12331. (org-footnote-at-definition-p))
  12332. (call-interactively 'org-footnote-action))
  12333. ((org-at-item-checkbox-p)
  12334. (call-interactively 'org-toggle-checkbox))
  12335. ((org-at-item-p)
  12336. (call-interactively 'org-maybe-renumber-ordered-list))
  12337. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12338. ;; Dynamic block
  12339. (beginning-of-line 1)
  12340. (save-excursion (org-update-dblock)))
  12341. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12342. (cond
  12343. ((equal (match-string 1) "TBLFM")
  12344. ;; Recalculate the table before this line
  12345. (save-excursion
  12346. (beginning-of-line 1)
  12347. (skip-chars-backward " \r\n\t")
  12348. (if (org-at-table-p)
  12349. (org-call-with-arg 'org-table-recalculate t))))
  12350. (t
  12351. ; (org-set-regexps-and-options)
  12352. ; (org-restart-font-lock)
  12353. (let ((org-inhibit-startup t)) (org-mode-restart))
  12354. (message "Local setup has been refreshed"))))
  12355. (t (error "C-c C-c can do nothing useful at this location.")))))
  12356. (defun org-mode-restart ()
  12357. "Restart Org-mode, to scan again for special lines.
  12358. Also updates the keyword regular expressions."
  12359. (interactive)
  12360. (org-mode)
  12361. (message "Org-mode restarted"))
  12362. (defun org-kill-note-or-show-branches ()
  12363. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12364. (interactive)
  12365. (if (not org-finish-function)
  12366. (call-interactively 'show-branches)
  12367. (let ((org-note-abort t))
  12368. (funcall org-finish-function))))
  12369. (defun org-return (&optional indent)
  12370. "Goto next table row or insert a newline.
  12371. Calls `org-table-next-row' or `newline', depending on context.
  12372. See the individual commands for more information."
  12373. (interactive)
  12374. (cond
  12375. ((bobp) (if indent (newline-and-indent) (newline)))
  12376. ((and (org-at-heading-p)
  12377. (looking-at
  12378. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12379. (org-show-entry)
  12380. (end-of-line 1)
  12381. (newline))
  12382. ((org-at-table-p)
  12383. (org-table-justify-field-maybe)
  12384. (call-interactively 'org-table-next-row))
  12385. (t (if indent (newline-and-indent) (newline)))))
  12386. (defun org-return-indent ()
  12387. "Goto next table row or insert a newline and indent.
  12388. Calls `org-table-next-row' or `newline-and-indent', depending on
  12389. context. See the individual commands for more information."
  12390. (interactive)
  12391. (org-return t))
  12392. (defun org-ctrl-c-star ()
  12393. "Compute table, or change heading status of lines.
  12394. Calls `org-table-recalculate' or `org-toggle-heading',
  12395. depending on context."
  12396. (interactive)
  12397. (cond
  12398. ((org-at-table-p)
  12399. (call-interactively 'org-table-recalculate))
  12400. (t
  12401. ;; Convert all lines in region to list items
  12402. (call-interactively 'org-toggle-heading))))
  12403. (defun org-ctrl-c-minus ()
  12404. "Insert separator line in table or modify bullet status of line.
  12405. Also turns a plain line or a region of lines into list items.
  12406. Calls `org-table-insert-hline', `org-toggle-item', or
  12407. `org-cycle-list-bullet', depending on context."
  12408. (interactive)
  12409. (cond
  12410. ((org-at-table-p)
  12411. (call-interactively 'org-table-insert-hline))
  12412. ((org-region-active-p)
  12413. (call-interactively 'org-toggle-item))
  12414. ((org-in-item-p)
  12415. (call-interactively 'org-cycle-list-bullet))
  12416. (t
  12417. (call-interactively 'org-toggle-item))))
  12418. (defun org-toggle-item ()
  12419. "Convert headings or normal lines to items, items to normal lines.
  12420. If there is no active region, only the current line is considered.
  12421. If the first line in the region is a headline, convert all headlines to items.
  12422. If the first line in the region is an item, convert all items to normal lines.
  12423. If the first line is normal text, add an item bullet to each line."
  12424. (interactive)
  12425. (let (l2 l beg end)
  12426. (if (org-region-active-p)
  12427. (setq beg (region-beginning) end (region-end))
  12428. (setq beg (point-at-bol)
  12429. end (min (1+ (point-at-eol)) (point-max))))
  12430. (save-excursion
  12431. (goto-char end)
  12432. (setq l2 (org-current-line))
  12433. (goto-char beg)
  12434. (beginning-of-line 1)
  12435. (setq l (1- (org-current-line)))
  12436. (if (org-at-item-p)
  12437. ;; We already have items, de-itemize
  12438. (while (< (setq l (1+ l)) l2)
  12439. (when (org-at-item-p)
  12440. (goto-char (match-beginning 2))
  12441. (delete-region (match-beginning 2) (match-end 2))
  12442. (and (looking-at "[ \t]+") (replace-match "")))
  12443. (beginning-of-line 2))
  12444. (if (org-on-heading-p)
  12445. ;; Headings, convert to items
  12446. (while (< (setq l (1+ l)) l2)
  12447. (if (looking-at org-outline-regexp)
  12448. (replace-match "- " t t))
  12449. (beginning-of-line 2))
  12450. ;; normal lines, turn them into items
  12451. (while (< (setq l (1+ l)) l2)
  12452. (unless (org-at-item-p)
  12453. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12454. (replace-match "\\1- \\2")))
  12455. (beginning-of-line 2)))))))
  12456. (defun org-toggle-heading (&optional nstars)
  12457. "Convert headings to normal text, or items or text to headings.
  12458. If there is no active region, only the current line is considered.
  12459. If the first line is a heading, remove the stars from all headlines
  12460. in the region.
  12461. If the first line is a plain list item, turn all plain list items into
  12462. headings.
  12463. If the first line is a normal line, turn each and every line in the region
  12464. into a heading.
  12465. When converting a line into a heading, the number of stars is chosen
  12466. such that the lines become children of the current entry. However, when
  12467. a prefix argument is given, its value determines the number of stars to add."
  12468. (interactive "P")
  12469. (let (l2 l itemp beg end)
  12470. (if (org-region-active-p)
  12471. (setq beg (region-beginning) end (region-end))
  12472. (setq beg (point-at-bol)
  12473. end (min (1+ (point-at-eol)) (point-max))))
  12474. (save-excursion
  12475. (goto-char end)
  12476. (setq l2 (org-current-line))
  12477. (goto-char beg)
  12478. (beginning-of-line 1)
  12479. (setq l (1- (org-current-line)))
  12480. (if (org-on-heading-p)
  12481. ;; We already have headlines, de-star them
  12482. (while (< (setq l (1+ l)) l2)
  12483. (when (org-on-heading-p t)
  12484. (and (looking-at outline-regexp) (replace-match "")))
  12485. (beginning-of-line 2))
  12486. (setq itemp (org-at-item-p))
  12487. (let* ((stars
  12488. (if nstars
  12489. (make-string (prefix-numeric-value current-prefix-arg)
  12490. ?*)
  12491. (save-excursion
  12492. (re-search-backward org-complex-heading-regexp nil t)
  12493. (or (match-string 1) "*"))))
  12494. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12495. (rpl (concat stars add-stars " ")))
  12496. (while (< (setq l (1+ l)) l2)
  12497. (if itemp
  12498. (and (org-at-item-p) (replace-match rpl t t))
  12499. (unless (org-on-heading-p)
  12500. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12501. (replace-match (concat rpl (match-string 2))))))
  12502. (beginning-of-line 2)))))))
  12503. (defun org-meta-return (&optional arg)
  12504. "Insert a new heading or wrap a region in a table.
  12505. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12506. See the individual commands for more information."
  12507. (interactive "P")
  12508. (cond
  12509. ((org-at-table-p)
  12510. (call-interactively 'org-table-wrap-region))
  12511. (t (call-interactively 'org-insert-heading))))
  12512. ;;; Menu entries
  12513. ;; Define the Org-mode menus
  12514. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12515. '("Tbl"
  12516. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12517. ["Next Field" org-cycle (org-at-table-p)]
  12518. ["Previous Field" org-shifttab (org-at-table-p)]
  12519. ["Next Row" org-return (org-at-table-p)]
  12520. "--"
  12521. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12522. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12523. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12524. "--"
  12525. ("Column"
  12526. ["Move Column Left" org-metaleft (org-at-table-p)]
  12527. ["Move Column Right" org-metaright (org-at-table-p)]
  12528. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12529. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12530. ("Row"
  12531. ["Move Row Up" org-metaup (org-at-table-p)]
  12532. ["Move Row Down" org-metadown (org-at-table-p)]
  12533. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12534. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12535. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12536. "--"
  12537. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12538. ("Rectangle"
  12539. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12540. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12541. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12542. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12543. "--"
  12544. ("Calculate"
  12545. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12546. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12547. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12548. "--"
  12549. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12550. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12551. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12552. "--"
  12553. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12554. "--"
  12555. ["Sum Column/Rectangle" org-table-sum
  12556. (or (org-at-table-p) (org-region-active-p))]
  12557. ["Which Column?" org-table-current-column (org-at-table-p)])
  12558. ["Debug Formulas"
  12559. org-table-toggle-formula-debugger
  12560. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12561. ["Show Col/Row Numbers"
  12562. org-table-toggle-coordinate-overlays
  12563. :style toggle
  12564. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12565. "--"
  12566. ["Create" org-table-create (and (not (org-at-table-p))
  12567. org-enable-table-editor)]
  12568. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12569. ["Import from File" org-table-import (not (org-at-table-p))]
  12570. ["Export to File" org-table-export (org-at-table-p)]
  12571. "--"
  12572. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12573. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12574. '("Org"
  12575. ("Show/Hide"
  12576. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12577. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12578. ["Sparse Tree..." org-sparse-tree t]
  12579. ["Reveal Context" org-reveal t]
  12580. ["Show All" show-all t]
  12581. "--"
  12582. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12583. "--"
  12584. ["New Heading" org-insert-heading t]
  12585. ("Navigate Headings"
  12586. ["Up" outline-up-heading t]
  12587. ["Next" outline-next-visible-heading t]
  12588. ["Previous" outline-previous-visible-heading t]
  12589. ["Next Same Level" outline-forward-same-level t]
  12590. ["Previous Same Level" outline-backward-same-level t]
  12591. "--"
  12592. ["Jump" org-goto t])
  12593. ("Edit Structure"
  12594. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12595. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12596. "--"
  12597. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12598. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12599. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12600. "--"
  12601. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12602. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12603. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12604. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12605. "--"
  12606. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12607. "--"
  12608. ["Convert to odd levels" org-convert-to-odd-levels t]
  12609. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12610. ("Editing"
  12611. ["Emphasis..." org-emphasize t]
  12612. ["Edit Source Example" org-edit-special t]
  12613. "--"
  12614. ["Footnote new/jump" org-footnote-action t]
  12615. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12616. ("Archive"
  12617. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12618. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12619. ; :active t :keys "C-u C-c C-x C-a"]
  12620. ["Sparse trees open ARCHIVE trees"
  12621. (setq org-sparse-tree-open-archived-trees
  12622. (not org-sparse-tree-open-archived-trees))
  12623. :style toggle :selected org-sparse-tree-open-archived-trees]
  12624. ["Cycling opens ARCHIVE trees"
  12625. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12626. :style toggle :selected org-cycle-open-archived-trees]
  12627. "--"
  12628. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12629. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12630. ; ["Check and Move Children" (org-archive-subtree '(4))
  12631. ; :active t :keys "C-u C-c C-x C-s"]
  12632. )
  12633. "--"
  12634. ("TODO Lists"
  12635. ["TODO/DONE/-" org-todo t]
  12636. ("Select keyword"
  12637. ["Next keyword" org-shiftright (org-on-heading-p)]
  12638. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12639. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12640. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12641. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12642. ["Show TODO Tree" org-show-todo-tree t]
  12643. ["Global TODO list" org-todo-list t]
  12644. "--"
  12645. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12646. :selected org-enforce-todo-dependencies :style toggle :active t]
  12647. "Settings for tree at point"
  12648. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12649. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12650. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12651. ["Do Children parallel" org-toggle-ordered-property :style radio
  12652. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12653. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12654. "--"
  12655. ["Set Priority" org-priority t]
  12656. ["Priority Up" org-shiftup t]
  12657. ["Priority Down" org-shiftdown t])
  12658. ("TAGS and Properties"
  12659. ["Set Tags" org-set-tags-command t]
  12660. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12661. "--"
  12662. ["Set property" org-set-property t]
  12663. ["Column view of properties" org-columns t]
  12664. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12665. ("Dates and Scheduling"
  12666. ["Timestamp" org-time-stamp t]
  12667. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12668. ("Change Date"
  12669. ["1 Day Later" org-shiftright t]
  12670. ["1 Day Earlier" org-shiftleft t]
  12671. ["1 ... Later" org-shiftup t]
  12672. ["1 ... Earlier" org-shiftdown t])
  12673. ["Compute Time Range" org-evaluate-time-range t]
  12674. ["Schedule Item" org-schedule t]
  12675. ["Deadline" org-deadline t]
  12676. "--"
  12677. ["Custom time format" org-toggle-time-stamp-overlays
  12678. :style radio :selected org-display-custom-times]
  12679. "--"
  12680. ["Goto Calendar" org-goto-calendar t]
  12681. ["Date from Calendar" org-date-from-calendar t]
  12682. "--"
  12683. ["Start/Restart Timer" org-timer-start t]
  12684. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12685. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12686. ["Insert Timer String" org-timer t]
  12687. ["Insert Timer Item" org-timer-item t])
  12688. ("Logging work"
  12689. ["Clock in" org-clock-in t]
  12690. ["Clock out" org-clock-out t]
  12691. ["Clock cancel" org-clock-cancel t]
  12692. ["Goto running clock" org-clock-goto t]
  12693. ["Display times" org-clock-display t]
  12694. ["Create clock table" org-clock-report t]
  12695. "--"
  12696. ["Record DONE time"
  12697. (progn (setq org-log-done (not org-log-done))
  12698. (message "Switching to %s will %s record a timestamp"
  12699. (car org-done-keywords)
  12700. (if org-log-done "automatically" "not")))
  12701. :style toggle :selected org-log-done])
  12702. "--"
  12703. ["Agenda Command..." org-agenda t]
  12704. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12705. ("File List for Agenda")
  12706. ("Special views current file"
  12707. ["TODO Tree" org-show-todo-tree t]
  12708. ["Check Deadlines" org-check-deadlines t]
  12709. ["Timeline" org-timeline t]
  12710. ["Tags Tree" org-tags-sparse-tree t])
  12711. "--"
  12712. ("Hyperlinks"
  12713. ["Store Link (Global)" org-store-link t]
  12714. ["Insert Link" org-insert-link t]
  12715. ["Follow Link" org-open-at-point t]
  12716. "--"
  12717. ["Next link" org-next-link t]
  12718. ["Previous link" org-previous-link t]
  12719. "--"
  12720. ["Descriptive Links"
  12721. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12722. :style radio
  12723. :selected (member '(org-link) buffer-invisibility-spec)]
  12724. ["Literal Links"
  12725. (progn
  12726. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12727. :style radio
  12728. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12729. "--"
  12730. ["Export/Publish..." org-export t]
  12731. ("LaTeX"
  12732. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12733. :selected org-cdlatex-mode]
  12734. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12735. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12736. ["Modify math symbol" org-cdlatex-math-modify
  12737. (org-inside-LaTeX-fragment-p)]
  12738. ["Export LaTeX fragments as images"
  12739. (if (featurep 'org-exp)
  12740. (setq org-export-with-LaTeX-fragments
  12741. (not org-export-with-LaTeX-fragments))
  12742. (require 'org-exp))
  12743. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12744. org-export-with-LaTeX-fragments)])
  12745. "--"
  12746. ("Documentation"
  12747. ["Show Version" org-version t]
  12748. ["Info Documentation" org-info t])
  12749. ("Customize"
  12750. ["Browse Org Group" org-customize t]
  12751. "--"
  12752. ["Expand This Menu" org-create-customize-menu
  12753. (fboundp 'customize-menu-create)])
  12754. "--"
  12755. ["Refresh setup" org-mode-restart t]
  12756. ))
  12757. (defun org-info (&optional node)
  12758. "Read documentation for Org-mode in the info system.
  12759. With optional NODE, go directly to that node."
  12760. (interactive)
  12761. (info (format "(org)%s" (or node ""))))
  12762. (defun org-install-agenda-files-menu ()
  12763. (let ((bl (buffer-list)))
  12764. (save-excursion
  12765. (while bl
  12766. (set-buffer (pop bl))
  12767. (if (org-mode-p) (setq bl nil)))
  12768. (when (org-mode-p)
  12769. (easy-menu-change
  12770. '("Org") "File List for Agenda"
  12771. (append
  12772. (list
  12773. ["Edit File List" (org-edit-agenda-file-list) t]
  12774. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12775. ["Remove Current File from List" org-remove-file t]
  12776. ["Cycle through agenda files" org-cycle-agenda-files t]
  12777. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12778. "--")
  12779. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12780. ;;;; Documentation
  12781. ;;;###autoload
  12782. (defun org-require-autoloaded-modules ()
  12783. (interactive)
  12784. (mapc 'require
  12785. '(org-agenda org-archive org-attach org-clock org-colview
  12786. org-exp org-id org-export-latex org-publish
  12787. org-remember org-table org-timer)))
  12788. ;;;###autoload
  12789. (defun org-customize ()
  12790. "Call the customize function with org as argument."
  12791. (interactive)
  12792. (org-load-modules-maybe)
  12793. (org-require-autoloaded-modules)
  12794. (customize-browse 'org))
  12795. (defun org-create-customize-menu ()
  12796. "Create a full customization menu for Org-mode, insert it into the menu."
  12797. (interactive)
  12798. (org-load-modules-maybe)
  12799. (org-require-autoloaded-modules)
  12800. (if (fboundp 'customize-menu-create)
  12801. (progn
  12802. (easy-menu-change
  12803. '("Org") "Customize"
  12804. `(["Browse Org group" org-customize t]
  12805. "--"
  12806. ,(customize-menu-create 'org)
  12807. ["Set" Custom-set t]
  12808. ["Save" Custom-save t]
  12809. ["Reset to Current" Custom-reset-current t]
  12810. ["Reset to Saved" Custom-reset-saved t]
  12811. ["Reset to Standard Settings" Custom-reset-standard t]))
  12812. (message "\"Org\"-menu now contains full customization menu"))
  12813. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12814. ;;;; Miscellaneous stuff
  12815. ;;; Generally useful functions
  12816. (defun org-find-text-property-in-string (prop s)
  12817. "Return the first non-nil value of property PROP in string S."
  12818. (or (get-text-property 0 prop s)
  12819. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12820. prop s)))
  12821. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12822. "Display the given MESSAGE as a warning."
  12823. (if (fboundp 'display-warning)
  12824. (display-warning 'org message
  12825. (if (featurep 'xemacs)
  12826. 'warning
  12827. :warning))
  12828. (let ((buf (get-buffer-create "*Org warnings*")))
  12829. (with-current-buffer buf
  12830. (goto-char (point-max))
  12831. (insert "Warning (Org): " message)
  12832. (unless (bolp)
  12833. (newline)))
  12834. (display-buffer buf)
  12835. (sit-for 0))))
  12836. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12837. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12838. (if (and marker (marker-buffer marker)
  12839. (buffer-live-p (marker-buffer marker)))
  12840. (progn
  12841. (switch-to-buffer (marker-buffer marker))
  12842. (if (or (> marker (point-max)) (< marker (point-min)))
  12843. (widen))
  12844. (goto-char marker)
  12845. (org-show-context 'org-goto))
  12846. (if bookmark
  12847. (bookmark-jump bookmark)
  12848. (error "Cannot find location"))))
  12849. (defun org-quote-csv-field (s)
  12850. "Quote field for inclusion in CSV material."
  12851. (if (string-match "[\",]" s)
  12852. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12853. s))
  12854. (defun org-plist-delete (plist property)
  12855. "Delete PROPERTY from PLIST.
  12856. This is in contrast to merely setting it to 0."
  12857. (let (p)
  12858. (while plist
  12859. (if (not (eq property (car plist)))
  12860. (setq p (plist-put p (car plist) (nth 1 plist))))
  12861. (setq plist (cddr plist)))
  12862. p))
  12863. (defun org-force-self-insert (N)
  12864. "Needed to enforce self-insert under remapping."
  12865. (interactive "p")
  12866. (self-insert-command N))
  12867. (defun org-string-width (s)
  12868. "Compute width of string, ignoring invisible characters.
  12869. This ignores character with invisibility property `org-link', and also
  12870. characters with property `org-cwidth', because these will become invisible
  12871. upon the next fontification round."
  12872. (let (b l)
  12873. (when (or (eq t buffer-invisibility-spec)
  12874. (assq 'org-link buffer-invisibility-spec))
  12875. (while (setq b (text-property-any 0 (length s)
  12876. 'invisible 'org-link s))
  12877. (setq s (concat (substring s 0 b)
  12878. (substring s (or (next-single-property-change
  12879. b 'invisible s) (length s)))))))
  12880. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12881. (setq s (concat (substring s 0 b)
  12882. (substring s (or (next-single-property-change
  12883. b 'org-cwidth s) (length s))))))
  12884. (setq l (string-width s) b -1)
  12885. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12886. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12887. l))
  12888. (defun org-get-indentation (&optional line)
  12889. "Get the indentation of the current line, interpreting tabs.
  12890. When LINE is given, assume it represents a line and compute its indentation."
  12891. (if line
  12892. (if (string-match "^ *" (org-remove-tabs line))
  12893. (match-end 0))
  12894. (save-excursion
  12895. (beginning-of-line 1)
  12896. (skip-chars-forward " \t")
  12897. (current-column))))
  12898. (defun org-remove-tabs (s &optional width)
  12899. "Replace tabulators in S with spaces.
  12900. Assumes that s is a single line, starting in column 0."
  12901. (setq width (or width tab-width))
  12902. (while (string-match "\t" s)
  12903. (setq s (replace-match
  12904. (make-string
  12905. (- (* width (/ (+ (match-beginning 0) width) width))
  12906. (match-beginning 0)) ?\ )
  12907. t t s)))
  12908. s)
  12909. (defun org-fix-indentation (line ind)
  12910. "Fix indentation in LINE.
  12911. IND is a cons cell with target and minimum indentation.
  12912. If the current indentation in LINE is smaller than the minimum,
  12913. leave it alone. If it is larger than ind, set it to the target."
  12914. (let* ((l (org-remove-tabs line))
  12915. (i (org-get-indentation l))
  12916. (i1 (car ind)) (i2 (cdr ind)))
  12917. (if (>= i i2) (setq l (substring line i2)))
  12918. (if (> i1 0)
  12919. (concat (make-string i1 ?\ ) l)
  12920. l)))
  12921. (defun org-base-buffer (buffer)
  12922. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12923. (if (not buffer)
  12924. buffer
  12925. (or (buffer-base-buffer buffer)
  12926. buffer)))
  12927. (defun org-trim (s)
  12928. "Remove whitespace at beginning and end of string."
  12929. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12930. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12931. s)
  12932. (defun org-wrap (string &optional width lines)
  12933. "Wrap string to either a number of lines, or a width in characters.
  12934. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12935. that costs. If there is a word longer than WIDTH, the text is actually
  12936. wrapped to the length of that word.
  12937. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12938. many lines, whatever width that takes.
  12939. The return value is a list of lines, without newlines at the end."
  12940. (let* ((words (org-split-string string "[ \t\n]+"))
  12941. (maxword (apply 'max (mapcar 'org-string-width words)))
  12942. w ll)
  12943. (cond (width
  12944. (org-do-wrap words (max maxword width)))
  12945. (lines
  12946. (setq w maxword)
  12947. (setq ll (org-do-wrap words maxword))
  12948. (if (<= (length ll) lines)
  12949. ll
  12950. (setq ll words)
  12951. (while (> (length ll) lines)
  12952. (setq w (1+ w))
  12953. (setq ll (org-do-wrap words w)))
  12954. ll))
  12955. (t (error "Cannot wrap this")))))
  12956. (defun org-do-wrap (words width)
  12957. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12958. (let (lines line)
  12959. (while words
  12960. (setq line (pop words))
  12961. (while (and words (< (+ (length line) (length (car words))) width))
  12962. (setq line (concat line " " (pop words))))
  12963. (setq lines (push line lines)))
  12964. (nreverse lines)))
  12965. (defun org-split-string (string &optional separators)
  12966. "Splits STRING into substrings at SEPARATORS.
  12967. No empty strings are returned if there are matches at the beginning
  12968. and end of string."
  12969. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12970. (start 0)
  12971. notfirst
  12972. (list nil))
  12973. (while (and (string-match rexp string
  12974. (if (and notfirst
  12975. (= start (match-beginning 0))
  12976. (< start (length string)))
  12977. (1+ start) start))
  12978. (< (match-beginning 0) (length string)))
  12979. (setq notfirst t)
  12980. (or (eq (match-beginning 0) 0)
  12981. (and (eq (match-beginning 0) (match-end 0))
  12982. (eq (match-beginning 0) start))
  12983. (setq list
  12984. (cons (substring string start (match-beginning 0))
  12985. list)))
  12986. (setq start (match-end 0)))
  12987. (or (eq start (length string))
  12988. (setq list
  12989. (cons (substring string start)
  12990. list)))
  12991. (nreverse list)))
  12992. (defun org-context ()
  12993. "Return a list of contexts of the current cursor position.
  12994. If several contexts apply, all are returned.
  12995. Each context entry is a list with a symbol naming the context, and
  12996. two positions indicating start and end of the context. Possible
  12997. contexts are:
  12998. :headline anywhere in a headline
  12999. :headline-stars on the leading stars in a headline
  13000. :todo-keyword on a TODO keyword (including DONE) in a headline
  13001. :tags on the TAGS in a headline
  13002. :priority on the priority cookie in a headline
  13003. :item on the first line of a plain list item
  13004. :item-bullet on the bullet/number of a plain list item
  13005. :checkbox on the checkbox in a plain list item
  13006. :table in an org-mode table
  13007. :table-special on a special filed in a table
  13008. :table-table in a table.el table
  13009. :link on a hyperlink
  13010. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13011. :target on a <<target>>
  13012. :radio-target on a <<<radio-target>>>
  13013. :latex-fragment on a LaTeX fragment
  13014. :latex-preview on a LaTeX fragment with overlayed preview image
  13015. This function expects the position to be visible because it uses font-lock
  13016. faces as a help to recognize the following contexts: :table-special, :link,
  13017. and :keyword."
  13018. (let* ((f (get-text-property (point) 'face))
  13019. (faces (if (listp f) f (list f)))
  13020. (p (point)) clist o)
  13021. ;; First the large context
  13022. (cond
  13023. ((org-on-heading-p t)
  13024. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13025. (when (progn
  13026. (beginning-of-line 1)
  13027. (looking-at org-todo-line-tags-regexp))
  13028. (push (org-point-in-group p 1 :headline-stars) clist)
  13029. (push (org-point-in-group p 2 :todo-keyword) clist)
  13030. (push (org-point-in-group p 4 :tags) clist))
  13031. (goto-char p)
  13032. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13033. (if (looking-at "\\[#[A-Z0-9]\\]")
  13034. (push (org-point-in-group p 0 :priority) clist)))
  13035. ((org-at-item-p)
  13036. (push (org-point-in-group p 2 :item-bullet) clist)
  13037. (push (list :item (point-at-bol)
  13038. (save-excursion (org-end-of-item) (point)))
  13039. clist)
  13040. (and (org-at-item-checkbox-p)
  13041. (push (org-point-in-group p 0 :checkbox) clist)))
  13042. ((org-at-table-p)
  13043. (push (list :table (org-table-begin) (org-table-end)) clist)
  13044. (if (memq 'org-formula faces)
  13045. (push (list :table-special
  13046. (previous-single-property-change p 'face)
  13047. (next-single-property-change p 'face)) clist)))
  13048. ((org-at-table-p 'any)
  13049. (push (list :table-table) clist)))
  13050. (goto-char p)
  13051. ;; Now the small context
  13052. (cond
  13053. ((org-at-timestamp-p)
  13054. (push (org-point-in-group p 0 :timestamp) clist))
  13055. ((memq 'org-link faces)
  13056. (push (list :link
  13057. (previous-single-property-change p 'face)
  13058. (next-single-property-change p 'face)) clist))
  13059. ((memq 'org-special-keyword faces)
  13060. (push (list :keyword
  13061. (previous-single-property-change p 'face)
  13062. (next-single-property-change p 'face)) clist))
  13063. ((org-on-target-p)
  13064. (push (org-point-in-group p 0 :target) clist)
  13065. (goto-char (1- (match-beginning 0)))
  13066. (if (looking-at org-radio-target-regexp)
  13067. (push (org-point-in-group p 0 :radio-target) clist))
  13068. (goto-char p))
  13069. ((setq o (car (delq nil
  13070. (mapcar
  13071. (lambda (x)
  13072. (if (memq x org-latex-fragment-image-overlays) x))
  13073. (org-overlays-at (point))))))
  13074. (push (list :latex-fragment
  13075. (org-overlay-start o) (org-overlay-end o)) clist)
  13076. (push (list :latex-preview
  13077. (org-overlay-start o) (org-overlay-end o)) clist))
  13078. ((org-inside-LaTeX-fragment-p)
  13079. ;; FIXME: positions wrong.
  13080. (push (list :latex-fragment (point) (point)) clist)))
  13081. (setq clist (nreverse (delq nil clist)))
  13082. clist))
  13083. ;; FIXME: Compare with at-regexp-p Do we need both?
  13084. (defun org-in-regexp (re &optional nlines visually)
  13085. "Check if point is inside a match of regexp.
  13086. Normally only the current line is checked, but you can include NLINES extra
  13087. lines both before and after point into the search.
  13088. If VISUALLY is set, require that the cursor is not after the match but
  13089. really on, so that the block visually is on the match."
  13090. (catch 'exit
  13091. (let ((pos (point))
  13092. (eol (point-at-eol (+ 1 (or nlines 0))))
  13093. (inc (if visually 1 0)))
  13094. (save-excursion
  13095. (beginning-of-line (- 1 (or nlines 0)))
  13096. (while (re-search-forward re eol t)
  13097. (if (and (<= (match-beginning 0) pos)
  13098. (>= (+ inc (match-end 0)) pos))
  13099. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13100. (defun org-at-regexp-p (regexp)
  13101. "Is point inside a match of REGEXP in the current line?"
  13102. (catch 'exit
  13103. (save-excursion
  13104. (let ((pos (point)) (end (point-at-eol)))
  13105. (beginning-of-line 1)
  13106. (while (re-search-forward regexp end t)
  13107. (if (and (<= (match-beginning 0) pos)
  13108. (>= (match-end 0) pos))
  13109. (throw 'exit t)))
  13110. nil))))
  13111. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13112. "Call `multi-occur' with buffers for all agenda files."
  13113. (interactive "sOrg-files matching: \np")
  13114. (let* ((files (org-agenda-files))
  13115. (tnames (mapcar 'file-truename files))
  13116. (extra org-agenda-text-search-extra-files)
  13117. f)
  13118. (when (eq (car extra) 'agenda-archives)
  13119. (setq extra (cdr extra))
  13120. (setq files (org-add-archive-files files)))
  13121. (while (setq f (pop extra))
  13122. (unless (member (file-truename f) tnames)
  13123. (add-to-list 'files f 'append)
  13124. (add-to-list 'tnames (file-truename f) 'append)))
  13125. (multi-occur
  13126. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13127. regexp)))
  13128. (if (boundp 'occur-mode-find-occurrence-hook)
  13129. ;; Emacs 23
  13130. (add-hook 'occur-mode-find-occurrence-hook
  13131. (lambda ()
  13132. (when (org-mode-p)
  13133. (org-reveal))))
  13134. ;; Emacs 22
  13135. (defadvice occur-mode-goto-occurrence
  13136. (after org-occur-reveal activate)
  13137. (and (org-mode-p) (org-reveal)))
  13138. (defadvice occur-mode-goto-occurrence-other-window
  13139. (after org-occur-reveal activate)
  13140. (and (org-mode-p) (org-reveal)))
  13141. (defadvice occur-mode-display-occurrence
  13142. (after org-occur-reveal activate)
  13143. (when (org-mode-p)
  13144. (let ((pos (occur-mode-find-occurrence)))
  13145. (with-current-buffer (marker-buffer pos)
  13146. (save-excursion
  13147. (goto-char pos)
  13148. (org-reveal)))))))
  13149. (defun org-uniquify (list)
  13150. "Remove duplicate elements from LIST."
  13151. (let (res)
  13152. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13153. res))
  13154. (defun org-delete-all (elts list)
  13155. "Remove all elements in ELTS from LIST."
  13156. (while elts
  13157. (setq list (delete (pop elts) list)))
  13158. list)
  13159. (defun org-back-over-empty-lines ()
  13160. "Move backwards over whitespace, to the beginning of the first empty line.
  13161. Returns the number of empty lines passed."
  13162. (let ((pos (point)))
  13163. (skip-chars-backward " \t\n\r")
  13164. (beginning-of-line 2)
  13165. (goto-char (min (point) pos))
  13166. (count-lines (point) pos)))
  13167. (defun org-skip-whitespace ()
  13168. (skip-chars-forward " \t\n\r"))
  13169. (defun org-point-in-group (point group &optional context)
  13170. "Check if POINT is in match-group GROUP.
  13171. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13172. match. If the match group does ot exist or point is not inside it,
  13173. return nil."
  13174. (and (match-beginning group)
  13175. (>= point (match-beginning group))
  13176. (<= point (match-end group))
  13177. (if context
  13178. (list context (match-beginning group) (match-end group))
  13179. t)))
  13180. (defun org-switch-to-buffer-other-window (&rest args)
  13181. "Switch to buffer in a second window on the current frame.
  13182. In particular, do not allow pop-up frames."
  13183. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13184. special-display-function)
  13185. (apply 'switch-to-buffer-other-window args)))
  13186. (defun org-combine-plists (&rest plists)
  13187. "Create a single property list from all plists in PLISTS.
  13188. The process starts by copying the first list, and then setting properties
  13189. from the other lists. Settings in the last list are the most significant
  13190. ones and overrule settings in the other lists."
  13191. (let ((rtn (copy-sequence (pop plists)))
  13192. p v ls)
  13193. (while plists
  13194. (setq ls (pop plists))
  13195. (while ls
  13196. (setq p (pop ls) v (pop ls))
  13197. (setq rtn (plist-put rtn p v))))
  13198. rtn))
  13199. (defun org-move-line-down (arg)
  13200. "Move the current line down. With prefix argument, move it past ARG lines."
  13201. (interactive "p")
  13202. (let ((col (current-column))
  13203. beg end pos)
  13204. (beginning-of-line 1) (setq beg (point))
  13205. (beginning-of-line 2) (setq end (point))
  13206. (beginning-of-line (+ 1 arg))
  13207. (setq pos (move-marker (make-marker) (point)))
  13208. (insert (delete-and-extract-region beg end))
  13209. (goto-char pos)
  13210. (org-move-to-column col)))
  13211. (defun org-move-line-up (arg)
  13212. "Move the current line up. With prefix argument, move it past ARG lines."
  13213. (interactive "p")
  13214. (let ((col (current-column))
  13215. beg end pos)
  13216. (beginning-of-line 1) (setq beg (point))
  13217. (beginning-of-line 2) (setq end (point))
  13218. (beginning-of-line (- arg))
  13219. (setq pos (move-marker (make-marker) (point)))
  13220. (insert (delete-and-extract-region beg end))
  13221. (goto-char pos)
  13222. (org-move-to-column col)))
  13223. (defun org-replace-escapes (string table)
  13224. "Replace %-escapes in STRING with values in TABLE.
  13225. TABLE is an association list with keys like \"%a\" and string values.
  13226. The sequences in STRING may contain normal field width and padding information,
  13227. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13228. so values can contain further %-escapes if they are define later in TABLE."
  13229. (let ((case-fold-search nil)
  13230. e re rpl)
  13231. (while (setq e (pop table))
  13232. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13233. (while (string-match re string)
  13234. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13235. (cdr e)))
  13236. (setq string (replace-match rpl t t string))))
  13237. string))
  13238. (defun org-sublist (list start end)
  13239. "Return a section of LIST, from START to END.
  13240. Counting starts at 1."
  13241. (let (rtn (c start))
  13242. (setq list (nthcdr (1- start) list))
  13243. (while (and list (<= c end))
  13244. (push (pop list) rtn)
  13245. (setq c (1+ c)))
  13246. (nreverse rtn)))
  13247. (defun org-find-base-buffer-visiting (file)
  13248. "Like `find-buffer-visiting' but alway return the base buffer and
  13249. not an indirect buffer."
  13250. (let ((buf (find-buffer-visiting file)))
  13251. (if buf
  13252. (or (buffer-base-buffer buf) buf)
  13253. nil)))
  13254. (defun org-image-file-name-regexp (&optional extensions)
  13255. "Return regexp matching the file names of images.
  13256. If EXTENSIONS is given, only match these."
  13257. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13258. (image-file-name-regexp)
  13259. (let ((image-file-name-extensions
  13260. (or extensions
  13261. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13262. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13263. (concat "\\."
  13264. (regexp-opt (nconc (mapcar 'upcase
  13265. image-file-name-extensions)
  13266. image-file-name-extensions)
  13267. t)
  13268. "\\'"))))
  13269. (defun org-file-image-p (file &optional extensions)
  13270. "Return non-nil if FILE is an image."
  13271. (save-match-data
  13272. (string-match (org-image-file-name-regexp extensions) file)))
  13273. (defun org-get-cursor-date ()
  13274. "Return the date at cursor in as a time.
  13275. This works in the calendar and in the agenda, anywhere else it just
  13276. returns the current time."
  13277. (let (date day defd)
  13278. (cond
  13279. ((eq major-mode 'calendar-mode)
  13280. (setq date (calendar-cursor-to-date)
  13281. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13282. ((eq major-mode 'org-agenda-mode)
  13283. (setq day (get-text-property (point) 'day))
  13284. (if day
  13285. (setq date (calendar-gregorian-from-absolute day)
  13286. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13287. (nth 2 date))))))
  13288. (or defd (current-time))))
  13289. (defvar org-agenda-action-marker (make-marker)
  13290. "Marker pointing to the entry for the next agenda action.")
  13291. (defun org-mark-entry-for-agenda-action ()
  13292. "Mark the current entry as target of an agenda action.
  13293. Agenda actions are actions executed from the agenda with the key `k',
  13294. which make use of the date at the cursor."
  13295. (interactive)
  13296. (move-marker org-agenda-action-marker
  13297. (save-excursion (org-back-to-heading t) (point))
  13298. (current-buffer))
  13299. (message
  13300. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13301. ;;; Paragraph filling stuff.
  13302. ;; We want this to be just right, so use the full arsenal.
  13303. (defun org-indent-line-function ()
  13304. "Indent line like previous, but further if previous was headline or item."
  13305. (interactive)
  13306. (let* ((pos (point))
  13307. (itemp (org-at-item-p))
  13308. column bpos bcol tpos tcol bullet btype bullet-type)
  13309. ;; Find the previous relevant line
  13310. (beginning-of-line 1)
  13311. (cond
  13312. ((looking-at "#") (setq column 0))
  13313. ((looking-at "\\*+ ") (setq column 0))
  13314. (t
  13315. (beginning-of-line 0)
  13316. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13317. (beginning-of-line 0))
  13318. (cond
  13319. ((looking-at "\\*+[ \t]+")
  13320. (if (not org-adapt-indentation)
  13321. (setq column 0)
  13322. (goto-char (match-end 0))
  13323. (setq column (current-column))))
  13324. ((org-in-item-p)
  13325. (org-beginning-of-item)
  13326. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13327. (setq bpos (match-beginning 1) tpos (match-end 0)
  13328. bcol (progn (goto-char bpos) (current-column))
  13329. tcol (progn (goto-char tpos) (current-column))
  13330. bullet (match-string 1)
  13331. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13332. (if (> tcol (+ bcol org-description-max-indent))
  13333. (setq tcol (+ bcol 5)))
  13334. (if (not itemp)
  13335. (setq column tcol)
  13336. (goto-char pos)
  13337. (beginning-of-line 1)
  13338. (if (looking-at "\\S-")
  13339. (progn
  13340. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13341. (setq bullet (match-string 1)
  13342. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13343. (setq column (if (equal btype bullet-type) bcol tcol)))
  13344. (setq column (org-get-indentation)))))
  13345. (t (setq column (org-get-indentation))))))
  13346. (goto-char pos)
  13347. (if (<= (current-column) (current-indentation))
  13348. (org-indent-line-to column)
  13349. (save-excursion (org-indent-line-to column)))
  13350. (setq column (current-column))
  13351. (beginning-of-line 1)
  13352. (if (looking-at
  13353. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13354. (replace-match (concat "\\1" (format org-property-format
  13355. (match-string 2) (match-string 3)))
  13356. t nil))
  13357. (org-move-to-column column)))
  13358. (defun org-set-autofill-regexps ()
  13359. (interactive)
  13360. ;; In the paragraph separator we include headlines, because filling
  13361. ;; text in a line directly attached to a headline would otherwise
  13362. ;; fill the headline as well.
  13363. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13364. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13365. ;; The paragraph starter includes hand-formatted lists.
  13366. (org-set-local 'paragraph-start
  13367. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13368. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13369. ;; But only if the user has not turned off tables or fixed-width regions
  13370. (org-set-local
  13371. 'auto-fill-inhibit-regexp
  13372. (concat "\\*+ \\|#\\+"
  13373. "\\|[ \t]*" org-keyword-time-regexp
  13374. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13375. (concat
  13376. "\\|[ \t]*["
  13377. (if org-enable-table-editor "|" "")
  13378. (if org-enable-fixed-width-editor ":" "")
  13379. "]"))))
  13380. ;; We use our own fill-paragraph function, to make sure that tables
  13381. ;; and fixed-width regions are not wrapped. That function will pass
  13382. ;; through to `fill-paragraph' when appropriate.
  13383. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13384. ; Adaptive filling: To get full control, first make sure that
  13385. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13386. (org-set-local 'adaptive-fill-regexp "\000")
  13387. (org-set-local 'adaptive-fill-function
  13388. 'org-adaptive-fill-function)
  13389. (org-set-local
  13390. 'align-mode-rules-list
  13391. '((org-in-buffer-settings
  13392. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13393. (modes . '(org-mode))))))
  13394. (defun org-fill-paragraph (&optional justify)
  13395. "Re-align a table, pass through to fill-paragraph if no table."
  13396. (let ((table-p (org-at-table-p))
  13397. (table.el-p (org-at-table.el-p)))
  13398. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13399. (save-excursion (goto-char (point-at-bol))
  13400. (looking-at outline-regexp)))
  13401. t) ; skip headlines
  13402. (table.el-p t) ; skip table.el tables
  13403. (table-p (org-table-align) t) ; align org-mode tables
  13404. (t nil)))) ; call paragraph-fill
  13405. ;; For reference, this is the default value of adaptive-fill-regexp
  13406. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13407. (defun org-adaptive-fill-function ()
  13408. "Return a fill prefix for org-mode files.
  13409. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13410. work correctly."
  13411. (cond ((looking-at "#[ \t]+")
  13412. (match-string 0))
  13413. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13414. (save-excursion
  13415. (if (> (match-end 1) (+ (match-beginning 1)
  13416. org-description-max-indent))
  13417. (goto-char (+ (match-beginning 1) 5))
  13418. (goto-char (match-end 0)))
  13419. (make-string (current-column) ?\ )))
  13420. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13421. (save-excursion
  13422. (goto-char (match-end 0))
  13423. (make-string (current-column) ?\ )))
  13424. (t nil)))
  13425. ;;; Other stuff.
  13426. (defun org-toggle-fixed-width-section (arg)
  13427. "Toggle the fixed-width export.
  13428. If there is no active region, the QUOTE keyword at the current headline is
  13429. inserted or removed. When present, it causes the text between this headline
  13430. and the next to be exported as fixed-width text, and unmodified.
  13431. If there is an active region, this command adds or removes a colon as the
  13432. first character of this line. If the first character of a line is a colon,
  13433. this line is also exported in fixed-width font."
  13434. (interactive "P")
  13435. (let* ((cc 0)
  13436. (regionp (org-region-active-p))
  13437. (beg (if regionp (region-beginning) (point)))
  13438. (end (if regionp (region-end)))
  13439. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13440. (case-fold-search nil)
  13441. (re "[ \t]*\\(:\\)")
  13442. off)
  13443. (if regionp
  13444. (save-excursion
  13445. (goto-char beg)
  13446. (setq cc (current-column))
  13447. (beginning-of-line 1)
  13448. (setq off (looking-at re))
  13449. (while (> nlines 0)
  13450. (setq nlines (1- nlines))
  13451. (beginning-of-line 1)
  13452. (cond
  13453. (arg
  13454. (org-move-to-column cc t)
  13455. (insert ":\n")
  13456. (forward-line -1))
  13457. ((and off (looking-at re))
  13458. (replace-match "" t t nil 1))
  13459. ((not off) (org-move-to-column cc t) (insert ":")))
  13460. (forward-line 1)))
  13461. (save-excursion
  13462. (org-back-to-heading)
  13463. (if (looking-at (concat outline-regexp
  13464. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13465. (replace-match "" t t nil 1)
  13466. (if (looking-at outline-regexp)
  13467. (progn
  13468. (goto-char (match-end 0))
  13469. (insert org-quote-string " "))))))))
  13470. ;;;; Functions extending outline functionality
  13471. (defun org-beginning-of-line (&optional arg)
  13472. "Go to the beginning of the current line. If that is invisible, continue
  13473. to a visible line beginning. This makes the function of C-a more intuitive.
  13474. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13475. first attempt, and only move to after the tags when the cursor is already
  13476. beyond the end of the headline."
  13477. (interactive "P")
  13478. (let ((pos (point)) refpos)
  13479. (beginning-of-line 1)
  13480. (if (bobp)
  13481. nil
  13482. (backward-char 1)
  13483. (if (org-invisible-p)
  13484. (while (and (not (bobp)) (org-invisible-p))
  13485. (backward-char 1)
  13486. (beginning-of-line 1))
  13487. (forward-char 1)))
  13488. (when org-special-ctrl-a/e
  13489. (cond
  13490. ((and (looking-at org-complex-heading-regexp)
  13491. (= (char-after (match-end 1)) ?\ ))
  13492. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13493. (point-at-eol)))
  13494. (goto-char
  13495. (if (eq org-special-ctrl-a/e t)
  13496. (cond ((> pos refpos) refpos)
  13497. ((= pos (point)) refpos)
  13498. (t (point)))
  13499. (cond ((> pos (point)) (point))
  13500. ((not (eq last-command this-command)) (point))
  13501. (t refpos)))))
  13502. ((org-at-item-p)
  13503. (goto-char
  13504. (if (eq org-special-ctrl-a/e t)
  13505. (cond ((> pos (match-end 4)) (match-end 4))
  13506. ((= pos (point)) (match-end 4))
  13507. (t (point)))
  13508. (cond ((> pos (point)) (point))
  13509. ((not (eq last-command this-command)) (point))
  13510. (t (match-end 4))))))))
  13511. (org-no-warnings
  13512. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13513. (defun org-end-of-line (&optional arg)
  13514. "Go to the end of the line.
  13515. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13516. first attempt, and only move to after the tags when the cursor is already
  13517. beyond the end of the headline."
  13518. (interactive "P")
  13519. (if (or (not org-special-ctrl-a/e)
  13520. (not (org-on-heading-p)))
  13521. (end-of-line arg)
  13522. (let ((pos (point)))
  13523. (beginning-of-line 1)
  13524. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13525. (if (eq org-special-ctrl-a/e t)
  13526. (if (or (< pos (match-beginning 1))
  13527. (= pos (match-end 0)))
  13528. (goto-char (match-beginning 1))
  13529. (goto-char (match-end 0)))
  13530. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13531. (goto-char (match-end 0))
  13532. (goto-char (match-beginning 1))))
  13533. (end-of-line arg))))
  13534. (org-no-warnings
  13535. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13536. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13537. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13538. (defun org-kill-line (&optional arg)
  13539. "Kill line, to tags or end of line."
  13540. (interactive "P")
  13541. (cond
  13542. ((or (not org-special-ctrl-k)
  13543. (bolp)
  13544. (not (org-on-heading-p)))
  13545. (call-interactively 'kill-line))
  13546. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13547. (kill-region (point) (match-beginning 1))
  13548. (org-set-tags nil t))
  13549. (t (kill-region (point) (point-at-eol)))))
  13550. (define-key org-mode-map "\C-k" 'org-kill-line)
  13551. (defun org-yank (&optional arg)
  13552. "Yank. If the kill is a subtree, treat it specially.
  13553. This command will look at the current kill and check if is a single
  13554. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13555. cursor is at the beginning of a line or after the stars of a currently
  13556. empty headline, then the yank is handled specially. How exactly depends
  13557. on the value of the following variables, both set by default.
  13558. org-yank-folded-subtrees
  13559. When set, the subtree(s) will be folded after insertion, but only
  13560. if doing so would now swallow text after the yanked text.
  13561. org-yank-adjusted-subtrees
  13562. When set, the subtree will be promoted or demoted in order to
  13563. fit into the local outline tree structure, which means that the level
  13564. will be adjusted so that it becomes the smaller one of the two
  13565. *visible* surrounding headings.
  13566. Any prefix to this command will cause `yank' to be called directly with
  13567. no special treatment. In particular, a simple `C-u' prefix will just
  13568. plainly yank the text as it is.
  13569. \[1] Basically, the test checks if the first non-white line is a heading
  13570. and if there are no other headings with fewer stars."
  13571. (interactive "P")
  13572. (setq this-command 'yank)
  13573. (if arg
  13574. (call-interactively 'yank)
  13575. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13576. (and (org-kill-is-subtree-p)
  13577. (or (bolp)
  13578. (and (looking-at "[ \t]*$")
  13579. (string-match
  13580. "\\`\\*+\\'"
  13581. (buffer-substring (point-at-bol) (point)))))))
  13582. swallowp)
  13583. (cond
  13584. ((and subtreep org-yank-folded-subtrees)
  13585. (let ((beg (point))
  13586. end)
  13587. (if (and subtreep org-yank-adjusted-subtrees)
  13588. (org-paste-subtree nil nil 'for-yank)
  13589. (call-interactively 'yank))
  13590. (setq end (point))
  13591. (goto-char beg)
  13592. (when (and (bolp) subtreep
  13593. (not (setq swallowp
  13594. (org-yank-folding-would-swallow-text beg end))))
  13595. (or (looking-at outline-regexp)
  13596. (re-search-forward (concat "^" outline-regexp) end t))
  13597. (while (and (< (point) end) (looking-at outline-regexp))
  13598. (hide-subtree)
  13599. (org-cycle-show-empty-lines 'folded)
  13600. (condition-case nil
  13601. (outline-forward-same-level 1)
  13602. (error (goto-char end)))))
  13603. (when swallowp
  13604. (message
  13605. "Yanked text not folded because that would swallow text"))
  13606. (goto-char end)
  13607. (skip-chars-forward " \t\n\r")
  13608. (beginning-of-line 1)
  13609. (push-mark beg 'nomsg)))
  13610. ((and subtreep org-yank-adjusted-subtrees)
  13611. (let ((beg (point-at-bol)))
  13612. (org-paste-subtree nil nil 'for-yank)
  13613. (push-mark beg 'nomsg)))
  13614. (t
  13615. (call-interactively 'yank))))))
  13616. (defun org-yank-folding-would-swallow-text (beg end)
  13617. "Would hide-subtree at BEG swallow any text after END?"
  13618. (let (level)
  13619. (save-excursion
  13620. (goto-char beg)
  13621. (when (or (looking-at outline-regexp)
  13622. (re-search-forward (concat "^" outline-regexp) end t))
  13623. (setq level (org-outline-level)))
  13624. (goto-char end)
  13625. (skip-chars-forward " \t\r\n\v\f")
  13626. (if (or (eobp)
  13627. (and (bolp) (looking-at org-outline-regexp)
  13628. (<= (org-outline-level) level)))
  13629. nil ; Nothing would be swallowed
  13630. t)))) ; something would swallow
  13631. (define-key org-mode-map "\C-y" 'org-yank)
  13632. (defun org-invisible-p ()
  13633. "Check if point is at a character currently not visible."
  13634. ;; Early versions of noutline don't have `outline-invisible-p'.
  13635. (if (fboundp 'outline-invisible-p)
  13636. (outline-invisible-p)
  13637. (get-char-property (point) 'invisible)))
  13638. (defun org-invisible-p2 ()
  13639. "Check if point is at a character currently not visible."
  13640. (save-excursion
  13641. (if (and (eolp) (not (bobp))) (backward-char 1))
  13642. ;; Early versions of noutline don't have `outline-invisible-p'.
  13643. (if (fboundp 'outline-invisible-p)
  13644. (outline-invisible-p)
  13645. (get-char-property (point) 'invisible))))
  13646. (defun org-back-to-heading (&optional invisible-ok)
  13647. "Call `outline-back-to-heading', but provide a better error message."
  13648. (condition-case nil
  13649. (outline-back-to-heading invisible-ok)
  13650. (error (error "Before first headline at position %d in buffer %s"
  13651. (point) (current-buffer)))))
  13652. (defun org-before-first-heading-p ()
  13653. "Before first heading?"
  13654. (save-excursion
  13655. (null (re-search-backward "^\\*+ " nil t))))
  13656. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13657. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13658. (defun org-at-heading-or-item-p ()
  13659. (or (org-on-heading-p) (org-at-item-p)))
  13660. (defun org-on-target-p ()
  13661. (or (org-in-regexp org-radio-target-regexp)
  13662. (org-in-regexp org-target-regexp)))
  13663. (defun org-up-heading-all (arg)
  13664. "Move to the heading line of which the present line is a subheading.
  13665. This function considers both visible and invisible heading lines.
  13666. With argument, move up ARG levels."
  13667. (if (fboundp 'outline-up-heading-all)
  13668. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13669. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13670. (defun org-up-heading-safe ()
  13671. "Move to the heading line of which the present line is a subheading.
  13672. This version will not throw an error. It will return the level of the
  13673. headline found, or nil if no higher level is found."
  13674. (let (start-level re)
  13675. (org-back-to-heading t)
  13676. (setq start-level (funcall outline-level))
  13677. (if (equal start-level 1)
  13678. nil
  13679. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13680. (if (re-search-backward re nil t)
  13681. (funcall outline-level)))))
  13682. (defun org-first-sibling-p ()
  13683. "Is this heading the first child of its parents?"
  13684. (interactive)
  13685. (let ((re (concat "^" outline-regexp))
  13686. level l)
  13687. (unless (org-at-heading-p t)
  13688. (error "Not at a heading"))
  13689. (setq level (funcall outline-level))
  13690. (save-excursion
  13691. (if (not (re-search-backward re nil t))
  13692. t
  13693. (setq l (funcall outline-level))
  13694. (< l level)))))
  13695. (defun org-goto-sibling (&optional previous)
  13696. "Goto the next sibling, even if it is invisible.
  13697. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13698. when a sibling was found. When none is found, return nil and don't
  13699. move point."
  13700. (let ((fun (if previous 're-search-backward 're-search-forward))
  13701. (pos (point))
  13702. (re (concat "^" outline-regexp))
  13703. level l)
  13704. (when (condition-case nil (org-back-to-heading t) (error nil))
  13705. (setq level (funcall outline-level))
  13706. (catch 'exit
  13707. (or previous (forward-char 1))
  13708. (while (funcall fun re nil t)
  13709. (setq l (funcall outline-level))
  13710. (when (< l level) (goto-char pos) (throw 'exit nil))
  13711. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13712. (goto-char pos)
  13713. nil))))
  13714. (defun org-show-siblings ()
  13715. "Show all siblings of the current headline."
  13716. (save-excursion
  13717. (while (org-goto-sibling) (org-flag-heading nil)))
  13718. (save-excursion
  13719. (while (org-goto-sibling 'previous)
  13720. (org-flag-heading nil))))
  13721. (defun org-show-hidden-entry ()
  13722. "Show an entry where even the heading is hidden."
  13723. (save-excursion
  13724. (org-show-entry)))
  13725. (defun org-flag-heading (flag &optional entry)
  13726. "Flag the current heading. FLAG non-nil means make invisible.
  13727. When ENTRY is non-nil, show the entire entry."
  13728. (save-excursion
  13729. (org-back-to-heading t)
  13730. ;; Check if we should show the entire entry
  13731. (if entry
  13732. (progn
  13733. (org-show-entry)
  13734. (save-excursion
  13735. (and (outline-next-heading)
  13736. (org-flag-heading nil))))
  13737. (outline-flag-region (max (point-min) (1- (point)))
  13738. (save-excursion (outline-end-of-heading) (point))
  13739. flag))))
  13740. (defun org-forward-same-level (arg)
  13741. "Move forward to the ARG'th subheading at same level as this one.
  13742. Stop at the first and last subheadings of a superior heading.
  13743. This is like outline-forward-same-level, but invisible headings are ok."
  13744. (interactive "p")
  13745. (org-back-to-heading t)
  13746. (while (> arg 0)
  13747. (let ((point-to-move-to (save-excursion
  13748. (org-get-next-sibling))))
  13749. (if point-to-move-to
  13750. (progn
  13751. (goto-char point-to-move-to)
  13752. (setq arg (1- arg)))
  13753. (progn
  13754. (setq arg 0)
  13755. (error "No following same-level heading"))))))
  13756. (defun org-get-next-sibling ()
  13757. "Move to next heading of the same level, and return point.
  13758. If there is no such heading, return nil.
  13759. This is like outline-next-sibling, but invisible headings are ok."
  13760. (let ((level (funcall outline-level)))
  13761. (outline-next-heading)
  13762. (while (and (not (eobp)) (> (funcall outline-level) level))
  13763. (outline-next-heading))
  13764. (if (or (eobp) (< (funcall outline-level) level))
  13765. nil
  13766. (point))))
  13767. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13768. ;; This is an exact copy of the original function, but it uses
  13769. ;; `org-back-to-heading', to make it work also in invisible
  13770. ;; trees. And is uses an invisible-OK argument.
  13771. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13772. (org-back-to-heading invisible-OK)
  13773. (let ((first t)
  13774. (level (funcall outline-level)))
  13775. (while (and (not (eobp))
  13776. (or first (> (funcall outline-level) level)))
  13777. (setq first nil)
  13778. (outline-next-heading))
  13779. (unless to-heading
  13780. (if (memq (preceding-char) '(?\n ?\^M))
  13781. (progn
  13782. ;; Go to end of line before heading
  13783. (forward-char -1)
  13784. (if (memq (preceding-char) '(?\n ?\^M))
  13785. ;; leave blank line before heading
  13786. (forward-char -1))))))
  13787. (point))
  13788. (defun org-show-subtree ()
  13789. "Show everything after this heading at deeper levels."
  13790. (outline-flag-region
  13791. (point)
  13792. (save-excursion
  13793. (outline-end-of-subtree) (outline-next-heading) (point))
  13794. nil))
  13795. (defun org-show-entry ()
  13796. "Show the body directly following this heading.
  13797. Show the heading too, if it is currently invisible."
  13798. (interactive)
  13799. (save-excursion
  13800. (condition-case nil
  13801. (progn
  13802. (org-back-to-heading t)
  13803. (outline-flag-region
  13804. (max (point-min) (1- (point)))
  13805. (save-excursion
  13806. (re-search-forward
  13807. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13808. (or (match-beginning 1) (point-max)))
  13809. nil))
  13810. (error nil))))
  13811. (defun org-make-options-regexp (kwds)
  13812. "Make a regular expression for keyword lines."
  13813. (concat
  13814. "^"
  13815. "#?[ \t]*\\+\\("
  13816. (mapconcat 'regexp-quote kwds "\\|")
  13817. "\\):[ \t]*"
  13818. "\\(.+\\)"))
  13819. ;; Make isearch reveal the necessary context
  13820. (defun org-isearch-end ()
  13821. "Reveal context after isearch exits."
  13822. (when isearch-success ; only if search was successful
  13823. (if (featurep 'xemacs)
  13824. ;; Under XEmacs, the hook is run in the correct place,
  13825. ;; we directly show the context.
  13826. (org-show-context 'isearch)
  13827. ;; In Emacs the hook runs *before* restoring the overlays.
  13828. ;; So we have to use a one-time post-command-hook to do this.
  13829. ;; (Emacs 22 has a special variable, see function `org-mode')
  13830. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13831. isearch-mode-end-hook-quit)
  13832. ;; Only when the isearch was not quitted.
  13833. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13834. 'append 'local)))))
  13835. (defun org-isearch-post-command ()
  13836. "Remove self from hook, and show context."
  13837. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13838. (org-show-context 'isearch))
  13839. ;;;; Integration with and fixes for other packages
  13840. ;;; Imenu support
  13841. (defvar org-imenu-markers nil
  13842. "All markers currently used by Imenu.")
  13843. (make-variable-buffer-local 'org-imenu-markers)
  13844. (defun org-imenu-new-marker (&optional pos)
  13845. "Return a new marker for use by Imenu, and remember the marker."
  13846. (let ((m (make-marker)))
  13847. (move-marker m (or pos (point)))
  13848. (push m org-imenu-markers)
  13849. m))
  13850. (defun org-imenu-get-tree ()
  13851. "Produce the index for Imenu."
  13852. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13853. (setq org-imenu-markers nil)
  13854. (let* ((n org-imenu-depth)
  13855. (re (concat "^" outline-regexp))
  13856. (subs (make-vector (1+ n) nil))
  13857. (last-level 0)
  13858. m level head)
  13859. (save-excursion
  13860. (save-restriction
  13861. (widen)
  13862. (goto-char (point-max))
  13863. (while (re-search-backward re nil t)
  13864. (setq level (org-reduced-level (funcall outline-level)))
  13865. (when (<= level n)
  13866. (looking-at org-complex-heading-regexp)
  13867. (setq head (org-link-display-format
  13868. (org-match-string-no-properties 4))
  13869. m (org-imenu-new-marker))
  13870. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13871. (if (>= level last-level)
  13872. (push (cons head m) (aref subs level))
  13873. (push (cons head (aref subs (1+ level))) (aref subs level))
  13874. (loop for i from (1+ level) to n do (aset subs i nil)))
  13875. (setq last-level level)))))
  13876. (aref subs 1)))
  13877. (eval-after-load "imenu"
  13878. '(progn
  13879. (add-hook 'imenu-after-jump-hook
  13880. (lambda ()
  13881. (if (eq major-mode 'org-mode)
  13882. (org-show-context 'org-goto))))))
  13883. (defun org-link-display-format (link)
  13884. "Replace a link with either the description, or the link target
  13885. if no description is present"
  13886. (save-match-data
  13887. (if (string-match org-bracket-link-analytic-regexp link)
  13888. (replace-match (or (match-string 5 link)
  13889. (concat (match-string 1 link)
  13890. (match-string 3 link)))
  13891. nil nil link)
  13892. link)))
  13893. ;; Speedbar support
  13894. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13895. "Overlay marking the agenda restriction line in speedbar.")
  13896. (org-overlay-put org-speedbar-restriction-lock-overlay
  13897. 'face 'org-agenda-restriction-lock)
  13898. (org-overlay-put org-speedbar-restriction-lock-overlay
  13899. 'help-echo "Agendas are currently limited to this item.")
  13900. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13901. (defun org-speedbar-set-agenda-restriction ()
  13902. "Restrict future agenda commands to the location at point in speedbar.
  13903. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13904. (interactive)
  13905. (require 'org-agenda)
  13906. (let (p m tp np dir txt)
  13907. (cond
  13908. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13909. 'org-imenu t))
  13910. (setq m (get-text-property p 'org-imenu-marker))
  13911. (save-excursion
  13912. (save-restriction
  13913. (set-buffer (marker-buffer m))
  13914. (goto-char m)
  13915. (org-agenda-set-restriction-lock 'subtree))))
  13916. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13917. 'speedbar-function 'speedbar-find-file))
  13918. (setq tp (previous-single-property-change
  13919. (1+ p) 'speedbar-function)
  13920. np (next-single-property-change
  13921. tp 'speedbar-function)
  13922. dir (speedbar-line-directory)
  13923. txt (buffer-substring-no-properties (or tp (point-min))
  13924. (or np (point-max))))
  13925. (save-excursion
  13926. (save-restriction
  13927. (set-buffer (find-file-noselect
  13928. (let ((default-directory dir))
  13929. (expand-file-name txt))))
  13930. (unless (org-mode-p)
  13931. (error "Cannot restrict to non-Org-mode file"))
  13932. (org-agenda-set-restriction-lock 'file))))
  13933. (t (error "Don't know how to restrict Org-mode's agenda")))
  13934. (org-move-overlay org-speedbar-restriction-lock-overlay
  13935. (point-at-bol) (point-at-eol))
  13936. (setq current-prefix-arg nil)
  13937. (org-agenda-maybe-redo)))
  13938. (eval-after-load "speedbar"
  13939. '(progn
  13940. (speedbar-add-supported-extension ".org")
  13941. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13942. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13943. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13944. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13945. (add-hook 'speedbar-visiting-tag-hook
  13946. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13947. ;;; Fixes and Hacks for problems with other packages
  13948. ;; Make flyspell not check words in links, to not mess up our keymap
  13949. (defun org-mode-flyspell-verify ()
  13950. "Don't let flyspell put overlays at active buttons."
  13951. (not (get-text-property (point) 'keymap)))
  13952. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13953. (eval-after-load "bookmark"
  13954. '(if (boundp 'bookmark-after-jump-hook)
  13955. ;; We can use the hook
  13956. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13957. ;; Hook not available, use advice
  13958. (defadvice bookmark-jump (after org-make-visible activate)
  13959. "Make the position visible."
  13960. (org-bookmark-jump-unhide))))
  13961. ;; Make sure saveplace show the location if it was hidden
  13962. (eval-after-load "saveplace"
  13963. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13964. "Make the position visible."
  13965. (org-bookmark-jump-unhide)))
  13966. (defun org-bookmark-jump-unhide ()
  13967. "Unhide the current position, to show the bookmark location."
  13968. (and (org-mode-p)
  13969. (or (org-invisible-p)
  13970. (save-excursion (goto-char (max (point-min) (1- (point))))
  13971. (org-invisible-p)))
  13972. (org-show-context 'bookmark-jump)))
  13973. ;; Make session.el ignore our circular variable
  13974. (eval-after-load "session"
  13975. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13976. ;;;; Experimental code
  13977. (defun org-closed-in-range ()
  13978. "Sparse tree of items closed in a certain time range.
  13979. Still experimental, may disappear in the future."
  13980. (interactive)
  13981. ;; Get the time interval from the user.
  13982. (let* ((time1 (time-to-seconds
  13983. (org-read-date nil 'to-time nil "Starting date: ")))
  13984. (time2 (time-to-seconds
  13985. (org-read-date nil 'to-time nil "End date:")))
  13986. ;; callback function
  13987. (callback (lambda ()
  13988. (let ((time
  13989. (time-to-seconds
  13990. (apply 'encode-time
  13991. (org-parse-time-string
  13992. (match-string 1))))))
  13993. ;; check if time in interval
  13994. (and (>= time time1) (<= time time2))))))
  13995. ;; make tree, check each match with the callback
  13996. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13997. ;;;; Finish up
  13998. (provide 'org)
  13999. (run-hooks 'org-load-hook)
  14000. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14001. ;;; org.el ends here