org.el 618 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574
  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.27trans
  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-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.27trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let ((version (format "Org-mode version %s" org-version)))
  96. (message version)
  97. (if here
  98. (insert version))))
  99. ;;; Compatibility constants
  100. ;;; The custom variables
  101. (defgroup org nil
  102. "Outline-based notes management and organizer."
  103. :tag "Org"
  104. :group 'outlines
  105. :group 'hypermedia
  106. :group 'calendar)
  107. (defcustom org-load-hook nil
  108. "Hook that is run after org.el has been loaded."
  109. :group 'org
  110. :type 'hook)
  111. (defvar org-modules) ; defined below
  112. (defvar org-modules-loaded nil
  113. "Have the modules been loaded already?")
  114. (defun org-load-modules-maybe (&optional force)
  115. "Load all extensions listed in `org-modules'."
  116. (when (or force (not org-modules-loaded))
  117. (mapc (lambda (ext)
  118. (condition-case nil (require ext)
  119. (error (message "Problems while trying to load feature `%s'" ext))))
  120. org-modules)
  121. (setq org-modules-loaded t)))
  122. (defun org-set-modules (var value)
  123. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  124. (set var value)
  125. (when (featurep 'org)
  126. (org-load-modules-maybe 'force)))
  127. (when (org-bound-and-true-p org-modules)
  128. (let ((a (member 'org-infojs org-modules)))
  129. (and a (setcar a 'org-jsinfo))))
  130. (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)
  131. "Modules that should always be loaded together with org.el.
  132. If a description starts with <C>, the file is not part of Emacs
  133. and loading it will require that you have downloaded and properly installed
  134. the org-mode distribution.
  135. You can also use this system to load external packages (i.e. neither Org
  136. core modules, not modules from the CONTRIB directory). Just add symbols
  137. to the end of the list. If the package is called org-xyz.el, then you need
  138. to add the symbol `xyz', and the package must have a call to
  139. (provide 'org-xyz)"
  140. :group 'org
  141. :set 'org-set-modules
  142. :type
  143. '(set :greedy t
  144. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  145. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  146. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  147. (const :tag " id: Global IDs for identifying entries" org-id)
  148. (const :tag " info: Links to Info nodes" org-info)
  149. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  150. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  151. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  152. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  153. (const :tag " mew Links to Mew folders/messages" org-mew)
  154. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  155. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  156. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  157. (const :tag " vm: Links to VM folders/messages" org-vm)
  158. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  159. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  160. (const :tag " mouse: Additional mouse support" org-mouse)
  161. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  162. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  163. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  164. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  165. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  166. (const :tag "C collector: Collect properties into tables" org-collector)
  167. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  168. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  169. (const :tag "C eval: Include command output as text" org-eval)
  170. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  171. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  172. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  173. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  174. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  175. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  176. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  177. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  178. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  179. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  180. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  181. (const :tag "C R: Computation using the R language" org-R)
  182. (const :tag "C registry: A registry for Org links" org-registry)
  183. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  184. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  185. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  186. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  187. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  188. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  189. (defcustom org-support-shift-select nil
  190. "Non-nil means, make shift-cursor commands select text when possible.
  191. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  192. selecting a region, or enlarge thusly regions started in this way.
  193. In Org-mode, in special contexts, these same keys are used for other
  194. purposes, important enough to compete with shift selection. Org tries
  195. to balance these needs by supporting `shift-select-mode' outside these
  196. special contexts, under control of this variable.
  197. The default of this variable is nil, to avoid confusing behavior. Shifted
  198. cursor keys will then execute Org commands in the following contexts:
  199. - on a headline, changing TODO state (left/right) and priority (up/down)
  200. - on a time stamp, changing the time
  201. - in a plain list item, changing the bullet type
  202. - in a property definition line, switching between allowed values
  203. - in the BEGIN line of a clock table (changing the time block).
  204. Outside these contexts, the commands will throw an error.
  205. When this variable is t and the cursor is not in a special context,
  206. Org-mode will support shift-selection for making and enlarging regions.
  207. To make this more effective, the bullet cycling will no longer happen
  208. anywhere in an item line, but only if the cursor is exactly on the bullet.
  209. If you set this variable to the symbol `always', then the keys
  210. will not be special in headlines, property lines, and item lines, to make
  211. shift selection work there as well. If this is what you want, you can
  212. use the following alternative commands: `C-c C-t' and `C-c ,' to
  213. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  214. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  215. edited by hand or in column view.
  216. However, when the cursor is on a timestamp, shift-cursor commands
  217. will still edit the time stamp - this is just too good to give up.
  218. XEmacs user should have this variable set to nil, because shift-select-mode
  219. is Emacs 23 only."
  220. :group 'org
  221. :type '(choice
  222. (const :tag "Never" nil)
  223. (const :tag "When outside special context" t)
  224. (const :tag "Everywhere except timestamps" always)))
  225. (defgroup org-startup nil
  226. "Options concerning startup of Org-mode."
  227. :tag "Org Startup"
  228. :group 'org)
  229. (defcustom org-startup-folded t
  230. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  231. This can also be configured on a per-file basis by adding one of
  232. the following lines anywhere in the buffer:
  233. #+STARTUP: fold
  234. #+STARTUP: nofold
  235. #+STARTUP: content"
  236. :group 'org-startup
  237. :type '(choice
  238. (const :tag "nofold: show all" nil)
  239. (const :tag "fold: overview" t)
  240. (const :tag "content: all headlines" content)))
  241. (defcustom org-startup-truncated t
  242. "Non-nil means, entering Org-mode will set `truncate-lines'.
  243. This is useful since some lines containing links can be very long and
  244. uninteresting. Also tables look terrible when wrapped."
  245. :group 'org-startup
  246. :type 'boolean)
  247. (defcustom org-startup-align-all-tables nil
  248. "Non-nil means, align all tables when visiting a file.
  249. This is useful when the column width in tables is forced with <N> cookies
  250. in table fields. Such tables will look correct only after the first re-align.
  251. This can also be configured on a per-file basis by adding one of
  252. the following lines anywhere in the buffer:
  253. #+STARTUP: align
  254. #+STARTUP: noalign"
  255. :group 'org-startup
  256. :type 'boolean)
  257. (defcustom org-insert-mode-line-in-empty-file nil
  258. "Non-nil means insert the first line setting Org-mode in empty files.
  259. When the function `org-mode' is called interactively in an empty file, this
  260. normally means that the file name does not automatically trigger Org-mode.
  261. To ensure that the file will always be in Org-mode in the future, a
  262. line enforcing Org-mode will be inserted into the buffer, if this option
  263. has been set."
  264. :group 'org-startup
  265. :type 'boolean)
  266. (defcustom org-replace-disputed-keys nil
  267. "Non-nil means use alternative key bindings for some keys.
  268. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  269. These keys are also used by other packages like shift-selection-mode'
  270. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  271. If you want to use Org-mode together with one of these other modes,
  272. or more generally if you would like to move some Org-mode commands to
  273. other keys, set this variable and configure the keys with the variable
  274. `org-disputed-keys'.
  275. This option is only relevant at load-time of Org-mode, and must be set
  276. *before* org.el is loaded. Changing it requires a restart of Emacs to
  277. become effective."
  278. :group 'org-startup
  279. :type 'boolean)
  280. (defcustom org-use-extra-keys nil
  281. "Non-nil means use extra key sequence definitions for certain
  282. commands. This happens automatically if you run XEmacs or if
  283. window-system is nil. This variable lets you do the same
  284. manually. You must set it before loading org.
  285. Example: on Carbon Emacs 22 running graphically, with an external
  286. keyboard on a Powerbook, the default way of setting M-left might
  287. not work for either Alt or ESC. Setting this variable will make
  288. it work for ESC."
  289. :group 'org-startup
  290. :type 'boolean)
  291. (if (fboundp 'defvaralias)
  292. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  293. (defcustom org-disputed-keys
  294. '(([(shift up)] . [(meta p)])
  295. ([(shift down)] . [(meta n)])
  296. ([(shift left)] . [(meta -)])
  297. ([(shift right)] . [(meta +)])
  298. ([(control shift right)] . [(meta shift +)])
  299. ([(control shift left)] . [(meta shift -)]))
  300. "Keys for which Org-mode and other modes compete.
  301. This is an alist, cars are the default keys, second element specifies
  302. the alternative to use when `org-replace-disputed-keys' is t.
  303. Keys can be specified in any syntax supported by `define-key'.
  304. The value of this option takes effect only at Org-mode's startup,
  305. therefore you'll have to restart Emacs to apply it after changing."
  306. :group 'org-startup
  307. :type 'alist)
  308. (defun org-key (key)
  309. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  310. Or return the original if not disputed."
  311. (if org-replace-disputed-keys
  312. (let* ((nkey (key-description key))
  313. (x (org-find-if (lambda (x)
  314. (equal (key-description (car x)) nkey))
  315. org-disputed-keys)))
  316. (if x (cdr x) key))
  317. key))
  318. (defun org-find-if (predicate seq)
  319. (catch 'exit
  320. (while seq
  321. (if (funcall predicate (car seq))
  322. (throw 'exit (car seq))
  323. (pop seq)))))
  324. (defun org-defkey (keymap key def)
  325. "Define a key, possibly translated, as returned by `org-key'."
  326. (define-key keymap (org-key key) def))
  327. (defcustom org-ellipsis nil
  328. "The ellipsis to use in the Org-mode outline.
  329. When nil, just use the standard three dots. When a string, use that instead,
  330. When a face, use the standard 3 dots, but with the specified face.
  331. The change affects only Org-mode (which will then use its own display table).
  332. Changing this requires executing `M-x org-mode' in a buffer to become
  333. effective."
  334. :group 'org-startup
  335. :type '(choice (const :tag "Default" nil)
  336. (face :tag "Face" :value org-warning)
  337. (string :tag "String" :value "...#")))
  338. (defvar org-display-table nil
  339. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  340. (defgroup org-keywords nil
  341. "Keywords in Org-mode."
  342. :tag "Org Keywords"
  343. :group 'org)
  344. (defcustom org-deadline-string "DEADLINE:"
  345. "String to mark deadline entries.
  346. A deadline is this string, followed by a time stamp. Should be a word,
  347. terminated by a colon. You can insert a schedule keyword and
  348. a timestamp with \\[org-deadline].
  349. Changes become only effective after restarting Emacs."
  350. :group 'org-keywords
  351. :type 'string)
  352. (defcustom org-scheduled-string "SCHEDULED:"
  353. "String to mark scheduled TODO entries.
  354. A schedule is this string, followed by a time stamp. Should be a word,
  355. terminated by a colon. You can insert a schedule keyword and
  356. a timestamp with \\[org-schedule].
  357. Changes become only effective after restarting Emacs."
  358. :group 'org-keywords
  359. :type 'string)
  360. (defcustom org-closed-string "CLOSED:"
  361. "String used as the prefix for timestamps logging closing a TODO entry."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-clock-string "CLOCK:"
  365. "String used as prefix for timestamps clocking work hours on an item."
  366. :group 'org-keywords
  367. :type 'string)
  368. (defcustom org-comment-string "COMMENT"
  369. "Entries starting with this keyword will never be exported.
  370. An entry can be toggled between COMMENT and normal with
  371. \\[org-toggle-comment].
  372. Changes become only effective after restarting Emacs."
  373. :group 'org-keywords
  374. :type 'string)
  375. (defcustom org-quote-string "QUOTE"
  376. "Entries starting with this keyword will be exported in fixed-width font.
  377. Quoting applies only to the text in the entry following the headline, and does
  378. not extend beyond the next headline, even if that is lower level.
  379. An entry can be toggled between QUOTE and normal with
  380. \\[org-toggle-fixed-width-section]."
  381. :group 'org-keywords
  382. :type 'string)
  383. (defconst org-repeat-re
  384. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  385. "Regular expression for specifying repeated events.
  386. After a match, group 1 contains the repeat expression.")
  387. (defgroup org-structure nil
  388. "Options concerning the general structure of Org-mode files."
  389. :tag "Org Structure"
  390. :group 'org)
  391. (defgroup org-reveal-location nil
  392. "Options about how to make context of a location visible."
  393. :tag "Org Reveal Location"
  394. :group 'org-structure)
  395. (defconst org-context-choice
  396. '(choice
  397. (const :tag "Always" t)
  398. (const :tag "Never" nil)
  399. (repeat :greedy t :tag "Individual contexts"
  400. (cons
  401. (choice :tag "Context"
  402. (const agenda)
  403. (const org-goto)
  404. (const occur-tree)
  405. (const tags-tree)
  406. (const link-search)
  407. (const mark-goto)
  408. (const bookmark-jump)
  409. (const isearch)
  410. (const default))
  411. (boolean))))
  412. "Contexts for the reveal options.")
  413. (defcustom org-show-hierarchy-above '((default . t))
  414. "Non-nil means, show full hierarchy when revealing a location.
  415. Org-mode often shows locations in an org-mode file which might have
  416. been invisible before. When this is set, the hierarchy of headings
  417. above the exposed location is shown.
  418. Turning this off for example for sparse trees makes them very compact.
  419. Instead of t, this can also be an alist specifying this option for different
  420. contexts. Valid contexts are
  421. agenda when exposing an entry from the agenda
  422. org-goto when using the command `org-goto' on key C-c C-j
  423. occur-tree when using the command `org-occur' on key C-c /
  424. tags-tree when constructing a sparse tree based on tags matches
  425. link-search when exposing search matches associated with a link
  426. mark-goto when exposing the jump goal of a mark
  427. bookmark-jump when exposing a bookmark location
  428. isearch when exiting from an incremental search
  429. default default for all contexts not set explicitly"
  430. :group 'org-reveal-location
  431. :type org-context-choice)
  432. (defcustom org-show-following-heading '((default . nil))
  433. "Non-nil means, show following heading when revealing a location.
  434. Org-mode often shows locations in an org-mode file which might have
  435. been invisible before. When this is set, the heading following the
  436. match is shown.
  437. Turning this off for example for sparse trees makes them very compact,
  438. but makes it harder to edit the location of the match. In such a case,
  439. use the command \\[org-reveal] to show more context.
  440. Instead of t, this can also be an alist specifying this option for different
  441. contexts. See `org-show-hierarchy-above' for valid contexts."
  442. :group 'org-reveal-location
  443. :type org-context-choice)
  444. (defcustom org-show-siblings '((default . nil) (isearch t))
  445. "Non-nil means, show all sibling heading when revealing a location.
  446. Org-mode often shows locations in an org-mode file which might have
  447. been invisible before. When this is set, the sibling of the current entry
  448. heading are all made visible. If `org-show-hierarchy-above' is t,
  449. the same happens on each level of the hierarchy above the current entry.
  450. By default this is on for the isearch context, off for all other contexts.
  451. Turning this off for example for sparse trees makes them very compact,
  452. but makes it harder to edit the location of the match. In such a case,
  453. use the command \\[org-reveal] to show more context.
  454. Instead of t, this can also be an alist specifying this option for different
  455. contexts. See `org-show-hierarchy-above' for valid contexts."
  456. :group 'org-reveal-location
  457. :type org-context-choice)
  458. (defcustom org-show-entry-below '((default . nil))
  459. "Non-nil means, show the entry below a headline when revealing a location.
  460. Org-mode often shows locations in an org-mode file which might have
  461. been invisible before. When this is set, the text below the headline that is
  462. exposed is also shown.
  463. By default this is off for all contexts.
  464. Instead of t, this can also be an alist specifying this option for different
  465. contexts. See `org-show-hierarchy-above' for valid contexts."
  466. :group 'org-reveal-location
  467. :type org-context-choice)
  468. (defcustom org-indirect-buffer-display 'other-window
  469. "How should indirect tree buffers be displayed?
  470. This applies to indirect buffers created with the commands
  471. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  472. Valid values are:
  473. current-window Display in the current window
  474. other-window Just display in another window.
  475. dedicated-frame Create one new frame, and re-use it each time.
  476. new-frame Make a new frame each time. Note that in this case
  477. previously-made indirect buffers are kept, and you need to
  478. kill these buffers yourself."
  479. :group 'org-structure
  480. :group 'org-agenda-windows
  481. :type '(choice
  482. (const :tag "In current window" current-window)
  483. (const :tag "In current frame, other window" other-window)
  484. (const :tag "Each time a new frame" new-frame)
  485. (const :tag "One dedicated frame" dedicated-frame)))
  486. (defgroup org-cycle nil
  487. "Options concerning visibility cycling in Org-mode."
  488. :tag "Org Cycle"
  489. :group 'org-structure)
  490. (defcustom org-cycle-max-level nil
  491. "Maximum level which should still be subject to visibility cycling.
  492. Levels higher than this will, for cycling, be treated as text, not a headline.
  493. When `org-odd-levels-only' is set, a value of N in this variable actually
  494. means 2N-1 stars as the limiting headline.
  495. When nil, cycle all levels.
  496. Note that the limiting level of cycling is also influenced by
  497. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  498. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  499. than its value."
  500. :group 'org-cycle
  501. :type '(choice
  502. (const :tag "No limit" nil)
  503. (integer :tag "Maximum level")))
  504. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  505. "Names of drawers. Drawers are not opened by cycling on the headline above.
  506. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  507. this:
  508. :DRAWERNAME:
  509. .....
  510. :END:
  511. The drawer \"PROPERTIES\" is special for capturing properties through
  512. the property API.
  513. Drawers can be defined on the per-file basis with a line like:
  514. #+DRAWERS: HIDDEN STATE PROPERTIES"
  515. :group 'org-structure
  516. :group 'org-cycle
  517. :type '(repeat (string :tag "Drawer Name")))
  518. (defcustom org-hide-block-startup nil
  519. "Non-nil means, , entering Org-mode will fold all blocks.
  520. This can also be set in on a per-file basis with
  521. #+STARTUP: hideblocks
  522. #+STARTUP: showblocks"
  523. :group 'org-startup
  524. :group 'org-cycle
  525. :type 'boolean)
  526. (defcustom org-cycle-global-at-bob nil
  527. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  528. This makes it possible to do global cycling without having to use S-TAB or
  529. C-u TAB. For this special case to work, the first line of the buffer
  530. must not be a headline - it may be empty or some other text. When used in
  531. this way, `org-cycle-hook' is disables temporarily, to make sure the
  532. cursor stays at the beginning of the buffer.
  533. When this option is nil, don't do anything special at the beginning
  534. of the buffer."
  535. :group 'org-cycle
  536. :type 'boolean)
  537. (defcustom org-cycle-emulate-tab t
  538. "Where should `org-cycle' emulate TAB.
  539. nil Never
  540. white Only in completely white lines
  541. whitestart Only at the beginning of lines, before the first non-white char
  542. t Everywhere except in headlines
  543. exc-hl-bol Everywhere except at the start of a headline
  544. If TAB is used in a place where it does not emulate TAB, the current subtree
  545. visibility is cycled."
  546. :group 'org-cycle
  547. :type '(choice (const :tag "Never" nil)
  548. (const :tag "Only in completely white lines" white)
  549. (const :tag "Before first char in a line" whitestart)
  550. (const :tag "Everywhere except in headlines" t)
  551. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  552. ))
  553. (defcustom org-cycle-separator-lines 2
  554. "Number of empty lines needed to keep an empty line between collapsed trees.
  555. If you leave an empty line between the end of a subtree and the following
  556. headline, this empty line is hidden when the subtree is folded.
  557. Org-mode will leave (exactly) one empty line visible if the number of
  558. empty lines is equal or larger to the number given in this variable.
  559. So the default 2 means, at least 2 empty lines after the end of a subtree
  560. are needed to produce free space between a collapsed subtree and the
  561. following headline.
  562. Special case: when 0, never leave empty lines in collapsed view."
  563. :group 'org-cycle
  564. :type 'integer)
  565. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  566. (defcustom org-pre-cycle-hook nil
  567. "Hook that is run before visibility cycling is happening.
  568. The function(s) in this hook must accept a single argument which indicates
  569. the new state that will be set right after running this hook. The
  570. argument is a symbol. Before a global state change, it can have the values
  571. `overview', `content', or `all'. Before a local state change, it can have
  572. the values `folded', `children', or `subtree'."
  573. :group 'org-cycle
  574. :type 'hook)
  575. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  576. org-cycle-hide-drawers
  577. org-cycle-show-empty-lines
  578. org-optimize-window-after-visibility-change)
  579. "Hook that is run after `org-cycle' has changed the buffer visibility.
  580. The function(s) in this hook must accept a single argument which indicates
  581. the new state that was set by the most recent `org-cycle' command. The
  582. argument is a symbol. After a global state change, it can have the values
  583. `overview', `content', or `all'. After a local state change, it can have
  584. the values `folded', `children', or `subtree'."
  585. :group 'org-cycle
  586. :type 'hook)
  587. (defgroup org-edit-structure nil
  588. "Options concerning structure editing in Org-mode."
  589. :tag "Org Edit Structure"
  590. :group 'org-structure)
  591. (defcustom org-odd-levels-only nil
  592. "Non-nil means, skip even levels and only use odd levels for the outline.
  593. This has the effect that two stars are being added/taken away in
  594. promotion/demotion commands. It also influences how levels are
  595. handled by the exporters.
  596. Changing it requires restart of `font-lock-mode' to become effective
  597. for fontification also in regions already fontified.
  598. You may also set this on a per-file basis by adding one of the following
  599. lines to the buffer:
  600. #+STARTUP: odd
  601. #+STARTUP: oddeven"
  602. :group 'org-edit-structure
  603. :group 'org-font-lock
  604. :type 'boolean)
  605. (defcustom org-adapt-indentation t
  606. "Non-nil means, adapt indentation when promoting and demoting.
  607. When this is set and the *entire* text in an entry is indented, the
  608. indentation is increased by one space in a demotion command, and
  609. decreased by one in a promotion command. If any line in the entry
  610. body starts at column 0, indentation is not changed at all.
  611. This variable also influences how property drawers and planning
  612. information is inserted. When t, these lines drawers will be inserted
  613. indented. When nil, they will not be indented."
  614. :group 'org-edit-structure
  615. :type 'boolean)
  616. (defcustom org-special-ctrl-a/e nil
  617. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  618. When t, `C-a' will bring back the cursor to the beginning of the
  619. headline text, i.e. after the stars and after a possible TODO keyword.
  620. In an item, this will be the position after the bullet.
  621. When the cursor is already at that position, another `C-a' will bring
  622. it to the beginning of the line.
  623. `C-e' will jump to the end of the headline, ignoring the presence of tags
  624. in the headline. A second `C-e' will then jump to the true end of the
  625. line, after any tags.
  626. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  627. and only a directly following, identical keypress will bring the cursor
  628. to the special positions.
  629. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  630. set separately."
  631. :group 'org-edit-structure
  632. :type '(choice
  633. (const :tag "off" nil)
  634. (const :tag "after stars/bullet and before tags first" t)
  635. (const :tag "true line boundary first" reversed)
  636. (cons :tag "Set C-a and C-e separately"
  637. (choice :tag "Special C-a"
  638. (const :tag "off" nil)
  639. (const :tag "after stars/bullet first" t)
  640. (const :tag "before stars/bullet first" reversed))
  641. (choice :tag "Special C-e"
  642. (const :tag "off" nil)
  643. (const :tag "before tags first" t)
  644. (const :tag "after tags first" reversed)))))
  645. (if (fboundp 'defvaralias)
  646. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  647. (defcustom org-special-ctrl-k nil
  648. "Non-nil means `C-k' will behave specially in headlines.
  649. When nil, `C-k' will call the default `kill-line' command.
  650. When t, the following will happen while the cursor is in the headline:
  651. - When the cursor is at the beginning of a headline, kill the entire
  652. line and possible the folded subtree below the line.
  653. - When in the middle of the headline text, kill the headline up to the tags.
  654. - When after the headline text, kill the tags."
  655. :group 'org-edit-structure
  656. :type 'boolean)
  657. (defcustom org-yank-folded-subtrees t
  658. "Non-nil means, when yanking subtrees, fold them.
  659. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  660. it starts with a heading and all other headings in it are either children
  661. or siblings, then fold all the subtrees. However, do this only if no
  662. text after the yank would be swallowed into a folded tree by this action."
  663. :group 'org-edit-structure
  664. :type 'boolean)
  665. (defcustom org-yank-adjusted-subtrees nil
  666. "Non-nil means, when yanking subtrees, adjust the level.
  667. With this setting, `org-paste-subtree' is used to insert the subtree, see
  668. this function for details."
  669. :group 'org-edit-structure
  670. :type 'boolean)
  671. (defcustom org-M-RET-may-split-line '((default . t))
  672. "Non-nil means, M-RET will split the line at the cursor position.
  673. When nil, it will go to the end of the line before making a
  674. new line.
  675. You may also set this option in a different way for different
  676. contexts. Valid contexts are:
  677. headline when creating a new headline
  678. item when creating a new item
  679. table in a table field
  680. default the value to be used for all contexts not explicitly
  681. customized"
  682. :group 'org-structure
  683. :group 'org-table
  684. :type '(choice
  685. (const :tag "Always" t)
  686. (const :tag "Never" nil)
  687. (repeat :greedy t :tag "Individual contexts"
  688. (cons
  689. (choice :tag "Context"
  690. (const headline)
  691. (const item)
  692. (const table)
  693. (const default))
  694. (boolean)))))
  695. (defcustom org-insert-heading-respect-content nil
  696. "Non-nil means, insert new headings after the current subtree.
  697. When nil, the new heading is created directly after the current line.
  698. The commands \\[org-insert-heading-respect-content] and
  699. \\[org-insert-todo-heading-respect-content] turn this variable on
  700. for the duration of the command."
  701. :group 'org-structure
  702. :type 'boolean)
  703. (defcustom org-blank-before-new-entry '((heading . auto)
  704. (plain-list-item . auto))
  705. "Should `org-insert-heading' leave a blank line before new heading/item?
  706. The value is an alist, with `heading' and `plain-list-item' as car,
  707. and a boolean flag as cdr. For plain lists, if the variable
  708. `org-empty-line-terminates-plain-lists' is set, the setting here
  709. is ignored and no empty line is inserted, to keep the list in tact."
  710. :group 'org-edit-structure
  711. :type '(list
  712. (cons (const heading)
  713. (choice (const :tag "Never" nil)
  714. (const :tag "Always" t)
  715. (const :tag "Auto" auto)))
  716. (cons (const plain-list-item)
  717. (choice (const :tag "Never" nil)
  718. (const :tag "Always" t)
  719. (const :tag "Auto" auto)))))
  720. (defcustom org-insert-heading-hook nil
  721. "Hook being run after inserting a new heading."
  722. :group 'org-edit-structure
  723. :type 'hook)
  724. (defcustom org-enable-fixed-width-editor t
  725. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  726. This currently only means, they are never auto-wrapped.
  727. When nil, such lines will be treated like ordinary lines.
  728. See also the QUOTE keyword."
  729. :group 'org-edit-structure
  730. :type 'boolean)
  731. (defcustom org-goto-auto-isearch t
  732. "Non-nil means, typing characters in org-goto starts incremental search."
  733. :group 'org-edit-structure
  734. :type 'boolean)
  735. (defgroup org-sparse-trees nil
  736. "Options concerning sparse trees in Org-mode."
  737. :tag "Org Sparse Trees"
  738. :group 'org-structure)
  739. (defcustom org-highlight-sparse-tree-matches t
  740. "Non-nil means, highlight all matches that define a sparse tree.
  741. The highlights will automatically disappear the next time the buffer is
  742. changed by an edit command."
  743. :group 'org-sparse-trees
  744. :type 'boolean)
  745. (defcustom org-remove-highlights-with-change t
  746. "Non-nil means, any change to the buffer will remove temporary highlights.
  747. Such highlights are created by `org-occur' and `org-clock-display'.
  748. When nil, `C-c C-c needs to be used to get rid of the highlights.
  749. The highlights created by `org-preview-latex-fragment' always need
  750. `C-c C-c' to be removed."
  751. :group 'org-sparse-trees
  752. :group 'org-time
  753. :type 'boolean)
  754. (defcustom org-occur-hook '(org-first-headline-recenter)
  755. "Hook that is run after `org-occur' has constructed a sparse tree.
  756. This can be used to recenter the window to show as much of the structure
  757. as possible."
  758. :group 'org-sparse-trees
  759. :type 'hook)
  760. (defgroup org-imenu-and-speedbar nil
  761. "Options concerning imenu and speedbar in Org-mode."
  762. :tag "Org Imenu and Speedbar"
  763. :group 'org-structure)
  764. (defcustom org-imenu-depth 2
  765. "The maximum level for Imenu access to Org-mode headlines.
  766. This also applied for speedbar access."
  767. :group 'org-imenu-and-speedbar
  768. :type 'integer)
  769. (defgroup org-table nil
  770. "Options concerning tables in Org-mode."
  771. :tag "Org Table"
  772. :group 'org)
  773. (defcustom org-enable-table-editor 'optimized
  774. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  775. When nil, such lines will be treated like ordinary lines.
  776. When equal to the symbol `optimized', the table editor will be optimized to
  777. do the following:
  778. - Automatic overwrite mode in front of whitespace in table fields.
  779. This makes the structure of the table stay in tact as long as the edited
  780. field does not exceed the column width.
  781. - Minimize the number of realigns. Normally, the table is aligned each time
  782. TAB or RET are pressed to move to another field. With optimization this
  783. happens only if changes to a field might have changed the column width.
  784. Optimization requires replacing the functions `self-insert-command',
  785. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  786. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  787. very good at guessing when a re-align will be necessary, but you can always
  788. force one with \\[org-ctrl-c-ctrl-c].
  789. If you would like to use the optimized version in Org-mode, but the
  790. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  791. This variable can be used to turn on and off the table editor during a session,
  792. but in order to toggle optimization, a restart is required.
  793. See also the variable `org-table-auto-blank-field'."
  794. :group 'org-table
  795. :type '(choice
  796. (const :tag "off" nil)
  797. (const :tag "on" t)
  798. (const :tag "on, optimized" optimized)))
  799. (defcustom org-self-insert-cluster-for-undo t
  800. "Non-nil means cluster self-insert commands for undo when possible.
  801. If this is set, then, like in the Emacs command loop, 20 consequtive
  802. characters will be undone together.
  803. This is configurable, because there is some impact on typing performance."
  804. :group 'org-table
  805. :type 'boolean)
  806. (defcustom org-table-tab-recognizes-table.el t
  807. "Non-nil means, TAB will automatically notice a table.el table.
  808. When it sees such a table, it moves point into it and - if necessary -
  809. calls `table-recognize-table'."
  810. :group 'org-table-editing
  811. :type 'boolean)
  812. (defgroup org-link nil
  813. "Options concerning links in Org-mode."
  814. :tag "Org Link"
  815. :group 'org)
  816. (defvar org-link-abbrev-alist-local nil
  817. "Buffer-local version of `org-link-abbrev-alist', which see.
  818. The value of this is taken from the #+LINK lines.")
  819. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  820. (defcustom org-link-abbrev-alist nil
  821. "Alist of link abbreviations.
  822. The car of each element is a string, to be replaced at the start of a link.
  823. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  824. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  825. [[linkkey:tag][description]]
  826. The 'linkkey' must be a word word, starting with a letter, followed
  827. by letters, numbers, '-' or '_'.
  828. If REPLACE is a string, the tag will simply be appended to create the link.
  829. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  830. the placeholder \"%h\" will cause a url-encoded version of the tag to
  831. be inserted at that point (see the function `url-hexify-string').
  832. REPLACE may also be a function that will be called with the tag as the
  833. only argument to create the link, which should be returned as a string.
  834. See the manual for examples."
  835. :group 'org-link
  836. :type '(repeat
  837. (cons
  838. (string :tag "Protocol")
  839. (choice
  840. (string :tag "Format")
  841. (function)))))
  842. (defcustom org-descriptive-links t
  843. "Non-nil means, hide link part and only show description of bracket links.
  844. Bracket links are like [[link][description]]. This variable sets the initial
  845. state in new org-mode buffers. The setting can then be toggled on a
  846. per-buffer basis from the Org->Hyperlinks menu."
  847. :group 'org-link
  848. :type 'boolean)
  849. (defcustom org-link-file-path-type 'adaptive
  850. "How the path name in file links should be stored.
  851. Valid values are:
  852. relative Relative to the current directory, i.e. the directory of the file
  853. into which the link is being inserted.
  854. absolute Absolute path, if possible with ~ for home directory.
  855. noabbrev Absolute path, no abbreviation of home directory.
  856. adaptive Use relative path for files in the current directory and sub-
  857. directories of it. For other files, use an absolute path."
  858. :group 'org-link
  859. :type '(choice
  860. (const relative)
  861. (const absolute)
  862. (const noabbrev)
  863. (const adaptive)))
  864. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  865. "Types of links that should be activated in Org-mode files.
  866. This is a list of symbols, each leading to the activation of a certain link
  867. type. In principle, it does not hurt to turn on most link types - there may
  868. be a small gain when turning off unused link types. The types are:
  869. bracket The recommended [[link][description]] or [[link]] links with hiding.
  870. angular Links in angular brackets that may contain whitespace like
  871. <bbdb:Carsten Dominik>.
  872. plain Plain links in normal text, no whitespace, like http://google.com.
  873. radio Text that is matched by a radio target, see manual for details.
  874. tag Tag settings in a headline (link to tag search).
  875. date Time stamps (link to calendar).
  876. footnote Footnote labels.
  877. Changing this variable requires a restart of Emacs to become effective."
  878. :group 'org-link
  879. :type '(set :greedy t
  880. (const :tag "Double bracket links (new style)" bracket)
  881. (const :tag "Angular bracket links (old style)" angular)
  882. (const :tag "Plain text links" plain)
  883. (const :tag "Radio target matches" radio)
  884. (const :tag "Tags" tag)
  885. (const :tag "Timestamps" date)
  886. (const :tag "Footnotes" footnote)))
  887. (defcustom org-make-link-description-function nil
  888. "Function to use to generate link descriptions from links. If
  889. nil the link location will be used. This function must take two
  890. parameters; the first is the link and the second the description
  891. org-insert-link has generated, and should return the description
  892. to use."
  893. :group 'org-link
  894. :type 'function)
  895. (defgroup org-link-store nil
  896. "Options concerning storing links in Org-mode."
  897. :tag "Org Store Link"
  898. :group 'org-link)
  899. (defcustom org-email-link-description-format "Email %c: %.30s"
  900. "Format of the description part of a link to an email or usenet message.
  901. The following %-escapes will be replaced by corresponding information:
  902. %F full \"From\" field
  903. %f name, taken from \"From\" field, address if no name
  904. %T full \"To\" field
  905. %t first name in \"To\" field, address if no name
  906. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  907. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  908. %s subject
  909. %m message-id.
  910. You may use normal field width specification between the % and the letter.
  911. This is for example useful to limit the length of the subject.
  912. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  913. :group 'org-link-store
  914. :type 'string)
  915. (defcustom org-from-is-user-regexp
  916. (let (r1 r2)
  917. (when (and user-mail-address (not (string= user-mail-address "")))
  918. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  919. (when (and user-full-name (not (string= user-full-name "")))
  920. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  921. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  922. "Regexp matched against the \"From:\" header of an email or usenet message.
  923. It should match if the message is from the user him/herself."
  924. :group 'org-link-store
  925. :type 'regexp)
  926. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  927. "Non-nil means, storing a link to an Org file will use entry IDs.
  928. Note that before this variable is even considered, org-id must be loaded,
  929. to please customize `org-modules' and turn it on.
  930. The variable can have the following values:
  931. t Create an ID if needed to make a link to the current entry.
  932. create-if-interactive
  933. If `org-store-link' is called directly (interactively, as a user
  934. command), do create an ID to support the link. But when doing the
  935. job for remember, only use the ID if it already exists. The
  936. purpose of this setting is to avoid proliferation of unwanted
  937. IDs, just because you happen to be in an Org file when you
  938. call `org-remember' that automatically and preemptively
  939. creates a link. If you do want to get an ID link in a remember
  940. template to an entry not having an ID, create it first by
  941. explicitly creating a link to it, using `C-c C-l' first.
  942. use-existing
  943. Use existing ID, do not create one.
  944. nil Never use an ID to make a link, instead link using a text search for
  945. the headline text."
  946. :group 'org-link-store
  947. :type '(choice
  948. (const :tag "Create ID to make link" t)
  949. (const :tag "Create if storing link interactively"
  950. create-if-interactive)
  951. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  952. create-if-interactive-and-no-custom-id)
  953. (const :tag "Only use existing" use-existing)
  954. (const :tag "Do not use ID to create link" nil)))
  955. (defcustom org-context-in-file-links t
  956. "Non-nil means, file links from `org-store-link' contain context.
  957. A search string will be added to the file name with :: as separator and
  958. used to find the context when the link is activated by the command
  959. `org-open-at-point'.
  960. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  961. negates this setting for the duration of the command."
  962. :group 'org-link-store
  963. :type 'boolean)
  964. (defcustom org-keep-stored-link-after-insertion nil
  965. "Non-nil means, keep link in list for entire session.
  966. The command `org-store-link' adds a link pointing to the current
  967. location to an internal list. These links accumulate during a session.
  968. The command `org-insert-link' can be used to insert links into any
  969. Org-mode file (offering completion for all stored links). When this
  970. option is nil, every link which has been inserted once using \\[org-insert-link]
  971. will be removed from the list, to make completing the unused links
  972. more efficient."
  973. :group 'org-link-store
  974. :type 'boolean)
  975. (defgroup org-link-follow nil
  976. "Options concerning following links in Org-mode."
  977. :tag "Org Follow Link"
  978. :group 'org-link)
  979. (defcustom org-link-translation-function nil
  980. "Function to translate links with different syntax to Org syntax.
  981. This can be used to translate links created for example by the Planner
  982. or emacs-wiki packages to Org syntax.
  983. The function must accept two parameters, a TYPE containing the link
  984. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  985. which is everything after the link protocol. It should return a cons
  986. with possibly modified values of type and path.
  987. Org contains a function for this, so if you set this variable to
  988. `org-translate-link-from-planner', you should be able follow many
  989. links created by planner."
  990. :group 'org-link-follow
  991. :type 'function)
  992. (defcustom org-follow-link-hook nil
  993. "Hook that is run after a link has been followed."
  994. :group 'org-link-follow
  995. :type 'hook)
  996. (defcustom org-tab-follows-link nil
  997. "Non-nil means, on links TAB will follow the link.
  998. Needs to be set before org.el is loaded.
  999. This really should not be used, it does not make sense, and the
  1000. implementation is bad."
  1001. :group 'org-link-follow
  1002. :type 'boolean)
  1003. (defcustom org-return-follows-link nil
  1004. "Non-nil means, on links RET will follow the link.
  1005. Needs to be set before org.el is loaded."
  1006. :group 'org-link-follow
  1007. :type 'boolean)
  1008. (defcustom org-mouse-1-follows-link
  1009. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1010. "Non-nil means, mouse-1 on a link will follow the link.
  1011. A longer mouse click will still set point. Does not work on XEmacs.
  1012. Needs to be set before org.el is loaded."
  1013. :group 'org-link-follow
  1014. :type 'boolean)
  1015. (defcustom org-mark-ring-length 4
  1016. "Number of different positions to be recorded in the ring
  1017. Changing this requires a restart of Emacs to work correctly."
  1018. :group 'org-link-follow
  1019. :type 'integer)
  1020. (defcustom org-link-frame-setup
  1021. '((vm . vm-visit-folder-other-frame)
  1022. (gnus . gnus-other-frame)
  1023. (file . find-file-other-window))
  1024. "Setup the frame configuration for following links.
  1025. When following a link with Emacs, it may often be useful to display
  1026. this link in another window or frame. This variable can be used to
  1027. set this up for the different types of links.
  1028. For VM, use any of
  1029. `vm-visit-folder'
  1030. `vm-visit-folder-other-frame'
  1031. For Gnus, use any of
  1032. `gnus'
  1033. `gnus-other-frame'
  1034. `org-gnus-no-new-news'
  1035. For FILE, use any of
  1036. `find-file'
  1037. `find-file-other-window'
  1038. `find-file-other-frame'
  1039. For the calendar, use the variable `calendar-setup'.
  1040. For BBDB, it is currently only possible to display the matches in
  1041. another window."
  1042. :group 'org-link-follow
  1043. :type '(list
  1044. (cons (const vm)
  1045. (choice
  1046. (const vm-visit-folder)
  1047. (const vm-visit-folder-other-window)
  1048. (const vm-visit-folder-other-frame)))
  1049. (cons (const gnus)
  1050. (choice
  1051. (const gnus)
  1052. (const gnus-other-frame)
  1053. (const org-gnus-no-new-news)))
  1054. (cons (const file)
  1055. (choice
  1056. (const find-file)
  1057. (const find-file-other-window)
  1058. (const find-file-other-frame)))))
  1059. (defcustom org-display-internal-link-with-indirect-buffer nil
  1060. "Non-nil means, use indirect buffer to display infile links.
  1061. Activating internal links (from one location in a file to another location
  1062. in the same file) normally just jumps to the location. When the link is
  1063. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1064. another window. When this option is set, the other window actually displays
  1065. an indirect buffer clone of the current buffer, to avoid any visibility
  1066. changes to the current buffer."
  1067. :group 'org-link-follow
  1068. :type 'boolean)
  1069. (defcustom org-open-non-existing-files nil
  1070. "Non-nil means, `org-open-file' will open non-existing files.
  1071. When nil, an error will be generated."
  1072. :group 'org-link-follow
  1073. :type 'boolean)
  1074. (defcustom org-open-directory-means-index-dot-org nil
  1075. "Non-nil means, a link to a directory really means to index.org.
  1076. When nil, following a directory link will run dired or open a finder/explorer
  1077. window on that directory."
  1078. :group 'org-link-follow
  1079. :type 'boolean)
  1080. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1081. "Function and arguments to call for following mailto links.
  1082. This is a list with the first element being a lisp function, and the
  1083. remaining elements being arguments to the function. In string arguments,
  1084. %a will be replaced by the address, and %s will be replaced by the subject
  1085. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1086. :group 'org-link-follow
  1087. :type '(choice
  1088. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1089. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1090. (const :tag "message-mail" (message-mail "%a" "%s"))
  1091. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1092. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1093. "Non-nil means, ask for confirmation before executing shell links.
  1094. Shell links can be dangerous: just think about a link
  1095. [[shell:rm -rf ~/*][Google Search]]
  1096. This link would show up in your Org-mode document as \"Google Search\",
  1097. but really it would remove your entire home directory.
  1098. Therefore we advise against setting this variable to nil.
  1099. Just change it to `y-or-n-p' if you want to confirm with a
  1100. single keystroke rather than having to type \"yes\"."
  1101. :group 'org-link-follow
  1102. :type '(choice
  1103. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1104. (const :tag "with y-or-n (faster)" y-or-n-p)
  1105. (const :tag "no confirmation (dangerous)" nil)))
  1106. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1107. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1108. Elisp links can be dangerous: just think about a link
  1109. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1110. This link would show up in your Org-mode document as \"Google Search\",
  1111. but really it would remove your entire home directory.
  1112. Therefore we advise against setting this variable to nil.
  1113. Just change it to `y-or-n-p' if you want to confirm with a
  1114. single keystroke rather than having to type \"yes\"."
  1115. :group 'org-link-follow
  1116. :type '(choice
  1117. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1118. (const :tag "with y-or-n (faster)" y-or-n-p)
  1119. (const :tag "no confirmation (dangerous)" nil)))
  1120. (defconst org-file-apps-defaults-gnu
  1121. '((remote . emacs)
  1122. (system . mailcap)
  1123. (t . mailcap))
  1124. "Default file applications on a UNIX or GNU/Linux system.
  1125. See `org-file-apps'.")
  1126. (defconst org-file-apps-defaults-macosx
  1127. '((remote . emacs)
  1128. (t . "open %s")
  1129. (system . "open %s")
  1130. ("ps.gz" . "gv %s")
  1131. ("eps.gz" . "gv %s")
  1132. ("dvi" . "xdvi %s")
  1133. ("fig" . "xfig %s"))
  1134. "Default file applications on a MacOS X system.
  1135. The system \"open\" is known as a default, but we use X11 applications
  1136. for some files for which the OS does not have a good default.
  1137. See `org-file-apps'.")
  1138. (defconst org-file-apps-defaults-windowsnt
  1139. (list
  1140. '(remote . emacs)
  1141. (cons t
  1142. (list (if (featurep 'xemacs)
  1143. 'mswindows-shell-execute
  1144. 'w32-shell-execute)
  1145. "open" 'file))
  1146. (cons 'system
  1147. (list (if (featurep 'xemacs)
  1148. 'mswindows-shell-execute
  1149. 'w32-shell-execute)
  1150. "open" 'file)))
  1151. "Default file applications on a Windows NT system.
  1152. The system \"open\" is used for most files.
  1153. See `org-file-apps'.")
  1154. (defcustom org-file-apps
  1155. '(
  1156. (auto-mode . emacs)
  1157. ("\\.x?html?\\'" . default)
  1158. ("\\.pdf\\'" . default)
  1159. )
  1160. "External applications for opening `file:path' items in a document.
  1161. Org-mode uses system defaults for different file types, but
  1162. you can use this variable to set the application for a given file
  1163. extension. The entries in this list are cons cells where the car identifies
  1164. files and the cdr the corresponding command. Possible values for the
  1165. file identifier are
  1166. \"regex\" Regular expression matched against the file name. For backward
  1167. compatibility, this can also be a string with only alphanumeric
  1168. characters, which is then interpreted as an extension.
  1169. `directory' Matches a directory
  1170. `remote' Matches a remote file, accessible through tramp or efs.
  1171. Remote files most likely should be visited through Emacs
  1172. because external applications cannot handle such paths.
  1173. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1174. so all files Emacs knows how to handle. Using this with
  1175. command `emacs' will open most files in Emacs. Beware that this
  1176. will also open html files inside Emacs, unless you add
  1177. (\"html\" . default) to the list as well.
  1178. t Default for files not matched by any of the other options.
  1179. `system' The system command to open files, like `open' on Windows
  1180. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1181. that will be selected if you call `C-c C-o' with a double
  1182. `C-u C-u' prefix.
  1183. Possible values for the command are:
  1184. `emacs' The file will be visited by the current Emacs process.
  1185. `default' Use the default application for this file type, which is the
  1186. association for t in the list, most likely in the system-specific
  1187. part.
  1188. This can be used to overrule an unwanted setting in the
  1189. system-specific variable.
  1190. `system' Use the system command for opening files, like \"open\".
  1191. This command is specified by the entry whose car is `system'.
  1192. Most likely, the system-specific version of this variable
  1193. does define this command, but you can overrule/replace it
  1194. here.
  1195. string A command to be executed by a shell; %s will be replaced
  1196. by the path to the file.
  1197. sexp A Lisp form which will be evaluated. The file path will
  1198. be available in the Lisp variable `file'.
  1199. For more examples, see the system specific constants
  1200. `org-file-apps-defaults-macosx'
  1201. `org-file-apps-defaults-windowsnt'
  1202. `org-file-apps-defaults-gnu'."
  1203. :group 'org-link-follow
  1204. :type '(repeat
  1205. (cons (choice :value ""
  1206. (string :tag "Extension")
  1207. (const :tag "System command to open files" system)
  1208. (const :tag "Default for unrecognized files" t)
  1209. (const :tag "Remote file" remote)
  1210. (const :tag "Links to a directory" directory)
  1211. (const :tag "Any files that have Emacs modes"
  1212. auto-mode))
  1213. (choice :value ""
  1214. (const :tag "Visit with Emacs" emacs)
  1215. (const :tag "Use default" default)
  1216. (const :tag "Use the system command" system)
  1217. (string :tag "Command")
  1218. (sexp :tag "Lisp form")))))
  1219. (defgroup org-refile nil
  1220. "Options concerning refiling entries in Org-mode."
  1221. :tag "Org Refile"
  1222. :group 'org)
  1223. (defcustom org-directory "~/org"
  1224. "Directory with org files.
  1225. This is just a default location to look for Org files. There is no need
  1226. at all to put your files into this directory. It is only used in the
  1227. following situations:
  1228. 1. When a remember template specifies a target file that is not an
  1229. absolute path. The path will then be interpreted relative to
  1230. `org-directory'
  1231. 2. When a remember note is filed away in an interactive way (when exiting the
  1232. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1233. with `org-directory' as the default path."
  1234. :group 'org-refile
  1235. :group 'org-remember
  1236. :type 'directory)
  1237. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1238. "Default target for storing notes.
  1239. Used by the hooks for remember.el. This can be a string, or nil to mean
  1240. the value of `remember-data-file'.
  1241. You can set this on a per-template basis with the variable
  1242. `org-remember-templates'."
  1243. :group 'org-refile
  1244. :group 'org-remember
  1245. :type '(choice
  1246. (const :tag "Default from remember-data-file" nil)
  1247. file))
  1248. (defcustom org-goto-interface 'outline
  1249. "The default interface to be used for `org-goto'.
  1250. Allowed values are:
  1251. outline The interface shows an outline of the relevant file
  1252. and the correct heading is found by moving through
  1253. the outline or by searching with incremental search.
  1254. outline-path-completion Headlines in the current buffer are offered via
  1255. completion. This is the interface also used by
  1256. the refile command."
  1257. :group 'org-refile
  1258. :type '(choice
  1259. (const :tag "Outline" outline)
  1260. (const :tag "Outline-path-completion" outline-path-completion)))
  1261. (defcustom org-goto-max-level 5
  1262. "Maximum level to be considered when running org-goto with refile interface."
  1263. :group 'org-refile
  1264. :type 'integer)
  1265. (defcustom org-reverse-note-order nil
  1266. "Non-nil means, store new notes at the beginning of a file or entry.
  1267. When nil, new notes will be filed to the end of a file or entry.
  1268. This can also be a list with cons cells of regular expressions that
  1269. are matched against file names, and values."
  1270. :group 'org-remember
  1271. :group 'org-refile
  1272. :type '(choice
  1273. (const :tag "Reverse always" t)
  1274. (const :tag "Reverse never" nil)
  1275. (repeat :tag "By file name regexp"
  1276. (cons regexp boolean))))
  1277. (defcustom org-refile-targets nil
  1278. "Targets for refiling entries with \\[org-refile].
  1279. This is list of cons cells. Each cell contains:
  1280. - a specification of the files to be considered, either a list of files,
  1281. or a symbol whose function or variable value will be used to retrieve
  1282. a file name or a list of file names. If you use `org-agenda-files' for
  1283. that, all agenda files will be scanned for targets. Nil means, consider
  1284. headings in the current buffer.
  1285. - A specification of how to find candidate refile targets. This may be
  1286. any of:
  1287. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1288. This tag has to be present in all target headlines, inheritance will
  1289. not be considered.
  1290. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1291. todo keyword.
  1292. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1293. headlines that are refiling targets.
  1294. - a cons cell (:level . N). Any headline of level N is considered a target.
  1295. Note that, when `org-odd-levels-only' is set, level corresponds to
  1296. order in hierarchy, not to the number of stars.
  1297. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1298. Note that, when `org-odd-levels-only' is set, level corresponds to
  1299. order in hierarchy, not to the number of stars.
  1300. You can set the variable `org-refile-target-verify-function' to a function
  1301. to verify each headline found by the simple critery above.
  1302. When this variable is nil, all top-level headlines in the current buffer
  1303. are used, equivalent to the value `((nil . (:level . 1))'."
  1304. :group 'org-refile
  1305. :type '(repeat
  1306. (cons
  1307. (choice :value org-agenda-files
  1308. (const :tag "All agenda files" org-agenda-files)
  1309. (const :tag "Current buffer" nil)
  1310. (function) (variable) (file))
  1311. (choice :tag "Identify target headline by"
  1312. (cons :tag "Specific tag" (const :value :tag) (string))
  1313. (cons :tag "TODO keyword" (const :value :todo) (string))
  1314. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1315. (cons :tag "Level number" (const :value :level) (integer))
  1316. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1317. (defcustom org-refile-target-verify-function nil
  1318. "Function to verify if the headline at point should be a refile target.
  1319. The function will be called without arguments, with point at the
  1320. beginning of the headline. It should return t and leave point
  1321. where it is if the headline is a valid target for refiling.
  1322. If the target should not be selected, the function must return nil.
  1323. In addition to this, it may move point to a place from where the search
  1324. should be continued. For example, the function may decide that the entire
  1325. subtree of the current entry should be excluded and move point to the end
  1326. of the subtree."
  1327. :group 'org-refile
  1328. :type 'function)
  1329. (defcustom org-refile-use-outline-path nil
  1330. "Non-nil means, provide refile targets as paths.
  1331. So a level 3 headline will be available as level1/level2/level3.
  1332. When the value is `file', also include the file name (without directory)
  1333. into the path. In this case, you can also stop the completion after
  1334. the file name, to get entries inserted as top level in the file.
  1335. When `full-file-path', include the full file path."
  1336. :group 'org-refile
  1337. :type '(choice
  1338. (const :tag "Not" nil)
  1339. (const :tag "Yes" t)
  1340. (const :tag "Start with file name" file)
  1341. (const :tag "Start with full file path" full-file-path)))
  1342. (defcustom org-outline-path-complete-in-steps t
  1343. "Non-nil means, complete the outline path in hierarchical steps.
  1344. When Org-mode uses the refile interface to select an outline path
  1345. \(see variable `org-refile-use-outline-path'), the completion of
  1346. the path can be done is a single go, or if can be done in steps down
  1347. the headline hierarchy. Going in steps is probably the best if you
  1348. do not use a special completion package like `ido' or `icicles'.
  1349. However, when using these packages, going in one step can be very
  1350. fast, while still showing the whole path to the entry."
  1351. :group 'org-refile
  1352. :type 'boolean)
  1353. (defcustom org-refile-allow-creating-parent-nodes nil
  1354. "Non-nil means, allow to create new nodes as refile targets.
  1355. New nodes are then created by adding \"/new node name\" to the completion
  1356. of an existing node. When the value of this variable is `confirm',
  1357. new node creation must be confirmed by the user (recommended)
  1358. When nil, the completion must match an existing entry.
  1359. Note that, if the new heading is not seen by the criteria
  1360. listed in `org-refile-targets', multiple instances of the same
  1361. heading would be created by trying again to file under the new
  1362. heading."
  1363. :group 'org-refile
  1364. :type '(choice
  1365. (const :tag "Never" nil)
  1366. (const :tag "Always" t)
  1367. (const :tag "Prompt for confirmation" confirm)))
  1368. (defgroup org-todo nil
  1369. "Options concerning TODO items in Org-mode."
  1370. :tag "Org TODO"
  1371. :group 'org)
  1372. (defgroup org-progress nil
  1373. "Options concerning Progress logging in Org-mode."
  1374. :tag "Org Progress"
  1375. :group 'org-time)
  1376. (defvar org-todo-interpretation-widgets
  1377. '(
  1378. (:tag "Sequence (cycling hits every state)" sequence)
  1379. (:tag "Type (cycling directly to DONE)" type))
  1380. "The available interpretation symbols for customizing
  1381. `org-todo-keywords'.
  1382. Interested libraries should add to this list.")
  1383. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1384. "List of TODO entry keyword sequences and their interpretation.
  1385. \\<org-mode-map>This is a list of sequences.
  1386. Each sequence starts with a symbol, either `sequence' or `type',
  1387. indicating if the keywords should be interpreted as a sequence of
  1388. action steps, or as different types of TODO items. The first
  1389. keywords are states requiring action - these states will select a headline
  1390. for inclusion into the global TODO list Org-mode produces. If one of
  1391. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1392. signify that no further action is necessary. If \"|\" is not found,
  1393. the last keyword is treated as the only DONE state of the sequence.
  1394. The command \\[org-todo] cycles an entry through these states, and one
  1395. additional state where no keyword is present. For details about this
  1396. cycling, see the manual.
  1397. TODO keywords and interpretation can also be set on a per-file basis with
  1398. the special #+SEQ_TODO and #+TYP_TODO lines.
  1399. Each keyword can optionally specify a character for fast state selection
  1400. \(in combination with the variable `org-use-fast-todo-selection')
  1401. and specifiers for state change logging, using the same syntax
  1402. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1403. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1404. indicates to record a time stamp each time this state is selected.
  1405. Each keyword may also specify if a timestamp or a note should be
  1406. recorded when entering or leaving the state, by adding additional
  1407. characters in the parenthesis after the keyword. This looks like this:
  1408. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1409. record only the time of the state change. With X and Y being either
  1410. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1411. Y when leaving the state if and only if the *target* state does not
  1412. define X. You may omit any of the fast-selection key or X or /Y,
  1413. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1414. For backward compatibility, this variable may also be just a list
  1415. of keywords - in this case the interpretation (sequence or type) will be
  1416. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1417. :group 'org-todo
  1418. :group 'org-keywords
  1419. :type '(choice
  1420. (repeat :tag "Old syntax, just keywords"
  1421. (string :tag "Keyword"))
  1422. (repeat :tag "New syntax"
  1423. (cons
  1424. (choice
  1425. :tag "Interpretation"
  1426. ;;Quick and dirty way to see
  1427. ;;`org-todo-interpretations'. This takes the
  1428. ;;place of item arguments
  1429. :convert-widget
  1430. (lambda (widget)
  1431. (widget-put widget
  1432. :args (mapcar
  1433. #'(lambda (x)
  1434. (widget-convert
  1435. (cons 'const x)))
  1436. org-todo-interpretation-widgets))
  1437. widget))
  1438. (repeat
  1439. (string :tag "Keyword"))))))
  1440. (defvar org-todo-keywords-1 nil
  1441. "All TODO and DONE keywords active in a buffer.")
  1442. (make-variable-buffer-local 'org-todo-keywords-1)
  1443. (defvar org-todo-keywords-for-agenda nil)
  1444. (defvar org-done-keywords-for-agenda nil)
  1445. (defvar org-todo-keyword-alist-for-agenda nil)
  1446. (defvar org-tag-alist-for-agenda nil)
  1447. (defvar org-agenda-contributing-files nil)
  1448. (defvar org-not-done-keywords nil)
  1449. (make-variable-buffer-local 'org-not-done-keywords)
  1450. (defvar org-done-keywords nil)
  1451. (make-variable-buffer-local 'org-done-keywords)
  1452. (defvar org-todo-heads nil)
  1453. (make-variable-buffer-local 'org-todo-heads)
  1454. (defvar org-todo-sets nil)
  1455. (make-variable-buffer-local 'org-todo-sets)
  1456. (defvar org-todo-log-states nil)
  1457. (make-variable-buffer-local 'org-todo-log-states)
  1458. (defvar org-todo-kwd-alist nil)
  1459. (make-variable-buffer-local 'org-todo-kwd-alist)
  1460. (defvar org-todo-key-alist nil)
  1461. (make-variable-buffer-local 'org-todo-key-alist)
  1462. (defvar org-todo-key-trigger nil)
  1463. (make-variable-buffer-local 'org-todo-key-trigger)
  1464. (defcustom org-todo-interpretation 'sequence
  1465. "Controls how TODO keywords are interpreted.
  1466. This variable is in principle obsolete and is only used for
  1467. backward compatibility, if the interpretation of todo keywords is
  1468. not given already in `org-todo-keywords'. See that variable for
  1469. more information."
  1470. :group 'org-todo
  1471. :group 'org-keywords
  1472. :type '(choice (const sequence)
  1473. (const type)))
  1474. (defcustom org-use-fast-todo-selection t
  1475. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1476. This variable describes if and under what circumstances the cycling
  1477. mechanism for TODO keywords will be replaced by a single-key, direct
  1478. selection scheme.
  1479. When nil, fast selection is never used.
  1480. When the symbol `prefix', it will be used when `org-todo' is called with
  1481. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1482. in an agenda buffer.
  1483. When t, fast selection is used by default. In this case, the prefix
  1484. argument forces cycling instead.
  1485. In all cases, the special interface is only used if access keys have actually
  1486. been assigned by the user, i.e. if keywords in the configuration are followed
  1487. by a letter in parenthesis, like TODO(t)."
  1488. :group 'org-todo
  1489. :type '(choice
  1490. (const :tag "Never" nil)
  1491. (const :tag "By default" t)
  1492. (const :tag "Only with C-u C-c C-t" prefix)))
  1493. (defcustom org-provide-todo-statistics t
  1494. "Non-nil means, update todo statistics after insert and toggle.
  1495. When this is set, todo statistics is updated in the parent of the current
  1496. entry each time a todo state is changed."
  1497. :group 'org-todo
  1498. :type 'boolean)
  1499. (defcustom org-hierarchical-todo-statistics t
  1500. "Non-nil means, TODO statistics covers just direct children.
  1501. When nil, all entries in the subtree are considered.
  1502. This has only an effect if `org-provide-todo-statistics' is set."
  1503. :group 'org-todo
  1504. :type 'boolean)
  1505. (defcustom org-after-todo-state-change-hook nil
  1506. "Hook which is run after the state of a TODO item was changed.
  1507. The new state (a string with a TODO keyword, or nil) is available in the
  1508. Lisp variable `state'."
  1509. :group 'org-todo
  1510. :type 'hook)
  1511. (defvar org-blocker-hook nil
  1512. "Hook for functions that are allowed to block a state change.
  1513. Each function gets as its single argument a property list, see
  1514. `org-trigger-hook' for more information about this list.
  1515. If any of the functions in this hook returns nil, the state change
  1516. is blocked.")
  1517. (defvar org-trigger-hook nil
  1518. "Hook for functions that are triggered by a state change.
  1519. Each function gets as its single argument a property list with at least
  1520. the following elements:
  1521. (:type type-of-change :position pos-at-entry-start
  1522. :from old-state :to new-state)
  1523. Depending on the type, more properties may be present.
  1524. This mechanism is currently implemented for:
  1525. TODO state changes
  1526. ------------------
  1527. :type todo-state-change
  1528. :from previous state (keyword as a string), or nil, or a symbol
  1529. 'todo' or 'done', to indicate the general type of state.
  1530. :to new state, like in :from")
  1531. (defcustom org-enforce-todo-dependencies nil
  1532. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1533. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1534. be blocked if any prior sibling is not yet done.
  1535. Finally, if the parent is blocked because of ordered siblings of its own,
  1536. the child will also be blocked.
  1537. This variable needs to be set before org.el is loaded, and you need to
  1538. restart Emacs after a change to make the change effective. The only way
  1539. to change is while Emacs is running is through the customize interface."
  1540. :set (lambda (var val)
  1541. (set var val)
  1542. (if val
  1543. (add-hook 'org-blocker-hook
  1544. 'org-block-todo-from-children-or-siblings-or-parent)
  1545. (remove-hook 'org-blocker-hook
  1546. 'org-block-todo-from-children-or-siblings-or-parent)))
  1547. :group 'org-todo
  1548. :type 'boolean)
  1549. (defcustom org-enforce-todo-checkbox-dependencies nil
  1550. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1551. When this is nil, checkboxes have no influence on switching TODO states.
  1552. When non-nil, you first need to check off all check boxes before the TODO
  1553. entry can be switched to DONE.
  1554. This variable needs to be set before org.el is loaded, and you need to
  1555. restart Emacs after a change to make the change effective. The only way
  1556. to change is while Emacs is running is through the customize interface."
  1557. :set (lambda (var val)
  1558. (set var val)
  1559. (if val
  1560. (add-hook 'org-blocker-hook
  1561. 'org-block-todo-from-checkboxes)
  1562. (remove-hook 'org-blocker-hook
  1563. 'org-block-todo-from-checkboxes)))
  1564. :group 'org-todo
  1565. :type 'boolean)
  1566. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1567. "Non-nil means, inserting a TODO heading is treated as state change.
  1568. So when the command \\[org-insert-todo-heading] is used, state change
  1569. logging will apply if appropriate. When nil, the new TODO item will
  1570. be inserted directly, and no logging will take place."
  1571. :group 'org-todo
  1572. :type 'boolean)
  1573. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1574. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1575. This is the default behavior. However, setting this to nil allows a
  1576. convenient way to select a TODO state and bypass any logging associated
  1577. with that."
  1578. :group 'org-todo
  1579. :type 'boolean)
  1580. (defcustom org-todo-state-tags-triggers nil
  1581. "Tag changes that should be triggered by TODO state changes.
  1582. This is a list. Each entry is
  1583. (state-change (tag . flag) .......)
  1584. State-change can be a string with a state, and empty string to indicate the
  1585. state that has no TODO keyword, or it can be one of the symbols `todo'
  1586. or `done', meaning any not-done or done state, respectively."
  1587. :group 'org-todo
  1588. :group 'org-tags
  1589. :type '(repeat
  1590. (cons (choice :tag "When changing to"
  1591. (const :tag "Not-done state" todo)
  1592. (const :tag "Done state" done)
  1593. (string :tag "State"))
  1594. (repeat
  1595. (cons :tag "Tag action"
  1596. (string :tag "Tag")
  1597. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1598. (defcustom org-log-done nil
  1599. "Information to record when a task moves to the DONE state.
  1600. Possible values are:
  1601. nil Don't add anything, just change the keyword
  1602. time Add a time stamp to the task
  1603. note Prompt a closing note and add it with template `org-log-note-headings'
  1604. This option can also be set with on a per-file-basis with
  1605. #+STARTUP: nologdone
  1606. #+STARTUP: logdone
  1607. #+STARTUP: lognotedone
  1608. You can have local logging settings for a subtree by setting the LOGGING
  1609. property to one or more of these keywords."
  1610. :group 'org-todo
  1611. :group 'org-progress
  1612. :type '(choice
  1613. (const :tag "No logging" nil)
  1614. (const :tag "Record CLOSED timestamp" time)
  1615. (const :tag "Record CLOSED timestamp with closing note." note)))
  1616. ;; Normalize old uses of org-log-done.
  1617. (cond
  1618. ((eq org-log-done t) (setq org-log-done 'time))
  1619. ((and (listp org-log-done) (memq 'done org-log-done))
  1620. (setq org-log-done 'note)))
  1621. (defcustom org-log-note-clock-out nil
  1622. "Non-nil means, record a note when clocking out of an item.
  1623. This can also be configured on a per-file basis by adding one of
  1624. the following lines anywhere in the buffer:
  1625. #+STARTUP: lognoteclock-out
  1626. #+STARTUP: nolognoteclock-out"
  1627. :group 'org-todo
  1628. :group 'org-progress
  1629. :type 'boolean)
  1630. (defcustom org-log-done-with-time t
  1631. "Non-nil means, the CLOSED time stamp will contain date and time.
  1632. When nil, only the date will be recorded."
  1633. :group 'org-progress
  1634. :type 'boolean)
  1635. (defcustom org-log-note-headings
  1636. '((done . "CLOSING NOTE %t")
  1637. (state . "State %-12s from %-12S %t")
  1638. (note . "Note taken on %t")
  1639. (clock-out . ""))
  1640. "Headings for notes added to entries.
  1641. The value is an alist, with the car being a symbol indicating the note
  1642. context, and the cdr is the heading to be used. The heading may also be the
  1643. empty string.
  1644. %t in the heading will be replaced by a time stamp.
  1645. %s will be replaced by the new TODO state, in double quotes.
  1646. %S will be replaced by the old TODO state, in double quotes.
  1647. %u will be replaced by the user name.
  1648. %U will be replaced by the full user name."
  1649. :group 'org-todo
  1650. :group 'org-progress
  1651. :type '(list :greedy t
  1652. (cons (const :tag "Heading when closing an item" done) string)
  1653. (cons (const :tag
  1654. "Heading when changing todo state (todo sequence only)"
  1655. state) string)
  1656. (cons (const :tag "Heading when just taking a note" note) string)
  1657. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1658. (unless (assq 'note org-log-note-headings)
  1659. (push '(note . "%t") org-log-note-headings))
  1660. (defcustom org-log-into-drawer nil
  1661. "Non-nil means, insert state change notes and time stamps into a drawer.
  1662. When nil, state changes notes will be inserted after the headline and
  1663. any scheduling and clock lines, but not inside a drawer.
  1664. The value of this variable should be the name of the drawer to use.
  1665. LOGBOOK is proposed at the default drawer for this purpose, you can
  1666. also set this to a string to define the drawer of your choice.
  1667. A value of t is also allowed, representing \"LOGBOOK\".
  1668. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1669. will be ignored.
  1670. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1671. a subtree."
  1672. :group 'org-todo
  1673. :group 'org-progress
  1674. :type '(choice
  1675. (const :tag "Not into a drawer" nil)
  1676. (const :tag "LOGBOOK" t)
  1677. (string :tag "Other")))
  1678. (if (fboundp 'defvaralias)
  1679. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1680. (defun org-log-into-drawer ()
  1681. "Return the value of `org-log-into-drawer', but let properties overrule.
  1682. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1683. used instead of the default value."
  1684. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1685. (cond
  1686. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1687. ((equal p "t") "LOGBOOK")
  1688. (t p))))
  1689. (defcustom org-log-state-notes-insert-after-drawers nil
  1690. "Non-nil means, insert state change notes after any drawers in entry.
  1691. Only the drawers that *immediately* follow the headline and the
  1692. deadline/scheduled line are skipped.
  1693. When nil, insert notes right after the heading and perhaps the line
  1694. with deadline/scheduling if present.
  1695. This variable will have no effect if `org-log-into-drawer' is
  1696. set."
  1697. :group 'org-todo
  1698. :group 'org-progress
  1699. :type 'boolean)
  1700. (defcustom org-log-states-order-reversed t
  1701. "Non-nil means, the latest state change note will be directly after heading.
  1702. When nil, the notes will be orderer according to time."
  1703. :group 'org-todo
  1704. :group 'org-progress
  1705. :type 'boolean)
  1706. (defcustom org-log-repeat 'time
  1707. "Non-nil means, record moving through the DONE state when triggering repeat.
  1708. An auto-repeating tasks is immediately switched back to TODO when marked
  1709. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1710. the TODO keyword definition, or recording a closing note by setting
  1711. `org-log-done', there will be no record of the task moving through DONE.
  1712. This variable forces taking a note anyway. Possible values are:
  1713. nil Don't force a record
  1714. time Record a time stamp
  1715. note Record a note
  1716. This option can also be set with on a per-file-basis with
  1717. #+STARTUP: logrepeat
  1718. #+STARTUP: lognoterepeat
  1719. #+STARTUP: nologrepeat
  1720. You can have local logging settings for a subtree by setting the LOGGING
  1721. property to one or more of these keywords."
  1722. :group 'org-todo
  1723. :group 'org-progress
  1724. :type '(choice
  1725. (const :tag "Don't force a record" nil)
  1726. (const :tag "Force recording the DONE state" time)
  1727. (const :tag "Force recording a note with the DONE state" note)))
  1728. (defgroup org-priorities nil
  1729. "Priorities in Org-mode."
  1730. :tag "Org Priorities"
  1731. :group 'org-todo)
  1732. (defcustom org-enable-priority-commands t
  1733. "Non-nil means, priority commands are active.
  1734. When nil, these commands will be disabled, so that you never accidentally
  1735. set a priority."
  1736. :group 'org-priorities
  1737. :type 'boolean)
  1738. (defcustom org-highest-priority ?A
  1739. "The highest priority of TODO items. A character like ?A, ?B etc.
  1740. Must have a smaller ASCII number than `org-lowest-priority'."
  1741. :group 'org-priorities
  1742. :type 'character)
  1743. (defcustom org-lowest-priority ?C
  1744. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1745. Must have a larger ASCII number than `org-highest-priority'."
  1746. :group 'org-priorities
  1747. :type 'character)
  1748. (defcustom org-default-priority ?B
  1749. "The default priority of TODO items.
  1750. This is the priority an item get if no explicit priority is given."
  1751. :group 'org-priorities
  1752. :type 'character)
  1753. (defcustom org-priority-start-cycle-with-default t
  1754. "Non-nil means, start with default priority when starting to cycle.
  1755. When this is nil, the first step in the cycle will be (depending on the
  1756. command used) one higher or lower that the default priority."
  1757. :group 'org-priorities
  1758. :type 'boolean)
  1759. (defgroup org-time nil
  1760. "Options concerning time stamps and deadlines in Org-mode."
  1761. :tag "Org Time"
  1762. :group 'org)
  1763. (defcustom org-insert-labeled-timestamps-at-point nil
  1764. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1765. When nil, these labeled time stamps are forces into the second line of an
  1766. entry, just after the headline. When scheduling from the global TODO list,
  1767. the time stamp will always be forced into the second line."
  1768. :group 'org-time
  1769. :type 'boolean)
  1770. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1771. "Formats for `format-time-string' which are used for time stamps.
  1772. It is not recommended to change this constant.")
  1773. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1774. "Number of minutes to round time stamps to.
  1775. These are two values, the first applies when first creating a time stamp.
  1776. The second applies when changing it with the commands `S-up' and `S-down'.
  1777. When changing the time stamp, this means that it will change in steps
  1778. of N minutes, as given by the second value.
  1779. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1780. numbers should be factors of 60, so for example 5, 10, 15.
  1781. When this is larger than 1, you can still force an exact time-stamp by using
  1782. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1783. and by using a prefix arg to `S-up/down' to specify the exact number
  1784. of minutes to shift."
  1785. :group 'org-time
  1786. :get '(lambda (var) ; Make sure all entries have 5 elements
  1787. (if (integerp (default-value var))
  1788. (list (default-value var) 5)
  1789. (default-value var)))
  1790. :type '(list
  1791. (integer :tag "when inserting times")
  1792. (integer :tag "when modifying times")))
  1793. ;; Normalize old customizations of this variable.
  1794. (when (integerp org-time-stamp-rounding-minutes)
  1795. (setq org-time-stamp-rounding-minutes
  1796. (list org-time-stamp-rounding-minutes
  1797. org-time-stamp-rounding-minutes)))
  1798. (defcustom org-display-custom-times nil
  1799. "Non-nil means, overlay custom formats over all time stamps.
  1800. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1801. To turn this on on a per-file basis, insert anywhere in the file:
  1802. #+STARTUP: customtime"
  1803. :group 'org-time
  1804. :set 'set-default
  1805. :type 'sexp)
  1806. (make-variable-buffer-local 'org-display-custom-times)
  1807. (defcustom org-time-stamp-custom-formats
  1808. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1809. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1810. These are overlayed over the default ISO format if the variable
  1811. `org-display-custom-times' is set. Time like %H:%M should be at the
  1812. end of the second format. The custom formats are also honored by export
  1813. commands, if custom time display is turned on at the time of export."
  1814. :group 'org-time
  1815. :type 'sexp)
  1816. (defun org-time-stamp-format (&optional long inactive)
  1817. "Get the right format for a time string."
  1818. (let ((f (if long (cdr org-time-stamp-formats)
  1819. (car org-time-stamp-formats))))
  1820. (if inactive
  1821. (concat "[" (substring f 1 -1) "]")
  1822. f)))
  1823. (defcustom org-time-clocksum-format "%d:%02d"
  1824. "The format string used when creating CLOCKSUM lines, or when
  1825. org-mode generates a time duration."
  1826. :group 'org-time
  1827. :type 'string)
  1828. (defcustom org-deadline-warning-days 14
  1829. "No. of days before expiration during which a deadline becomes active.
  1830. This variable governs the display in sparse trees and in the agenda.
  1831. When 0 or negative, it means use this number (the absolute value of it)
  1832. even if a deadline has a different individual lead time specified.
  1833. Custom commands can set this variable in the options section."
  1834. :group 'org-time
  1835. :group 'org-agenda-daily/weekly
  1836. :type 'integer)
  1837. (defcustom org-read-date-prefer-future t
  1838. "Non-nil means, assume future for incomplete date input from user.
  1839. This affects the following situations:
  1840. 1. The user gives a day, but no month.
  1841. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1842. read this as the third of *next* month. However, if you enter \"17\",
  1843. it will be considered as *this* month.
  1844. 2. The user gives a month but not a year.
  1845. For example, if it is april and you enter \"feb 2\", this will be read
  1846. as feb 2, *next* year. \"May 5\", however, will be this year.
  1847. Currently this does not work for ISO week specifications.
  1848. When this option is nil, the current month and year will always be used
  1849. as defaults."
  1850. :group 'org-time
  1851. :type 'boolean)
  1852. (defcustom org-read-date-display-live t
  1853. "Non-nil means, display current interpretation of date prompt live.
  1854. This display will be in an overlay, in the minibuffer."
  1855. :group 'org-time
  1856. :type 'boolean)
  1857. (defcustom org-read-date-popup-calendar t
  1858. "Non-nil means, pop up a calendar when prompting for a date.
  1859. In the calendar, the date can be selected with mouse-1. However, the
  1860. minibuffer will also be active, and you can simply enter the date as well.
  1861. When nil, only the minibuffer will be available."
  1862. :group 'org-time
  1863. :type 'boolean)
  1864. (if (fboundp 'defvaralias)
  1865. (defvaralias 'org-popup-calendar-for-date-prompt
  1866. 'org-read-date-popup-calendar))
  1867. (defcustom org-read-date-minibuffer-setup-hook nil
  1868. "Hook to be used to set up keys for the date/time interface.
  1869. Add key definitions to `minibuffer-local-map', which will be a temporary
  1870. copy."
  1871. :group 'org-time
  1872. :type 'hook)
  1873. (defcustom org-extend-today-until 0
  1874. "The hour when your day really ends. Must be an integer.
  1875. This has influence for the following applications:
  1876. - When switching the agenda to \"today\". It it is still earlier than
  1877. the time given here, the day recognized as TODAY is actually yesterday.
  1878. - When a date is read from the user and it is still before the time given
  1879. here, the current date and time will be assumed to be yesterday, 23:59.
  1880. Also, timestamps inserted in remember templates follow this rule.
  1881. IMPORTANT: This is a feature whose implementation is and likely will
  1882. remain incomplete. Really, it is only here because past midnight seems to
  1883. be the favorite working time of John Wiegley :-)"
  1884. :group 'org-time
  1885. :type 'integer)
  1886. (defcustom org-edit-timestamp-down-means-later nil
  1887. "Non-nil means, S-down will increase the time in a time stamp.
  1888. When nil, S-up will increase."
  1889. :group 'org-time
  1890. :type 'boolean)
  1891. (defcustom org-calendar-follow-timestamp-change t
  1892. "Non-nil means, make the calendar window follow timestamp changes.
  1893. When a timestamp is modified and the calendar window is visible, it will be
  1894. moved to the new date."
  1895. :group 'org-time
  1896. :type 'boolean)
  1897. (defgroup org-tags nil
  1898. "Options concerning tags in Org-mode."
  1899. :tag "Org Tags"
  1900. :group 'org)
  1901. (defcustom org-tag-alist nil
  1902. "List of tags allowed in Org-mode files.
  1903. When this list is nil, Org-mode will base TAG input on what is already in the
  1904. buffer.
  1905. The value of this variable is an alist, the car of each entry must be a
  1906. keyword as a string, the cdr may be a character that is used to select
  1907. that tag through the fast-tag-selection interface.
  1908. See the manual for details."
  1909. :group 'org-tags
  1910. :type '(repeat
  1911. (choice
  1912. (cons (string :tag "Tag name")
  1913. (character :tag "Access char"))
  1914. (const :tag "Start radio group" (:startgroup))
  1915. (const :tag "End radio group" (:endgroup))
  1916. (const :tag "New line" (:newline)))))
  1917. (defcustom org-tag-persistent-alist nil
  1918. "List of tags that will always appear in all Org-mode files.
  1919. This is in addition to any in buffer settings or customizations
  1920. of `org-tag-alist'.
  1921. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1922. The value of this variable is an alist, the car of each entry must be a
  1923. keyword as a string, the cdr may be a character that is used to select
  1924. that tag through the fast-tag-selection interface.
  1925. See the manual for details.
  1926. To disable these tags on a per-file basis, insert anywhere in the file:
  1927. #+STARTUP: noptag"
  1928. :group 'org-tags
  1929. :type '(repeat
  1930. (choice
  1931. (cons (string :tag "Tag name")
  1932. (character :tag "Access char"))
  1933. (const :tag "Start radio group" (:startgroup))
  1934. (const :tag "End radio group" (:endgroup))
  1935. (const :tag "New line" (:newline)))))
  1936. (defvar org-file-tags nil
  1937. "List of tags that can be inherited by all entries in the file.
  1938. The tags will be inherited if the variable `org-use-tag-inheritance'
  1939. says they should be.
  1940. This variable is populated from #+TAG lines.")
  1941. (defcustom org-use-fast-tag-selection 'auto
  1942. "Non-nil means, use fast tag selection scheme.
  1943. This is a special interface to select and deselect tags with single keys.
  1944. When nil, fast selection is never used.
  1945. When the symbol `auto', fast selection is used if and only if selection
  1946. characters for tags have been configured, either through the variable
  1947. `org-tag-alist' or through a #+TAGS line in the buffer.
  1948. When t, fast selection is always used and selection keys are assigned
  1949. automatically if necessary."
  1950. :group 'org-tags
  1951. :type '(choice
  1952. (const :tag "Always" t)
  1953. (const :tag "Never" nil)
  1954. (const :tag "When selection characters are configured" 'auto)))
  1955. (defcustom org-fast-tag-selection-single-key nil
  1956. "Non-nil means, fast tag selection exits after first change.
  1957. When nil, you have to press RET to exit it.
  1958. During fast tag selection, you can toggle this flag with `C-c'.
  1959. This variable can also have the value `expert'. In this case, the window
  1960. displaying the tags menu is not even shown, until you press C-c again."
  1961. :group 'org-tags
  1962. :type '(choice
  1963. (const :tag "No" nil)
  1964. (const :tag "Yes" t)
  1965. (const :tag "Expert" expert)))
  1966. (defvar org-fast-tag-selection-include-todo nil
  1967. "Non-nil means, fast tags selection interface will also offer TODO states.
  1968. This is an undocumented feature, you should not rely on it.")
  1969. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1970. "The column to which tags should be indented in a headline.
  1971. If this number is positive, it specifies the column. If it is negative,
  1972. it means that the tags should be flushright to that column. For example,
  1973. -80 works well for a normal 80 character screen."
  1974. :group 'org-tags
  1975. :type 'integer)
  1976. (defcustom org-auto-align-tags t
  1977. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1978. These operations change the length of a headline and therefore shift
  1979. the tags around. With this options turned on, after each such operation
  1980. the tags are again aligned to `org-tags-column'."
  1981. :group 'org-tags
  1982. :type 'boolean)
  1983. (defcustom org-use-tag-inheritance t
  1984. "Non-nil means, tags in levels apply also for sublevels.
  1985. When nil, only the tags directly given in a specific line apply there.
  1986. This may also be a list of tags that should be inherited, or a regexp that
  1987. matches tags that should be inherited. Additional control is possible
  1988. with the variable `org-tags-exclude-from-inheritance' which gives an
  1989. explicit list of tags to be excluded from inheritance., even if the value of
  1990. `org-use-tag-inheritance' would select it for inheritance.
  1991. If this option is t, a match early-on in a tree can lead to a large
  1992. number of matches in the subtree when constructing the agenda or creating
  1993. a sparse tree. If you only want to see the first match in a tree during
  1994. a search, check out the variable `org-tags-match-list-sublevels'."
  1995. :group 'org-tags
  1996. :type '(choice
  1997. (const :tag "Not" nil)
  1998. (const :tag "Always" t)
  1999. (repeat :tag "Specific tags" (string :tag "Tag"))
  2000. (regexp :tag "Tags matched by regexp")))
  2001. (defcustom org-tags-exclude-from-inheritance nil
  2002. "List of tags that should never be inherited.
  2003. This is a way to exclude a few tags from inheritance. For way to do
  2004. the opposite, to actively allow inheritance for selected tags,
  2005. see the variable `org-use-tag-inheritance'."
  2006. :group 'org-tags
  2007. :type '(repeat (string :tag "Tag")))
  2008. (defun org-tag-inherit-p (tag)
  2009. "Check if TAG is one that should be inherited."
  2010. (cond
  2011. ((member tag org-tags-exclude-from-inheritance) nil)
  2012. ((eq org-use-tag-inheritance t) t)
  2013. ((not org-use-tag-inheritance) nil)
  2014. ((stringp org-use-tag-inheritance)
  2015. (string-match org-use-tag-inheritance tag))
  2016. ((listp org-use-tag-inheritance)
  2017. (member tag org-use-tag-inheritance))
  2018. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2019. (defcustom org-tags-match-list-sublevels t
  2020. "Non-nil means list also sublevels of headlines matching a search.
  2021. This variable applies to tags/property searches, and also to stuck
  2022. projects because this search is based on a tags match as well.
  2023. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2024. the sublevels of a headline matching a tag search often also match
  2025. the same search. Listing all of them can create very long lists.
  2026. Setting this variable to nil causes subtrees of a match to be skipped.
  2027. As a special case, if the tag search is restricted to TODO items, the
  2028. value of this variable is ignored and sublevels are always checked, to
  2029. make sure all corresponding TODO items find their way into the list.
  2030. This variable is semi-obsolete and probably should always be true. It
  2031. is better to limit inheritance to certain tags using the variables
  2032. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2033. :group 'org-tags
  2034. :type '(choice
  2035. (const :tag "No, don't list them" nil)
  2036. (const :tag "Yes, do list them" t)
  2037. (const :tag "List them, indented with leading dots" indented)))
  2038. (defvar org-tags-history nil
  2039. "History of minibuffer reads for tags.")
  2040. (defvar org-last-tags-completion-table nil
  2041. "The last used completion table for tags.")
  2042. (defvar org-after-tags-change-hook nil
  2043. "Hook that is run after the tags in a line have changed.")
  2044. (defgroup org-properties nil
  2045. "Options concerning properties in Org-mode."
  2046. :tag "Org Properties"
  2047. :group 'org)
  2048. (defcustom org-property-format "%-10s %s"
  2049. "How property key/value pairs should be formatted by `indent-line'.
  2050. When `indent-line' hits a property definition, it will format the line
  2051. according to this format, mainly to make sure that the values are
  2052. lined-up with respect to each other."
  2053. :group 'org-properties
  2054. :type 'string)
  2055. (defcustom org-use-property-inheritance nil
  2056. "Non-nil means, properties apply also for sublevels.
  2057. This setting is chiefly used during property searches. Turning it on can
  2058. cause significant overhead when doing a search, which is why it is not
  2059. on by default.
  2060. When nil, only the properties directly given in the current entry count.
  2061. When t, every property is inherited. The value may also be a list of
  2062. properties that should have inheritance, or a regular expression matching
  2063. properties that should be inherited.
  2064. However, note that some special properties use inheritance under special
  2065. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2066. and the properties ending in \"_ALL\" when they are used as descriptor
  2067. for valid values of a property.
  2068. Note for programmers:
  2069. When querying an entry with `org-entry-get', you can control if inheritance
  2070. should be used. By default, `org-entry-get' looks only at the local
  2071. properties. You can request inheritance by setting the inherit argument
  2072. to t (to force inheritance) or to `selective' (to respect the setting
  2073. in this variable)."
  2074. :group 'org-properties
  2075. :type '(choice
  2076. (const :tag "Not" nil)
  2077. (const :tag "Always" t)
  2078. (repeat :tag "Specific properties" (string :tag "Property"))
  2079. (regexp :tag "Properties matched by regexp")))
  2080. (defun org-property-inherit-p (property)
  2081. "Check if PROPERTY is one that should be inherited."
  2082. (cond
  2083. ((eq org-use-property-inheritance t) t)
  2084. ((not org-use-property-inheritance) nil)
  2085. ((stringp org-use-property-inheritance)
  2086. (string-match org-use-property-inheritance property))
  2087. ((listp org-use-property-inheritance)
  2088. (member property org-use-property-inheritance))
  2089. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2090. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2091. "The default column format, if no other format has been defined.
  2092. This variable can be set on the per-file basis by inserting a line
  2093. #+COLUMNS: %25ITEM ....."
  2094. :group 'org-properties
  2095. :type 'string)
  2096. (defcustom org-columns-ellipses ".."
  2097. "The ellipses to be used when a field in column view is truncated.
  2098. When this is the empty string, as many characters as possible are shown,
  2099. but then there will be no visual indication that the field has been truncated.
  2100. When this is a string of length N, the last N characters of a truncated
  2101. field are replaced by this string. If the column is narrower than the
  2102. ellipses string, only part of the ellipses string will be shown."
  2103. :group 'org-properties
  2104. :type 'string)
  2105. (defcustom org-columns-modify-value-for-display-function nil
  2106. "Function that modifies values for display in column view.
  2107. For example, it can be used to cut out a certain part from a time stamp.
  2108. The function must take 2 arguments:
  2109. column-title The title of the column (*not* the property name)
  2110. value The value that should be modified.
  2111. The function should return the value that should be displayed,
  2112. or nil if the normal value should be used."
  2113. :group 'org-properties
  2114. :type 'function)
  2115. (defcustom org-effort-property "Effort"
  2116. "The property that is being used to keep track of effort estimates.
  2117. Effort estimates given in this property need to have the format H:MM."
  2118. :group 'org-properties
  2119. :group 'org-progress
  2120. :type '(string :tag "Property"))
  2121. (defconst org-global-properties-fixed
  2122. '(("VISIBILITY_ALL" . "folded children content all")
  2123. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2124. "List of property/value pairs that can be inherited by any entry.
  2125. These are fixed values, for the preset properties. The user variable
  2126. that can be used to add to this list is `org-global-properties'.
  2127. The entries in this list are cons cells where the car is a property
  2128. name and cdr is a string with the value. If the value represents
  2129. multiple items like an \"_ALL\" property, separate the items by
  2130. spaces.")
  2131. (defcustom org-global-properties nil
  2132. "List of property/value pairs that can be inherited by any entry.
  2133. This list will be combined with the constant `org-global-properties-fixed'.
  2134. The entries in this list are cons cells where the car is a property
  2135. name and cdr is a string with the value.
  2136. You can set buffer-local values for the same purpose in the variable
  2137. `org-file-properties' this by adding lines like
  2138. #+PROPERTY: NAME VALUE"
  2139. :group 'org-properties
  2140. :type '(repeat
  2141. (cons (string :tag "Property")
  2142. (string :tag "Value"))))
  2143. (defvar org-file-properties nil
  2144. "List of property/value pairs that can be inherited by any entry.
  2145. Valid for the current buffer.
  2146. This variable is populated from #+PROPERTY lines.")
  2147. (make-variable-buffer-local 'org-file-properties)
  2148. (defgroup org-agenda nil
  2149. "Options concerning agenda views in Org-mode."
  2150. :tag "Org Agenda"
  2151. :group 'org)
  2152. (defvar org-category nil
  2153. "Variable used by org files to set a category for agenda display.
  2154. Such files should use a file variable to set it, for example
  2155. # -*- mode: org; org-category: \"ELisp\"
  2156. or contain a special line
  2157. #+CATEGORY: ELisp
  2158. If the file does not specify a category, then file's base name
  2159. is used instead.")
  2160. (make-variable-buffer-local 'org-category)
  2161. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2162. (defcustom org-agenda-files nil
  2163. "The files to be used for agenda display.
  2164. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2165. \\[org-remove-file]. You can also use customize to edit the list.
  2166. If an entry is a directory, all files in that directory that are matched by
  2167. `org-agenda-file-regexp' will be part of the file list.
  2168. If the value of the variable is not a list but a single file name, then
  2169. the list of agenda files is actually stored and maintained in that file, one
  2170. agenda file per line."
  2171. :group 'org-agenda
  2172. :type '(choice
  2173. (repeat :tag "List of files and directories" file)
  2174. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2175. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2176. "Regular expression to match files for `org-agenda-files'.
  2177. If any element in the list in that variable contains a directory instead
  2178. of a normal file, all files in that directory that are matched by this
  2179. regular expression will be included."
  2180. :group 'org-agenda
  2181. :type 'regexp)
  2182. (defcustom org-agenda-text-search-extra-files nil
  2183. "List of extra files to be searched by text search commands.
  2184. These files will be search in addition to the agenda files by the
  2185. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2186. Note that these files will only be searched for text search commands,
  2187. not for the other agenda views like todo lists, tag searches or the weekly
  2188. agenda. This variable is intended to list notes and possibly archive files
  2189. that should also be searched by these two commands.
  2190. In fact, if the first element in the list is the symbol `agenda-archives',
  2191. than all archive files of all agenda files will be added to the search
  2192. scope."
  2193. :group 'org-agenda
  2194. :type '(set :greedy t
  2195. (const :tag "Agenda Archives" agenda-archives)
  2196. (repeat :inline t (file))))
  2197. (if (fboundp 'defvaralias)
  2198. (defvaralias 'org-agenda-multi-occur-extra-files
  2199. 'org-agenda-text-search-extra-files))
  2200. (defcustom org-agenda-skip-unavailable-files nil
  2201. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2202. A nil value means to remove them, after a query, from the list."
  2203. :group 'org-agenda
  2204. :type 'boolean)
  2205. (defcustom org-calendar-to-agenda-key [?c]
  2206. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2207. The command `org-calendar-goto-agenda' will be bound to this key. The
  2208. default is the character `c' because then `c' can be used to switch back and
  2209. forth between agenda and calendar."
  2210. :group 'org-agenda
  2211. :type 'sexp)
  2212. (defcustom org-calendar-agenda-action-key [?k]
  2213. "The key to be installed in `calendar-mode-map' for agenda-action.
  2214. The command `org-agenda-action' will be bound to this key. The
  2215. default is the character `k' because we use the same key in the agenda."
  2216. :group 'org-agenda
  2217. :type 'sexp)
  2218. (eval-after-load "calendar"
  2219. '(progn
  2220. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2221. 'org-calendar-goto-agenda)
  2222. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2223. 'org-agenda-action)))
  2224. (defgroup org-latex nil
  2225. "Options for embedding LaTeX code into Org-mode."
  2226. :tag "Org LaTeX"
  2227. :group 'org)
  2228. (defcustom org-format-latex-options
  2229. '(:foreground default :background default :scale 1.0
  2230. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2231. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2232. "Options for creating images from LaTeX fragments.
  2233. This is a property list with the following properties:
  2234. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2235. `default' means use the foreground of the default face.
  2236. :background the background color, or \"Transparent\".
  2237. `default' means use the background of the default face.
  2238. :scale a scaling factor for the size of the images.
  2239. :html-foreground, :html-background, :html-scale
  2240. the same numbers for HTML export.
  2241. :matchers a list indicating which matchers should be used to
  2242. find LaTeX fragments. Valid members of this list are:
  2243. \"begin\" find environments
  2244. \"$1\" find single characters surrounded by $.$
  2245. \"$\" find math expressions surrounded by $...$
  2246. \"$$\" find math expressions surrounded by $$....$$
  2247. \"\\(\" find math expressions surrounded by \\(...\\)
  2248. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2249. :group 'org-latex
  2250. :type 'plist)
  2251. (defcustom org-format-latex-header "\\documentclass{article}
  2252. \\usepackage{fullpage} % do not remove
  2253. \\usepackage{amssymb}
  2254. \\usepackage[usenames]{color}
  2255. \\usepackage{amsmath}
  2256. \\usepackage{latexsym}
  2257. \\usepackage[mathscr]{eucal}
  2258. \\pagestyle{empty} % do not remove"
  2259. "The document header used for processing LaTeX fragments."
  2260. :group 'org-latex
  2261. :type 'string)
  2262. (defgroup org-font-lock nil
  2263. "Font-lock settings for highlighting in Org-mode."
  2264. :tag "Org Font Lock"
  2265. :group 'org)
  2266. (defcustom org-level-color-stars-only nil
  2267. "Non-nil means fontify only the stars in each headline.
  2268. When nil, the entire headline is fontified.
  2269. Changing it requires restart of `font-lock-mode' to become effective
  2270. also in regions already fontified."
  2271. :group 'org-font-lock
  2272. :type 'boolean)
  2273. (defcustom org-hide-leading-stars nil
  2274. "Non-nil means, hide the first N-1 stars in a headline.
  2275. This works by using the face `org-hide' for these stars. This
  2276. face is white for a light background, and black for a dark
  2277. background. You may have to customize the face `org-hide' to
  2278. make this work.
  2279. Changing it requires restart of `font-lock-mode' to become effective
  2280. also in regions already fontified.
  2281. You may also set this on a per-file basis by adding one of the following
  2282. lines to the buffer:
  2283. #+STARTUP: hidestars
  2284. #+STARTUP: showstars"
  2285. :group 'org-font-lock
  2286. :type 'boolean)
  2287. (defcustom org-fontify-done-headline nil
  2288. "Non-nil means, change the face of a headline if it is marked DONE.
  2289. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2290. When this is non-nil, the headline after the keyword is set to the
  2291. `org-headline-done' as an additional indication."
  2292. :group 'org-font-lock
  2293. :type 'boolean)
  2294. (defcustom org-fontify-emphasized-text t
  2295. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2296. Changing this variable requires a restart of Emacs to take effect."
  2297. :group 'org-font-lock
  2298. :type 'boolean)
  2299. (defcustom org-highlight-latex-fragments-and-specials nil
  2300. "Non-nil means, fontify what is treated specially by the exporters."
  2301. :group 'org-font-lock
  2302. :type 'boolean)
  2303. (defcustom org-hide-emphasis-markers nil
  2304. "Non-nil mean font-lock should hide the emphasis marker characters."
  2305. :group 'org-font-lock
  2306. :type 'boolean)
  2307. (defvar org-emph-re nil
  2308. "Regular expression for matching emphasis.")
  2309. (defvar org-verbatim-re nil
  2310. "Regular expression for matching verbatim text.")
  2311. (defvar org-emphasis-regexp-components) ; defined just below
  2312. (defvar org-emphasis-alist) ; defined just below
  2313. (defun org-set-emph-re (var val)
  2314. "Set variable and compute the emphasis regular expression."
  2315. (set var val)
  2316. (when (and (boundp 'org-emphasis-alist)
  2317. (boundp 'org-emphasis-regexp-components)
  2318. org-emphasis-alist org-emphasis-regexp-components)
  2319. (let* ((e org-emphasis-regexp-components)
  2320. (pre (car e))
  2321. (post (nth 1 e))
  2322. (border (nth 2 e))
  2323. (body (nth 3 e))
  2324. (nl (nth 4 e))
  2325. (body1 (concat body "*?"))
  2326. (markers (mapconcat 'car org-emphasis-alist ""))
  2327. (vmarkers (mapconcat
  2328. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2329. org-emphasis-alist "")))
  2330. ;; make sure special characters appear at the right position in the class
  2331. (if (string-match "\\^" markers)
  2332. (setq markers (concat (replace-match "" t t markers) "^")))
  2333. (if (string-match "-" markers)
  2334. (setq markers (concat (replace-match "" t t markers) "-")))
  2335. (if (string-match "\\^" vmarkers)
  2336. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2337. (if (string-match "-" vmarkers)
  2338. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2339. (if (> nl 0)
  2340. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2341. (int-to-string nl) "\\}")))
  2342. ;; Make the regexp
  2343. (setq org-emph-re
  2344. (concat "\\([" pre "]\\|^\\)"
  2345. "\\("
  2346. "\\([" markers "]\\)"
  2347. "\\("
  2348. "[^" border "]\\|"
  2349. "[^" border "]"
  2350. body1
  2351. "[^" border "]"
  2352. "\\)"
  2353. "\\3\\)"
  2354. "\\([" post "]\\|$\\)"))
  2355. (setq org-verbatim-re
  2356. (concat "\\([" pre "]\\|^\\)"
  2357. "\\("
  2358. "\\([" vmarkers "]\\)"
  2359. "\\("
  2360. "[^" border "]\\|"
  2361. "[^" border "]"
  2362. body1
  2363. "[^" border "]"
  2364. "\\)"
  2365. "\\3\\)"
  2366. "\\([" post "]\\|$\\)")))))
  2367. (defcustom org-emphasis-regexp-components
  2368. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2369. "Components used to build the regular expression for emphasis.
  2370. This is a list with 6 entries. Terminology: In an emphasis string
  2371. like \" *strong word* \", we call the initial space PREMATCH, the final
  2372. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2373. and \"trong wor\" is the body. The different components in this variable
  2374. specify what is allowed/forbidden in each part:
  2375. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2376. post Chars allowed as postmatch. End of line will be allowed too.
  2377. border The chars *forbidden* as border characters.
  2378. body-regexp A regexp like \".\" to match a body character. Don't use
  2379. non-shy groups here, and don't allow newline here.
  2380. newline The maximum number of newlines allowed in an emphasis exp.
  2381. Use customize to modify this, or restart Emacs after changing it."
  2382. :group 'org-font-lock
  2383. :set 'org-set-emph-re
  2384. :type '(list
  2385. (sexp :tag "Allowed chars in pre ")
  2386. (sexp :tag "Allowed chars in post ")
  2387. (sexp :tag "Forbidden chars in border ")
  2388. (sexp :tag "Regexp for body ")
  2389. (integer :tag "number of newlines allowed")
  2390. (option (boolean :tag "Please ignore this button"))))
  2391. (defcustom org-emphasis-alist
  2392. `(("*" bold "<b>" "</b>")
  2393. ("/" italic "<i>" "</i>")
  2394. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2395. ("=" org-code "<code>" "</code>" verbatim)
  2396. ("~" org-verbatim "<code>" "</code>" verbatim)
  2397. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2398. "<del>" "</del>")
  2399. )
  2400. "Special syntax for emphasized text.
  2401. Text starting and ending with a special character will be emphasized, for
  2402. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2403. characters, the face to be used by font-lock for highlighting in Org-mode
  2404. Emacs buffers, and the HTML tags to be used for this.
  2405. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2406. Use customize to modify this, or restart Emacs after changing it."
  2407. :group 'org-font-lock
  2408. :set 'org-set-emph-re
  2409. :type '(repeat
  2410. (list
  2411. (string :tag "Marker character")
  2412. (choice
  2413. (face :tag "Font-lock-face")
  2414. (plist :tag "Face property list"))
  2415. (string :tag "HTML start tag")
  2416. (string :tag "HTML end tag")
  2417. (option (const verbatim)))))
  2418. ;;; Miscellaneous options
  2419. (defgroup org-completion nil
  2420. "Completion in Org-mode."
  2421. :tag "Org Completion"
  2422. :group 'org)
  2423. (defcustom org-completion-use-ido nil
  2424. "Non-nil means, use ido completion wherever possible.
  2425. Note that `ido-mode' must be active for this variable to be relevant.
  2426. If you decide to turn this variable on, you might well want to turn off
  2427. `org-outline-path-complete-in-steps'."
  2428. :group 'org-completion
  2429. :type 'boolean)
  2430. (defcustom org-completion-fallback-command 'hippie-expand
  2431. "The expansion command called by \\[org-complete] in normal context.
  2432. Normal means, no org-mode-specific context."
  2433. :group 'org-completion
  2434. :type 'function)
  2435. ;;; Functions and variables from ther packages
  2436. ;; Declared here to avoid compiler warnings
  2437. ;; XEmacs only
  2438. (defvar outline-mode-menu-heading)
  2439. (defvar outline-mode-menu-show)
  2440. (defvar outline-mode-menu-hide)
  2441. (defvar zmacs-regions) ; XEmacs regions
  2442. ;; Emacs only
  2443. (defvar mark-active)
  2444. ;; Various packages
  2445. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2446. (declare-function calendar-forward-day "cal-move" (arg))
  2447. (declare-function calendar-goto-date "cal-move" (date))
  2448. (declare-function calendar-goto-today "cal-move" ())
  2449. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2450. (defvar calc-embedded-close-formula)
  2451. (defvar calc-embedded-open-formula)
  2452. (declare-function cdlatex-tab "ext:cdlatex" ())
  2453. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2454. (defvar font-lock-unfontify-region-function)
  2455. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2456. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2457. (defvar iswitchb-temp-buflist)
  2458. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2459. (defvar org-agenda-tags-todo-honor-ignore-options)
  2460. (declare-function org-agenda-skip "org-agenda" ())
  2461. (declare-function org-format-agenda-item "org-agenda"
  2462. (extra txt &optional category tags dotime noprefix remove-re))
  2463. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2464. (declare-function org-agenda-change-all-lines "org-agenda"
  2465. (newhead hdmarker &optional fixface just-this))
  2466. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2467. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2468. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2469. (beg end))
  2470. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2471. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2472. "org-agenda" (&optional end))
  2473. (declare-function parse-time-string "parse-time" (string))
  2474. (declare-function remember "remember" (&optional initial))
  2475. (declare-function remember-buffer-desc "remember" ())
  2476. (declare-function remember-finalize "remember" ())
  2477. (defvar remember-save-after-remembering)
  2478. (defvar remember-data-file)
  2479. (defvar remember-register)
  2480. (defvar remember-buffer)
  2481. (defvar remember-handler-functions)
  2482. (defvar remember-annotation-functions)
  2483. (defvar texmathp-why)
  2484. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2485. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2486. (defvar w3m-current-url)
  2487. (defvar w3m-current-title)
  2488. (defvar org-latex-regexps)
  2489. ;;; Autoload and prepare some org modules
  2490. ;; Some table stuff that needs to be defined here, because it is used
  2491. ;; by the functions setting up org-mode or checking for table context.
  2492. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2493. "Detects an org-type or table-type table.")
  2494. (defconst org-table-line-regexp "^[ \t]*|"
  2495. "Detects an org-type table line.")
  2496. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2497. "Detects an org-type table line.")
  2498. (defconst org-table-hline-regexp "^[ \t]*|-"
  2499. "Detects an org-type table hline.")
  2500. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2501. "Detects a table-type table hline.")
  2502. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2503. "Searching from within a table (any type) this finds the first line
  2504. outside the table.")
  2505. ;; Autoload the functions in org-table.el that are needed by functions here.
  2506. (eval-and-compile
  2507. (org-autoload "org-table"
  2508. '(org-table-align org-table-begin org-table-blank-field
  2509. org-table-convert org-table-convert-region org-table-copy-down
  2510. org-table-copy-region org-table-create
  2511. org-table-create-or-convert-from-region
  2512. org-table-create-with-table.el org-table-current-dline
  2513. org-table-cut-region org-table-delete-column org-table-edit-field
  2514. org-table-edit-formulas org-table-end org-table-eval-formula
  2515. org-table-export org-table-field-info
  2516. org-table-get-stored-formulas org-table-goto-column
  2517. org-table-hline-and-move org-table-import org-table-insert-column
  2518. org-table-insert-hline org-table-insert-row org-table-iterate
  2519. org-table-justify-field-maybe org-table-kill-row
  2520. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2521. org-table-move-column org-table-move-column-left
  2522. org-table-move-column-right org-table-move-row
  2523. org-table-move-row-down org-table-move-row-up
  2524. org-table-next-field org-table-next-row org-table-paste-rectangle
  2525. org-table-previous-field org-table-recalculate
  2526. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2527. org-table-toggle-coordinate-overlays
  2528. org-table-toggle-formula-debugger org-table-wrap-region
  2529. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2530. (defun org-at-table-p (&optional table-type)
  2531. "Return t if the cursor is inside an org-type table.
  2532. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2533. (if org-enable-table-editor
  2534. (save-excursion
  2535. (beginning-of-line 1)
  2536. (looking-at (if table-type org-table-any-line-regexp
  2537. org-table-line-regexp)))
  2538. nil))
  2539. (defsubst org-table-p () (org-at-table-p))
  2540. (defun org-at-table.el-p ()
  2541. "Return t if and only if we are at a table.el table."
  2542. (and (org-at-table-p 'any)
  2543. (save-excursion
  2544. (goto-char (org-table-begin 'any))
  2545. (looking-at org-table1-hline-regexp))))
  2546. (defun org-table-recognize-table.el ()
  2547. "If there is a table.el table nearby, recognize it and move into it."
  2548. (if org-table-tab-recognizes-table.el
  2549. (if (org-at-table.el-p)
  2550. (progn
  2551. (beginning-of-line 1)
  2552. (if (looking-at org-table-dataline-regexp)
  2553. nil
  2554. (if (looking-at org-table1-hline-regexp)
  2555. (progn
  2556. (beginning-of-line 2)
  2557. (if (looking-at org-table-any-border-regexp)
  2558. (beginning-of-line -1)))))
  2559. (if (re-search-forward "|" (org-table-end t) t)
  2560. (progn
  2561. (require 'table)
  2562. (if (table--at-cell-p (point))
  2563. t
  2564. (message "recognizing table.el table...")
  2565. (table-recognize-table)
  2566. (message "recognizing table.el table...done")))
  2567. (error "This should not happen..."))
  2568. t)
  2569. nil)
  2570. nil))
  2571. (defun org-at-table-hline-p ()
  2572. "Return t if the cursor is inside a hline in a table."
  2573. (if org-enable-table-editor
  2574. (save-excursion
  2575. (beginning-of-line 1)
  2576. (looking-at org-table-hline-regexp))
  2577. nil))
  2578. (defvar org-table-clean-did-remove-column nil)
  2579. (defun org-table-map-tables (function)
  2580. "Apply FUNCTION to the start of all tables in the buffer."
  2581. (save-excursion
  2582. (save-restriction
  2583. (widen)
  2584. (goto-char (point-min))
  2585. (while (re-search-forward org-table-any-line-regexp nil t)
  2586. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2587. (beginning-of-line 1)
  2588. (if (looking-at org-table-line-regexp)
  2589. (save-excursion (funcall function)))
  2590. (re-search-forward org-table-any-border-regexp nil 1))))
  2591. (message "Mapping tables: done"))
  2592. ;; Declare and autoload functions from org-exp.el & Co
  2593. (declare-function org-default-export-plist "org-exp")
  2594. (declare-function org-infile-export-plist "org-exp")
  2595. (declare-function org-get-current-options "org-exp")
  2596. (eval-and-compile
  2597. (org-autoload "org-exp"
  2598. '(org-export org-export-visible
  2599. org-insert-export-options-template
  2600. org-table-clean-before-export))
  2601. (org-autoload "org-ascii"
  2602. '(org-export-as-ascii org-export-ascii-preprocess
  2603. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2604. org-export-region-as-ascii))
  2605. (org-autoload "org-html"
  2606. '(org-export-as-html-and-open
  2607. org-export-as-html-batch org-export-as-html-to-buffer
  2608. org-replace-region-by-html org-export-region-as-html
  2609. org-export-as-html))
  2610. (org-autoload "org-icalendar"
  2611. '(org-export-icalendar-this-file
  2612. org-export-icalendar-all-agenda-files
  2613. org-export-icalendar-combine-agenda-files))
  2614. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2615. ;; Declare and autoload functions from org-agenda.el
  2616. (eval-and-compile
  2617. (org-autoload "org-agenda"
  2618. '(org-agenda org-agenda-list org-search-view
  2619. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2620. org-diary org-agenda-to-appt
  2621. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2622. ;; Autoload org-remember
  2623. (eval-and-compile
  2624. (org-autoload "org-remember"
  2625. '(org-remember-insinuate org-remember-annotation
  2626. org-remember-apply-template org-remember org-remember-handler)))
  2627. ;; Autoload org-clock.el
  2628. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2629. (beg end))
  2630. (declare-function org-clock-update-mode-line "org-clock" ())
  2631. (defvar org-clock-start-time)
  2632. (defvar org-clock-marker (make-marker)
  2633. "Marker recording the last clock-in.")
  2634. (eval-and-compile
  2635. (org-autoload
  2636. "org-clock"
  2637. '(org-clock-in org-clock-out org-clock-cancel
  2638. org-clock-goto org-clock-sum org-clock-display
  2639. org-clock-remove-overlays org-clock-report
  2640. org-clocktable-shift org-dblock-write:clocktable
  2641. org-get-clocktable)))
  2642. (defun org-clock-update-time-maybe ()
  2643. "If this is a CLOCK line, update it and return t.
  2644. Otherwise, return nil."
  2645. (interactive)
  2646. (save-excursion
  2647. (beginning-of-line 1)
  2648. (skip-chars-forward " \t")
  2649. (when (looking-at org-clock-string)
  2650. (let ((re (concat "[ \t]*" org-clock-string
  2651. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2652. "\\([ \t]*=>.*\\)?\\)?"))
  2653. ts te h m s neg)
  2654. (cond
  2655. ((not (looking-at re))
  2656. nil)
  2657. ((not (match-end 2))
  2658. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2659. (> org-clock-marker (point))
  2660. (<= org-clock-marker (point-at-eol)))
  2661. ;; The clock is running here
  2662. (setq org-clock-start-time
  2663. (apply 'encode-time
  2664. (org-parse-time-string (match-string 1))))
  2665. (org-clock-update-mode-line)))
  2666. (t
  2667. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2668. (end-of-line 1)
  2669. (setq ts (match-string 1)
  2670. te (match-string 3))
  2671. (setq s (- (time-to-seconds
  2672. (apply 'encode-time (org-parse-time-string te)))
  2673. (time-to-seconds
  2674. (apply 'encode-time (org-parse-time-string ts))))
  2675. neg (< s 0)
  2676. s (abs s)
  2677. h (floor (/ s 3600))
  2678. s (- s (* 3600 h))
  2679. m (floor (/ s 60))
  2680. s (- s (* 60 s)))
  2681. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2682. t))))))
  2683. (defun org-check-running-clock ()
  2684. "Check if the current buffer contains the running clock.
  2685. If yes, offer to stop it and to save the buffer with the changes."
  2686. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2687. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2688. (buffer-name))))
  2689. (org-clock-out)
  2690. (when (y-or-n-p "Save changed buffer?")
  2691. (save-buffer))))
  2692. (defun org-clocktable-try-shift (dir n)
  2693. "Check if this line starts a clock table, if yes, shift the time block."
  2694. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2695. (org-clocktable-shift dir n)))
  2696. ;; Autoload org-timer.el
  2697. (eval-and-compile
  2698. (org-autoload
  2699. "org-timer"
  2700. '(org-timer-start org-timer org-timer-item
  2701. org-timer-change-times-in-region)))
  2702. ;; Autoload org-feed.el
  2703. (eval-and-compile
  2704. (org-autoload
  2705. "org-feed"
  2706. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2707. ;; Autoload archiving code
  2708. ;; The stuff that is needed for cycling and tags has to be defined here.
  2709. (defgroup org-archive nil
  2710. "Options concerning archiving in Org-mode."
  2711. :tag "Org Archive"
  2712. :group 'org-structure)
  2713. (defcustom org-archive-location "%s_archive::"
  2714. "The location where subtrees should be archived.
  2715. The value of this variable is a string, consisting of two parts,
  2716. separated by a double-colon. The first part is a filename and
  2717. the second part is a headline.
  2718. When the filename is omitted, archiving happens in the same file.
  2719. %s in the filename will be replaced by the current file
  2720. name (without the directory part). Archiving to a different file
  2721. is useful to keep archived entries from contributing to the
  2722. Org-mode Agenda.
  2723. The archived entries will be filed as subtrees of the specified
  2724. headline. When the headline is omitted, the subtrees are simply
  2725. filed away at the end of the file, as top-level entries. Also in
  2726. the heading you can use %s to represent the file name, this can be
  2727. useful when using the same archive for a number of different files.
  2728. Here are a few examples:
  2729. \"%s_archive::\"
  2730. If the current file is Projects.org, archive in file
  2731. Projects.org_archive, as top-level trees. This is the default.
  2732. \"::* Archived Tasks\"
  2733. Archive in the current file, under the top-level headline
  2734. \"* Archived Tasks\".
  2735. \"~/org/archive.org::\"
  2736. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2737. \"~/org/archive.org::From %s\"
  2738. Archive in file ~/org/archive.org (absolute path), und headlines
  2739. \"From FILENAME\" where file name is the current file name.
  2740. \"basement::** Finished Tasks\"
  2741. Archive in file ./basement (relative path), as level 3 trees
  2742. below the level 2 heading \"** Finished Tasks\".
  2743. You may set this option on a per-file basis by adding to the buffer a
  2744. line like
  2745. #+ARCHIVE: basement::** Finished Tasks
  2746. You may also define it locally for a subtree by setting an ARCHIVE property
  2747. in the entry. If such a property is found in an entry, or anywhere up
  2748. the hierarchy, it will be used."
  2749. :group 'org-archive
  2750. :type 'string)
  2751. (defcustom org-archive-tag "ARCHIVE"
  2752. "The tag that marks a subtree as archived.
  2753. An archived subtree does not open during visibility cycling, and does
  2754. not contribute to the agenda listings.
  2755. After changing this, font-lock must be restarted in the relevant buffers to
  2756. get the proper fontification."
  2757. :group 'org-archive
  2758. :group 'org-keywords
  2759. :type 'string)
  2760. (defcustom org-agenda-skip-archived-trees t
  2761. "Non-nil means, the agenda will skip any items located in archived trees.
  2762. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2763. variable is no longer recommended, you should leave it at the value t.
  2764. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2765. :group 'org-archive
  2766. :group 'org-agenda-skip
  2767. :type 'boolean)
  2768. (defcustom org-columns-skip-arrchived-trees t
  2769. "Non-nil means, irgnore archived trees when creating column view."
  2770. :group 'org-archive
  2771. :group 'org-properties
  2772. :type 'boolean)
  2773. (defcustom org-cycle-open-archived-trees nil
  2774. "Non-nil means, `org-cycle' will open archived trees.
  2775. An archived tree is a tree marked with the tag ARCHIVE.
  2776. When nil, archived trees will stay folded. You can still open them with
  2777. normal outline commands like `show-all', but not with the cycling commands."
  2778. :group 'org-archive
  2779. :group 'org-cycle
  2780. :type 'boolean)
  2781. (defcustom org-sparse-tree-open-archived-trees nil
  2782. "Non-nil means sparse tree construction shows matches in archived trees.
  2783. When nil, matches in these trees are highlighted, but the trees are kept in
  2784. collapsed state."
  2785. :group 'org-archive
  2786. :group 'org-sparse-trees
  2787. :type 'boolean)
  2788. (defun org-cycle-hide-archived-subtrees (state)
  2789. "Re-hide all archived subtrees after a visibility state change."
  2790. (when (and (not org-cycle-open-archived-trees)
  2791. (not (memq state '(overview folded))))
  2792. (save-excursion
  2793. (let* ((globalp (memq state '(contents all)))
  2794. (beg (if globalp (point-min) (point)))
  2795. (end (if globalp (point-max) (org-end-of-subtree t))))
  2796. (org-hide-archived-subtrees beg end)
  2797. (goto-char beg)
  2798. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2799. (message "%s" (substitute-command-keys
  2800. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2801. (defun org-force-cycle-archived ()
  2802. "Cycle subtree even if it is archived."
  2803. (interactive)
  2804. (setq this-command 'org-cycle)
  2805. (let ((org-cycle-open-archived-trees t))
  2806. (call-interactively 'org-cycle)))
  2807. (defun org-hide-archived-subtrees (beg end)
  2808. "Re-hide all archived subtrees after a visibility state change."
  2809. (save-excursion
  2810. (let* ((re (concat ":" org-archive-tag ":")))
  2811. (goto-char beg)
  2812. (while (re-search-forward re end t)
  2813. (and (org-on-heading-p) (hide-subtree))
  2814. (org-end-of-subtree t)))))
  2815. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2816. (eval-and-compile
  2817. (org-autoload "org-archive"
  2818. '(org-add-archive-files org-archive-subtree
  2819. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2820. ;; Autoload Column View Code
  2821. (declare-function org-columns-number-to-string "org-colview")
  2822. (declare-function org-columns-get-format-and-top-level "org-colview")
  2823. (declare-function org-columns-compute "org-colview")
  2824. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2825. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2826. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2827. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2828. ;; Autoload ID code
  2829. (declare-function org-id-store-link "org-id")
  2830. (declare-function org-id-locations-load "org-id")
  2831. (declare-function org-id-locations-save "org-id")
  2832. (defvar org-id-track-globally)
  2833. (org-autoload "org-id"
  2834. '(org-id-get-create org-id-new org-id-copy org-id-get
  2835. org-id-get-with-outline-path-completion
  2836. org-id-get-with-outline-drilling
  2837. org-id-goto org-id-find org-id-store-link))
  2838. ;; Autoload Plotting Code
  2839. (org-autoload "org-plot"
  2840. '(org-plot/gnuplot))
  2841. ;;; Variables for pre-computed regular expressions, all buffer local
  2842. (defvar org-drawer-regexp nil
  2843. "Matches first line of a hidden block.")
  2844. (make-variable-buffer-local 'org-drawer-regexp)
  2845. (defvar org-todo-regexp nil
  2846. "Matches any of the TODO state keywords.")
  2847. (make-variable-buffer-local 'org-todo-regexp)
  2848. (defvar org-not-done-regexp nil
  2849. "Matches any of the TODO state keywords except the last one.")
  2850. (make-variable-buffer-local 'org-not-done-regexp)
  2851. (defvar org-not-done-heading-regexp nil
  2852. "Matches a TODO headline that is not done.")
  2853. (make-variable-buffer-local 'org-not-done-regexp)
  2854. (defvar org-todo-line-regexp nil
  2855. "Matches a headline and puts TODO state into group 2 if present.")
  2856. (make-variable-buffer-local 'org-todo-line-regexp)
  2857. (defvar org-complex-heading-regexp nil
  2858. "Matches a headline and puts everything into groups:
  2859. group 1: the stars
  2860. group 2: The todo keyword, maybe
  2861. group 3: Priority cookie
  2862. group 4: True headline
  2863. group 5: Tags")
  2864. (make-variable-buffer-local 'org-complex-heading-regexp)
  2865. (defvar org-todo-line-tags-regexp nil
  2866. "Matches a headline and puts TODO state into group 2 if present.
  2867. Also put tags into group 4 if tags are present.")
  2868. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2869. (defvar org-nl-done-regexp nil
  2870. "Matches newline followed by a headline with the DONE keyword.")
  2871. (make-variable-buffer-local 'org-nl-done-regexp)
  2872. (defvar org-looking-at-done-regexp nil
  2873. "Matches the DONE keyword a point.")
  2874. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2875. (defvar org-ds-keyword-length 12
  2876. "Maximum length of the Deadline and SCHEDULED keywords.")
  2877. (make-variable-buffer-local 'org-ds-keyword-length)
  2878. (defvar org-deadline-regexp nil
  2879. "Matches the DEADLINE keyword.")
  2880. (make-variable-buffer-local 'org-deadline-regexp)
  2881. (defvar org-deadline-time-regexp nil
  2882. "Matches the DEADLINE keyword together with a time stamp.")
  2883. (make-variable-buffer-local 'org-deadline-time-regexp)
  2884. (defvar org-deadline-line-regexp nil
  2885. "Matches the DEADLINE keyword and the rest of the line.")
  2886. (make-variable-buffer-local 'org-deadline-line-regexp)
  2887. (defvar org-scheduled-regexp nil
  2888. "Matches the SCHEDULED keyword.")
  2889. (make-variable-buffer-local 'org-scheduled-regexp)
  2890. (defvar org-scheduled-time-regexp nil
  2891. "Matches the SCHEDULED keyword together with a time stamp.")
  2892. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2893. (defvar org-closed-time-regexp nil
  2894. "Matches the CLOSED keyword together with a time stamp.")
  2895. (make-variable-buffer-local 'org-closed-time-regexp)
  2896. (defvar org-keyword-time-regexp nil
  2897. "Matches any of the 4 keywords, together with the time stamp.")
  2898. (make-variable-buffer-local 'org-keyword-time-regexp)
  2899. (defvar org-keyword-time-not-clock-regexp nil
  2900. "Matches any of the 3 keywords, together with the time stamp.")
  2901. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2902. (defvar org-maybe-keyword-time-regexp nil
  2903. "Matches a timestamp, possibly preceeded by a keyword.")
  2904. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2905. (defvar org-planning-or-clock-line-re nil
  2906. "Matches a line with planning or clock info.")
  2907. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2908. (defconst org-plain-time-of-day-regexp
  2909. (concat
  2910. "\\(\\<[012]?[0-9]"
  2911. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2912. "\\(--?"
  2913. "\\(\\<[012]?[0-9]"
  2914. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2915. "\\)?")
  2916. "Regular expression to match a plain time or time range.
  2917. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2918. groups carry important information:
  2919. 0 the full match
  2920. 1 the first time, range or not
  2921. 8 the second time, if it is a range.")
  2922. (defconst org-plain-time-extension-regexp
  2923. (concat
  2924. "\\(\\<[012]?[0-9]"
  2925. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2926. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2927. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2928. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2929. groups carry important information:
  2930. 0 the full match
  2931. 7 hours of duration
  2932. 9 minutes of duration")
  2933. (defconst org-stamp-time-of-day-regexp
  2934. (concat
  2935. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2936. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2937. "\\(--?"
  2938. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2939. "Regular expression to match a timestamp time or time range.
  2940. After a match, the following groups carry important information:
  2941. 0 the full match
  2942. 1 date plus weekday, for backreferencing to make sure both times on same day
  2943. 2 the first time, range or not
  2944. 4 the second time, if it is a range.")
  2945. (defconst org-startup-options
  2946. '(("fold" org-startup-folded t)
  2947. ("overview" org-startup-folded t)
  2948. ("nofold" org-startup-folded nil)
  2949. ("showall" org-startup-folded nil)
  2950. ("content" org-startup-folded content)
  2951. ("hidestars" org-hide-leading-stars t)
  2952. ("showstars" org-hide-leading-stars nil)
  2953. ("odd" org-odd-levels-only t)
  2954. ("oddeven" org-odd-levels-only nil)
  2955. ("align" org-startup-align-all-tables t)
  2956. ("noalign" org-startup-align-all-tables nil)
  2957. ("customtime" org-display-custom-times t)
  2958. ("logdone" org-log-done time)
  2959. ("lognotedone" org-log-done note)
  2960. ("nologdone" org-log-done nil)
  2961. ("lognoteclock-out" org-log-note-clock-out t)
  2962. ("nolognoteclock-out" org-log-note-clock-out nil)
  2963. ("logrepeat" org-log-repeat state)
  2964. ("lognoterepeat" org-log-repeat note)
  2965. ("nologrepeat" org-log-repeat nil)
  2966. ("fninline" org-footnote-define-inline t)
  2967. ("nofninline" org-footnote-define-inline nil)
  2968. ("fnlocal" org-footnote-section nil)
  2969. ("fnauto" org-footnote-auto-label t)
  2970. ("fnprompt" org-footnote-auto-label nil)
  2971. ("fnconfirm" org-footnote-auto-label confirm)
  2972. ("fnplain" org-footnote-auto-label plain)
  2973. ("constcgs" constants-unit-system cgs)
  2974. ("constSI" constants-unit-system SI)
  2975. ("noptag" org-tag-persistent-alist nil)
  2976. ("hideblocks" org-hide-block-startup t)
  2977. ("nohideblocks" org-hide-block-startup nil))
  2978. "Variable associated with STARTUP options for org-mode.
  2979. Each element is a list of three items: The startup options as written
  2980. in the #+STARTUP line, the corresponding variable, and the value to
  2981. set this variable to if the option is found. An optional forth element PUSH
  2982. means to push this value onto the list in the variable.")
  2983. (defun org-set-regexps-and-options ()
  2984. "Precompute regular expressions for current buffer."
  2985. (when (org-mode-p)
  2986. (org-set-local 'org-todo-kwd-alist nil)
  2987. (org-set-local 'org-todo-key-alist nil)
  2988. (org-set-local 'org-todo-key-trigger nil)
  2989. (org-set-local 'org-todo-keywords-1 nil)
  2990. (org-set-local 'org-done-keywords nil)
  2991. (org-set-local 'org-todo-heads nil)
  2992. (org-set-local 'org-todo-sets nil)
  2993. (org-set-local 'org-todo-log-states nil)
  2994. (org-set-local 'org-file-properties nil)
  2995. (org-set-local 'org-file-tags nil)
  2996. (let ((re (org-make-options-regexp
  2997. '("CATEGORY" "TODO" "COLUMNS"
  2998. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2999. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3000. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3001. (splitre "[ \t]+")
  3002. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3003. tail sep kws1 prio props ftags drawers
  3004. ext-setup-or-nil setup-contents (start 0))
  3005. (save-excursion
  3006. (save-restriction
  3007. (widen)
  3008. (goto-char (point-min))
  3009. (while (or (and ext-setup-or-nil
  3010. (string-match re ext-setup-or-nil start)
  3011. (setq start (match-end 0)))
  3012. (and (setq ext-setup-or-nil nil start 0)
  3013. (re-search-forward re nil t)))
  3014. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3015. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3016. (cond
  3017. ((equal key "CATEGORY")
  3018. (if (string-match "[ \t]+$" value)
  3019. (setq value (replace-match "" t t value)))
  3020. (setq cat value))
  3021. ((member key '("SEQ_TODO" "TODO"))
  3022. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3023. ((equal key "TYP_TODO")
  3024. (push (cons 'type (org-split-string value splitre)) kwds))
  3025. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3026. ;; general TODO-like setup
  3027. (push (cons (intern (downcase (match-string 1 key)))
  3028. (org-split-string value splitre)) kwds))
  3029. ((equal key "TAGS")
  3030. (setq tags (append tags (if tags '("\\n") nil)
  3031. (org-split-string value splitre))))
  3032. ((equal key "COLUMNS")
  3033. (org-set-local 'org-columns-default-format value))
  3034. ((equal key "LINK")
  3035. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3036. (push (cons (match-string 1 value)
  3037. (org-trim (match-string 2 value)))
  3038. links)))
  3039. ((equal key "PRIORITIES")
  3040. (setq prio (org-split-string value " +")))
  3041. ((equal key "PROPERTY")
  3042. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3043. (push (cons (match-string 1 value) (match-string 2 value))
  3044. props)))
  3045. ((equal key "FILETAGS")
  3046. (when (string-match "\\S-" value)
  3047. (setq ftags
  3048. (append
  3049. ftags
  3050. (apply 'append
  3051. (mapcar (lambda (x) (org-split-string x ":"))
  3052. (org-split-string value)))))))
  3053. ((equal key "DRAWERS")
  3054. (setq drawers (org-split-string value splitre)))
  3055. ((equal key "CONSTANTS")
  3056. (setq const (append const (org-split-string value splitre))))
  3057. ((equal key "STARTUP")
  3058. (let ((opts (org-split-string value splitre))
  3059. l var val)
  3060. (while (setq l (pop opts))
  3061. (when (setq l (assoc l org-startup-options))
  3062. (setq var (nth 1 l) val (nth 2 l))
  3063. (if (not (nth 3 l))
  3064. (set (make-local-variable var) val)
  3065. (if (not (listp (symbol-value var)))
  3066. (set (make-local-variable var) nil))
  3067. (set (make-local-variable var) (symbol-value var))
  3068. (add-to-list var val))))))
  3069. ((equal key "ARCHIVE")
  3070. (string-match " *$" value)
  3071. (setq arch (replace-match "" t t value))
  3072. (remove-text-properties 0 (length arch)
  3073. '(face t fontified t) arch))
  3074. ((equal key "SETUPFILE")
  3075. (setq setup-contents (org-file-contents
  3076. (expand-file-name
  3077. (org-remove-double-quotes value))
  3078. 'noerror))
  3079. (if (not ext-setup-or-nil)
  3080. (setq ext-setup-or-nil setup-contents start 0)
  3081. (setq ext-setup-or-nil
  3082. (concat (substring ext-setup-or-nil 0 start)
  3083. "\n" setup-contents "\n"
  3084. (substring ext-setup-or-nil start)))))
  3085. ))))
  3086. (when cat
  3087. (org-set-local 'org-category (intern cat))
  3088. (push (cons "CATEGORY" cat) props))
  3089. (when prio
  3090. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3091. (setq prio (mapcar 'string-to-char prio))
  3092. (org-set-local 'org-highest-priority (nth 0 prio))
  3093. (org-set-local 'org-lowest-priority (nth 1 prio))
  3094. (org-set-local 'org-default-priority (nth 2 prio)))
  3095. (and props (org-set-local 'org-file-properties (nreverse props)))
  3096. (and ftags (org-set-local 'org-file-tags
  3097. (mapcar 'org-add-prop-inherited ftags)))
  3098. (and drawers (org-set-local 'org-drawers drawers))
  3099. (and arch (org-set-local 'org-archive-location arch))
  3100. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3101. ;; Process the TODO keywords
  3102. (unless kwds
  3103. ;; Use the global values as if they had been given locally.
  3104. (setq kwds (default-value 'org-todo-keywords))
  3105. (if (stringp (car kwds))
  3106. (setq kwds (list (cons org-todo-interpretation
  3107. (default-value 'org-todo-keywords)))))
  3108. (setq kwds (reverse kwds)))
  3109. (setq kwds (nreverse kwds))
  3110. (let (inter kws kw)
  3111. (while (setq kws (pop kwds))
  3112. (let ((kws (or
  3113. (run-hook-with-args-until-success
  3114. 'org-todo-setup-filter-hook kws)
  3115. kws)))
  3116. (setq inter (pop kws) sep (member "|" kws)
  3117. kws0 (delete "|" (copy-sequence kws))
  3118. kwsa nil
  3119. kws1 (mapcar
  3120. (lambda (x)
  3121. ;; 1 2
  3122. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3123. (progn
  3124. (setq kw (match-string 1 x)
  3125. key (and (match-end 2) (match-string 2 x))
  3126. log (org-extract-log-state-settings x))
  3127. (push (cons kw (and key (string-to-char key))) kwsa)
  3128. (and log (push log org-todo-log-states))
  3129. kw)
  3130. (error "Invalid TODO keyword %s" x)))
  3131. kws0)
  3132. kwsa (if kwsa (append '((:startgroup))
  3133. (nreverse kwsa)
  3134. '((:endgroup))))
  3135. hw (car kws1)
  3136. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3137. tail (list inter hw (car dws) (org-last dws))))
  3138. (add-to-list 'org-todo-heads hw 'append)
  3139. (push kws1 org-todo-sets)
  3140. (setq org-done-keywords (append org-done-keywords dws nil))
  3141. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3142. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3143. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3144. (setq org-todo-sets (nreverse org-todo-sets)
  3145. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3146. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3147. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3148. ;; Process the constants
  3149. (when const
  3150. (let (e cst)
  3151. (while (setq e (pop const))
  3152. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3153. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3154. (setq org-table-formula-constants-local cst)))
  3155. ;; Process the tags.
  3156. (when tags
  3157. (let (e tgs)
  3158. (while (setq e (pop tags))
  3159. (cond
  3160. ((equal e "{") (push '(:startgroup) tgs))
  3161. ((equal e "}") (push '(:endgroup) tgs))
  3162. ((equal e "\\n") (push '(:newline) tgs))
  3163. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3164. (push (cons (match-string 1 e)
  3165. (string-to-char (match-string 2 e)))
  3166. tgs))
  3167. (t (push (list e) tgs))))
  3168. (org-set-local 'org-tag-alist nil)
  3169. (while (setq e (pop tgs))
  3170. (or (and (stringp (car e))
  3171. (assoc (car e) org-tag-alist))
  3172. (push e org-tag-alist)))))
  3173. ;; Compute the regular expressions and other local variables
  3174. (if (not org-done-keywords)
  3175. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3176. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3177. (length org-scheduled-string)
  3178. (length org-clock-string)
  3179. (length org-closed-string)))
  3180. org-drawer-regexp
  3181. (concat "^[ \t]*:\\("
  3182. (mapconcat 'regexp-quote org-drawers "\\|")
  3183. "\\):[ \t]*$")
  3184. org-not-done-keywords
  3185. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3186. org-todo-regexp
  3187. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3188. "\\|") "\\)\\>")
  3189. org-not-done-regexp
  3190. (concat "\\<\\("
  3191. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3192. "\\)\\>")
  3193. org-not-done-heading-regexp
  3194. (concat "^\\(\\*+\\)[ \t]+\\("
  3195. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3196. "\\)\\>")
  3197. org-todo-line-regexp
  3198. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3199. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3200. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3201. org-complex-heading-regexp
  3202. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3203. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3204. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3205. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3206. org-nl-done-regexp
  3207. (concat "\n\\*+[ \t]+"
  3208. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3209. "\\)" "\\>")
  3210. org-todo-line-tags-regexp
  3211. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3212. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3213. (org-re
  3214. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3215. org-looking-at-done-regexp
  3216. (concat "^" "\\(?:"
  3217. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3218. "\\>")
  3219. org-deadline-regexp (concat "\\<" org-deadline-string)
  3220. org-deadline-time-regexp
  3221. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3222. org-deadline-line-regexp
  3223. (concat "\\<\\(" org-deadline-string "\\).*")
  3224. org-scheduled-regexp
  3225. (concat "\\<" org-scheduled-string)
  3226. org-scheduled-time-regexp
  3227. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3228. org-closed-time-regexp
  3229. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3230. org-keyword-time-regexp
  3231. (concat "\\<\\(" org-scheduled-string
  3232. "\\|" org-deadline-string
  3233. "\\|" org-closed-string
  3234. "\\|" org-clock-string "\\)"
  3235. " *[[<]\\([^]>]+\\)[]>]")
  3236. org-keyword-time-not-clock-regexp
  3237. (concat "\\<\\(" org-scheduled-string
  3238. "\\|" org-deadline-string
  3239. "\\|" org-closed-string
  3240. "\\)"
  3241. " *[[<]\\([^]>]+\\)[]>]")
  3242. org-maybe-keyword-time-regexp
  3243. (concat "\\(\\<\\(" org-scheduled-string
  3244. "\\|" org-deadline-string
  3245. "\\|" org-closed-string
  3246. "\\|" org-clock-string "\\)\\)?"
  3247. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3248. org-planning-or-clock-line-re
  3249. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3250. "\\|" org-deadline-string
  3251. "\\|" org-closed-string "\\|" org-clock-string
  3252. "\\)\\>\\)")
  3253. )
  3254. (org-compute-latex-and-specials-regexp)
  3255. (org-set-font-lock-defaults))))
  3256. (defun org-file-contents (file &optional noerror)
  3257. "Return the contents of FILE, as a string."
  3258. (if (or (not file)
  3259. (not (file-readable-p file)))
  3260. (if noerror
  3261. (progn
  3262. (message "Cannot read file %s" file)
  3263. (ding) (sit-for 2)
  3264. "")
  3265. (error "Cannot read file %s" file))
  3266. (with-temp-buffer
  3267. (insert-file-contents file)
  3268. (buffer-string))))
  3269. (defun org-extract-log-state-settings (x)
  3270. "Extract the log state setting from a TODO keyword string.
  3271. This will extract info from a string like \"WAIT(w@/!)\"."
  3272. (let (kw key log1 log2)
  3273. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3274. (setq kw (match-string 1 x)
  3275. key (and (match-end 2) (match-string 2 x))
  3276. log1 (and (match-end 3) (match-string 3 x))
  3277. log2 (and (match-end 4) (match-string 4 x)))
  3278. (and (or log1 log2)
  3279. (list kw
  3280. (and log1 (if (equal log1 "!") 'time 'note))
  3281. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3282. (defun org-remove-keyword-keys (list)
  3283. "Remove a pair of parenthesis at the end of each string in LIST."
  3284. (mapcar (lambda (x)
  3285. (if (string-match "(.*)$" x)
  3286. (substring x 0 (match-beginning 0))
  3287. x))
  3288. list))
  3289. ;; FIXME: this could be done much better, using second characters etc.
  3290. (defun org-assign-fast-keys (alist)
  3291. "Assign fast keys to a keyword-key alist.
  3292. Respect keys that are already there."
  3293. (let (new e k c c1 c2 (char ?a))
  3294. (while (setq e (pop alist))
  3295. (cond
  3296. ((equal e '(:startgroup)) (push e new))
  3297. ((equal e '(:endgroup)) (push e new))
  3298. ((equal e '(:newline)) (push e new))
  3299. (t
  3300. (setq k (car e) c2 nil)
  3301. (if (cdr e)
  3302. (setq c (cdr e))
  3303. ;; automatically assign a character.
  3304. (setq c1 (string-to-char
  3305. (downcase (substring
  3306. k (if (= (string-to-char k) ?@) 1 0)))))
  3307. (if (or (rassoc c1 new) (rassoc c1 alist))
  3308. (while (or (rassoc char new) (rassoc char alist))
  3309. (setq char (1+ char)))
  3310. (setq c2 c1))
  3311. (setq c (or c2 char)))
  3312. (push (cons k c) new))))
  3313. (nreverse new)))
  3314. ;;; Some variables used in various places
  3315. (defvar org-window-configuration nil
  3316. "Used in various places to store a window configuration.")
  3317. (defvar org-finish-function nil
  3318. "Function to be called when `C-c C-c' is used.
  3319. This is for getting out of special buffers like remember.")
  3320. ;; FIXME: Occasionally check by commenting these, to make sure
  3321. ;; no other functions uses these, forgetting to let-bind them.
  3322. (defvar entry)
  3323. (defvar last-state)
  3324. (defvar date)
  3325. ;; Defined somewhere in this file, but used before definition.
  3326. (defvar org-html-entities)
  3327. (defvar org-struct-menu)
  3328. (defvar org-org-menu)
  3329. (defvar org-tbl-menu)
  3330. (defvar org-agenda-keymap)
  3331. ;;;; Define the Org-mode
  3332. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3333. (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."))
  3334. ;; We use a before-change function to check if a table might need
  3335. ;; an update.
  3336. (defvar org-table-may-need-update t
  3337. "Indicates that a table might need an update.
  3338. This variable is set by `org-before-change-function'.
  3339. `org-table-align' sets it back to nil.")
  3340. (defun org-before-change-function (beg end)
  3341. "Every change indicates that a table might need an update."
  3342. (setq org-table-may-need-update t))
  3343. (defvar org-mode-map)
  3344. (defvar org-mode-hook nil
  3345. "Mode hook for Org-mode, run after the mode was turned on.")
  3346. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3347. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3348. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3349. (defvar org-table-buffer-is-an nil)
  3350. (defconst org-outline-regexp "\\*+ ")
  3351. ;;;###autoload
  3352. (define-derived-mode org-mode outline-mode "Org"
  3353. "Outline-based notes management and organizer, alias
  3354. \"Carsten's outline-mode for keeping track of everything.\"
  3355. Org-mode develops organizational tasks around a NOTES file which
  3356. contains information about projects as plain text. Org-mode is
  3357. implemented on top of outline-mode, which is ideal to keep the content
  3358. of large files well structured. It supports ToDo items, deadlines and
  3359. time stamps, which magically appear in the diary listing of the Emacs
  3360. calendar. Tables are easily created with a built-in table editor.
  3361. Plain text URL-like links connect to websites, emails (VM), Usenet
  3362. messages (Gnus), BBDB entries, and any files related to the project.
  3363. For printing and sharing of notes, an Org-mode file (or a part of it)
  3364. can be exported as a structured ASCII or HTML file.
  3365. The following commands are available:
  3366. \\{org-mode-map}"
  3367. ;; Get rid of Outline menus, they are not needed
  3368. ;; Need to do this here because define-derived-mode sets up
  3369. ;; the keymap so late. Still, it is a waste to call this each time
  3370. ;; we switch another buffer into org-mode.
  3371. (if (featurep 'xemacs)
  3372. (when (boundp 'outline-mode-menu-heading)
  3373. ;; Assume this is Greg's port, it used easymenu
  3374. (easy-menu-remove outline-mode-menu-heading)
  3375. (easy-menu-remove outline-mode-menu-show)
  3376. (easy-menu-remove outline-mode-menu-hide))
  3377. (define-key org-mode-map [menu-bar headings] 'undefined)
  3378. (define-key org-mode-map [menu-bar hide] 'undefined)
  3379. (define-key org-mode-map [menu-bar show] 'undefined))
  3380. (org-load-modules-maybe)
  3381. (easy-menu-add org-org-menu)
  3382. (easy-menu-add org-tbl-menu)
  3383. (org-install-agenda-files-menu)
  3384. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3385. (org-add-to-invisibility-spec '(org-cwidth))
  3386. (org-add-to-invisibility-spec '(org-hide-block . t))
  3387. (when (featurep 'xemacs)
  3388. (org-set-local 'line-move-ignore-invisible t))
  3389. (org-set-local 'outline-regexp org-outline-regexp)
  3390. (org-set-local 'outline-level 'org-outline-level)
  3391. (when (and org-ellipsis
  3392. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3393. (fboundp 'make-glyph-code))
  3394. (unless org-display-table
  3395. (setq org-display-table (make-display-table)))
  3396. (set-display-table-slot
  3397. org-display-table 4
  3398. (vconcat (mapcar
  3399. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3400. org-ellipsis)))
  3401. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3402. (setq buffer-display-table org-display-table))
  3403. (org-set-regexps-and-options)
  3404. (when (and org-tag-faces (not org-tags-special-faces-re))
  3405. ;; tag faces set outside customize.... force initialization.
  3406. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3407. ;; Calc embedded
  3408. (org-set-local 'calc-embedded-open-mode "# ")
  3409. (modify-syntax-entry ?# "<")
  3410. (modify-syntax-entry ?@ "w")
  3411. (if org-startup-truncated (setq truncate-lines t))
  3412. (org-set-local 'font-lock-unfontify-region-function
  3413. 'org-unfontify-region)
  3414. ;; Activate before-change-function
  3415. (org-set-local 'org-table-may-need-update t)
  3416. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3417. 'local)
  3418. ;; Check for running clock before killing a buffer
  3419. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3420. ;; Paragraphs and auto-filling
  3421. (org-set-autofill-regexps)
  3422. (setq indent-line-function 'org-indent-line-function)
  3423. (org-update-radio-target-regexp)
  3424. ;; Make sure dependence stuff works reliably, even for users who set it
  3425. ;; too late :-(
  3426. (if org-enforce-todo-dependencies
  3427. (add-hook 'org-blocker-hook
  3428. 'org-block-todo-from-children-or-siblings-or-parent)
  3429. (remove-hook 'org-blocker-hook
  3430. 'org-block-todo-from-children-or-siblings-or-parent))
  3431. (if org-enforce-todo-checkbox-dependencies
  3432. (add-hook 'org-blocker-hook
  3433. 'org-block-todo-from-checkboxes)
  3434. (remove-hook 'org-blocker-hook
  3435. 'org-block-todo-from-checkboxes))
  3436. ;; Comment characters
  3437. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3438. (org-set-local 'comment-padding " ")
  3439. ;; Align options lines
  3440. (org-set-local
  3441. 'align-mode-rules-list
  3442. '((org-in-buffer-settings
  3443. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3444. (modes . '(org-mode)))))
  3445. ;; Imenu
  3446. (org-set-local 'imenu-create-index-function
  3447. 'org-imenu-get-tree)
  3448. ;; Make isearch reveal context
  3449. (if (or (featurep 'xemacs)
  3450. (not (boundp 'outline-isearch-open-invisible-function)))
  3451. ;; Emacs 21 and XEmacs make use of the hook
  3452. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3453. ;; Emacs 22 deals with this through a special variable
  3454. (org-set-local 'outline-isearch-open-invisible-function
  3455. (lambda (&rest ignore) (org-show-context 'isearch))))
  3456. ;; If empty file that did not turn on org-mode automatically, make it to.
  3457. (if (and org-insert-mode-line-in-empty-file
  3458. (interactive-p)
  3459. (= (point-min) (point-max)))
  3460. (insert "# -*- mode: org -*-\n\n"))
  3461. (unless org-inhibit-startup
  3462. (when org-startup-align-all-tables
  3463. (let ((bmp (buffer-modified-p)))
  3464. (org-table-map-tables 'org-table-align)
  3465. (set-buffer-modified-p bmp)))
  3466. (org-set-startup-visibility)))
  3467. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3468. (defun org-current-time ()
  3469. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3470. (if (> (car org-time-stamp-rounding-minutes) 1)
  3471. (let ((r (car org-time-stamp-rounding-minutes))
  3472. (time (decode-time)))
  3473. (apply 'encode-time
  3474. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3475. (nthcdr 2 time))))
  3476. (current-time)))
  3477. ;;;; Font-Lock stuff, including the activators
  3478. (defvar org-mouse-map (make-sparse-keymap))
  3479. (org-defkey org-mouse-map
  3480. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3481. (org-defkey org-mouse-map
  3482. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3483. (when org-mouse-1-follows-link
  3484. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3485. (when org-tab-follows-link
  3486. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3487. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3488. (require 'font-lock)
  3489. (defconst org-non-link-chars "]\t\n\r<>")
  3490. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3491. "shell" "elisp"))
  3492. (defvar org-link-types-re nil
  3493. "Matches a link that has a url-like prefix like \"http:\"")
  3494. (defvar org-link-re-with-space nil
  3495. "Matches a link with spaces, optional angular brackets around it.")
  3496. (defvar org-link-re-with-space2 nil
  3497. "Matches a link with spaces, optional angular brackets around it.")
  3498. (defvar org-link-re-with-space3 nil
  3499. "Matches a link with spaces, only for internal part in bracket links.")
  3500. (defvar org-angle-link-re nil
  3501. "Matches link with angular brackets, spaces are allowed.")
  3502. (defvar org-plain-link-re nil
  3503. "Matches plain link, without spaces.")
  3504. (defvar org-bracket-link-regexp nil
  3505. "Matches a link in double brackets.")
  3506. (defvar org-bracket-link-analytic-regexp nil
  3507. "Regular expression used to analyze links.
  3508. Here is what the match groups contain after a match:
  3509. 1: http:
  3510. 2: http
  3511. 3: path
  3512. 4: [desc]
  3513. 5: desc")
  3514. (defvar org-bracket-link-analytic-regexp++ nil
  3515. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3516. (defvar org-any-link-re nil
  3517. "Regular expression matching any link.")
  3518. (defun org-make-link-regexps ()
  3519. "Update the link regular expressions.
  3520. This should be called after the variable `org-link-types' has changed."
  3521. (setq org-link-types-re
  3522. (concat
  3523. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3524. org-link-re-with-space
  3525. (concat
  3526. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3527. "\\([^" org-non-link-chars " ]"
  3528. "[^" org-non-link-chars "]*"
  3529. "[^" org-non-link-chars " ]\\)>?")
  3530. org-link-re-with-space2
  3531. (concat
  3532. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3533. "\\([^" org-non-link-chars " ]"
  3534. "[^\t\n\r]*"
  3535. "[^" org-non-link-chars " ]\\)>?")
  3536. org-link-re-with-space3
  3537. (concat
  3538. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3539. "\\([^" org-non-link-chars " ]"
  3540. "[^\t\n\r]*\\)")
  3541. org-angle-link-re
  3542. (concat
  3543. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3544. "\\([^" org-non-link-chars " ]"
  3545. "[^" org-non-link-chars "]*"
  3546. "\\)>")
  3547. org-plain-link-re
  3548. (concat
  3549. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3550. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3551. org-bracket-link-regexp
  3552. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3553. org-bracket-link-analytic-regexp
  3554. (concat
  3555. "\\[\\["
  3556. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3557. "\\([^]]+\\)"
  3558. "\\]"
  3559. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3560. "\\]")
  3561. org-bracket-link-analytic-regexp++
  3562. (concat
  3563. "\\[\\["
  3564. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3565. "\\([^]]+\\)"
  3566. "\\]"
  3567. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3568. "\\]")
  3569. org-any-link-re
  3570. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3571. org-angle-link-re "\\)\\|\\("
  3572. org-plain-link-re "\\)")))
  3573. (org-make-link-regexps)
  3574. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3575. "Regular expression for fast time stamp matching.")
  3576. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3577. "Regular expression for fast time stamp matching.")
  3578. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3579. "Regular expression matching time strings for analysis.
  3580. This one does not require the space after the date, so it can be used
  3581. on a string that terminates immediately after the date.")
  3582. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3583. "Regular expression matching time strings for analysis.")
  3584. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3585. "Regular expression matching time stamps, with groups.")
  3586. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3587. "Regular expression matching time stamps (also [..]), with groups.")
  3588. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3589. "Regular expression matching a time stamp range.")
  3590. (defconst org-tr-regexp-both
  3591. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3592. "Regular expression matching a time stamp range.")
  3593. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3594. org-ts-regexp "\\)?")
  3595. "Regular expression matching a time stamp or time stamp range.")
  3596. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3597. org-ts-regexp-both "\\)?")
  3598. "Regular expression matching a time stamp or time stamp range.
  3599. The time stamps may be either active or inactive.")
  3600. (defvar org-emph-face nil)
  3601. (defun org-do-emphasis-faces (limit)
  3602. "Run through the buffer and add overlays to links."
  3603. (let (rtn a)
  3604. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3605. (if (not (= (char-after (match-beginning 3))
  3606. (char-after (match-beginning 4))))
  3607. (progn
  3608. (setq rtn t)
  3609. (setq a (assoc (match-string 3) org-emphasis-alist))
  3610. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3611. 'face
  3612. (nth 1 a))
  3613. (and (nth 4 a)
  3614. (org-remove-flyspell-overlays-in
  3615. (match-beginning 0) (match-end 0)))
  3616. (add-text-properties (match-beginning 2) (match-end 2)
  3617. '(font-lock-multiline t))
  3618. (when org-hide-emphasis-markers
  3619. (add-text-properties (match-end 4) (match-beginning 5)
  3620. '(invisible org-link))
  3621. (add-text-properties (match-beginning 3) (match-end 3)
  3622. '(invisible org-link)))))
  3623. (backward-char 1))
  3624. rtn))
  3625. (defun org-emphasize (&optional char)
  3626. "Insert or change an emphasis, i.e. a font like bold or italic.
  3627. If there is an active region, change that region to a new emphasis.
  3628. If there is no region, just insert the marker characters and position
  3629. the cursor between them.
  3630. CHAR should be either the marker character, or the first character of the
  3631. HTML tag associated with that emphasis. If CHAR is a space, the means
  3632. to remove the emphasis of the selected region.
  3633. If char is not given (for example in an interactive call) it
  3634. will be prompted for."
  3635. (interactive)
  3636. (let ((eal org-emphasis-alist) e det
  3637. (erc org-emphasis-regexp-components)
  3638. (prompt "")
  3639. (string "") beg end move tag c s)
  3640. (if (org-region-active-p)
  3641. (setq beg (region-beginning) end (region-end)
  3642. string (buffer-substring beg end))
  3643. (setq move t))
  3644. (while (setq e (pop eal))
  3645. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3646. c (aref tag 0))
  3647. (push (cons c (string-to-char (car e))) det)
  3648. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3649. (substring tag 1)))))
  3650. (setq det (nreverse det))
  3651. (unless char
  3652. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3653. (setq char (read-char-exclusive)))
  3654. (setq char (or (cdr (assoc char det)) char))
  3655. (if (equal char ?\ )
  3656. (setq s "" move nil)
  3657. (unless (assoc (char-to-string char) org-emphasis-alist)
  3658. (error "No such emphasis marker: \"%c\"" char))
  3659. (setq s (char-to-string char)))
  3660. (while (and (> (length string) 1)
  3661. (equal (substring string 0 1) (substring string -1))
  3662. (assoc (substring string 0 1) org-emphasis-alist))
  3663. (setq string (substring string 1 -1)))
  3664. (setq string (concat s string s))
  3665. (if beg (delete-region beg end))
  3666. (unless (or (bolp)
  3667. (string-match (concat "[" (nth 0 erc) "\n]")
  3668. (char-to-string (char-before (point)))))
  3669. (insert " "))
  3670. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3671. (char-to-string (char-after (point))))
  3672. (insert " ") (backward-char 1))
  3673. (insert string)
  3674. (and move (backward-char 1))))
  3675. (defconst org-nonsticky-props
  3676. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3677. (defsubst org-rear-nonsticky-at (pos)
  3678. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3679. (defun org-activate-plain-links (limit)
  3680. "Run through the buffer and add overlays to links."
  3681. (catch 'exit
  3682. (let (f)
  3683. (while (re-search-forward org-plain-link-re limit t)
  3684. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3685. (setq f (get-text-property (match-beginning 0) 'face))
  3686. (if (or (eq f 'org-tag)
  3687. (and (listp f) (memq 'org-tag f)))
  3688. nil
  3689. (add-text-properties (match-beginning 0) (match-end 0)
  3690. (list 'mouse-face 'highlight
  3691. 'keymap org-mouse-map))
  3692. (org-rear-nonsticky-at (match-end 0))
  3693. (throw 'exit t))))))
  3694. (defun org-activate-code (limit)
  3695. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3696. (progn
  3697. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3698. (remove-text-properties (match-beginning 0) (match-end 0)
  3699. '(display t invisible t intangible t))
  3700. t)))
  3701. (defun org-fontify-meta-lines-and-blocks (limit)
  3702. "Fontify #+ lines and blocks, in the correct ways."
  3703. (let ((case-fold-search t))
  3704. (if (re-search-forward
  3705. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3706. limit t)
  3707. (let ((beg (match-beginning 0))
  3708. (beg1 (line-beginning-position 2))
  3709. (dc1 (downcase (match-string 2)))
  3710. (dc3 (downcase (match-string 3)))
  3711. end end1 quoting)
  3712. (cond
  3713. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3714. ;; a single line of backend-specific content
  3715. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3716. (remove-text-properties (match-beginning 0) (match-end 0)
  3717. '(display t invisible t intangible t))
  3718. (add-text-properties (match-beginning 1) (match-end 3)
  3719. '(font-lock-fontified t face org-meta-line))
  3720. (add-text-properties (match-beginning 6) (match-end 6)
  3721. '(font-lock-fontified t face org-block))
  3722. t)
  3723. ((and (match-end 4) (equal dc3 "begin"))
  3724. ;; Truely a block
  3725. (setq quoting (member (downcase (match-string 5))
  3726. org-protecting-blocks))
  3727. (when (re-search-forward
  3728. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3729. nil t) ;; on purpose, we look further than LIMIT
  3730. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3731. (when quoting
  3732. (remove-text-properties beg end
  3733. '(display t invisible t intangible t)))
  3734. (add-text-properties
  3735. beg end
  3736. '(font-lock-fontified t font-lock-multiline t))
  3737. (add-text-properties beg beg1 '(face org-meta-line))
  3738. (add-text-properties end1 end '(face org-meta-line))
  3739. (when quoting
  3740. (add-text-properties beg1 end1 '(face org-block)))
  3741. t))
  3742. ((not (member (char-after beg) '(?\ ?\t)))
  3743. ;; just any other in-buffer setting, but not indented
  3744. (add-text-properties
  3745. beg (match-end 0)
  3746. '(font-lock-fontified t face org-meta-line))
  3747. t)
  3748. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:"))
  3749. (and (match-end 4) (equal dc3 "attr")))
  3750. (add-text-properties
  3751. beg (match-end 0)
  3752. '(font-lock-fontified t face org-meta-line))
  3753. t)
  3754. (t nil))))))
  3755. (defun org-activate-angle-links (limit)
  3756. "Run through the buffer and add overlays to links."
  3757. (if (re-search-forward org-angle-link-re limit t)
  3758. (progn
  3759. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3760. (add-text-properties (match-beginning 0) (match-end 0)
  3761. (list 'mouse-face 'highlight
  3762. 'keymap org-mouse-map))
  3763. (org-rear-nonsticky-at (match-end 0))
  3764. t)))
  3765. (defun org-activate-footnote-links (limit)
  3766. "Run through the buffer and add overlays to links."
  3767. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3768. limit t)
  3769. (progn
  3770. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3771. (add-text-properties (match-beginning 2) (match-end 2)
  3772. (list 'mouse-face 'highlight
  3773. 'keymap org-mouse-map
  3774. 'help-echo
  3775. (if (= (point-at-bol) (match-beginning 2))
  3776. "Footnote definition"
  3777. "Footnote reference")
  3778. ))
  3779. (org-rear-nonsticky-at (match-end 2))
  3780. t)))
  3781. (defun org-activate-bracket-links (limit)
  3782. "Run through the buffer and add overlays to bracketed links."
  3783. (if (re-search-forward org-bracket-link-regexp limit t)
  3784. (let* ((help (concat "LINK: "
  3785. (org-match-string-no-properties 1)))
  3786. ;; FIXME: above we should remove the escapes.
  3787. ;; but that requires another match, protecting match data,
  3788. ;; a lot of overhead for font-lock.
  3789. (ip (org-maybe-intangible
  3790. (list 'invisible 'org-link
  3791. 'keymap org-mouse-map 'mouse-face 'highlight
  3792. 'font-lock-multiline t 'help-echo help)))
  3793. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3794. 'font-lock-multiline t 'help-echo help)))
  3795. ;; We need to remove the invisible property here. Table narrowing
  3796. ;; may have made some of this invisible.
  3797. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3798. (remove-text-properties (match-beginning 0) (match-end 0)
  3799. '(invisible nil))
  3800. (if (match-end 3)
  3801. (progn
  3802. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3803. (org-rear-nonsticky-at (match-beginning 3))
  3804. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3805. (org-rear-nonsticky-at (match-end 3))
  3806. (add-text-properties (match-end 3) (match-end 0) ip)
  3807. (org-rear-nonsticky-at (match-end 0)))
  3808. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3809. (org-rear-nonsticky-at (match-beginning 1))
  3810. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3811. (org-rear-nonsticky-at (match-end 1))
  3812. (add-text-properties (match-end 1) (match-end 0) ip)
  3813. (org-rear-nonsticky-at (match-end 0)))
  3814. t)))
  3815. (defun org-activate-dates (limit)
  3816. "Run through the buffer and add overlays to dates."
  3817. (if (re-search-forward org-tsr-regexp-both limit t)
  3818. (progn
  3819. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3820. (add-text-properties (match-beginning 0) (match-end 0)
  3821. (list 'mouse-face 'highlight
  3822. 'keymap org-mouse-map))
  3823. (org-rear-nonsticky-at (match-end 0))
  3824. (when org-display-custom-times
  3825. (if (match-end 3)
  3826. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3827. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3828. t)))
  3829. (defvar org-target-link-regexp nil
  3830. "Regular expression matching radio targets in plain text.")
  3831. (make-variable-buffer-local 'org-target-link-regexp)
  3832. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3833. "Regular expression matching a link target.")
  3834. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3835. "Regular expression matching a radio target.")
  3836. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3837. "Regular expression matching any target.")
  3838. (defun org-activate-target-links (limit)
  3839. "Run through the buffer and add overlays to target matches."
  3840. (when org-target-link-regexp
  3841. (let ((case-fold-search t))
  3842. (if (re-search-forward org-target-link-regexp limit t)
  3843. (progn
  3844. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3845. (add-text-properties (match-beginning 0) (match-end 0)
  3846. (list 'mouse-face 'highlight
  3847. 'keymap org-mouse-map
  3848. 'help-echo "Radio target link"
  3849. 'org-linked-text t))
  3850. (org-rear-nonsticky-at (match-end 0))
  3851. t)))))
  3852. (defun org-update-radio-target-regexp ()
  3853. "Find all radio targets in this file and update the regular expression."
  3854. (interactive)
  3855. (when (memq 'radio org-activate-links)
  3856. (setq org-target-link-regexp
  3857. (org-make-target-link-regexp (org-all-targets 'radio)))
  3858. (org-restart-font-lock)))
  3859. (defun org-hide-wide-columns (limit)
  3860. (let (s e)
  3861. (setq s (text-property-any (point) (or limit (point-max))
  3862. 'org-cwidth t))
  3863. (when s
  3864. (setq e (next-single-property-change s 'org-cwidth))
  3865. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3866. (goto-char e)
  3867. t)))
  3868. (defvar org-latex-and-specials-regexp nil
  3869. "Regular expression for highlighting export special stuff.")
  3870. (defvar org-match-substring-regexp)
  3871. (defvar org-match-substring-with-braces-regexp)
  3872. (defvar org-export-html-special-string-regexps)
  3873. (defun org-compute-latex-and-specials-regexp ()
  3874. "Compute regular expression for stuff treated specially by exporters."
  3875. (if (not org-highlight-latex-fragments-and-specials)
  3876. (org-set-local 'org-latex-and-specials-regexp nil)
  3877. (require 'org-exp)
  3878. (let*
  3879. ((matchers (plist-get org-format-latex-options :matchers))
  3880. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3881. org-latex-regexps)))
  3882. (options (org-combine-plists (org-default-export-plist)
  3883. (org-infile-export-plist)))
  3884. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3885. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3886. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3887. (org-export-html-expand (plist-get options :expand-quoted-html))
  3888. (org-export-with-special-strings (plist-get options :special-strings))
  3889. (re-sub
  3890. (cond
  3891. ((equal org-export-with-sub-superscripts '{})
  3892. (list org-match-substring-with-braces-regexp))
  3893. (org-export-with-sub-superscripts
  3894. (list org-match-substring-regexp))
  3895. (t nil)))
  3896. (re-latex
  3897. (if org-export-with-LaTeX-fragments
  3898. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3899. (re-macros
  3900. (if org-export-with-TeX-macros
  3901. (list (concat "\\\\"
  3902. (regexp-opt
  3903. (append (mapcar 'car org-html-entities)
  3904. (if (boundp 'org-latex-entities)
  3905. (mapcar (lambda (x)
  3906. (or (car-safe x) x))
  3907. org-latex-entities)
  3908. nil))
  3909. 'words))) ; FIXME
  3910. ))
  3911. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3912. (re-special (if org-export-with-special-strings
  3913. (mapcar (lambda (x) (car x))
  3914. org-export-html-special-string-regexps)))
  3915. (re-rest
  3916. (delq nil
  3917. (list
  3918. (if org-export-html-expand "@<[^>\n]+>")
  3919. ))))
  3920. (org-set-local
  3921. 'org-latex-and-specials-regexp
  3922. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3923. re-rest) "\\|")))))
  3924. (defun org-do-latex-and-special-faces (limit)
  3925. "Run through the buffer and add overlays to links."
  3926. (when org-latex-and-specials-regexp
  3927. (let (rtn d)
  3928. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3929. limit t))
  3930. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3931. 'face))
  3932. '(org-code org-verbatim underline)))
  3933. (progn
  3934. (setq rtn t
  3935. d (cond ((member (char-after (1+ (match-beginning 0)))
  3936. '(?_ ?^)) 1)
  3937. (t 0)))
  3938. (font-lock-prepend-text-property
  3939. (+ d (match-beginning 0)) (match-end 0)
  3940. 'face 'org-latex-and-export-specials)
  3941. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3942. '(font-lock-multiline t)))))
  3943. rtn)))
  3944. (defun org-restart-font-lock ()
  3945. "Restart font-lock-mode, to force refontification."
  3946. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3947. (font-lock-mode -1)
  3948. (font-lock-mode 1)))
  3949. (defun org-all-targets (&optional radio)
  3950. "Return a list of all targets in this file.
  3951. With optional argument RADIO, only find radio targets."
  3952. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3953. rtn)
  3954. (save-excursion
  3955. (goto-char (point-min))
  3956. (while (re-search-forward re nil t)
  3957. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3958. rtn)))
  3959. (defun org-make-target-link-regexp (targets)
  3960. "Make regular expression matching all strings in TARGETS.
  3961. The regular expression finds the targets also if there is a line break
  3962. between words."
  3963. (and targets
  3964. (concat
  3965. "\\<\\("
  3966. (mapconcat
  3967. (lambda (x)
  3968. (while (string-match " +" x)
  3969. (setq x (replace-match "\\s-+" t t x)))
  3970. x)
  3971. targets
  3972. "\\|")
  3973. "\\)\\>")))
  3974. (defun org-activate-tags (limit)
  3975. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3976. (progn
  3977. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3978. (add-text-properties (match-beginning 1) (match-end 1)
  3979. (list 'mouse-face 'highlight
  3980. 'keymap org-mouse-map))
  3981. (org-rear-nonsticky-at (match-end 1))
  3982. t)))
  3983. (defun org-outline-level ()
  3984. (save-excursion
  3985. (looking-at outline-regexp)
  3986. (if (match-beginning 1)
  3987. (+ (org-get-string-indentation (match-string 1)) 1000)
  3988. (1- (- (match-end 0) (match-beginning 0))))))
  3989. (defvar org-font-lock-keywords nil)
  3990. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3991. "Regular expression matching a property line.")
  3992. (defvar org-font-lock-hook nil
  3993. "Functions to be called for special font lock stuff.")
  3994. (defun org-font-lock-hook (limit)
  3995. (run-hook-with-args 'org-font-lock-hook limit))
  3996. (defun org-set-font-lock-defaults ()
  3997. (let* ((em org-fontify-emphasized-text)
  3998. (lk org-activate-links)
  3999. (org-font-lock-extra-keywords
  4000. (list
  4001. ;; Call the hook
  4002. '(org-font-lock-hook)
  4003. ;; Headlines
  4004. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4005. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4006. ;; Table lines
  4007. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4008. (1 'org-table t))
  4009. ;; Table internals
  4010. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4011. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4012. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4013. ;; Drawers
  4014. (list org-drawer-regexp '(0 'org-special-keyword t))
  4015. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4016. ;; Properties
  4017. (list org-property-re
  4018. '(1 'org-special-keyword t)
  4019. '(3 'org-property-value t))
  4020. (if org-format-transports-properties-p
  4021. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4022. ;; Links
  4023. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4024. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4025. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4026. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4027. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4028. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4029. (if (memq 'footnote lk) '(org-activate-footnote-links
  4030. (2 'org-footnote t)))
  4031. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4032. '(org-hide-wide-columns (0 nil append))
  4033. ;; TODO lines
  4034. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4035. '(1 (org-get-todo-face 1) t))
  4036. ;; DONE
  4037. (if org-fontify-done-headline
  4038. (list (concat "^[*]+ +\\<\\("
  4039. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4040. "\\)\\(.*\\)")
  4041. '(2 'org-headline-done t))
  4042. nil)
  4043. ;; Priorities
  4044. '(org-font-lock-add-priority-faces)
  4045. ;; Tags
  4046. '(org-font-lock-add-tag-faces)
  4047. ;; Special keywords
  4048. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4049. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4050. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4051. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4052. ;; Emphasis
  4053. (if em
  4054. (if (featurep 'xemacs)
  4055. '(org-do-emphasis-faces (0 nil append))
  4056. '(org-do-emphasis-faces)))
  4057. ;; Checkboxes
  4058. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4059. 2 'org-checkbox prepend)
  4060. (if org-provide-checkbox-statistics
  4061. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4062. (0 (org-get-checkbox-statistics-face) t)))
  4063. ;; Description list items
  4064. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4065. 2 'bold prepend)
  4066. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4067. '(1 'org-archived prepend))
  4068. ;; Specials
  4069. '(org-do-latex-and-special-faces)
  4070. ;; Code
  4071. '(org-activate-code (1 'org-code t))
  4072. ;; COMMENT
  4073. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4074. "\\|" org-quote-string "\\)\\>")
  4075. '(1 'org-special-keyword t))
  4076. '("^#.*" (0 'font-lock-comment-face t))
  4077. ;; Blocks and meta lines
  4078. '(org-fontify-meta-lines-and-blocks)
  4079. )))
  4080. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4081. ;; Now set the full font-lock-keywords
  4082. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4083. (org-set-local 'font-lock-defaults
  4084. '(org-font-lock-keywords t nil nil backward-paragraph))
  4085. (kill-local-variable 'font-lock-keywords) nil))
  4086. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4087. "Fontify string S like in Org-mode"
  4088. (with-temp-buffer
  4089. (insert s)
  4090. (let ((org-odd-levels-only odd-levels))
  4091. (org-mode)
  4092. (font-lock-fontify-buffer)
  4093. (buffer-string))))
  4094. (defvar org-m nil)
  4095. (defvar org-l nil)
  4096. (defvar org-f nil)
  4097. (defun org-get-level-face (n)
  4098. "Get the right face for match N in font-lock matching of headlines."
  4099. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4100. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4101. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4102. (cond
  4103. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4104. ((eq n 2) org-f)
  4105. (t (if org-level-color-stars-only nil org-f))))
  4106. (defun org-get-todo-face (kwd)
  4107. "Get the right face for a TODO keyword KWD.
  4108. If KWD is a number, get the corresponding match group."
  4109. (if (numberp kwd) (setq kwd (match-string kwd)))
  4110. (or (cdr (assoc kwd org-todo-keyword-faces))
  4111. (and (member kwd org-done-keywords) 'org-done)
  4112. 'org-todo))
  4113. (defun org-font-lock-add-tag-faces (limit)
  4114. "Add the special tag faces."
  4115. (when (and org-tag-faces org-tags-special-faces-re)
  4116. (while (re-search-forward org-tags-special-faces-re limit t)
  4117. (add-text-properties (match-beginning 1) (match-end 1)
  4118. (list 'face (org-get-tag-face 1)
  4119. 'font-lock-fontified t))
  4120. (backward-char 1))))
  4121. (defun org-font-lock-add-priority-faces (limit)
  4122. "Add the special priority faces."
  4123. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4124. (add-text-properties
  4125. (match-beginning 0) (match-end 0)
  4126. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4127. org-priority-faces))
  4128. 'org-special-keyword)
  4129. 'font-lock-fontified t))))
  4130. (defun org-get-tag-face (kwd)
  4131. "Get the right face for a TODO keyword KWD.
  4132. If KWD is a number, get the corresponding match group."
  4133. (if (numberp kwd) (setq kwd (match-string kwd)))
  4134. (or (cdr (assoc kwd org-tag-faces))
  4135. 'org-tag))
  4136. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4137. "Remove fontification and activation overlays from links."
  4138. (font-lock-default-unfontify-region beg end)
  4139. (let* ((buffer-undo-list t)
  4140. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4141. (inhibit-modification-hooks t)
  4142. deactivate-mark buffer-file-name buffer-file-truename)
  4143. (remove-text-properties beg end
  4144. '(mouse-face t keymap t org-linked-text t
  4145. invisible t intangible t
  4146. org-no-flyspell t))))
  4147. ;;;; Visibility cycling, including org-goto and indirect buffer
  4148. ;;; Cycling
  4149. (defvar org-cycle-global-status nil)
  4150. (make-variable-buffer-local 'org-cycle-global-status)
  4151. (defvar org-cycle-subtree-status nil)
  4152. (make-variable-buffer-local 'org-cycle-subtree-status)
  4153. ;;;###autoload
  4154. (defvar org-inlinetask-min-level)
  4155. (defun org-cycle (&optional arg)
  4156. "TAB-action and visibility cycling for Org-mode.
  4157. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4158. is outine visibility cycling, but it also invokes other actions
  4159. in special contexts.
  4160. - When this function is called with a prefix argument, rotate the entire
  4161. buffer through 3 states (global cycling)
  4162. 1. OVERVIEW: Show only top-level headlines.
  4163. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4164. 3. SHOW ALL: Show everything.
  4165. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4166. determined by the variable `org-startup-folded', and by any VISIBILITY
  4167. properties in the buffer.
  4168. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4169. including any drawers.
  4170. - When inside a table, re-align the table and move to the next field.
  4171. - When point is at the beginning of a headline, rotate the subtree started
  4172. by this line through 3 different states (local cycling)
  4173. 1. FOLDED: Only the main headline is shown.
  4174. 2. CHILDREN: The main headline and the direct children are shown.
  4175. From this state, you can move to one of the children
  4176. and zoom in further.
  4177. 3. SUBTREE: Show the entire subtree, including body text.
  4178. - When there is a numeric prefix, go up to a heading with level ARG, do
  4179. a `show-subtree' and return to the previous cursor position. If ARG
  4180. is negative, go up that many levels.
  4181. - When point is not at the beginning of a headline, execute the global
  4182. binding for TAB, which is re-indenting the line. See the option
  4183. `org-cycle-emulate-tab' for details.
  4184. - Special case: if point is at the beginning of the buffer and there is
  4185. no headline in line 1, this function will act as if called with prefix arg.
  4186. But only if also the variable `org-cycle-global-at-bob' is t."
  4187. (interactive "P")
  4188. (org-load-modules-maybe)
  4189. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4190. (let* ((limit-level
  4191. (or org-cycle-max-level
  4192. (and (boundp 'org-inlinetask-min-level)
  4193. org-inlinetask-min-level
  4194. (1- org-inlinetask-min-level))))
  4195. (nstars (and limit-level
  4196. (if org-odd-levels-only
  4197. (and limit-level (1- (* limit-level 2)))
  4198. limit-level)))
  4199. (outline-regexp
  4200. (cond
  4201. ((not (org-mode-p)) outline-regexp)
  4202. (org-cycle-include-plain-lists
  4203. (concat "\\(?:\\*"
  4204. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4205. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4206. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4207. (bob-special (and org-cycle-global-at-bob (bobp)
  4208. (not (looking-at outline-regexp))))
  4209. (org-cycle-hook
  4210. (if bob-special
  4211. (delq 'org-optimize-window-after-visibility-change
  4212. (copy-sequence org-cycle-hook))
  4213. org-cycle-hook))
  4214. (pos (point)))
  4215. (if (or bob-special (equal arg '(4)))
  4216. ;; special case: use global cycling
  4217. (setq arg t))
  4218. (cond
  4219. ((equal arg '(16))
  4220. (org-set-startup-visibility)
  4221. (message "Startup visibility, plus VISIBILITY properties"))
  4222. ((equal arg '(64))
  4223. (show-all)
  4224. (message "Entire buffer visible, including drawers"))
  4225. ((org-at-table-p 'any)
  4226. ;; Enter the table or move to the next field in the table
  4227. (or (org-table-recognize-table.el)
  4228. (progn
  4229. (if arg (org-table-edit-field t)
  4230. (org-table-justify-field-maybe)
  4231. (call-interactively 'org-table-next-field)))))
  4232. ((run-hook-with-args-until-success
  4233. 'org-tab-after-check-for-table-hook))
  4234. ((eq arg t) ;; Global cycling
  4235. (org-cycle-internal-global))
  4236. ((and org-drawers org-drawer-regexp
  4237. (save-excursion
  4238. (beginning-of-line 1)
  4239. (looking-at org-drawer-regexp)))
  4240. ;; Toggle block visibility
  4241. (org-flag-drawer
  4242. (not (get-char-property (match-end 0) 'invisible))))
  4243. ((integerp arg)
  4244. ;; Show-subtree, ARG levels up from here.
  4245. (save-excursion
  4246. (org-back-to-heading)
  4247. (outline-up-heading (if (< arg 0) (- arg)
  4248. (- (funcall outline-level) arg)))
  4249. (org-show-subtree)))
  4250. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4251. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4252. (org-cycle-internal-local))
  4253. ;; TAB emulation and template completion
  4254. (buffer-read-only (org-back-to-heading))
  4255. ((run-hook-with-args-until-success
  4256. 'org-tab-after-check-for-cycling-hook))
  4257. ((org-try-structure-completion))
  4258. ((org-try-cdlatex-tab))
  4259. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4260. (or (not (bolp))
  4261. (not (looking-at outline-regexp))))
  4262. (call-interactively (global-key-binding "\t")))
  4263. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4264. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4265. (or (and (eq org-cycle-emulate-tab 'white)
  4266. (= (match-end 0) (point-at-eol)))
  4267. (and (eq org-cycle-emulate-tab 'whitestart)
  4268. (>= (match-end 0) pos))))
  4269. t
  4270. (eq org-cycle-emulate-tab t))
  4271. (call-interactively (global-key-binding "\t")))
  4272. (t (save-excursion
  4273. (org-back-to-heading)
  4274. (org-cycle)))))))
  4275. (defun org-cycle-internal-global ()
  4276. "Do the global cycling action."
  4277. (cond
  4278. ((and (eq last-command this-command)
  4279. (eq org-cycle-global-status 'overview))
  4280. ;; We just created the overview - now do table of contents
  4281. ;; This can be slow in very large buffers, so indicate action
  4282. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4283. (message "CONTENTS...")
  4284. (org-content)
  4285. (message "CONTENTS...done")
  4286. (setq org-cycle-global-status 'contents)
  4287. (run-hook-with-args 'org-cycle-hook 'contents))
  4288. ((and (eq last-command this-command)
  4289. (eq org-cycle-global-status 'contents))
  4290. ;; We just showed the table of contents - now show everything
  4291. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4292. (show-all)
  4293. (message "SHOW ALL")
  4294. (setq org-cycle-global-status 'all)
  4295. (run-hook-with-args 'org-cycle-hook 'all))
  4296. (t
  4297. ;; Default action: go to overview
  4298. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4299. (org-overview)
  4300. (message "OVERVIEW")
  4301. (setq org-cycle-global-status 'overview)
  4302. (run-hook-with-args 'org-cycle-hook 'overview))))
  4303. (defun org-cycle-internal-local ()
  4304. "Do the local cycling action."
  4305. (org-back-to-heading)
  4306. (let ((goal-column 0) eoh eol eos)
  4307. ;; First, some boundaries
  4308. (save-excursion
  4309. (org-back-to-heading)
  4310. (save-excursion
  4311. (beginning-of-line 2)
  4312. (while (and (not (eobp)) ;; this is like `next-line'
  4313. (get-char-property (1- (point)) 'invisible))
  4314. (beginning-of-line 2)) (setq eol (point)))
  4315. (outline-end-of-heading) (setq eoh (point))
  4316. (org-end-of-subtree t)
  4317. (unless (eobp)
  4318. (skip-chars-forward " \t\n")
  4319. (beginning-of-line 1) ; in case this is an item
  4320. )
  4321. (setq eos (1- (point))))
  4322. ;; Find out what to do next and set `this-command'
  4323. (cond
  4324. ((= eos eoh)
  4325. ;; Nothing is hidden behind this heading
  4326. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4327. (message "EMPTY ENTRY")
  4328. (setq org-cycle-subtree-status nil)
  4329. (save-excursion
  4330. (goto-char eos)
  4331. (outline-next-heading)
  4332. (if (org-invisible-p) (org-flag-heading nil))))
  4333. ((or (>= eol eos)
  4334. (not (string-match "\\S-" (buffer-substring eol eos))))
  4335. ;; Entire subtree is hidden in one line: open it
  4336. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4337. (org-show-entry)
  4338. (show-children)
  4339. (message "CHILDREN")
  4340. (save-excursion
  4341. (goto-char eos)
  4342. (outline-next-heading)
  4343. (if (org-invisible-p) (org-flag-heading nil)))
  4344. (setq org-cycle-subtree-status 'children)
  4345. (run-hook-with-args 'org-cycle-hook 'children))
  4346. ((and (eq last-command this-command)
  4347. (eq org-cycle-subtree-status 'children))
  4348. ;; We just showed the children, now show everything.
  4349. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4350. (org-show-subtree)
  4351. (message "SUBTREE")
  4352. (setq org-cycle-subtree-status 'subtree)
  4353. (run-hook-with-args 'org-cycle-hook 'subtree))
  4354. (t
  4355. ;; Default action: hide the subtree.
  4356. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4357. (hide-subtree)
  4358. (message "FOLDED")
  4359. (setq org-cycle-subtree-status 'folded)
  4360. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4361. ;;;###autoload
  4362. (defun org-global-cycle (&optional arg)
  4363. "Cycle the global visibility. For details see `org-cycle'.
  4364. With C-u prefix arg, switch to startup visibility.
  4365. With a numeric prefix, show all headlines up to that level."
  4366. (interactive "P")
  4367. (let ((org-cycle-include-plain-lists
  4368. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4369. (cond
  4370. ((integerp arg)
  4371. (show-all)
  4372. (hide-sublevels arg)
  4373. (setq org-cycle-global-status 'contents))
  4374. ((equal arg '(4))
  4375. (org-set-startup-visibility)
  4376. (message "Startup visibility, plus VISIBILITY properties."))
  4377. (t
  4378. (org-cycle '(4))))))
  4379. (defun org-set-startup-visibility ()
  4380. "Set the visibility required by startup options and properties."
  4381. (cond
  4382. ((eq org-startup-folded t)
  4383. (org-cycle '(4)))
  4384. ((eq org-startup-folded 'content)
  4385. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4386. (org-cycle '(4)) (org-cycle '(4)))))
  4387. (if org-hide-block-startup (org-hide-block-all))
  4388. (org-set-visibility-according-to-property 'no-cleanup)
  4389. (org-cycle-hide-archived-subtrees 'all)
  4390. (org-cycle-hide-drawers 'all)
  4391. (org-cycle-show-empty-lines 'all))
  4392. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4393. "Switch subtree visibilities according to :VISIBILITY: property."
  4394. (interactive)
  4395. (let (org-show-entry-below state)
  4396. (save-excursion
  4397. (goto-char (point-min))
  4398. (while (re-search-forward
  4399. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4400. nil t)
  4401. (setq state (match-string 1))
  4402. (save-excursion
  4403. (org-back-to-heading t)
  4404. (hide-subtree)
  4405. (org-reveal)
  4406. (cond
  4407. ((equal state '("fold" "folded"))
  4408. (hide-subtree))
  4409. ((equal state "children")
  4410. (org-show-hidden-entry)
  4411. (show-children))
  4412. ((equal state "content")
  4413. (save-excursion
  4414. (save-restriction
  4415. (org-narrow-to-subtree)
  4416. (org-content))))
  4417. ((member state '("all" "showall"))
  4418. (show-subtree)))))
  4419. (unless no-cleanup
  4420. (org-cycle-hide-archived-subtrees 'all)
  4421. (org-cycle-hide-drawers 'all)
  4422. (org-cycle-show-empty-lines 'all)))))
  4423. (defun org-overview ()
  4424. "Switch to overview mode, showing only top-level headlines.
  4425. Really, this shows all headlines with level equal or greater than the level
  4426. of the first headline in the buffer. This is important, because if the
  4427. first headline is not level one, then (hide-sublevels 1) gives confusing
  4428. results."
  4429. (interactive)
  4430. (let ((level (save-excursion
  4431. (goto-char (point-min))
  4432. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4433. (progn
  4434. (goto-char (match-beginning 0))
  4435. (funcall outline-level))))))
  4436. (and level (hide-sublevels level))))
  4437. (defun org-content (&optional arg)
  4438. "Show all headlines in the buffer, like a table of contents.
  4439. With numerical argument N, show content up to level N."
  4440. (interactive "P")
  4441. (save-excursion
  4442. ;; Visit all headings and show their offspring
  4443. (and (integerp arg) (org-overview))
  4444. (goto-char (point-max))
  4445. (catch 'exit
  4446. (while (and (progn (condition-case nil
  4447. (outline-previous-visible-heading 1)
  4448. (error (goto-char (point-min))))
  4449. t)
  4450. (looking-at outline-regexp))
  4451. (if (integerp arg)
  4452. (show-children (1- arg))
  4453. (show-branches))
  4454. (if (bobp) (throw 'exit nil))))))
  4455. (defun org-optimize-window-after-visibility-change (state)
  4456. "Adjust the window after a change in outline visibility.
  4457. This function is the default value of the hook `org-cycle-hook'."
  4458. (when (get-buffer-window (current-buffer))
  4459. (cond
  4460. ((eq state 'content) nil)
  4461. ((eq state 'all) nil)
  4462. ((eq state 'folded) nil)
  4463. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4464. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4465. (defun org-compact-display-after-subtree-move ()
  4466. "Show a compacter version of the tree of the entry's parent."
  4467. (save-excursion
  4468. (if (org-up-heading-safe)
  4469. (progn
  4470. (hide-subtree)
  4471. (show-entry)
  4472. (show-children)
  4473. (org-cycle-show-empty-lines 'children)
  4474. (org-cycle-hide-drawers 'children))
  4475. (org-overview))))
  4476. (defun org-cycle-show-empty-lines (state)
  4477. "Show empty lines above all visible headlines.
  4478. The region to be covered depends on STATE when called through
  4479. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4480. entire buffer covered. Note that an empty line is only shown if there
  4481. are at least `org-cycle-separator-lines' empty lines before the headline."
  4482. (when (> org-cycle-separator-lines 0)
  4483. (save-excursion
  4484. (let* ((n org-cycle-separator-lines)
  4485. (re (cond
  4486. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4487. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4488. (t (let ((ns (number-to-string (- n 2))))
  4489. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4490. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4491. beg end)
  4492. (cond
  4493. ((memq state '(overview contents t))
  4494. (setq beg (point-min) end (point-max)))
  4495. ((memq state '(children folded))
  4496. (setq beg (point) end (progn (org-end-of-subtree t t)
  4497. (beginning-of-line 2)
  4498. (point)))))
  4499. (when beg
  4500. (goto-char beg)
  4501. (while (re-search-forward re end t)
  4502. (if (not (get-char-property (match-end 1) 'invisible))
  4503. (outline-flag-region
  4504. (match-beginning 1) (match-end 1) nil)))))))
  4505. ;; Never hide empty lines at the end of the file.
  4506. (save-excursion
  4507. (goto-char (point-max))
  4508. (outline-previous-heading)
  4509. (outline-end-of-heading)
  4510. (if (and (looking-at "[ \t\n]+")
  4511. (= (match-end 0) (point-max)))
  4512. (outline-flag-region (point) (match-end 0) nil))))
  4513. (defun org-show-empty-lines-in-parent ()
  4514. "Move to the parent and re-show empty lines before visible headlines."
  4515. (save-excursion
  4516. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4517. (org-cycle-show-empty-lines context))))
  4518. (defun org-cycle-hide-drawers (state)
  4519. "Re-hide all drawers after a visibility state change."
  4520. (when (and (org-mode-p)
  4521. (not (memq state '(overview folded))))
  4522. (save-excursion
  4523. (let* ((globalp (memq state '(contents all)))
  4524. (beg (if globalp (point-min) (point)))
  4525. (end (if globalp (point-max) (org-end-of-subtree t))))
  4526. (goto-char beg)
  4527. (while (re-search-forward org-drawer-regexp end t)
  4528. (org-flag-drawer t))))))
  4529. (defun org-flag-drawer (flag)
  4530. (save-excursion
  4531. (beginning-of-line 1)
  4532. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4533. (let ((b (match-end 0))
  4534. (outline-regexp org-outline-regexp))
  4535. (if (re-search-forward
  4536. "^[ \t]*:END:"
  4537. (save-excursion (outline-next-heading) (point)) t)
  4538. (outline-flag-region b (point-at-eol) flag)
  4539. (error ":END: line missing"))))))
  4540. (defun org-subtree-end-visible-p ()
  4541. "Is the end of the current subtree visible?"
  4542. (pos-visible-in-window-p
  4543. (save-excursion (org-end-of-subtree t) (point))))
  4544. (defun org-first-headline-recenter (&optional N)
  4545. "Move cursor to the first headline and recenter the headline.
  4546. Optional argument N means, put the headline into the Nth line of the window."
  4547. (goto-char (point-min))
  4548. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4549. (beginning-of-line)
  4550. (recenter (prefix-numeric-value N))))
  4551. ;;; Folding of blocks
  4552. (defconst org-block-regexp
  4553. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4554. "Regular expression for hiding blocks.")
  4555. (defvar org-hide-block-overlays nil
  4556. "Overays hiding blocks.")
  4557. (make-variable-buffer-local 'org-hide-block-overlays)
  4558. (defun org-block-map (function &optional start end)
  4559. "Call func at the head of all source blocks in the current
  4560. buffer. Optional arguments START and END can be used to limit
  4561. the range."
  4562. (let ((start (or start (point-min)))
  4563. (end (or end (point-max))))
  4564. (save-excursion
  4565. (goto-char start)
  4566. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4567. (save-excursion
  4568. (save-match-data
  4569. (goto-char (match-beginning 0))
  4570. (funcall function)))))))
  4571. (defun org-hide-block-toggle-all ()
  4572. "Toggle the visibility of all blocks in the current buffer."
  4573. (org-block-map #'org-hide-block-toggle))
  4574. (defun org-hide-block-all ()
  4575. "Fold all blocks in the current buffer."
  4576. (interactive)
  4577. (org-show-block-all)
  4578. (org-block-map #'org-hide-block-toggle-maybe))
  4579. (defun org-show-block-all ()
  4580. "Unfold all blocks in the current buffer."
  4581. (mapc 'org-delete-overlay org-hide-block-overlays)
  4582. (setq org-hide-block-overlays nil))
  4583. (defun org-hide-block-toggle-maybe ()
  4584. "Toggle visibility of block at point."
  4585. (interactive)
  4586. (let ((case-fold-search t))
  4587. (if (save-excursion
  4588. (beginning-of-line 1)
  4589. (looking-at org-block-regexp))
  4590. (progn (org-hide-block-toggle)
  4591. t) ;; to signal that we took action
  4592. nil))) ;; to signal that we did not
  4593. (defun org-hide-block-toggle (&optional force)
  4594. "Toggle the visibility of the current block."
  4595. (interactive)
  4596. (save-excursion
  4597. (beginning-of-line)
  4598. (if (re-search-forward org-block-regexp nil t)
  4599. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4600. (end (match-end 0))
  4601. ov) ;; end of entire body
  4602. (if (memq t (mapcar (lambda (overlay)
  4603. (eq (org-overlay-get overlay 'invisible)
  4604. 'org-hide-block))
  4605. (org-overlays-at start)))
  4606. (if (or (not force) (eq force 'off))
  4607. (mapc (lambda (ov)
  4608. (when (member ov org-hide-block-overlays)
  4609. (setq org-hide-block-overlays
  4610. (delq ov org-hide-block-overlays)))
  4611. (when (eq (org-overlay-get ov 'invisible)
  4612. 'org-hide-block)
  4613. (org-delete-overlay ov)))
  4614. (org-overlays-at start)))
  4615. (setq ov (make-overlay start end))
  4616. (org-overlay-put ov 'invisible 'org-hide-block)
  4617. (push ov org-hide-block-overlays)))
  4618. (error "Not looking at a source block"))))
  4619. ;; org-tab-after-check-for-cycling-hook
  4620. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4621. ;; Remove overlays when changing major mode
  4622. (add-hook 'org-mode-hook
  4623. (lambda () (org-add-hook 'change-major-mode-hook
  4624. 'org-show-block-all 'append 'local)))
  4625. ;;; Org-goto
  4626. (defvar org-goto-window-configuration nil)
  4627. (defvar org-goto-marker nil)
  4628. (defvar org-goto-map
  4629. (let ((map (make-sparse-keymap)))
  4630. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4631. (while (setq cmd (pop cmds))
  4632. (substitute-key-definition cmd cmd map global-map)))
  4633. (suppress-keymap map)
  4634. (org-defkey map "\C-m" 'org-goto-ret)
  4635. (org-defkey map [(return)] 'org-goto-ret)
  4636. (org-defkey map [(left)] 'org-goto-left)
  4637. (org-defkey map [(right)] 'org-goto-right)
  4638. (org-defkey map [(control ?g)] 'org-goto-quit)
  4639. (org-defkey map "\C-i" 'org-cycle)
  4640. (org-defkey map [(tab)] 'org-cycle)
  4641. (org-defkey map [(down)] 'outline-next-visible-heading)
  4642. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4643. (if org-goto-auto-isearch
  4644. (if (fboundp 'define-key-after)
  4645. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4646. nil)
  4647. (org-defkey map "q" 'org-goto-quit)
  4648. (org-defkey map "n" 'outline-next-visible-heading)
  4649. (org-defkey map "p" 'outline-previous-visible-heading)
  4650. (org-defkey map "f" 'outline-forward-same-level)
  4651. (org-defkey map "b" 'outline-backward-same-level)
  4652. (org-defkey map "u" 'outline-up-heading))
  4653. (org-defkey map "/" 'org-occur)
  4654. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4655. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4656. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4657. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4658. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4659. map))
  4660. (defconst org-goto-help
  4661. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4662. RET=jump to location [Q]uit and return to previous location
  4663. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4664. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4665. ;; FIXME: Docstring doe not mention both interfaces
  4666. (defun org-goto (&optional alternative-interface)
  4667. "Look up a different location in the current file, keeping current visibility.
  4668. When you want look-up or go to a different location in a document, the
  4669. fastest way is often to fold the entire buffer and then dive into the tree.
  4670. This method has the disadvantage, that the previous location will be folded,
  4671. which may not be what you want.
  4672. This command works around this by showing a copy of the current buffer
  4673. in an indirect buffer, in overview mode. You can dive into the tree in
  4674. that copy, use org-occur and incremental search to find a location.
  4675. When pressing RET or `Q', the command returns to the original buffer in
  4676. which the visibility is still unchanged. After RET is will also jump to
  4677. the location selected in the indirect buffer and expose the
  4678. the headline hierarchy above."
  4679. (interactive "P")
  4680. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4681. (org-refile-use-outline-path t)
  4682. (org-refile-target-verify-function nil)
  4683. (interface
  4684. (if (not alternative-interface)
  4685. org-goto-interface
  4686. (if (eq org-goto-interface 'outline)
  4687. 'outline-path-completion
  4688. 'outline)))
  4689. (org-goto-start-pos (point))
  4690. (selected-point
  4691. (if (eq interface 'outline)
  4692. (car (org-get-location (current-buffer) org-goto-help))
  4693. (nth 3 (org-refile-get-location "Goto: ")))))
  4694. (if selected-point
  4695. (progn
  4696. (org-mark-ring-push org-goto-start-pos)
  4697. (goto-char selected-point)
  4698. (if (or (org-invisible-p) (org-invisible-p2))
  4699. (org-show-context 'org-goto)))
  4700. (message "Quit"))))
  4701. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4702. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4703. (defvar org-goto-local-auto-isearch-map) ; defined below
  4704. (defun org-get-location (buf help)
  4705. "Let the user select a location in the Org-mode buffer BUF.
  4706. This function uses a recursive edit. It returns the selected position
  4707. or nil."
  4708. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4709. (isearch-hide-immediately nil)
  4710. (isearch-search-fun-function
  4711. (lambda () 'org-goto-local-search-headings))
  4712. (org-goto-selected-point org-goto-exit-command))
  4713. (save-excursion
  4714. (save-window-excursion
  4715. (delete-other-windows)
  4716. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4717. (switch-to-buffer
  4718. (condition-case nil
  4719. (make-indirect-buffer (current-buffer) "*org-goto*")
  4720. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4721. (with-output-to-temp-buffer "*Help*"
  4722. (princ help))
  4723. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4724. (setq buffer-read-only nil)
  4725. (let ((org-startup-truncated t)
  4726. (org-startup-folded nil)
  4727. (org-startup-align-all-tables nil))
  4728. (org-mode)
  4729. (org-overview))
  4730. (setq buffer-read-only t)
  4731. (if (and (boundp 'org-goto-start-pos)
  4732. (integer-or-marker-p org-goto-start-pos))
  4733. (let ((org-show-hierarchy-above t)
  4734. (org-show-siblings t)
  4735. (org-show-following-heading t))
  4736. (goto-char org-goto-start-pos)
  4737. (and (org-invisible-p) (org-show-context)))
  4738. (goto-char (point-min)))
  4739. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4740. (message "Select location and press RET")
  4741. (use-local-map org-goto-map)
  4742. (recursive-edit)
  4743. ))
  4744. (kill-buffer "*org-goto*")
  4745. (cons org-goto-selected-point org-goto-exit-command)))
  4746. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4747. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4748. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4749. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4750. (defun org-goto-local-search-headings (string bound noerror)
  4751. "Search and make sure that any matches are in headlines."
  4752. (catch 'return
  4753. (while (if isearch-forward
  4754. (search-forward string bound noerror)
  4755. (search-backward string bound noerror))
  4756. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4757. (and (member :headline context)
  4758. (not (member :tags context))))
  4759. (throw 'return (point))))))
  4760. (defun org-goto-local-auto-isearch ()
  4761. "Start isearch."
  4762. (interactive)
  4763. (goto-char (point-min))
  4764. (let ((keys (this-command-keys)))
  4765. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4766. (isearch-mode t)
  4767. (isearch-process-search-char (string-to-char keys)))))
  4768. (defun org-goto-ret (&optional arg)
  4769. "Finish `org-goto' by going to the new location."
  4770. (interactive "P")
  4771. (setq org-goto-selected-point (point)
  4772. org-goto-exit-command 'return)
  4773. (throw 'exit nil))
  4774. (defun org-goto-left ()
  4775. "Finish `org-goto' by going to the new location."
  4776. (interactive)
  4777. (if (org-on-heading-p)
  4778. (progn
  4779. (beginning-of-line 1)
  4780. (setq org-goto-selected-point (point)
  4781. org-goto-exit-command 'left)
  4782. (throw 'exit nil))
  4783. (error "Not on a heading")))
  4784. (defun org-goto-right ()
  4785. "Finish `org-goto' by going to the new location."
  4786. (interactive)
  4787. (if (org-on-heading-p)
  4788. (progn
  4789. (setq org-goto-selected-point (point)
  4790. org-goto-exit-command 'right)
  4791. (throw 'exit nil))
  4792. (error "Not on a heading")))
  4793. (defun org-goto-quit ()
  4794. "Finish `org-goto' without cursor motion."
  4795. (interactive)
  4796. (setq org-goto-selected-point nil)
  4797. (setq org-goto-exit-command 'quit)
  4798. (throw 'exit nil))
  4799. ;;; Indirect buffer display of subtrees
  4800. (defvar org-indirect-dedicated-frame nil
  4801. "This is the frame being used for indirect tree display.")
  4802. (defvar org-last-indirect-buffer nil)
  4803. (defun org-tree-to-indirect-buffer (&optional arg)
  4804. "Create indirect buffer and narrow it to current subtree.
  4805. With numerical prefix ARG, go up to this level and then take that tree.
  4806. If ARG is negative, go up that many levels.
  4807. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4808. indirect buffer previously made with this command, to avoid proliferation of
  4809. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4810. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4811. is kept so that you can work with several indirect buffers at the same time.
  4812. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4813. requests that a new frame be made for the new buffer, so that the dedicated
  4814. frame is not changed."
  4815. (interactive "P")
  4816. (let ((cbuf (current-buffer))
  4817. (cwin (selected-window))
  4818. (pos (point))
  4819. beg end level heading ibuf)
  4820. (save-excursion
  4821. (org-back-to-heading t)
  4822. (when (numberp arg)
  4823. (setq level (org-outline-level))
  4824. (if (< arg 0) (setq arg (+ level arg)))
  4825. (while (> (setq level (org-outline-level)) arg)
  4826. (outline-up-heading 1 t)))
  4827. (setq beg (point)
  4828. heading (org-get-heading))
  4829. (org-end-of-subtree t) (setq end (point)))
  4830. (if (and (buffer-live-p org-last-indirect-buffer)
  4831. (not (eq org-indirect-buffer-display 'new-frame))
  4832. (not arg))
  4833. (kill-buffer org-last-indirect-buffer))
  4834. (setq ibuf (org-get-indirect-buffer cbuf)
  4835. org-last-indirect-buffer ibuf)
  4836. (cond
  4837. ((or (eq org-indirect-buffer-display 'new-frame)
  4838. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4839. (select-frame (make-frame))
  4840. (delete-other-windows)
  4841. (switch-to-buffer ibuf)
  4842. (org-set-frame-title heading))
  4843. ((eq org-indirect-buffer-display 'dedicated-frame)
  4844. (raise-frame
  4845. (select-frame (or (and org-indirect-dedicated-frame
  4846. (frame-live-p org-indirect-dedicated-frame)
  4847. org-indirect-dedicated-frame)
  4848. (setq org-indirect-dedicated-frame (make-frame)))))
  4849. (delete-other-windows)
  4850. (switch-to-buffer ibuf)
  4851. (org-set-frame-title (concat "Indirect: " heading)))
  4852. ((eq org-indirect-buffer-display 'current-window)
  4853. (switch-to-buffer ibuf))
  4854. ((eq org-indirect-buffer-display 'other-window)
  4855. (pop-to-buffer ibuf))
  4856. (t (error "Invalid value.")))
  4857. (if (featurep 'xemacs)
  4858. (save-excursion (org-mode) (turn-on-font-lock)))
  4859. (narrow-to-region beg end)
  4860. (show-all)
  4861. (goto-char pos)
  4862. (and (window-live-p cwin) (select-window cwin))))
  4863. (defun org-get-indirect-buffer (&optional buffer)
  4864. (setq buffer (or buffer (current-buffer)))
  4865. (let ((n 1) (base (buffer-name buffer)) bname)
  4866. (while (buffer-live-p
  4867. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4868. (setq n (1+ n)))
  4869. (condition-case nil
  4870. (make-indirect-buffer buffer bname 'clone)
  4871. (error (make-indirect-buffer buffer bname)))))
  4872. (defun org-set-frame-title (title)
  4873. "Set the title of the current frame to the string TITLE."
  4874. ;; FIXME: how to name a single frame in XEmacs???
  4875. (unless (featurep 'xemacs)
  4876. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4877. ;;;; Structure editing
  4878. ;;; Inserting headlines
  4879. (defun org-previous-line-empty-p ()
  4880. (save-excursion
  4881. (and (not (bobp))
  4882. (or (beginning-of-line 0) t)
  4883. (save-match-data
  4884. (looking-at "[ \t]*$")))))
  4885. (defun org-insert-heading (&optional force-heading)
  4886. "Insert a new heading or item with same depth at point.
  4887. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4888. If point is at the beginning of a headline, insert a sibling before the
  4889. current headline. If point is not at the beginning, do not split the line,
  4890. but create the new headline after the current line."
  4891. (interactive "P")
  4892. (if (= (buffer-size) 0)
  4893. (insert "\n* ")
  4894. (when (or force-heading (not (org-insert-item)))
  4895. (let* ((empty-line-p nil)
  4896. (head (save-excursion
  4897. (condition-case nil
  4898. (progn
  4899. (org-back-to-heading)
  4900. (setq empty-line-p (org-previous-line-empty-p))
  4901. (match-string 0))
  4902. (error "*"))))
  4903. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4904. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4905. pos hide-previous previous-pos)
  4906. (cond
  4907. ((and (org-on-heading-p) (bolp)
  4908. (or (bobp)
  4909. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4910. ;; insert before the current line
  4911. (open-line (if blank 2 1)))
  4912. ((and (bolp)
  4913. (or (bobp)
  4914. (save-excursion
  4915. (backward-char 1) (not (org-invisible-p)))))
  4916. ;; insert right here
  4917. nil)
  4918. (t
  4919. ;; somewhere in the line
  4920. (save-excursion
  4921. (setq previous-pos (point-at-bol))
  4922. (end-of-line)
  4923. (setq hide-previous (org-invisible-p)))
  4924. (and org-insert-heading-respect-content (org-show-subtree))
  4925. (let ((split
  4926. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4927. (save-excursion
  4928. (let ((p (point)))
  4929. (goto-char (point-at-bol))
  4930. (and (looking-at org-complex-heading-regexp)
  4931. (> p (match-beginning 4)))))))
  4932. tags pos)
  4933. (cond
  4934. (org-insert-heading-respect-content
  4935. (org-end-of-subtree nil t)
  4936. (or (bolp) (newline))
  4937. (or (org-previous-line-empty-p)
  4938. (and blank (newline)))
  4939. (open-line 1))
  4940. ((org-on-heading-p)
  4941. (when hide-previous
  4942. (show-children)
  4943. (org-show-entry))
  4944. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4945. (setq tags (and (match-end 2) (match-string 2)))
  4946. (and (match-end 1)
  4947. (delete-region (match-beginning 1) (match-end 1)))
  4948. (setq pos (point-at-bol))
  4949. (or split (end-of-line 1))
  4950. (delete-horizontal-space)
  4951. (newline (if blank 2 1))
  4952. (when tags
  4953. (save-excursion
  4954. (goto-char pos)
  4955. (end-of-line 1)
  4956. (insert " " tags)
  4957. (org-set-tags nil 'align))))
  4958. (t
  4959. (or split (end-of-line 1))
  4960. (newline (if blank 2 1)))))))
  4961. (insert head) (just-one-space)
  4962. (setq pos (point))
  4963. (end-of-line 1)
  4964. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4965. (when (and org-insert-heading-respect-content hide-previous)
  4966. (save-excursion
  4967. (goto-char previous-pos)
  4968. (hide-subtree)))
  4969. (run-hooks 'org-insert-heading-hook)))))
  4970. (defun org-get-heading (&optional no-tags)
  4971. "Return the heading of the current entry, without the stars."
  4972. (save-excursion
  4973. (org-back-to-heading t)
  4974. (if (looking-at
  4975. (if no-tags
  4976. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4977. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4978. (match-string 1) "")))
  4979. (defun org-heading-components ()
  4980. "Return the components of the current heading.
  4981. This is a list with the following elements:
  4982. - the level as an integer
  4983. - the reduced level, different if `org-odd-levels-only' is set.
  4984. - the TODO keyword, or nil
  4985. - the priority character, like ?A, or nil if no priority is given
  4986. - the headline text itself, or the tags string if no headline text
  4987. - the tags string, or nil."
  4988. (save-excursion
  4989. (org-back-to-heading t)
  4990. (if (looking-at org-complex-heading-regexp)
  4991. (list (length (match-string 1))
  4992. (org-reduced-level (length (match-string 1)))
  4993. (org-match-string-no-properties 2)
  4994. (and (match-end 3) (aref (match-string 3) 2))
  4995. (org-match-string-no-properties 4)
  4996. (org-match-string-no-properties 5)))))
  4997. (defun org-get-entry ()
  4998. "Get the entry text, after heading, entire subtree."
  4999. (save-excursion
  5000. (org-back-to-heading t)
  5001. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5002. (defun org-insert-heading-after-current ()
  5003. "Insert a new heading with same level as current, after current subtree."
  5004. (interactive)
  5005. (org-back-to-heading)
  5006. (org-insert-heading)
  5007. (org-move-subtree-down)
  5008. (end-of-line 1))
  5009. (defun org-insert-heading-respect-content ()
  5010. (interactive)
  5011. (let ((org-insert-heading-respect-content t))
  5012. (org-insert-heading t)))
  5013. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5014. (interactive "P")
  5015. (let ((org-insert-heading-respect-content t))
  5016. (org-insert-todo-heading force-state t)))
  5017. (defun org-insert-todo-heading (arg &optional force-heading)
  5018. "Insert a new heading with the same level and TODO state as current heading.
  5019. If the heading has no TODO state, or if the state is DONE, use the first
  5020. state (TODO by default). Also with prefix arg, force first state."
  5021. (interactive "P")
  5022. (when (or force-heading (not (org-insert-item 'checkbox)))
  5023. (org-insert-heading force-heading)
  5024. (save-excursion
  5025. (org-back-to-heading)
  5026. (outline-previous-heading)
  5027. (looking-at org-todo-line-regexp))
  5028. (let*
  5029. ((new-mark-x
  5030. (if (or arg
  5031. (not (match-beginning 2))
  5032. (member (match-string 2) org-done-keywords))
  5033. (car org-todo-keywords-1)
  5034. (match-string 2)))
  5035. (new-mark
  5036. (or
  5037. (run-hook-with-args-until-success
  5038. 'org-todo-get-default-hook new-mark-x nil)
  5039. new-mark-x)))
  5040. (beginning-of-line 1)
  5041. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5042. (if org-treat-insert-todo-heading-as-state-change
  5043. (org-todo new-mark)
  5044. (insert new-mark " "))))
  5045. (when org-provide-todo-statistics
  5046. (org-update-parent-todo-statistics))))
  5047. (defun org-insert-subheading (arg)
  5048. "Insert a new subheading and demote it.
  5049. Works for outline headings and for plain lists alike."
  5050. (interactive "P")
  5051. (org-insert-heading arg)
  5052. (cond
  5053. ((org-on-heading-p) (org-do-demote))
  5054. ((org-at-item-p) (org-indent-item 1))))
  5055. (defun org-insert-todo-subheading (arg)
  5056. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5057. Works for outline headings and for plain lists alike."
  5058. (interactive "P")
  5059. (org-insert-todo-heading arg)
  5060. (cond
  5061. ((org-on-heading-p) (org-do-demote))
  5062. ((org-at-item-p) (org-indent-item 1))))
  5063. ;;; Promotion and Demotion
  5064. (defun org-promote-subtree ()
  5065. "Promote the entire subtree.
  5066. See also `org-promote'."
  5067. (interactive)
  5068. (save-excursion
  5069. (org-map-tree 'org-promote))
  5070. (org-fix-position-after-promote))
  5071. (defun org-demote-subtree ()
  5072. "Demote the entire subtree. See `org-demote'.
  5073. See also `org-promote'."
  5074. (interactive)
  5075. (save-excursion
  5076. (org-map-tree 'org-demote))
  5077. (org-fix-position-after-promote))
  5078. (defun org-do-promote ()
  5079. "Promote the current heading higher up the tree.
  5080. If the region is active in `transient-mark-mode', promote all headings
  5081. in the region."
  5082. (interactive)
  5083. (save-excursion
  5084. (if (org-region-active-p)
  5085. (org-map-region 'org-promote (region-beginning) (region-end))
  5086. (org-promote)))
  5087. (org-fix-position-after-promote))
  5088. (defun org-do-demote ()
  5089. "Demote the current heading lower down the tree.
  5090. If the region is active in `transient-mark-mode', demote all headings
  5091. in the region."
  5092. (interactive)
  5093. (save-excursion
  5094. (if (org-region-active-p)
  5095. (org-map-region 'org-demote (region-beginning) (region-end))
  5096. (org-demote)))
  5097. (org-fix-position-after-promote))
  5098. (defun org-fix-position-after-promote ()
  5099. "Make sure that after pro/demotion cursor position is right."
  5100. (let ((pos (point)))
  5101. (when (save-excursion
  5102. (beginning-of-line 1)
  5103. (looking-at org-todo-line-regexp)
  5104. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5105. (cond ((eobp) (insert " "))
  5106. ((eolp) (insert " "))
  5107. ((equal (char-after) ?\ ) (forward-char 1))))))
  5108. (defun org-reduced-level (l)
  5109. "Compute the effective level of a heading.
  5110. This takes into account the setting of `org-odd-levels-only'."
  5111. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5112. (defun org-get-valid-level (level &optional change)
  5113. "Rectify a level change under the influence of `org-odd-levels-only'
  5114. LEVEL is a current level, CHANGE is by how much the level should be
  5115. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5116. even level numbers will become the next higher odd number."
  5117. (if org-odd-levels-only
  5118. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5119. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5120. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5121. (max 1 (+ level (or change 0)))))
  5122. (if (boundp 'define-obsolete-function-alias)
  5123. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5124. (define-obsolete-function-alias 'org-get-legal-level
  5125. 'org-get-valid-level)
  5126. (define-obsolete-function-alias 'org-get-legal-level
  5127. 'org-get-valid-level "23.1")))
  5128. (defun org-promote ()
  5129. "Promote the current heading higher up the tree.
  5130. If the region is active in `transient-mark-mode', promote all headings
  5131. in the region."
  5132. (org-back-to-heading t)
  5133. (let* ((level (save-match-data (funcall outline-level)))
  5134. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5135. (diff (abs (- level (length up-head) -1))))
  5136. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5137. (replace-match up-head nil t)
  5138. ;; Fixup tag positioning
  5139. (and org-auto-align-tags (org-set-tags nil t))
  5140. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5141. (defun org-demote ()
  5142. "Demote the current heading lower down the tree.
  5143. If the region is active in `transient-mark-mode', demote all headings
  5144. in the region."
  5145. (org-back-to-heading t)
  5146. (let* ((level (save-match-data (funcall outline-level)))
  5147. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5148. (diff (abs (- level (length down-head) -1))))
  5149. (replace-match down-head nil t)
  5150. ;; Fixup tag positioning
  5151. (and org-auto-align-tags (org-set-tags nil t))
  5152. (if org-adapt-indentation (org-fixup-indentation diff))))
  5153. (defun org-map-tree (fun)
  5154. "Call FUN for every heading underneath the current one."
  5155. (org-back-to-heading)
  5156. (let ((level (funcall outline-level)))
  5157. (save-excursion
  5158. (funcall fun)
  5159. (while (and (progn
  5160. (outline-next-heading)
  5161. (> (funcall outline-level) level))
  5162. (not (eobp)))
  5163. (funcall fun)))))
  5164. (defun org-map-region (fun beg end)
  5165. "Call FUN for every heading between BEG and END."
  5166. (let ((org-ignore-region t))
  5167. (save-excursion
  5168. (setq end (copy-marker end))
  5169. (goto-char beg)
  5170. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5171. (< (point) end))
  5172. (funcall fun))
  5173. (while (and (progn
  5174. (outline-next-heading)
  5175. (< (point) end))
  5176. (not (eobp)))
  5177. (funcall fun)))))
  5178. (defun org-fixup-indentation (diff)
  5179. "Change the indentation in the current entry by DIFF
  5180. However, if any line in the current entry has no indentation, or if it
  5181. would end up with no indentation after the change, nothing at all is done."
  5182. (save-excursion
  5183. (let ((end (save-excursion (outline-next-heading)
  5184. (point-marker)))
  5185. (prohibit (if (> diff 0)
  5186. "^\\S-"
  5187. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5188. col)
  5189. (unless (save-excursion (end-of-line 1)
  5190. (re-search-forward prohibit end t))
  5191. (while (and (< (point) end)
  5192. (re-search-forward "^[ \t]+" end t))
  5193. (goto-char (match-end 0))
  5194. (setq col (current-column))
  5195. (if (< diff 0) (replace-match ""))
  5196. (org-indent-to-column (+ diff col))))
  5197. (move-marker end nil))))
  5198. (defun org-convert-to-odd-levels ()
  5199. "Convert an org-mode file with all levels allowed to one with odd levels.
  5200. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5201. level 5 etc."
  5202. (interactive)
  5203. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5204. (let ((org-odd-levels-only nil) n)
  5205. (save-excursion
  5206. (goto-char (point-min))
  5207. (while (re-search-forward "^\\*\\*+ " nil t)
  5208. (setq n (- (length (match-string 0)) 2))
  5209. (while (>= (setq n (1- n)) 0)
  5210. (org-demote))
  5211. (end-of-line 1))))))
  5212. (defun org-convert-to-oddeven-levels ()
  5213. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5214. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5215. section with an even level, conversion would destroy the structure of the file. An error
  5216. is signaled in this case."
  5217. (interactive)
  5218. (goto-char (point-min))
  5219. ;; First check if there are no even levels
  5220. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5221. (org-show-context t)
  5222. (error "Not all levels are odd in this file. Conversion not possible."))
  5223. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5224. (let ((org-odd-levels-only nil) n)
  5225. (save-excursion
  5226. (goto-char (point-min))
  5227. (while (re-search-forward "^\\*\\*+ " nil t)
  5228. (setq n (/ (1- (length (match-string 0))) 2))
  5229. (while (>= (setq n (1- n)) 0)
  5230. (org-promote))
  5231. (end-of-line 1))))))
  5232. (defun org-tr-level (n)
  5233. "Make N odd if required."
  5234. (if org-odd-levels-only (1+ (/ n 2)) n))
  5235. ;;; Vertical tree motion, cutting and pasting of subtrees
  5236. (defun org-move-subtree-up (&optional arg)
  5237. "Move the current subtree up past ARG headlines of the same level."
  5238. (interactive "p")
  5239. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5240. (defun org-move-subtree-down (&optional arg)
  5241. "Move the current subtree down past ARG headlines of the same level."
  5242. (interactive "p")
  5243. (setq arg (prefix-numeric-value arg))
  5244. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5245. 'outline-get-last-sibling))
  5246. (ins-point (make-marker))
  5247. (cnt (abs arg))
  5248. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5249. ;; Select the tree
  5250. (org-back-to-heading)
  5251. (setq beg0 (point))
  5252. (save-excursion
  5253. (setq ne-beg (org-back-over-empty-lines))
  5254. (setq beg (point)))
  5255. (save-match-data
  5256. (save-excursion (outline-end-of-heading)
  5257. (setq folded (org-invisible-p)))
  5258. (outline-end-of-subtree))
  5259. (outline-next-heading)
  5260. (setq ne-end (org-back-over-empty-lines))
  5261. (setq end (point))
  5262. (goto-char beg0)
  5263. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5264. ;; include less whitespace
  5265. (save-excursion
  5266. (goto-char beg)
  5267. (forward-line (- ne-beg ne-end))
  5268. (setq beg (point))))
  5269. ;; Find insertion point, with error handling
  5270. (while (> cnt 0)
  5271. (or (and (funcall movfunc) (looking-at outline-regexp))
  5272. (progn (goto-char beg0)
  5273. (error "Cannot move past superior level or buffer limit")))
  5274. (setq cnt (1- cnt)))
  5275. (if (> arg 0)
  5276. ;; Moving forward - still need to move over subtree
  5277. (progn (org-end-of-subtree t t)
  5278. (save-excursion
  5279. (org-back-over-empty-lines)
  5280. (or (bolp) (newline)))))
  5281. (setq ne-ins (org-back-over-empty-lines))
  5282. (move-marker ins-point (point))
  5283. (setq txt (buffer-substring beg end))
  5284. (org-save-markers-in-region beg end)
  5285. (delete-region beg end)
  5286. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5287. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5288. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5289. (let ((bbb (point)))
  5290. (insert-before-markers txt)
  5291. (org-reinstall-markers-in-region bbb)
  5292. (move-marker ins-point bbb))
  5293. (or (bolp) (insert "\n"))
  5294. (setq ins-end (point))
  5295. (goto-char ins-point)
  5296. (org-skip-whitespace)
  5297. (when (and (< arg 0)
  5298. (org-first-sibling-p)
  5299. (> ne-ins ne-beg))
  5300. ;; Move whitespace back to beginning
  5301. (save-excursion
  5302. (goto-char ins-end)
  5303. (let ((kill-whole-line t))
  5304. (kill-line (- ne-ins ne-beg)) (point)))
  5305. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5306. (move-marker ins-point nil)
  5307. (org-compact-display-after-subtree-move)
  5308. (org-show-empty-lines-in-parent)
  5309. (unless folded
  5310. (org-show-entry)
  5311. (show-children)
  5312. (org-cycle-hide-drawers 'children))))
  5313. (defvar org-subtree-clip ""
  5314. "Clipboard for cut and paste of subtrees.
  5315. This is actually only a copy of the kill, because we use the normal kill
  5316. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5317. (defvar org-subtree-clip-folded nil
  5318. "Was the last copied subtree folded?
  5319. This is used to fold the tree back after pasting.")
  5320. (defun org-cut-subtree (&optional n)
  5321. "Cut the current subtree into the clipboard.
  5322. With prefix arg N, cut this many sequential subtrees.
  5323. This is a short-hand for marking the subtree and then cutting it."
  5324. (interactive "p")
  5325. (org-copy-subtree n 'cut))
  5326. (defun org-copy-subtree (&optional n cut force-store-markers)
  5327. "Cut the current subtree into the clipboard.
  5328. With prefix arg N, cut this many sequential subtrees.
  5329. This is a short-hand for marking the subtree and then copying it.
  5330. If CUT is non-nil, actually cut the subtree.
  5331. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5332. of some markers in the region, even if CUT is non-nil. This is
  5333. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5334. (interactive "p")
  5335. (let (beg end folded (beg0 (point)))
  5336. (if (interactive-p)
  5337. (org-back-to-heading nil) ; take what looks like a subtree
  5338. (org-back-to-heading t)) ; take what is really there
  5339. (org-back-over-empty-lines)
  5340. (setq beg (point))
  5341. (skip-chars-forward " \t\r\n")
  5342. (save-match-data
  5343. (save-excursion (outline-end-of-heading)
  5344. (setq folded (org-invisible-p)))
  5345. (condition-case nil
  5346. (outline-forward-same-level (1- n))
  5347. (error nil))
  5348. (org-end-of-subtree t t))
  5349. (org-back-over-empty-lines)
  5350. (setq end (point))
  5351. (goto-char beg0)
  5352. (when (> end beg)
  5353. (setq org-subtree-clip-folded folded)
  5354. (when (or cut force-store-markers)
  5355. (org-save-markers-in-region beg end))
  5356. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5357. (setq org-subtree-clip (current-kill 0))
  5358. (message "%s: Subtree(s) with %d characters"
  5359. (if cut "Cut" "Copied")
  5360. (length org-subtree-clip)))))
  5361. (defun org-paste-subtree (&optional level tree for-yank)
  5362. "Paste the clipboard as a subtree, with modification of headline level.
  5363. The entire subtree is promoted or demoted in order to match a new headline
  5364. level.
  5365. If the cursor is at the beginning of a headline, the same level as
  5366. that headline is used to paste the tree
  5367. If not, the new level is derived from the *visible* headings
  5368. before and after the insertion point, and taken to be the inferior headline
  5369. level of the two. So if the previous visible heading is level 3 and the
  5370. next is level 4 (or vice versa), level 4 will be used for insertion.
  5371. This makes sure that the subtree remains an independent subtree and does
  5372. not swallow low level entries.
  5373. You can also force a different level, either by using a numeric prefix
  5374. argument, or by inserting the heading marker by hand. For example, if the
  5375. cursor is after \"*****\", then the tree will be shifted to level 5.
  5376. If optional TREE is given, use this text instead of the kill ring.
  5377. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5378. move back over whitespace before inserting, and move point to the end of
  5379. the inserted text when done."
  5380. (interactive "P")
  5381. (unless (org-kill-is-subtree-p tree)
  5382. (error "%s"
  5383. (substitute-command-keys
  5384. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5385. (let* ((visp (not (org-invisible-p)))
  5386. (txt (or tree (and kill-ring (current-kill 0))))
  5387. (^re (concat "^\\(" outline-regexp "\\)"))
  5388. (re (concat "\\(" outline-regexp "\\)"))
  5389. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5390. (old-level (if (string-match ^re txt)
  5391. (- (match-end 0) (match-beginning 0) 1)
  5392. -1))
  5393. (force-level (cond (level (prefix-numeric-value level))
  5394. ((and (looking-at "[ \t]*$")
  5395. (string-match
  5396. ^re_ (buffer-substring
  5397. (point-at-bol) (point))))
  5398. (- (match-end 1) (match-beginning 1)))
  5399. ((and (bolp)
  5400. (looking-at org-outline-regexp))
  5401. (- (match-end 0) (point) 1))
  5402. (t nil)))
  5403. (previous-level (save-excursion
  5404. (condition-case nil
  5405. (progn
  5406. (outline-previous-visible-heading 1)
  5407. (if (looking-at re)
  5408. (- (match-end 0) (match-beginning 0) 1)
  5409. 1))
  5410. (error 1))))
  5411. (next-level (save-excursion
  5412. (condition-case nil
  5413. (progn
  5414. (or (looking-at outline-regexp)
  5415. (outline-next-visible-heading 1))
  5416. (if (looking-at re)
  5417. (- (match-end 0) (match-beginning 0) 1)
  5418. 1))
  5419. (error 1))))
  5420. (new-level (or force-level (max previous-level next-level)))
  5421. (shift (if (or (= old-level -1)
  5422. (= new-level -1)
  5423. (= old-level new-level))
  5424. 0
  5425. (- new-level old-level)))
  5426. (delta (if (> shift 0) -1 1))
  5427. (func (if (> shift 0) 'org-demote 'org-promote))
  5428. (org-odd-levels-only nil)
  5429. beg end newend)
  5430. ;; Remove the forced level indicator
  5431. (if force-level
  5432. (delete-region (point-at-bol) (point)))
  5433. ;; Paste
  5434. (beginning-of-line 1)
  5435. (unless for-yank (org-back-over-empty-lines))
  5436. (setq beg (point))
  5437. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5438. (insert-before-markers txt)
  5439. (unless (string-match "\n\\'" txt) (insert "\n"))
  5440. (setq newend (point))
  5441. (org-reinstall-markers-in-region beg)
  5442. (setq end (point))
  5443. (goto-char beg)
  5444. (skip-chars-forward " \t\n\r")
  5445. (setq beg (point))
  5446. (if (and (org-invisible-p) visp)
  5447. (save-excursion (outline-show-heading)))
  5448. ;; Shift if necessary
  5449. (unless (= shift 0)
  5450. (save-restriction
  5451. (narrow-to-region beg end)
  5452. (while (not (= shift 0))
  5453. (org-map-region func (point-min) (point-max))
  5454. (setq shift (+ delta shift)))
  5455. (goto-char (point-min))
  5456. (setq newend (point-max))))
  5457. (when (or (interactive-p) for-yank)
  5458. (message "Clipboard pasted as level %d subtree" new-level))
  5459. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5460. kill-ring
  5461. (eq org-subtree-clip (current-kill 0))
  5462. org-subtree-clip-folded)
  5463. ;; The tree was folded before it was killed/copied
  5464. (hide-subtree))
  5465. (and for-yank (goto-char newend))))
  5466. (defun org-kill-is-subtree-p (&optional txt)
  5467. "Check if the current kill is an outline subtree, or a set of trees.
  5468. Returns nil if kill does not start with a headline, or if the first
  5469. headline level is not the largest headline level in the tree.
  5470. So this will actually accept several entries of equal levels as well,
  5471. which is OK for `org-paste-subtree'.
  5472. If optional TXT is given, check this string instead of the current kill."
  5473. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5474. (start-level (and kill
  5475. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5476. org-outline-regexp "\\)")
  5477. kill)
  5478. (- (match-end 2) (match-beginning 2) 1)))
  5479. (re (concat "^" org-outline-regexp))
  5480. (start (1+ (or (match-beginning 2) -1))))
  5481. (if (not start-level)
  5482. (progn
  5483. nil) ;; does not even start with a heading
  5484. (catch 'exit
  5485. (while (setq start (string-match re kill (1+ start)))
  5486. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5487. (throw 'exit nil)))
  5488. t))))
  5489. (defvar org-markers-to-move nil
  5490. "Markers that should be moved with a cut-and-paste operation.
  5491. Those markers are stored together with their positions relative to
  5492. the start of the region.")
  5493. (defun org-save-markers-in-region (beg end)
  5494. "Check markers in region.
  5495. If these markers are between BEG and END, record their position relative
  5496. to BEG, so that after moving the block of text, we can put the markers back
  5497. into place.
  5498. This function gets called just before an entry or tree gets cut from the
  5499. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5500. called immediately, to move the markers with the entries."
  5501. (setq org-markers-to-move nil)
  5502. (when (featurep 'org-clock)
  5503. (org-clock-save-markers-for-cut-and-paste beg end))
  5504. (when (featurep 'org-agenda)
  5505. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5506. (defun org-check-and-save-marker (marker beg end)
  5507. "Check if MARKER is between BEG and END.
  5508. If yes, remember the marker and the distance to BEG."
  5509. (when (and (marker-buffer marker)
  5510. (equal (marker-buffer marker) (current-buffer)))
  5511. (if (and (>= marker beg) (< marker end))
  5512. (push (cons marker (- marker beg)) org-markers-to-move))))
  5513. (defun org-reinstall-markers-in-region (beg)
  5514. "Move all remembered markers to their position relative to BEG."
  5515. (mapc (lambda (x)
  5516. (move-marker (car x) (+ beg (cdr x))))
  5517. org-markers-to-move)
  5518. (setq org-markers-to-move nil))
  5519. (defun org-narrow-to-subtree ()
  5520. "Narrow buffer to the current subtree."
  5521. (interactive)
  5522. (save-excursion
  5523. (save-match-data
  5524. (narrow-to-region
  5525. (progn (org-back-to-heading) (point))
  5526. (progn (org-end-of-subtree t) (point))))))
  5527. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5528. "Clone the task (subtree) at point N times.
  5529. The clones will be inserted as siblings.
  5530. In interactive use, the user will be prompted for the number of clones
  5531. to be produced, and for a time SHIFT, which may be a repeater as used
  5532. in time stamps, for example `+3d'.
  5533. When a valid repeater is given and the entry contains any time stamps,
  5534. the clones will become a sequence in time, with time stamps in the
  5535. subtree shifted for each clone produced. If SHIFT is nil or the
  5536. empty string, time stamps will be left alone.
  5537. If the original subtree did contain time stamps with a repeater,
  5538. the following will happen:
  5539. - the repeater will be removed in each clone
  5540. - an additional clone will be produced, with the current, unshifted
  5541. date(s) in the entry.
  5542. - the original entry will be placed *after* all the clones, with
  5543. repeater intact.
  5544. - the start days in the repeater in the original entry will be shifted
  5545. to past the last clone.
  5546. I this way you can spell out a number of instances of a repeating task,
  5547. and still retain the repeater to cover future instances of the task."
  5548. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5549. (let (beg end template task
  5550. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5551. (if (not (and (integerp n) (> n 0)))
  5552. (error "Invalid number of replications %s" n))
  5553. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5554. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5555. shift)))
  5556. (error "Invalid shift specification %s" shift))
  5557. (when doshift
  5558. (setq shift-n (string-to-number (match-string 1 shift))
  5559. shift-what (cdr (assoc (match-string 2 shift)
  5560. '(("d" . day) ("w" . week)
  5561. ("m" . month) ("y" . year))))))
  5562. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5563. (setq nmin 1 nmax n)
  5564. (org-back-to-heading t)
  5565. (setq beg (point))
  5566. (org-end-of-subtree t t)
  5567. (setq end (point))
  5568. (setq template (buffer-substring beg end))
  5569. (when (and doshift
  5570. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5571. (delete-region beg end)
  5572. (setq end beg)
  5573. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5574. (goto-char end)
  5575. (loop for n from nmin to nmax do
  5576. (if (not doshift)
  5577. (setq task template)
  5578. (with-temp-buffer
  5579. (insert template)
  5580. (org-mode)
  5581. (goto-char (point-min))
  5582. (while (re-search-forward org-ts-regexp nil t)
  5583. (org-timestamp-change (* n shift-n) shift-what))
  5584. (unless (= n n-no-remove)
  5585. (goto-char (point-min))
  5586. (while (re-search-forward org-ts-regexp nil t)
  5587. (save-excursion
  5588. (goto-char (match-beginning 0))
  5589. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5590. (delete-region (match-beginning 1) (match-end 1))))))
  5591. (setq task (buffer-string))))
  5592. (insert task))
  5593. (goto-char beg)))
  5594. ;;; Outline Sorting
  5595. (defun org-sort (with-case)
  5596. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5597. Optional argument WITH-CASE means sort case-sensitively.
  5598. With a double prefix argument, also remove duplicate entries."
  5599. (interactive "P")
  5600. (if (org-at-table-p)
  5601. (org-call-with-arg 'org-table-sort-lines with-case)
  5602. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5603. (defun org-sort-remove-invisible (s)
  5604. (remove-text-properties 0 (length s) org-rm-props s)
  5605. (while (string-match org-bracket-link-regexp s)
  5606. (setq s (replace-match (if (match-end 2)
  5607. (match-string 3 s)
  5608. (match-string 1 s)) t t s)))
  5609. s)
  5610. (defvar org-priority-regexp) ; defined later in the file
  5611. (defvar org-after-sorting-entries-or-items-hook nil
  5612. "Hook that is run after a bunch of entries or items have been sorted.
  5613. When children are sorted, the cursor is in the parent line when this
  5614. hook gets called. When a region or a plain list is sorted, the cursor
  5615. will be in the first entry of the sorted region/list.")
  5616. (defun org-sort-entries-or-items
  5617. (&optional with-case sorting-type getkey-func compare-func property)
  5618. "Sort entries on a certain level of an outline tree, or plain list items.
  5619. If there is an active region, the entries in the region are sorted.
  5620. Else, if the cursor is before the first entry, sort the top-level items.
  5621. Else, the children of the entry at point are sorted.
  5622. If the cursor is at the first item in a plain list, the list items will be
  5623. sorted.
  5624. Sorting can be alphabetically, numerically, by date/time as given by
  5625. a time stamp, by a property or by priority.
  5626. The command prompts for the sorting type unless it has been given to the
  5627. function through the SORTING-TYPE argument, which needs to a character,
  5628. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5629. precise meaning of each character:
  5630. n Numerically, by converting the beginning of the entry/item to a number.
  5631. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5632. t By date/time, either the first active time stamp in the entry, or, if
  5633. none exist, by the first inactive one.
  5634. In items, only the first line will be chekced.
  5635. s By the scheduled date/time.
  5636. d By deadline date/time.
  5637. c By creation time, which is assumed to be the first inactive time stamp
  5638. at the beginning of a line.
  5639. p By priority according to the cookie.
  5640. r By the value of a property.
  5641. Capital letters will reverse the sort order.
  5642. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5643. called with point at the beginning of the record. It must return either
  5644. a string or a number that should serve as the sorting key for that record.
  5645. Comparing entries ignores case by default. However, with an optional argument
  5646. WITH-CASE, the sorting considers case as well."
  5647. (interactive "P")
  5648. (let ((case-func (if with-case 'identity 'downcase))
  5649. start beg end stars re re2
  5650. txt what tmp plain-list-p)
  5651. ;; Find beginning and end of region to sort
  5652. (cond
  5653. ((org-region-active-p)
  5654. ;; we will sort the region
  5655. (setq end (region-end)
  5656. what "region")
  5657. (goto-char (region-beginning))
  5658. (if (not (org-on-heading-p)) (outline-next-heading))
  5659. (setq start (point)))
  5660. ((org-at-item-p)
  5661. ;; we will sort this plain list
  5662. (org-beginning-of-item-list) (setq start (point))
  5663. (org-end-of-item-list) (setq end (point))
  5664. (goto-char start)
  5665. (setq plain-list-p t
  5666. what "plain list"))
  5667. ((or (org-on-heading-p)
  5668. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5669. ;; we will sort the children of the current headline
  5670. (org-back-to-heading)
  5671. (setq start (point)
  5672. end (progn (org-end-of-subtree t t)
  5673. (org-back-over-empty-lines)
  5674. (point))
  5675. what "children")
  5676. (goto-char start)
  5677. (show-subtree)
  5678. (outline-next-heading))
  5679. (t
  5680. ;; we will sort the top-level entries in this file
  5681. (goto-char (point-min))
  5682. (or (org-on-heading-p) (outline-next-heading))
  5683. (setq start (point) end (point-max) what "top-level")
  5684. (goto-char start)
  5685. (show-all)))
  5686. (setq beg (point))
  5687. (if (>= beg end) (error "Nothing to sort"))
  5688. (unless plain-list-p
  5689. (looking-at "\\(\\*+\\)")
  5690. (setq stars (match-string 1)
  5691. re (concat "^" (regexp-quote stars) " +")
  5692. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5693. txt (buffer-substring beg end))
  5694. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5695. (if (and (not (equal stars "*")) (string-match re2 txt))
  5696. (error "Region to sort contains a level above the first entry")))
  5697. (unless sorting-type
  5698. (message
  5699. (if plain-list-p
  5700. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5701. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5702. [t]ime [s]cheduled [d]eadline [c]reated
  5703. A/N/T/S/D/C/P/O/F means reversed:")
  5704. what)
  5705. (setq sorting-type (read-char-exclusive))
  5706. (and (= (downcase sorting-type) ?f)
  5707. (setq getkey-func
  5708. (org-ido-completing-read "Sort using function: "
  5709. obarray 'fboundp t nil nil))
  5710. (setq getkey-func (intern getkey-func)))
  5711. (and (= (downcase sorting-type) ?r)
  5712. (setq property
  5713. (org-ido-completing-read "Property: "
  5714. (mapcar 'list (org-buffer-property-keys t))
  5715. nil t))))
  5716. (message "Sorting entries...")
  5717. (save-restriction
  5718. (narrow-to-region start end)
  5719. (let ((dcst (downcase sorting-type))
  5720. (case-fold-search nil)
  5721. (now (current-time)))
  5722. (sort-subr
  5723. (/= dcst sorting-type)
  5724. ;; This function moves to the beginning character of the "record" to
  5725. ;; be sorted.
  5726. (if plain-list-p
  5727. (lambda nil
  5728. (if (org-at-item-p) t (goto-char (point-max))))
  5729. (lambda nil
  5730. (if (re-search-forward re nil t)
  5731. (goto-char (match-beginning 0))
  5732. (goto-char (point-max)))))
  5733. ;; This function moves to the last character of the "record" being
  5734. ;; sorted.
  5735. (if plain-list-p
  5736. 'org-end-of-item
  5737. (lambda nil
  5738. (save-match-data
  5739. (condition-case nil
  5740. (outline-forward-same-level 1)
  5741. (error
  5742. (goto-char (point-max)))))))
  5743. ;; This function returns the value that gets sorted against.
  5744. (if plain-list-p
  5745. (lambda nil
  5746. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5747. (cond
  5748. ((= dcst ?n)
  5749. (string-to-number (buffer-substring (match-end 0)
  5750. (point-at-eol))))
  5751. ((= dcst ?a)
  5752. (buffer-substring (match-end 0) (point-at-eol)))
  5753. ((= dcst ?t)
  5754. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5755. (re-search-forward org-ts-regexp-both
  5756. (point-at-eol) t))
  5757. (org-time-string-to-seconds (match-string 0))
  5758. (time-to-seconds now)))
  5759. ((= dcst ?f)
  5760. (if getkey-func
  5761. (progn
  5762. (setq tmp (funcall getkey-func))
  5763. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5764. tmp)
  5765. (error "Invalid key function `%s'" getkey-func)))
  5766. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5767. (lambda nil
  5768. (cond
  5769. ((= dcst ?n)
  5770. (if (looking-at org-complex-heading-regexp)
  5771. (string-to-number (match-string 4))
  5772. nil))
  5773. ((= dcst ?a)
  5774. (if (looking-at org-complex-heading-regexp)
  5775. (funcall case-func (match-string 4))
  5776. nil))
  5777. ((= dcst ?t)
  5778. (let ((end (save-excursion (outline-next-heading) (point))))
  5779. (if (or (re-search-forward org-ts-regexp end t)
  5780. (re-search-forward org-ts-regexp-both end t))
  5781. (org-time-string-to-seconds (match-string 0))
  5782. (time-to-seconds now))))
  5783. ((= dcst ?c)
  5784. (let ((end (save-excursion (outline-next-heading) (point))))
  5785. (if (re-search-forward
  5786. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5787. end t)
  5788. (org-time-string-to-seconds (match-string 0))
  5789. (time-to-seconds now))))
  5790. ((= dcst ?s)
  5791. (let ((end (save-excursion (outline-next-heading) (point))))
  5792. (if (re-search-forward org-scheduled-time-regexp end t)
  5793. (org-time-string-to-seconds (match-string 1))
  5794. (time-to-seconds now))))
  5795. ((= dcst ?d)
  5796. (let ((end (save-excursion (outline-next-heading) (point))))
  5797. (if (re-search-forward org-deadline-time-regexp end t)
  5798. (org-time-string-to-seconds (match-string 1))
  5799. (time-to-seconds now))))
  5800. ((= dcst ?p)
  5801. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5802. (string-to-char (match-string 2))
  5803. org-default-priority))
  5804. ((= dcst ?r)
  5805. (or (org-entry-get nil property) ""))
  5806. ((= dcst ?o)
  5807. (if (looking-at org-complex-heading-regexp)
  5808. (- 9999 (length (member (match-string 2)
  5809. org-todo-keywords-1)))))
  5810. ((= dcst ?f)
  5811. (if getkey-func
  5812. (progn
  5813. (setq tmp (funcall getkey-func))
  5814. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5815. tmp)
  5816. (error "Invalid key function `%s'" getkey-func)))
  5817. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5818. nil
  5819. (cond
  5820. ((= dcst ?a) 'string<)
  5821. ((= dcst ?f) compare-func)
  5822. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  5823. (t nil)))))
  5824. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5825. (message "Sorting entries...done")))
  5826. (defun org-do-sort (table what &optional with-case sorting-type)
  5827. "Sort TABLE of WHAT according to SORTING-TYPE.
  5828. The user will be prompted for the SORTING-TYPE if the call to this
  5829. function does not specify it. WHAT is only for the prompt, to indicate
  5830. what is being sorted. The sorting key will be extracted from
  5831. the car of the elements of the table.
  5832. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5833. (unless sorting-type
  5834. (message
  5835. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5836. what)
  5837. (setq sorting-type (read-char-exclusive)))
  5838. (let ((dcst (downcase sorting-type))
  5839. extractfun comparefun)
  5840. ;; Define the appropriate functions
  5841. (cond
  5842. ((= dcst ?n)
  5843. (setq extractfun 'string-to-number
  5844. comparefun (if (= dcst sorting-type) '< '>)))
  5845. ((= dcst ?a)
  5846. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5847. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5848. comparefun (if (= dcst sorting-type)
  5849. 'string<
  5850. (lambda (a b) (and (not (string< a b))
  5851. (not (string= a b)))))))
  5852. ((= dcst ?t)
  5853. (setq extractfun
  5854. (lambda (x)
  5855. (if (or (string-match org-ts-regexp x)
  5856. (string-match org-ts-regexp-both x))
  5857. (time-to-seconds
  5858. (org-time-string-to-time (match-string 0 x)))
  5859. 0))
  5860. comparefun (if (= dcst sorting-type) '< '>)))
  5861. (t (error "Invalid sorting type `%c'" sorting-type)))
  5862. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5863. table)
  5864. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5865. ;;; The orgstruct minor mode
  5866. ;; Define a minor mode which can be used in other modes in order to
  5867. ;; integrate the org-mode structure editing commands.
  5868. ;; This is really a hack, because the org-mode structure commands use
  5869. ;; keys which normally belong to the major mode. Here is how it
  5870. ;; works: The minor mode defines all the keys necessary to operate the
  5871. ;; structure commands, but wraps the commands into a function which
  5872. ;; tests if the cursor is currently at a headline or a plain list
  5873. ;; item. If that is the case, the structure command is used,
  5874. ;; temporarily setting many Org-mode variables like regular
  5875. ;; expressions for filling etc. However, when any of those keys is
  5876. ;; used at a different location, function uses `key-binding' to look
  5877. ;; up if the key has an associated command in another currently active
  5878. ;; keymap (minor modes, major mode, global), and executes that
  5879. ;; command. There might be problems if any of the keys is otherwise
  5880. ;; used as a prefix key.
  5881. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5882. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5883. ;; addresses this by checking explicitly for both bindings.
  5884. (defvar orgstruct-mode-map (make-sparse-keymap)
  5885. "Keymap for the minor `orgstruct-mode'.")
  5886. (defvar org-local-vars nil
  5887. "List of local variables, for use by `orgstruct-mode'")
  5888. ;;;###autoload
  5889. (define-minor-mode orgstruct-mode
  5890. "Toggle the minor more `orgstruct-mode'.
  5891. This mode is for using Org-mode structure commands in other modes.
  5892. The following key behave as if Org-mode was active, if the cursor
  5893. is on a headline, or on a plain list item (both in the definition
  5894. of Org-mode).
  5895. M-up Move entry/item up
  5896. M-down Move entry/item down
  5897. M-left Promote
  5898. M-right Demote
  5899. M-S-up Move entry/item up
  5900. M-S-down Move entry/item down
  5901. M-S-left Promote subtree
  5902. M-S-right Demote subtree
  5903. M-q Fill paragraph and items like in Org-mode
  5904. C-c ^ Sort entries
  5905. C-c - Cycle list bullet
  5906. TAB Cycle item visibility
  5907. M-RET Insert new heading/item
  5908. S-M-RET Insert new TODO heading / Checkbox item
  5909. C-c C-c Set tags / toggle checkbox"
  5910. nil " OrgStruct" nil
  5911. (org-load-modules-maybe)
  5912. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5913. ;;;###autoload
  5914. (defun turn-on-orgstruct ()
  5915. "Unconditionally turn on `orgstruct-mode'."
  5916. (orgstruct-mode 1))
  5917. (defun orgstruct++-mode (&optional arg)
  5918. "Toggle `orgstruct-mode', the enhanced version of it.
  5919. In addition to setting orgstruct-mode, this also exports all indentation and
  5920. autofilling variables from org-mode into the buffer. It will also
  5921. recognize item context in multiline items.
  5922. Note that turning off orgstruct-mode will *not* remove the
  5923. indentation/paragraph settings. This can only be done by refreshing the
  5924. major mode, for example with \[normal-mode]."
  5925. (interactive "P")
  5926. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5927. (if (< arg 1)
  5928. (orgstruct-mode -1)
  5929. (orgstruct-mode 1)
  5930. (let (var val)
  5931. (mapc
  5932. (lambda (x)
  5933. (when (string-match
  5934. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5935. (symbol-name (car x)))
  5936. (setq var (car x) val (nth 1 x))
  5937. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5938. org-local-vars)
  5939. (org-set-local 'orgstruct-is-++ t))))
  5940. (defvar orgstruct-is-++ nil
  5941. "Is orgstruct-mode in ++ version in the current-buffer?")
  5942. (make-variable-buffer-local 'orgstruct-is-++)
  5943. ;;;###autoload
  5944. (defun turn-on-orgstruct++ ()
  5945. "Unconditionally turn on `orgstruct++-mode'."
  5946. (orgstruct++-mode 1))
  5947. (defun orgstruct-error ()
  5948. "Error when there is no default binding for a structure key."
  5949. (interactive)
  5950. (error "This key has no function outside structure elements"))
  5951. (defun orgstruct-setup ()
  5952. "Setup orgstruct keymaps."
  5953. (let ((nfunc 0)
  5954. (bindings
  5955. (list
  5956. '([(meta up)] org-metaup)
  5957. '([(meta down)] org-metadown)
  5958. '([(meta left)] org-metaleft)
  5959. '([(meta right)] org-metaright)
  5960. '([(meta shift up)] org-shiftmetaup)
  5961. '([(meta shift down)] org-shiftmetadown)
  5962. '([(meta shift left)] org-shiftmetaleft)
  5963. '([(meta shift right)] org-shiftmetaright)
  5964. '([?\e (up)] org-metaup)
  5965. '([?\e (down)] org-metadown)
  5966. '([?\e (left)] org-metaleft)
  5967. '([?\e (right)] org-metaright)
  5968. '([?\e (shift up)] org-shiftmetaup)
  5969. '([?\e (shift down)] org-shiftmetadown)
  5970. '([?\e (shift left)] org-shiftmetaleft)
  5971. '([?\e (shift right)] org-shiftmetaright)
  5972. '([(shift up)] org-shiftup)
  5973. '([(shift down)] org-shiftdown)
  5974. '([(shift left)] org-shiftleft)
  5975. '([(shift right)] org-shiftright)
  5976. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5977. '("\M-q" fill-paragraph)
  5978. '("\C-c^" org-sort)
  5979. '("\C-c-" org-cycle-list-bullet)))
  5980. elt key fun cmd)
  5981. (while (setq elt (pop bindings))
  5982. (setq nfunc (1+ nfunc))
  5983. (setq key (org-key (car elt))
  5984. fun (nth 1 elt)
  5985. cmd (orgstruct-make-binding fun nfunc key))
  5986. (org-defkey orgstruct-mode-map key cmd))
  5987. ;; Special treatment needed for TAB and RET
  5988. (org-defkey orgstruct-mode-map [(tab)]
  5989. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5990. (org-defkey orgstruct-mode-map "\C-i"
  5991. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5992. (org-defkey orgstruct-mode-map "\M-\C-m"
  5993. (orgstruct-make-binding 'org-insert-heading 105
  5994. "\M-\C-m" [(meta return)]))
  5995. (org-defkey orgstruct-mode-map [(meta return)]
  5996. (orgstruct-make-binding 'org-insert-heading 106
  5997. [(meta return)] "\M-\C-m"))
  5998. (org-defkey orgstruct-mode-map [(shift meta return)]
  5999. (orgstruct-make-binding 'org-insert-todo-heading 107
  6000. [(meta return)] "\M-\C-m"))
  6001. (org-defkey orgstruct-mode-map "\e\C-m"
  6002. (orgstruct-make-binding 'org-insert-heading 108
  6003. "\e\C-m" [?\e (return)]))
  6004. (org-defkey orgstruct-mode-map [?\e (return)]
  6005. (orgstruct-make-binding 'org-insert-heading 109
  6006. [?\e (return)] "\e\C-m"))
  6007. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6008. (orgstruct-make-binding 'org-insert-todo-heading 110
  6009. [?\e (return)] "\e\C-m"))
  6010. (unless org-local-vars
  6011. (setq org-local-vars (org-get-local-variables)))
  6012. t))
  6013. (defun orgstruct-make-binding (fun n &rest keys)
  6014. "Create a function for binding in the structure minor mode.
  6015. FUN is the command to call inside a table. N is used to create a unique
  6016. command name. KEYS are keys that should be checked in for a command
  6017. to execute outside of tables."
  6018. (eval
  6019. (list 'defun
  6020. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6021. '(arg)
  6022. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6023. "Outside of structure, run the binding of `"
  6024. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6025. "'.")
  6026. '(interactive "p")
  6027. (list 'if
  6028. `(org-context-p 'headline 'item
  6029. (and orgstruct-is-++
  6030. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6031. 'item-body))
  6032. (list 'org-run-like-in-org-mode (list 'quote fun))
  6033. (list 'let '(orgstruct-mode)
  6034. (list 'call-interactively
  6035. (append '(or)
  6036. (mapcar (lambda (k)
  6037. (list 'key-binding k))
  6038. keys)
  6039. '('orgstruct-error))))))))
  6040. (defun org-context-p (&rest contexts)
  6041. "Check if local context is any of CONTEXTS.
  6042. Possible values in the list of contexts are `table', `headline', and `item'."
  6043. (let ((pos (point)))
  6044. (goto-char (point-at-bol))
  6045. (prog1 (or (and (memq 'table contexts)
  6046. (looking-at "[ \t]*|"))
  6047. (and (memq 'headline contexts)
  6048. ;;????????? (looking-at "\\*+"))
  6049. (looking-at outline-regexp))
  6050. (and (memq 'item contexts)
  6051. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6052. (and (memq 'item-body contexts)
  6053. (org-in-item-p)))
  6054. (goto-char pos))))
  6055. (defun org-get-local-variables ()
  6056. "Return a list of all local variables in an org-mode buffer."
  6057. (let (varlist)
  6058. (with-current-buffer (get-buffer-create "*Org tmp*")
  6059. (erase-buffer)
  6060. (org-mode)
  6061. (setq varlist (buffer-local-variables)))
  6062. (kill-buffer "*Org tmp*")
  6063. (delq nil
  6064. (mapcar
  6065. (lambda (x)
  6066. (setq x
  6067. (if (symbolp x)
  6068. (list x)
  6069. (list (car x) (list 'quote (cdr x)))))
  6070. (if (string-match
  6071. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6072. (symbol-name (car x)))
  6073. x nil))
  6074. varlist))))
  6075. ;;;###autoload
  6076. (defun org-run-like-in-org-mode (cmd)
  6077. "Run a command, pretending that the current buffer is in Org-mode.
  6078. This will temporarily bind local variables that are typically bound in
  6079. Org-mode to the values they have in Org-mode, and then interactively
  6080. call CMD."
  6081. (org-load-modules-maybe)
  6082. (unless org-local-vars
  6083. (setq org-local-vars (org-get-local-variables)))
  6084. (eval (list 'let org-local-vars
  6085. (list 'call-interactively (list 'quote cmd)))))
  6086. ;;;; Archiving
  6087. (defun org-get-category (&optional pos)
  6088. "Get the category applying to position POS."
  6089. (get-text-property (or pos (point)) 'org-category))
  6090. (defun org-refresh-category-properties ()
  6091. "Refresh category text properties in the buffer."
  6092. (let ((def-cat (cond
  6093. ((null org-category)
  6094. (if buffer-file-name
  6095. (file-name-sans-extension
  6096. (file-name-nondirectory buffer-file-name))
  6097. "???"))
  6098. ((symbolp org-category) (symbol-name org-category))
  6099. (t org-category)))
  6100. beg end cat pos optionp)
  6101. (org-unmodified
  6102. (save-excursion
  6103. (save-restriction
  6104. (widen)
  6105. (goto-char (point-min))
  6106. (put-text-property (point) (point-max) 'org-category def-cat)
  6107. (while (re-search-forward
  6108. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6109. (setq pos (match-end 0)
  6110. optionp (equal (char-after (match-beginning 0)) ?#)
  6111. cat (org-trim (match-string 2)))
  6112. (if optionp
  6113. (setq beg (point-at-bol) end (point-max))
  6114. (org-back-to-heading t)
  6115. (setq beg (point) end (org-end-of-subtree t t)))
  6116. (put-text-property beg end 'org-category cat)
  6117. (goto-char pos)))))))
  6118. ;;;; Link Stuff
  6119. ;;; Link abbreviations
  6120. (defun org-link-expand-abbrev (link)
  6121. "Apply replacements as defined in `org-link-abbrev-alist."
  6122. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6123. (let* ((key (match-string 1 link))
  6124. (as (or (assoc key org-link-abbrev-alist-local)
  6125. (assoc key org-link-abbrev-alist)))
  6126. (tag (and (match-end 2) (match-string 3 link)))
  6127. rpl)
  6128. (if (not as)
  6129. link
  6130. (setq rpl (cdr as))
  6131. (cond
  6132. ((symbolp rpl) (funcall rpl tag))
  6133. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6134. ((string-match "%h" rpl)
  6135. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6136. (t (concat rpl tag)))))
  6137. link))
  6138. ;;; Storing and inserting links
  6139. (defvar org-insert-link-history nil
  6140. "Minibuffer history for links inserted with `org-insert-link'.")
  6141. (defvar org-stored-links nil
  6142. "Contains the links stored with `org-store-link'.")
  6143. (defvar org-store-link-plist nil
  6144. "Plist with info about the most recently link created with `org-store-link'.")
  6145. (defvar org-link-protocols nil
  6146. "Link protocols added to Org-mode using `org-add-link-type'.")
  6147. (defvar org-store-link-functions nil
  6148. "List of functions that are called to create and store a link.
  6149. Each function will be called in turn until one returns a non-nil
  6150. value. Each function should check if it is responsible for creating
  6151. this link (for example by looking at the major mode).
  6152. If not, it must exit and return nil.
  6153. If yes, it should return a non-nil value after a calling
  6154. `org-store-link-props' with a list of properties and values.
  6155. Special properties are:
  6156. :type The link prefix. like \"http\". This must be given.
  6157. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6158. This is obligatory as well.
  6159. :description Optional default description for the second pair
  6160. of brackets in an Org-mode link. The user can still change
  6161. this when inserting this link into an Org-mode buffer.
  6162. In addition to these, any additional properties can be specified
  6163. and then used in remember templates.")
  6164. (defun org-add-link-type (type &optional follow export)
  6165. "Add TYPE to the list of `org-link-types'.
  6166. Re-compute all regular expressions depending on `org-link-types'
  6167. FOLLOW and EXPORT are two functions.
  6168. FOLLOW should take the link path as the single argument and do whatever
  6169. is necessary to follow the link, for example find a file or display
  6170. a mail message.
  6171. EXPORT should format the link path for export to one of the export formats.
  6172. It should be a function accepting three arguments:
  6173. path the path of the link, the text after the prefix (like \"http:\")
  6174. desc the description of the link, if any, nil if there was no description
  6175. format the export format, a symbol like `html' or `latex'.
  6176. The function may use the FORMAT information to return different values
  6177. depending on the format. The return value will be put literally into
  6178. the exported file.
  6179. Org-mode has a built-in default for exporting links. If you are happy with
  6180. this default, there is no need to define an export function for the link
  6181. type. For a simple example of an export function, see `org-bbdb.el'."
  6182. (add-to-list 'org-link-types type t)
  6183. (org-make-link-regexps)
  6184. (if (assoc type org-link-protocols)
  6185. (setcdr (assoc type org-link-protocols) (list follow export))
  6186. (push (list type follow export) org-link-protocols)))
  6187. ;;;###autoload
  6188. (defun org-store-link (arg)
  6189. "\\<org-mode-map>Store an org-link to the current location.
  6190. This link is added to `org-stored-links' and can later be inserted
  6191. into an org-buffer with \\[org-insert-link].
  6192. For some link types, a prefix arg is interpreted:
  6193. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6194. For file links, arg negates `org-context-in-file-links'."
  6195. (interactive "P")
  6196. (org-load-modules-maybe)
  6197. (setq org-store-link-plist nil) ; reset
  6198. (let (link cpltxt desc description search txt custom-id)
  6199. (cond
  6200. ((run-hook-with-args-until-success 'org-store-link-functions)
  6201. (setq link (plist-get org-store-link-plist :link)
  6202. desc (or (plist-get org-store-link-plist :description) link)))
  6203. ((equal (buffer-name) "*Org Edit Src Example*")
  6204. (let (label gc)
  6205. (while (or (not label)
  6206. (save-excursion
  6207. (save-restriction
  6208. (widen)
  6209. (goto-char (point-min))
  6210. (re-search-forward
  6211. (regexp-quote (format org-coderef-label-format label))
  6212. nil t))))
  6213. (when label (message "Label exists already") (sit-for 2))
  6214. (setq label (read-string "Code line label: " label)))
  6215. (end-of-line 1)
  6216. (setq link (format org-coderef-label-format label))
  6217. (setq gc (- 79 (length link)))
  6218. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6219. (insert link)
  6220. (setq link (concat "(" label ")") desc nil)))
  6221. ((eq major-mode 'calendar-mode)
  6222. (let ((cd (calendar-cursor-to-date)))
  6223. (setq link
  6224. (format-time-string
  6225. (car org-time-stamp-formats)
  6226. (apply 'encode-time
  6227. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6228. nil nil nil))))
  6229. (org-store-link-props :type "calendar" :date cd)))
  6230. ((eq major-mode 'w3-mode)
  6231. (setq cpltxt (if (and (buffer-name)
  6232. (not (string-match "Untitled" (buffer-name))))
  6233. (buffer-name)
  6234. (url-view-url t))
  6235. link (org-make-link (url-view-url t)))
  6236. (org-store-link-props :type "w3" :url (url-view-url t)))
  6237. ((eq major-mode 'w3m-mode)
  6238. (setq cpltxt (or w3m-current-title w3m-current-url)
  6239. link (org-make-link w3m-current-url))
  6240. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6241. ((setq search (run-hook-with-args-until-success
  6242. 'org-create-file-search-functions))
  6243. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6244. "::" search))
  6245. (setq cpltxt (or description link)))
  6246. ((eq major-mode 'image-mode)
  6247. (setq cpltxt (concat "file:"
  6248. (abbreviate-file-name buffer-file-name))
  6249. link (org-make-link cpltxt))
  6250. (org-store-link-props :type "image" :file buffer-file-name))
  6251. ((eq major-mode 'dired-mode)
  6252. ;; link to the file in the current line
  6253. (setq cpltxt (concat "file:"
  6254. (abbreviate-file-name
  6255. (expand-file-name
  6256. (dired-get-filename nil t))))
  6257. link (org-make-link cpltxt)))
  6258. ((and buffer-file-name (org-mode-p))
  6259. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6260. (cond
  6261. ((org-in-regexp "<<\\(.*?\\)>>")
  6262. (setq cpltxt
  6263. (concat "file:"
  6264. (abbreviate-file-name buffer-file-name)
  6265. "::" (match-string 1))
  6266. link (org-make-link cpltxt)))
  6267. ((and (featurep 'org-id)
  6268. (or (eq org-link-to-org-use-id t)
  6269. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6270. (interactive-p))
  6271. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6272. (interactive-p)
  6273. (not custom-id))
  6274. (and org-link-to-org-use-id
  6275. (condition-case nil
  6276. (org-entry-get nil "ID")
  6277. (error nil)))))
  6278. ;; We can make a link using the ID.
  6279. (setq link (condition-case nil
  6280. (prog1 (org-id-store-link)
  6281. (setq desc (plist-get org-store-link-plist
  6282. :description)))
  6283. (error
  6284. ;; probably before first headline, link to file only
  6285. (concat "file:"
  6286. (abbreviate-file-name buffer-file-name))))))
  6287. (t
  6288. ;; Just link to current headline
  6289. (setq cpltxt (concat "file:"
  6290. (abbreviate-file-name buffer-file-name)))
  6291. ;; Add a context search string
  6292. (when (org-xor org-context-in-file-links arg)
  6293. (setq txt (cond
  6294. ((org-on-heading-p) nil)
  6295. ((org-region-active-p)
  6296. (buffer-substring (region-beginning) (region-end)))
  6297. (t nil)))
  6298. (when (or (null txt) (string-match "\\S-" txt))
  6299. (setq cpltxt
  6300. (concat cpltxt "::"
  6301. (condition-case nil
  6302. (org-make-org-heading-search-string txt)
  6303. (error "")))
  6304. desc "NONE")))
  6305. (if (string-match "::\\'" cpltxt)
  6306. (setq cpltxt (substring cpltxt 0 -2)))
  6307. (setq link (org-make-link cpltxt)))))
  6308. ((buffer-file-name (buffer-base-buffer))
  6309. ;; Just link to this file here.
  6310. (setq cpltxt (concat "file:"
  6311. (abbreviate-file-name
  6312. (buffer-file-name (buffer-base-buffer)))))
  6313. ;; Add a context string
  6314. (when (org-xor org-context-in-file-links arg)
  6315. (setq txt (if (org-region-active-p)
  6316. (buffer-substring (region-beginning) (region-end))
  6317. (buffer-substring (point-at-bol) (point-at-eol))))
  6318. ;; Only use search option if there is some text.
  6319. (when (string-match "\\S-" txt)
  6320. (setq cpltxt
  6321. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6322. desc "NONE")))
  6323. (setq link (org-make-link cpltxt)))
  6324. ((interactive-p)
  6325. (error "Cannot link to a buffer which is not visiting a file"))
  6326. (t (setq link nil)))
  6327. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6328. (setq link (or link cpltxt)
  6329. desc (or desc cpltxt))
  6330. (if (equal desc "NONE") (setq desc nil))
  6331. (if (and (interactive-p) link)
  6332. (progn
  6333. (setq org-stored-links
  6334. (cons (list link desc) org-stored-links))
  6335. (message "Stored: %s" (or desc link))
  6336. (when custom-id
  6337. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6338. "::#" custom-id))
  6339. (setq org-stored-links
  6340. (cons (list link desc) org-stored-links))))
  6341. (and link (org-make-link-string link desc)))))
  6342. (defun org-store-link-props (&rest plist)
  6343. "Store link properties, extract names and addresses."
  6344. (let (x adr)
  6345. (when (setq x (plist-get plist :from))
  6346. (setq adr (mail-extract-address-components x))
  6347. (setq plist (plist-put plist :fromname (car adr)))
  6348. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6349. (when (setq x (plist-get plist :to))
  6350. (setq adr (mail-extract-address-components x))
  6351. (setq plist (plist-put plist :toname (car adr)))
  6352. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6353. (let ((from (plist-get plist :from))
  6354. (to (plist-get plist :to)))
  6355. (when (and from to org-from-is-user-regexp)
  6356. (setq plist
  6357. (plist-put plist :fromto
  6358. (if (string-match org-from-is-user-regexp from)
  6359. (concat "to %t")
  6360. (concat "from %f"))))))
  6361. (setq org-store-link-plist plist))
  6362. (defun org-add-link-props (&rest plist)
  6363. "Add these properties to the link property list."
  6364. (let (key value)
  6365. (while plist
  6366. (setq key (pop plist) value (pop plist))
  6367. (setq org-store-link-plist
  6368. (plist-put org-store-link-plist key value)))))
  6369. (defun org-email-link-description (&optional fmt)
  6370. "Return the description part of an email link.
  6371. This takes information from `org-store-link-plist' and formats it
  6372. according to FMT (default from `org-email-link-description-format')."
  6373. (setq fmt (or fmt org-email-link-description-format))
  6374. (let* ((p org-store-link-plist)
  6375. (to (plist-get p :toaddress))
  6376. (from (plist-get p :fromaddress))
  6377. (table
  6378. (list
  6379. (cons "%c" (plist-get p :fromto))
  6380. (cons "%F" (plist-get p :from))
  6381. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6382. (cons "%T" (plist-get p :to))
  6383. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6384. (cons "%s" (plist-get p :subject))
  6385. (cons "%m" (plist-get p :message-id)))))
  6386. (when (string-match "%c" fmt)
  6387. ;; Check if the user wrote this message
  6388. (if (and org-from-is-user-regexp from to
  6389. (save-match-data (string-match org-from-is-user-regexp from)))
  6390. (setq fmt (replace-match "to %t" t t fmt))
  6391. (setq fmt (replace-match "from %f" t t fmt))))
  6392. (org-replace-escapes fmt table)))
  6393. (defun org-make-org-heading-search-string (&optional string heading)
  6394. "Make search string for STRING or current headline."
  6395. (interactive)
  6396. (let ((s (or string (org-get-heading))))
  6397. (unless (and string (not heading))
  6398. ;; We are using a headline, clean up garbage in there.
  6399. (if (string-match org-todo-regexp s)
  6400. (setq s (replace-match "" t t s)))
  6401. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6402. (setq s (replace-match "" t t s)))
  6403. (setq s (org-trim s))
  6404. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6405. org-comment-string "\\)") s)
  6406. (setq s (replace-match "" t t s)))
  6407. (while (string-match org-ts-regexp s)
  6408. (setq s (replace-match "" t t s))))
  6409. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6410. (setq s (replace-match " " t t s)))
  6411. (or string (setq s (concat "*" s))) ; Add * for headlines
  6412. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6413. (defun org-make-link (&rest strings)
  6414. "Concatenate STRINGS."
  6415. (apply 'concat strings))
  6416. (defun org-make-link-string (link &optional description)
  6417. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6418. (unless (string-match "\\S-" link)
  6419. (error "Empty link"))
  6420. (when (stringp description)
  6421. ;; Remove brackets from the description, they are fatal.
  6422. (while (string-match "\\[" description)
  6423. (setq description (replace-match "{" t t description)))
  6424. (while (string-match "\\]" description)
  6425. (setq description (replace-match "}" t t description))))
  6426. (when (equal (org-link-escape link) description)
  6427. ;; No description needed, it is identical
  6428. (setq description nil))
  6429. (when (and (not description)
  6430. (not (equal link (org-link-escape link))))
  6431. (setq description (org-extract-attributes link)))
  6432. (concat "[[" (org-link-escape link) "]"
  6433. (if description (concat "[" description "]") "")
  6434. "]"))
  6435. (defconst org-link-escape-chars
  6436. '((?\ . "%20")
  6437. (?\[ . "%5B")
  6438. (?\] . "%5D")
  6439. (?\340 . "%E0") ; `a
  6440. (?\342 . "%E2") ; ^a
  6441. (?\347 . "%E7") ; ,c
  6442. (?\350 . "%E8") ; `e
  6443. (?\351 . "%E9") ; 'e
  6444. (?\352 . "%EA") ; ^e
  6445. (?\356 . "%EE") ; ^i
  6446. (?\364 . "%F4") ; ^o
  6447. (?\371 . "%F9") ; `u
  6448. (?\373 . "%FB") ; ^u
  6449. (?\; . "%3B")
  6450. (?? . "%3F")
  6451. (?= . "%3D")
  6452. (?+ . "%2B")
  6453. )
  6454. "Association list of escapes for some characters problematic in links.
  6455. This is the list that is used for internal purposes.")
  6456. (defvar org-url-encoding-use-url-hexify nil)
  6457. (defconst org-link-escape-chars-browser
  6458. '((?\ . "%20")) ; 32 for the SPC char
  6459. "Association list of escapes for some characters problematic in links.
  6460. This is the list that is used before handing over to the browser.")
  6461. (defun org-link-escape (text &optional table)
  6462. "Escape characters in TEXT that are problematic for links."
  6463. (if org-url-encoding-use-url-hexify
  6464. (url-hexify-string text)
  6465. (setq table (or table org-link-escape-chars))
  6466. (when text
  6467. (let ((re (mapconcat (lambda (x) (regexp-quote
  6468. (char-to-string (car x))))
  6469. table "\\|")))
  6470. (while (string-match re text)
  6471. (setq text
  6472. (replace-match
  6473. (cdr (assoc (string-to-char (match-string 0 text))
  6474. table))
  6475. t t text)))
  6476. text))))
  6477. (defun org-link-unescape (text &optional table)
  6478. "Reverse the action of `org-link-escape'."
  6479. (if org-url-encoding-use-url-hexify
  6480. (url-unhex-string text)
  6481. (setq table (or table org-link-escape-chars))
  6482. (when text
  6483. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6484. table "\\|")))
  6485. (while (string-match re text)
  6486. (setq text
  6487. (replace-match
  6488. (char-to-string (car (rassoc (match-string 0 text) table)))
  6489. t t text)))
  6490. text))))
  6491. (defun org-xor (a b)
  6492. "Exclusive or."
  6493. (if a (not b) b))
  6494. (defun org-fixup-message-id-for-http (s)
  6495. "Replace special characters in a message id, so it can be used in an http query."
  6496. (while (string-match "<" s)
  6497. (setq s (replace-match "%3C" t t s)))
  6498. (while (string-match ">" s)
  6499. (setq s (replace-match "%3E" t t s)))
  6500. (while (string-match "@" s)
  6501. (setq s (replace-match "%40" t t s)))
  6502. s)
  6503. ;;;###autoload
  6504. (defun org-insert-link-global ()
  6505. "Insert a link like Org-mode does.
  6506. This command can be called in any mode to insert a link in Org-mode syntax."
  6507. (interactive)
  6508. (org-load-modules-maybe)
  6509. (org-run-like-in-org-mode 'org-insert-link))
  6510. (defun org-insert-link (&optional complete-file link-location)
  6511. "Insert a link. At the prompt, enter the link.
  6512. Completion can be used to insert any of the link protocol prefixes like
  6513. http or ftp in use.
  6514. The history can be used to select a link previously stored with
  6515. `org-store-link'. When the empty string is entered (i.e. if you just
  6516. press RET at the prompt), the link defaults to the most recently
  6517. stored link. As SPC triggers completion in the minibuffer, you need to
  6518. use M-SPC or C-q SPC to force the insertion of a space character.
  6519. You will also be prompted for a description, and if one is given, it will
  6520. be displayed in the buffer instead of the link.
  6521. If there is already a link at point, this command will allow you to edit link
  6522. and description parts.
  6523. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6524. be selected using completion. The path to the file will be relative to the
  6525. current directory if the file is in the current directory or a subdirectory.
  6526. Otherwise, the link will be the absolute path as completed in the minibuffer
  6527. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6528. option `org-link-file-path-type'.
  6529. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6530. the current directory or below.
  6531. With three \\[universal-argument] prefixes, negate the meaning of
  6532. `org-keep-stored-link-after-insertion'.
  6533. If `org-make-link-description-function' is non-nil, this function will be
  6534. called with the link target, and the result will be the default
  6535. link description.
  6536. If the LINK-LOCATION parameter is non-nil, this value will be
  6537. used as the link location instead of reading one interactively."
  6538. (interactive "P")
  6539. (let* ((wcf (current-window-configuration))
  6540. (region (if (org-region-active-p)
  6541. (buffer-substring (region-beginning) (region-end))))
  6542. (remove (and region (list (region-beginning) (region-end))))
  6543. (desc region)
  6544. tmphist ; byte-compile incorrectly complains about this
  6545. (link link-location)
  6546. entry file all-prefixes)
  6547. (cond
  6548. (link-location) ; specified by arg, just use it.
  6549. ((org-in-regexp org-bracket-link-regexp 1)
  6550. ;; We do have a link at point, and we are going to edit it.
  6551. (setq remove (list (match-beginning 0) (match-end 0)))
  6552. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6553. (setq link (read-string "Link: "
  6554. (org-link-unescape
  6555. (org-match-string-no-properties 1)))))
  6556. ((or (org-in-regexp org-angle-link-re)
  6557. (org-in-regexp org-plain-link-re))
  6558. ;; Convert to bracket link
  6559. (setq remove (list (match-beginning 0) (match-end 0))
  6560. link (read-string "Link: "
  6561. (org-remove-angle-brackets (match-string 0)))))
  6562. ((member complete-file '((4) (16)))
  6563. ;; Completing read for file names.
  6564. (setq link (org-file-complete-link complete-file)))
  6565. (t
  6566. ;; Read link, with completion for stored links.
  6567. (with-output-to-temp-buffer "*Org Links*"
  6568. (princ "Insert a link.
  6569. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6570. (when org-stored-links
  6571. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6572. (princ (mapconcat
  6573. (lambda (x)
  6574. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6575. (reverse org-stored-links) "\n"))))
  6576. (let ((cw (selected-window)))
  6577. (select-window (get-buffer-window "*Org Links*"))
  6578. (setq truncate-lines t)
  6579. (unless (pos-visible-in-window-p (point-max))
  6580. (org-fit-window-to-buffer))
  6581. (and (window-live-p cw) (select-window cw)))
  6582. ;; Fake a link history, containing the stored links.
  6583. (setq tmphist (append (mapcar 'car org-stored-links)
  6584. org-insert-link-history))
  6585. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6586. (mapcar 'car org-link-abbrev-alist)
  6587. org-link-types))
  6588. (unwind-protect
  6589. (progn
  6590. (setq link
  6591. (let ((org-completion-use-ido nil))
  6592. (org-completing-read
  6593. "Link: "
  6594. (mapcar (lambda (x) (list (concat x ":")))
  6595. all-prefixes)
  6596. nil nil nil
  6597. 'tmphist
  6598. (or (car (car org-stored-links))))))
  6599. (if (or (member link all-prefixes)
  6600. (and (equal ":" (substring link -1))
  6601. (member (substring link 0 -1) all-prefixes)
  6602. (setq link (substring link 0 -1))))
  6603. (setq link (org-link-try-special-completion link))))
  6604. (set-window-configuration wcf)
  6605. (kill-buffer "*Org Links*"))
  6606. (setq entry (assoc link org-stored-links))
  6607. (or entry (push link org-insert-link-history))
  6608. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6609. (not org-keep-stored-link-after-insertion))
  6610. (setq org-stored-links (delq (assoc link org-stored-links)
  6611. org-stored-links)))
  6612. (setq desc (or desc (nth 1 entry)))))
  6613. (if (string-match org-plain-link-re link)
  6614. ;; URL-like link, normalize the use of angular brackets.
  6615. (setq link (org-make-link (org-remove-angle-brackets link))))
  6616. ;; Check if we are linking to the current file with a search option
  6617. ;; If yes, simplify the link by using only the search option.
  6618. (when (and buffer-file-name
  6619. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6620. (let* ((path (match-string 1 link))
  6621. (case-fold-search nil)
  6622. (search (match-string 2 link)))
  6623. (save-match-data
  6624. (if (equal (file-truename buffer-file-name) (file-truename path))
  6625. ;; We are linking to this same file, with a search option
  6626. (setq link search)))))
  6627. ;; Check if we can/should use a relative path. If yes, simplify the link
  6628. (when (string-match "^file:\\(.*\\)" link)
  6629. (let* ((path (match-string 1 link))
  6630. (origpath path)
  6631. (case-fold-search nil))
  6632. (cond
  6633. ((or (eq org-link-file-path-type 'absolute)
  6634. (equal complete-file '(16)))
  6635. (setq path (abbreviate-file-name (expand-file-name path))))
  6636. ((eq org-link-file-path-type 'noabbrev)
  6637. (setq path (expand-file-name path)))
  6638. ((eq org-link-file-path-type 'relative)
  6639. (setq path (file-relative-name path)))
  6640. (t
  6641. (save-match-data
  6642. (if (string-match (concat "^" (regexp-quote
  6643. (file-name-as-directory
  6644. (expand-file-name "."))))
  6645. (expand-file-name path))
  6646. ;; We are linking a file with relative path name.
  6647. (setq path (substring (expand-file-name path)
  6648. (match-end 0)))
  6649. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6650. (setq link (concat "file:" path))
  6651. (if (equal desc origpath)
  6652. (setq desc path))))
  6653. (if org-make-link-description-function
  6654. (setq desc (funcall org-make-link-description-function link desc)))
  6655. (setq desc (read-string "Description: " desc))
  6656. (unless (string-match "\\S-" desc) (setq desc nil))
  6657. (if remove (apply 'delete-region remove))
  6658. (insert (org-make-link-string link desc))))
  6659. (defun org-link-try-special-completion (type)
  6660. "If there is completion support for link type TAPE, offer it."
  6661. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6662. (if (functionp fun)
  6663. (funcall fun)
  6664. (read-string "Link (no completion support): " (concat type ":")))))
  6665. (defun org-file-complete-link (&optional arg)
  6666. "Create a file link using completion."
  6667. (let (file link)
  6668. (setq file (read-file-name "File: "))
  6669. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6670. (pwd1 (file-name-as-directory (abbreviate-file-name
  6671. (expand-file-name ".")))))
  6672. (cond
  6673. ((equal arg '(16))
  6674. (setq link (org-make-link
  6675. "file:"
  6676. (abbreviate-file-name (expand-file-name file)))))
  6677. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6678. (setq link (org-make-link "file:" (match-string 1 file))))
  6679. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6680. (expand-file-name file))
  6681. (setq link (org-make-link
  6682. "file:" (match-string 1 (expand-file-name file)))))
  6683. (t (setq link (org-make-link "file:" file)))))
  6684. link))
  6685. (defun org-completing-read (&rest args)
  6686. "Completing-read with SPACE being a normal character."
  6687. (let ((minibuffer-local-completion-map
  6688. (copy-keymap minibuffer-local-completion-map)))
  6689. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6690. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6691. (apply 'org-ido-completing-read args)))
  6692. (defun org-completing-read-no-ido (&rest args)
  6693. (let (org-completion-use-ido)
  6694. (apply 'org-completing-read args)))
  6695. (defun org-ido-completing-read (&rest args)
  6696. "Completing-read using `ido-mode' speedups if available"
  6697. (if (and org-completion-use-ido
  6698. (fboundp 'ido-completing-read)
  6699. (boundp 'ido-mode) ido-mode
  6700. (listp (second args)))
  6701. (let ((ido-enter-matching-directory nil))
  6702. (apply 'ido-completing-read (concat (car args))
  6703. (mapcar (lambda (x) (car x)) (nth 1 args))
  6704. (cddr args)))
  6705. (apply 'completing-read args)))
  6706. (defun org-extract-attributes (s)
  6707. "Extract the attributes cookie from a string and set as text property."
  6708. (let (a attr (start 0) key value)
  6709. (save-match-data
  6710. (when (string-match "{{\\([^}]+\\)}}$" s)
  6711. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6712. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6713. (setq key (match-string 1 a) value (match-string 2 a)
  6714. start (match-end 0)
  6715. attr (plist-put attr (intern key) value))))
  6716. (org-add-props s nil 'org-attr attr))
  6717. s))
  6718. (defun org-extract-attributes-from-string (tag)
  6719. (let (key value attr)
  6720. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6721. (setq key (match-string 1 tag) value (match-string 2 tag)
  6722. tag (replace-match "" t t tag)
  6723. attr (plist-put attr (intern key) value)))
  6724. (cons tag attr)))
  6725. (defun org-attributes-to-string (plist)
  6726. "Format a property list into an HTML attribute list."
  6727. (let ((s "") key value)
  6728. (while plist
  6729. (setq key (pop plist) value (pop plist))
  6730. (and value
  6731. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6732. s))
  6733. ;;; Opening/following a link
  6734. (defvar org-link-search-failed nil)
  6735. (defun org-next-link ()
  6736. "Move forward to the next link.
  6737. If the link is in hidden text, expose it."
  6738. (interactive)
  6739. (when (and org-link-search-failed (eq this-command last-command))
  6740. (goto-char (point-min))
  6741. (message "Link search wrapped back to beginning of buffer"))
  6742. (setq org-link-search-failed nil)
  6743. (let* ((pos (point))
  6744. (ct (org-context))
  6745. (a (assoc :link ct)))
  6746. (if a (goto-char (nth 2 a)))
  6747. (if (re-search-forward org-any-link-re nil t)
  6748. (progn
  6749. (goto-char (match-beginning 0))
  6750. (if (org-invisible-p) (org-show-context)))
  6751. (goto-char pos)
  6752. (setq org-link-search-failed t)
  6753. (error "No further link found"))))
  6754. (defun org-previous-link ()
  6755. "Move backward to the previous link.
  6756. If the link is in hidden text, expose it."
  6757. (interactive)
  6758. (when (and org-link-search-failed (eq this-command last-command))
  6759. (goto-char (point-max))
  6760. (message "Link search wrapped back to end of buffer"))
  6761. (setq org-link-search-failed nil)
  6762. (let* ((pos (point))
  6763. (ct (org-context))
  6764. (a (assoc :link ct)))
  6765. (if a (goto-char (nth 1 a)))
  6766. (if (re-search-backward org-any-link-re nil t)
  6767. (progn
  6768. (goto-char (match-beginning 0))
  6769. (if (org-invisible-p) (org-show-context)))
  6770. (goto-char pos)
  6771. (setq org-link-search-failed t)
  6772. (error "No further link found"))))
  6773. (defun org-translate-link (s)
  6774. "Translate a link string if a translation function has been defined."
  6775. (if (and org-link-translation-function
  6776. (fboundp org-link-translation-function)
  6777. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6778. (progn
  6779. (setq s (funcall org-link-translation-function
  6780. (match-string 1) (match-string 2)))
  6781. (concat (car s) ":" (cdr s)))
  6782. s))
  6783. (defun org-translate-link-from-planner (type path)
  6784. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6785. This is still an experimental function, your mileage may vary."
  6786. (cond
  6787. ((member type '("http" "https" "news" "ftp"))
  6788. ;; standard Internet links are the same.
  6789. nil)
  6790. ((and (equal type "irc") (string-match "^//" path))
  6791. ;; Planner has two / at the beginning of an irc link, we have 1.
  6792. ;; We should have zero, actually....
  6793. (setq path (substring path 1)))
  6794. ((and (equal type "lisp") (string-match "^/" path))
  6795. ;; Planner has a slash, we do not.
  6796. (setq type "elisp" path (substring path 1)))
  6797. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6798. ;; A typical message link. Planner has the id after the fina slash,
  6799. ;; we separate it with a hash mark
  6800. (setq path (concat (match-string 1 path) "#"
  6801. (org-remove-angle-brackets (match-string 2 path)))))
  6802. )
  6803. (cons type path))
  6804. (defun org-find-file-at-mouse (ev)
  6805. "Open file link or URL at mouse."
  6806. (interactive "e")
  6807. (mouse-set-point ev)
  6808. (org-open-at-point 'in-emacs))
  6809. (defun org-open-at-mouse (ev)
  6810. "Open file link or URL at mouse."
  6811. (interactive "e")
  6812. (mouse-set-point ev)
  6813. (if (eq major-mode 'org-agenda-mode)
  6814. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6815. (org-open-at-point))
  6816. (defvar org-window-config-before-follow-link nil
  6817. "The window configuration before following a link.
  6818. This is saved in case the need arises to restore it.")
  6819. (defvar org-open-link-marker (make-marker)
  6820. "Marker pointing to the location where `org-open-at-point; was called.")
  6821. ;;;###autoload
  6822. (defun org-open-at-point-global ()
  6823. "Follow a link like Org-mode does.
  6824. This command can be called in any mode to follow a link that has
  6825. Org-mode syntax."
  6826. (interactive)
  6827. (org-run-like-in-org-mode 'org-open-at-point))
  6828. ;;;###autoload
  6829. (defun org-open-link-from-string (s &optional arg)
  6830. "Open a link in the string S, as if it was in Org-mode."
  6831. (interactive "sLink: \nP")
  6832. (let ((reference-buffer (current-buffer)))
  6833. (with-temp-buffer
  6834. (let ((org-inhibit-startup t))
  6835. (org-mode)
  6836. (insert s)
  6837. (goto-char (point-min))
  6838. (org-open-at-point arg reference-buffer)))))
  6839. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6840. "Open link at or after point.
  6841. If there is no link at point, this function will search forward up to
  6842. the end of the current line.
  6843. Normally, files will be opened by an appropriate application. If the
  6844. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6845. With a double prefix argument, try to open outside of Emacs, in the
  6846. application the system uses for this file type."
  6847. (interactive "P")
  6848. (org-load-modules-maybe)
  6849. (move-marker org-open-link-marker (point))
  6850. (setq org-window-config-before-follow-link (current-window-configuration))
  6851. (org-remove-occur-highlights nil nil t)
  6852. (cond
  6853. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6854. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6855. (org-footnote-action))
  6856. (t
  6857. (let (type path link line search (pos (point)))
  6858. (catch 'match
  6859. (save-excursion
  6860. (skip-chars-forward "^]\n\r")
  6861. (when (org-in-regexp org-bracket-link-regexp)
  6862. (setq link (org-extract-attributes
  6863. (org-link-unescape (org-match-string-no-properties 1))))
  6864. (while (string-match " *\n *" link)
  6865. (setq link (replace-match " " t t link)))
  6866. (setq link (org-link-expand-abbrev link))
  6867. (cond
  6868. ((or (file-name-absolute-p link)
  6869. (string-match "^\\.\\.?/" link))
  6870. (setq type "file" path link))
  6871. ((string-match org-link-re-with-space3 link)
  6872. (setq type (match-string 1 link) path (match-string 2 link)))
  6873. (t (setq type "thisfile" path link)))
  6874. (throw 'match t)))
  6875. (when (get-text-property (point) 'org-linked-text)
  6876. (setq type "thisfile"
  6877. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6878. (1+ (point)) (point))
  6879. path (buffer-substring
  6880. (previous-single-property-change pos 'org-linked-text)
  6881. (next-single-property-change pos 'org-linked-text)))
  6882. (throw 'match t))
  6883. (save-excursion
  6884. (when (or (org-in-regexp org-angle-link-re)
  6885. (org-in-regexp org-plain-link-re))
  6886. (setq type (match-string 1) path (match-string 2))
  6887. (throw 'match t)))
  6888. (save-excursion
  6889. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6890. (setq type "tags"
  6891. path (match-string 1))
  6892. (while (string-match ":" path)
  6893. (setq path (replace-match "+" t t path)))
  6894. (throw 'match t)))
  6895. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6896. (setq type "tree-match"
  6897. path (match-string 1))
  6898. (throw 'match t)))
  6899. (unless path
  6900. (error "No link found"))
  6901. ;; switch back to reference buffer
  6902. ;; needed when if called in a temporary buffer through
  6903. ;; org-open-link-from-string
  6904. (and reference-buffer (switch-to-buffer reference-buffer))
  6905. ;; Remove any trailing spaces in path
  6906. (if (string-match " +\\'" path)
  6907. (setq path (replace-match "" t t path)))
  6908. (if (and org-link-translation-function
  6909. (fboundp org-link-translation-function))
  6910. ;; Check if we need to translate the link
  6911. (let ((tmp (funcall org-link-translation-function type path)))
  6912. (setq type (car tmp) path (cdr tmp))))
  6913. (cond
  6914. ((assoc type org-link-protocols)
  6915. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6916. ((equal type "mailto")
  6917. (let ((cmd (car org-link-mailto-program))
  6918. (args (cdr org-link-mailto-program)) args1
  6919. (address path) (subject "") a)
  6920. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6921. (setq address (match-string 1 path)
  6922. subject (org-link-escape (match-string 2 path))))
  6923. (while args
  6924. (cond
  6925. ((not (stringp (car args))) (push (pop args) args1))
  6926. (t (setq a (pop args))
  6927. (if (string-match "%a" a)
  6928. (setq a (replace-match address t t a)))
  6929. (if (string-match "%s" a)
  6930. (setq a (replace-match subject t t a)))
  6931. (push a args1))))
  6932. (apply cmd (nreverse args1))))
  6933. ((member type '("http" "https" "ftp" "news"))
  6934. (browse-url (concat type ":" (org-link-escape
  6935. path org-link-escape-chars-browser))))
  6936. ((member type '("message"))
  6937. (browse-url (concat type ":" path)))
  6938. ((string= type "tags")
  6939. (org-tags-view in-emacs path))
  6940. ((string= type "thisfile")
  6941. (if in-emacs
  6942. (switch-to-buffer-other-window
  6943. (org-get-buffer-for-internal-link (current-buffer)))
  6944. (org-mark-ring-push))
  6945. (let ((cmd `(org-link-search
  6946. ,path
  6947. ,(cond ((equal in-emacs '(4)) 'occur)
  6948. ((equal in-emacs '(16)) 'org-occur)
  6949. (t nil))
  6950. ,pos)))
  6951. (condition-case nil (eval cmd)
  6952. (error (progn (widen) (eval cmd))))))
  6953. ((string= type "tree-match")
  6954. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6955. ((string= type "file")
  6956. (if (string-match "::\\([0-9]+\\)\\'" path)
  6957. (setq line (string-to-number (match-string 1 path))
  6958. path (substring path 0 (match-beginning 0)))
  6959. (if (string-match "::\\(.+\\)\\'" path)
  6960. (setq search (match-string 1 path)
  6961. path (substring path 0 (match-beginning 0)))))
  6962. (if (string-match "[*?{]" (file-name-nondirectory path))
  6963. (dired path)
  6964. (org-open-file path in-emacs line search)))
  6965. ((string= type "news")
  6966. (require 'org-gnus)
  6967. (org-gnus-follow-link path))
  6968. ((string= type "shell")
  6969. (let ((cmd path))
  6970. (if (or (not org-confirm-shell-link-function)
  6971. (funcall org-confirm-shell-link-function
  6972. (format "Execute \"%s\" in shell? "
  6973. (org-add-props cmd nil
  6974. 'face 'org-warning))))
  6975. (progn
  6976. (message "Executing %s" cmd)
  6977. (shell-command cmd))
  6978. (error "Abort"))))
  6979. ((string= type "elisp")
  6980. (let ((cmd path))
  6981. (if (or (not org-confirm-elisp-link-function)
  6982. (funcall org-confirm-elisp-link-function
  6983. (format "Execute \"%s\" as elisp? "
  6984. (org-add-props cmd nil
  6985. 'face 'org-warning))))
  6986. (message "%s => %s" cmd
  6987. (if (equal (string-to-char cmd) ?\()
  6988. (eval (read cmd))
  6989. (call-interactively (read cmd))))
  6990. (error "Abort"))))
  6991. (t
  6992. (browse-url-at-point))))))
  6993. (move-marker org-open-link-marker nil)
  6994. (run-hook-with-args 'org-follow-link-hook))
  6995. ;;;; Time estimates
  6996. (defun org-get-effort (&optional pom)
  6997. "Get the effort estimate for the current entry."
  6998. (org-entry-get pom org-effort-property))
  6999. ;;; File search
  7000. (defvar org-create-file-search-functions nil
  7001. "List of functions to construct the right search string for a file link.
  7002. These functions are called in turn with point at the location to
  7003. which the link should point.
  7004. A function in the hook should first test if it would like to
  7005. handle this file type, for example by checking the major-mode or
  7006. the file extension. If it decides not to handle this file, it
  7007. should just return nil to give other functions a chance. If it
  7008. does handle the file, it must return the search string to be used
  7009. when following the link. The search string will be part of the
  7010. file link, given after a double colon, and `org-open-at-point'
  7011. will automatically search for it. If special measures must be
  7012. taken to make the search successful, another function should be
  7013. added to the companion hook `org-execute-file-search-functions',
  7014. which see.
  7015. A function in this hook may also use `setq' to set the variable
  7016. `description' to provide a suggestion for the descriptive text to
  7017. be used for this link when it gets inserted into an Org-mode
  7018. buffer with \\[org-insert-link].")
  7019. (defvar org-execute-file-search-functions nil
  7020. "List of functions to execute a file search triggered by a link.
  7021. Functions added to this hook must accept a single argument, the
  7022. search string that was part of the file link, the part after the
  7023. double colon. The function must first check if it would like to
  7024. handle this search, for example by checking the major-mode or the
  7025. file extension. If it decides not to handle this search, it
  7026. should just return nil to give other functions a chance. If it
  7027. does handle the search, it must return a non-nil value to keep
  7028. other functions from trying.
  7029. Each function can access the current prefix argument through the
  7030. variable `current-prefix-argument'. Note that a single prefix is
  7031. used to force opening a link in Emacs, so it may be good to only
  7032. use a numeric or double prefix to guide the search function.
  7033. In case this is needed, a function in this hook can also restore
  7034. the window configuration before `org-open-at-point' was called using:
  7035. (set-window-configuration org-window-config-before-follow-link)")
  7036. (defun org-link-search (s &optional type avoid-pos)
  7037. "Search for a link search option.
  7038. If S is surrounded by forward slashes, it is interpreted as a
  7039. regular expression. In org-mode files, this will create an `org-occur'
  7040. sparse tree. In ordinary files, `occur' will be used to list matches.
  7041. If the current buffer is in `dired-mode', grep will be used to search
  7042. in all files. If AVOID-POS is given, ignore matches near that position."
  7043. (let ((case-fold-search t)
  7044. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7045. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7046. (append '(("") (" ") ("\t") ("\n"))
  7047. org-emphasis-alist)
  7048. "\\|") "\\)"))
  7049. (pos (point))
  7050. (pre nil) (post nil)
  7051. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7052. (cond
  7053. ;; First check if there are any special
  7054. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7055. ;; Now try the builtin stuff
  7056. ((and (equal (string-to-char s0) ?#)
  7057. (> (length s0) 1)
  7058. (save-excursion
  7059. (goto-char (point-min))
  7060. (and
  7061. (re-search-forward
  7062. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7063. (setq type 'dedicated
  7064. pos (match-beginning 0))))
  7065. ;; There is an exact target for this
  7066. (goto-char pos)
  7067. (org-back-to-heading t)))
  7068. ((save-excursion
  7069. (goto-char (point-min))
  7070. (and
  7071. (re-search-forward
  7072. (concat "<<" (regexp-quote s0) ">>") nil t)
  7073. (setq type 'dedicated
  7074. pos (match-beginning 0))))
  7075. ;; There is an exact target for this
  7076. (goto-char pos))
  7077. ((and (string-match "^(\\(.*\\))$" s0)
  7078. (save-excursion
  7079. (goto-char (point-min))
  7080. (and
  7081. (re-search-forward
  7082. (concat "[^[]" (regexp-quote
  7083. (format org-coderef-label-format
  7084. (match-string 1 s0))))
  7085. nil t)
  7086. (setq type 'dedicated
  7087. pos (1+ (match-beginning 0))))))
  7088. ;; There is a coderef target for this
  7089. (goto-char pos))
  7090. ((string-match "^/\\(.*\\)/$" s)
  7091. ;; A regular expression
  7092. (cond
  7093. ((org-mode-p)
  7094. (org-occur (match-string 1 s)))
  7095. ;;((eq major-mode 'dired-mode)
  7096. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7097. (t (org-do-occur (match-string 1 s)))))
  7098. (t
  7099. ;; A normal search strings
  7100. (when (equal (string-to-char s) ?*)
  7101. ;; Anchor on headlines, post may include tags.
  7102. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7103. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7104. s (substring s 1)))
  7105. (remove-text-properties
  7106. 0 (length s)
  7107. '(face nil mouse-face nil keymap nil fontified nil) s)
  7108. ;; Make a series of regular expressions to find a match
  7109. (setq words (org-split-string s "[ \n\r\t]+")
  7110. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7111. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7112. "\\)" markers)
  7113. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7114. re2a (concat "[ \t\r\n]" re2a_)
  7115. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7116. re4 (concat "[^a-zA-Z_]" re4_)
  7117. re1 (concat pre re2 post)
  7118. re3 (concat pre (if pre re4_ re4) post)
  7119. re5 (concat pre ".*" re4)
  7120. re2 (concat pre re2)
  7121. re2a (concat pre (if pre re2a_ re2a))
  7122. re4 (concat pre (if pre re4_ re4))
  7123. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7124. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7125. re5 "\\)"
  7126. ))
  7127. (cond
  7128. ((eq type 'org-occur) (org-occur reall))
  7129. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7130. (t (goto-char (point-min))
  7131. (setq type 'fuzzy)
  7132. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7133. (org-search-not-self 1 re1 nil t)
  7134. (org-search-not-self 1 re2 nil t)
  7135. (org-search-not-self 1 re2a nil t)
  7136. (org-search-not-self 1 re3 nil t)
  7137. (org-search-not-self 1 re4 nil t)
  7138. (org-search-not-self 1 re5 nil t)
  7139. )
  7140. (goto-char (match-beginning 1))
  7141. (goto-char pos)
  7142. (error "No match")))))
  7143. (t
  7144. ;; Normal string-search
  7145. (goto-char (point-min))
  7146. (if (search-forward s nil t)
  7147. (goto-char (match-beginning 0))
  7148. (error "No match"))))
  7149. (and (org-mode-p) (org-show-context 'link-search))
  7150. type))
  7151. (defun org-search-not-self (group &rest args)
  7152. "Execute `re-search-forward', but only accept matches that do not
  7153. enclose the position of `org-open-link-marker'."
  7154. (let ((m org-open-link-marker))
  7155. (catch 'exit
  7156. (while (apply 're-search-forward args)
  7157. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7158. (goto-char (match-end group))
  7159. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7160. (> (match-beginning 0) (marker-position m))
  7161. (< (match-end 0) (marker-position m)))
  7162. (save-match-data
  7163. (or (not (org-in-regexp
  7164. org-bracket-link-analytic-regexp 1))
  7165. (not (match-end 4)) ; no description
  7166. (and (<= (match-beginning 4) (point))
  7167. (>= (match-end 4) (point))))))
  7168. (throw 'exit (point))))))))
  7169. (defun org-get-buffer-for-internal-link (buffer)
  7170. "Return a buffer to be used for displaying the link target of internal links."
  7171. (cond
  7172. ((not org-display-internal-link-with-indirect-buffer)
  7173. buffer)
  7174. ((string-match "(Clone)$" (buffer-name buffer))
  7175. (message "Buffer is already a clone, not making another one")
  7176. ;; we also do not modify visibility in this case
  7177. buffer)
  7178. (t ; make a new indirect buffer for displaying the link
  7179. (let* ((bn (buffer-name buffer))
  7180. (ibn (concat bn "(Clone)"))
  7181. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7182. (with-current-buffer ib (org-overview))
  7183. ib))))
  7184. (defun org-do-occur (regexp &optional cleanup)
  7185. "Call the Emacs command `occur'.
  7186. If CLEANUP is non-nil, remove the printout of the regular expression
  7187. in the *Occur* buffer. This is useful if the regex is long and not useful
  7188. to read."
  7189. (occur regexp)
  7190. (when cleanup
  7191. (let ((cwin (selected-window)) win beg end)
  7192. (when (setq win (get-buffer-window "*Occur*"))
  7193. (select-window win))
  7194. (goto-char (point-min))
  7195. (when (re-search-forward "match[a-z]+" nil t)
  7196. (setq beg (match-end 0))
  7197. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7198. (setq end (1- (match-beginning 0)))))
  7199. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7200. (goto-char (point-min))
  7201. (select-window cwin))))
  7202. ;;; The mark ring for links jumps
  7203. (defvar org-mark-ring nil
  7204. "Mark ring for positions before jumps in Org-mode.")
  7205. (defvar org-mark-ring-last-goto nil
  7206. "Last position in the mark ring used to go back.")
  7207. ;; Fill and close the ring
  7208. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7209. (loop for i from 1 to org-mark-ring-length do
  7210. (push (make-marker) org-mark-ring))
  7211. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7212. org-mark-ring)
  7213. (defun org-mark-ring-push (&optional pos buffer)
  7214. "Put the current position or POS into the mark ring and rotate it."
  7215. (interactive)
  7216. (setq pos (or pos (point)))
  7217. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7218. (move-marker (car org-mark-ring)
  7219. (or pos (point))
  7220. (or buffer (current-buffer)))
  7221. (message "%s"
  7222. (substitute-command-keys
  7223. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7224. (defun org-mark-ring-goto (&optional n)
  7225. "Jump to the previous position in the mark ring.
  7226. With prefix arg N, jump back that many stored positions. When
  7227. called several times in succession, walk through the entire ring.
  7228. Org-mode commands jumping to a different position in the current file,
  7229. or to another Org-mode file, automatically push the old position
  7230. onto the ring."
  7231. (interactive "p")
  7232. (let (p m)
  7233. (if (eq last-command this-command)
  7234. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7235. (setq p org-mark-ring))
  7236. (setq org-mark-ring-last-goto p)
  7237. (setq m (car p))
  7238. (switch-to-buffer (marker-buffer m))
  7239. (goto-char m)
  7240. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7241. (defun org-remove-angle-brackets (s)
  7242. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7243. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7244. s)
  7245. (defun org-add-angle-brackets (s)
  7246. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7247. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7248. s)
  7249. (defun org-remove-double-quotes (s)
  7250. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7251. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7252. s)
  7253. ;;; Following specific links
  7254. (defun org-follow-timestamp-link ()
  7255. (cond
  7256. ((org-at-date-range-p t)
  7257. (let ((org-agenda-start-on-weekday)
  7258. (t1 (match-string 1))
  7259. (t2 (match-string 2)))
  7260. (setq t1 (time-to-days (org-time-string-to-time t1))
  7261. t2 (time-to-days (org-time-string-to-time t2)))
  7262. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7263. ((org-at-timestamp-p t)
  7264. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7265. (substring (match-string 1) 0 10)))
  7266. 1))
  7267. (t (error "This should not happen"))))
  7268. ;;; Following file links
  7269. (defvar org-wait nil)
  7270. (defun org-open-file (path &optional in-emacs line search)
  7271. "Open the file at PATH.
  7272. First, this expands any special file name abbreviations. Then the
  7273. configuration variable `org-file-apps' is checked if it contains an
  7274. entry for this file type, and if yes, the corresponding command is launched.
  7275. If no application is found, Emacs simply visits the file.
  7276. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7277. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7278. and o use an external application to visit the file.
  7279. Optional LINE specifies a line to go to, optional SEARCH a string to
  7280. search for. If LINE or SEARCH is given, the file will always be
  7281. opened in Emacs.
  7282. If the file does not exist, an error is thrown."
  7283. (setq in-emacs (or in-emacs line search))
  7284. (let* ((file (if (equal path "")
  7285. buffer-file-name
  7286. (substitute-in-file-name (expand-file-name path))))
  7287. (apps (append org-file-apps (org-default-apps)))
  7288. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7289. (dirp (if remp nil (file-directory-p file)))
  7290. (file (if (and dirp org-open-directory-means-index-dot-org)
  7291. (concat (file-name-as-directory file) "index.org")
  7292. file))
  7293. (a-m-a-p (assq 'auto-mode apps))
  7294. (dfile (downcase file))
  7295. (old-buffer (current-buffer))
  7296. (old-pos (point))
  7297. (old-mode major-mode)
  7298. ext cmd)
  7299. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7300. (setq ext (match-string 1 dfile))
  7301. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7302. (setq ext (match-string 1 dfile))))
  7303. (cond
  7304. ((equal in-emacs '(16))
  7305. (setq cmd (cdr (assoc 'system apps))))
  7306. (in-emacs (setq cmd 'emacs))
  7307. (t
  7308. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7309. (and dirp (cdr (assoc 'directory apps)))
  7310. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7311. 'string-match)
  7312. (cdr (assoc ext apps))
  7313. (cdr (assoc t apps))))))
  7314. (when (eq cmd 'system)
  7315. (setq cmd (cdr (assoc 'system apps))))
  7316. (when (eq cmd 'default)
  7317. (setq cmd (cdr (assoc t apps))))
  7318. (when (eq cmd 'mailcap)
  7319. (require 'mailcap)
  7320. (mailcap-parse-mailcaps)
  7321. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7322. (command (mailcap-mime-info mime-type)))
  7323. (if (stringp command)
  7324. (setq cmd command)
  7325. (setq cmd 'emacs))))
  7326. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7327. (not (file-exists-p file))
  7328. (not org-open-non-existing-files))
  7329. (error "No such file: %s" file))
  7330. (cond
  7331. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7332. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7333. (while (string-match "['\"]%s['\"]" cmd)
  7334. (setq cmd (replace-match "%s" t t cmd)))
  7335. (while (string-match "%s" cmd)
  7336. (setq cmd (replace-match
  7337. (save-match-data
  7338. (shell-quote-argument
  7339. (convert-standard-filename file)))
  7340. t t cmd)))
  7341. (save-window-excursion
  7342. (start-process-shell-command cmd nil cmd)
  7343. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7344. ))
  7345. ((or (stringp cmd)
  7346. (eq cmd 'emacs))
  7347. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7348. (widen)
  7349. (if line (goto-line line)
  7350. (if search (org-link-search search))))
  7351. ((consp cmd)
  7352. (let ((file (convert-standard-filename file)))
  7353. (eval cmd)))
  7354. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7355. (and (org-mode-p) (eq old-mode 'org-mode)
  7356. (or (not (equal old-buffer (current-buffer)))
  7357. (not (equal old-pos (point))))
  7358. (org-mark-ring-push old-pos old-buffer))))
  7359. (defun org-default-apps ()
  7360. "Return the default applications for this operating system."
  7361. (cond
  7362. ((eq system-type 'darwin)
  7363. org-file-apps-defaults-macosx)
  7364. ((eq system-type 'windows-nt)
  7365. org-file-apps-defaults-windowsnt)
  7366. (t org-file-apps-defaults-gnu)))
  7367. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7368. "Convert extensions to regular expressions in the cars of LIST.
  7369. Also, weed out any non-string entries, because the return value is used
  7370. only for regexp matching.
  7371. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7372. point to the symbol `emacs', indicating that the file should
  7373. be opened in Emacs."
  7374. (append
  7375. (delq nil
  7376. (mapcar (lambda (x)
  7377. (if (not (stringp (car x)))
  7378. nil
  7379. (if (string-match "\\W" (car x))
  7380. x
  7381. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7382. list))
  7383. (if add-auto-mode
  7384. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7385. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7386. (defun org-file-remote-p (file)
  7387. "Test whether FILE specifies a location on a remote system.
  7388. Return non-nil if the location is indeed remote.
  7389. For example, the filename \"/user@host:/foo\" specifies a location
  7390. on the system \"/user@host:\"."
  7391. (cond ((fboundp 'file-remote-p)
  7392. (file-remote-p file))
  7393. ((fboundp 'tramp-handle-file-remote-p)
  7394. (tramp-handle-file-remote-p file))
  7395. ((and (boundp 'ange-ftp-name-format)
  7396. (string-match (car ange-ftp-name-format) file))
  7397. t)
  7398. (t nil)))
  7399. ;;;; Refiling
  7400. (defun org-get-org-file ()
  7401. "Read a filename, with default directory `org-directory'."
  7402. (let ((default (or org-default-notes-file remember-data-file)))
  7403. (read-file-name (format "File name [%s]: " default)
  7404. (file-name-as-directory org-directory)
  7405. default)))
  7406. (defun org-notes-order-reversed-p ()
  7407. "Check if the current file should receive notes in reversed order."
  7408. (cond
  7409. ((not org-reverse-note-order) nil)
  7410. ((eq t org-reverse-note-order) t)
  7411. ((not (listp org-reverse-note-order)) nil)
  7412. (t (catch 'exit
  7413. (let ((all org-reverse-note-order)
  7414. entry)
  7415. (while (setq entry (pop all))
  7416. (if (string-match (car entry) buffer-file-name)
  7417. (throw 'exit (cdr entry))))
  7418. nil)))))
  7419. (defvar org-refile-target-table nil
  7420. "The list of refile targets, created by `org-refile'.")
  7421. (defvar org-agenda-new-buffers nil
  7422. "Buffers created to visit agenda files.")
  7423. (defun org-get-refile-targets (&optional default-buffer)
  7424. "Produce a table with refile targets."
  7425. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7426. targets txt re files f desc descre fast-path-p level pos0)
  7427. (message "Getting targets...")
  7428. (with-current-buffer (or default-buffer (current-buffer))
  7429. (while (setq entry (pop entries))
  7430. (setq files (car entry) desc (cdr entry))
  7431. (setq fast-path-p nil)
  7432. (cond
  7433. ((null files) (setq files (list (current-buffer))))
  7434. ((eq files 'org-agenda-files)
  7435. (setq files (org-agenda-files 'unrestricted)))
  7436. ((and (symbolp files) (fboundp files))
  7437. (setq files (funcall files)))
  7438. ((and (symbolp files) (boundp files))
  7439. (setq files (symbol-value files))))
  7440. (if (stringp files) (setq files (list files)))
  7441. (cond
  7442. ((eq (car desc) :tag)
  7443. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7444. ((eq (car desc) :todo)
  7445. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7446. ((eq (car desc) :regexp)
  7447. (setq descre (cdr desc)))
  7448. ((eq (car desc) :level)
  7449. (setq descre (concat "^\\*\\{" (number-to-string
  7450. (if org-odd-levels-only
  7451. (1- (* 2 (cdr desc)))
  7452. (cdr desc)))
  7453. "\\}[ \t]")))
  7454. ((eq (car desc) :maxlevel)
  7455. (setq fast-path-p t)
  7456. (setq descre (concat "^\\*\\{1," (number-to-string
  7457. (if org-odd-levels-only
  7458. (1- (* 2 (cdr desc)))
  7459. (cdr desc)))
  7460. "\\}[ \t]")))
  7461. (t (error "Bad refiling target description %s" desc)))
  7462. (while (setq f (pop files))
  7463. (save-excursion
  7464. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7465. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7466. (setq f (expand-file-name f))
  7467. (if (eq org-refile-use-outline-path 'file)
  7468. (push (list (file-name-nondirectory f) f nil nil) targets))
  7469. (save-excursion
  7470. (save-restriction
  7471. (widen)
  7472. (goto-char (point-min))
  7473. (while (re-search-forward descre nil t)
  7474. (goto-char (setq pos0 (point-at-bol)))
  7475. (catch 'next
  7476. (when org-refile-target-verify-function
  7477. (save-match-data
  7478. (or (funcall org-refile-target-verify-function)
  7479. (throw 'next t))))
  7480. (when (looking-at org-complex-heading-regexp)
  7481. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7482. txt (org-link-display-format (match-string 4))
  7483. re (concat "^" (regexp-quote
  7484. (buffer-substring (match-beginning 1)
  7485. (match-end 4)))))
  7486. (if (match-end 5) (setq re (concat re "[ \t]+"
  7487. (regexp-quote
  7488. (match-string 5)))))
  7489. (setq re (concat re "[ \t]*$"))
  7490. (when org-refile-use-outline-path
  7491. (setq txt (mapconcat 'org-protect-slash
  7492. (append
  7493. (if (eq org-refile-use-outline-path 'file)
  7494. (list (file-name-nondirectory
  7495. (buffer-file-name (buffer-base-buffer))))
  7496. (if (eq org-refile-use-outline-path 'full-file-path)
  7497. (list (buffer-file-name (buffer-base-buffer)))))
  7498. (org-get-outline-path fast-path-p level txt)
  7499. (list txt))
  7500. "/")))
  7501. (push (list txt f re (point)) targets)))
  7502. (when (= (point) pos0)
  7503. ;; verification function has not moved point
  7504. (goto-char (point-at-eol))))))))))
  7505. (message "Getting targets...done")
  7506. (nreverse targets)))
  7507. (defun org-protect-slash (s)
  7508. (while (string-match "/" s)
  7509. (setq s (replace-match "\\" t t s)))
  7510. s)
  7511. (defvar org-olpa (make-vector 20 nil))
  7512. (defun org-get-outline-path (&optional fastp level heading)
  7513. "Return the outline path to the current entry, as a list."
  7514. (if fastp
  7515. (progn
  7516. (if (> level 19)
  7517. (error "Outline path failure, more than 19 levels."))
  7518. (loop for i from level upto 19 do
  7519. (aset org-olpa i nil))
  7520. (prog1
  7521. (delq nil (append org-olpa nil))
  7522. (aset org-olpa level heading)))
  7523. (let (rtn)
  7524. (save-excursion
  7525. (while (org-up-heading-safe)
  7526. (when (looking-at org-complex-heading-regexp)
  7527. (push (org-match-string-no-properties 4) rtn)))
  7528. rtn))))
  7529. (defvar org-refile-history nil
  7530. "History for refiling operations.")
  7531. (defvar org-after-refile-insert-hook nil
  7532. "Hook run after `org-refile' has inserted its stuff at the new location.
  7533. Note that this is still *before* the stuff will be removed from
  7534. the *old* location.")
  7535. (defun org-refile (&optional goto default-buffer)
  7536. "Move the entry at point to another heading.
  7537. The list of target headings is compiled using the information in
  7538. `org-refile-targets', which see. This list is created before each use
  7539. and will therefore always be up-to-date.
  7540. At the target location, the entry is filed as a subitem of the target heading.
  7541. Depending on `org-reverse-note-order', the new subitem will either be the
  7542. first or the last subitem.
  7543. If there is an active region, all entries in that region will be moved.
  7544. However, the region must fulfil the requirement that the first heading
  7545. is the first one sets the top-level of the moved text - at most siblings
  7546. below it are allowed.
  7547. With prefix arg GOTO, the command will only visit the target location,
  7548. not actually move anything.
  7549. With a double prefix `C-u C-u', go to the location where the last refiling
  7550. operation has put the subtree.
  7551. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7552. (interactive "P")
  7553. (let* ((cbuf (current-buffer))
  7554. (regionp (org-region-active-p))
  7555. (region-start (and regionp (region-beginning)))
  7556. (region-end (and regionp (region-end)))
  7557. (region-length (and regionp (- region-end region-start)))
  7558. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7559. pos it nbuf file re level reversed)
  7560. (when regionp (goto-char region-start)
  7561. (unless (org-kill-is-subtree-p
  7562. (buffer-substring region-start region-end))
  7563. (error "The region is not a (sequence of) subtree(s)")))
  7564. (if (equal goto '(16))
  7565. (org-refile-goto-last-stored)
  7566. (when (setq it (org-refile-get-location
  7567. (if goto "Goto: " "Refile to: ") default-buffer
  7568. org-refile-allow-creating-parent-nodes))
  7569. (setq file (nth 1 it)
  7570. re (nth 2 it)
  7571. pos (nth 3 it))
  7572. (if (and (not goto)
  7573. pos
  7574. (equal (buffer-file-name) file)
  7575. (if regionp
  7576. (and (>= pos region-start)
  7577. (<= pos region-end))
  7578. (and (>= pos (point))
  7579. (< pos (save-excursion
  7580. (org-end-of-subtree t t))))))
  7581. (error "Cannot refile to position inside the tree or region"))
  7582. (setq nbuf (or (find-buffer-visiting file)
  7583. (find-file-noselect file)))
  7584. (if goto
  7585. (progn
  7586. (switch-to-buffer nbuf)
  7587. (goto-char pos)
  7588. (org-show-context 'org-goto))
  7589. (if regionp
  7590. (progn
  7591. (kill-new (buffer-substring region-start region-end))
  7592. (org-save-markers-in-region region-start region-end))
  7593. (org-copy-subtree 1 nil t))
  7594. (save-excursion
  7595. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7596. (find-file-noselect file))))
  7597. (setq reversed (org-notes-order-reversed-p))
  7598. (save-excursion
  7599. (save-restriction
  7600. (widen)
  7601. (if pos
  7602. (progn
  7603. (goto-char pos)
  7604. (looking-at outline-regexp)
  7605. (setq level (org-get-valid-level (funcall outline-level) 1))
  7606. (goto-char
  7607. (if reversed
  7608. (or (outline-next-heading) (point-max))
  7609. (or (save-excursion (outline-get-next-sibling))
  7610. (org-end-of-subtree t t)
  7611. (point-max)))))
  7612. (setq level 1)
  7613. (if (not reversed)
  7614. (goto-char (point-max))
  7615. (goto-char (point-min))
  7616. (or (outline-next-heading) (goto-char (point-max)))))
  7617. (if (not (bolp)) (newline))
  7618. (bookmark-set "org-refile-last-stored")
  7619. (org-paste-subtree level)
  7620. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7621. (run-hooks 'org-after-refile-insert-hook))))
  7622. (if regionp
  7623. (delete-region (point) (+ (point) region-length))
  7624. (org-cut-subtree))
  7625. (setq org-markers-to-move nil)
  7626. (message "Refiled to \"%s\"" (car it))))))
  7627. (org-reveal))
  7628. (defun org-refile-goto-last-stored ()
  7629. "Go to the location where the last refile was stored."
  7630. (interactive)
  7631. (bookmark-jump "org-refile-last-stored")
  7632. (message "This is the location of the last refile"))
  7633. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7634. "Prompt the user for a refile location, using PROMPT."
  7635. (let ((org-refile-targets org-refile-targets)
  7636. (org-refile-use-outline-path org-refile-use-outline-path))
  7637. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7638. (unless org-refile-target-table
  7639. (error "No refile targets"))
  7640. (let* ((cbuf (current-buffer))
  7641. (partial-completion-mode nil)
  7642. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7643. (cfunc (if (and org-refile-use-outline-path
  7644. org-outline-path-complete-in-steps)
  7645. 'org-olpath-completing-read
  7646. 'org-ido-completing-read))
  7647. (extra (if org-refile-use-outline-path "/" ""))
  7648. (filename (and cfn (expand-file-name cfn)))
  7649. (tbl (mapcar
  7650. (lambda (x)
  7651. (if (and (not (member org-refile-use-outline-path
  7652. '(file full-file-path)))
  7653. (not (equal filename (nth 1 x))))
  7654. (cons (concat (car x) extra " ("
  7655. (file-name-nondirectory (nth 1 x)) ")")
  7656. (cdr x))
  7657. (cons (concat (car x) extra) (cdr x))))
  7658. org-refile-target-table))
  7659. (completion-ignore-case t)
  7660. pa answ parent-target child parent)
  7661. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7662. nil 'org-refile-history))
  7663. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7664. (if pa
  7665. pa
  7666. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7667. (setq parent (match-string 1 answ)
  7668. child (match-string 2 answ))
  7669. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7670. (when (and parent-target
  7671. (or (eq new-nodes t)
  7672. (and (eq new-nodes 'confirm)
  7673. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7674. (org-refile-new-child parent-target child))))))
  7675. (defun org-refile-new-child (parent-target child)
  7676. "Use refile target PARENT-TARGET to add new CHILD below it."
  7677. (unless parent-target
  7678. (error "Cannot find parent for new node"))
  7679. (let ((file (nth 1 parent-target))
  7680. (pos (nth 3 parent-target))
  7681. level)
  7682. (with-current-buffer (or (find-buffer-visiting file)
  7683. (find-file-noselect file))
  7684. (save-excursion
  7685. (save-restriction
  7686. (widen)
  7687. (if pos
  7688. (goto-char pos)
  7689. (goto-char (point-max))
  7690. (if (not (bolp)) (newline)))
  7691. (when (looking-at outline-regexp)
  7692. (setq level (funcall outline-level))
  7693. (org-end-of-subtree t t))
  7694. (org-back-over-empty-lines)
  7695. (insert "\n" (make-string
  7696. (if pos (org-get-valid-level level 1) 1) ?*)
  7697. " " child "\n")
  7698. (beginning-of-line 0)
  7699. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7700. (defun org-olpath-completing-read (prompt collection &rest args)
  7701. "Read an outline path like a file name."
  7702. (let ((thetable collection)
  7703. (org-completion-use-ido nil)) ; does not work with ido.
  7704. (apply
  7705. 'org-ido-completing-read prompt
  7706. (lambda (string predicate &optional flag)
  7707. (let (rtn r f (l (length string)))
  7708. (cond
  7709. ((eq flag nil)
  7710. ;; try completion
  7711. (try-completion string thetable))
  7712. ((eq flag t)
  7713. ;; all-completions
  7714. (setq rtn (all-completions string thetable predicate))
  7715. (mapcar
  7716. (lambda (x)
  7717. (setq r (substring x l))
  7718. (if (string-match " ([^)]*)$" x)
  7719. (setq f (match-string 0 x))
  7720. (setq f ""))
  7721. (if (string-match "/" r)
  7722. (concat string (substring r 0 (match-end 0)) f)
  7723. x))
  7724. rtn))
  7725. ((eq flag 'lambda)
  7726. ;; exact match?
  7727. (assoc string thetable)))
  7728. ))
  7729. args)))
  7730. ;;;; Dynamic blocks
  7731. (defun org-find-dblock (name)
  7732. "Find the first dynamic block with name NAME in the buffer.
  7733. If not found, stay at current position and return nil."
  7734. (let (pos)
  7735. (save-excursion
  7736. (goto-char (point-min))
  7737. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7738. nil t)
  7739. (match-beginning 0))))
  7740. (if pos (goto-char pos))
  7741. pos))
  7742. (defconst org-dblock-start-re
  7743. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7744. "Matches the startline of a dynamic block, with parameters.")
  7745. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7746. "Matches the end of a dynamic block.")
  7747. (defun org-create-dblock (plist)
  7748. "Create a dynamic block section, with parameters taken from PLIST.
  7749. PLIST must contain a :name entry which is used as name of the block."
  7750. (unless (bolp) (newline))
  7751. (let ((name (plist-get plist :name)))
  7752. (insert "#+BEGIN: " name)
  7753. (while plist
  7754. (if (eq (car plist) :name)
  7755. (setq plist (cddr plist))
  7756. (insert " " (prin1-to-string (pop plist)))))
  7757. (insert "\n\n#+END:\n")
  7758. (beginning-of-line -2)))
  7759. (defun org-prepare-dblock ()
  7760. "Prepare dynamic block for refresh.
  7761. This empties the block, puts the cursor at the insert position and returns
  7762. the property list including an extra property :name with the block name."
  7763. (unless (looking-at org-dblock-start-re)
  7764. (error "Not at a dynamic block"))
  7765. (let* ((begdel (1+ (match-end 0)))
  7766. (name (org-no-properties (match-string 1)))
  7767. (params (append (list :name name)
  7768. (read (concat "(" (match-string 3) ")")))))
  7769. (unless (re-search-forward org-dblock-end-re nil t)
  7770. (error "Dynamic block not terminated"))
  7771. (setq params
  7772. (append params
  7773. (list :content (buffer-substring
  7774. begdel (match-beginning 0)))))
  7775. (delete-region begdel (match-beginning 0))
  7776. (goto-char begdel)
  7777. (open-line 1)
  7778. params))
  7779. (defun org-map-dblocks (&optional command)
  7780. "Apply COMMAND to all dynamic blocks in the current buffer.
  7781. If COMMAND is not given, use `org-update-dblock'."
  7782. (let ((cmd (or command 'org-update-dblock))
  7783. pos)
  7784. (save-excursion
  7785. (goto-char (point-min))
  7786. (while (re-search-forward org-dblock-start-re nil t)
  7787. (goto-char (setq pos (match-beginning 0)))
  7788. (condition-case nil
  7789. (funcall cmd)
  7790. (error (message "Error during update of dynamic block")))
  7791. (goto-char pos)
  7792. (unless (re-search-forward org-dblock-end-re nil t)
  7793. (error "Dynamic block not terminated"))))))
  7794. (defun org-dblock-update (&optional arg)
  7795. "User command for updating dynamic blocks.
  7796. Update the dynamic block at point. With prefix ARG, update all dynamic
  7797. blocks in the buffer."
  7798. (interactive "P")
  7799. (if arg
  7800. (org-update-all-dblocks)
  7801. (or (looking-at org-dblock-start-re)
  7802. (org-beginning-of-dblock))
  7803. (org-update-dblock)))
  7804. (defun org-update-dblock ()
  7805. "Update the dynamic block at point
  7806. This means to empty the block, parse for parameters and then call
  7807. the correct writing function."
  7808. (save-window-excursion
  7809. (let* ((pos (point))
  7810. (line (org-current-line))
  7811. (params (org-prepare-dblock))
  7812. (name (plist-get params :name))
  7813. (cmd (intern (concat "org-dblock-write:" name))))
  7814. (message "Updating dynamic block `%s' at line %d..." name line)
  7815. (funcall cmd params)
  7816. (message "Updating dynamic block `%s' at line %d...done" name line)
  7817. (goto-char pos))))
  7818. (defun org-beginning-of-dblock ()
  7819. "Find the beginning of the dynamic block at point.
  7820. Error if there is no such block at point."
  7821. (let ((pos (point))
  7822. beg)
  7823. (end-of-line 1)
  7824. (if (and (re-search-backward org-dblock-start-re nil t)
  7825. (setq beg (match-beginning 0))
  7826. (re-search-forward org-dblock-end-re nil t)
  7827. (> (match-end 0) pos))
  7828. (goto-char beg)
  7829. (goto-char pos)
  7830. (error "Not in a dynamic block"))))
  7831. (defun org-update-all-dblocks ()
  7832. "Update all dynamic blocks in the buffer.
  7833. This function can be used in a hook."
  7834. (when (org-mode-p)
  7835. (org-map-dblocks 'org-update-dblock)))
  7836. ;;;; Completion
  7837. (defconst org-additional-option-like-keywords
  7838. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7839. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7840. "LATEX_HEADER:"
  7841. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7842. "BEGIN_QUOTE" "END_QUOTE"
  7843. "BEGIN_VERSE" "END_VERSE"
  7844. "BEGIN_CENTER" "END_CENTER"
  7845. "BEGIN_SRC" "END_SRC"
  7846. "CATEGORY" "COLUMNS"
  7847. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7848. (defcustom org-structure-template-alist
  7849. '(
  7850. ("s" "#+begin_src ?\n\n#+end_src"
  7851. "<src lang=\"?\">\n\n</src>")
  7852. ("e" "#+begin_example\n?\n#+end_example"
  7853. "<example>\n?\n</example>")
  7854. ("q" "#+begin_quote\n?\n#+end_quote"
  7855. "<quote>\n?\n</quote>")
  7856. ("v" "#+begin_verse\n?\n#+end_verse"
  7857. "<verse>\n?\n/verse>")
  7858. ("c" "#+begin_center\n?\n#+end_center"
  7859. "<center>\n?\n/center>")
  7860. ("l" "#+begin_latex\n?\n#+end_latex"
  7861. "<literal style=\"latex\">\n?\n</literal>")
  7862. ("L" "#+latex: "
  7863. "<literal style=\"latex\">?</literal>")
  7864. ("h" "#+begin_html\n?\n#+end_html"
  7865. "<literal style=\"html\">\n?\n</literal>")
  7866. ("H" "#+html: "
  7867. "<literal style=\"html\">?</literal>")
  7868. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7869. ("A" "#+ascii: ")
  7870. ("i" "#+include %file ?"
  7871. "<include file=%file markup=\"?\">")
  7872. )
  7873. "Structure completion elements.
  7874. This is a list of abbreviation keys and values. The value gets inserted
  7875. it you type @samp{.} followed by the key and then the completion key,
  7876. usually `M-TAB'. %file will be replaced by a file name after prompting
  7877. for the file using completion.
  7878. There are two templates for each key, the first uses the original Org syntax,
  7879. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7880. the default when the /org-mtags.el/ module has been loaded. See also the
  7881. variable `org-mtags-prefer-muse-templates'.
  7882. This is an experimental feature, it is undecided if it is going to stay in."
  7883. :group 'org-completion
  7884. :type '(repeat
  7885. (string :tag "Key")
  7886. (string :tag "Template")
  7887. (string :tag "Muse Template")))
  7888. (defun org-try-structure-completion ()
  7889. "Try to complete a structure template before point.
  7890. This looks for strings like \"<e\" on an otherwise empty line and
  7891. expands them."
  7892. (let ((l (buffer-substring (point-at-bol) (point)))
  7893. a)
  7894. (when (and (looking-at "[ \t]*$")
  7895. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7896. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7897. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7898. (match-beginning 1)) a)
  7899. t)))
  7900. (defun org-complete-expand-structure-template (start cell)
  7901. "Expand a structure template."
  7902. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7903. (rpl (nth (if musep 2 1) cell))
  7904. (ind ""))
  7905. (delete-region start (point))
  7906. (when (string-match "\\`#\\+" rpl)
  7907. (cond
  7908. ((bolp))
  7909. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7910. (setq ind (buffer-substring (point-at-bol) (point))))
  7911. (t (newline))))
  7912. (setq start (point))
  7913. (if (string-match "%file" rpl)
  7914. (setq rpl (replace-match
  7915. (concat
  7916. "\""
  7917. (save-match-data
  7918. (abbreviate-file-name (read-file-name "Include file: ")))
  7919. "\"")
  7920. t t rpl)))
  7921. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  7922. (concat "\n" ind)))
  7923. (insert rpl)
  7924. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7925. (defun org-complete (&optional arg)
  7926. "Perform completion on word at point.
  7927. At the beginning of a headline, this completes TODO keywords as given in
  7928. `org-todo-keywords'.
  7929. If the current word is preceded by a backslash, completes the TeX symbols
  7930. that are supported for HTML support.
  7931. If the current word is preceded by \"#+\", completes special words for
  7932. setting file options.
  7933. In the line after \"#+STARTUP:, complete valid keywords.\"
  7934. At all other locations, this simply calls the value of
  7935. `org-completion-fallback-command'."
  7936. (interactive "P")
  7937. (org-without-partial-completion
  7938. (catch 'exit
  7939. (let* ((a nil)
  7940. (end (point))
  7941. (beg1 (save-excursion
  7942. (skip-chars-backward (org-re "[:alnum:]_@"))
  7943. (point)))
  7944. (beg (save-excursion
  7945. (skip-chars-backward "a-zA-Z0-9_:$")
  7946. (point)))
  7947. (confirm (lambda (x) (stringp (car x))))
  7948. (searchhead (equal (char-before beg) ?*))
  7949. (struct
  7950. (when (and (member (char-before beg1) '(?. ?<))
  7951. (setq a (assoc (buffer-substring beg1 (point))
  7952. org-structure-template-alist)))
  7953. (org-complete-expand-structure-template (1- beg1) a)
  7954. (throw 'exit t)))
  7955. (tag (and (equal (char-before beg1) ?:)
  7956. (equal (char-after (point-at-bol)) ?*)))
  7957. (prop (and (equal (char-before beg1) ?:)
  7958. (not (equal (char-after (point-at-bol)) ?*))))
  7959. (texp (equal (char-before beg) ?\\))
  7960. (link (equal (char-before beg) ?\[))
  7961. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7962. beg)
  7963. "#+"))
  7964. (startup (string-match "^#\\+STARTUP:.*"
  7965. (buffer-substring (point-at-bol) (point))))
  7966. (completion-ignore-case opt)
  7967. (type nil)
  7968. (tbl nil)
  7969. (table (cond
  7970. (opt
  7971. (setq type :opt)
  7972. (require 'org-exp)
  7973. (append
  7974. (mapcar
  7975. (lambda (x)
  7976. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7977. (cons (match-string 2 x) (match-string 1 x)))
  7978. (org-split-string (org-get-current-options) "\n"))
  7979. (mapcar 'list org-additional-option-like-keywords)))
  7980. (startup
  7981. (setq type :startup)
  7982. org-startup-options)
  7983. (link (append org-link-abbrev-alist-local
  7984. org-link-abbrev-alist))
  7985. (texp
  7986. (setq type :tex)
  7987. org-html-entities)
  7988. ((string-match "\\`\\*+[ \t]+\\'"
  7989. (buffer-substring (point-at-bol) beg))
  7990. (setq type :todo)
  7991. (mapcar 'list org-todo-keywords-1))
  7992. (searchhead
  7993. (setq type :searchhead)
  7994. (save-excursion
  7995. (goto-char (point-min))
  7996. (while (re-search-forward org-todo-line-regexp nil t)
  7997. (push (list
  7998. (org-make-org-heading-search-string
  7999. (match-string 3) t))
  8000. tbl)))
  8001. tbl)
  8002. (tag (setq type :tag beg beg1)
  8003. (or org-tag-alist (org-get-buffer-tags)))
  8004. (prop (setq type :prop beg beg1)
  8005. (mapcar 'list (org-buffer-property-keys nil t t)))
  8006. (t (progn
  8007. (call-interactively org-completion-fallback-command)
  8008. (throw 'exit nil)))))
  8009. (pattern (buffer-substring-no-properties beg end))
  8010. (completion (try-completion pattern table confirm)))
  8011. (cond ((eq completion t)
  8012. (if (not (assoc (upcase pattern) table))
  8013. (message "Already complete")
  8014. (if (and (equal type :opt)
  8015. (not (member (car (assoc (upcase pattern) table))
  8016. org-additional-option-like-keywords)))
  8017. (insert (substring (cdr (assoc (upcase pattern) table))
  8018. (length pattern)))
  8019. (if (memq type '(:tag :prop)) (insert ":")))))
  8020. ((null completion)
  8021. (message "Can't find completion for \"%s\"" pattern)
  8022. (ding))
  8023. ((not (string= pattern completion))
  8024. (delete-region beg end)
  8025. (if (string-match " +$" completion)
  8026. (setq completion (replace-match "" t t completion)))
  8027. (insert completion)
  8028. (if (get-buffer-window "*Completions*")
  8029. (delete-window (get-buffer-window "*Completions*")))
  8030. (if (assoc completion table)
  8031. (if (eq type :todo) (insert " ")
  8032. (if (memq type '(:tag :prop)) (insert ":"))))
  8033. (if (and (equal type :opt) (assoc completion table))
  8034. (message "%s" (substitute-command-keys
  8035. "Press \\[org-complete] again to insert example settings"))))
  8036. (t
  8037. (message "Making completion list...")
  8038. (let ((list (sort (all-completions pattern table confirm)
  8039. 'string<)))
  8040. (with-output-to-temp-buffer "*Completions*"
  8041. (condition-case nil
  8042. ;; Protection needed for XEmacs and emacs 21
  8043. (display-completion-list list pattern)
  8044. (error (display-completion-list list)))))
  8045. (message "Making completion list...%s" "done")))))))
  8046. ;;;; TODO, DEADLINE, Comments
  8047. (defun org-toggle-comment ()
  8048. "Change the COMMENT state of an entry."
  8049. (interactive)
  8050. (save-excursion
  8051. (org-back-to-heading)
  8052. (let (case-fold-search)
  8053. (if (looking-at (concat outline-regexp
  8054. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8055. (replace-match "" t t nil 1)
  8056. (if (looking-at outline-regexp)
  8057. (progn
  8058. (goto-char (match-end 0))
  8059. (insert org-comment-string " ")))))))
  8060. (defvar org-last-todo-state-is-todo nil
  8061. "This is non-nil when the last TODO state change led to a TODO state.
  8062. If the last change removed the TODO tag or switched to DONE, then
  8063. this is nil.")
  8064. (defvar org-setting-tags nil) ; dynamically skipped
  8065. (defun org-parse-local-options (string var)
  8066. "Parse STRING for startup setting relevant for variable VAR."
  8067. (let ((rtn (symbol-value var))
  8068. e opts)
  8069. (save-match-data
  8070. (if (or (not string) (not (string-match "\\S-" string)))
  8071. rtn
  8072. (setq opts (delq nil (mapcar (lambda (x)
  8073. (setq e (assoc x org-startup-options))
  8074. (if (eq (nth 1 e) var) e nil))
  8075. (org-split-string string "[ \t]+"))))
  8076. (if (not opts)
  8077. rtn
  8078. (setq rtn nil)
  8079. (while (setq e (pop opts))
  8080. (if (not (nth 3 e))
  8081. (setq rtn (nth 2 e))
  8082. (if (not (listp rtn)) (setq rtn nil))
  8083. (push (nth 2 e) rtn)))
  8084. rtn)))))
  8085. (defvar org-todo-setup-filter-hook nil
  8086. "Hook for functions that pre-filter todo specs.
  8087. Each function takes a todo spec and returns either `nil' or the spec
  8088. transformed into canonical form." )
  8089. (defvar org-todo-get-default-hook nil
  8090. "Hook for functions that get a default item for todo.
  8091. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8092. `nil' or a string to be used for the todo mark." )
  8093. (defvar org-agenda-headline-snapshot-before-repeat)
  8094. (defun org-todo (&optional arg)
  8095. "Change the TODO state of an item.
  8096. The state of an item is given by a keyword at the start of the heading,
  8097. like
  8098. *** TODO Write paper
  8099. *** DONE Call mom
  8100. The different keywords are specified in the variable `org-todo-keywords'.
  8101. By default the available states are \"TODO\" and \"DONE\".
  8102. So for this example: when the item starts with TODO, it is changed to DONE.
  8103. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8104. DONE are present, add TODO at the beginning of the heading.
  8105. With C-u prefix arg, use completion to determine the new state.
  8106. With numeric prefix arg, switch to that state.
  8107. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8108. With a tripple C-u prefix, circumvent any state blocking.
  8109. For calling through lisp, arg is also interpreted in the following way:
  8110. 'none -> empty state
  8111. \"\"(empty string) -> switch to empty state
  8112. 'done -> switch to DONE
  8113. 'nextset -> switch to the next set of keywords
  8114. 'previousset -> switch to the previous set of keywords
  8115. \"WAITING\" -> switch to the specified keyword, but only if it
  8116. really is a member of `org-todo-keywords'."
  8117. (interactive "P")
  8118. (if (equal arg '(16)) (setq arg 'nextset))
  8119. (let ((org-blocker-hook org-blocker-hook)
  8120. (case-fold-search nil))
  8121. (when (equal arg '(64))
  8122. (setq arg nil org-blocker-hook nil))
  8123. (when (and org-blocker-hook
  8124. (org-entry-get nil "NOBLOCKING"))
  8125. (setq org-blocker-hook nil))
  8126. (save-excursion
  8127. (catch 'exit
  8128. (org-back-to-heading)
  8129. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8130. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8131. (looking-at " *"))
  8132. (let* ((match-data (match-data))
  8133. (startpos (point-at-bol))
  8134. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8135. (org-log-done org-log-done)
  8136. (org-log-repeat org-log-repeat)
  8137. (org-todo-log-states org-todo-log-states)
  8138. (this (match-string 1))
  8139. (hl-pos (match-beginning 0))
  8140. (head (org-get-todo-sequence-head this))
  8141. (ass (assoc head org-todo-kwd-alist))
  8142. (interpret (nth 1 ass))
  8143. (done-word (nth 3 ass))
  8144. (final-done-word (nth 4 ass))
  8145. (last-state (or this ""))
  8146. (completion-ignore-case t)
  8147. (member (member this org-todo-keywords-1))
  8148. (tail (cdr member))
  8149. (state (cond
  8150. ((and org-todo-key-trigger
  8151. (or (and (equal arg '(4))
  8152. (eq org-use-fast-todo-selection 'prefix))
  8153. (and (not arg) org-use-fast-todo-selection
  8154. (not (eq org-use-fast-todo-selection
  8155. 'prefix)))))
  8156. ;; Use fast selection
  8157. (org-fast-todo-selection))
  8158. ((and (equal arg '(4))
  8159. (or (not org-use-fast-todo-selection)
  8160. (not org-todo-key-trigger)))
  8161. ;; Read a state with completion
  8162. (org-ido-completing-read
  8163. "State: " (mapcar (lambda(x) (list x))
  8164. org-todo-keywords-1)
  8165. nil t))
  8166. ((eq arg 'right)
  8167. (if this
  8168. (if tail (car tail) nil)
  8169. (car org-todo-keywords-1)))
  8170. ((eq arg 'left)
  8171. (if (equal member org-todo-keywords-1)
  8172. nil
  8173. (if this
  8174. (nth (- (length org-todo-keywords-1)
  8175. (length tail) 2)
  8176. org-todo-keywords-1)
  8177. (org-last org-todo-keywords-1))))
  8178. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8179. (setq arg nil))) ; hack to fall back to cycling
  8180. (arg
  8181. ;; user or caller requests a specific state
  8182. (cond
  8183. ((equal arg "") nil)
  8184. ((eq arg 'none) nil)
  8185. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8186. ((eq arg 'nextset)
  8187. (or (car (cdr (member head org-todo-heads)))
  8188. (car org-todo-heads)))
  8189. ((eq arg 'previousset)
  8190. (let ((org-todo-heads (reverse org-todo-heads)))
  8191. (or (car (cdr (member head org-todo-heads)))
  8192. (car org-todo-heads))))
  8193. ((car (member arg org-todo-keywords-1)))
  8194. ((nth (1- (prefix-numeric-value arg))
  8195. org-todo-keywords-1))))
  8196. ((null member) (or head (car org-todo-keywords-1)))
  8197. ((equal this final-done-word) nil) ;; -> make empty
  8198. ((null tail) nil) ;; -> first entry
  8199. ((memq interpret '(type priority))
  8200. (if (eq this-command last-command)
  8201. (car tail)
  8202. (if (> (length tail) 0)
  8203. (or done-word (car org-done-keywords))
  8204. nil)))
  8205. (t
  8206. (car tail))))
  8207. (state (or
  8208. (run-hook-with-args-until-success
  8209. 'org-todo-get-default-hook state last-state)
  8210. state))
  8211. (next (if state (concat " " state " ") " "))
  8212. (change-plist (list :type 'todo-state-change :from this :to state
  8213. :position startpos))
  8214. dolog now-done-p)
  8215. (when org-blocker-hook
  8216. (setq org-last-todo-state-is-todo
  8217. (not (member this org-done-keywords)))
  8218. (unless (save-excursion
  8219. (save-match-data
  8220. (run-hook-with-args-until-failure
  8221. 'org-blocker-hook change-plist)))
  8222. (if (interactive-p)
  8223. (error "TODO state change from %s to %s blocked" this state)
  8224. ;; fail silently
  8225. (message "TODO state change from %s to %s blocked" this state)
  8226. (throw 'exit nil))))
  8227. (store-match-data match-data)
  8228. (replace-match next t t)
  8229. (unless (pos-visible-in-window-p hl-pos)
  8230. (message "TODO state changed to %s" (org-trim next)))
  8231. (unless head
  8232. (setq head (org-get-todo-sequence-head state)
  8233. ass (assoc head org-todo-kwd-alist)
  8234. interpret (nth 1 ass)
  8235. done-word (nth 3 ass)
  8236. final-done-word (nth 4 ass)))
  8237. (when (memq arg '(nextset previousset))
  8238. (message "Keyword-Set %d/%d: %s"
  8239. (- (length org-todo-sets) -1
  8240. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8241. (length org-todo-sets)
  8242. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8243. (setq org-last-todo-state-is-todo
  8244. (not (member state org-done-keywords)))
  8245. (setq now-done-p (and (member state org-done-keywords)
  8246. (not (member this org-done-keywords))))
  8247. (and logging (org-local-logging logging))
  8248. (when (and (or org-todo-log-states org-log-done)
  8249. (not org-inhibit-logging)
  8250. (not (memq arg '(nextset previousset))))
  8251. ;; we need to look at recording a time and note
  8252. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8253. (nth 2 (assoc this org-todo-log-states))))
  8254. (when (and state
  8255. (member state org-not-done-keywords)
  8256. (not (member this org-not-done-keywords)))
  8257. ;; This is now a todo state and was not one before
  8258. ;; If there was a CLOSED time stamp, get rid of it.
  8259. (org-add-planning-info nil nil 'closed))
  8260. (when (and now-done-p org-log-done)
  8261. ;; It is now done, and it was not done before
  8262. (org-add-planning-info 'closed (org-current-time))
  8263. (if (and (not dolog) (eq 'note org-log-done))
  8264. (org-add-log-setup 'done state this 'findpos 'note)))
  8265. (when (and state dolog)
  8266. ;; This is a non-nil state, and we need to log it
  8267. (org-add-log-setup 'state state this 'findpos dolog)))
  8268. ;; Fixup tag positioning
  8269. (org-todo-trigger-tag-changes state)
  8270. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8271. (when org-provide-todo-statistics
  8272. (org-update-parent-todo-statistics))
  8273. (run-hooks 'org-after-todo-state-change-hook)
  8274. (if (and arg (not (member state org-done-keywords)))
  8275. (setq head (org-get-todo-sequence-head state)))
  8276. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8277. ;; Do we need to trigger a repeat?
  8278. (when now-done-p
  8279. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8280. ;; This is for the agenda, take a snapshot of the headline.
  8281. (save-match-data
  8282. (setq org-agenda-headline-snapshot-before-repeat
  8283. (org-get-heading))))
  8284. (org-auto-repeat-maybe state))
  8285. ;; Fixup cursor location if close to the keyword
  8286. (if (and (outline-on-heading-p)
  8287. (not (bolp))
  8288. (save-excursion (beginning-of-line 1)
  8289. (looking-at org-todo-line-regexp))
  8290. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8291. (progn
  8292. (goto-char (or (match-end 2) (match-end 1)))
  8293. (and (looking-at " ") (just-one-space))))
  8294. (when org-trigger-hook
  8295. (save-excursion
  8296. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8297. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8298. "Block turning an entry into a TODO, using the hierarchy.
  8299. This checks whether the current task should be blocked from state
  8300. changes. Such blocking occurs when:
  8301. 1. The task has children which are not all in a completed state.
  8302. 2. A task has a parent with the property :ORDERED:, and there
  8303. are siblings prior to the current task with incomplete
  8304. status.
  8305. 3. The parent of the task is blocked because it has siblings that should
  8306. be done first, or is child of a block grandparent TODO entry."
  8307. (catch 'dont-block
  8308. ;; If this is not a todo state change, or if this entry is already DONE,
  8309. ;; do not block
  8310. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8311. (member (plist-get change-plist :from)
  8312. (cons 'done org-done-keywords))
  8313. (member (plist-get change-plist :to)
  8314. (cons 'todo org-not-done-keywords)))
  8315. (throw 'dont-block t))
  8316. ;; If this task has children, and any are undone, it's blocked
  8317. (save-excursion
  8318. (org-back-to-heading t)
  8319. (let ((this-level (funcall outline-level)))
  8320. (outline-next-heading)
  8321. (let ((child-level (funcall outline-level)))
  8322. (while (and (not (eobp))
  8323. (> child-level this-level))
  8324. ;; this todo has children, check whether they are all
  8325. ;; completed
  8326. (if (and (not (org-entry-is-done-p))
  8327. (org-entry-is-todo-p))
  8328. (throw 'dont-block nil))
  8329. (outline-next-heading)
  8330. (setq child-level (funcall outline-level))))))
  8331. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8332. ;; any previous siblings are undone, it's blocked
  8333. (save-excursion
  8334. (org-back-to-heading t)
  8335. (let* ((pos (point))
  8336. (parent-pos (and (org-up-heading-safe) (point))))
  8337. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8338. (when (and (org-entry-get (point) "ORDERED")
  8339. (forward-line 1)
  8340. (re-search-forward org-not-done-heading-regexp pos t))
  8341. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8342. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8343. (while t
  8344. (goto-char parent-pos)
  8345. (if (not (looking-at org-not-done-heading-regexp))
  8346. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8347. (setq pos (point))
  8348. (setq parent-pos (and (org-up-heading-safe) (point)))
  8349. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8350. (when (and (org-entry-get (point) "ORDERED")
  8351. (forward-line 1)
  8352. (re-search-forward org-not-done-heading-regexp pos t))
  8353. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8354. (defcustom org-track-ordered-property-with-tag nil
  8355. "Should the ORDERED property also be shown as a tag?
  8356. The ORDERED property decides if an entry should require subtasks to be
  8357. completed in sequence. Since a property is not very visible, setting
  8358. this option means that toggling the ORDERED property with the command
  8359. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8360. not relevant for the behavior, but it makes things more visible.
  8361. Note that toggling the tag with tags commands will not change the property
  8362. and therefore not influence behavior!
  8363. This can be t, meaning the tag ORDERED should be used, It can also be a
  8364. string to select a different tag for this task."
  8365. :group 'org-todo
  8366. :type '(choice
  8367. (const :tag "No tracking" nil)
  8368. (const :tag "Track with ORDERED tag" t)
  8369. (string :tag "Use other tag")))
  8370. (defun org-toggle-ordered-property ()
  8371. "Toggle the ORDERED property of the current entry.
  8372. For better visibility, you can track the value of this property with a tag.
  8373. See variable `org-track-ordered-property-with-tag'."
  8374. (interactive)
  8375. (let* ((t1 org-track-ordered-property-with-tag)
  8376. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8377. (save-excursion
  8378. (org-back-to-heading)
  8379. (if (org-entry-get nil "ORDERED")
  8380. (progn
  8381. (org-delete-property "ORDERED")
  8382. (and tag (org-toggle-tag tag 'off))
  8383. (message "Subtasks can be completed in arbitrary order"))
  8384. (org-entry-put nil "ORDERED" "t")
  8385. (and tag (org-toggle-tag tag 'on))
  8386. (message "Subtasks must be completed in sequence")))))
  8387. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8388. (defun org-block-todo-from-checkboxes (change-plist)
  8389. "Block turning an entry into a TODO, using checkboxes.
  8390. This checks whether the current task should be blocked from state
  8391. changes because there are uncheckd boxes in this entry."
  8392. (catch 'dont-block
  8393. ;; If this is not a todo state change, or if this entry is already DONE,
  8394. ;; do not block
  8395. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8396. (member (plist-get change-plist :from)
  8397. (cons 'done org-done-keywords))
  8398. (member (plist-get change-plist :to)
  8399. (cons 'todo org-not-done-keywords)))
  8400. (throw 'dont-block t))
  8401. ;; If this task has checkboxes that are not checked, it's blocked
  8402. (save-excursion
  8403. (org-back-to-heading t)
  8404. (let ((beg (point)) end)
  8405. (outline-next-heading)
  8406. (setq end (point))
  8407. (goto-char beg)
  8408. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8409. end t)
  8410. (progn
  8411. (if (boundp 'org-blocked-by-checkboxes)
  8412. (setq org-blocked-by-checkboxes t))
  8413. (throw 'dont-block nil)))))
  8414. t)) ; do not block
  8415. (defvar org-entry-property-inherited-from) ;; defined below
  8416. (defun org-update-parent-todo-statistics ()
  8417. "Update any statistics cookie in the parent of the current headline.
  8418. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8419. the entire subtree and this will travel up the hierarchy and update
  8420. statistics everywhere."
  8421. (interactive)
  8422. (let* ((lim 0) prop
  8423. (recursive (or (not org-hierarchical-todo-statistics)
  8424. (string-match
  8425. "\\<recursive\\>"
  8426. (or (setq prop (org-entry-get
  8427. nil "COOKIE_DATA" 'inherit)) ""))))
  8428. (lim (or (and prop (marker-position
  8429. org-entry-property-inherited-from))
  8430. lim))
  8431. (first t)
  8432. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8433. level ltoggle l1
  8434. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8435. (catch 'exit
  8436. (save-excursion
  8437. (beginning-of-line 1)
  8438. (if (org-at-heading-p)
  8439. (setq ltoggle (funcall outline-level))
  8440. (error "This should not happen"))
  8441. (while (and (setq level (org-up-heading-safe))
  8442. (or recursive first)
  8443. (>= (point) lim))
  8444. (setq first nil)
  8445. (unless (and level
  8446. (not (string-match
  8447. "\\<checkbox\\>"
  8448. (downcase
  8449. (or (org-entry-get
  8450. nil "COOKIE_DATA")
  8451. "")))))
  8452. (throw 'exit nil))
  8453. (while (re-search-forward box-re (point-at-eol) t)
  8454. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8455. (setq is-percent (match-end 2))
  8456. (save-match-data
  8457. (unless (outline-next-heading) (throw 'exit nil))
  8458. (while (and (looking-at org-complex-heading-regexp)
  8459. (> (setq l1 (length (match-string 1))) level))
  8460. (setq kwd (and (or recursive (= l1 ltoggle))
  8461. (match-string 2)))
  8462. (and kwd (setq cnt-all (1+ cnt-all)))
  8463. (and (member kwd org-done-keywords)
  8464. (setq cnt-done (1+ cnt-done)))
  8465. (outline-next-heading)))
  8466. (replace-match
  8467. (if is-percent
  8468. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8469. (format "[%d/%d]" cnt-done cnt-all)))))
  8470. (when cookie-present
  8471. (run-hook-with-args 'org-after-todo-statistics-hook
  8472. cnt-done (- cnt-all cnt-done)))))
  8473. (run-hooks 'org-todo-statistics-hook)))
  8474. (defvar org-after-todo-statistics-hook nil
  8475. "Hook that is called after a TODO statistics cookie has been updated.
  8476. Each function is called with two arguments: the number of not-done entries
  8477. and the number of done entries.
  8478. For example, the following function, when added to this hook, will switch
  8479. an entry to DONE when all children are done, and back to TODO when new
  8480. entries are set to a TODO status. Note that this hook is only called
  8481. when there is a statistics cookie in the headline!
  8482. (defun org-summary-todo (n-done n-not-done)
  8483. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8484. (let (org-log-done org-log-states) ; turn off logging
  8485. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8486. ")
  8487. (defvar org-todo-statistics-hook nil
  8488. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8489. This hook runs even if there is no statisics cookie present, in which case
  8490. `org-after-todo-statistics-hook' would not run.")
  8491. (defun org-todo-trigger-tag-changes (state)
  8492. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8493. (let ((l org-todo-state-tags-triggers)
  8494. changes)
  8495. (when (or (not state) (equal state ""))
  8496. (setq changes (append changes (cdr (assoc "" l)))))
  8497. (when (and (stringp state) (> (length state) 0))
  8498. (setq changes (append changes (cdr (assoc state l)))))
  8499. (when (member state org-not-done-keywords)
  8500. (setq changes (append changes (cdr (assoc 'todo l)))))
  8501. (when (member state org-done-keywords)
  8502. (setq changes (append changes (cdr (assoc 'done l)))))
  8503. (dolist (c changes)
  8504. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8505. (defun org-local-logging (value)
  8506. "Get logging settings from a property VALUE."
  8507. (let* (words w a)
  8508. ;; directly set the variables, they are already local.
  8509. (setq org-log-done nil
  8510. org-log-repeat nil
  8511. org-todo-log-states nil)
  8512. (setq words (org-split-string value))
  8513. (while (setq w (pop words))
  8514. (cond
  8515. ((setq a (assoc w org-startup-options))
  8516. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8517. (set (nth 1 a) (nth 2 a))))
  8518. ((setq a (org-extract-log-state-settings w))
  8519. (and (member (car a) org-todo-keywords-1)
  8520. (push a org-todo-log-states)))))))
  8521. (defun org-get-todo-sequence-head (kwd)
  8522. "Return the head of the TODO sequence to which KWD belongs.
  8523. If KWD is not set, check if there is a text property remembering the
  8524. right sequence."
  8525. (let (p)
  8526. (cond
  8527. ((not kwd)
  8528. (or (get-text-property (point-at-bol) 'org-todo-head)
  8529. (progn
  8530. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8531. nil (point-at-eol)))
  8532. (get-text-property p 'org-todo-head))))
  8533. ((not (member kwd org-todo-keywords-1))
  8534. (car org-todo-keywords-1))
  8535. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8536. (defun org-fast-todo-selection ()
  8537. "Fast TODO keyword selection with single keys.
  8538. Returns the new TODO keyword, or nil if no state change should occur."
  8539. (let* ((fulltable org-todo-key-alist)
  8540. (done-keywords org-done-keywords) ;; needed for the faces.
  8541. (maxlen (apply 'max (mapcar
  8542. (lambda (x)
  8543. (if (stringp (car x)) (string-width (car x)) 0))
  8544. fulltable)))
  8545. (expert nil)
  8546. (fwidth (+ maxlen 3 1 3))
  8547. (ncol (/ (- (window-width) 4) fwidth))
  8548. tg cnt e c tbl
  8549. groups ingroup)
  8550. (save-excursion
  8551. (save-window-excursion
  8552. (if expert
  8553. (set-buffer (get-buffer-create " *Org todo*"))
  8554. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8555. (erase-buffer)
  8556. (org-set-local 'org-done-keywords done-keywords)
  8557. (setq tbl fulltable cnt 0)
  8558. (while (setq e (pop tbl))
  8559. (cond
  8560. ((equal e '(:startgroup))
  8561. (push '() groups) (setq ingroup t)
  8562. (when (not (= cnt 0))
  8563. (setq cnt 0)
  8564. (insert "\n"))
  8565. (insert "{ "))
  8566. ((equal e '(:endgroup))
  8567. (setq ingroup nil cnt 0)
  8568. (insert "}\n"))
  8569. ((equal e '(:newline))
  8570. (when (not (= cnt 0))
  8571. (setq cnt 0)
  8572. (insert "\n")
  8573. (setq e (car tbl))
  8574. (while (equal (car tbl) '(:newline))
  8575. (insert "\n")
  8576. (setq tbl (cdr tbl)))))
  8577. (t
  8578. (setq tg (car e) c (cdr e))
  8579. (if ingroup (push tg (car groups)))
  8580. (setq tg (org-add-props tg nil 'face
  8581. (org-get-todo-face tg)))
  8582. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8583. (insert "[" c "] " tg (make-string
  8584. (- fwidth 4 (length tg)) ?\ ))
  8585. (when (= (setq cnt (1+ cnt)) ncol)
  8586. (insert "\n")
  8587. (if ingroup (insert " "))
  8588. (setq cnt 0)))))
  8589. (insert "\n")
  8590. (goto-char (point-min))
  8591. (if (not expert) (org-fit-window-to-buffer))
  8592. (message "[a-z..]:Set [SPC]:clear")
  8593. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8594. (cond
  8595. ((or (= c ?\C-g)
  8596. (and (= c ?q) (not (rassoc c fulltable))))
  8597. (setq quit-flag t))
  8598. ((= c ?\ ) nil)
  8599. ((setq e (rassoc c fulltable) tg (car e))
  8600. tg)
  8601. (t (setq quit-flag t)))))))
  8602. (defun org-entry-is-todo-p ()
  8603. (member (org-get-todo-state) org-not-done-keywords))
  8604. (defun org-entry-is-done-p ()
  8605. (member (org-get-todo-state) org-done-keywords))
  8606. (defun org-get-todo-state ()
  8607. (save-excursion
  8608. (org-back-to-heading t)
  8609. (and (looking-at org-todo-line-regexp)
  8610. (match-end 2)
  8611. (match-string 2))))
  8612. (defun org-at-date-range-p (&optional inactive-ok)
  8613. "Is the cursor inside a date range?"
  8614. (interactive)
  8615. (save-excursion
  8616. (catch 'exit
  8617. (let ((pos (point)))
  8618. (skip-chars-backward "^[<\r\n")
  8619. (skip-chars-backward "<[")
  8620. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8621. (>= (match-end 0) pos)
  8622. (throw 'exit t))
  8623. (skip-chars-backward "^<[\r\n")
  8624. (skip-chars-backward "<[")
  8625. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8626. (>= (match-end 0) pos)
  8627. (throw 'exit t)))
  8628. nil)))
  8629. (defun org-get-repeat ()
  8630. "Check if there is a deadline/schedule with repeater in this entry."
  8631. (save-match-data
  8632. (save-excursion
  8633. (org-back-to-heading t)
  8634. (if (re-search-forward
  8635. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8636. (match-string 1)))))
  8637. (defvar org-last-changed-timestamp)
  8638. (defvar org-last-inserted-timestamp)
  8639. (defvar org-log-post-message)
  8640. (defvar org-log-note-purpose)
  8641. (defvar org-log-note-how)
  8642. (defvar org-log-note-extra)
  8643. (defun org-auto-repeat-maybe (done-word)
  8644. "Check if the current headline contains a repeated deadline/schedule.
  8645. If yes, set TODO state back to what it was and change the base date
  8646. of repeating deadline/scheduled time stamps to new date.
  8647. This function is run automatically after each state change to a DONE state."
  8648. ;; last-state is dynamically scoped into this function
  8649. (let* ((repeat (org-get-repeat))
  8650. (aa (assoc last-state org-todo-kwd-alist))
  8651. (interpret (nth 1 aa))
  8652. (head (nth 2 aa))
  8653. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8654. (msg "Entry repeats: ")
  8655. (org-log-done nil)
  8656. (org-todo-log-states nil)
  8657. (nshiftmax 10) (nshift 0)
  8658. re type n what ts time)
  8659. (when repeat
  8660. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8661. (org-todo (if (eq interpret 'type) last-state head))
  8662. (org-entry-put nil "LAST_REPEAT" (format-time-string
  8663. (org-time-stamp-format t t)))
  8664. (when org-log-repeat
  8665. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8666. (memq 'org-add-log-note post-command-hook))
  8667. ;; OK, we are already setup for some record
  8668. (if (eq org-log-repeat 'note)
  8669. ;; make sure we take a note, not only a time stamp
  8670. (setq org-log-note-how 'note))
  8671. ;; Set up for taking a record
  8672. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8673. last-state
  8674. 'findpos org-log-repeat)))
  8675. (org-back-to-heading t)
  8676. (org-add-planning-info nil nil 'closed)
  8677. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8678. org-deadline-time-regexp "\\)\\|\\("
  8679. org-ts-regexp "\\)"))
  8680. (while (re-search-forward
  8681. re (save-excursion (outline-next-heading) (point)) t)
  8682. (setq type (if (match-end 1) org-scheduled-string
  8683. (if (match-end 3) org-deadline-string "Plain:"))
  8684. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8685. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8686. (setq n (string-to-number (match-string 2 ts))
  8687. what (match-string 3 ts))
  8688. (if (equal what "w") (setq n (* n 7) what "d"))
  8689. ;; Preparation, see if we need to modify the start date for the change
  8690. (when (match-end 1)
  8691. (setq time (save-match-data (org-time-string-to-time ts)))
  8692. (cond
  8693. ((equal (match-string 1 ts) ".")
  8694. ;; Shift starting date to today
  8695. (org-timestamp-change
  8696. (- (time-to-days (current-time)) (time-to-days time))
  8697. 'day))
  8698. ((equal (match-string 1 ts) "+")
  8699. (while (or (= nshift 0)
  8700. (<= (time-to-days time) (time-to-days (current-time))))
  8701. (when (= (incf nshift) nshiftmax)
  8702. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8703. (error "Abort")))
  8704. (org-timestamp-change n (cdr (assoc what whata)))
  8705. (org-at-timestamp-p t)
  8706. (setq ts (match-string 1))
  8707. (setq time (save-match-data (org-time-string-to-time ts))))
  8708. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8709. ;; rematch, so that we have everything in place for the real shift
  8710. (org-at-timestamp-p t)
  8711. (setq ts (match-string 1))
  8712. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8713. (org-timestamp-change n (cdr (assoc what whata)))
  8714. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8715. (setq org-log-post-message msg)
  8716. (message "%s" msg))))
  8717. (defun org-show-todo-tree (arg)
  8718. "Make a compact tree which shows all headlines marked with TODO.
  8719. The tree will show the lines where the regexp matches, and all higher
  8720. headlines above the match.
  8721. With a \\[universal-argument] prefix, prompt for a regexp to match.
  8722. With a numeric prefix N, construct a sparse tree for the Nth element
  8723. of `org-todo-keywords-1'."
  8724. (interactive "P")
  8725. (let ((case-fold-search nil)
  8726. (kwd-re
  8727. (cond ((null arg) org-not-done-regexp)
  8728. ((equal arg '(4))
  8729. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8730. (mapcar 'list org-todo-keywords-1))))
  8731. (concat "\\("
  8732. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8733. "\\)\\>")))
  8734. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8735. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8736. org-todo-keywords-1)))
  8737. (t (error "Invalid prefix argument: %s" arg)))))
  8738. (message "%d TODO entries found"
  8739. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8740. (defun org-deadline (&optional remove time)
  8741. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8742. With argument REMOVE, remove any deadline from the item.
  8743. When TIME is set, it should be an internal time specification, and the
  8744. scheduling will use the corresponding date."
  8745. (interactive "P")
  8746. (if remove
  8747. (progn
  8748. (org-remove-timestamp-with-keyword org-deadline-string)
  8749. (message "Item no longer has a deadline."))
  8750. (if (org-get-repeat)
  8751. (error "Cannot change deadline on task with repeater, please do that by hand")
  8752. (org-add-planning-info 'deadline time 'closed)
  8753. (message "Deadline on %s" org-last-inserted-timestamp))))
  8754. (defun org-schedule (&optional remove time)
  8755. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8756. With argument REMOVE, remove any scheduling date from the item.
  8757. When TIME is set, it should be an internal time specification, and the
  8758. scheduling will use the corresponding date."
  8759. (interactive "P")
  8760. (if remove
  8761. (progn
  8762. (org-remove-timestamp-with-keyword org-scheduled-string)
  8763. (message "Item is no longer scheduled."))
  8764. (if (org-get-repeat)
  8765. (error "Cannot reschedule task with repeater, please do that by hand")
  8766. (org-add-planning-info 'scheduled time 'closed)
  8767. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8768. (defun org-get-scheduled-time (pom &optional inherit)
  8769. "Get the scheduled time as a time tuple, of a format suitable
  8770. for calling org-schedule with, or if there is no scheduling,
  8771. returns nil."
  8772. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8773. (when time
  8774. (apply 'encode-time (org-parse-time-string time)))))
  8775. (defun org-get-deadline-time (pom &optional inherit)
  8776. "Get the deadine as a time tuple, of a format suitable for
  8777. calling org-deadlin with, or if there is no scheduling, returns
  8778. nil."
  8779. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8780. (when time
  8781. (apply 'encode-time (org-parse-time-string time)))))
  8782. (defun org-remove-timestamp-with-keyword (keyword)
  8783. "Remove all time stamps with KEYWORD in the current entry."
  8784. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8785. beg)
  8786. (save-excursion
  8787. (org-back-to-heading t)
  8788. (setq beg (point))
  8789. (org-end-of-subtree t t)
  8790. (while (re-search-backward re beg t)
  8791. (replace-match "")
  8792. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8793. (equal (char-before) ?\ ))
  8794. (backward-delete-char 1)
  8795. (if (string-match "^[ \t]*$" (buffer-substring
  8796. (point-at-bol) (point-at-eol)))
  8797. (delete-region (point-at-bol)
  8798. (min (point-max) (1+ (point-at-eol))))))))))
  8799. (defun org-add-planning-info (what &optional time &rest remove)
  8800. "Insert new timestamp with keyword in the line directly after the headline.
  8801. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8802. If non is given, the user is prompted for a date.
  8803. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8804. be removed."
  8805. (interactive)
  8806. (let (org-time-was-given org-end-time-was-given ts
  8807. end default-time default-input)
  8808. (catch 'exit
  8809. (when (and (not time) (memq what '(scheduled deadline)))
  8810. ;; Try to get a default date/time from existing timestamp
  8811. (save-excursion
  8812. (org-back-to-heading t)
  8813. (setq end (save-excursion (outline-next-heading) (point)))
  8814. (when (re-search-forward (if (eq what 'scheduled)
  8815. org-scheduled-time-regexp
  8816. org-deadline-time-regexp)
  8817. end t)
  8818. (setq ts (match-string 1)
  8819. default-time
  8820. (apply 'encode-time (org-parse-time-string ts))
  8821. default-input (and ts (org-get-compact-tod ts))))))
  8822. (when what
  8823. ;; If necessary, get the time from the user
  8824. (setq time (or time (org-read-date nil 'to-time nil nil
  8825. default-time default-input))))
  8826. (when (and org-insert-labeled-timestamps-at-point
  8827. (member what '(scheduled deadline)))
  8828. (insert
  8829. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8830. (org-insert-time-stamp time org-time-was-given
  8831. nil nil nil (list org-end-time-was-given))
  8832. (setq what nil))
  8833. (save-excursion
  8834. (save-restriction
  8835. (let (col list elt ts buffer-invisibility-spec)
  8836. (org-back-to-heading t)
  8837. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8838. (goto-char (match-end 1))
  8839. (setq col (current-column))
  8840. (goto-char (match-end 0))
  8841. (if (eobp) (insert "\n") (forward-char 1))
  8842. (when (and (not what)
  8843. (not (looking-at
  8844. (concat "[ \t]*"
  8845. org-keyword-time-not-clock-regexp))))
  8846. ;; Nothing to add, nothing to remove...... :-)
  8847. (throw 'exit nil))
  8848. (if (and (not (looking-at outline-regexp))
  8849. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8850. "[^\r\n]*"))
  8851. (not (equal (match-string 1) org-clock-string)))
  8852. (narrow-to-region (match-beginning 0) (match-end 0))
  8853. (insert-before-markers "\n")
  8854. (backward-char 1)
  8855. (narrow-to-region (point) (point))
  8856. (and org-adapt-indentation (org-indent-to-column col)))
  8857. ;; Check if we have to remove something.
  8858. (setq list (cons what remove))
  8859. (while list
  8860. (setq elt (pop list))
  8861. (goto-char (point-min))
  8862. (when (or (and (eq elt 'scheduled)
  8863. (re-search-forward org-scheduled-time-regexp nil t))
  8864. (and (eq elt 'deadline)
  8865. (re-search-forward org-deadline-time-regexp nil t))
  8866. (and (eq elt 'closed)
  8867. (re-search-forward org-closed-time-regexp nil t)))
  8868. (replace-match "")
  8869. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8870. (if (looking-at " +") (replace-match ""))))
  8871. (goto-char (point-max))
  8872. (when what
  8873. (insert
  8874. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8875. (cond ((eq what 'scheduled) org-scheduled-string)
  8876. ((eq what 'deadline) org-deadline-string)
  8877. ((eq what 'closed) org-closed-string))
  8878. " ")
  8879. (setq ts (org-insert-time-stamp
  8880. time
  8881. (or org-time-was-given
  8882. (and (eq what 'closed) org-log-done-with-time))
  8883. (eq what 'closed)
  8884. nil nil (list org-end-time-was-given)))
  8885. (end-of-line 1))
  8886. (goto-char (point-min))
  8887. (widen)
  8888. (if (and (looking-at "[ \t]+\n")
  8889. (equal (char-before) ?\n))
  8890. (delete-region (1- (point)) (point-at-eol)))
  8891. ts))))))
  8892. (defvar org-log-note-marker (make-marker))
  8893. (defvar org-log-note-purpose nil)
  8894. (defvar org-log-note-state nil)
  8895. (defvar org-log-note-previous-state nil)
  8896. (defvar org-log-note-how nil)
  8897. (defvar org-log-note-extra nil)
  8898. (defvar org-log-note-window-configuration nil)
  8899. (defvar org-log-note-return-to (make-marker))
  8900. (defvar org-log-post-message nil
  8901. "Message to be displayed after a log note has been stored.
  8902. The auto-repeater uses this.")
  8903. (defun org-add-note ()
  8904. "Add a note to the current entry.
  8905. This is done in the same way as adding a state change note."
  8906. (interactive)
  8907. (org-add-log-setup 'note nil nil 'findpos nil))
  8908. (defvar org-property-end-re)
  8909. (defun org-add-log-setup (&optional purpose state prev-state
  8910. findpos how &optional extra)
  8911. "Set up the post command hook to take a note.
  8912. If this is about to TODO state change, the new state is expected in STATE.
  8913. When FINDPOS is non-nil, find the correct position for the note in
  8914. the current entry. If not, assume that it can be inserted at point.
  8915. HOW is an indicator what kind of note should be created.
  8916. EXTRA is additional text that will be inserted into the notes buffer."
  8917. (let* ((org-log-into-drawer (org-log-into-drawer))
  8918. (drawer (cond ((stringp org-log-into-drawer)
  8919. org-log-into-drawer)
  8920. (org-log-into-drawer "LOGBOOK")
  8921. (t nil))))
  8922. (save-restriction
  8923. (save-excursion
  8924. (when findpos
  8925. (org-back-to-heading t)
  8926. (narrow-to-region (point) (save-excursion
  8927. (outline-next-heading) (point)))
  8928. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8929. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8930. "[^\r\n]*\\)?"))
  8931. (goto-char (match-end 0))
  8932. (cond
  8933. (drawer
  8934. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8935. nil t)
  8936. (progn
  8937. (goto-char (match-end 0))
  8938. (or org-log-states-order-reversed
  8939. (and (re-search-forward org-property-end-re nil t)
  8940. (goto-char (1- (match-beginning 0))))))
  8941. (insert "\n:" drawer ":\n:END:")
  8942. (beginning-of-line 0)
  8943. (org-indent-line-function)
  8944. (beginning-of-line 2)
  8945. (org-indent-line-function)
  8946. (end-of-line 0)))
  8947. ((and org-log-state-notes-insert-after-drawers
  8948. (save-excursion
  8949. (forward-line) (looking-at org-drawer-regexp)))
  8950. (forward-line)
  8951. (while (looking-at org-drawer-regexp)
  8952. (goto-char (match-end 0))
  8953. (re-search-forward org-property-end-re (point-max) t)
  8954. (forward-line))
  8955. (forward-line -1)))
  8956. (unless org-log-states-order-reversed
  8957. (and (= (char-after) ?\n) (forward-char 1))
  8958. (org-skip-over-state-notes)
  8959. (skip-chars-backward " \t\n\r")))
  8960. (move-marker org-log-note-marker (point))
  8961. (setq org-log-note-purpose purpose
  8962. org-log-note-state state
  8963. org-log-note-previous-state prev-state
  8964. org-log-note-how how
  8965. org-log-note-extra extra)
  8966. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8967. (defun org-skip-over-state-notes ()
  8968. "Skip past the list of State notes in an entry."
  8969. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8970. (while (looking-at "[ \t]*- State")
  8971. (condition-case nil
  8972. (org-next-item)
  8973. (error (org-end-of-item)))))
  8974. (defun org-add-log-note (&optional purpose)
  8975. "Pop up a window for taking a note, and add this note later at point."
  8976. (remove-hook 'post-command-hook 'org-add-log-note)
  8977. (setq org-log-note-window-configuration (current-window-configuration))
  8978. (delete-other-windows)
  8979. (move-marker org-log-note-return-to (point))
  8980. (switch-to-buffer (marker-buffer org-log-note-marker))
  8981. (goto-char org-log-note-marker)
  8982. (org-switch-to-buffer-other-window "*Org Note*")
  8983. (erase-buffer)
  8984. (if (memq org-log-note-how '(time state))
  8985. (let (current-prefix-arg) (org-store-log-note))
  8986. (let ((org-inhibit-startup t)) (org-mode))
  8987. (insert (format "# Insert note for %s.
  8988. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8989. (cond
  8990. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8991. ((eq org-log-note-purpose 'done) "closed todo item")
  8992. ((eq org-log-note-purpose 'state)
  8993. (format "state change from \"%s\" to \"%s\""
  8994. (or org-log-note-previous-state "")
  8995. (or org-log-note-state "")))
  8996. ((eq org-log-note-purpose 'note)
  8997. "this entry")
  8998. (t (error "This should not happen")))))
  8999. (if org-log-note-extra (insert org-log-note-extra))
  9000. (org-set-local 'org-finish-function 'org-store-log-note)))
  9001. (defvar org-note-abort nil) ; dynamically scoped
  9002. (defun org-store-log-note ()
  9003. "Finish taking a log note, and insert it to where it belongs."
  9004. (let ((txt (buffer-string))
  9005. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9006. lines ind)
  9007. (kill-buffer (current-buffer))
  9008. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9009. (setq txt (replace-match "" t t txt)))
  9010. (if (string-match "\\s-+\\'" txt)
  9011. (setq txt (replace-match "" t t txt)))
  9012. (setq lines (org-split-string txt "\n"))
  9013. (when (and note (string-match "\\S-" note))
  9014. (setq note
  9015. (org-replace-escapes
  9016. note
  9017. (list (cons "%u" (user-login-name))
  9018. (cons "%U" user-full-name)
  9019. (cons "%t" (format-time-string
  9020. (org-time-stamp-format 'long 'inactive)
  9021. (current-time)))
  9022. (cons "%s" (if org-log-note-state
  9023. (concat "\"" org-log-note-state "\"")
  9024. ""))
  9025. (cons "%S" (if org-log-note-previous-state
  9026. (concat "\"" org-log-note-previous-state "\"")
  9027. "\"\"")))))
  9028. (if lines (setq note (concat note " \\\\")))
  9029. (push note lines))
  9030. (when (or current-prefix-arg org-note-abort)
  9031. (when org-log-into-drawer
  9032. (org-remove-empty-drawer-at
  9033. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9034. org-log-note-marker))
  9035. (setq lines nil))
  9036. (when lines
  9037. (save-excursion
  9038. (set-buffer (marker-buffer org-log-note-marker))
  9039. (save-excursion
  9040. (goto-char org-log-note-marker)
  9041. (move-marker org-log-note-marker nil)
  9042. (end-of-line 1)
  9043. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9044. (insert "- " (pop lines))
  9045. (org-indent-line-function)
  9046. (beginning-of-line 1)
  9047. (looking-at "[ \t]*")
  9048. (setq ind (concat (match-string 0) " "))
  9049. (end-of-line 1)
  9050. (while lines (insert "\n" ind (pop lines)))
  9051. (message "Note stored")
  9052. (org-back-to-heading t)
  9053. (org-cycle-hide-drawers 'children)))))
  9054. (set-window-configuration org-log-note-window-configuration)
  9055. (with-current-buffer (marker-buffer org-log-note-return-to)
  9056. (goto-char org-log-note-return-to))
  9057. (move-marker org-log-note-return-to nil)
  9058. (and org-log-post-message (message "%s" org-log-post-message)))
  9059. (defun org-remove-empty-drawer-at (drawer pos)
  9060. "Remove an emptyr DARWER drawer at position POS.
  9061. POS may also be a marker."
  9062. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9063. (save-excursion
  9064. (save-restriction
  9065. (widen)
  9066. (goto-char pos)
  9067. (if (org-in-regexp
  9068. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9069. (replace-match ""))))))
  9070. (defun org-sparse-tree (&optional arg)
  9071. "Create a sparse tree, prompt for the details.
  9072. This command can create sparse trees. You first need to select the type
  9073. of match used to create the tree:
  9074. t Show entries with a specific TODO keyword.
  9075. m Show entries selected by a tags/property match.
  9076. p Enter a property name and its value (both with completion on existing
  9077. names/values) and show entries with that property.
  9078. r Show entries matching a regular expression.
  9079. d Show deadlines due within `org-deadline-warning-days'.
  9080. b Show deadlines and scheduled items before a date.
  9081. a Show deadlines and scheduled items after a date."
  9082. (interactive "P")
  9083. (let (ans kwd value)
  9084. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9085. (setq ans (read-char-exclusive))
  9086. (cond
  9087. ((equal ans ?d)
  9088. (call-interactively 'org-check-deadlines))
  9089. ((equal ans ?b)
  9090. (call-interactively 'org-check-before-date))
  9091. ((equal ans ?a)
  9092. (call-interactively 'org-check-after-date))
  9093. ((equal ans ?t)
  9094. (org-show-todo-tree '(4)))
  9095. ((member ans '(?T ?m))
  9096. (call-interactively 'org-match-sparse-tree))
  9097. ((member ans '(?p ?P))
  9098. (setq kwd (org-ido-completing-read "Property: "
  9099. (mapcar 'list (org-buffer-property-keys))))
  9100. (setq value (org-ido-completing-read "Value: "
  9101. (mapcar 'list (org-property-values kwd))))
  9102. (unless (string-match "\\`{.*}\\'" value)
  9103. (setq value (concat "\"" value "\"")))
  9104. (org-match-sparse-tree arg (concat kwd "=" value)))
  9105. ((member ans '(?r ?R ?/))
  9106. (call-interactively 'org-occur))
  9107. (t (error "No such sparse tree command \"%c\"" ans)))))
  9108. (defvar org-occur-highlights nil
  9109. "List of overlays used for occur matches.")
  9110. (make-variable-buffer-local 'org-occur-highlights)
  9111. (defvar org-occur-parameters nil
  9112. "Parameters of the active org-occur calls.
  9113. This is a list, each call to org-occur pushes as cons cell,
  9114. containing the regular expression and the callback, onto the list.
  9115. The list can contain several entries if `org-occur' has been called
  9116. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9117. will only contain one set of parameters. When the highlights are
  9118. removed (for example with `C-c C-c', or with the next edit (depending
  9119. on `org-remove-highlights-with-change'), this variable is emptied
  9120. as well.")
  9121. (make-variable-buffer-local 'org-occur-parameters)
  9122. (defun org-occur (regexp &optional keep-previous callback)
  9123. "Make a compact tree which shows all matches of REGEXP.
  9124. The tree will show the lines where the regexp matches, and all higher
  9125. headlines above the match. It will also show the heading after the match,
  9126. to make sure editing the matching entry is easy.
  9127. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9128. call to `org-occur' will be kept, to allow stacking of calls to this
  9129. command.
  9130. If CALLBACK is non-nil, it is a function which is called to confirm
  9131. that the match should indeed be shown."
  9132. (interactive "sRegexp: \nP")
  9133. (unless keep-previous
  9134. (org-remove-occur-highlights nil nil t))
  9135. (push (cons regexp callback) org-occur-parameters)
  9136. (let ((cnt 0))
  9137. (save-excursion
  9138. (goto-char (point-min))
  9139. (if (or (not keep-previous) ; do not want to keep
  9140. (not org-occur-highlights)) ; no previous matches
  9141. ;; hide everything
  9142. (org-overview))
  9143. (while (re-search-forward regexp nil t)
  9144. (when (or (not callback)
  9145. (save-match-data (funcall callback)))
  9146. (setq cnt (1+ cnt))
  9147. (when org-highlight-sparse-tree-matches
  9148. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9149. (org-show-context 'occur-tree))))
  9150. (when org-remove-highlights-with-change
  9151. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9152. nil 'local))
  9153. (unless org-sparse-tree-open-archived-trees
  9154. (org-hide-archived-subtrees (point-min) (point-max)))
  9155. (run-hooks 'org-occur-hook)
  9156. (if (interactive-p)
  9157. (message "%d match(es) for regexp %s" cnt regexp))
  9158. cnt))
  9159. (defun org-show-context (&optional key)
  9160. "Make sure point and context and visible.
  9161. How much context is shown depends upon the variables
  9162. `org-show-hierarchy-above', `org-show-following-heading'. and
  9163. `org-show-siblings'."
  9164. (let ((heading-p (org-on-heading-p t))
  9165. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9166. (following-p (org-get-alist-option org-show-following-heading key))
  9167. (entry-p (org-get-alist-option org-show-entry-below key))
  9168. (siblings-p (org-get-alist-option org-show-siblings key)))
  9169. (catch 'exit
  9170. ;; Show heading or entry text
  9171. (if (and heading-p (not entry-p))
  9172. (org-flag-heading nil) ; only show the heading
  9173. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9174. (org-show-hidden-entry))) ; show entire entry
  9175. (when following-p
  9176. ;; Show next sibling, or heading below text
  9177. (save-excursion
  9178. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9179. (org-flag-heading nil))))
  9180. (when siblings-p (org-show-siblings))
  9181. (when hierarchy-p
  9182. ;; show all higher headings, possibly with siblings
  9183. (save-excursion
  9184. (while (and (condition-case nil
  9185. (progn (org-up-heading-all 1) t)
  9186. (error nil))
  9187. (not (bobp)))
  9188. (org-flag-heading nil)
  9189. (when siblings-p (org-show-siblings))))))))
  9190. (defun org-reveal (&optional siblings)
  9191. "Show current entry, hierarchy above it, and the following headline.
  9192. This can be used to show a consistent set of context around locations
  9193. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9194. not t for the search context.
  9195. With optional argument SIBLINGS, on each level of the hierarchy all
  9196. siblings are shown. This repairs the tree structure to what it would
  9197. look like when opened with hierarchical calls to `org-cycle'."
  9198. (interactive "P")
  9199. (let ((org-show-hierarchy-above t)
  9200. (org-show-following-heading t)
  9201. (org-show-siblings (if siblings t org-show-siblings)))
  9202. (org-show-context nil)))
  9203. (defun org-highlight-new-match (beg end)
  9204. "Highlight from BEG to END and mark the highlight is an occur headline."
  9205. (let ((ov (org-make-overlay beg end)))
  9206. (org-overlay-put ov 'face 'secondary-selection)
  9207. (push ov org-occur-highlights)))
  9208. (defun org-remove-occur-highlights (&optional beg end noremove)
  9209. "Remove the occur highlights from the buffer.
  9210. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9211. from the `before-change-functions' in the current buffer."
  9212. (interactive)
  9213. (unless org-inhibit-highlight-removal
  9214. (mapc 'org-delete-overlay org-occur-highlights)
  9215. (setq org-occur-highlights nil)
  9216. (setq org-occur-parameters nil)
  9217. (unless noremove
  9218. (remove-hook 'before-change-functions
  9219. 'org-remove-occur-highlights 'local))))
  9220. ;;;; Priorities
  9221. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9222. "Regular expression matching the priority indicator.")
  9223. (defvar org-remove-priority-next-time nil)
  9224. (defun org-priority-up ()
  9225. "Increase the priority of the current item."
  9226. (interactive)
  9227. (org-priority 'up))
  9228. (defun org-priority-down ()
  9229. "Decrease the priority of the current item."
  9230. (interactive)
  9231. (org-priority 'down))
  9232. (defun org-priority (&optional action)
  9233. "Change the priority of an item by ARG.
  9234. ACTION can be `set', `up', `down', or a character."
  9235. (interactive)
  9236. (unless org-enable-priority-commands
  9237. (error "Priority commands are disabled"))
  9238. (setq action (or action 'set))
  9239. (let (current new news have remove)
  9240. (save-excursion
  9241. (org-back-to-heading t)
  9242. (if (looking-at org-priority-regexp)
  9243. (setq current (string-to-char (match-string 2))
  9244. have t)
  9245. (setq current org-default-priority))
  9246. (cond
  9247. ((or (eq action 'set)
  9248. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9249. (if (not (eq action 'set))
  9250. (setq new action)
  9251. (message "Priority %c-%c, SPC to remove: "
  9252. org-highest-priority org-lowest-priority)
  9253. (setq new (read-char-exclusive)))
  9254. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9255. (= (upcase org-lowest-priority) org-lowest-priority))
  9256. (setq new (upcase new)))
  9257. (cond ((equal new ?\ ) (setq remove t))
  9258. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9259. (error "Priority must be between `%c' and `%c'"
  9260. org-highest-priority org-lowest-priority))))
  9261. ((eq action 'up)
  9262. (if (and (not have) (eq last-command this-command))
  9263. (setq new org-lowest-priority)
  9264. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9265. org-default-priority (1- current)))))
  9266. ((eq action 'down)
  9267. (if (and (not have) (eq last-command this-command))
  9268. (setq new org-highest-priority)
  9269. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9270. org-default-priority (1+ current)))))
  9271. (t (error "Invalid action")))
  9272. (if (or (< (upcase new) org-highest-priority)
  9273. (> (upcase new) org-lowest-priority))
  9274. (setq remove t))
  9275. (setq news (format "%c" new))
  9276. (if have
  9277. (if remove
  9278. (replace-match "" t t nil 1)
  9279. (replace-match news t t nil 2))
  9280. (if remove
  9281. (error "No priority cookie found in line")
  9282. (let ((case-fold-search nil))
  9283. (looking-at org-todo-line-regexp))
  9284. (if (match-end 2)
  9285. (progn
  9286. (goto-char (match-end 2))
  9287. (insert " [#" news "]"))
  9288. (goto-char (match-beginning 3))
  9289. (insert "[#" news "] ")))))
  9290. (org-preserve-lc (org-set-tags nil 'align))
  9291. (if remove
  9292. (message "Priority removed")
  9293. (message "Priority of current item set to %s" news))))
  9294. (defun org-get-priority (s)
  9295. "Find priority cookie and return priority."
  9296. (save-match-data
  9297. (if (not (string-match org-priority-regexp s))
  9298. (* 1000 (- org-lowest-priority org-default-priority))
  9299. (* 1000 (- org-lowest-priority
  9300. (string-to-char (match-string 2 s)))))))
  9301. ;;;; Tags
  9302. (defvar org-agenda-archives-mode)
  9303. (defvar org-map-continue-from nil
  9304. "Position from where mapping should continue.
  9305. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9306. (defvar org-scanner-tags nil
  9307. "The current tag list while the tags scanner is running.")
  9308. (defvar org-trust-scanner-tags nil
  9309. "Should `org-get-tags-at' use the tags fro the scanner.
  9310. This is for internal dynamical scoping only.
  9311. When this is non-nil, the function `org-get-tags-at' will return the value
  9312. of `org-scanner-tags' instead of building the list by itself. This
  9313. can lead to large speed-ups when the tags scanner is used in a file with
  9314. many entries, and when the list of tags is retrieved, for example to
  9315. obtain a list of properties. Building the tags list for each entry in such
  9316. a file becomes an N^2 operation - but with this variable set, it scales
  9317. as N.")
  9318. (defun org-scan-tags (action matcher &optional todo-only)
  9319. "Scan headline tags with inheritance and produce output ACTION.
  9320. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9321. or `agenda' to produce an entry list for an agenda view. It can also be
  9322. a Lisp form or a function that should be called at each matched headline, in
  9323. this case the return value is a list of all return values from these calls.
  9324. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9325. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9326. only lines with a TODO keyword are included in the output."
  9327. (require 'org-agenda)
  9328. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9329. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9330. (org-re
  9331. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9332. (props (list 'face 'default
  9333. 'done-face 'org-agenda-done
  9334. 'undone-face 'default
  9335. 'mouse-face 'highlight
  9336. 'org-not-done-regexp org-not-done-regexp
  9337. 'org-todo-regexp org-todo-regexp
  9338. 'keymap org-agenda-keymap
  9339. 'help-echo
  9340. (format "mouse-2 or RET jump to org file %s"
  9341. (abbreviate-file-name
  9342. (or (buffer-file-name (buffer-base-buffer))
  9343. (buffer-name (buffer-base-buffer)))))))
  9344. (case-fold-search nil)
  9345. (org-map-continue-from nil)
  9346. lspos tags tags-list
  9347. (tags-alist (list (cons 0 org-file-tags)))
  9348. (llast 0) rtn rtn1 level category i txt
  9349. todo marker entry priority)
  9350. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9351. (setq action (list 'lambda nil action)))
  9352. (save-excursion
  9353. (goto-char (point-min))
  9354. (when (eq action 'sparse-tree)
  9355. (org-overview)
  9356. (org-remove-occur-highlights))
  9357. (while (re-search-forward re nil t)
  9358. (catch :skip
  9359. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9360. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9361. (goto-char (setq lspos (match-beginning 0)))
  9362. (setq level (org-reduced-level (funcall outline-level))
  9363. category (org-get-category))
  9364. (setq i llast llast level)
  9365. ;; remove tag lists from same and sublevels
  9366. (while (>= i level)
  9367. (when (setq entry (assoc i tags-alist))
  9368. (setq tags-alist (delete entry tags-alist)))
  9369. (setq i (1- i)))
  9370. ;; add the next tags
  9371. (when tags
  9372. (setq tags (org-split-string tags ":")
  9373. tags-alist
  9374. (cons (cons level tags) tags-alist)))
  9375. ;; compile tags for current headline
  9376. (setq tags-list
  9377. (if org-use-tag-inheritance
  9378. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9379. tags)
  9380. org-scanner-tags tags-list)
  9381. (when org-use-tag-inheritance
  9382. (setcdr (car tags-alist)
  9383. (mapcar (lambda (x)
  9384. (setq x (copy-sequence x))
  9385. (org-add-prop-inherited x))
  9386. (cdar tags-alist))))
  9387. (when (and tags org-use-tag-inheritance
  9388. (or (not (eq t org-use-tag-inheritance))
  9389. org-tags-exclude-from-inheritance))
  9390. ;; selective inheritance, remove uninherited ones
  9391. (setcdr (car tags-alist)
  9392. (org-remove-uniherited-tags (cdar tags-alist))))
  9393. (when (and (or (not todo-only)
  9394. (and (member todo org-not-done-keywords)
  9395. (or (not org-agenda-tags-todo-honor-ignore-options)
  9396. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9397. (let ((case-fold-search t)) (eval matcher))
  9398. (or
  9399. (not (member org-archive-tag tags-list))
  9400. ;; we have an archive tag, should we use this anyway?
  9401. (or (not org-agenda-skip-archived-trees)
  9402. (and (eq action 'agenda) org-agenda-archives-mode))))
  9403. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9404. ;; select this headline
  9405. (cond
  9406. ((eq action 'sparse-tree)
  9407. (and org-highlight-sparse-tree-matches
  9408. (org-get-heading) (match-end 0)
  9409. (org-highlight-new-match
  9410. (match-beginning 0) (match-beginning 1)))
  9411. (org-show-context 'tags-tree))
  9412. ((eq action 'agenda)
  9413. (setq txt (org-format-agenda-item
  9414. ""
  9415. (concat
  9416. (if (eq org-tags-match-list-sublevels 'indented)
  9417. (make-string (1- level) ?.) "")
  9418. (org-get-heading))
  9419. category
  9420. tags-list
  9421. )
  9422. priority (org-get-priority txt))
  9423. (goto-char lspos)
  9424. (setq marker (org-agenda-new-marker))
  9425. (org-add-props txt props
  9426. 'org-marker marker 'org-hd-marker marker 'org-category category
  9427. 'todo-state todo
  9428. 'priority priority 'type "tagsmatch")
  9429. (push txt rtn))
  9430. ((functionp action)
  9431. (setq org-map-continue-from nil)
  9432. (save-excursion
  9433. (setq rtn1 (funcall action))
  9434. (push rtn1 rtn)))
  9435. (t (error "Invalid action")))
  9436. ;; if we are to skip sublevels, jump to end of subtree
  9437. (unless org-tags-match-list-sublevels
  9438. (org-end-of-subtree t)
  9439. (backward-char 1))))
  9440. ;; Get the correct position from where to continue
  9441. (if org-map-continue-from
  9442. (goto-char org-map-continue-from)
  9443. (and (= (point) lspos) (end-of-line 1)))))
  9444. (when (and (eq action 'sparse-tree)
  9445. (not org-sparse-tree-open-archived-trees))
  9446. (org-hide-archived-subtrees (point-min) (point-max)))
  9447. (nreverse rtn)))
  9448. (defun org-remove-uniherited-tags (tags)
  9449. "Remove all tags that are not inherited from the list TAGS."
  9450. (cond
  9451. ((eq org-use-tag-inheritance t)
  9452. (if org-tags-exclude-from-inheritance
  9453. (org-delete-all org-tags-exclude-from-inheritance tags)
  9454. tags))
  9455. ((not org-use-tag-inheritance) nil)
  9456. ((stringp org-use-tag-inheritance)
  9457. (delq nil (mapcar
  9458. (lambda (x)
  9459. (if (and (string-match org-use-tag-inheritance x)
  9460. (not (member x org-tags-exclude-from-inheritance)))
  9461. x nil))
  9462. tags)))
  9463. ((listp org-use-tag-inheritance)
  9464. (delq nil (mapcar
  9465. (lambda (x)
  9466. (if (member x org-use-tag-inheritance) x nil))
  9467. tags)))))
  9468. (defvar todo-only) ;; dynamically scoped
  9469. (defun org-match-sparse-tree (&optional todo-only match)
  9470. "Create a sparse tree according to tags string MATCH.
  9471. MATCH can contain positive and negative selection of tags, like
  9472. \"+WORK+URGENT-WITHBOSS\".
  9473. If optional argument TODO-ONLY is non-nil, only select lines that are
  9474. also TODO lines."
  9475. (interactive "P")
  9476. (org-prepare-agenda-buffers (list (current-buffer)))
  9477. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9478. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9479. (defvar org-cached-props nil)
  9480. (defun org-cached-entry-get (pom property)
  9481. (if (or (eq t org-use-property-inheritance)
  9482. (and (stringp org-use-property-inheritance)
  9483. (string-match org-use-property-inheritance property))
  9484. (and (listp org-use-property-inheritance)
  9485. (member property org-use-property-inheritance)))
  9486. ;; Caching is not possible, check it directly
  9487. (org-entry-get pom property 'inherit)
  9488. ;; Get all properties, so that we can do complicated checks easily
  9489. (cdr (assoc property (or org-cached-props
  9490. (setq org-cached-props
  9491. (org-entry-properties pom)))))))
  9492. (defun org-global-tags-completion-table (&optional files)
  9493. "Return the list of all tags in all agenda buffer/files."
  9494. (save-excursion
  9495. (org-uniquify
  9496. (delq nil
  9497. (apply 'append
  9498. (mapcar
  9499. (lambda (file)
  9500. (set-buffer (find-file-noselect file))
  9501. (append (org-get-buffer-tags)
  9502. (mapcar (lambda (x) (if (stringp (car-safe x))
  9503. (list (car-safe x)) nil))
  9504. org-tag-alist)))
  9505. (if (and files (car files))
  9506. files
  9507. (org-agenda-files))))))))
  9508. (defun org-make-tags-matcher (match)
  9509. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9510. ;; todo-only is scoped dynamically into this function, and the function
  9511. ;; may change it if the matcher asks for it.
  9512. (unless match
  9513. ;; Get a new match request, with completion
  9514. (let ((org-last-tags-completion-table
  9515. (org-global-tags-completion-table)))
  9516. (setq match (org-completing-read-no-ido
  9517. "Match: " 'org-tags-completion-function nil nil nil
  9518. 'org-tags-history))))
  9519. ;; Parse the string and create a lisp form
  9520. (let ((match0 match)
  9521. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9522. minus tag mm
  9523. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9524. orterms term orlist re-p str-p level-p level-op time-p
  9525. prop-p pn pv po cat-p gv rest)
  9526. (if (string-match "/+" match)
  9527. ;; match contains also a todo-matching request
  9528. (progn
  9529. (setq tagsmatch (substring match 0 (match-beginning 0))
  9530. todomatch (substring match (match-end 0)))
  9531. (if (string-match "^!" todomatch)
  9532. (setq todo-only t todomatch (substring todomatch 1)))
  9533. (if (string-match "^\\s-*$" todomatch)
  9534. (setq todomatch nil)))
  9535. ;; only matching tags
  9536. (setq tagsmatch match todomatch nil))
  9537. ;; Make the tags matcher
  9538. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9539. (setq tagsmatcher t)
  9540. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9541. (while (setq term (pop orterms))
  9542. (while (and (equal (substring term -1) "\\") orterms)
  9543. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9544. (while (string-match re term)
  9545. (setq rest (substring term (match-end 0))
  9546. minus (and (match-end 1)
  9547. (equal (match-string 1 term) "-"))
  9548. tag (match-string 2 term)
  9549. re-p (equal (string-to-char tag) ?{)
  9550. level-p (match-end 4)
  9551. prop-p (match-end 5)
  9552. mm (cond
  9553. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9554. (level-p
  9555. (setq level-op (org-op-to-function (match-string 3 term)))
  9556. `(,level-op level ,(string-to-number
  9557. (match-string 4 term))))
  9558. (prop-p
  9559. (setq pn (match-string 5 term)
  9560. po (match-string 6 term)
  9561. pv (match-string 7 term)
  9562. cat-p (equal pn "CATEGORY")
  9563. re-p (equal (string-to-char pv) ?{)
  9564. str-p (equal (string-to-char pv) ?\")
  9565. time-p (save-match-data
  9566. (string-match "^\"[[<].*[]>]\"$" pv))
  9567. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9568. (if time-p (setq pv (org-matcher-time pv)))
  9569. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9570. (cond
  9571. ((equal pn "CATEGORY")
  9572. (setq gv '(get-text-property (point) 'org-category)))
  9573. ((equal pn "TODO")
  9574. (setq gv 'todo))
  9575. (t
  9576. (setq gv `(org-cached-entry-get nil ,pn))))
  9577. (if re-p
  9578. (if (eq po 'org<>)
  9579. `(not (string-match ,pv (or ,gv "")))
  9580. `(string-match ,pv (or ,gv "")))
  9581. (if str-p
  9582. `(,po (or ,gv "") ,pv)
  9583. `(,po (string-to-number (or ,gv ""))
  9584. ,(string-to-number pv) ))))
  9585. (t `(member ,tag tags-list)))
  9586. mm (if minus (list 'not mm) mm)
  9587. term rest)
  9588. (push mm tagsmatcher))
  9589. (push (if (> (length tagsmatcher) 1)
  9590. (cons 'and tagsmatcher)
  9591. (car tagsmatcher))
  9592. orlist)
  9593. (setq tagsmatcher nil))
  9594. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9595. (setq tagsmatcher
  9596. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9597. ;; Make the todo matcher
  9598. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9599. (setq todomatcher t)
  9600. (setq orterms (org-split-string todomatch "|") orlist nil)
  9601. (while (setq term (pop orterms))
  9602. (while (string-match re term)
  9603. (setq minus (and (match-end 1)
  9604. (equal (match-string 1 term) "-"))
  9605. kwd (match-string 2 term)
  9606. re-p (equal (string-to-char kwd) ?{)
  9607. term (substring term (match-end 0))
  9608. mm (if re-p
  9609. `(string-match ,(substring kwd 1 -1) todo)
  9610. (list 'equal 'todo kwd))
  9611. mm (if minus (list 'not mm) mm))
  9612. (push mm todomatcher))
  9613. (push (if (> (length todomatcher) 1)
  9614. (cons 'and todomatcher)
  9615. (car todomatcher))
  9616. orlist)
  9617. (setq todomatcher nil))
  9618. (setq todomatcher (if (> (length orlist) 1)
  9619. (cons 'or orlist) (car orlist))))
  9620. ;; Return the string and lisp forms of the matcher
  9621. (setq matcher (if todomatcher
  9622. (list 'and tagsmatcher todomatcher)
  9623. tagsmatcher))
  9624. (cons match0 matcher)))
  9625. (defun org-op-to-function (op &optional stringp)
  9626. "Turn an operator into the appropriate function."
  9627. (setq op
  9628. (cond
  9629. ((equal op "<" ) '(< string< org-time<))
  9630. ((equal op ">" ) '(> org-string> org-time>))
  9631. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9632. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9633. ((member op '("=" "==")) '(= string= org-time=))
  9634. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9635. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9636. (defun org<> (a b) (not (= a b)))
  9637. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9638. (defun org-string>= (a b) (not (string< a b)))
  9639. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9640. (defun org-string<> (a b) (not (string= a b)))
  9641. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9642. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9643. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9644. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9645. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9646. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9647. (defun org-2ft (s)
  9648. "Convert S to a floating point time.
  9649. If S is already a number, just return it. If it is a string, parse
  9650. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9651. (cond
  9652. ((numberp s) s)
  9653. ((stringp s)
  9654. (condition-case nil
  9655. (float-time (apply 'encode-time (org-parse-time-string s)))
  9656. (error 0.)))
  9657. (t 0.)))
  9658. (defun org-time-today ()
  9659. "Time in seconds today at 0:00.
  9660. Returns the float number of seconds since the beginning of the
  9661. epoch to the beginning of today (00:00)."
  9662. (float-time (apply 'encode-time
  9663. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9664. (defun org-matcher-time (s)
  9665. "Interpret a time comparison value."
  9666. (save-match-data
  9667. (cond
  9668. ((string= s "<now>") (float-time))
  9669. ((string= s "<today>") (org-time-today))
  9670. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9671. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9672. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9673. (+ (org-time-today)
  9674. (* (string-to-number (match-string 1 s))
  9675. (cdr (assoc (match-string 2 s)
  9676. '(("d" . 86400.0) ("w" . 604800.0)
  9677. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9678. (t (org-2ft s)))))
  9679. (defun org-match-any-p (re list)
  9680. "Does re match any element of list?"
  9681. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9682. (delq nil list))
  9683. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9684. (defvar org-tags-overlay (org-make-overlay 1 1))
  9685. (org-detach-overlay org-tags-overlay)
  9686. (defun org-get-local-tags-at (&optional pos)
  9687. "Get a list of tags defined in the current headline."
  9688. (org-get-tags-at pos 'local))
  9689. (defun org-get-local-tags ()
  9690. "Get a list of tags defined in the current headline."
  9691. (org-get-tags-at nil 'local))
  9692. (defun org-get-tags-at (&optional pos local)
  9693. "Get a list of all headline tags applicable at POS.
  9694. POS defaults to point. If tags are inherited, the list contains
  9695. the targets in the same sequence as the headlines appear, i.e.
  9696. the tags of the current headline come last.
  9697. When LOCAL is non-nil, only return tags from the current headline,
  9698. ignore inherited ones."
  9699. (interactive)
  9700. (if (and org-trust-scanner-tags
  9701. (or (not pos) (equal pos (point)))
  9702. (not local))
  9703. org-scanner-tags
  9704. (let (tags ltags lastpos parent)
  9705. (save-excursion
  9706. (save-restriction
  9707. (widen)
  9708. (goto-char (or pos (point)))
  9709. (save-match-data
  9710. (catch 'done
  9711. (condition-case nil
  9712. (progn
  9713. (org-back-to-heading t)
  9714. (while (not (equal lastpos (point)))
  9715. (setq lastpos (point))
  9716. (when (looking-at
  9717. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9718. (setq ltags (org-split-string
  9719. (org-match-string-no-properties 1) ":"))
  9720. (when parent
  9721. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9722. (setq tags (append
  9723. (if parent
  9724. (org-remove-uniherited-tags ltags)
  9725. ltags)
  9726. tags)))
  9727. (or org-use-tag-inheritance (throw 'done t))
  9728. (if local (throw 'done t))
  9729. (or (org-up-heading-safe) (error nil))
  9730. (setq parent t)))
  9731. (error nil)))))
  9732. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9733. (defun org-add-prop-inherited (s)
  9734. (add-text-properties 0 (length s) '(inherited t) s)
  9735. s)
  9736. (defun org-toggle-tag (tag &optional onoff)
  9737. "Toggle the tag TAG for the current line.
  9738. If ONOFF is `on' or `off', don't toggle but set to this state."
  9739. (let (res current)
  9740. (save-excursion
  9741. (org-back-to-heading t)
  9742. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9743. (point-at-eol) t)
  9744. (progn
  9745. (setq current (match-string 1))
  9746. (replace-match ""))
  9747. (setq current ""))
  9748. (setq current (nreverse (org-split-string current ":")))
  9749. (cond
  9750. ((eq onoff 'on)
  9751. (setq res t)
  9752. (or (member tag current) (push tag current)))
  9753. ((eq onoff 'off)
  9754. (or (not (member tag current)) (setq current (delete tag current))))
  9755. (t (if (member tag current)
  9756. (setq current (delete tag current))
  9757. (setq res t)
  9758. (push tag current))))
  9759. (end-of-line 1)
  9760. (if current
  9761. (progn
  9762. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9763. (org-set-tags nil t))
  9764. (delete-horizontal-space))
  9765. (run-hooks 'org-after-tags-change-hook))
  9766. res))
  9767. (defun org-align-tags-here (to-col)
  9768. ;; Assumes that this is a headline
  9769. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9770. (beginning-of-line 1)
  9771. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9772. (< pos (match-beginning 2)))
  9773. (progn
  9774. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9775. (goto-char (match-beginning 1))
  9776. (insert " ")
  9777. (delete-region (point) (1+ (match-beginning 2)))
  9778. (setq ncol (max (1+ (current-column))
  9779. (1+ col)
  9780. (if (> to-col 0)
  9781. to-col
  9782. (- (abs to-col) tags-l))))
  9783. (setq p (point))
  9784. (insert (make-string (- ncol (current-column)) ?\ ))
  9785. (setq ncol (current-column))
  9786. (when indent-tabs-mode (tabify p (point-at-eol)))
  9787. (org-move-to-column (min ncol col) t))
  9788. (goto-char pos))))
  9789. (defun org-set-tags-command (&optional arg just-align)
  9790. "Call the set-tags command for the current entry."
  9791. (interactive "P")
  9792. (if (org-on-heading-p)
  9793. (org-set-tags arg just-align)
  9794. (save-excursion
  9795. (org-back-to-heading t)
  9796. (org-set-tags arg just-align))))
  9797. (defun org-set-tags (&optional arg just-align)
  9798. "Set the tags for the current headline.
  9799. With prefix ARG, realign all tags in headings in the current buffer."
  9800. (interactive "P")
  9801. (let* ((re (concat "^" outline-regexp))
  9802. (current (org-get-tags-string))
  9803. (col (current-column))
  9804. (org-setting-tags t)
  9805. table current-tags inherited-tags ; computed below when needed
  9806. tags p0 c0 c1 rpl)
  9807. (if arg
  9808. (save-excursion
  9809. (goto-char (point-min))
  9810. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9811. (while (re-search-forward re nil t)
  9812. (org-set-tags nil t)
  9813. (end-of-line 1)))
  9814. (message "All tags realigned to column %d" org-tags-column))
  9815. (if just-align
  9816. (setq tags current)
  9817. ;; Get a new set of tags from the user
  9818. (save-excursion
  9819. (setq table (append org-tag-persistent-alist
  9820. (or org-tag-alist (org-get-buffer-tags)))
  9821. org-last-tags-completion-table table
  9822. current-tags (org-split-string current ":")
  9823. inherited-tags (nreverse
  9824. (nthcdr (length current-tags)
  9825. (nreverse (org-get-tags-at))))
  9826. tags
  9827. (if (or (eq t org-use-fast-tag-selection)
  9828. (and org-use-fast-tag-selection
  9829. (delq nil (mapcar 'cdr table))))
  9830. (org-fast-tag-selection
  9831. current-tags inherited-tags table
  9832. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9833. (let ((org-add-colon-after-tag-completion t))
  9834. (org-trim
  9835. (org-without-partial-completion
  9836. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9837. nil nil current 'org-tags-history)))))))
  9838. (while (string-match "[-+&]+" tags)
  9839. ;; No boolean logic, just a list
  9840. (setq tags (replace-match ":" t t tags))))
  9841. (if (string-match "\\`[\t ]*\\'" tags)
  9842. (setq tags "")
  9843. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9844. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9845. ;; Insert new tags at the correct column
  9846. (beginning-of-line 1)
  9847. (cond
  9848. ((and (equal current "") (equal tags "")))
  9849. ((re-search-forward
  9850. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9851. (point-at-eol) t)
  9852. (if (equal tags "")
  9853. (setq rpl "")
  9854. (goto-char (match-beginning 0))
  9855. (setq c0 (current-column) p0 (point)
  9856. c1 (max (1+ c0) (if (> org-tags-column 0)
  9857. org-tags-column
  9858. (- (- org-tags-column) (length tags))))
  9859. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9860. (replace-match rpl t t)
  9861. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9862. tags)
  9863. (t (error "Tags alignment failed")))
  9864. (org-move-to-column col)
  9865. (unless just-align
  9866. (run-hooks 'org-after-tags-change-hook)))))
  9867. (defun org-change-tag-in-region (beg end tag off)
  9868. "Add or remove TAG for each entry in the region.
  9869. This works in the agenda, and also in an org-mode buffer."
  9870. (interactive
  9871. (list (region-beginning) (region-end)
  9872. (let ((org-last-tags-completion-table
  9873. (if (org-mode-p)
  9874. (org-get-buffer-tags)
  9875. (org-global-tags-completion-table))))
  9876. (org-ido-completing-read
  9877. "Tag: " 'org-tags-completion-function nil nil nil
  9878. 'org-tags-history))
  9879. (progn
  9880. (message "[s]et or [r]emove? ")
  9881. (equal (read-char-exclusive) ?r))))
  9882. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9883. (let ((agendap (equal major-mode 'org-agenda-mode))
  9884. l1 l2 m buf pos newhead (cnt 0))
  9885. (goto-char end)
  9886. (setq l2 (1- (org-current-line)))
  9887. (goto-char beg)
  9888. (setq l1 (org-current-line))
  9889. (loop for l from l1 to l2 do
  9890. (goto-line l)
  9891. (setq m (get-text-property (point) 'org-hd-marker))
  9892. (when (or (and (org-mode-p) (org-on-heading-p))
  9893. (and agendap m))
  9894. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9895. pos (if agendap m (point)))
  9896. (with-current-buffer buf
  9897. (save-excursion
  9898. (save-restriction
  9899. (goto-char pos)
  9900. (setq cnt (1+ cnt))
  9901. (org-toggle-tag tag (if off 'off 'on))
  9902. (setq newhead (org-get-heading)))))
  9903. (and agendap (org-agenda-change-all-lines newhead m))))
  9904. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9905. (defun org-tags-completion-function (string predicate &optional flag)
  9906. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9907. (confirm (lambda (x) (stringp (car x)))))
  9908. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9909. (setq s1 (match-string 1 string)
  9910. s2 (match-string 2 string))
  9911. (setq s1 "" s2 string))
  9912. (cond
  9913. ((eq flag nil)
  9914. ;; try completion
  9915. (setq rtn (try-completion s2 ctable confirm))
  9916. (if (stringp rtn)
  9917. (setq rtn
  9918. (concat s1 s2 (substring rtn (length s2))
  9919. (if (and org-add-colon-after-tag-completion
  9920. (assoc rtn ctable))
  9921. ":" ""))))
  9922. rtn)
  9923. ((eq flag t)
  9924. ;; all-completions
  9925. (all-completions s2 ctable confirm)
  9926. )
  9927. ((eq flag 'lambda)
  9928. ;; exact match?
  9929. (assoc s2 ctable)))
  9930. ))
  9931. (defun org-fast-tag-insert (kwd tags face &optional end)
  9932. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9933. (insert (format "%-12s" (concat kwd ":"))
  9934. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9935. (or end "")))
  9936. (defun org-fast-tag-show-exit (flag)
  9937. (save-excursion
  9938. (goto-line 3)
  9939. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9940. (replace-match ""))
  9941. (when flag
  9942. (end-of-line 1)
  9943. (org-move-to-column (- (window-width) 19) t)
  9944. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9945. (defun org-set-current-tags-overlay (current prefix)
  9946. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9947. (if (featurep 'xemacs)
  9948. (org-overlay-display org-tags-overlay (concat prefix s)
  9949. 'secondary-selection)
  9950. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9951. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9952. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9953. "Fast tag selection with single keys.
  9954. CURRENT is the current list of tags in the headline, INHERITED is the
  9955. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9956. possibly with grouping information. TODO-TABLE is a similar table with
  9957. TODO keywords, should these have keys assigned to them.
  9958. If the keys are nil, a-z are automatically assigned.
  9959. Returns the new tags string, or nil to not change the current settings."
  9960. (let* ((fulltable (append table todo-table))
  9961. (maxlen (apply 'max (mapcar
  9962. (lambda (x)
  9963. (if (stringp (car x)) (string-width (car x)) 0))
  9964. fulltable)))
  9965. (buf (current-buffer))
  9966. (expert (eq org-fast-tag-selection-single-key 'expert))
  9967. (buffer-tags nil)
  9968. (fwidth (+ maxlen 3 1 3))
  9969. (ncol (/ (- (window-width) 4) fwidth))
  9970. (i-face 'org-done)
  9971. (c-face 'org-todo)
  9972. tg cnt e c char c1 c2 ntable tbl rtn
  9973. ov-start ov-end ov-prefix
  9974. (exit-after-next org-fast-tag-selection-single-key)
  9975. (done-keywords org-done-keywords)
  9976. groups ingroup)
  9977. (save-excursion
  9978. (beginning-of-line 1)
  9979. (if (looking-at
  9980. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9981. (setq ov-start (match-beginning 1)
  9982. ov-end (match-end 1)
  9983. ov-prefix "")
  9984. (setq ov-start (1- (point-at-eol))
  9985. ov-end (1+ ov-start))
  9986. (skip-chars-forward "^\n\r")
  9987. (setq ov-prefix
  9988. (concat
  9989. (buffer-substring (1- (point)) (point))
  9990. (if (> (current-column) org-tags-column)
  9991. " "
  9992. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9993. (org-move-overlay org-tags-overlay ov-start ov-end)
  9994. (save-window-excursion
  9995. (if expert
  9996. (set-buffer (get-buffer-create " *Org tags*"))
  9997. (delete-other-windows)
  9998. (split-window-vertically)
  9999. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10000. (erase-buffer)
  10001. (org-set-local 'org-done-keywords done-keywords)
  10002. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10003. (org-fast-tag-insert "Current" current c-face "\n\n")
  10004. (org-fast-tag-show-exit exit-after-next)
  10005. (org-set-current-tags-overlay current ov-prefix)
  10006. (setq tbl fulltable char ?a cnt 0)
  10007. (while (setq e (pop tbl))
  10008. (cond
  10009. ((equal e '(:startgroup))
  10010. (push '() groups) (setq ingroup t)
  10011. (when (not (= cnt 0))
  10012. (setq cnt 0)
  10013. (insert "\n"))
  10014. (insert "{ "))
  10015. ((equal e '(:endgroup))
  10016. (setq ingroup nil cnt 0)
  10017. (insert "}\n"))
  10018. ((equal e '(:newline))
  10019. (when (not (= cnt 0))
  10020. (setq cnt 0)
  10021. (insert "\n")
  10022. (setq e (car tbl))
  10023. (while (equal (car tbl) '(:newline))
  10024. (insert "\n")
  10025. (setq tbl (cdr tbl)))))
  10026. (t
  10027. (setq tg (car e) c2 nil)
  10028. (if (cdr e)
  10029. (setq c (cdr e))
  10030. ;; automatically assign a character.
  10031. (setq c1 (string-to-char
  10032. (downcase (substring
  10033. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10034. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10035. (while (or (rassoc char ntable) (rassoc char table))
  10036. (setq char (1+ char)))
  10037. (setq c2 c1))
  10038. (setq c (or c2 char)))
  10039. (if ingroup (push tg (car groups)))
  10040. (setq tg (org-add-props tg nil 'face
  10041. (cond
  10042. ((not (assoc tg table))
  10043. (org-get-todo-face tg))
  10044. ((member tg current) c-face)
  10045. ((member tg inherited) i-face)
  10046. (t nil))))
  10047. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10048. (insert "[" c "] " tg (make-string
  10049. (- fwidth 4 (length tg)) ?\ ))
  10050. (push (cons tg c) ntable)
  10051. (when (= (setq cnt (1+ cnt)) ncol)
  10052. (insert "\n")
  10053. (if ingroup (insert " "))
  10054. (setq cnt 0)))))
  10055. (setq ntable (nreverse ntable))
  10056. (insert "\n")
  10057. (goto-char (point-min))
  10058. (if (not expert) (org-fit-window-to-buffer))
  10059. (setq rtn
  10060. (catch 'exit
  10061. (while t
  10062. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10063. (if groups " [!] no groups" " [!]groups")
  10064. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10065. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10066. (cond
  10067. ((= c ?\r) (throw 'exit t))
  10068. ((= c ?!)
  10069. (setq groups (not groups))
  10070. (goto-char (point-min))
  10071. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10072. ((= c ?\C-c)
  10073. (if (not expert)
  10074. (org-fast-tag-show-exit
  10075. (setq exit-after-next (not exit-after-next)))
  10076. (setq expert nil)
  10077. (delete-other-windows)
  10078. (split-window-vertically)
  10079. (org-switch-to-buffer-other-window " *Org tags*")
  10080. (org-fit-window-to-buffer)))
  10081. ((or (= c ?\C-g)
  10082. (and (= c ?q) (not (rassoc c ntable))))
  10083. (org-detach-overlay org-tags-overlay)
  10084. (setq quit-flag t))
  10085. ((= c ?\ )
  10086. (setq current nil)
  10087. (if exit-after-next (setq exit-after-next 'now)))
  10088. ((= c ?\t)
  10089. (condition-case nil
  10090. (setq tg (org-ido-completing-read
  10091. "Tag: "
  10092. (or buffer-tags
  10093. (with-current-buffer buf
  10094. (org-get-buffer-tags)))))
  10095. (quit (setq tg "")))
  10096. (when (string-match "\\S-" tg)
  10097. (add-to-list 'buffer-tags (list tg))
  10098. (if (member tg current)
  10099. (setq current (delete tg current))
  10100. (push tg current)))
  10101. (if exit-after-next (setq exit-after-next 'now)))
  10102. ((setq e (rassoc c todo-table) tg (car e))
  10103. (with-current-buffer buf
  10104. (save-excursion (org-todo tg)))
  10105. (if exit-after-next (setq exit-after-next 'now)))
  10106. ((setq e (rassoc c ntable) tg (car e))
  10107. (if (member tg current)
  10108. (setq current (delete tg current))
  10109. (loop for g in groups do
  10110. (if (member tg g)
  10111. (mapc (lambda (x)
  10112. (setq current (delete x current)))
  10113. g)))
  10114. (push tg current))
  10115. (if exit-after-next (setq exit-after-next 'now))))
  10116. ;; Create a sorted list
  10117. (setq current
  10118. (sort current
  10119. (lambda (a b)
  10120. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10121. (if (eq exit-after-next 'now) (throw 'exit t))
  10122. (goto-char (point-min))
  10123. (beginning-of-line 2)
  10124. (delete-region (point) (point-at-eol))
  10125. (org-fast-tag-insert "Current" current c-face)
  10126. (org-set-current-tags-overlay current ov-prefix)
  10127. (while (re-search-forward
  10128. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10129. (setq tg (match-string 1))
  10130. (add-text-properties
  10131. (match-beginning 1) (match-end 1)
  10132. (list 'face
  10133. (cond
  10134. ((member tg current) c-face)
  10135. ((member tg inherited) i-face)
  10136. (t (get-text-property (match-beginning 1) 'face))))))
  10137. (goto-char (point-min)))))
  10138. (org-detach-overlay org-tags-overlay)
  10139. (if rtn
  10140. (mapconcat 'identity current ":")
  10141. nil))))
  10142. (defun org-get-tags-string ()
  10143. "Get the TAGS string in the current headline."
  10144. (unless (org-on-heading-p t)
  10145. (error "Not on a heading"))
  10146. (save-excursion
  10147. (beginning-of-line 1)
  10148. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10149. (org-match-string-no-properties 1)
  10150. "")))
  10151. (defun org-get-tags ()
  10152. "Get the list of tags specified in the current headline."
  10153. (org-split-string (org-get-tags-string) ":"))
  10154. (defun org-get-buffer-tags ()
  10155. "Get a table of all tags used in the buffer, for completion."
  10156. (let (tags)
  10157. (save-excursion
  10158. (goto-char (point-min))
  10159. (while (re-search-forward
  10160. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10161. (when (equal (char-after (point-at-bol 0)) ?*)
  10162. (mapc (lambda (x) (add-to-list 'tags x))
  10163. (org-split-string (org-match-string-no-properties 1) ":")))))
  10164. (mapcar 'list tags)))
  10165. ;;;; The mapping API
  10166. ;;;###autoload
  10167. (defun org-map-entries (func &optional match scope &rest skip)
  10168. "Call FUNC at each headline selected by MATCH in SCOPE.
  10169. FUNC is a function or a lisp form. The function will be called without
  10170. arguments, with the cursor positioned at the beginning of the headline.
  10171. The return values of all calls to the function will be collected and
  10172. returned as a list.
  10173. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10174. does not need to preserve point. After evaluaton, the cursor will be
  10175. moved to the end of the line (presumably of the headline of the
  10176. processed entry) and search continues from there. Under some
  10177. circumstances, this may not produce the wanted results. For example,
  10178. if you have removed (e.g. archived) the current (sub)tree it could
  10179. mean that the next entry will be skipped entirely. In such cases, you
  10180. can specify the position from where search should continue by making
  10181. FUNC set the variable `org-map-continue-from' to the desired buffer
  10182. position.
  10183. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10184. Only headlines that are matched by this query will be considered during
  10185. the iteration. When MATCH is nil or t, all headlines will be
  10186. visited by the iteration.
  10187. SCOPE determines the scope of this command. It can be any of:
  10188. nil The current buffer, respecting the restriction if any
  10189. tree The subtree started with the entry at point
  10190. file The current buffer, without restriction
  10191. file-with-archives
  10192. The current buffer, and any archives associated with it
  10193. agenda All agenda files
  10194. agenda-with-archives
  10195. All agenda files with any archive files associated with them
  10196. \(file1 file2 ...)
  10197. If this is a list, all files in the list will be scanned
  10198. The remaining args are treated as settings for the skipping facilities of
  10199. the scanner. The following items can be given here:
  10200. archive skip trees with the archive tag.
  10201. comment skip trees with the COMMENT keyword
  10202. function or Emacs Lisp form:
  10203. will be used as value for `org-agenda-skip-function', so whenever
  10204. the the function returns t, FUNC will not be called for that
  10205. entry and search will continue from the point where the
  10206. function leaves it.
  10207. If your function needs to retrieve the tags including inherited tags
  10208. at the *current* entry, you can use the value of the variable
  10209. `org-scanner-tags' which will be much faster than getting the value
  10210. with `org-get-tags-at'. If your function gets properties with
  10211. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10212. to t around the call to `org-entry-properties' to get the same speedup.
  10213. Note that if your function moves around to retrieve tags and properties at
  10214. a *different* entry, you cannot use these techniques."
  10215. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10216. (org-agenda-skip-archived-trees (memq 'archive skip))
  10217. (org-agenda-skip-comment-trees (memq 'comment skip))
  10218. (org-agenda-skip-function
  10219. (car (org-delete-all '(comment archive) skip)))
  10220. (org-tags-match-list-sublevels t)
  10221. matcher file res
  10222. org-todo-keywords-for-agenda
  10223. org-done-keywords-for-agenda
  10224. org-todo-keyword-alist-for-agenda
  10225. org-tag-alist-for-agenda)
  10226. (cond
  10227. ((eq match t) (setq matcher t))
  10228. ((eq match nil) (setq matcher t))
  10229. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10230. (save-excursion
  10231. (save-restriction
  10232. (when (eq scope 'tree)
  10233. (org-back-to-heading t)
  10234. (org-narrow-to-subtree)
  10235. (setq scope nil))
  10236. (if (not scope)
  10237. (progn
  10238. (org-prepare-agenda-buffers
  10239. (list (buffer-file-name (current-buffer))))
  10240. (setq res (org-scan-tags func matcher)))
  10241. ;; Get the right scope
  10242. (cond
  10243. ((and scope (listp scope) (symbolp (car scope)))
  10244. (setq scope (eval scope)))
  10245. ((eq scope 'agenda)
  10246. (setq scope (org-agenda-files t)))
  10247. ((eq scope 'agenda-with-archives)
  10248. (setq scope (org-agenda-files t))
  10249. (setq scope (org-add-archive-files scope)))
  10250. ((eq scope 'file)
  10251. (setq scope (list (buffer-file-name))))
  10252. ((eq scope 'file-with-archives)
  10253. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10254. (org-prepare-agenda-buffers scope)
  10255. (while (setq file (pop scope))
  10256. (with-current-buffer (org-find-base-buffer-visiting file)
  10257. (save-excursion
  10258. (save-restriction
  10259. (widen)
  10260. (goto-char (point-min))
  10261. (setq res (append res (org-scan-tags func matcher))))))))))
  10262. res))
  10263. ;;;; Properties
  10264. ;;; Setting and retrieving properties
  10265. (defconst org-special-properties
  10266. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10267. "TIMESTAMP" "TIMESTAMP_IA")
  10268. "The special properties valid in Org-mode.
  10269. These are properties that are not defined in the property drawer,
  10270. but in some other way.")
  10271. (defconst org-default-properties
  10272. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10273. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10274. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10275. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10276. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10277. "CLOCK_MODELINE_TOTAL")
  10278. "Some properties that are used by Org-mode for various purposes.
  10279. Being in this list makes sure that they are offered for completion.")
  10280. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10281. "Regular expression matching the first line of a property drawer.")
  10282. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10283. "Regular expression matching the first line of a property drawer.")
  10284. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10285. "Regular expression matching the first line of a property drawer.")
  10286. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10287. "Regular expression matching the first line of a property drawer.")
  10288. (defconst org-property-drawer-re
  10289. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10290. org-property-end-re "\\)\n?")
  10291. "Matches an entire property drawer.")
  10292. (defconst org-clock-drawer-re
  10293. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10294. org-property-end-re "\\)\n?")
  10295. "Matches an entire clock drawer.")
  10296. (defun org-property-action ()
  10297. "Do an action on properties."
  10298. (interactive)
  10299. (let (c)
  10300. (org-at-property-p)
  10301. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10302. (setq c (read-char-exclusive))
  10303. (cond
  10304. ((equal c ?s)
  10305. (call-interactively 'org-set-property))
  10306. ((equal c ?d)
  10307. (call-interactively 'org-delete-property))
  10308. ((equal c ?D)
  10309. (call-interactively 'org-delete-property-globally))
  10310. ((equal c ?c)
  10311. (call-interactively 'org-compute-property-at-point))
  10312. (t (error "No such property action %c" c)))))
  10313. (defun org-at-property-p ()
  10314. "Is the cursor in a property line?"
  10315. ;; FIXME: Does not check if we are actually in the drawer.
  10316. ;; FIXME: also returns true on any drawers.....
  10317. ;; This is used by C-c C-c for property action.
  10318. (save-excursion
  10319. (beginning-of-line 1)
  10320. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10321. (defun org-get-property-block (&optional beg end force)
  10322. "Return the (beg . end) range of the body of the property drawer.
  10323. BEG and END can be beginning and end of subtree, if not given
  10324. they will be found.
  10325. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10326. (catch 'exit
  10327. (save-excursion
  10328. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10329. (end (or end (progn (outline-next-heading) (point)))))
  10330. (goto-char beg)
  10331. (if (re-search-forward org-property-start-re end t)
  10332. (setq beg (1+ (match-end 0)))
  10333. (if force
  10334. (save-excursion
  10335. (org-insert-property-drawer)
  10336. (setq end (progn (outline-next-heading) (point))))
  10337. (throw 'exit nil))
  10338. (goto-char beg)
  10339. (if (re-search-forward org-property-start-re end t)
  10340. (setq beg (1+ (match-end 0)))))
  10341. (if (re-search-forward org-property-end-re end t)
  10342. (setq end (match-beginning 0))
  10343. (or force (throw 'exit nil))
  10344. (goto-char beg)
  10345. (setq end beg)
  10346. (org-indent-line-function)
  10347. (insert ":END:\n"))
  10348. (cons beg end)))))
  10349. (defun org-entry-properties (&optional pom which)
  10350. "Get all properties of the entry at point-or-marker POM.
  10351. This includes the TODO keyword, the tags, time strings for deadline,
  10352. scheduled, and clocking, and any additional properties defined in the
  10353. entry. The return value is an alist, keys may occur multiple times
  10354. if the property key was used several times.
  10355. POM may also be nil, in which case the current entry is used.
  10356. If WHICH is nil or `all', get all properties. If WHICH is
  10357. `special' or `standard', only get that subclass."
  10358. (setq which (or which 'all))
  10359. (org-with-point-at pom
  10360. (let ((clockstr (substring org-clock-string 0 -1))
  10361. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10362. beg end range props sum-props key value string clocksum)
  10363. (save-excursion
  10364. (when (condition-case nil
  10365. (and (org-mode-p) (org-back-to-heading t))
  10366. (error nil))
  10367. (setq beg (point))
  10368. (setq sum-props (get-text-property (point) 'org-summaries))
  10369. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10370. (outline-next-heading)
  10371. (setq end (point))
  10372. (when (memq which '(all special))
  10373. ;; Get the special properties, like TODO and tags
  10374. (goto-char beg)
  10375. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10376. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10377. (when (looking-at org-priority-regexp)
  10378. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10379. (when (and (setq value (org-get-tags-string))
  10380. (string-match "\\S-" value))
  10381. (push (cons "TAGS" value) props))
  10382. (when (setq value (org-get-tags-at))
  10383. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10384. props))
  10385. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10386. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10387. string (if (equal key clockstr)
  10388. (org-no-properties
  10389. (org-trim
  10390. (buffer-substring
  10391. (match-beginning 3) (goto-char (point-at-eol)))))
  10392. (substring (org-match-string-no-properties 3) 1 -1)))
  10393. (unless key
  10394. (if (= (char-after (match-beginning 3)) ?\[)
  10395. (setq key "TIMESTAMP_IA")
  10396. (setq key "TIMESTAMP")))
  10397. (when (or (equal key clockstr) (not (assoc key props)))
  10398. (push (cons key string) props)))
  10399. )
  10400. (when (memq which '(all standard))
  10401. ;; Get the standard properties, like :PROP: ...
  10402. (setq range (org-get-property-block beg end))
  10403. (when range
  10404. (goto-char (car range))
  10405. (while (re-search-forward
  10406. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10407. (cdr range) t)
  10408. (setq key (org-match-string-no-properties 1)
  10409. value (org-trim (or (org-match-string-no-properties 2) "")))
  10410. (unless (member key excluded)
  10411. (push (cons key (or value "")) props)))))
  10412. (if clocksum
  10413. (push (cons "CLOCKSUM"
  10414. (org-columns-number-to-string (/ (float clocksum) 60.)
  10415. 'add_times))
  10416. props))
  10417. (unless (assoc "CATEGORY" props)
  10418. (setq value (or (org-get-category)
  10419. (progn (org-refresh-category-properties)
  10420. (org-get-category))))
  10421. (push (cons "CATEGORY" value) props))
  10422. (append sum-props (nreverse props)))))))
  10423. (defun org-entry-get (pom property &optional inherit)
  10424. "Get value of PROPERTY for entry at point-or-marker POM.
  10425. If INHERIT is non-nil and the entry does not have the property,
  10426. then also check higher levels of the hierarchy.
  10427. If INHERIT is the symbol `selective', use inheritance only if the setting
  10428. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10429. If the property is present but empty, the return value is the empty string.
  10430. If the property is not present at all, nil is returned."
  10431. (org-with-point-at pom
  10432. (if (and inherit (if (eq inherit 'selective)
  10433. (org-property-inherit-p property)
  10434. t))
  10435. (org-entry-get-with-inheritance property)
  10436. (if (member property org-special-properties)
  10437. ;; We need a special property. Use brute force, get all properties.
  10438. (cdr (assoc property (org-entry-properties nil 'special)))
  10439. (let ((range (org-get-property-block)))
  10440. (if (and range
  10441. (goto-char (car range))
  10442. (re-search-forward
  10443. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10444. (cdr range) t))
  10445. ;; Found the property, return it.
  10446. (if (match-end 1)
  10447. (org-match-string-no-properties 1)
  10448. "")))))))
  10449. (defun org-property-or-variable-value (var &optional inherit)
  10450. "Check if there is a property fixing the value of VAR.
  10451. If yes, return this value. If not, return the current value of the variable."
  10452. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10453. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10454. (read prop)
  10455. (symbol-value var))))
  10456. (defun org-entry-delete (pom property)
  10457. "Delete the property PROPERTY from entry at point-or-marker POM."
  10458. (org-with-point-at pom
  10459. (if (member property org-special-properties)
  10460. nil ; cannot delete these properties.
  10461. (let ((range (org-get-property-block)))
  10462. (if (and range
  10463. (goto-char (car range))
  10464. (re-search-forward
  10465. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10466. (cdr range) t))
  10467. (progn
  10468. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10469. t)
  10470. nil)))))
  10471. ;; Multi-values properties are properties that contain multiple values
  10472. ;; These values are assumed to be single words, separated by whitespace.
  10473. (defun org-entry-add-to-multivalued-property (pom property value)
  10474. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10475. (let* ((old (org-entry-get pom property))
  10476. (values (and old (org-split-string old "[ \t]"))))
  10477. (setq value (org-entry-protect-space value))
  10478. (unless (member value values)
  10479. (setq values (cons value values))
  10480. (org-entry-put pom property
  10481. (mapconcat 'identity values " ")))))
  10482. (defun org-entry-remove-from-multivalued-property (pom property value)
  10483. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10484. (let* ((old (org-entry-get pom property))
  10485. (values (and old (org-split-string old "[ \t]"))))
  10486. (setq value (org-entry-protect-space value))
  10487. (when (member value values)
  10488. (setq values (delete value values))
  10489. (org-entry-put pom property
  10490. (mapconcat 'identity values " ")))))
  10491. (defun org-entry-member-in-multivalued-property (pom property value)
  10492. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10493. (let* ((old (org-entry-get pom property))
  10494. (values (and old (org-split-string old "[ \t]"))))
  10495. (setq value (org-entry-protect-space value))
  10496. (member value values)))
  10497. (defun org-entry-get-multivalued-property (pom property)
  10498. "Return a list of values in a multivalued property."
  10499. (let* ((value (org-entry-get pom property))
  10500. (values (and value (org-split-string value "[ \t]"))))
  10501. (mapcar 'org-entry-restore-space values)))
  10502. (defun org-entry-put-multivalued-property (pom property &rest values)
  10503. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10504. VALUES should be a list of strings. Spaces will be protected."
  10505. (org-entry-put pom property
  10506. (mapconcat 'org-entry-protect-space values " "))
  10507. (let* ((value (org-entry-get pom property))
  10508. (values (and value (org-split-string value "[ \t]"))))
  10509. (mapcar 'org-entry-restore-space values)))
  10510. (defun org-entry-protect-space (s)
  10511. "Protect spaces and newline in string S."
  10512. (while (string-match " " s)
  10513. (setq s (replace-match "%20" t t s)))
  10514. (while (string-match "\n" s)
  10515. (setq s (replace-match "%0A" t t s)))
  10516. s)
  10517. (defun org-entry-restore-space (s)
  10518. "Restore spaces and newline in string S."
  10519. (while (string-match "%20" s)
  10520. (setq s (replace-match " " t t s)))
  10521. (while (string-match "%0A" s)
  10522. (setq s (replace-match "\n" t t s)))
  10523. s)
  10524. (defvar org-entry-property-inherited-from (make-marker)
  10525. "Marker pointing to the entry from where a property was inherited.
  10526. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10527. location of the entry where the inheritance search matched. If there was
  10528. no match, the marker will point nowhere.
  10529. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10530. is set.")
  10531. (defun org-entry-get-with-inheritance (property)
  10532. "Get entry property, and search higher levels if not present."
  10533. (move-marker org-entry-property-inherited-from nil)
  10534. (let (tmp)
  10535. (save-excursion
  10536. (save-restriction
  10537. (widen)
  10538. (catch 'ex
  10539. (while t
  10540. (when (setq tmp (org-entry-get nil property))
  10541. (org-back-to-heading t)
  10542. (move-marker org-entry-property-inherited-from (point))
  10543. (throw 'ex tmp))
  10544. (or (org-up-heading-safe) (throw 'ex nil)))))
  10545. (or tmp
  10546. (cdr (assoc property org-file-properties))
  10547. (cdr (assoc property org-global-properties))
  10548. (cdr (assoc property org-global-properties-fixed))))))
  10549. (defun org-entry-put (pom property value)
  10550. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10551. (org-with-point-at pom
  10552. (org-back-to-heading t)
  10553. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10554. range)
  10555. (cond
  10556. ((equal property "TODO")
  10557. (when (and (stringp value) (string-match "\\S-" value)
  10558. (not (member value org-todo-keywords-1)))
  10559. (error "\"%s\" is not a valid TODO state" value))
  10560. (if (or (not value)
  10561. (not (string-match "\\S-" value)))
  10562. (setq value 'none))
  10563. (org-todo value)
  10564. (org-set-tags nil 'align))
  10565. ((equal property "PRIORITY")
  10566. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10567. (string-to-char value) ?\ ))
  10568. (org-set-tags nil 'align))
  10569. ((equal property "SCHEDULED")
  10570. (if (re-search-forward org-scheduled-time-regexp end t)
  10571. (cond
  10572. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10573. ((eq value 'later) (org-timestamp-change 1 'day))
  10574. (t (call-interactively 'org-schedule)))
  10575. (call-interactively 'org-schedule)))
  10576. ((equal property "DEADLINE")
  10577. (if (re-search-forward org-deadline-time-regexp end t)
  10578. (cond
  10579. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10580. ((eq value 'later) (org-timestamp-change 1 'day))
  10581. (t (call-interactively 'org-deadline)))
  10582. (call-interactively 'org-deadline)))
  10583. ((member property org-special-properties)
  10584. (error "The %s property can not yet be set with `org-entry-put'"
  10585. property))
  10586. (t ; a non-special property
  10587. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10588. (setq range (org-get-property-block beg end 'force))
  10589. (goto-char (car range))
  10590. (if (re-search-forward
  10591. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10592. (progn
  10593. (delete-region (match-beginning 1) (match-end 1))
  10594. (goto-char (match-beginning 1)))
  10595. (goto-char (cdr range))
  10596. (insert "\n")
  10597. (backward-char 1)
  10598. (org-indent-line-function)
  10599. (insert ":" property ":"))
  10600. (and value (insert " " value))
  10601. (org-indent-line-function)))))))
  10602. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10603. "Get all property keys in the current buffer.
  10604. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10605. like tags and TODO state.
  10606. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10607. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10608. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10609. formats in the current buffer."
  10610. (let (rtn range cfmt s p)
  10611. (save-excursion
  10612. (save-restriction
  10613. (widen)
  10614. (goto-char (point-min))
  10615. (while (re-search-forward org-property-start-re nil t)
  10616. (setq range (org-get-property-block))
  10617. (goto-char (car range))
  10618. (while (re-search-forward
  10619. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10620. (cdr range) t)
  10621. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10622. (outline-next-heading))))
  10623. (when include-specials
  10624. (setq rtn (append org-special-properties rtn)))
  10625. (when include-defaults
  10626. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10627. (add-to-list 'rtn org-effort-property))
  10628. (when include-columns
  10629. (save-excursion
  10630. (save-restriction
  10631. (widen)
  10632. (goto-char (point-min))
  10633. (while (re-search-forward
  10634. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10635. nil t)
  10636. (setq cfmt (match-string 2) s 0)
  10637. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10638. cfmt s)
  10639. (setq s (match-end 0)
  10640. p (match-string 1 cfmt))
  10641. (unless (or (equal p "ITEM")
  10642. (member p org-special-properties))
  10643. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10644. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10645. (defun org-property-values (key)
  10646. "Return a list of all values of property KEY."
  10647. (save-excursion
  10648. (save-restriction
  10649. (widen)
  10650. (goto-char (point-min))
  10651. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10652. values)
  10653. (while (re-search-forward re nil t)
  10654. (add-to-list 'values (org-trim (match-string 1))))
  10655. (delete "" values)))))
  10656. (defun org-insert-property-drawer ()
  10657. "Insert a property drawer into the current entry."
  10658. (interactive)
  10659. (org-back-to-heading t)
  10660. (looking-at outline-regexp)
  10661. (let ((indent (if org-adapt-indentation
  10662. (- (match-end 0)(match-beginning 0))
  10663. 0))
  10664. (beg (point))
  10665. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10666. end hiddenp)
  10667. (outline-next-heading)
  10668. (setq end (point))
  10669. (goto-char beg)
  10670. (while (re-search-forward re end t))
  10671. (setq hiddenp (org-invisible-p))
  10672. (end-of-line 1)
  10673. (and (equal (char-after) ?\n) (forward-char 1))
  10674. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10675. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10676. ;; just skip this line
  10677. (beginning-of-line 2)
  10678. ;; Drawer start, find the end
  10679. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10680. (beginning-of-line 1)))
  10681. (org-skip-over-state-notes)
  10682. (skip-chars-backward " \t\n\r")
  10683. (if (eq (char-before) ?*) (forward-char 1))
  10684. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10685. (beginning-of-line 0)
  10686. (org-indent-to-column indent)
  10687. (beginning-of-line 2)
  10688. (org-indent-to-column indent)
  10689. (beginning-of-line 0)
  10690. (if hiddenp
  10691. (save-excursion
  10692. (org-back-to-heading t)
  10693. (hide-entry))
  10694. (org-flag-drawer t))))
  10695. (defun org-set-property (property value)
  10696. "In the current entry, set PROPERTY to VALUE.
  10697. When called interactively, this will prompt for a property name, offering
  10698. completion on existing and default properties. And then it will prompt
  10699. for a value, offering completion either on allowed values (via an inherited
  10700. xxx_ALL property) or on existing values in other instances of this property
  10701. in the current file."
  10702. (interactive
  10703. (let* ((completion-ignore-case t)
  10704. (keys (org-buffer-property-keys nil t t))
  10705. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10706. (prop (if (member prop0 keys)
  10707. prop0
  10708. (or (cdr (assoc (downcase prop0)
  10709. (mapcar (lambda (x) (cons (downcase x) x))
  10710. keys)))
  10711. prop0)))
  10712. (cur (org-entry-get nil prop))
  10713. (allowed (org-property-get-allowed-values nil prop 'table))
  10714. (existing (mapcar 'list (org-property-values prop)))
  10715. (val (if allowed
  10716. (org-completing-read "Value: " allowed nil 'req-match)
  10717. (let (org-completion-use-ido)
  10718. (org-completing-read
  10719. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10720. (concat "[" cur "]") "")
  10721. ": ")
  10722. existing nil nil "" nil cur)))))
  10723. (list prop (if (equal val "") cur val))))
  10724. (unless (equal (org-entry-get nil property) value)
  10725. (org-entry-put nil property value)))
  10726. (defun org-delete-property (property)
  10727. "In the current entry, delete PROPERTY."
  10728. (interactive
  10729. (let* ((completion-ignore-case t)
  10730. (prop (org-ido-completing-read
  10731. "Property: " (org-entry-properties nil 'standard))))
  10732. (list prop)))
  10733. (message "Property %s %s" property
  10734. (if (org-entry-delete nil property)
  10735. "deleted"
  10736. "was not present in the entry")))
  10737. (defun org-delete-property-globally (property)
  10738. "Remove PROPERTY globally, from all entries."
  10739. (interactive
  10740. (let* ((completion-ignore-case t)
  10741. (prop (org-ido-completing-read
  10742. "Globally remove property: "
  10743. (mapcar 'list (org-buffer-property-keys)))))
  10744. (list prop)))
  10745. (save-excursion
  10746. (save-restriction
  10747. (widen)
  10748. (goto-char (point-min))
  10749. (let ((cnt 0))
  10750. (while (re-search-forward
  10751. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10752. nil t)
  10753. (setq cnt (1+ cnt))
  10754. (replace-match ""))
  10755. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10756. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10757. (defun org-compute-property-at-point ()
  10758. "Compute the property at point.
  10759. This looks for an enclosing column format, extracts the operator and
  10760. then applies it to the property in the column format's scope."
  10761. (interactive)
  10762. (unless (org-at-property-p)
  10763. (error "Not at a property"))
  10764. (let ((prop (org-match-string-no-properties 2)))
  10765. (org-columns-get-format-and-top-level)
  10766. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10767. (error "No operator defined for property %s" prop))
  10768. (org-columns-compute prop)))
  10769. (defun org-property-get-allowed-values (pom property &optional table)
  10770. "Get allowed values for the property PROPERTY.
  10771. When TABLE is non-nil, return an alist that can directly be used for
  10772. completion."
  10773. (let (vals)
  10774. (cond
  10775. ((equal property "TODO")
  10776. (setq vals (org-with-point-at pom
  10777. (append org-todo-keywords-1 '("")))))
  10778. ((equal property "PRIORITY")
  10779. (let ((n org-lowest-priority))
  10780. (while (>= n org-highest-priority)
  10781. (push (char-to-string n) vals)
  10782. (setq n (1- n)))))
  10783. ((member property org-special-properties))
  10784. (t
  10785. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10786. (when (and vals (string-match "\\S-" vals))
  10787. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10788. (setq vals (mapcar (lambda (x)
  10789. (cond ((stringp x) x)
  10790. ((numberp x) (number-to-string x))
  10791. ((symbolp x) (symbol-name x))
  10792. (t "???")))
  10793. vals)))))
  10794. (if table (mapcar 'list vals) vals)))
  10795. (defun org-property-previous-allowed-value (&optional previous)
  10796. "Switch to the next allowed value for this property."
  10797. (interactive)
  10798. (org-property-next-allowed-value t))
  10799. (defun org-property-next-allowed-value (&optional previous)
  10800. "Switch to the next allowed value for this property."
  10801. (interactive)
  10802. (unless (org-at-property-p)
  10803. (error "Not at a property"))
  10804. (let* ((key (match-string 2))
  10805. (value (match-string 3))
  10806. (allowed (or (org-property-get-allowed-values (point) key)
  10807. (and (member value '("[ ]" "[-]" "[X]"))
  10808. '("[ ]" "[X]"))))
  10809. nval)
  10810. (unless allowed
  10811. (error "Allowed values for this property have not been defined"))
  10812. (if previous (setq allowed (reverse allowed)))
  10813. (if (member value allowed)
  10814. (setq nval (car (cdr (member value allowed)))))
  10815. (setq nval (or nval (car allowed)))
  10816. (if (equal nval value)
  10817. (error "Only one allowed value for this property"))
  10818. (org-at-property-p)
  10819. (replace-match (concat " :" key ": " nval) t t)
  10820. (org-indent-line-function)
  10821. (beginning-of-line 1)
  10822. (skip-chars-forward " \t")))
  10823. (defun org-find-entry-with-id (ident)
  10824. "Locate the entry that contains the ID property with exact value IDENT.
  10825. IDENT can be a string, a symbol or a number, this function will search for
  10826. the string representation of it.
  10827. Return the position where this entry starts, or nil if there is no such entry."
  10828. (interactive "sID: ")
  10829. (let ((id (cond
  10830. ((stringp ident) ident)
  10831. ((symbol-name ident) (symbol-name ident))
  10832. ((numberp ident) (number-to-string ident))
  10833. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10834. (case-fold-search nil))
  10835. (save-excursion
  10836. (save-restriction
  10837. (widen)
  10838. (goto-char (point-min))
  10839. (when (re-search-forward
  10840. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10841. nil t)
  10842. (org-back-to-heading)
  10843. (point))))))
  10844. ;;;; Timestamps
  10845. (defvar org-last-changed-timestamp nil)
  10846. (defvar org-last-inserted-timestamp nil
  10847. "The last time stamp inserted with `org-insert-time-stamp'.")
  10848. (defvar org-time-was-given) ; dynamically scoped parameter
  10849. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10850. (defvar org-ts-what) ; dynamically scoped parameter
  10851. (defun org-time-stamp (arg &optional inactive)
  10852. "Prompt for a date/time and insert a time stamp.
  10853. If the user specifies a time like HH:MM, or if this command is called
  10854. with a prefix argument, the time stamp will contain date and time.
  10855. Otherwise, only the date will be included. All parts of a date not
  10856. specified by the user will be filled in from the current date/time.
  10857. So if you press just return without typing anything, the time stamp
  10858. will represent the current date/time. If there is already a timestamp
  10859. at the cursor, it will be modified."
  10860. (interactive "P")
  10861. (let* ((ts nil)
  10862. (default-time
  10863. ;; Default time is either today, or, when entering a range,
  10864. ;; the range start.
  10865. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10866. (save-excursion
  10867. (re-search-backward
  10868. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10869. (- (point) 20) t)))
  10870. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10871. (current-time)))
  10872. (default-input (and ts (org-get-compact-tod ts)))
  10873. org-time-was-given org-end-time-was-given time)
  10874. (cond
  10875. ((and (org-at-timestamp-p t)
  10876. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10877. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10878. (insert "--")
  10879. (setq time (let ((this-command this-command))
  10880. (org-read-date arg 'totime nil nil
  10881. default-time default-input)))
  10882. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10883. ((org-at-timestamp-p t)
  10884. (setq time (let ((this-command this-command))
  10885. (org-read-date arg 'totime nil nil default-time default-input)))
  10886. (when (org-at-timestamp-p t) ; just to get the match data
  10887. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10888. (replace-match "")
  10889. (setq org-last-changed-timestamp
  10890. (org-insert-time-stamp
  10891. time (or org-time-was-given arg)
  10892. inactive nil nil (list org-end-time-was-given))))
  10893. (message "Timestamp updated"))
  10894. (t
  10895. (setq time (let ((this-command this-command))
  10896. (org-read-date arg 'totime nil nil default-time default-input)))
  10897. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10898. nil nil (list org-end-time-was-given))))))
  10899. ;; FIXME: can we use this for something else, like computing time differences?
  10900. (defun org-get-compact-tod (s)
  10901. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10902. (let* ((t1 (match-string 1 s))
  10903. (h1 (string-to-number (match-string 2 s)))
  10904. (m1 (string-to-number (match-string 3 s)))
  10905. (t2 (and (match-end 4) (match-string 5 s)))
  10906. (h2 (and t2 (string-to-number (match-string 6 s))))
  10907. (m2 (and t2 (string-to-number (match-string 7 s))))
  10908. dh dm)
  10909. (if (not t2)
  10910. t1
  10911. (setq dh (- h2 h1) dm (- m2 m1))
  10912. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10913. (concat t1 "+" (number-to-string dh)
  10914. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10915. (defun org-time-stamp-inactive (&optional arg)
  10916. "Insert an inactive time stamp.
  10917. An inactive time stamp is enclosed in square brackets instead of angle
  10918. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10919. does not link to the calendar and cannot be changed with the S-cursor keys.
  10920. So these are more for recording a certain time/date."
  10921. (interactive "P")
  10922. (org-time-stamp arg 'inactive))
  10923. (defvar org-date-ovl (org-make-overlay 1 1))
  10924. (org-overlay-put org-date-ovl 'face 'org-warning)
  10925. (org-detach-overlay org-date-ovl)
  10926. (defvar org-ans1) ; dynamically scoped parameter
  10927. (defvar org-ans2) ; dynamically scoped parameter
  10928. (defvar org-plain-time-of-day-regexp) ; defined below
  10929. (defvar org-overriding-default-time nil) ; dynamically scoped
  10930. (defvar org-read-date-overlay nil)
  10931. (defvar org-dcst nil) ; dynamically scoped
  10932. (defvar org-read-date-history nil)
  10933. (defun org-read-date (&optional with-time to-time from-string prompt
  10934. default-time default-input)
  10935. "Read a date, possibly a time, and make things smooth for the user.
  10936. The prompt will suggest to enter an ISO date, but you can also enter anything
  10937. which will at least partially be understood by `parse-time-string'.
  10938. Unrecognized parts of the date will default to the current day, month, year,
  10939. hour and minute. If this command is called to replace a timestamp at point,
  10940. of to enter the second timestamp of a range, the default time is taken from the
  10941. existing stamp. For example,
  10942. 3-2-5 --> 2003-02-05
  10943. feb 15 --> currentyear-02-15
  10944. sep 12 9 --> 2009-09-12
  10945. 12:45 --> today 12:45
  10946. 22 sept 0:34 --> currentyear-09-22 0:34
  10947. 12 --> currentyear-currentmonth-12
  10948. Fri --> nearest Friday (today or later)
  10949. etc.
  10950. Furthermore you can specify a relative date by giving, as the *first* thing
  10951. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10952. change in days weeks, months, years.
  10953. With a single plus or minus, the date is relative to today. With a double
  10954. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10955. +4d --> four days from today
  10956. +4 --> same as above
  10957. +2w --> two weeks from today
  10958. ++5 --> five days from default date
  10959. The function understands only English month and weekday abbreviations,
  10960. but this can be configured with the variables `parse-time-months' and
  10961. `parse-time-weekdays'.
  10962. While prompting, a calendar is popped up - you can also select the
  10963. date with the mouse (button 1). The calendar shows a period of three
  10964. months. To scroll it to other months, use the keys `>' and `<'.
  10965. If you don't like the calendar, turn it off with
  10966. \(setq org-read-date-popup-calendar nil)
  10967. With optional argument TO-TIME, the date will immediately be converted
  10968. to an internal time.
  10969. With an optional argument WITH-TIME, the prompt will suggest to also
  10970. insert a time. Note that when WITH-TIME is not set, you can still
  10971. enter a time, and this function will inform the calling routine about
  10972. this change. The calling routine may then choose to change the format
  10973. used to insert the time stamp into the buffer to include the time.
  10974. With optional argument FROM-STRING, read from this string instead from
  10975. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10976. the time/date that is used for everything that is not specified by the
  10977. user."
  10978. (require 'parse-time)
  10979. (let* ((org-time-stamp-rounding-minutes
  10980. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10981. (org-dcst org-display-custom-times)
  10982. (ct (org-current-time))
  10983. (def (or org-overriding-default-time default-time ct))
  10984. (defdecode (decode-time def))
  10985. (dummy (progn
  10986. (when (< (nth 2 defdecode) org-extend-today-until)
  10987. (setcar (nthcdr 2 defdecode) -1)
  10988. (setcar (nthcdr 1 defdecode) 59)
  10989. (setq def (apply 'encode-time defdecode)
  10990. defdecode (decode-time def)))))
  10991. (calendar-move-hook nil)
  10992. (calendar-view-diary-initially-flag nil)
  10993. (view-diary-entries-initially nil)
  10994. (calendar-view-holidays-initially-flag nil)
  10995. (view-calendar-holidays-initially nil)
  10996. (timestr (format-time-string
  10997. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10998. (prompt (concat (if prompt (concat prompt " ") "")
  10999. (format "Date+time [%s]: " timestr)))
  11000. ans (org-ans0 "") org-ans1 org-ans2 final)
  11001. (cond
  11002. (from-string (setq ans from-string))
  11003. (org-read-date-popup-calendar
  11004. (save-excursion
  11005. (save-window-excursion
  11006. (calendar)
  11007. (calendar-forward-day (- (time-to-days def)
  11008. (calendar-absolute-from-gregorian
  11009. (calendar-current-date))))
  11010. (org-eval-in-calendar nil t)
  11011. (let* ((old-map (current-local-map))
  11012. (map (copy-keymap calendar-mode-map))
  11013. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11014. (org-defkey map (kbd "RET") 'org-calendar-select)
  11015. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11016. 'org-calendar-select-mouse)
  11017. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11018. 'org-calendar-select-mouse)
  11019. (org-defkey minibuffer-local-map [(meta shift left)]
  11020. (lambda () (interactive)
  11021. (org-eval-in-calendar '(calendar-backward-month 1))))
  11022. (org-defkey minibuffer-local-map [(meta shift right)]
  11023. (lambda () (interactive)
  11024. (org-eval-in-calendar '(calendar-forward-month 1))))
  11025. (org-defkey minibuffer-local-map [(meta shift up)]
  11026. (lambda () (interactive)
  11027. (org-eval-in-calendar '(calendar-backward-year 1))))
  11028. (org-defkey minibuffer-local-map [(meta shift down)]
  11029. (lambda () (interactive)
  11030. (org-eval-in-calendar '(calendar-forward-year 1))))
  11031. (org-defkey minibuffer-local-map [?\e (shift left)]
  11032. (lambda () (interactive)
  11033. (org-eval-in-calendar '(calendar-backward-month 1))))
  11034. (org-defkey minibuffer-local-map [?\e (shift right)]
  11035. (lambda () (interactive)
  11036. (org-eval-in-calendar '(calendar-forward-month 1))))
  11037. (org-defkey minibuffer-local-map [?\e (shift up)]
  11038. (lambda () (interactive)
  11039. (org-eval-in-calendar '(calendar-backward-year 1))))
  11040. (org-defkey minibuffer-local-map [?\e (shift down)]
  11041. (lambda () (interactive)
  11042. (org-eval-in-calendar '(calendar-forward-year 1))))
  11043. (org-defkey minibuffer-local-map [(shift up)]
  11044. (lambda () (interactive)
  11045. (org-eval-in-calendar '(calendar-backward-week 1))))
  11046. (org-defkey minibuffer-local-map [(shift down)]
  11047. (lambda () (interactive)
  11048. (org-eval-in-calendar '(calendar-forward-week 1))))
  11049. (org-defkey minibuffer-local-map [(shift left)]
  11050. (lambda () (interactive)
  11051. (org-eval-in-calendar '(calendar-backward-day 1))))
  11052. (org-defkey minibuffer-local-map [(shift right)]
  11053. (lambda () (interactive)
  11054. (org-eval-in-calendar '(calendar-forward-day 1))))
  11055. (org-defkey minibuffer-local-map ">"
  11056. (lambda () (interactive)
  11057. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11058. (org-defkey minibuffer-local-map "<"
  11059. (lambda () (interactive)
  11060. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11061. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11062. (unwind-protect
  11063. (progn
  11064. (use-local-map map)
  11065. (add-hook 'post-command-hook 'org-read-date-display)
  11066. (setq org-ans0 (read-string prompt default-input
  11067. 'org-read-date-history nil))
  11068. ;; org-ans0: from prompt
  11069. ;; org-ans1: from mouse click
  11070. ;; org-ans2: from calendar motion
  11071. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11072. (remove-hook 'post-command-hook 'org-read-date-display)
  11073. (use-local-map old-map)
  11074. (when org-read-date-overlay
  11075. (org-delete-overlay org-read-date-overlay)
  11076. (setq org-read-date-overlay nil)))))))
  11077. (t ; Naked prompt only
  11078. (unwind-protect
  11079. (setq ans (read-string prompt default-input
  11080. 'org-read-date-history timestr))
  11081. (when org-read-date-overlay
  11082. (org-delete-overlay org-read-date-overlay)
  11083. (setq org-read-date-overlay nil)))))
  11084. (setq final (org-read-date-analyze ans def defdecode))
  11085. (if to-time
  11086. (apply 'encode-time final)
  11087. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11088. (format "%04d-%02d-%02d %02d:%02d"
  11089. (nth 5 final) (nth 4 final) (nth 3 final)
  11090. (nth 2 final) (nth 1 final))
  11091. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11092. (defvar def)
  11093. (defvar defdecode)
  11094. (defvar with-time)
  11095. (defun org-read-date-display ()
  11096. "Display the current date prompt interpretation in the minibuffer."
  11097. (when org-read-date-display-live
  11098. (when org-read-date-overlay
  11099. (org-delete-overlay org-read-date-overlay))
  11100. (let ((p (point)))
  11101. (end-of-line 1)
  11102. (while (not (equal (buffer-substring
  11103. (max (point-min) (- (point) 4)) (point))
  11104. " "))
  11105. (insert " "))
  11106. (goto-char p))
  11107. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11108. " " (or org-ans1 org-ans2)))
  11109. (org-end-time-was-given nil)
  11110. (f (org-read-date-analyze ans def defdecode))
  11111. (fmts (if org-dcst
  11112. org-time-stamp-custom-formats
  11113. org-time-stamp-formats))
  11114. (fmt (if (or with-time
  11115. (and (boundp 'org-time-was-given) org-time-was-given))
  11116. (cdr fmts)
  11117. (car fmts)))
  11118. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11119. (when (and org-end-time-was-given
  11120. (string-match org-plain-time-of-day-regexp txt))
  11121. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11122. org-end-time-was-given
  11123. (substring txt (match-end 0)))))
  11124. (setq org-read-date-overlay
  11125. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11126. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11127. (defun org-read-date-analyze (ans def defdecode)
  11128. "Analyse the combined answer of the date prompt."
  11129. ;; FIXME: cleanup and comment
  11130. (let (delta deltan deltaw deltadef year month day
  11131. hour minute second wday pm h2 m2 tl wday1
  11132. iso-year iso-weekday iso-week iso-year iso-date)
  11133. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11134. (setq ans "+0"))
  11135. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11136. (setq ans (replace-match "" t t ans)
  11137. deltan (car delta)
  11138. deltaw (nth 1 delta)
  11139. deltadef (nth 2 delta)))
  11140. ;; Check if there is an iso week date in there
  11141. ;; If yes, sore the info and postpone interpreting it until the rest
  11142. ;; of the parsing is done
  11143. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11144. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11145. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11146. iso-week (string-to-number (match-string 2 ans)))
  11147. (setq ans (replace-match "" t t ans)))
  11148. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11149. (when (string-match
  11150. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11151. (setq year (if (match-end 2)
  11152. (string-to-number (match-string 2 ans))
  11153. (string-to-number (format-time-string "%Y")))
  11154. month (string-to-number (match-string 3 ans))
  11155. day (string-to-number (match-string 4 ans)))
  11156. (if (< year 100) (setq year (+ 2000 year)))
  11157. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11158. t nil ans)))
  11159. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11160. ;; If there is a time with am/pm, and *no* time without it, we convert
  11161. ;; so that matching will be successful.
  11162. (loop for i from 1 to 2 do ; twice, for end time as well
  11163. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11164. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11165. (setq hour (string-to-number (match-string 1 ans))
  11166. minute (if (match-end 3)
  11167. (string-to-number (match-string 3 ans))
  11168. 0)
  11169. pm (equal ?p
  11170. (string-to-char (downcase (match-string 4 ans)))))
  11171. (if (and (= hour 12) (not pm))
  11172. (setq hour 0)
  11173. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11174. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11175. t t ans))))
  11176. ;; Check if a time range is given as a duration
  11177. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11178. (setq hour (string-to-number (match-string 1 ans))
  11179. h2 (+ hour (string-to-number (match-string 3 ans)))
  11180. minute (string-to-number (match-string 2 ans))
  11181. m2 (+ minute (if (match-end 5) (string-to-number
  11182. (match-string 5 ans))0)))
  11183. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11184. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11185. t t ans)))
  11186. ;; Check if there is a time range
  11187. (when (boundp 'org-end-time-was-given)
  11188. (setq org-time-was-given nil)
  11189. (when (and (string-match org-plain-time-of-day-regexp ans)
  11190. (match-end 8))
  11191. (setq org-end-time-was-given (match-string 8 ans))
  11192. (setq ans (concat (substring ans 0 (match-beginning 7))
  11193. (substring ans (match-end 7))))))
  11194. (setq tl (parse-time-string ans)
  11195. day (or (nth 3 tl) (nth 3 defdecode))
  11196. month (or (nth 4 tl)
  11197. (if (and org-read-date-prefer-future
  11198. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11199. (1+ (nth 4 defdecode))
  11200. (nth 4 defdecode)))
  11201. year (or (nth 5 tl)
  11202. (if (and org-read-date-prefer-future
  11203. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11204. (1+ (nth 5 defdecode))
  11205. (nth 5 defdecode)))
  11206. hour (or (nth 2 tl) (nth 2 defdecode))
  11207. minute (or (nth 1 tl) (nth 1 defdecode))
  11208. second (or (nth 0 tl) 0)
  11209. wday (nth 6 tl))
  11210. ;; Special date definitions below
  11211. (cond
  11212. (iso-week
  11213. ;; There was an iso week
  11214. (setq year (or iso-year year)
  11215. day (or iso-weekday wday 1)
  11216. wday nil ; to make sure that the trigger below does not match
  11217. iso-date (calendar-gregorian-from-absolute
  11218. (calendar-absolute-from-iso
  11219. (list iso-week day year))))
  11220. ; FIXME: Should we also push ISO weeks into the future?
  11221. ; (when (and org-read-date-prefer-future
  11222. ; (not iso-year)
  11223. ; (< (calendar-absolute-from-gregorian iso-date)
  11224. ; (time-to-days (current-time))))
  11225. ; (setq year (1+ year)
  11226. ; iso-date (calendar-gregorian-from-absolute
  11227. ; (calendar-absolute-from-iso
  11228. ; (list iso-week day year)))))
  11229. (setq month (car iso-date)
  11230. year (nth 2 iso-date)
  11231. day (nth 1 iso-date)))
  11232. (deltan
  11233. (unless deltadef
  11234. (let ((now (decode-time (current-time))))
  11235. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11236. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11237. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11238. ((equal deltaw "m") (setq month (+ month deltan)))
  11239. ((equal deltaw "y") (setq year (+ year deltan)))))
  11240. ((and wday (not (nth 3 tl)))
  11241. ;; Weekday was given, but no day, so pick that day in the week
  11242. ;; on or after the derived date.
  11243. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11244. (unless (equal wday wday1)
  11245. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11246. (if (and (boundp 'org-time-was-given)
  11247. (nth 2 tl))
  11248. (setq org-time-was-given t))
  11249. (if (< year 100) (setq year (+ 2000 year)))
  11250. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11251. (list second minute hour day month year)))
  11252. (defvar parse-time-weekdays)
  11253. (defun org-read-date-get-relative (s today default)
  11254. "Check string S for special relative date string.
  11255. TODAY and DEFAULT are internal times, for today and for a default.
  11256. Return shift list (N what def-flag)
  11257. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11258. N is the number of WHATs to shift.
  11259. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11260. the DEFAULT date rather than TODAY."
  11261. (when (and
  11262. (string-match
  11263. (concat
  11264. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11265. "\\([0-9]+\\)?"
  11266. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11267. "\\([ \t]\\|$\\)") s)
  11268. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11269. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11270. (string-to-char (substring (match-string 1 s) -1))
  11271. ?+))
  11272. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11273. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11274. (what (if (match-end 3) (match-string 3 s) "d"))
  11275. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11276. (date (if rel default today))
  11277. (wday (nth 6 (decode-time date)))
  11278. delta)
  11279. (if wday1
  11280. (progn
  11281. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11282. (if (= dir ?-) (setq delta (- delta 7)))
  11283. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11284. (list delta "d" rel))
  11285. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11286. (defun org-eval-in-calendar (form &optional keepdate)
  11287. "Eval FORM in the calendar window and return to current window.
  11288. Also, store the cursor date in variable org-ans2."
  11289. (let ((sw (selected-window)))
  11290. (select-window (get-buffer-window "*Calendar*"))
  11291. (eval form)
  11292. (when (and (not keepdate) (calendar-cursor-to-date))
  11293. (let* ((date (calendar-cursor-to-date))
  11294. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11295. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11296. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11297. (select-window sw)))
  11298. (defun org-calendar-select ()
  11299. "Return to `org-read-date' with the date currently selected.
  11300. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11301. (interactive)
  11302. (when (calendar-cursor-to-date)
  11303. (let* ((date (calendar-cursor-to-date))
  11304. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11305. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11306. (if (active-minibuffer-window) (exit-minibuffer))))
  11307. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11308. "Insert a date stamp for the date given by the internal TIME.
  11309. WITH-HM means, use the stamp format that includes the time of the day.
  11310. INACTIVE means use square brackets instead of angular ones, so that the
  11311. stamp will not contribute to the agenda.
  11312. PRE and POST are optional strings to be inserted before and after the
  11313. stamp.
  11314. The command returns the inserted time stamp."
  11315. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11316. stamp)
  11317. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11318. (insert-before-markers (or pre ""))
  11319. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11320. (when (listp extra)
  11321. (setq extra (car extra))
  11322. (if (and (stringp extra)
  11323. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11324. (setq extra (format "-%02d:%02d"
  11325. (string-to-number (match-string 1 extra))
  11326. (string-to-number (match-string 2 extra))))
  11327. (setq extra nil)))
  11328. (when extra
  11329. (backward-char 1)
  11330. (insert-before-markers extra)
  11331. (forward-char 1))
  11332. (insert-before-markers (or post ""))
  11333. (setq org-last-inserted-timestamp stamp)))
  11334. (defun org-toggle-time-stamp-overlays ()
  11335. "Toggle the use of custom time stamp formats."
  11336. (interactive)
  11337. (setq org-display-custom-times (not org-display-custom-times))
  11338. (unless org-display-custom-times
  11339. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11340. (while (setq p (next-single-property-change p 'display))
  11341. (if (and (get-text-property p 'display)
  11342. (eq (get-text-property p 'face) 'org-date))
  11343. (remove-text-properties
  11344. p (setq p (next-single-property-change p 'display))
  11345. '(display t))))
  11346. (set-buffer-modified-p bmp)))
  11347. (if (featurep 'xemacs)
  11348. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11349. (org-restart-font-lock)
  11350. (setq org-table-may-need-update t)
  11351. (if org-display-custom-times
  11352. (message "Time stamps are overlayed with custom format")
  11353. (message "Time stamp overlays removed")))
  11354. (defun org-display-custom-time (beg end)
  11355. "Overlay modified time stamp format over timestamp between BEG and END."
  11356. (let* ((ts (buffer-substring beg end))
  11357. t1 w1 with-hm tf time str w2 (off 0))
  11358. (save-match-data
  11359. (setq t1 (org-parse-time-string ts t))
  11360. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11361. (setq off (- (match-end 0) (match-beginning 0)))))
  11362. (setq end (- end off))
  11363. (setq w1 (- end beg)
  11364. with-hm (and (nth 1 t1) (nth 2 t1))
  11365. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11366. time (org-fix-decoded-time t1)
  11367. str (org-add-props
  11368. (format-time-string
  11369. (substring tf 1 -1) (apply 'encode-time time))
  11370. nil 'mouse-face 'highlight)
  11371. w2 (length str))
  11372. (if (not (= w2 w1))
  11373. (add-text-properties (1+ beg) (+ 2 beg)
  11374. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11375. (if (featurep 'xemacs)
  11376. (progn
  11377. (put-text-property beg end 'invisible t)
  11378. (put-text-property beg end 'end-glyph (make-glyph str)))
  11379. (put-text-property beg end 'display str))))
  11380. (defun org-translate-time (string)
  11381. "Translate all timestamps in STRING to custom format.
  11382. But do this only if the variable `org-display-custom-times' is set."
  11383. (when org-display-custom-times
  11384. (save-match-data
  11385. (let* ((start 0)
  11386. (re org-ts-regexp-both)
  11387. t1 with-hm inactive tf time str beg end)
  11388. (while (setq start (string-match re string start))
  11389. (setq beg (match-beginning 0)
  11390. end (match-end 0)
  11391. t1 (save-match-data
  11392. (org-parse-time-string (substring string beg end) t))
  11393. with-hm (and (nth 1 t1) (nth 2 t1))
  11394. inactive (equal (substring string beg (1+ beg)) "[")
  11395. tf (funcall (if with-hm 'cdr 'car)
  11396. org-time-stamp-custom-formats)
  11397. time (org-fix-decoded-time t1)
  11398. str (format-time-string
  11399. (concat
  11400. (if inactive "[" "<") (substring tf 1 -1)
  11401. (if inactive "]" ">"))
  11402. (apply 'encode-time time))
  11403. string (replace-match str t t string)
  11404. start (+ start (length str)))))))
  11405. string)
  11406. (defun org-fix-decoded-time (time)
  11407. "Set 0 instead of nil for the first 6 elements of time.
  11408. Don't touch the rest."
  11409. (let ((n 0))
  11410. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11411. (defun org-days-to-time (timestamp-string)
  11412. "Difference between TIMESTAMP-STRING and now in days."
  11413. (- (time-to-days (org-time-string-to-time timestamp-string))
  11414. (time-to-days (current-time))))
  11415. (defun org-deadline-close (timestamp-string &optional ndays)
  11416. "Is the time in TIMESTAMP-STRING close to the current date?"
  11417. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11418. (and (< (org-days-to-time timestamp-string) ndays)
  11419. (not (org-entry-is-done-p))))
  11420. (defun org-get-wdays (ts)
  11421. "Get the deadline lead time appropriate for timestring TS."
  11422. (cond
  11423. ((<= org-deadline-warning-days 0)
  11424. ;; 0 or negative, enforce this value no matter what
  11425. (- org-deadline-warning-days))
  11426. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11427. ;; lead time is specified.
  11428. (floor (* (string-to-number (match-string 1 ts))
  11429. (cdr (assoc (match-string 2 ts)
  11430. '(("d" . 1) ("w" . 7)
  11431. ("m" . 30.4) ("y" . 365.25)))))))
  11432. ;; go for the default.
  11433. (t org-deadline-warning-days)))
  11434. (defun org-calendar-select-mouse (ev)
  11435. "Return to `org-read-date' with the date currently selected.
  11436. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11437. (interactive "e")
  11438. (mouse-set-point ev)
  11439. (when (calendar-cursor-to-date)
  11440. (let* ((date (calendar-cursor-to-date))
  11441. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11442. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11443. (if (active-minibuffer-window) (exit-minibuffer))))
  11444. (defun org-check-deadlines (ndays)
  11445. "Check if there are any deadlines due or past due.
  11446. A deadline is considered due if it happens within `org-deadline-warning-days'
  11447. days from today's date. If the deadline appears in an entry marked DONE,
  11448. it is not shown. The prefix arg NDAYS can be used to test that many
  11449. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11450. (interactive "P")
  11451. (let* ((org-warn-days
  11452. (cond
  11453. ((equal ndays '(4)) 100000)
  11454. (ndays (prefix-numeric-value ndays))
  11455. (t (abs org-deadline-warning-days))))
  11456. (case-fold-search nil)
  11457. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11458. (callback
  11459. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11460. (message "%d deadlines past-due or due within %d days"
  11461. (org-occur regexp nil callback)
  11462. org-warn-days)))
  11463. (defun org-check-before-date (date)
  11464. "Check if there are deadlines or scheduled entries before DATE."
  11465. (interactive (list (org-read-date)))
  11466. (let ((case-fold-search nil)
  11467. (regexp (concat "\\<\\(" org-deadline-string
  11468. "\\|" org-scheduled-string
  11469. "\\) *<\\([^>]+\\)>"))
  11470. (callback
  11471. (lambda () (time-less-p
  11472. (org-time-string-to-time (match-string 2))
  11473. (org-time-string-to-time date)))))
  11474. (message "%d entries before %s"
  11475. (org-occur regexp nil callback) date)))
  11476. (defun org-check-after-date (date)
  11477. "Check if there are deadlines or scheduled entries after DATE."
  11478. (interactive (list (org-read-date)))
  11479. (let ((case-fold-search nil)
  11480. (regexp (concat "\\<\\(" org-deadline-string
  11481. "\\|" org-scheduled-string
  11482. "\\) *<\\([^>]+\\)>"))
  11483. (callback
  11484. (lambda () (not
  11485. (time-less-p
  11486. (org-time-string-to-time (match-string 2))
  11487. (org-time-string-to-time date))))))
  11488. (message "%d entries after %s"
  11489. (org-occur regexp nil callback) date)))
  11490. (defun org-evaluate-time-range (&optional to-buffer)
  11491. "Evaluate a time range by computing the difference between start and end.
  11492. Normally the result is just printed in the echo area, but with prefix arg
  11493. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11494. If the time range is actually in a table, the result is inserted into the
  11495. next column.
  11496. For time difference computation, a year is assumed to be exactly 365
  11497. days in order to avoid rounding problems."
  11498. (interactive "P")
  11499. (or
  11500. (org-clock-update-time-maybe)
  11501. (save-excursion
  11502. (unless (org-at-date-range-p t)
  11503. (goto-char (point-at-bol))
  11504. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11505. (if (not (org-at-date-range-p t))
  11506. (error "Not at a time-stamp range, and none found in current line")))
  11507. (let* ((ts1 (match-string 1))
  11508. (ts2 (match-string 2))
  11509. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11510. (match-end (match-end 0))
  11511. (time1 (org-time-string-to-time ts1))
  11512. (time2 (org-time-string-to-time ts2))
  11513. (t1 (time-to-seconds time1))
  11514. (t2 (time-to-seconds time2))
  11515. (diff (abs (- t2 t1)))
  11516. (negative (< (- t2 t1) 0))
  11517. ;; (ys (floor (* 365 24 60 60)))
  11518. (ds (* 24 60 60))
  11519. (hs (* 60 60))
  11520. (fy "%dy %dd %02d:%02d")
  11521. (fy1 "%dy %dd")
  11522. (fd "%dd %02d:%02d")
  11523. (fd1 "%dd")
  11524. (fh "%02d:%02d")
  11525. y d h m align)
  11526. (if havetime
  11527. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11528. y 0
  11529. d (floor (/ diff ds)) diff (mod diff ds)
  11530. h (floor (/ diff hs)) diff (mod diff hs)
  11531. m (floor (/ diff 60)))
  11532. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11533. y 0
  11534. d (floor (+ (/ diff ds) 0.5))
  11535. h 0 m 0))
  11536. (if (not to-buffer)
  11537. (message "%s" (org-make-tdiff-string y d h m))
  11538. (if (org-at-table-p)
  11539. (progn
  11540. (goto-char match-end)
  11541. (setq align t)
  11542. (and (looking-at " *|") (goto-char (match-end 0))))
  11543. (goto-char match-end))
  11544. (if (looking-at
  11545. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11546. (replace-match ""))
  11547. (if negative (insert " -"))
  11548. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11549. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11550. (insert " " (format fh h m))))
  11551. (if align (org-table-align))
  11552. (message "Time difference inserted")))))
  11553. (defun org-make-tdiff-string (y d h m)
  11554. (let ((fmt "")
  11555. (l nil))
  11556. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11557. l (push y l)))
  11558. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11559. l (push d l)))
  11560. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11561. l (push h l)))
  11562. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11563. l (push m l)))
  11564. (apply 'format fmt (nreverse l))))
  11565. (defun org-time-string-to-time (s)
  11566. (apply 'encode-time (org-parse-time-string s)))
  11567. (defun org-time-string-to-seconds (s)
  11568. (time-to-seconds (org-time-string-to-time s)))
  11569. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11570. "Convert a time stamp to an absolute day number.
  11571. If there is a specifyer for a cyclic time stamp, get the closest date to
  11572. DAYNR.
  11573. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  11574. the variable date is bound by the calendar when this is called."
  11575. (cond
  11576. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11577. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11578. daynr
  11579. (+ daynr 1000)))
  11580. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11581. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11582. (time-to-days (current-time))) (match-string 0 s)
  11583. prefer show-all))
  11584. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11585. (defun org-days-to-iso-week (days)
  11586. "Return the iso week number."
  11587. (require 'cal-iso)
  11588. (car (calendar-iso-from-absolute days)))
  11589. (defun org-small-year-to-year (year)
  11590. "Convert 2-digit years into 4-digit years.
  11591. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11592. The year 2000 cannot be abbreviated. Any year larger than 99
  11593. is returned unchanged."
  11594. (if (< year 38)
  11595. (setq year (+ 2000 year))
  11596. (if (< year 100)
  11597. (setq year (+ 1900 year))))
  11598. year)
  11599. (defun org-time-from-absolute (d)
  11600. "Return the time corresponding to date D.
  11601. D may be an absolute day number, or a calendar-type list (month day year)."
  11602. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11603. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11604. (defun org-calendar-holiday ()
  11605. "List of holidays, for Diary display in Org-mode."
  11606. (require 'holidays)
  11607. (let ((hl (funcall
  11608. (if (fboundp 'calendar-check-holidays)
  11609. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11610. (if hl (mapconcat 'identity hl "; "))))
  11611. (defun org-diary-sexp-entry (sexp entry date)
  11612. "Process a SEXP diary ENTRY for DATE."
  11613. (require 'diary-lib)
  11614. (let ((result (if calendar-debug-sexp
  11615. (let ((stack-trace-on-error t))
  11616. (eval (car (read-from-string sexp))))
  11617. (condition-case nil
  11618. (eval (car (read-from-string sexp)))
  11619. (error
  11620. (beep)
  11621. (message "Bad sexp at line %d in %s: %s"
  11622. (org-current-line)
  11623. (buffer-file-name) sexp)
  11624. (sleep-for 2))))))
  11625. (cond ((stringp result) result)
  11626. ((and (consp result)
  11627. (stringp (cdr result))) (cdr result))
  11628. (result entry)
  11629. (t nil))))
  11630. (defun org-diary-to-ical-string (frombuf)
  11631. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11632. This uses the icalendar.el library."
  11633. (let* ((tmpdir (if (featurep 'xemacs)
  11634. (temp-directory)
  11635. temporary-file-directory))
  11636. (tmpfile (make-temp-name
  11637. (expand-file-name "orgics" tmpdir)))
  11638. buf rtn b e)
  11639. (save-excursion
  11640. (set-buffer frombuf)
  11641. (icalendar-export-region (point-min) (point-max) tmpfile)
  11642. (setq buf (find-buffer-visiting tmpfile))
  11643. (set-buffer buf)
  11644. (goto-char (point-min))
  11645. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11646. (setq b (match-beginning 0)))
  11647. (goto-char (point-max))
  11648. (if (re-search-backward "^END:VEVENT" nil t)
  11649. (setq e (match-end 0)))
  11650. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11651. (kill-buffer buf)
  11652. (delete-file tmpfile)
  11653. rtn))
  11654. (defun org-closest-date (start current change prefer show-all)
  11655. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11656. When PREFER is `past' return a date that is either CURRENT or past.
  11657. When PREFER is `future', return a date that is either CURRENT or future.
  11658. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11659. ;; Make the proper lists from the dates
  11660. (catch 'exit
  11661. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11662. dn dw sday cday n1 n2 n0
  11663. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11664. (setq start (org-date-to-gregorian start)
  11665. current (org-date-to-gregorian
  11666. (if show-all
  11667. current
  11668. (time-to-days (current-time))))
  11669. sday (calendar-absolute-from-gregorian start)
  11670. cday (calendar-absolute-from-gregorian current))
  11671. (if (<= cday sday) (throw 'exit sday))
  11672. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11673. (setq dn (string-to-number (match-string 1 change))
  11674. dw (cdr (assoc (match-string 2 change) a1)))
  11675. (error "Invalid change specifyer: %s" change))
  11676. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11677. (cond
  11678. ((eq dw 'day)
  11679. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11680. n2 (+ n1 dn)))
  11681. ((eq dw 'year)
  11682. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11683. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11684. (setq date1 (list m d y1)
  11685. n1 (calendar-absolute-from-gregorian date1)
  11686. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11687. n2 (calendar-absolute-from-gregorian date2)))
  11688. ((eq dw 'month)
  11689. ;; approx number of month between the two dates
  11690. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11691. ;; How often does dn fit in there?
  11692. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11693. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11694. m (+ m nm)
  11695. ny (floor (/ m 12))
  11696. y (+ y ny)
  11697. m (- m (* ny 12)))
  11698. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11699. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11700. (setq m2 (+ m dn) y2 y)
  11701. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11702. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11703. (while (<= n2 cday)
  11704. (setq n1 n2 m m2 y y2)
  11705. (setq m2 (+ m dn) y2 y)
  11706. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11707. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11708. ;; Make sure n1 is the earlier date
  11709. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11710. (if show-all
  11711. (cond
  11712. ((eq prefer 'past) n1)
  11713. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11714. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11715. (cond
  11716. ((eq prefer 'past) n1)
  11717. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11718. (t (if (= cday n1) n1 n2)))))))
  11719. (defun org-date-to-gregorian (date)
  11720. "Turn any specification of DATE into a gregorian date for the calendar."
  11721. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11722. ((and (listp date) (= (length date) 3)) date)
  11723. ((stringp date)
  11724. (setq date (org-parse-time-string date))
  11725. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11726. ((listp date)
  11727. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11728. (defun org-parse-time-string (s &optional nodefault)
  11729. "Parse the standard Org-mode time string.
  11730. This should be a lot faster than the normal `parse-time-string'.
  11731. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11732. hour and minute fields will be nil if not given."
  11733. (if (string-match org-ts-regexp0 s)
  11734. (list 0
  11735. (if (or (match-beginning 8) (not nodefault))
  11736. (string-to-number (or (match-string 8 s) "0")))
  11737. (if (or (match-beginning 7) (not nodefault))
  11738. (string-to-number (or (match-string 7 s) "0")))
  11739. (string-to-number (match-string 4 s))
  11740. (string-to-number (match-string 3 s))
  11741. (string-to-number (match-string 2 s))
  11742. nil nil nil)
  11743. (make-list 9 0)))
  11744. (defun org-timestamp-up (&optional arg)
  11745. "Increase the date item at the cursor by one.
  11746. If the cursor is on the year, change the year. If it is on the month or
  11747. the day, change that.
  11748. With prefix ARG, change by that many units."
  11749. (interactive "p")
  11750. (org-timestamp-change (prefix-numeric-value arg)))
  11751. (defun org-timestamp-down (&optional arg)
  11752. "Decrease the date item at the cursor by one.
  11753. If the cursor is on the year, change the year. If it is on the month or
  11754. the day, change that.
  11755. With prefix ARG, change by that many units."
  11756. (interactive "p")
  11757. (org-timestamp-change (- (prefix-numeric-value arg))))
  11758. (defun org-timestamp-up-day (&optional arg)
  11759. "Increase the date in the time stamp by one day.
  11760. With prefix ARG, change that many days."
  11761. (interactive "p")
  11762. (if (and (not (org-at-timestamp-p t))
  11763. (org-on-heading-p))
  11764. (org-todo 'up)
  11765. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11766. (defun org-timestamp-down-day (&optional arg)
  11767. "Decrease the date in the time stamp by one day.
  11768. With prefix ARG, change that many days."
  11769. (interactive "p")
  11770. (if (and (not (org-at-timestamp-p t))
  11771. (org-on-heading-p))
  11772. (org-todo 'down)
  11773. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11774. (defun org-at-timestamp-p (&optional inactive-ok)
  11775. "Determine if the cursor is in or at a timestamp."
  11776. (interactive)
  11777. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11778. (pos (point))
  11779. (ans (or (looking-at tsr)
  11780. (save-excursion
  11781. (skip-chars-backward "^[<\n\r\t")
  11782. (if (> (point) (point-min)) (backward-char 1))
  11783. (and (looking-at tsr)
  11784. (> (- (match-end 0) pos) -1))))))
  11785. (and ans
  11786. (boundp 'org-ts-what)
  11787. (setq org-ts-what
  11788. (cond
  11789. ((= pos (match-beginning 0)) 'bracket)
  11790. ((= pos (1- (match-end 0))) 'bracket)
  11791. ((org-pos-in-match-range pos 2) 'year)
  11792. ((org-pos-in-match-range pos 3) 'month)
  11793. ((org-pos-in-match-range pos 7) 'hour)
  11794. ((org-pos-in-match-range pos 8) 'minute)
  11795. ((or (org-pos-in-match-range pos 4)
  11796. (org-pos-in-match-range pos 5)) 'day)
  11797. ((and (> pos (or (match-end 8) (match-end 5)))
  11798. (< pos (match-end 0)))
  11799. (- pos (or (match-end 8) (match-end 5))))
  11800. (t 'day))))
  11801. ans))
  11802. (defun org-toggle-timestamp-type ()
  11803. "Toggle the type (<active> or [inactive]) of a time stamp."
  11804. (interactive)
  11805. (when (org-at-timestamp-p t)
  11806. (let ((beg (match-beginning 0)) (end (match-end 0))
  11807. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11808. (save-excursion
  11809. (goto-char beg)
  11810. (while (re-search-forward "[][<>]" end t)
  11811. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11812. t t)))
  11813. (message "Timestamp is now %sactive"
  11814. (if (equal (char-after beg) ?<) "" "in")))))
  11815. (defun org-timestamp-change (n &optional what)
  11816. "Change the date in the time stamp at point.
  11817. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11818. `year', `minute', `second'. If WHAT is not given, the cursor position
  11819. in the timestamp determines what will be changed."
  11820. (let ((pos (point))
  11821. with-hm inactive
  11822. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11823. org-ts-what
  11824. extra rem
  11825. ts time time0)
  11826. (if (not (org-at-timestamp-p t))
  11827. (error "Not at a timestamp"))
  11828. (if (and (not what) (eq org-ts-what 'bracket))
  11829. (org-toggle-timestamp-type)
  11830. (if (and (not what) (not (eq org-ts-what 'day))
  11831. org-display-custom-times
  11832. (get-text-property (point) 'display)
  11833. (not (get-text-property (1- (point)) 'display)))
  11834. (setq org-ts-what 'day))
  11835. (setq org-ts-what (or what org-ts-what)
  11836. inactive (= (char-after (match-beginning 0)) ?\[)
  11837. ts (match-string 0))
  11838. (replace-match "")
  11839. (if (string-match
  11840. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11841. ts)
  11842. (setq extra (match-string 1 ts)))
  11843. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11844. (setq with-hm t))
  11845. (setq time0 (org-parse-time-string ts))
  11846. (when (and (eq org-ts-what 'minute)
  11847. (eq current-prefix-arg nil))
  11848. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11849. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11850. (setcar (cdr time0) (+ (nth 1 time0)
  11851. (if (> n 0) (- rem) (- dm rem))))))
  11852. (setq time
  11853. (encode-time (or (car time0) 0)
  11854. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11855. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11856. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11857. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11858. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11859. (nthcdr 6 time0)))
  11860. (when (and (member org-ts-what '(hour minute))
  11861. extra
  11862. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11863. (setq extra (org-modify-ts-extra
  11864. extra
  11865. (if (eq org-ts-what 'hour) 2 5)
  11866. n dm)))
  11867. (when (integerp org-ts-what)
  11868. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11869. (if (eq what 'calendar)
  11870. (let ((cal-date (org-get-date-from-calendar)))
  11871. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11872. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11873. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11874. (setcar time0 (or (car time0) 0))
  11875. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11876. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11877. (setq time (apply 'encode-time time0))))
  11878. (setq org-last-changed-timestamp
  11879. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11880. (org-clock-update-time-maybe)
  11881. (goto-char pos)
  11882. ;; Try to recenter the calendar window, if any
  11883. (if (and org-calendar-follow-timestamp-change
  11884. (get-buffer-window "*Calendar*" t)
  11885. (memq org-ts-what '(day month year)))
  11886. (org-recenter-calendar (time-to-days time))))))
  11887. (defun org-modify-ts-extra (s pos n dm)
  11888. "Change the different parts of the lead-time and repeat fields in timestamp."
  11889. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11890. ng h m new rem)
  11891. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11892. (cond
  11893. ((or (org-pos-in-match-range pos 2)
  11894. (org-pos-in-match-range pos 3))
  11895. (setq m (string-to-number (match-string 3 s))
  11896. h (string-to-number (match-string 2 s)))
  11897. (if (org-pos-in-match-range pos 2)
  11898. (setq h (+ h n))
  11899. (setq n (* dm (org-no-warnings (signum n))))
  11900. (when (not (= 0 (setq rem (% m dm))))
  11901. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11902. (setq m (+ m n)))
  11903. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11904. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11905. (setq h (min 24 (max 0 h)))
  11906. (setq ng 1 new (format "-%02d:%02d" h m)))
  11907. ((org-pos-in-match-range pos 6)
  11908. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11909. ((org-pos-in-match-range pos 5)
  11910. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11911. ((org-pos-in-match-range pos 9)
  11912. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11913. ((org-pos-in-match-range pos 8)
  11914. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11915. (when ng
  11916. (setq s (concat
  11917. (substring s 0 (match-beginning ng))
  11918. new
  11919. (substring s (match-end ng))))))
  11920. s))
  11921. (defun org-recenter-calendar (date)
  11922. "If the calendar is visible, recenter it to DATE."
  11923. (let* ((win (selected-window))
  11924. (cwin (get-buffer-window "*Calendar*" t))
  11925. (calendar-move-hook nil))
  11926. (when cwin
  11927. (select-window cwin)
  11928. (calendar-goto-date (if (listp date) date
  11929. (calendar-gregorian-from-absolute date)))
  11930. (select-window win))))
  11931. (defun org-goto-calendar (&optional arg)
  11932. "Go to the Emacs calendar at the current date.
  11933. If there is a time stamp in the current line, go to that date.
  11934. A prefix ARG can be used to force the current date."
  11935. (interactive "P")
  11936. (let ((tsr org-ts-regexp) diff
  11937. (calendar-move-hook nil)
  11938. (calendar-view-holidays-initially-flag nil)
  11939. (view-calendar-holidays-initially nil)
  11940. (calendar-view-diary-initially-flag nil)
  11941. (view-diary-entries-initially nil))
  11942. (if (or (org-at-timestamp-p)
  11943. (save-excursion
  11944. (beginning-of-line 1)
  11945. (looking-at (concat ".*" tsr))))
  11946. (let ((d1 (time-to-days (current-time)))
  11947. (d2 (time-to-days
  11948. (org-time-string-to-time (match-string 1)))))
  11949. (setq diff (- d2 d1))))
  11950. (calendar)
  11951. (calendar-goto-today)
  11952. (if (and diff (not arg)) (calendar-forward-day diff))))
  11953. (defun org-get-date-from-calendar ()
  11954. "Return a list (month day year) of date at point in calendar."
  11955. (with-current-buffer "*Calendar*"
  11956. (save-match-data
  11957. (calendar-cursor-to-date))))
  11958. (defun org-date-from-calendar ()
  11959. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11960. If there is already a time stamp at the cursor position, update it."
  11961. (interactive)
  11962. (if (org-at-timestamp-p t)
  11963. (org-timestamp-change 0 'calendar)
  11964. (let ((cal-date (org-get-date-from-calendar)))
  11965. (org-insert-time-stamp
  11966. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11967. (defun org-minutes-to-hh:mm-string (m)
  11968. "Compute H:MM from a number of minutes."
  11969. (let ((h (/ m 60)))
  11970. (setq m (- m (* 60 h)))
  11971. (format org-time-clocksum-format h m)))
  11972. (defun org-hh:mm-string-to-minutes (s)
  11973. "Convert a string H:MM to a number of minutes."
  11974. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11975. (+ (* (string-to-number (match-string 1 s)) 60)
  11976. (string-to-number (match-string 2 s)))
  11977. 0))
  11978. ;;;; Files
  11979. (defun org-save-all-org-buffers ()
  11980. "Save all Org-mode buffers without user confirmation."
  11981. (interactive)
  11982. (message "Saving all Org-mode buffers...")
  11983. (save-some-buffers t 'org-mode-p)
  11984. (when (featurep 'org-id) (org-id-locations-save))
  11985. (message "Saving all Org-mode buffers... done"))
  11986. (defun org-revert-all-org-buffers ()
  11987. "Revert all Org-mode buffers.
  11988. Prompt for confirmation when there are unsaved changes.
  11989. Be sure you know what you are doing before letting this function
  11990. overwrite your changes.
  11991. This function is useful in a setup where one tracks org files
  11992. with a version control system, to revert on one machine after pulling
  11993. changes from another. I believe the procedure must be like this:
  11994. 1. M-x org-save-all-org-buffers
  11995. 2. Pull changes from the other machine, resolve conflicts
  11996. 3. M-x org-revert-all-org-buffers"
  11997. (interactive)
  11998. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11999. (error "Abort"))
  12000. (save-excursion
  12001. (save-window-excursion
  12002. (mapc
  12003. (lambda (b)
  12004. (when (and (with-current-buffer b (org-mode-p))
  12005. (with-current-buffer b buffer-file-name))
  12006. (switch-to-buffer b)
  12007. (revert-buffer t 'no-confirm)))
  12008. (buffer-list))
  12009. (when (and (featurep 'org-id) org-id-track-globally)
  12010. (org-id-locations-load)))))
  12011. ;;;; Agenda files
  12012. ;;;###autoload
  12013. (defun org-iswitchb (&optional arg)
  12014. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12015. With a prefix argument, restrict available to files.
  12016. With two prefix arguments, restrict available buffers to agenda files.
  12017. Due to some yet unresolved reason, the global function
  12018. `iswitchb-mode' needs to be active for this function to work."
  12019. (interactive "P")
  12020. (require 'iswitchb)
  12021. (let ((enabled iswitchb-mode) blist)
  12022. (or enabled (iswitchb-mode 1))
  12023. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12024. ((equal arg '(16)) (org-buffer-list 'agenda))
  12025. (t (org-buffer-list))))
  12026. (unwind-protect
  12027. (let ((iswitchb-make-buflist-hook
  12028. (lambda ()
  12029. (setq iswitchb-temp-buflist
  12030. (mapcar 'buffer-name blist)))))
  12031. (switch-to-buffer
  12032. (iswitchb-read-buffer
  12033. "Switch-to: " nil t))
  12034. (or enabled (iswitchb-mode -1))))))
  12035. ;;;###autoload
  12036. (defun org-ido-switchb (&optional arg)
  12037. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12038. With a prefix argument, restrict available to files.
  12039. With two prefix arguments, restrict available buffers to agenda files."
  12040. (interactive "P")
  12041. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12042. ((equal arg '(16)) (org-buffer-list 'agenda))
  12043. (t (org-buffer-list)))))
  12044. (switch-to-buffer
  12045. (org-ido-completing-read "Org buffer: "
  12046. (mapcar 'list (mapcar 'buffer-name blist))
  12047. nil t))))
  12048. (defun org-buffer-list (&optional predicate exclude-tmp)
  12049. "Return a list of Org buffers.
  12050. PREDICATE can be `export', `files' or `agenda'.
  12051. export restrict the list to Export buffers.
  12052. files restrict the list to buffers visiting Org files.
  12053. agenda restrict the list to buffers visiting agenda files.
  12054. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12055. (let* ((bfn nil)
  12056. (agenda-files (and (eq predicate 'agenda)
  12057. (mapcar 'file-truename (org-agenda-files t))))
  12058. (filter
  12059. (cond
  12060. ((eq predicate 'files)
  12061. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12062. ((eq predicate 'export)
  12063. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12064. ((eq predicate 'agenda)
  12065. (lambda (b)
  12066. (with-current-buffer b
  12067. (and (eq major-mode 'org-mode)
  12068. (setq bfn (buffer-file-name b))
  12069. (member (file-truename bfn) agenda-files)))))
  12070. (t (lambda (b) (with-current-buffer b
  12071. (or (eq major-mode 'org-mode)
  12072. (string-match "\*Org .*Export"
  12073. (buffer-name b)))))))))
  12074. (delq nil
  12075. (mapcar
  12076. (lambda(b)
  12077. (if (and (funcall filter b)
  12078. (or (not exclude-tmp)
  12079. (not (string-match "tmp" (buffer-name b)))))
  12080. b
  12081. nil))
  12082. (buffer-list)))))
  12083. (defun org-agenda-files (&optional unrestricted archives)
  12084. "Get the list of agenda files.
  12085. Optional UNRESTRICTED means return the full list even if a restriction
  12086. is currently in place.
  12087. When ARCHIVES is t, include all archive files hat are really being
  12088. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12089. `org-agenda-archives-mode' is t."
  12090. (let ((files
  12091. (cond
  12092. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12093. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12094. ((listp org-agenda-files) org-agenda-files)
  12095. (t (error "Invalid value of `org-agenda-files'")))))
  12096. (setq files (apply 'append
  12097. (mapcar (lambda (f)
  12098. (if (file-directory-p f)
  12099. (directory-files
  12100. f t org-agenda-file-regexp)
  12101. (list f)))
  12102. files)))
  12103. (when org-agenda-skip-unavailable-files
  12104. (setq files (delq nil
  12105. (mapcar (function
  12106. (lambda (file)
  12107. (and (file-readable-p file) file)))
  12108. files))))
  12109. (when (or (eq archives t)
  12110. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12111. (setq files (org-add-archive-files files)))
  12112. files))
  12113. (defun org-edit-agenda-file-list ()
  12114. "Edit the list of agenda files.
  12115. Depending on setup, this either uses customize to edit the variable
  12116. `org-agenda-files', or it visits the file that is holding the list. In the
  12117. latter case, the buffer is set up in a way that saving it automatically kills
  12118. the buffer and restores the previous window configuration."
  12119. (interactive)
  12120. (if (stringp org-agenda-files)
  12121. (let ((cw (current-window-configuration)))
  12122. (find-file org-agenda-files)
  12123. (org-set-local 'org-window-configuration cw)
  12124. (org-add-hook 'after-save-hook
  12125. (lambda ()
  12126. (set-window-configuration
  12127. (prog1 org-window-configuration
  12128. (kill-buffer (current-buffer))))
  12129. (org-install-agenda-files-menu)
  12130. (message "New agenda file list installed"))
  12131. nil 'local)
  12132. (message "%s" (substitute-command-keys
  12133. "Edit list and finish with \\[save-buffer]")))
  12134. (customize-variable 'org-agenda-files)))
  12135. (defun org-store-new-agenda-file-list (list)
  12136. "Set new value for the agenda file list and save it correctly."
  12137. (if (stringp org-agenda-files)
  12138. (let ((f org-agenda-files) b)
  12139. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12140. (with-temp-file f
  12141. (insert (mapconcat 'identity list "\n") "\n")))
  12142. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12143. (setq org-agenda-files list)
  12144. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12145. (defun org-read-agenda-file-list ()
  12146. "Read the list of agenda files from a file."
  12147. (when (file-directory-p org-agenda-files)
  12148. (error "`org-agenda-files' cannot be a single directory"))
  12149. (when (stringp org-agenda-files)
  12150. (with-temp-buffer
  12151. (insert-file-contents org-agenda-files)
  12152. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12153. ;;;###autoload
  12154. (defun org-cycle-agenda-files ()
  12155. "Cycle through the files in `org-agenda-files'.
  12156. If the current buffer visits an agenda file, find the next one in the list.
  12157. If the current buffer does not, find the first agenda file."
  12158. (interactive)
  12159. (let* ((fs (org-agenda-files t))
  12160. (files (append fs (list (car fs))))
  12161. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12162. file)
  12163. (unless files (error "No agenda files"))
  12164. (catch 'exit
  12165. (while (setq file (pop files))
  12166. (if (equal (file-truename file) tcf)
  12167. (when (car files)
  12168. (find-file (car files))
  12169. (throw 'exit t))))
  12170. (find-file (car fs)))
  12171. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12172. (defun org-agenda-file-to-front (&optional to-end)
  12173. "Move/add the current file to the top of the agenda file list.
  12174. If the file is not present in the list, it is added to the front. If it is
  12175. present, it is moved there. With optional argument TO-END, add/move to the
  12176. end of the list."
  12177. (interactive "P")
  12178. (let ((org-agenda-skip-unavailable-files nil)
  12179. (file-alist (mapcar (lambda (x)
  12180. (cons (file-truename x) x))
  12181. (org-agenda-files t)))
  12182. (ctf (file-truename buffer-file-name))
  12183. x had)
  12184. (setq x (assoc ctf file-alist) had x)
  12185. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12186. (if to-end
  12187. (setq file-alist (append (delq x file-alist) (list x)))
  12188. (setq file-alist (cons x (delq x file-alist))))
  12189. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12190. (org-install-agenda-files-menu)
  12191. (message "File %s to %s of agenda file list"
  12192. (if had "moved" "added") (if to-end "end" "front"))))
  12193. (defun org-remove-file (&optional file)
  12194. "Remove current file from the list of files in variable `org-agenda-files'.
  12195. These are the files which are being checked for agenda entries.
  12196. Optional argument FILE means, use this file instead of the current."
  12197. (interactive)
  12198. (let* ((org-agenda-skip-unavailable-files nil)
  12199. (file (or file buffer-file-name))
  12200. (true-file (file-truename file))
  12201. (afile (abbreviate-file-name file))
  12202. (files (delq nil (mapcar
  12203. (lambda (x)
  12204. (if (equal true-file
  12205. (file-truename x))
  12206. nil x))
  12207. (org-agenda-files t)))))
  12208. (if (not (= (length files) (length (org-agenda-files t))))
  12209. (progn
  12210. (org-store-new-agenda-file-list files)
  12211. (org-install-agenda-files-menu)
  12212. (message "Removed file: %s" afile))
  12213. (message "File was not in list: %s (not removed)" afile))))
  12214. (defun org-file-menu-entry (file)
  12215. (vector file (list 'find-file file) t))
  12216. (defun org-check-agenda-file (file)
  12217. "Make sure FILE exists. If not, ask user what to do."
  12218. (when (not (file-exists-p file))
  12219. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12220. (abbreviate-file-name file))
  12221. (let ((r (downcase (read-char-exclusive))))
  12222. (cond
  12223. ((equal r ?r)
  12224. (org-remove-file file)
  12225. (throw 'nextfile t))
  12226. (t (error "Abort"))))))
  12227. (defun org-get-agenda-file-buffer (file)
  12228. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12229. it to the list of buffers which might be released later."
  12230. (let ((buf (org-find-base-buffer-visiting file)))
  12231. (if buf
  12232. buf ; just return it
  12233. ;; Make a new buffer and remember it
  12234. (setq buf (find-file-noselect file))
  12235. (if buf (push buf org-agenda-new-buffers))
  12236. buf)))
  12237. (defun org-release-buffers (blist)
  12238. "Release all buffers in list, asking the user for confirmation when needed.
  12239. When a buffer is unmodified, it is just killed. When modified, it is saved
  12240. \(if the user agrees) and then killed."
  12241. (let (buf file)
  12242. (while (setq buf (pop blist))
  12243. (setq file (buffer-file-name buf))
  12244. (when (and (buffer-modified-p buf)
  12245. file
  12246. (y-or-n-p (format "Save file %s? " file)))
  12247. (with-current-buffer buf (save-buffer)))
  12248. (kill-buffer buf))))
  12249. (defun org-prepare-agenda-buffers (files)
  12250. "Create buffers for all agenda files, protect archived trees and comments."
  12251. (interactive)
  12252. (let ((pa '(:org-archived t))
  12253. (pc '(:org-comment t))
  12254. (pall '(:org-archived t :org-comment t))
  12255. (inhibit-read-only t)
  12256. (rea (concat ":" org-archive-tag ":"))
  12257. bmp file re)
  12258. (save-excursion
  12259. (save-restriction
  12260. (while (setq file (pop files))
  12261. (catch 'nextfile
  12262. (if (bufferp file)
  12263. (set-buffer file)
  12264. (org-check-agenda-file file)
  12265. (set-buffer (org-get-agenda-file-buffer file)))
  12266. (widen)
  12267. (setq bmp (buffer-modified-p))
  12268. (org-refresh-category-properties)
  12269. (setq org-todo-keywords-for-agenda
  12270. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12271. (setq org-done-keywords-for-agenda
  12272. (append org-done-keywords-for-agenda org-done-keywords))
  12273. (setq org-todo-keyword-alist-for-agenda
  12274. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12275. (setq org-tag-alist-for-agenda
  12276. (append org-tag-alist-for-agenda org-tag-alist))
  12277. (save-excursion
  12278. (remove-text-properties (point-min) (point-max) pall)
  12279. (when org-agenda-skip-archived-trees
  12280. (goto-char (point-min))
  12281. (while (re-search-forward rea nil t)
  12282. (if (org-on-heading-p t)
  12283. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12284. (goto-char (point-min))
  12285. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12286. (while (re-search-forward re nil t)
  12287. (add-text-properties
  12288. (match-beginning 0) (org-end-of-subtree t) pc)))
  12289. (set-buffer-modified-p bmp)))))
  12290. (setq org-todo-keyword-alist-for-agenda
  12291. (org-uniquify org-todo-keyword-alist-for-agenda)
  12292. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12293. ;;;; Embedded LaTeX
  12294. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12295. "Keymap for the minor `org-cdlatex-mode'.")
  12296. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12297. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12298. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12299. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12300. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12301. (defvar org-cdlatex-texmathp-advice-is-done nil
  12302. "Flag remembering if we have applied the advice to texmathp already.")
  12303. (define-minor-mode org-cdlatex-mode
  12304. "Toggle the minor `org-cdlatex-mode'.
  12305. This mode supports entering LaTeX environment and math in LaTeX fragments
  12306. in Org-mode.
  12307. \\{org-cdlatex-mode-map}"
  12308. nil " OCDL" nil
  12309. (when org-cdlatex-mode (require 'cdlatex))
  12310. (unless org-cdlatex-texmathp-advice-is-done
  12311. (setq org-cdlatex-texmathp-advice-is-done t)
  12312. (defadvice texmathp (around org-math-always-on activate)
  12313. "Always return t in org-mode buffers.
  12314. This is because we want to insert math symbols without dollars even outside
  12315. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12316. an embedded LaTeX fragment, let texmathp do its job.
  12317. \\[org-cdlatex-mode-map]"
  12318. (interactive)
  12319. (let (p)
  12320. (cond
  12321. ((not (org-mode-p)) ad-do-it)
  12322. ((eq this-command 'cdlatex-math-symbol)
  12323. (setq ad-return-value t
  12324. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12325. (t
  12326. (let ((p (org-inside-LaTeX-fragment-p)))
  12327. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12328. (setq ad-return-value t
  12329. texmathp-why '("Org-mode embedded math" . 0))
  12330. (if p ad-do-it)))))))))
  12331. (defun turn-on-org-cdlatex ()
  12332. "Unconditionally turn on `org-cdlatex-mode'."
  12333. (org-cdlatex-mode 1))
  12334. (defun org-inside-LaTeX-fragment-p ()
  12335. "Test if point is inside a LaTeX fragment.
  12336. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12337. sequence appearing also before point.
  12338. Even though the matchers for math are configurable, this function assumes
  12339. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12340. delimiters are skipped when they have been removed by customization.
  12341. The return value is nil, or a cons cell with the delimiter and
  12342. and the position of this delimiter.
  12343. This function does a reasonably good job, but can locally be fooled by
  12344. for example currency specifications. For example it will assume being in
  12345. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12346. fragments that are properly closed, but during editing, we have to live
  12347. with the uncertainty caused by missing closing delimiters. This function
  12348. looks only before point, not after."
  12349. (catch 'exit
  12350. (let ((pos (point))
  12351. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12352. (lim (progn
  12353. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12354. (point)))
  12355. dd-on str (start 0) m re)
  12356. (goto-char pos)
  12357. (when dodollar
  12358. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12359. re (nth 1 (assoc "$" org-latex-regexps)))
  12360. (while (string-match re str start)
  12361. (cond
  12362. ((= (match-end 0) (length str))
  12363. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12364. ((= (match-end 0) (- (length str) 5))
  12365. (throw 'exit nil))
  12366. (t (setq start (match-end 0))))))
  12367. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12368. (goto-char pos)
  12369. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12370. (and (match-beginning 2) (throw 'exit nil))
  12371. ;; count $$
  12372. (while (re-search-backward "\\$\\$" lim t)
  12373. (setq dd-on (not dd-on)))
  12374. (goto-char pos)
  12375. (if dd-on (cons "$$" m))))))
  12376. (defun org-try-cdlatex-tab ()
  12377. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12378. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12379. - inside a LaTeX fragment, or
  12380. - after the first word in a line, where an abbreviation expansion could
  12381. insert a LaTeX environment."
  12382. (when org-cdlatex-mode
  12383. (cond
  12384. ((save-excursion
  12385. (skip-chars-backward "a-zA-Z0-9*")
  12386. (skip-chars-backward " \t")
  12387. (bolp))
  12388. (cdlatex-tab) t)
  12389. ((org-inside-LaTeX-fragment-p)
  12390. (cdlatex-tab) t)
  12391. (t nil))))
  12392. (defun org-cdlatex-underscore-caret (&optional arg)
  12393. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12394. Revert to the normal definition outside of these fragments."
  12395. (interactive "P")
  12396. (if (org-inside-LaTeX-fragment-p)
  12397. (call-interactively 'cdlatex-sub-superscript)
  12398. (let (org-cdlatex-mode)
  12399. (call-interactively (key-binding (vector last-input-event))))))
  12400. (defun org-cdlatex-math-modify (&optional arg)
  12401. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12402. Revert to the normal definition outside of these fragments."
  12403. (interactive "P")
  12404. (if (org-inside-LaTeX-fragment-p)
  12405. (call-interactively 'cdlatex-math-modify)
  12406. (let (org-cdlatex-mode)
  12407. (call-interactively (key-binding (vector last-input-event))))))
  12408. (defvar org-latex-fragment-image-overlays nil
  12409. "List of overlays carrying the images of latex fragments.")
  12410. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12411. (defun org-remove-latex-fragment-image-overlays ()
  12412. "Remove all overlays with LaTeX fragment images in current buffer."
  12413. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12414. (setq org-latex-fragment-image-overlays nil))
  12415. (defun org-preview-latex-fragment (&optional subtree)
  12416. "Preview the LaTeX fragment at point, or all locally or globally.
  12417. If the cursor is in a LaTeX fragment, create the image and overlay
  12418. it over the source code. If there is no fragment at point, display
  12419. all fragments in the current text, from one headline to the next. With
  12420. prefix SUBTREE, display all fragments in the current subtree. With a
  12421. double prefix `C-u C-u', or when the cursor is before the first headline,
  12422. display all fragments in the buffer.
  12423. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12424. (interactive "P")
  12425. (org-remove-latex-fragment-image-overlays)
  12426. (save-excursion
  12427. (save-restriction
  12428. (let (beg end at msg)
  12429. (cond
  12430. ((or (equal subtree '(16))
  12431. (not (save-excursion
  12432. (re-search-backward (concat "^" outline-regexp) nil t))))
  12433. (setq beg (point-min) end (point-max)
  12434. msg "Creating images for buffer...%s"))
  12435. ((equal subtree '(4))
  12436. (org-back-to-heading)
  12437. (setq beg (point) end (org-end-of-subtree t)
  12438. msg "Creating images for subtree...%s"))
  12439. (t
  12440. (if (setq at (org-inside-LaTeX-fragment-p))
  12441. (goto-char (max (point-min) (- (cdr at) 2)))
  12442. (org-back-to-heading))
  12443. (setq beg (point) end (progn (outline-next-heading) (point))
  12444. msg (if at "Creating image...%s"
  12445. "Creating images for entry...%s"))))
  12446. (message msg "")
  12447. (narrow-to-region beg end)
  12448. (goto-char beg)
  12449. (org-format-latex
  12450. (concat "ltxpng/" (file-name-sans-extension
  12451. (file-name-nondirectory
  12452. buffer-file-name)))
  12453. default-directory 'overlays msg at 'forbuffer)
  12454. (message msg "done. Use `C-c C-c' to remove images.")))))
  12455. (defvar org-latex-regexps
  12456. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12457. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12458. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12459. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12460. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12461. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12462. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12463. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12464. "Regular expressions for matching embedded LaTeX.")
  12465. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12466. "Replace LaTeX fragments with links to an image, and produce images."
  12467. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12468. (let* ((prefixnodir (file-name-nondirectory prefix))
  12469. (absprefix (expand-file-name prefix dir))
  12470. (todir (file-name-directory absprefix))
  12471. (opt org-format-latex-options)
  12472. (matchers (plist-get opt :matchers))
  12473. (re-list org-latex-regexps)
  12474. (cnt 0) txt link beg end re e checkdir
  12475. executables-checked
  12476. m n block linkfile movefile ov)
  12477. ;; Check if there are old images files with this prefix, and remove them
  12478. (when (file-directory-p todir)
  12479. (mapc 'delete-file
  12480. (directory-files
  12481. todir 'full
  12482. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12483. ;; Check the different regular expressions
  12484. (while (setq e (pop re-list))
  12485. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12486. block (if (nth 3 e) "\n\n" ""))
  12487. (when (member m matchers)
  12488. (goto-char (point-min))
  12489. (while (re-search-forward re nil t)
  12490. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12491. (not (get-text-property (match-beginning n)
  12492. 'org-protected)))
  12493. (setq txt (match-string n)
  12494. beg (match-beginning n) end (match-end n)
  12495. cnt (1+ cnt)
  12496. linkfile (format "%s_%04d.png" prefix cnt)
  12497. movefile (format "%s_%04d.png" absprefix cnt)
  12498. link (concat block "[[file:" linkfile "]]" block))
  12499. (if msg (message msg cnt))
  12500. (goto-char beg)
  12501. (unless checkdir ; make sure the directory exists
  12502. (setq checkdir t)
  12503. (or (file-directory-p todir) (make-directory todir)))
  12504. (unless executables-checked
  12505. (org-check-external-command
  12506. "latex" "needed to convert LaTeX fragments to images")
  12507. (org-check-external-command
  12508. "dvipng" "needed to convert LaTeX fragments to images")
  12509. (setq executables-checked t))
  12510. (org-create-formula-image
  12511. txt movefile opt forbuffer)
  12512. (if overlays
  12513. (progn
  12514. (setq ov (org-make-overlay beg end))
  12515. (if (featurep 'xemacs)
  12516. (progn
  12517. (org-overlay-put ov 'invisible t)
  12518. (org-overlay-put
  12519. ov 'end-glyph
  12520. (make-glyph (vector 'png :file movefile))))
  12521. (org-overlay-put
  12522. ov 'display
  12523. (list 'image :type 'png :file movefile :ascent 'center)))
  12524. (push ov org-latex-fragment-image-overlays)
  12525. (goto-char end))
  12526. (delete-region beg end)
  12527. (insert link))))))))
  12528. ;; This function borrows from Ganesh Swami's latex2png.el
  12529. (defun org-create-formula-image (string tofile options buffer)
  12530. (let* ((tmpdir (if (featurep 'xemacs)
  12531. (temp-directory)
  12532. temporary-file-directory))
  12533. (texfilebase (make-temp-name
  12534. (expand-file-name "orgtex" tmpdir)))
  12535. (texfile (concat texfilebase ".tex"))
  12536. (dvifile (concat texfilebase ".dvi"))
  12537. (pngfile (concat texfilebase ".png"))
  12538. (fnh (if (featurep 'xemacs)
  12539. (font-height (get-face-font 'default))
  12540. (face-attribute 'default :height nil)))
  12541. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12542. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12543. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12544. "Black"))
  12545. (bg (or (plist-get options (if buffer :background :html-background))
  12546. "Transparent")))
  12547. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12548. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12549. (with-temp-file texfile
  12550. (insert org-format-latex-header
  12551. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12552. (let ((dir default-directory))
  12553. (condition-case nil
  12554. (progn
  12555. (cd tmpdir)
  12556. (call-process "latex" nil nil nil texfile))
  12557. (error nil))
  12558. (cd dir))
  12559. (if (not (file-exists-p dvifile))
  12560. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12561. (condition-case nil
  12562. (call-process "dvipng" nil nil nil
  12563. "-E" "-fg" fg "-bg" bg
  12564. "-D" dpi
  12565. ;;"-x" scale "-y" scale
  12566. "-T" "tight"
  12567. "-o" pngfile
  12568. dvifile)
  12569. (error nil))
  12570. (if (not (file-exists-p pngfile))
  12571. (progn (message "Failed to create png file from %s" texfile) nil)
  12572. ;; Use the requested file name and clean up
  12573. (copy-file pngfile tofile 'replace)
  12574. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12575. (delete-file (concat texfilebase e)))
  12576. pngfile))))
  12577. (defun org-dvipng-color (attr)
  12578. "Return an rgb color specification for dvipng."
  12579. (apply 'format "rgb %s %s %s"
  12580. (mapcar 'org-normalize-color
  12581. (color-values (face-attribute 'default attr nil)))))
  12582. (defun org-normalize-color (value)
  12583. "Return string to be used as color value for an RGB component."
  12584. (format "%g" (/ value 65535.0)))
  12585. ;;;; Key bindings
  12586. ;; Make `C-c C-x' a prefix key
  12587. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12588. ;; TAB key with modifiers
  12589. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12590. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12591. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12592. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12593. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12594. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12595. ;; The following line is necessary under Suse GNU/Linux
  12596. (unless (featurep 'xemacs)
  12597. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12598. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12599. (define-key org-mode-map [backtab] 'org-shifttab)
  12600. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12601. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12602. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12603. ;; Cursor keys with modifiers
  12604. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12605. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12606. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12607. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12608. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12609. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12610. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12611. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12612. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12613. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12614. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12615. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12616. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12617. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12618. ;;; Extra keys for tty access.
  12619. ;; We only set them when really needed because otherwise the
  12620. ;; menus don't show the simple keys
  12621. (when (or org-use-extra-keys
  12622. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12623. (not window-system))
  12624. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12625. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12626. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12627. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12628. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12629. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12630. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12631. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12632. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12633. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12634. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12635. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12636. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12637. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12638. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12639. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12640. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12641. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12642. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12643. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12644. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12645. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  12646. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  12647. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  12648. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  12649. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  12650. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  12651. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  12652. ;; All the other keys
  12653. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12654. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12655. (if (boundp 'narrow-map)
  12656. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12657. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12658. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12659. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12660. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12661. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12662. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12663. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12664. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12665. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12666. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12667. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12668. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12669. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12670. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12671. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12672. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12673. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12674. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12675. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12676. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12677. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12678. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12679. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12680. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12681. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12682. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12683. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12684. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12685. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12686. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12687. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12688. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12689. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12690. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12691. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12692. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12693. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12694. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12695. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12696. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12697. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12698. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12699. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12700. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12701. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12702. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12703. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12704. (org-defkey org-mode-map "\C-m" 'org-return)
  12705. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12706. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12707. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12708. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12709. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12710. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12711. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12712. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12713. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12714. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12715. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12716. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12717. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12718. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12719. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12720. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12721. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12722. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12723. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12724. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12725. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12726. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12727. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12728. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12729. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12730. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12731. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12732. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12733. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12734. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12735. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12736. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12737. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12738. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12739. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12740. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12741. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12742. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12743. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12744. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  12745. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12746. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12747. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12748. (when (featurep 'xemacs)
  12749. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12750. (defvar org-self-insert-command-undo-counter 0)
  12751. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12752. (defun org-self-insert-command (N)
  12753. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12754. If the cursor is in a table looking at whitespace, the whitespace is
  12755. overwritten, and the table is not marked as requiring realignment."
  12756. (interactive "p")
  12757. (if (and
  12758. (org-table-p)
  12759. (progn
  12760. ;; check if we blank the field, and if that triggers align
  12761. (and (featurep 'org-table) org-table-auto-blank-field
  12762. (member last-command
  12763. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12764. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12765. ;; got extra space, this field does not determine column width
  12766. (let (org-table-may-need-update) (org-table-blank-field))
  12767. ;; no extra space, this field may determine column width
  12768. (org-table-blank-field)))
  12769. t)
  12770. (eq N 1)
  12771. (looking-at "[^|\n]* |"))
  12772. (let (org-table-may-need-update)
  12773. (goto-char (1- (match-end 0)))
  12774. (delete-backward-char 1)
  12775. (goto-char (match-beginning 0))
  12776. (self-insert-command N))
  12777. (setq org-table-may-need-update t)
  12778. (self-insert-command N)
  12779. (org-fix-tags-on-the-fly)
  12780. (if org-self-insert-cluster-for-undo
  12781. (if (not (eq last-command 'org-self-insert-command))
  12782. (setq org-self-insert-command-undo-counter 1)
  12783. (if (>= org-self-insert-command-undo-counter 20)
  12784. (setq org-self-insert-command-undo-counter 1)
  12785. (and (> org-self-insert-command-undo-counter 0)
  12786. buffer-undo-list
  12787. (not (cadr buffer-undo-list)) ; remove nil entry
  12788. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12789. (setq org-self-insert-command-undo-counter
  12790. (1+ org-self-insert-command-undo-counter)))))))
  12791. (defun org-fix-tags-on-the-fly ()
  12792. (when (and (equal (char-after (point-at-bol)) ?*)
  12793. (org-on-heading-p))
  12794. (org-align-tags-here org-tags-column)))
  12795. (defun org-delete-backward-char (N)
  12796. "Like `delete-backward-char', insert whitespace at field end in tables.
  12797. When deleting backwards, in tables this function will insert whitespace in
  12798. front of the next \"|\" separator, to keep the table aligned. The table will
  12799. still be marked for re-alignment if the field did fill the entire column,
  12800. because, in this case the deletion might narrow the column."
  12801. (interactive "p")
  12802. (if (and (org-table-p)
  12803. (eq N 1)
  12804. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12805. (looking-at ".*?|"))
  12806. (let ((pos (point))
  12807. (noalign (looking-at "[^|\n\r]* |"))
  12808. (c org-table-may-need-update))
  12809. (backward-delete-char N)
  12810. (skip-chars-forward "^|")
  12811. (insert " ")
  12812. (goto-char (1- pos))
  12813. ;; noalign: if there were two spaces at the end, this field
  12814. ;; does not determine the width of the column.
  12815. (if noalign (setq org-table-may-need-update c)))
  12816. (backward-delete-char N)
  12817. (org-fix-tags-on-the-fly)))
  12818. (defun org-delete-char (N)
  12819. "Like `delete-char', but insert whitespace at field end in tables.
  12820. When deleting characters, in tables this function will insert whitespace in
  12821. front of the next \"|\" separator, to keep the table aligned. The table will
  12822. still be marked for re-alignment if the field did fill the entire column,
  12823. because, in this case the deletion might narrow the column."
  12824. (interactive "p")
  12825. (if (and (org-table-p)
  12826. (not (bolp))
  12827. (not (= (char-after) ?|))
  12828. (eq N 1))
  12829. (if (looking-at ".*?|")
  12830. (let ((pos (point))
  12831. (noalign (looking-at "[^|\n\r]* |"))
  12832. (c org-table-may-need-update))
  12833. (replace-match (concat
  12834. (substring (match-string 0) 1 -1)
  12835. " |"))
  12836. (goto-char pos)
  12837. ;; noalign: if there were two spaces at the end, this field
  12838. ;; does not determine the width of the column.
  12839. (if noalign (setq org-table-may-need-update c)))
  12840. (delete-char N))
  12841. (delete-char N)
  12842. (org-fix-tags-on-the-fly)))
  12843. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12844. (put 'org-self-insert-command 'delete-selection t)
  12845. (put 'orgtbl-self-insert-command 'delete-selection t)
  12846. (put 'org-delete-char 'delete-selection 'supersede)
  12847. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12848. (put 'org-yank 'delete-selection 'yank)
  12849. ;; Make `flyspell-mode' delay after some commands
  12850. (put 'org-self-insert-command 'flyspell-delayed t)
  12851. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12852. (put 'org-delete-char 'flyspell-delayed t)
  12853. (put 'org-delete-backward-char 'flyspell-delayed t)
  12854. ;; Make pabbrev-mode expand after org-mode commands
  12855. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12856. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12857. ;; How to do this: Measure non-white length of current string
  12858. ;; If equal to column width, we should realign.
  12859. (defun org-remap (map &rest commands)
  12860. "In MAP, remap the functions given in COMMANDS.
  12861. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12862. (let (new old)
  12863. (while commands
  12864. (setq old (pop commands) new (pop commands))
  12865. (if (fboundp 'command-remapping)
  12866. (org-defkey map (vector 'remap old) new)
  12867. (substitute-key-definition old new map global-map)))))
  12868. (when (eq org-enable-table-editor 'optimized)
  12869. ;; If the user wants maximum table support, we need to hijack
  12870. ;; some standard editing functions
  12871. (org-remap org-mode-map
  12872. 'self-insert-command 'org-self-insert-command
  12873. 'delete-char 'org-delete-char
  12874. 'delete-backward-char 'org-delete-backward-char)
  12875. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12876. (defvar org-ctrl-c-ctrl-c-hook nil
  12877. "Hook for functions attaching themselves to `C-c C-c'.
  12878. This can be used to add additional functionality to the C-c C-c key which
  12879. executes context-dependent commands.
  12880. Each function will be called with no arguments. The function must check
  12881. if the context is appropriate for it to act. If yes, it should do its
  12882. thing and then return a non-nil value. If the context is wrong,
  12883. just do nothing and return nil.")
  12884. (defvar org-tab-first-hook nil
  12885. "Hook for functions to attach themselves to TAB.
  12886. See `org-ctrl-c-ctrl-c-hook' for more information.
  12887. This hook runs as the first action when TAB is pressed, even before
  12888. `org-cycle' messes around with the `outline-regexp' to cater for
  12889. inline tasks and plain list item folding.
  12890. If any function in this hook returns t, not other actions like table
  12891. field motion visibility cycling will be done.")
  12892. (defvar org-tab-after-check-for-table-hook nil
  12893. "Hook for functions to attach themselves to TAB.
  12894. See `org-ctrl-c-ctrl-c-hook' for more information.
  12895. This hook runs after it has been established that the cursor is not in a
  12896. table, but before checking if the cursor is in a headline or if global cycling
  12897. should be done.
  12898. If any function in this hook returns t, not other actions like visibility
  12899. cycling will be done.")
  12900. (defvar org-tab-after-check-for-cycling-hook nil
  12901. "Hook for functions to attach themselves to TAB.
  12902. See `org-ctrl-c-ctrl-c-hook' for more information.
  12903. This hook runs after it has been established that not table field motion and
  12904. not visibility should be done because of current context. This is probably
  12905. the place where a package like yasnippets can hook in.")
  12906. (defvar org-metaleft-hook nil
  12907. "Hook for functions attaching themselves to `M-left'.
  12908. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12909. (defvar org-metaright-hook nil
  12910. "Hook for functions attaching themselves to `M-right'.
  12911. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12912. (defvar org-metaup-hook nil
  12913. "Hook for functions attaching themselves to `M-up'.
  12914. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12915. (defvar org-metadown-hook nil
  12916. "Hook for functions attaching themselves to `M-down'.
  12917. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12918. (defvar org-shiftmetaleft-hook nil
  12919. "Hook for functions attaching themselves to `M-S-left'.
  12920. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12921. (defvar org-shiftmetaright-hook nil
  12922. "Hook for functions attaching themselves to `M-S-right'.
  12923. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12924. (defvar org-shiftmetaup-hook nil
  12925. "Hook for functions attaching themselves to `M-S-up'.
  12926. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12927. (defvar org-shiftmetadown-hook nil
  12928. "Hook for functions attaching themselves to `M-S-down'.
  12929. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12930. (defvar org-metareturn-hook nil
  12931. "Hook for functions attaching themselves to `M-RET'.
  12932. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12933. (defun org-modifier-cursor-error ()
  12934. "Throw an error, a modified cursor command was applied in wrong context."
  12935. (error "This command is active in special context like tables, headlines or items"))
  12936. (defun org-shiftselect-error ()
  12937. "Throw an error because Shift-Cursor command was applied in wrong context."
  12938. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12939. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12940. (error "This command works only in special context like headlines or timestamps.")))
  12941. (defun org-call-for-shift-select (cmd)
  12942. (let ((this-command-keys-shift-translated t))
  12943. (call-interactively cmd)))
  12944. (defun org-shifttab (&optional arg)
  12945. "Global visibility cycling or move to previous table field.
  12946. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12947. on context.
  12948. See the individual commands for more information."
  12949. (interactive "P")
  12950. (cond
  12951. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12952. ((integerp arg)
  12953. (message "Content view to level: %d" arg)
  12954. (org-content (prefix-numeric-value arg))
  12955. (setq org-cycle-global-status 'overview))
  12956. (t (call-interactively 'org-global-cycle))))
  12957. (defun org-shiftmetaleft ()
  12958. "Promote subtree or delete table column.
  12959. Calls `org-promote-subtree', `org-outdent-item',
  12960. or `org-table-delete-column', depending on context.
  12961. See the individual commands for more information."
  12962. (interactive)
  12963. (cond
  12964. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12965. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12966. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12967. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12968. (t (org-modifier-cursor-error))))
  12969. (defun org-shiftmetaright ()
  12970. "Demote subtree or insert table column.
  12971. Calls `org-demote-subtree', `org-indent-item',
  12972. or `org-table-insert-column', depending on context.
  12973. See the individual commands for more information."
  12974. (interactive)
  12975. (cond
  12976. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12977. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12978. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12979. ((org-at-item-p) (call-interactively 'org-indent-item))
  12980. (t (org-modifier-cursor-error))))
  12981. (defun org-shiftmetaup (&optional arg)
  12982. "Move subtree up or kill table row.
  12983. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12984. `org-move-item-up' depending on context. See the individual commands
  12985. for more information."
  12986. (interactive "P")
  12987. (cond
  12988. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12989. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12990. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12991. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12992. (t (org-modifier-cursor-error))))
  12993. (defun org-shiftmetadown (&optional arg)
  12994. "Move subtree down or insert table row.
  12995. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12996. `org-move-item-down', depending on context. See the individual
  12997. commands for more information."
  12998. (interactive "P")
  12999. (cond
  13000. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13001. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13002. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13003. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13004. (t (org-modifier-cursor-error))))
  13005. (defun org-metaleft (&optional arg)
  13006. "Promote heading or move table column to left.
  13007. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13008. With no specific context, calls the Emacs default `backward-word'.
  13009. See the individual commands for more information."
  13010. (interactive "P")
  13011. (cond
  13012. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13013. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13014. ((or (org-on-heading-p)
  13015. (and (org-region-active-p)
  13016. (save-excursion
  13017. (goto-char (region-beginning))
  13018. (org-on-heading-p))))
  13019. (call-interactively 'org-do-promote))
  13020. ((or (org-at-item-p)
  13021. (and (org-region-active-p)
  13022. (save-excursion
  13023. (goto-char (region-beginning))
  13024. (org-at-item-p))))
  13025. (call-interactively 'org-outdent-item))
  13026. (t (call-interactively 'backward-word))))
  13027. (defun org-metaright (&optional arg)
  13028. "Demote subtree or move table column to right.
  13029. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13030. With no specific context, calls the Emacs default `forward-word'.
  13031. See the individual commands for more information."
  13032. (interactive "P")
  13033. (cond
  13034. ((run-hook-with-args-until-success 'org-metaright-hook))
  13035. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13036. ((or (org-on-heading-p)
  13037. (and (org-region-active-p)
  13038. (save-excursion
  13039. (goto-char (region-beginning))
  13040. (org-on-heading-p))))
  13041. (call-interactively 'org-do-demote))
  13042. ((or (org-at-item-p)
  13043. (and (org-region-active-p)
  13044. (save-excursion
  13045. (goto-char (region-beginning))
  13046. (org-at-item-p))))
  13047. (call-interactively 'org-indent-item))
  13048. (t (call-interactively 'forward-word))))
  13049. (defun org-metaup (&optional arg)
  13050. "Move subtree up or move table row up.
  13051. Calls `org-move-subtree-up' or `org-table-move-row' or
  13052. `org-move-item-up', depending on context. See the individual commands
  13053. for more information."
  13054. (interactive "P")
  13055. (cond
  13056. ((run-hook-with-args-until-success 'org-metaup-hook))
  13057. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13058. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13059. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13060. (t (transpose-lines 1) (beginning-of-line -1))))
  13061. (defun org-metadown (&optional arg)
  13062. "Move subtree down or move table row down.
  13063. Calls `org-move-subtree-down' or `org-table-move-row' or
  13064. `org-move-item-down', depending on context. See the individual
  13065. commands for more information."
  13066. (interactive "P")
  13067. (cond
  13068. ((run-hook-with-args-until-success 'org-metadown-hook))
  13069. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13070. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13071. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13072. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13073. (defun org-shiftup (&optional arg)
  13074. "Increase item in timestamp or increase priority of current headline.
  13075. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13076. depending on context. See the individual commands for more information."
  13077. (interactive "P")
  13078. (cond
  13079. ((and org-support-shift-select (org-region-active-p))
  13080. (org-call-for-shift-select 'previous-line))
  13081. ((org-at-timestamp-p t)
  13082. (call-interactively (if org-edit-timestamp-down-means-later
  13083. 'org-timestamp-down 'org-timestamp-up)))
  13084. ((and (not (eq org-support-shift-select 'always))
  13085. org-enable-priority-commands
  13086. (org-on-heading-p))
  13087. (call-interactively 'org-priority-up))
  13088. ((and (not org-support-shift-select) (org-at-item-p))
  13089. (call-interactively 'org-previous-item))
  13090. ((org-clocktable-try-shift 'up arg))
  13091. (org-support-shift-select
  13092. (org-call-for-shift-select 'previous-line))
  13093. (t (org-shiftselect-error))))
  13094. (defun org-shiftdown (&optional arg)
  13095. "Decrease item in timestamp or decrease priority of current headline.
  13096. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13097. depending on context. See the individual commands for more information."
  13098. (interactive "P")
  13099. (cond
  13100. ((and org-support-shift-select (org-region-active-p))
  13101. (org-call-for-shift-select 'next-line))
  13102. ((org-at-timestamp-p t)
  13103. (call-interactively (if org-edit-timestamp-down-means-later
  13104. 'org-timestamp-up 'org-timestamp-down)))
  13105. ((and (not (eq org-support-shift-select 'always))
  13106. org-enable-priority-commands
  13107. (org-on-heading-p))
  13108. (call-interactively 'org-priority-down))
  13109. ((and (not org-support-shift-select) (org-at-item-p))
  13110. (call-interactively 'org-next-item))
  13111. ((org-clocktable-try-shift 'down arg))
  13112. (org-support-shift-select
  13113. (org-call-for-shift-select 'next-line))
  13114. (t (org-shiftselect-error))))
  13115. (defun org-shiftright (&optional arg)
  13116. "Cycle the thing at point or in the current line, depending on context.
  13117. Depending on context, this does one of the following:
  13118. - switch a timestamp at point one day into the future
  13119. - on a headline, switch to the next TODO keyword.
  13120. - on an item, switch entire list to the next bullet type
  13121. - on a property line, switch to the next allowed value
  13122. - on a clocktable definition line, move time block into the future"
  13123. (interactive "P")
  13124. (cond
  13125. ((and org-support-shift-select (org-region-active-p))
  13126. (org-call-for-shift-select 'forward-char))
  13127. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13128. ((and (not (eq org-support-shift-select 'always))
  13129. (org-on-heading-p))
  13130. (let ((org-inhibit-logging
  13131. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13132. (org-call-with-arg 'org-todo 'right)))
  13133. ((or (and org-support-shift-select
  13134. (not (eq org-support-shift-select 'always))
  13135. (org-at-item-bullet-p))
  13136. (and (not org-support-shift-select) (org-at-item-p)))
  13137. (org-call-with-arg 'org-cycle-list-bullet nil))
  13138. ((and (not (eq org-support-shift-select 'always))
  13139. (org-at-property-p))
  13140. (call-interactively 'org-property-next-allowed-value))
  13141. ((org-clocktable-try-shift 'right arg))
  13142. (org-support-shift-select
  13143. (org-call-for-shift-select 'forward-char))
  13144. (t (org-shiftselect-error))))
  13145. (defun org-shiftleft (&optional arg)
  13146. "Cycle the thing at point or in the current line, depending on context.
  13147. Depending on context, this does one of the following:
  13148. - switch a timestamp at point one day into the past
  13149. - on a headline, switch to the previous TODO keyword.
  13150. - on an item, switch entire list to the previous bullet type
  13151. - on a property line, switch to the previous allowed value
  13152. - on a clocktable definition line, move time block into the past"
  13153. (interactive "P")
  13154. (cond
  13155. ((and org-support-shift-select (org-region-active-p))
  13156. (org-call-for-shift-select 'backward-char))
  13157. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13158. ((and (not (eq org-support-shift-select 'always))
  13159. (org-on-heading-p))
  13160. (let ((org-inhibit-logging
  13161. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13162. (org-call-with-arg 'org-todo 'left)))
  13163. ((or (and org-support-shift-select
  13164. (not (eq org-support-shift-select 'always))
  13165. (org-at-item-bullet-p))
  13166. (and (not org-support-shift-select) (org-at-item-p)))
  13167. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13168. ((and (not (eq org-support-shift-select 'always))
  13169. (org-at-property-p))
  13170. (call-interactively 'org-property-previous-allowed-value))
  13171. ((org-clocktable-try-shift 'left arg))
  13172. (org-support-shift-select
  13173. (org-call-for-shift-select 'backward-char))
  13174. (t (org-shiftselect-error))))
  13175. (defun org-shiftcontrolright ()
  13176. "Switch to next TODO set."
  13177. (interactive)
  13178. (cond
  13179. ((and org-support-shift-select (org-region-active-p))
  13180. (org-call-for-shift-select 'forward-word))
  13181. ((and (not (eq org-support-shift-select 'always))
  13182. (org-on-heading-p))
  13183. (org-call-with-arg 'org-todo 'nextset))
  13184. (org-support-shift-select
  13185. (org-call-for-shift-select 'forward-word))
  13186. (t (org-shiftselect-error))))
  13187. (defun org-shiftcontrolleft ()
  13188. "Switch to previous TODO set."
  13189. (interactive)
  13190. (cond
  13191. ((and org-support-shift-select (org-region-active-p))
  13192. (org-call-for-shift-select 'backward-word))
  13193. ((and (not (eq org-support-shift-select 'always))
  13194. (org-on-heading-p))
  13195. (org-call-with-arg 'org-todo 'previousset))
  13196. (org-support-shift-select
  13197. (org-call-for-shift-select 'backward-word))
  13198. (t (org-shiftselect-error))))
  13199. (defun org-ctrl-c-ret ()
  13200. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13201. (interactive)
  13202. (cond
  13203. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13204. (t (call-interactively 'org-insert-heading))))
  13205. (defun org-copy-special ()
  13206. "Copy region in table or copy current subtree.
  13207. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13208. See the individual commands for more information."
  13209. (interactive)
  13210. (call-interactively
  13211. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13212. (defun org-cut-special ()
  13213. "Cut region in table or cut current subtree.
  13214. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13215. See the individual commands for more information."
  13216. (interactive)
  13217. (call-interactively
  13218. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13219. (defun org-paste-special (arg)
  13220. "Paste rectangular region into table, or past subtree relative to level.
  13221. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13222. See the individual commands for more information."
  13223. (interactive "P")
  13224. (if (org-at-table-p)
  13225. (org-table-paste-rectangle)
  13226. (org-paste-subtree arg)))
  13227. (defun org-edit-special ()
  13228. "Call a special editor for the stuff at point.
  13229. When at a table, call the formula editor with `org-table-edit-formulas'.
  13230. When at the first line of an src example, call `org-edit-src-code'.
  13231. When in an #+include line, visit the include file. Otherwise call
  13232. `ffap' to visit the file at point."
  13233. (interactive)
  13234. (cond
  13235. ((org-at-table-p)
  13236. (call-interactively 'org-table-edit-formulas))
  13237. ((save-excursion
  13238. (beginning-of-line 1)
  13239. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13240. (find-file (org-trim (match-string 1))))
  13241. ((org-edit-src-code))
  13242. ((org-edit-fixed-width-region))
  13243. (t (call-interactively 'ffap))))
  13244. (defun org-ctrl-c-ctrl-c (&optional arg)
  13245. "Set tags in headline, or update according to changed information at point.
  13246. This command does many different things, depending on context:
  13247. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13248. this is what we do.
  13249. - If the cursor is in a headline, prompt for tags and insert them
  13250. into the current line, aligned to `org-tags-column'. When called
  13251. with prefix arg, realign all tags in the current buffer.
  13252. - If the cursor is in one of the special #+KEYWORD lines, this
  13253. triggers scanning the buffer for these lines and updating the
  13254. information.
  13255. - If the cursor is inside a table, realign the table. This command
  13256. works even if the automatic table editor has been turned off.
  13257. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13258. the entire table.
  13259. - If the cursor is at a footnote reference or definition, jump to
  13260. the corresponding definition or references, respectively.
  13261. - If the cursor is a the beginning of a dynamic block, update it.
  13262. - If the cursor is inside a table created by the table.el package,
  13263. activate that table.
  13264. - If the current buffer is a remember buffer, close note and file
  13265. it. A prefix argument of 1 files to the default location
  13266. without further interaction. A prefix argument of 2 files to
  13267. the currently clocking task.
  13268. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13269. links in this buffer.
  13270. - If the cursor is on a numbered item in a plain list, renumber the
  13271. ordered list.
  13272. - If the cursor is on a checkbox, toggle it."
  13273. (interactive "P")
  13274. (let ((org-enable-table-editor t))
  13275. (cond
  13276. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13277. org-occur-highlights
  13278. org-latex-fragment-image-overlays)
  13279. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13280. (org-remove-occur-highlights)
  13281. (org-remove-latex-fragment-image-overlays)
  13282. (message "Temporary highlights/overlays removed from current buffer"))
  13283. ((and (local-variable-p 'org-finish-function (current-buffer))
  13284. (fboundp org-finish-function))
  13285. (funcall org-finish-function))
  13286. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13287. ((org-at-property-p)
  13288. (call-interactively 'org-property-action))
  13289. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13290. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13291. ((org-at-table.el-p)
  13292. (require 'table)
  13293. (beginning-of-line 1)
  13294. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13295. (call-interactively 'table-recognize-table))
  13296. ((org-at-table-p)
  13297. (org-table-maybe-eval-formula)
  13298. (if arg
  13299. (call-interactively 'org-table-recalculate)
  13300. (org-table-maybe-recalculate-line))
  13301. (call-interactively 'org-table-align))
  13302. ((or (org-footnote-at-reference-p)
  13303. (org-footnote-at-definition-p))
  13304. (call-interactively 'org-footnote-action))
  13305. ((org-at-item-checkbox-p)
  13306. (call-interactively 'org-toggle-checkbox))
  13307. ((org-at-item-p)
  13308. (if arg
  13309. (call-interactively 'org-toggle-checkbox)
  13310. (call-interactively 'org-maybe-renumber-ordered-list)))
  13311. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13312. ;; Dynamic block
  13313. (beginning-of-line 1)
  13314. (save-excursion (org-update-dblock)))
  13315. ((save-excursion
  13316. (beginning-of-line 1)
  13317. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13318. (cond
  13319. ((equal (match-string 1) "TBLFM")
  13320. ;; Recalculate the table before this line
  13321. (save-excursion
  13322. (beginning-of-line 1)
  13323. (skip-chars-backward " \r\n\t")
  13324. (if (org-at-table-p)
  13325. (org-call-with-arg 'org-table-recalculate t))))
  13326. (t
  13327. ; (org-set-regexps-and-options)
  13328. ; (org-restart-font-lock)
  13329. (let ((org-inhibit-startup t)) (org-mode-restart))
  13330. (message "Local setup has been refreshed"))))
  13331. ((org-clock-update-time-maybe))
  13332. (t (error "C-c C-c can do nothing useful at this location.")))))
  13333. (defun org-mode-restart ()
  13334. "Restart Org-mode, to scan again for special lines.
  13335. Also updates the keyword regular expressions."
  13336. (interactive)
  13337. (org-mode)
  13338. (message "Org-mode restarted"))
  13339. (defun org-kill-note-or-show-branches ()
  13340. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13341. (interactive)
  13342. (if (not org-finish-function)
  13343. (call-interactively 'show-branches)
  13344. (let ((org-note-abort t))
  13345. (funcall org-finish-function))))
  13346. (defun org-return (&optional indent)
  13347. "Goto next table row or insert a newline.
  13348. Calls `org-table-next-row' or `newline', depending on context.
  13349. See the individual commands for more information."
  13350. (interactive)
  13351. (cond
  13352. ((bobp) (if indent (newline-and-indent) (newline)))
  13353. ((org-at-table-p)
  13354. (org-table-justify-field-maybe)
  13355. (call-interactively 'org-table-next-row))
  13356. ((and org-return-follows-link
  13357. (eq (get-text-property (point) 'face) 'org-link))
  13358. (call-interactively 'org-open-at-point))
  13359. ((and (org-at-heading-p)
  13360. (looking-at
  13361. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13362. (org-show-entry)
  13363. (end-of-line 1)
  13364. (newline))
  13365. (t (if indent (newline-and-indent) (newline)))))
  13366. (defun org-return-indent ()
  13367. "Goto next table row or insert a newline and indent.
  13368. Calls `org-table-next-row' or `newline-and-indent', depending on
  13369. context. See the individual commands for more information."
  13370. (interactive)
  13371. (org-return t))
  13372. (defun org-ctrl-c-star ()
  13373. "Compute table, or change heading status of lines.
  13374. Calls `org-table-recalculate' or `org-toggle-heading',
  13375. depending on context."
  13376. (interactive)
  13377. (cond
  13378. ((org-at-table-p)
  13379. (call-interactively 'org-table-recalculate))
  13380. (t
  13381. ;; Convert all lines in region to list items
  13382. (call-interactively 'org-toggle-heading))))
  13383. (defun org-ctrl-c-minus ()
  13384. "Insert separator line in table or modify bullet status of line.
  13385. Also turns a plain line or a region of lines into list items.
  13386. Calls `org-table-insert-hline', `org-toggle-item', or
  13387. `org-cycle-list-bullet', depending on context."
  13388. (interactive)
  13389. (cond
  13390. ((org-at-table-p)
  13391. (call-interactively 'org-table-insert-hline))
  13392. ((org-region-active-p)
  13393. (call-interactively 'org-toggle-item))
  13394. ((org-in-item-p)
  13395. (call-interactively 'org-cycle-list-bullet))
  13396. (t
  13397. (call-interactively 'org-toggle-item))))
  13398. (defun org-toggle-item ()
  13399. "Convert headings or normal lines to items, items to normal lines.
  13400. If there is no active region, only the current line is considered.
  13401. If the first line in the region is a headline, convert all headlines to items.
  13402. If the first line in the region is an item, convert all items to normal lines.
  13403. If the first line is normal text, add an item bullet to each line."
  13404. (interactive)
  13405. (let (l2 l beg end)
  13406. (if (org-region-active-p)
  13407. (setq beg (region-beginning) end (region-end))
  13408. (setq beg (point-at-bol)
  13409. end (min (1+ (point-at-eol)) (point-max))))
  13410. (save-excursion
  13411. (goto-char end)
  13412. (setq l2 (org-current-line))
  13413. (goto-char beg)
  13414. (beginning-of-line 1)
  13415. (setq l (1- (org-current-line)))
  13416. (if (org-at-item-p)
  13417. ;; We already have items, de-itemize
  13418. (while (< (setq l (1+ l)) l2)
  13419. (when (org-at-item-p)
  13420. (goto-char (match-beginning 2))
  13421. (delete-region (match-beginning 2) (match-end 2))
  13422. (and (looking-at "[ \t]+") (replace-match "")))
  13423. (beginning-of-line 2))
  13424. (if (org-on-heading-p)
  13425. ;; Headings, convert to items
  13426. (while (< (setq l (1+ l)) l2)
  13427. (if (looking-at org-outline-regexp)
  13428. (replace-match "- " t t))
  13429. (beginning-of-line 2))
  13430. ;; normal lines, turn them into items
  13431. (while (< (setq l (1+ l)) l2)
  13432. (unless (org-at-item-p)
  13433. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13434. (replace-match "\\1- \\2")))
  13435. (beginning-of-line 2)))))))
  13436. (defun org-toggle-heading (&optional nstars)
  13437. "Convert headings to normal text, or items or text to headings.
  13438. If there is no active region, only the current line is considered.
  13439. If the first line is a heading, remove the stars from all headlines
  13440. in the region.
  13441. If the first line is a plain list item, turn all plain list items into
  13442. headings.
  13443. If the first line is a normal line, turn each and every line in the region
  13444. into a heading.
  13445. When converting a line into a heading, the number of stars is chosen
  13446. such that the lines become children of the current entry. However, when
  13447. a prefix argument is given, its value determines the number of stars to add."
  13448. (interactive "P")
  13449. (let (l2 l itemp beg end)
  13450. (if (org-region-active-p)
  13451. (setq beg (region-beginning) end (region-end))
  13452. (setq beg (point-at-bol)
  13453. end (min (1+ (point-at-eol)) (point-max))))
  13454. (save-excursion
  13455. (goto-char end)
  13456. (setq l2 (org-current-line))
  13457. (goto-char beg)
  13458. (beginning-of-line 1)
  13459. (setq l (1- (org-current-line)))
  13460. (if (org-on-heading-p)
  13461. ;; We already have headlines, de-star them
  13462. (while (< (setq l (1+ l)) l2)
  13463. (when (org-on-heading-p t)
  13464. (and (looking-at outline-regexp) (replace-match "")))
  13465. (beginning-of-line 2))
  13466. (setq itemp (org-at-item-p))
  13467. (let* ((stars
  13468. (if nstars
  13469. (make-string (prefix-numeric-value current-prefix-arg)
  13470. ?*)
  13471. (save-excursion
  13472. (re-search-backward org-complex-heading-regexp nil t)
  13473. (or (match-string 1) ""))))
  13474. (add-stars (cond (nstars "")
  13475. ((equal stars "") "*")
  13476. (org-odd-levels-only "**")
  13477. (t "*")))
  13478. (rpl (concat stars add-stars " ")))
  13479. (while (< (setq l (1+ l)) l2)
  13480. (if itemp
  13481. (and (org-at-item-p) (replace-match rpl t t))
  13482. (unless (org-on-heading-p)
  13483. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13484. (replace-match (concat rpl (match-string 2))))))
  13485. (beginning-of-line 2)))))))
  13486. (defun org-meta-return (&optional arg)
  13487. "Insert a new heading or wrap a region in a table.
  13488. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13489. See the individual commands for more information."
  13490. (interactive "P")
  13491. (cond
  13492. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13493. ((org-at-table-p)
  13494. (call-interactively 'org-table-wrap-region))
  13495. (t (call-interactively 'org-insert-heading))))
  13496. ;;; Menu entries
  13497. ;; Define the Org-mode menus
  13498. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13499. '("Tbl"
  13500. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13501. ["Next Field" org-cycle (org-at-table-p)]
  13502. ["Previous Field" org-shifttab (org-at-table-p)]
  13503. ["Next Row" org-return (org-at-table-p)]
  13504. "--"
  13505. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13506. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13507. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13508. "--"
  13509. ("Column"
  13510. ["Move Column Left" org-metaleft (org-at-table-p)]
  13511. ["Move Column Right" org-metaright (org-at-table-p)]
  13512. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13513. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13514. ("Row"
  13515. ["Move Row Up" org-metaup (org-at-table-p)]
  13516. ["Move Row Down" org-metadown (org-at-table-p)]
  13517. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13518. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13519. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13520. "--"
  13521. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13522. ("Rectangle"
  13523. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13524. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13525. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13526. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13527. "--"
  13528. ("Calculate"
  13529. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13530. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13531. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13532. "--"
  13533. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13534. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13535. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13536. "--"
  13537. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13538. "--"
  13539. ["Sum Column/Rectangle" org-table-sum
  13540. (or (org-at-table-p) (org-region-active-p))]
  13541. ["Which Column?" org-table-current-column (org-at-table-p)])
  13542. ["Debug Formulas"
  13543. org-table-toggle-formula-debugger
  13544. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13545. ["Show Col/Row Numbers"
  13546. org-table-toggle-coordinate-overlays
  13547. :style toggle
  13548. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13549. "--"
  13550. ["Create" org-table-create (and (not (org-at-table-p))
  13551. org-enable-table-editor)]
  13552. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13553. ["Import from File" org-table-import (not (org-at-table-p))]
  13554. ["Export to File" org-table-export (org-at-table-p)]
  13555. "--"
  13556. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13557. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13558. '("Org"
  13559. ("Show/Hide"
  13560. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13561. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13562. ["Sparse Tree..." org-sparse-tree t]
  13563. ["Reveal Context" org-reveal t]
  13564. ["Show All" show-all t]
  13565. "--"
  13566. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13567. "--"
  13568. ["New Heading" org-insert-heading t]
  13569. ("Navigate Headings"
  13570. ["Up" outline-up-heading t]
  13571. ["Next" outline-next-visible-heading t]
  13572. ["Previous" outline-previous-visible-heading t]
  13573. ["Next Same Level" outline-forward-same-level t]
  13574. ["Previous Same Level" outline-backward-same-level t]
  13575. "--"
  13576. ["Jump" org-goto t])
  13577. ("Edit Structure"
  13578. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13579. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13580. "--"
  13581. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13582. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13583. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13584. "--"
  13585. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13586. "--"
  13587. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13588. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13589. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13590. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13591. "--"
  13592. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13593. "--"
  13594. ["Convert to odd levels" org-convert-to-odd-levels t]
  13595. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13596. ("Editing"
  13597. ["Emphasis..." org-emphasize t]
  13598. ["Edit Source Example" org-edit-special t]
  13599. "--"
  13600. ["Footnote new/jump" org-footnote-action t]
  13601. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13602. ("Archive"
  13603. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13604. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13605. ; :active t :keys "C-u C-c C-x C-a"]
  13606. ["Sparse trees open ARCHIVE trees"
  13607. (setq org-sparse-tree-open-archived-trees
  13608. (not org-sparse-tree-open-archived-trees))
  13609. :style toggle :selected org-sparse-tree-open-archived-trees]
  13610. ["Cycling opens ARCHIVE trees"
  13611. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13612. :style toggle :selected org-cycle-open-archived-trees]
  13613. "--"
  13614. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13615. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13616. ; ["Check and Move Children" (org-archive-subtree '(4))
  13617. ; :active t :keys "C-u C-c C-x C-s"]
  13618. )
  13619. "--"
  13620. ("Hyperlinks"
  13621. ["Store Link (Global)" org-store-link t]
  13622. ["Insert Link" org-insert-link t]
  13623. ["Follow Link" org-open-at-point t]
  13624. "--"
  13625. ["Next link" org-next-link t]
  13626. ["Previous link" org-previous-link t]
  13627. "--"
  13628. ["Descriptive Links"
  13629. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13630. :style radio
  13631. :selected (member '(org-link) buffer-invisibility-spec)]
  13632. ["Literal Links"
  13633. (progn
  13634. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13635. :style radio
  13636. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13637. "--"
  13638. ("TODO Lists"
  13639. ["TODO/DONE/-" org-todo t]
  13640. ("Select keyword"
  13641. ["Next keyword" org-shiftright (org-on-heading-p)]
  13642. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13643. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13644. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13645. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13646. ["Show TODO Tree" org-show-todo-tree t]
  13647. ["Global TODO list" org-todo-list t]
  13648. "--"
  13649. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13650. :selected org-enforce-todo-dependencies :style toggle :active t]
  13651. "Settings for tree at point"
  13652. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13653. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13654. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13655. ["Do Children parallel" org-toggle-ordered-property :style radio
  13656. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13657. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13658. "--"
  13659. ["Set Priority" org-priority t]
  13660. ["Priority Up" org-shiftup t]
  13661. ["Priority Down" org-shiftdown t]
  13662. "--"
  13663. ["Get news from all feeds" org-feed-update-all t]
  13664. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13665. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13666. ("TAGS and Properties"
  13667. ["Set Tags" org-set-tags-command t]
  13668. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13669. "--"
  13670. ["Set property" org-set-property t]
  13671. ["Column view of properties" org-columns t]
  13672. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13673. ("Dates and Scheduling"
  13674. ["Timestamp" org-time-stamp t]
  13675. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13676. ("Change Date"
  13677. ["1 Day Later" org-shiftright t]
  13678. ["1 Day Earlier" org-shiftleft t]
  13679. ["1 ... Later" org-shiftup t]
  13680. ["1 ... Earlier" org-shiftdown t])
  13681. ["Compute Time Range" org-evaluate-time-range t]
  13682. ["Schedule Item" org-schedule t]
  13683. ["Deadline" org-deadline t]
  13684. "--"
  13685. ["Custom time format" org-toggle-time-stamp-overlays
  13686. :style radio :selected org-display-custom-times]
  13687. "--"
  13688. ["Goto Calendar" org-goto-calendar t]
  13689. ["Date from Calendar" org-date-from-calendar t]
  13690. "--"
  13691. ["Start/Restart Timer" org-timer-start t]
  13692. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13693. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13694. ["Insert Timer String" org-timer t]
  13695. ["Insert Timer Item" org-timer-item t])
  13696. ("Logging work"
  13697. ["Clock in" org-clock-in t]
  13698. ["Clock out" org-clock-out t]
  13699. ["Clock cancel" org-clock-cancel t]
  13700. ["Goto running clock" org-clock-goto t]
  13701. ["Display times" org-clock-display t]
  13702. ["Create clock table" org-clock-report t]
  13703. "--"
  13704. ["Record DONE time"
  13705. (progn (setq org-log-done (not org-log-done))
  13706. (message "Switching to %s will %s record a timestamp"
  13707. (car org-done-keywords)
  13708. (if org-log-done "automatically" "not")))
  13709. :style toggle :selected org-log-done])
  13710. "--"
  13711. ["Agenda Command..." org-agenda t]
  13712. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13713. ("File List for Agenda")
  13714. ("Special views current file"
  13715. ["TODO Tree" org-show-todo-tree t]
  13716. ["Check Deadlines" org-check-deadlines t]
  13717. ["Timeline" org-timeline t]
  13718. ["Tags/Property tree" org-match-sparse-tree t])
  13719. "--"
  13720. ["Export/Publish..." org-export t]
  13721. ("LaTeX"
  13722. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13723. :selected org-cdlatex-mode]
  13724. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13725. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13726. ["Modify math symbol" org-cdlatex-math-modify
  13727. (org-inside-LaTeX-fragment-p)]
  13728. ["Insert citation" org-reftex-citation t]
  13729. "--"
  13730. ["Export LaTeX fragments as images"
  13731. (if (featurep 'org-exp)
  13732. (setq org-export-with-LaTeX-fragments
  13733. (not org-export-with-LaTeX-fragments))
  13734. (require 'org-exp))
  13735. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13736. org-export-with-LaTeX-fragments)])
  13737. "--"
  13738. ("Documentation"
  13739. ["Show Version" org-version t]
  13740. ["Info Documentation" org-info t])
  13741. ("Customize"
  13742. ["Browse Org Group" org-customize t]
  13743. "--"
  13744. ["Expand This Menu" org-create-customize-menu
  13745. (fboundp 'customize-menu-create)])
  13746. "--"
  13747. ("Refresh/Reload"
  13748. ["Refresh setup current buffer" org-mode-restart t]
  13749. ["Reload Org (after update)" org-reload t]
  13750. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13751. ))
  13752. (defun org-info (&optional node)
  13753. "Read documentation for Org-mode in the info system.
  13754. With optional NODE, go directly to that node."
  13755. (interactive)
  13756. (info (format "(org)%s" (or node ""))))
  13757. (defun org-install-agenda-files-menu ()
  13758. (let ((bl (buffer-list)))
  13759. (save-excursion
  13760. (while bl
  13761. (set-buffer (pop bl))
  13762. (if (org-mode-p) (setq bl nil)))
  13763. (when (org-mode-p)
  13764. (easy-menu-change
  13765. '("Org") "File List for Agenda"
  13766. (append
  13767. (list
  13768. ["Edit File List" (org-edit-agenda-file-list) t]
  13769. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13770. ["Remove Current File from List" org-remove-file t]
  13771. ["Cycle through agenda files" org-cycle-agenda-files t]
  13772. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13773. "--")
  13774. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13775. ;;;; Documentation
  13776. ;;;###autoload
  13777. (defun org-require-autoloaded-modules ()
  13778. (interactive)
  13779. (mapc 'require
  13780. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13781. org-docbook org-exp org-html org-icalendar
  13782. org-id org-latex
  13783. org-publish org-remember org-table
  13784. org-timer org-xoxo)))
  13785. ;;;###autoload
  13786. (defun org-reload (&optional uncompiled)
  13787. "Reload all org lisp files.
  13788. With prefix arg UNCOMPILED, load the uncompiled versions."
  13789. (interactive "P")
  13790. (require 'find-func)
  13791. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13792. (dir-org (file-name-directory (org-find-library-name "org")))
  13793. (dir-org-contrib (ignore-errors
  13794. (file-name-directory
  13795. (org-find-library-name "org-contribdir"))))
  13796. (files
  13797. (append (directory-files dir-org t file-re)
  13798. (and dir-org-contrib
  13799. (directory-files dir-org-contrib t file-re))))
  13800. (remove-re (concat (if (featurep 'xemacs)
  13801. "org-colview" "org-colview-xemacs")
  13802. "\\'")))
  13803. (setq files (mapcar 'file-name-sans-extension files))
  13804. (setq files (mapcar
  13805. (lambda (x) (if (string-match remove-re x) nil x))
  13806. files))
  13807. (setq files (delq nil files))
  13808. (mapc
  13809. (lambda (f)
  13810. (when (featurep (intern (file-name-nondirectory f)))
  13811. (if (and (not uncompiled)
  13812. (file-exists-p (concat f ".elc")))
  13813. (load (concat f ".elc") nil nil t)
  13814. (load (concat f ".el") nil nil t))))
  13815. files))
  13816. (org-version))
  13817. ;;;###autoload
  13818. (defun org-customize ()
  13819. "Call the customize function with org as argument."
  13820. (interactive)
  13821. (org-load-modules-maybe)
  13822. (org-require-autoloaded-modules)
  13823. (customize-browse 'org))
  13824. (defun org-create-customize-menu ()
  13825. "Create a full customization menu for Org-mode, insert it into the menu."
  13826. (interactive)
  13827. (org-load-modules-maybe)
  13828. (org-require-autoloaded-modules)
  13829. (if (fboundp 'customize-menu-create)
  13830. (progn
  13831. (easy-menu-change
  13832. '("Org") "Customize"
  13833. `(["Browse Org group" org-customize t]
  13834. "--"
  13835. ,(customize-menu-create 'org)
  13836. ["Set" Custom-set t]
  13837. ["Save" Custom-save t]
  13838. ["Reset to Current" Custom-reset-current t]
  13839. ["Reset to Saved" Custom-reset-saved t]
  13840. ["Reset to Standard Settings" Custom-reset-standard t]))
  13841. (message "\"Org\"-menu now contains full customization menu"))
  13842. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13843. ;;;; Miscellaneous stuff
  13844. ;;; Generally useful functions
  13845. (defun org-find-text-property-in-string (prop s)
  13846. "Return the first non-nil value of property PROP in string S."
  13847. (or (get-text-property 0 prop s)
  13848. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13849. prop s)))
  13850. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13851. "Display the given MESSAGE as a warning."
  13852. (if (fboundp 'display-warning)
  13853. (display-warning 'org message
  13854. (if (featurep 'xemacs)
  13855. 'warning
  13856. :warning))
  13857. (let ((buf (get-buffer-create "*Org warnings*")))
  13858. (with-current-buffer buf
  13859. (goto-char (point-max))
  13860. (insert "Warning (Org): " message)
  13861. (unless (bolp)
  13862. (newline)))
  13863. (display-buffer buf)
  13864. (sit-for 0))))
  13865. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13866. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13867. (if (and marker (marker-buffer marker)
  13868. (buffer-live-p (marker-buffer marker)))
  13869. (progn
  13870. (switch-to-buffer (marker-buffer marker))
  13871. (if (or (> marker (point-max)) (< marker (point-min)))
  13872. (widen))
  13873. (goto-char marker)
  13874. (org-show-context 'org-goto))
  13875. (if bookmark
  13876. (bookmark-jump bookmark)
  13877. (error "Cannot find location"))))
  13878. (defun org-quote-csv-field (s)
  13879. "Quote field for inclusion in CSV material."
  13880. (if (string-match "[\",]" s)
  13881. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13882. s))
  13883. (defun org-plist-delete (plist property)
  13884. "Delete PROPERTY from PLIST.
  13885. This is in contrast to merely setting it to 0."
  13886. (let (p)
  13887. (while plist
  13888. (if (not (eq property (car plist)))
  13889. (setq p (plist-put p (car plist) (nth 1 plist))))
  13890. (setq plist (cddr plist)))
  13891. p))
  13892. (defun org-force-self-insert (N)
  13893. "Needed to enforce self-insert under remapping."
  13894. (interactive "p")
  13895. (self-insert-command N))
  13896. (defun org-string-width (s)
  13897. "Compute width of string, ignoring invisible characters.
  13898. This ignores character with invisibility property `org-link', and also
  13899. characters with property `org-cwidth', because these will become invisible
  13900. upon the next fontification round."
  13901. (let (b l)
  13902. (when (or (eq t buffer-invisibility-spec)
  13903. (assq 'org-link buffer-invisibility-spec))
  13904. (while (setq b (text-property-any 0 (length s)
  13905. 'invisible 'org-link s))
  13906. (setq s (concat (substring s 0 b)
  13907. (substring s (or (next-single-property-change
  13908. b 'invisible s) (length s)))))))
  13909. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13910. (setq s (concat (substring s 0 b)
  13911. (substring s (or (next-single-property-change
  13912. b 'org-cwidth s) (length s))))))
  13913. (setq l (string-width s) b -1)
  13914. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13915. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13916. l))
  13917. (defun org-get-indentation (&optional line)
  13918. "Get the indentation of the current line, interpreting tabs.
  13919. When LINE is given, assume it represents a line and compute its indentation."
  13920. (if line
  13921. (if (string-match "^ *" (org-remove-tabs line))
  13922. (match-end 0))
  13923. (save-excursion
  13924. (beginning-of-line 1)
  13925. (skip-chars-forward " \t")
  13926. (current-column))))
  13927. (defun org-remove-tabs (s &optional width)
  13928. "Replace tabulators in S with spaces.
  13929. Assumes that s is a single line, starting in column 0."
  13930. (setq width (or width tab-width))
  13931. (while (string-match "\t" s)
  13932. (setq s (replace-match
  13933. (make-string
  13934. (- (* width (/ (+ (match-beginning 0) width) width))
  13935. (match-beginning 0)) ?\ )
  13936. t t s)))
  13937. s)
  13938. (defun org-fix-indentation (line ind)
  13939. "Fix indentation in LINE.
  13940. IND is a cons cell with target and minimum indentation.
  13941. If the current indentation in LINE is smaller than the minimum,
  13942. leave it alone. If it is larger than ind, set it to the target."
  13943. (let* ((l (org-remove-tabs line))
  13944. (i (org-get-indentation l))
  13945. (i1 (car ind)) (i2 (cdr ind)))
  13946. (if (>= i i2) (setq l (substring line i2)))
  13947. (if (> i1 0)
  13948. (concat (make-string i1 ?\ ) l)
  13949. l)))
  13950. (defun org-remove-indentation (code &optional n)
  13951. "Remove the maximum common indentation from the lines in CODE.
  13952. N may optionally be the number of spaces to remove."
  13953. (with-temp-buffer
  13954. (insert code)
  13955. (org-do-remove-indentation n)
  13956. (buffer-string)))
  13957. (defun org-do-remove-indentation (&optional n)
  13958. "Remove the maximum common indentation from the buffer."
  13959. (untabify (point-min) (point-max))
  13960. (let ((min 10000) re)
  13961. (if n
  13962. (setq min n)
  13963. (goto-char (point-min))
  13964. (while (re-search-forward "^ *[^ \n]" nil t)
  13965. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  13966. (unless (or (= min 0) (= min 10000))
  13967. (setq re (format "^ \\{%d\\}" min))
  13968. (goto-char (point-min))
  13969. (while (re-search-forward re nil t)
  13970. (replace-match "")
  13971. (end-of-line 1))
  13972. min)))
  13973. (defun org-base-buffer (buffer)
  13974. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13975. (if (not buffer)
  13976. buffer
  13977. (or (buffer-base-buffer buffer)
  13978. buffer)))
  13979. (defun org-trim (s)
  13980. "Remove whitespace at beginning and end of string."
  13981. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13982. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13983. s)
  13984. (defun org-wrap (string &optional width lines)
  13985. "Wrap string to either a number of lines, or a width in characters.
  13986. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13987. that costs. If there is a word longer than WIDTH, the text is actually
  13988. wrapped to the length of that word.
  13989. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13990. many lines, whatever width that takes.
  13991. The return value is a list of lines, without newlines at the end."
  13992. (let* ((words (org-split-string string "[ \t\n]+"))
  13993. (maxword (apply 'max (mapcar 'org-string-width words)))
  13994. w ll)
  13995. (cond (width
  13996. (org-do-wrap words (max maxword width)))
  13997. (lines
  13998. (setq w maxword)
  13999. (setq ll (org-do-wrap words maxword))
  14000. (if (<= (length ll) lines)
  14001. ll
  14002. (setq ll words)
  14003. (while (> (length ll) lines)
  14004. (setq w (1+ w))
  14005. (setq ll (org-do-wrap words w)))
  14006. ll))
  14007. (t (error "Cannot wrap this")))))
  14008. (defun org-do-wrap (words width)
  14009. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14010. (let (lines line)
  14011. (while words
  14012. (setq line (pop words))
  14013. (while (and words (< (+ (length line) (length (car words))) width))
  14014. (setq line (concat line " " (pop words))))
  14015. (setq lines (push line lines)))
  14016. (nreverse lines)))
  14017. (defun org-split-string (string &optional separators)
  14018. "Splits STRING into substrings at SEPARATORS.
  14019. No empty strings are returned if there are matches at the beginning
  14020. and end of string."
  14021. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14022. (start 0)
  14023. notfirst
  14024. (list nil))
  14025. (while (and (string-match rexp string
  14026. (if (and notfirst
  14027. (= start (match-beginning 0))
  14028. (< start (length string)))
  14029. (1+ start) start))
  14030. (< (match-beginning 0) (length string)))
  14031. (setq notfirst t)
  14032. (or (eq (match-beginning 0) 0)
  14033. (and (eq (match-beginning 0) (match-end 0))
  14034. (eq (match-beginning 0) start))
  14035. (setq list
  14036. (cons (substring string start (match-beginning 0))
  14037. list)))
  14038. (setq start (match-end 0)))
  14039. (or (eq start (length string))
  14040. (setq list
  14041. (cons (substring string start)
  14042. list)))
  14043. (nreverse list)))
  14044. (defun org-uuidgen-p (s)
  14045. "Is S an ID created by UUIDGEN?"
  14046. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14047. (defun org-context ()
  14048. "Return a list of contexts of the current cursor position.
  14049. If several contexts apply, all are returned.
  14050. Each context entry is a list with a symbol naming the context, and
  14051. two positions indicating start and end of the context. Possible
  14052. contexts are:
  14053. :headline anywhere in a headline
  14054. :headline-stars on the leading stars in a headline
  14055. :todo-keyword on a TODO keyword (including DONE) in a headline
  14056. :tags on the TAGS in a headline
  14057. :priority on the priority cookie in a headline
  14058. :item on the first line of a plain list item
  14059. :item-bullet on the bullet/number of a plain list item
  14060. :checkbox on the checkbox in a plain list item
  14061. :table in an org-mode table
  14062. :table-special on a special filed in a table
  14063. :table-table in a table.el table
  14064. :link on a hyperlink
  14065. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14066. :target on a <<target>>
  14067. :radio-target on a <<<radio-target>>>
  14068. :latex-fragment on a LaTeX fragment
  14069. :latex-preview on a LaTeX fragment with overlayed preview image
  14070. This function expects the position to be visible because it uses font-lock
  14071. faces as a help to recognize the following contexts: :table-special, :link,
  14072. and :keyword."
  14073. (let* ((f (get-text-property (point) 'face))
  14074. (faces (if (listp f) f (list f)))
  14075. (p (point)) clist o)
  14076. ;; First the large context
  14077. (cond
  14078. ((org-on-heading-p t)
  14079. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14080. (when (progn
  14081. (beginning-of-line 1)
  14082. (looking-at org-todo-line-tags-regexp))
  14083. (push (org-point-in-group p 1 :headline-stars) clist)
  14084. (push (org-point-in-group p 2 :todo-keyword) clist)
  14085. (push (org-point-in-group p 4 :tags) clist))
  14086. (goto-char p)
  14087. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14088. (if (looking-at "\\[#[A-Z0-9]\\]")
  14089. (push (org-point-in-group p 0 :priority) clist)))
  14090. ((org-at-item-p)
  14091. (push (org-point-in-group p 2 :item-bullet) clist)
  14092. (push (list :item (point-at-bol)
  14093. (save-excursion (org-end-of-item) (point)))
  14094. clist)
  14095. (and (org-at-item-checkbox-p)
  14096. (push (org-point-in-group p 0 :checkbox) clist)))
  14097. ((org-at-table-p)
  14098. (push (list :table (org-table-begin) (org-table-end)) clist)
  14099. (if (memq 'org-formula faces)
  14100. (push (list :table-special
  14101. (previous-single-property-change p 'face)
  14102. (next-single-property-change p 'face)) clist)))
  14103. ((org-at-table-p 'any)
  14104. (push (list :table-table) clist)))
  14105. (goto-char p)
  14106. ;; Now the small context
  14107. (cond
  14108. ((org-at-timestamp-p)
  14109. (push (org-point-in-group p 0 :timestamp) clist))
  14110. ((memq 'org-link faces)
  14111. (push (list :link
  14112. (previous-single-property-change p 'face)
  14113. (next-single-property-change p 'face)) clist))
  14114. ((memq 'org-special-keyword faces)
  14115. (push (list :keyword
  14116. (previous-single-property-change p 'face)
  14117. (next-single-property-change p 'face)) clist))
  14118. ((org-on-target-p)
  14119. (push (org-point-in-group p 0 :target) clist)
  14120. (goto-char (1- (match-beginning 0)))
  14121. (if (looking-at org-radio-target-regexp)
  14122. (push (org-point-in-group p 0 :radio-target) clist))
  14123. (goto-char p))
  14124. ((setq o (car (delq nil
  14125. (mapcar
  14126. (lambda (x)
  14127. (if (memq x org-latex-fragment-image-overlays) x))
  14128. (org-overlays-at (point))))))
  14129. (push (list :latex-fragment
  14130. (org-overlay-start o) (org-overlay-end o)) clist)
  14131. (push (list :latex-preview
  14132. (org-overlay-start o) (org-overlay-end o)) clist))
  14133. ((org-inside-LaTeX-fragment-p)
  14134. ;; FIXME: positions wrong.
  14135. (push (list :latex-fragment (point) (point)) clist)))
  14136. (setq clist (nreverse (delq nil clist)))
  14137. clist))
  14138. ;; FIXME: Compare with at-regexp-p Do we need both?
  14139. (defun org-in-regexp (re &optional nlines visually)
  14140. "Check if point is inside a match of regexp.
  14141. Normally only the current line is checked, but you can include NLINES extra
  14142. lines both before and after point into the search.
  14143. If VISUALLY is set, require that the cursor is not after the match but
  14144. really on, so that the block visually is on the match."
  14145. (catch 'exit
  14146. (let ((pos (point))
  14147. (eol (point-at-eol (+ 1 (or nlines 0))))
  14148. (inc (if visually 1 0)))
  14149. (save-excursion
  14150. (beginning-of-line (- 1 (or nlines 0)))
  14151. (while (re-search-forward re eol t)
  14152. (if (and (<= (match-beginning 0) pos)
  14153. (>= (+ inc (match-end 0)) pos))
  14154. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14155. (defun org-at-regexp-p (regexp)
  14156. "Is point inside a match of REGEXP in the current line?"
  14157. (catch 'exit
  14158. (save-excursion
  14159. (let ((pos (point)) (end (point-at-eol)))
  14160. (beginning-of-line 1)
  14161. (while (re-search-forward regexp end t)
  14162. (if (and (<= (match-beginning 0) pos)
  14163. (>= (match-end 0) pos))
  14164. (throw 'exit t)))
  14165. nil))))
  14166. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14167. "Call `multi-occur' with buffers for all agenda files."
  14168. (interactive "sOrg-files matching: \np")
  14169. (let* ((files (org-agenda-files))
  14170. (tnames (mapcar 'file-truename files))
  14171. (extra org-agenda-text-search-extra-files)
  14172. f)
  14173. (when (eq (car extra) 'agenda-archives)
  14174. (setq extra (cdr extra))
  14175. (setq files (org-add-archive-files files)))
  14176. (while (setq f (pop extra))
  14177. (unless (member (file-truename f) tnames)
  14178. (add-to-list 'files f 'append)
  14179. (add-to-list 'tnames (file-truename f) 'append)))
  14180. (multi-occur
  14181. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14182. regexp)))
  14183. (if (boundp 'occur-mode-find-occurrence-hook)
  14184. ;; Emacs 23
  14185. (add-hook 'occur-mode-find-occurrence-hook
  14186. (lambda ()
  14187. (when (org-mode-p)
  14188. (org-reveal))))
  14189. ;; Emacs 22
  14190. (defadvice occur-mode-goto-occurrence
  14191. (after org-occur-reveal activate)
  14192. (and (org-mode-p) (org-reveal)))
  14193. (defadvice occur-mode-goto-occurrence-other-window
  14194. (after org-occur-reveal activate)
  14195. (and (org-mode-p) (org-reveal)))
  14196. (defadvice occur-mode-display-occurrence
  14197. (after org-occur-reveal activate)
  14198. (when (org-mode-p)
  14199. (let ((pos (occur-mode-find-occurrence)))
  14200. (with-current-buffer (marker-buffer pos)
  14201. (save-excursion
  14202. (goto-char pos)
  14203. (org-reveal)))))))
  14204. (defun org-uniquify (list)
  14205. "Remove duplicate elements from LIST."
  14206. (let (res)
  14207. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14208. res))
  14209. (defun org-delete-all (elts list)
  14210. "Remove all elements in ELTS from LIST."
  14211. (while elts
  14212. (setq list (delete (pop elts) list)))
  14213. list)
  14214. (defun org-back-over-empty-lines ()
  14215. "Move backwards over whitespace, to the beginning of the first empty line.
  14216. Returns the number of empty lines passed."
  14217. (let ((pos (point)))
  14218. (skip-chars-backward " \t\n\r")
  14219. (beginning-of-line 2)
  14220. (goto-char (min (point) pos))
  14221. (count-lines (point) pos)))
  14222. (defun org-skip-whitespace ()
  14223. (skip-chars-forward " \t\n\r"))
  14224. (defun org-point-in-group (point group &optional context)
  14225. "Check if POINT is in match-group GROUP.
  14226. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14227. match. If the match group does ot exist or point is not inside it,
  14228. return nil."
  14229. (and (match-beginning group)
  14230. (>= point (match-beginning group))
  14231. (<= point (match-end group))
  14232. (if context
  14233. (list context (match-beginning group) (match-end group))
  14234. t)))
  14235. (defun org-switch-to-buffer-other-window (&rest args)
  14236. "Switch to buffer in a second window on the current frame.
  14237. In particular, do not allow pop-up frames."
  14238. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14239. special-display-function)
  14240. (apply 'switch-to-buffer-other-window args)))
  14241. (defun org-combine-plists (&rest plists)
  14242. "Create a single property list from all plists in PLISTS.
  14243. The process starts by copying the first list, and then setting properties
  14244. from the other lists. Settings in the last list are the most significant
  14245. ones and overrule settings in the other lists."
  14246. (let ((rtn (copy-sequence (pop plists)))
  14247. p v ls)
  14248. (while plists
  14249. (setq ls (pop plists))
  14250. (while ls
  14251. (setq p (pop ls) v (pop ls))
  14252. (setq rtn (plist-put rtn p v))))
  14253. rtn))
  14254. (defun org-move-line-down (arg)
  14255. "Move the current line down. With prefix argument, move it past ARG lines."
  14256. (interactive "p")
  14257. (let ((col (current-column))
  14258. beg end pos)
  14259. (beginning-of-line 1) (setq beg (point))
  14260. (beginning-of-line 2) (setq end (point))
  14261. (beginning-of-line (+ 1 arg))
  14262. (setq pos (move-marker (make-marker) (point)))
  14263. (insert (delete-and-extract-region beg end))
  14264. (goto-char pos)
  14265. (org-move-to-column col)))
  14266. (defun org-move-line-up (arg)
  14267. "Move the current line up. With prefix argument, move it past ARG lines."
  14268. (interactive "p")
  14269. (let ((col (current-column))
  14270. beg end pos)
  14271. (beginning-of-line 1) (setq beg (point))
  14272. (beginning-of-line 2) (setq end (point))
  14273. (beginning-of-line (- arg))
  14274. (setq pos (move-marker (make-marker) (point)))
  14275. (insert (delete-and-extract-region beg end))
  14276. (goto-char pos)
  14277. (org-move-to-column col)))
  14278. (defun org-replace-escapes (string table)
  14279. "Replace %-escapes in STRING with values in TABLE.
  14280. TABLE is an association list with keys like \"%a\" and string values.
  14281. The sequences in STRING may contain normal field width and padding information,
  14282. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14283. so values can contain further %-escapes if they are define later in TABLE."
  14284. (let ((case-fold-search nil)
  14285. e re rpl)
  14286. (while (setq e (pop table))
  14287. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14288. (while (string-match re string)
  14289. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14290. (cdr e)))
  14291. (setq string (replace-match rpl t t string))))
  14292. string))
  14293. (defun org-sublist (list start end)
  14294. "Return a section of LIST, from START to END.
  14295. Counting starts at 1."
  14296. (let (rtn (c start))
  14297. (setq list (nthcdr (1- start) list))
  14298. (while (and list (<= c end))
  14299. (push (pop list) rtn)
  14300. (setq c (1+ c)))
  14301. (nreverse rtn)))
  14302. (defun org-find-base-buffer-visiting (file)
  14303. "Like `find-buffer-visiting' but always return the base buffer and
  14304. not an indirect buffer."
  14305. (let ((buf (or (get-file-buffer file)
  14306. (find-buffer-visiting file))))
  14307. (if buf
  14308. (or (buffer-base-buffer buf) buf)
  14309. nil)))
  14310. (defun org-image-file-name-regexp (&optional extensions)
  14311. "Return regexp matching the file names of images.
  14312. If EXTENSIONS is given, only match these."
  14313. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14314. (image-file-name-regexp)
  14315. (let ((image-file-name-extensions
  14316. (or extensions
  14317. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14318. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14319. (concat "\\."
  14320. (regexp-opt (nconc (mapcar 'upcase
  14321. image-file-name-extensions)
  14322. image-file-name-extensions)
  14323. t)
  14324. "\\'"))))
  14325. (defun org-file-image-p (file &optional extensions)
  14326. "Return non-nil if FILE is an image."
  14327. (save-match-data
  14328. (string-match (org-image-file-name-regexp extensions) file)))
  14329. (defun org-get-cursor-date ()
  14330. "Return the date at cursor in as a time.
  14331. This works in the calendar and in the agenda, anywhere else it just
  14332. returns the current time."
  14333. (let (date day defd)
  14334. (cond
  14335. ((eq major-mode 'calendar-mode)
  14336. (setq date (calendar-cursor-to-date)
  14337. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14338. ((eq major-mode 'org-agenda-mode)
  14339. (setq day (get-text-property (point) 'day))
  14340. (if day
  14341. (setq date (calendar-gregorian-from-absolute day)
  14342. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14343. (nth 2 date))))))
  14344. (or defd (current-time))))
  14345. (defvar org-agenda-action-marker (make-marker)
  14346. "Marker pointing to the entry for the next agenda action.")
  14347. (defun org-mark-entry-for-agenda-action ()
  14348. "Mark the current entry as target of an agenda action.
  14349. Agenda actions are actions executed from the agenda with the key `k',
  14350. which make use of the date at the cursor."
  14351. (interactive)
  14352. (move-marker org-agenda-action-marker
  14353. (save-excursion (org-back-to-heading t) (point))
  14354. (current-buffer))
  14355. (message
  14356. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14357. ;;; Paragraph filling stuff.
  14358. ;; We want this to be just right, so use the full arsenal.
  14359. (defun org-indent-line-function ()
  14360. "Indent line like previous, but further if previous was headline or item."
  14361. (interactive)
  14362. (let* ((pos (point))
  14363. (itemp (org-at-item-p))
  14364. (case-fold-search t)
  14365. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14366. column bpos bcol tpos tcol bullet btype bullet-type)
  14367. ;; Find the previous relevant line
  14368. (beginning-of-line 1)
  14369. (cond
  14370. ((looking-at "#") (setq column 0))
  14371. ((looking-at "\\*+ ") (setq column 0))
  14372. ((and (looking-at "[ \t]*:END:")
  14373. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14374. (save-excursion
  14375. (goto-char (1- (match-beginning 1)))
  14376. (setq column (current-column))))
  14377. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14378. (save-excursion
  14379. (re-search-backward
  14380. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14381. (setq column (org-get-indentation (match-string 0))))
  14382. (t
  14383. (beginning-of-line 0)
  14384. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14385. (not (looking-at "[ \t]*:END:"))
  14386. (not (looking-at org-drawer-regexp)))
  14387. (beginning-of-line 0))
  14388. (cond
  14389. ((looking-at "\\*+[ \t]+")
  14390. (if (not org-adapt-indentation)
  14391. (setq column 0)
  14392. (goto-char (match-end 0))
  14393. (setq column (current-column))))
  14394. ((looking-at org-drawer-regexp)
  14395. (goto-char (1- (match-beginning 1)))
  14396. (setq column (current-column)))
  14397. ((looking-at "\\([ \t]*\\):END:")
  14398. (goto-char (match-end 1))
  14399. (setq column (current-column)))
  14400. ((org-in-item-p)
  14401. (org-beginning-of-item)
  14402. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14403. (setq bpos (match-beginning 1) tpos (match-end 0)
  14404. bcol (progn (goto-char bpos) (current-column))
  14405. tcol (progn (goto-char tpos) (current-column))
  14406. bullet (match-string 1)
  14407. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14408. (if (> tcol (+ bcol org-description-max-indent))
  14409. (setq tcol (+ bcol 5)))
  14410. (if (not itemp)
  14411. (setq column tcol)
  14412. (goto-char pos)
  14413. (beginning-of-line 1)
  14414. (if (looking-at "\\S-")
  14415. (progn
  14416. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14417. (setq bullet (match-string 1)
  14418. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14419. (setq column (if (equal btype bullet-type) bcol tcol)))
  14420. (setq column (org-get-indentation)))))
  14421. (t (setq column (org-get-indentation))))))
  14422. (goto-char pos)
  14423. (if (<= (current-column) (current-indentation))
  14424. (org-indent-line-to column)
  14425. (save-excursion (org-indent-line-to column)))
  14426. (setq column (current-column))
  14427. (beginning-of-line 1)
  14428. (if (looking-at
  14429. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14430. (replace-match (concat "\\1" (format org-property-format
  14431. (match-string 2) (match-string 3)))
  14432. t nil))
  14433. (org-move-to-column column)))
  14434. (defun org-set-autofill-regexps ()
  14435. (interactive)
  14436. ;; In the paragraph separator we include headlines, because filling
  14437. ;; text in a line directly attached to a headline would otherwise
  14438. ;; fill the headline as well.
  14439. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14440. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14441. ;; The paragraph starter includes hand-formatted lists.
  14442. (org-set-local 'paragraph-start
  14443. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14444. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14445. ;; But only if the user has not turned off tables or fixed-width regions
  14446. (org-set-local
  14447. 'auto-fill-inhibit-regexp
  14448. (concat "\\*+ \\|#\\+"
  14449. "\\|[ \t]*" org-keyword-time-regexp
  14450. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14451. (concat
  14452. "\\|[ \t]*["
  14453. (if org-enable-table-editor "|" "")
  14454. (if org-enable-fixed-width-editor ":" "")
  14455. "]"))))
  14456. ;; We use our own fill-paragraph function, to make sure that tables
  14457. ;; and fixed-width regions are not wrapped. That function will pass
  14458. ;; through to `fill-paragraph' when appropriate.
  14459. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14460. ; Adaptive filling: To get full control, first make sure that
  14461. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14462. (org-set-local 'adaptive-fill-regexp "\000")
  14463. (org-set-local 'adaptive-fill-function
  14464. 'org-adaptive-fill-function)
  14465. (org-set-local
  14466. 'align-mode-rules-list
  14467. '((org-in-buffer-settings
  14468. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14469. (modes . '(org-mode))))))
  14470. (defun org-fill-paragraph (&optional justify)
  14471. "Re-align a table, pass through to fill-paragraph if no table."
  14472. (let ((table-p (org-at-table-p))
  14473. (table.el-p (org-at-table.el-p)))
  14474. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14475. (save-excursion (goto-char (point-at-bol))
  14476. (looking-at outline-regexp)))
  14477. t) ; skip headlines
  14478. (table.el-p t) ; skip table.el tables
  14479. (table-p (org-table-align) t) ; align org-mode tables
  14480. (t nil)))) ; call paragraph-fill
  14481. ;; For reference, this is the default value of adaptive-fill-regexp
  14482. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14483. (defun org-adaptive-fill-function ()
  14484. "Return a fill prefix for org-mode files.
  14485. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14486. work correctly."
  14487. (cond ((looking-at "#[ \t]+")
  14488. (match-string 0))
  14489. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14490. (save-excursion
  14491. (if (> (match-end 1) (+ (match-beginning 1)
  14492. org-description-max-indent))
  14493. (goto-char (+ (match-beginning 1) 5))
  14494. (goto-char (match-end 0)))
  14495. (make-string (current-column) ?\ )))
  14496. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14497. (save-excursion
  14498. (goto-char (match-end 0))
  14499. (make-string (current-column) ?\ )))
  14500. (t nil)))
  14501. ;;; Other stuff.
  14502. (defun org-toggle-fixed-width-section (arg)
  14503. "Toggle the fixed-width export.
  14504. If there is no active region, the QUOTE keyword at the current headline is
  14505. inserted or removed. When present, it causes the text between this headline
  14506. and the next to be exported as fixed-width text, and unmodified.
  14507. If there is an active region, this command adds or removes a colon as the
  14508. first character of this line. If the first character of a line is a colon,
  14509. this line is also exported in fixed-width font."
  14510. (interactive "P")
  14511. (let* ((cc 0)
  14512. (regionp (org-region-active-p))
  14513. (beg (if regionp (region-beginning) (point)))
  14514. (end (if regionp (region-end)))
  14515. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14516. (case-fold-search nil)
  14517. (re "[ \t]*\\(: \\)")
  14518. off)
  14519. (if regionp
  14520. (save-excursion
  14521. (goto-char beg)
  14522. (setq cc (current-column))
  14523. (beginning-of-line 1)
  14524. (setq off (looking-at re))
  14525. (while (> nlines 0)
  14526. (setq nlines (1- nlines))
  14527. (beginning-of-line 1)
  14528. (cond
  14529. (arg
  14530. (org-move-to-column cc t)
  14531. (insert ": \n")
  14532. (forward-line -1))
  14533. ((and off (looking-at re))
  14534. (replace-match "" t t nil 1))
  14535. ((not off) (org-move-to-column cc t) (insert ": ")))
  14536. (forward-line 1)))
  14537. (save-excursion
  14538. (org-back-to-heading)
  14539. (if (looking-at (concat outline-regexp
  14540. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14541. (replace-match "" t t nil 1)
  14542. (if (looking-at outline-regexp)
  14543. (progn
  14544. (goto-char (match-end 0))
  14545. (insert org-quote-string " "))))))))
  14546. (defun org-reftex-citation ()
  14547. "Use reftex-citation to insert a citation into the buffer.
  14548. This looks for a line like
  14549. #+BIBLIOGRAPHY: foo plain option:-d
  14550. and derives from it that foo.bib is the bbliography file relevant
  14551. for this document. It then installs the necessary environment for RefTeX
  14552. to work in this buffer and calls `reftex-citation' to insert a citation
  14553. into the buffer.
  14554. Export of such citations to both LaTeX and HTML is handled by the contributed
  14555. package org-exp-bibtex by Taru Karttunen."
  14556. (interactive)
  14557. (let ((reftex-docstruct-symbol 'rds)
  14558. (reftex-cite-format "\\cite{%l}")
  14559. rds bib)
  14560. (save-excursion
  14561. (save-restriction
  14562. (widen)
  14563. (let ((case-fold-search t)
  14564. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14565. (if (not (save-excursion
  14566. (or (re-search-forward re nil t)
  14567. (re-search-backward re nil t))))
  14568. (error "No bibliography defined in file")
  14569. (setq bib (concat (match-string 1) ".bib")
  14570. rds (list (list 'bib bib)))))))
  14571. (call-interactively 'reftex-citation)))
  14572. ;;;; Functions extending outline functionality
  14573. (defun org-beginning-of-line (&optional arg)
  14574. "Go to the beginning of the current line. If that is invisible, continue
  14575. to a visible line beginning. This makes the function of C-a more intuitive.
  14576. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14577. first attempt, and only move to after the tags when the cursor is already
  14578. beyond the end of the headline."
  14579. (interactive "P")
  14580. (let ((pos (point))
  14581. (special (if (consp org-special-ctrl-a/e)
  14582. (car org-special-ctrl-a/e)
  14583. org-special-ctrl-a/e))
  14584. refpos)
  14585. (beginning-of-line 1)
  14586. (if (and arg (fboundp 'move-beginning-of-line))
  14587. (call-interactively 'move-beginning-of-line)
  14588. (if (bobp)
  14589. nil
  14590. (backward-char 1)
  14591. (if (org-invisible-p)
  14592. (while (and (not (bobp)) (org-invisible-p))
  14593. (backward-char 1)
  14594. (beginning-of-line 1))
  14595. (forward-char 1))))
  14596. (when special
  14597. (cond
  14598. ((and (looking-at org-complex-heading-regexp)
  14599. (= (char-after (match-end 1)) ?\ ))
  14600. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14601. (point-at-eol)))
  14602. (goto-char
  14603. (if (eq special t)
  14604. (cond ((> pos refpos) refpos)
  14605. ((= pos (point)) refpos)
  14606. (t (point)))
  14607. (cond ((> pos (point)) (point))
  14608. ((not (eq last-command this-command)) (point))
  14609. (t refpos)))))
  14610. ((org-at-item-p)
  14611. (goto-char
  14612. (if (eq special t)
  14613. (cond ((> pos (match-end 4)) (match-end 4))
  14614. ((= pos (point)) (match-end 4))
  14615. (t (point)))
  14616. (cond ((> pos (point)) (point))
  14617. ((not (eq last-command this-command)) (point))
  14618. (t (match-end 4))))))))
  14619. (org-no-warnings
  14620. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14621. (defun org-end-of-line (&optional arg)
  14622. "Go to the end of the line.
  14623. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14624. first attempt, and only move to after the tags when the cursor is already
  14625. beyond the end of the headline."
  14626. (interactive "P")
  14627. (let ((special (if (consp org-special-ctrl-a/e)
  14628. (cdr org-special-ctrl-a/e)
  14629. org-special-ctrl-a/e)))
  14630. (if (or (not special)
  14631. (not (org-on-heading-p))
  14632. arg)
  14633. (call-interactively (if (fboundp 'move-end-of-line)
  14634. 'move-end-of-line
  14635. 'end-of-line))
  14636. (let ((pos (point)))
  14637. (beginning-of-line 1)
  14638. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14639. (if (eq special t)
  14640. (if (or (< pos (match-beginning 1))
  14641. (= pos (match-end 0)))
  14642. (goto-char (match-beginning 1))
  14643. (goto-char (match-end 0)))
  14644. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14645. (goto-char (match-end 0))
  14646. (goto-char (match-beginning 1))))
  14647. (call-interactively (if (fboundp 'move-end-of-line)
  14648. 'move-end-of-line
  14649. 'end-of-line)))))
  14650. (org-no-warnings
  14651. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14652. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14653. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14654. (defun org-backward-sentence (&optional arg)
  14655. "Go to beginning of sentence, or beginning of table field.
  14656. This will call `backward-sentence' or `org-table-beginning-of-field',
  14657. depending on context."
  14658. (interactive "P")
  14659. (cond
  14660. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14661. (t (call-interactively 'backward-sentence))))
  14662. (defun org-forward-sentence (&optional arg)
  14663. "Go to end of sentence, or end of table field.
  14664. This will call `forward-sentence' or `org-table-end-of-field',
  14665. depending on context."
  14666. (interactive "P")
  14667. (cond
  14668. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14669. (t (call-interactively 'forward-sentence))))
  14670. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14671. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14672. (defun org-kill-line (&optional arg)
  14673. "Kill line, to tags or end of line."
  14674. (interactive "P")
  14675. (cond
  14676. ((or (not org-special-ctrl-k)
  14677. (bolp)
  14678. (not (org-on-heading-p)))
  14679. (call-interactively 'kill-line))
  14680. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14681. (kill-region (point) (match-beginning 1))
  14682. (org-set-tags nil t))
  14683. (t (kill-region (point) (point-at-eol)))))
  14684. (define-key org-mode-map "\C-k" 'org-kill-line)
  14685. (defun org-yank (&optional arg)
  14686. "Yank. If the kill is a subtree, treat it specially.
  14687. This command will look at the current kill and check if is a single
  14688. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14689. cursor is at the beginning of a line or after the stars of a currently
  14690. empty headline, then the yank is handled specially. How exactly depends
  14691. on the value of the following variables, both set by default.
  14692. org-yank-folded-subtrees
  14693. When set, the subtree(s) will be folded after insertion, but only
  14694. if doing so would now swallow text after the yanked text.
  14695. org-yank-adjusted-subtrees
  14696. When set, the subtree will be promoted or demoted in order to
  14697. fit into the local outline tree structure, which means that the level
  14698. will be adjusted so that it becomes the smaller one of the two
  14699. *visible* surrounding headings.
  14700. Any prefix to this command will cause `yank' to be called directly with
  14701. no special treatment. In particular, a simple `C-u' prefix will just
  14702. plainly yank the text as it is.
  14703. \[1] The test checks if the first non-white line is a heading
  14704. and if there are no other headings with fewer stars."
  14705. (interactive "P")
  14706. (org-yank-generic 'yank arg))
  14707. (defun org-yank-generic (command arg)
  14708. "Perform some yank-like command.
  14709. This function implements the behavior described in the `org-yank'
  14710. documentation. However, it has been generalized to work for any
  14711. interactive command with similar behavior."
  14712. ;; pretend to be command COMMAND
  14713. (setq this-command command)
  14714. (if arg
  14715. (call-interactively command)
  14716. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14717. (and (org-kill-is-subtree-p)
  14718. (or (bolp)
  14719. (and (looking-at "[ \t]*$")
  14720. (string-match
  14721. "\\`\\*+\\'"
  14722. (buffer-substring (point-at-bol) (point)))))))
  14723. swallowp)
  14724. (cond
  14725. ((and subtreep org-yank-folded-subtrees)
  14726. (let ((beg (point))
  14727. end)
  14728. (if (and subtreep org-yank-adjusted-subtrees)
  14729. (org-paste-subtree nil nil 'for-yank)
  14730. (call-interactively command))
  14731. (setq end (point))
  14732. (goto-char beg)
  14733. (when (and (bolp) subtreep
  14734. (not (setq swallowp
  14735. (org-yank-folding-would-swallow-text beg end))))
  14736. (or (looking-at outline-regexp)
  14737. (re-search-forward (concat "^" outline-regexp) end t))
  14738. (while (and (< (point) end) (looking-at outline-regexp))
  14739. (hide-subtree)
  14740. (org-cycle-show-empty-lines 'folded)
  14741. (condition-case nil
  14742. (outline-forward-same-level 1)
  14743. (error (goto-char end)))))
  14744. (when swallowp
  14745. (message
  14746. "Inserted text not folded because that would swallow text"))
  14747. (goto-char end)
  14748. (skip-chars-forward " \t\n\r")
  14749. (beginning-of-line 1)
  14750. (push-mark beg 'nomsg)))
  14751. ((and subtreep org-yank-adjusted-subtrees)
  14752. (let ((beg (point-at-bol)))
  14753. (org-paste-subtree nil nil 'for-yank)
  14754. (push-mark beg 'nomsg)))
  14755. (t
  14756. (call-interactively command))))))
  14757. (defun org-yank-folding-would-swallow-text (beg end)
  14758. "Would hide-subtree at BEG swallow any text after END?"
  14759. (let (level)
  14760. (save-excursion
  14761. (goto-char beg)
  14762. (when (or (looking-at outline-regexp)
  14763. (re-search-forward (concat "^" outline-regexp) end t))
  14764. (setq level (org-outline-level)))
  14765. (goto-char end)
  14766. (skip-chars-forward " \t\r\n\v\f")
  14767. (if (or (eobp)
  14768. (and (bolp) (looking-at org-outline-regexp)
  14769. (<= (org-outline-level) level)))
  14770. nil ; Nothing would be swallowed
  14771. t)))) ; something would swallow
  14772. (define-key org-mode-map "\C-y" 'org-yank)
  14773. (defun org-invisible-p ()
  14774. "Check if point is at a character currently not visible."
  14775. ;; Early versions of noutline don't have `outline-invisible-p'.
  14776. (if (fboundp 'outline-invisible-p)
  14777. (outline-invisible-p)
  14778. (get-char-property (point) 'invisible)))
  14779. (defun org-invisible-p2 ()
  14780. "Check if point is at a character currently not visible."
  14781. (save-excursion
  14782. (if (and (eolp) (not (bobp))) (backward-char 1))
  14783. ;; Early versions of noutline don't have `outline-invisible-p'.
  14784. (if (fboundp 'outline-invisible-p)
  14785. (outline-invisible-p)
  14786. (get-char-property (point) 'invisible))))
  14787. (defun org-back-to-heading (&optional invisible-ok)
  14788. "Call `outline-back-to-heading', but provide a better error message."
  14789. (condition-case nil
  14790. (outline-back-to-heading invisible-ok)
  14791. (error (error "Before first headline at position %d in buffer %s"
  14792. (point) (current-buffer)))))
  14793. (defun org-before-first-heading-p ()
  14794. "Before first heading?"
  14795. (save-excursion
  14796. (null (re-search-backward "^\\*+ " nil t))))
  14797. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14798. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14799. (defun org-at-heading-or-item-p ()
  14800. (or (org-on-heading-p) (org-at-item-p)))
  14801. (defun org-on-target-p ()
  14802. (or (org-in-regexp org-radio-target-regexp)
  14803. (org-in-regexp org-target-regexp)))
  14804. (defun org-up-heading-all (arg)
  14805. "Move to the heading line of which the present line is a subheading.
  14806. This function considers both visible and invisible heading lines.
  14807. With argument, move up ARG levels."
  14808. (if (fboundp 'outline-up-heading-all)
  14809. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14810. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14811. (defun org-up-heading-safe ()
  14812. "Move to the heading line of which the present line is a subheading.
  14813. This version will not throw an error. It will return the level of the
  14814. headline found, or nil if no higher level is found.
  14815. Also, this function will be a lot faster than `outline-up-heading',
  14816. because it relies on stars being the outline starters. This can really
  14817. make a significant difference in outlines with very many siblings."
  14818. (let (start-level re)
  14819. (org-back-to-heading t)
  14820. (setq start-level (funcall outline-level))
  14821. (if (equal start-level 1)
  14822. nil
  14823. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14824. (if (re-search-backward re nil t)
  14825. (funcall outline-level)))))
  14826. (defun org-first-sibling-p ()
  14827. "Is this heading the first child of its parents?"
  14828. (interactive)
  14829. (let ((re (concat "^" outline-regexp))
  14830. level l)
  14831. (unless (org-at-heading-p t)
  14832. (error "Not at a heading"))
  14833. (setq level (funcall outline-level))
  14834. (save-excursion
  14835. (if (not (re-search-backward re nil t))
  14836. t
  14837. (setq l (funcall outline-level))
  14838. (< l level)))))
  14839. (defun org-goto-sibling (&optional previous)
  14840. "Goto the next sibling, even if it is invisible.
  14841. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14842. when a sibling was found. When none is found, return nil and don't
  14843. move point."
  14844. (let ((fun (if previous 're-search-backward 're-search-forward))
  14845. (pos (point))
  14846. (re (concat "^" outline-regexp))
  14847. level l)
  14848. (when (condition-case nil (org-back-to-heading t) (error nil))
  14849. (setq level (funcall outline-level))
  14850. (catch 'exit
  14851. (or previous (forward-char 1))
  14852. (while (funcall fun re nil t)
  14853. (setq l (funcall outline-level))
  14854. (when (< l level) (goto-char pos) (throw 'exit nil))
  14855. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14856. (goto-char pos)
  14857. nil))))
  14858. (defun org-show-siblings ()
  14859. "Show all siblings of the current headline."
  14860. (save-excursion
  14861. (while (org-goto-sibling) (org-flag-heading nil)))
  14862. (save-excursion
  14863. (while (org-goto-sibling 'previous)
  14864. (org-flag-heading nil))))
  14865. (defun org-show-hidden-entry ()
  14866. "Show an entry where even the heading is hidden."
  14867. (save-excursion
  14868. (org-show-entry)))
  14869. (defun org-flag-heading (flag &optional entry)
  14870. "Flag the current heading. FLAG non-nil means make invisible.
  14871. When ENTRY is non-nil, show the entire entry."
  14872. (save-excursion
  14873. (org-back-to-heading t)
  14874. ;; Check if we should show the entire entry
  14875. (if entry
  14876. (progn
  14877. (org-show-entry)
  14878. (save-excursion
  14879. (and (outline-next-heading)
  14880. (org-flag-heading nil))))
  14881. (outline-flag-region (max (point-min) (1- (point)))
  14882. (save-excursion (outline-end-of-heading) (point))
  14883. flag))))
  14884. (defun org-forward-same-level (arg)
  14885. "Move forward to the ARG'th subheading at same level as this one.
  14886. Stop at the first and last subheadings of a superior heading.
  14887. This is like outline-forward-same-level, but invisible headings are ok."
  14888. (interactive "p")
  14889. (org-back-to-heading t)
  14890. (while (> arg 0)
  14891. (let ((point-to-move-to (save-excursion
  14892. (org-get-next-sibling))))
  14893. (if point-to-move-to
  14894. (progn
  14895. (goto-char point-to-move-to)
  14896. (setq arg (1- arg)))
  14897. (progn
  14898. (setq arg 0)
  14899. (error "No following same-level heading"))))))
  14900. (defun org-get-next-sibling ()
  14901. "Move to next heading of the same level, and return point.
  14902. If there is no such heading, return nil.
  14903. This is like outline-next-sibling, but invisible headings are ok."
  14904. (let ((level (funcall outline-level)))
  14905. (outline-next-heading)
  14906. (while (and (not (eobp)) (> (funcall outline-level) level))
  14907. (outline-next-heading))
  14908. (if (or (eobp) (< (funcall outline-level) level))
  14909. nil
  14910. (point))))
  14911. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14912. ;; This contains an exact copy of the original function, but it uses
  14913. ;; `org-back-to-heading', to make it work also in invisible
  14914. ;; trees. And is uses an invisible-OK argument.
  14915. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14916. ;; Furthermore, when used inside Org, finding the end of a large subtree
  14917. ;; with many children and grandchildren etc, this can be much faster
  14918. ;; than the outline version.
  14919. (org-back-to-heading invisible-OK)
  14920. (let ((first t)
  14921. (level (funcall outline-level)))
  14922. (if (and (org-mode-p) (< level 1000))
  14923. ;; A true heading (not a plain list item), in Org-mode
  14924. ;; This means we can easily find the end by looking
  14925. ;; only for the right number of stars. Using a regexp to do
  14926. ;; this is so much faster than using a Lisp loop.
  14927. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  14928. (forward-char 1)
  14929. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  14930. ;; something else, do it the slow way
  14931. (while (and (not (eobp))
  14932. (or first (> (funcall outline-level) level)))
  14933. (setq first nil)
  14934. (outline-next-heading)))
  14935. (unless to-heading
  14936. (if (memq (preceding-char) '(?\n ?\^M))
  14937. (progn
  14938. ;; Go to end of line before heading
  14939. (forward-char -1)
  14940. (if (memq (preceding-char) '(?\n ?\^M))
  14941. ;; leave blank line before heading
  14942. (forward-char -1))))))
  14943. (point))
  14944. (defun org-show-subtree ()
  14945. "Show everything after this heading at deeper levels."
  14946. (outline-flag-region
  14947. (point)
  14948. (save-excursion
  14949. (outline-end-of-subtree) (outline-next-heading) (point))
  14950. nil))
  14951. (defun org-show-entry ()
  14952. "Show the body directly following this heading.
  14953. Show the heading too, if it is currently invisible."
  14954. (interactive)
  14955. (save-excursion
  14956. (condition-case nil
  14957. (progn
  14958. (org-back-to-heading t)
  14959. (outline-flag-region
  14960. (max (point-min) (1- (point)))
  14961. (save-excursion
  14962. (if (re-search-forward
  14963. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14964. (match-beginning 1)
  14965. (point-max)))
  14966. nil))
  14967. (error nil))))
  14968. (defun org-make-options-regexp (kwds &optional extra)
  14969. "Make a regular expression for keyword lines."
  14970. (concat
  14971. "^"
  14972. "#?[ \t]*\\+\\("
  14973. (mapconcat 'regexp-quote kwds "\\|")
  14974. (if extra (concat "\\|" extra))
  14975. "\\):[ \t]*"
  14976. "\\(.+\\)"))
  14977. ;; Make isearch reveal the necessary context
  14978. (defun org-isearch-end ()
  14979. "Reveal context after isearch exits."
  14980. (when isearch-success ; only if search was successful
  14981. (if (featurep 'xemacs)
  14982. ;; Under XEmacs, the hook is run in the correct place,
  14983. ;; we directly show the context.
  14984. (org-show-context 'isearch)
  14985. ;; In Emacs the hook runs *before* restoring the overlays.
  14986. ;; So we have to use a one-time post-command-hook to do this.
  14987. ;; (Emacs 22 has a special variable, see function `org-mode')
  14988. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14989. isearch-mode-end-hook-quit)
  14990. ;; Only when the isearch was not quitted.
  14991. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14992. 'append 'local)))))
  14993. (defun org-isearch-post-command ()
  14994. "Remove self from hook, and show context."
  14995. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14996. (org-show-context 'isearch))
  14997. ;;;; Integration with and fixes for other packages
  14998. ;;; Imenu support
  14999. (defvar org-imenu-markers nil
  15000. "All markers currently used by Imenu.")
  15001. (make-variable-buffer-local 'org-imenu-markers)
  15002. (defun org-imenu-new-marker (&optional pos)
  15003. "Return a new marker for use by Imenu, and remember the marker."
  15004. (let ((m (make-marker)))
  15005. (move-marker m (or pos (point)))
  15006. (push m org-imenu-markers)
  15007. m))
  15008. (defun org-imenu-get-tree ()
  15009. "Produce the index for Imenu."
  15010. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15011. (setq org-imenu-markers nil)
  15012. (let* ((n org-imenu-depth)
  15013. (re (concat "^" outline-regexp))
  15014. (subs (make-vector (1+ n) nil))
  15015. (last-level 0)
  15016. m level head)
  15017. (save-excursion
  15018. (save-restriction
  15019. (widen)
  15020. (goto-char (point-max))
  15021. (while (re-search-backward re nil t)
  15022. (setq level (org-reduced-level (funcall outline-level)))
  15023. (when (<= level n)
  15024. (looking-at org-complex-heading-regexp)
  15025. (setq head (org-link-display-format
  15026. (org-match-string-no-properties 4))
  15027. m (org-imenu-new-marker))
  15028. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15029. (if (>= level last-level)
  15030. (push (cons head m) (aref subs level))
  15031. (push (cons head (aref subs (1+ level))) (aref subs level))
  15032. (loop for i from (1+ level) to n do (aset subs i nil)))
  15033. (setq last-level level)))))
  15034. (aref subs 1)))
  15035. (eval-after-load "imenu"
  15036. '(progn
  15037. (add-hook 'imenu-after-jump-hook
  15038. (lambda ()
  15039. (if (eq major-mode 'org-mode)
  15040. (org-show-context 'org-goto))))))
  15041. (defun org-link-display-format (link)
  15042. "Replace a link with either the description, or the link target
  15043. if no description is present"
  15044. (save-match-data
  15045. (if (string-match org-bracket-link-analytic-regexp link)
  15046. (replace-match (or (match-string 5 link)
  15047. (concat (match-string 1 link)
  15048. (match-string 3 link)))
  15049. nil nil link)
  15050. link)))
  15051. ;; Speedbar support
  15052. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15053. "Overlay marking the agenda restriction line in speedbar.")
  15054. (org-overlay-put org-speedbar-restriction-lock-overlay
  15055. 'face 'org-agenda-restriction-lock)
  15056. (org-overlay-put org-speedbar-restriction-lock-overlay
  15057. 'help-echo "Agendas are currently limited to this item.")
  15058. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15059. (defun org-speedbar-set-agenda-restriction ()
  15060. "Restrict future agenda commands to the location at point in speedbar.
  15061. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15062. (interactive)
  15063. (require 'org-agenda)
  15064. (let (p m tp np dir txt)
  15065. (cond
  15066. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15067. 'org-imenu t))
  15068. (setq m (get-text-property p 'org-imenu-marker))
  15069. (save-excursion
  15070. (save-restriction
  15071. (set-buffer (marker-buffer m))
  15072. (goto-char m)
  15073. (org-agenda-set-restriction-lock 'subtree))))
  15074. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15075. 'speedbar-function 'speedbar-find-file))
  15076. (setq tp (previous-single-property-change
  15077. (1+ p) 'speedbar-function)
  15078. np (next-single-property-change
  15079. tp 'speedbar-function)
  15080. dir (speedbar-line-directory)
  15081. txt (buffer-substring-no-properties (or tp (point-min))
  15082. (or np (point-max))))
  15083. (save-excursion
  15084. (save-restriction
  15085. (set-buffer (find-file-noselect
  15086. (let ((default-directory dir))
  15087. (expand-file-name txt))))
  15088. (unless (org-mode-p)
  15089. (error "Cannot restrict to non-Org-mode file"))
  15090. (org-agenda-set-restriction-lock 'file))))
  15091. (t (error "Don't know how to restrict Org-mode's agenda")))
  15092. (org-move-overlay org-speedbar-restriction-lock-overlay
  15093. (point-at-bol) (point-at-eol))
  15094. (setq current-prefix-arg nil)
  15095. (org-agenda-maybe-redo)))
  15096. (eval-after-load "speedbar"
  15097. '(progn
  15098. (speedbar-add-supported-extension ".org")
  15099. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15100. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15101. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15102. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15103. (add-hook 'speedbar-visiting-tag-hook
  15104. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15105. ;;; Fixes and Hacks for problems with other packages
  15106. ;; Make flyspell not check words in links, to not mess up our keymap
  15107. (defun org-mode-flyspell-verify ()
  15108. "Don't let flyspell put overlays at active buttons."
  15109. (and (not (get-text-property (point) 'keymap))
  15110. (not (get-text-property (point) 'org-no-flyspell))))
  15111. (defun org-remove-flyspell-overlays-in (beg end)
  15112. "Remove flyspell overlays in region."
  15113. (and (org-bound-and-true-p flyspell-mode)
  15114. (fboundp 'flyspell-delete-region-overlays)
  15115. (flyspell-delete-region-overlays beg end))
  15116. (add-text-properties beg end '(org-no-flyspell t)))
  15117. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15118. (eval-after-load "bookmark"
  15119. '(if (boundp 'bookmark-after-jump-hook)
  15120. ;; We can use the hook
  15121. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15122. ;; Hook not available, use advice
  15123. (defadvice bookmark-jump (after org-make-visible activate)
  15124. "Make the position visible."
  15125. (org-bookmark-jump-unhide))))
  15126. ;; Make sure saveplace show the location if it was hidden
  15127. (eval-after-load "saveplace"
  15128. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15129. "Make the position visible."
  15130. (org-bookmark-jump-unhide)))
  15131. (defun org-bookmark-jump-unhide ()
  15132. "Unhide the current position, to show the bookmark location."
  15133. (and (org-mode-p)
  15134. (or (org-invisible-p)
  15135. (save-excursion (goto-char (max (point-min) (1- (point))))
  15136. (org-invisible-p)))
  15137. (org-show-context 'bookmark-jump)))
  15138. ;; Make session.el ignore our circular variable
  15139. (eval-after-load "session"
  15140. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15141. ;;;; Experimental code
  15142. (defun org-closed-in-range ()
  15143. "Sparse tree of items closed in a certain time range.
  15144. Still experimental, may disappear in the future."
  15145. (interactive)
  15146. ;; Get the time interval from the user.
  15147. (let* ((time1 (time-to-seconds
  15148. (org-read-date nil 'to-time nil "Starting date: ")))
  15149. (time2 (time-to-seconds
  15150. (org-read-date nil 'to-time nil "End date:")))
  15151. ;; callback function
  15152. (callback (lambda ()
  15153. (let ((time
  15154. (time-to-seconds
  15155. (apply 'encode-time
  15156. (org-parse-time-string
  15157. (match-string 1))))))
  15158. ;; check if time in interval
  15159. (and (>= time time1) (<= time time2))))))
  15160. ;; make tree, check each match with the callback
  15161. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15162. ;;;; Finish up
  15163. (provide 'org)
  15164. (run-hooks 'org-load-hook)
  15165. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15166. ;;; org.el ends here