org.el 618 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587
  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 interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  174. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  175. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  176. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  177. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  178. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  179. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  180. (const :tag "C R: Computation using the R language" org-R)
  181. (const :tag "C registry: A registry for Org links" org-registry)
  182. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  183. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  184. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  185. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  186. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  187. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  188. (defcustom org-support-shift-select nil
  189. "Non-nil means, make shift-cursor commands select text when possible.
  190. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  191. selecting a region, or enlarge thusly regions started in this way.
  192. In Org-mode, in special contexts, these same keys are used for other
  193. purposes, important enough to compete with shift selection. Org tries
  194. to balance these needs by supporting `shift-select-mode' outside these
  195. special contexts, under control of this variable.
  196. The default of this variable is nil, to avoid confusing behavior. Shifted
  197. cursor keys will then execute Org commands in the following contexts:
  198. - on a headline, changing TODO state (left/right) and priority (up/down)
  199. - on a time stamp, changing the time
  200. - in a plain list item, changing the bullet type
  201. - in a property definition line, switching between allowed values
  202. - in the BEGIN line of a clock table (changing the time block).
  203. Outside these contexts, the commands will throw an error.
  204. When this variable is t and the cursor is not in a special context,
  205. Org-mode will support shift-selection for making and enlarging regions.
  206. To make this more effective, the bullet cycling will no longer happen
  207. anywhere in an item line, but only if the cursor is exactly on the bullet.
  208. If you set this variable to the symbol `always', then the keys
  209. will not be special in headlines, property lines, and item lines, to make
  210. shift selection work there as well. If this is what you want, you can
  211. use the following alternative commands: `C-c C-t' and `C-c ,' to
  212. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  213. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  214. edited by hand or in column view.
  215. However, when the cursor is on a timestamp, shift-cursor commands
  216. will still edit the time stamp - this is just too good to give up.
  217. XEmacs user should have this variable set to nil, because shift-select-mode
  218. is Emacs 23 only."
  219. :group 'org
  220. :type '(choice
  221. (const :tag "Never" nil)
  222. (const :tag "When outside special context" t)
  223. (const :tag "Everywhere except timestamps" always)))
  224. (defgroup org-startup nil
  225. "Options concerning startup of Org-mode."
  226. :tag "Org Startup"
  227. :group 'org)
  228. (defcustom org-startup-folded t
  229. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  230. This can also be configured on a per-file basis by adding one of
  231. the following lines anywhere in the buffer:
  232. #+STARTUP: fold
  233. #+STARTUP: nofold
  234. #+STARTUP: content"
  235. :group 'org-startup
  236. :type '(choice
  237. (const :tag "nofold: show all" nil)
  238. (const :tag "fold: overview" t)
  239. (const :tag "content: all headlines" content)))
  240. (defcustom org-startup-truncated t
  241. "Non-nil means, entering Org-mode will set `truncate-lines'.
  242. This is useful since some lines containing links can be very long and
  243. uninteresting. Also tables look terrible when wrapped."
  244. :group 'org-startup
  245. :type 'boolean)
  246. (defcustom org-startup-align-all-tables nil
  247. "Non-nil means, align all tables when visiting a file.
  248. This is useful when the column width in tables is forced with <N> cookies
  249. in table fields. Such tables will look correct only after the first re-align.
  250. This can also be configured on a per-file basis by adding one of
  251. the following lines anywhere in the buffer:
  252. #+STARTUP: align
  253. #+STARTUP: noalign"
  254. :group 'org-startup
  255. :type 'boolean)
  256. (defcustom org-insert-mode-line-in-empty-file nil
  257. "Non-nil means insert the first line setting Org-mode in empty files.
  258. When the function `org-mode' is called interactively in an empty file, this
  259. normally means that the file name does not automatically trigger Org-mode.
  260. To ensure that the file will always be in Org-mode in the future, a
  261. line enforcing Org-mode will be inserted into the buffer, if this option
  262. has been set."
  263. :group 'org-startup
  264. :type 'boolean)
  265. (defcustom org-replace-disputed-keys nil
  266. "Non-nil means use alternative key bindings for some keys.
  267. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  268. These keys are also used by other packages like shift-selection-mode'
  269. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  270. If you want to use Org-mode together with one of these other modes,
  271. or more generally if you would like to move some Org-mode commands to
  272. other keys, set this variable and configure the keys with the variable
  273. `org-disputed-keys'.
  274. This option is only relevant at load-time of Org-mode, and must be set
  275. *before* org.el is loaded. Changing it requires a restart of Emacs to
  276. become effective."
  277. :group 'org-startup
  278. :type 'boolean)
  279. (defcustom org-use-extra-keys nil
  280. "Non-nil means use extra key sequence definitions for certain
  281. commands. This happens automatically if you run XEmacs or if
  282. window-system is nil. This variable lets you do the same
  283. manually. You must set it before loading org.
  284. Example: on Carbon Emacs 22 running graphically, with an external
  285. keyboard on a Powerbook, the default way of setting M-left might
  286. not work for either Alt or ESC. Setting this variable will make
  287. it work for ESC."
  288. :group 'org-startup
  289. :type 'boolean)
  290. (if (fboundp 'defvaralias)
  291. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  292. (defcustom org-disputed-keys
  293. '(([(shift up)] . [(meta p)])
  294. ([(shift down)] . [(meta n)])
  295. ([(shift left)] . [(meta -)])
  296. ([(shift right)] . [(meta +)])
  297. ([(control shift right)] . [(meta shift +)])
  298. ([(control shift left)] . [(meta shift -)]))
  299. "Keys for which Org-mode and other modes compete.
  300. This is an alist, cars are the default keys, second element specifies
  301. the alternative to use when `org-replace-disputed-keys' is t.
  302. Keys can be specified in any syntax supported by `define-key'.
  303. The value of this option takes effect only at Org-mode's startup,
  304. therefore you'll have to restart Emacs to apply it after changing."
  305. :group 'org-startup
  306. :type 'alist)
  307. (defun org-key (key)
  308. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  309. Or return the original if not disputed."
  310. (if org-replace-disputed-keys
  311. (let* ((nkey (key-description key))
  312. (x (org-find-if (lambda (x)
  313. (equal (key-description (car x)) nkey))
  314. org-disputed-keys)))
  315. (if x (cdr x) key))
  316. key))
  317. (defun org-find-if (predicate seq)
  318. (catch 'exit
  319. (while seq
  320. (if (funcall predicate (car seq))
  321. (throw 'exit (car seq))
  322. (pop seq)))))
  323. (defun org-defkey (keymap key def)
  324. "Define a key, possibly translated, as returned by `org-key'."
  325. (define-key keymap (org-key key) def))
  326. (defcustom org-ellipsis nil
  327. "The ellipsis to use in the Org-mode outline.
  328. When nil, just use the standard three dots. When a string, use that instead,
  329. When a face, use the standard 3 dots, but with the specified face.
  330. The change affects only Org-mode (which will then use its own display table).
  331. Changing this requires executing `M-x org-mode' in a buffer to become
  332. effective."
  333. :group 'org-startup
  334. :type '(choice (const :tag "Default" nil)
  335. (face :tag "Face" :value org-warning)
  336. (string :tag "String" :value "...#")))
  337. (defvar org-display-table nil
  338. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  339. (defgroup org-keywords nil
  340. "Keywords in Org-mode."
  341. :tag "Org Keywords"
  342. :group 'org)
  343. (defcustom org-deadline-string "DEADLINE:"
  344. "String to mark deadline entries.
  345. A deadline is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-deadline].
  348. Changes become only effective after restarting Emacs."
  349. :group 'org-keywords
  350. :type 'string)
  351. (defcustom org-scheduled-string "SCHEDULED:"
  352. "String to mark scheduled TODO entries.
  353. A schedule is this string, followed by a time stamp. Should be a word,
  354. terminated by a colon. You can insert a schedule keyword and
  355. a timestamp with \\[org-schedule].
  356. Changes become only effective after restarting Emacs."
  357. :group 'org-keywords
  358. :type 'string)
  359. (defcustom org-closed-string "CLOSED:"
  360. "String used as the prefix for timestamps logging closing a TODO entry."
  361. :group 'org-keywords
  362. :type 'string)
  363. (defcustom org-clock-string "CLOCK:"
  364. "String used as prefix for timestamps clocking work hours on an item."
  365. :group 'org-keywords
  366. :type 'string)
  367. (defcustom org-comment-string "COMMENT"
  368. "Entries starting with this keyword will never be exported.
  369. An entry can be toggled between COMMENT and normal with
  370. \\[org-toggle-comment].
  371. Changes become only effective after restarting Emacs."
  372. :group 'org-keywords
  373. :type 'string)
  374. (defcustom org-quote-string "QUOTE"
  375. "Entries starting with this keyword will be exported in fixed-width font.
  376. Quoting applies only to the text in the entry following the headline, and does
  377. not extend beyond the next headline, even if that is lower level.
  378. An entry can be toggled between QUOTE and normal with
  379. \\[org-toggle-fixed-width-section]."
  380. :group 'org-keywords
  381. :type 'string)
  382. (defconst org-repeat-re
  383. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  384. "Regular expression for specifying repeated events.
  385. After a match, group 1 contains the repeat expression.")
  386. (defgroup org-structure nil
  387. "Options concerning the general structure of Org-mode files."
  388. :tag "Org Structure"
  389. :group 'org)
  390. (defgroup org-reveal-location nil
  391. "Options about how to make context of a location visible."
  392. :tag "Org Reveal Location"
  393. :group 'org-structure)
  394. (defconst org-context-choice
  395. '(choice
  396. (const :tag "Always" t)
  397. (const :tag "Never" nil)
  398. (repeat :greedy t :tag "Individual contexts"
  399. (cons
  400. (choice :tag "Context"
  401. (const agenda)
  402. (const org-goto)
  403. (const occur-tree)
  404. (const tags-tree)
  405. (const link-search)
  406. (const mark-goto)
  407. (const bookmark-jump)
  408. (const isearch)
  409. (const default))
  410. (boolean))))
  411. "Contexts for the reveal options.")
  412. (defcustom org-show-hierarchy-above '((default . t))
  413. "Non-nil means, show full hierarchy when revealing a location.
  414. Org-mode often shows locations in an org-mode file which might have
  415. been invisible before. When this is set, the hierarchy of headings
  416. above the exposed location is shown.
  417. Turning this off for example for sparse trees makes them very compact.
  418. Instead of t, this can also be an alist specifying this option for different
  419. contexts. Valid contexts are
  420. agenda when exposing an entry from the agenda
  421. org-goto when using the command `org-goto' on key C-c C-j
  422. occur-tree when using the command `org-occur' on key C-c /
  423. tags-tree when constructing a sparse tree based on tags matches
  424. link-search when exposing search matches associated with a link
  425. mark-goto when exposing the jump goal of a mark
  426. bookmark-jump when exposing a bookmark location
  427. isearch when exiting from an incremental search
  428. default default for all contexts not set explicitly"
  429. :group 'org-reveal-location
  430. :type org-context-choice)
  431. (defcustom org-show-following-heading '((default . nil))
  432. "Non-nil means, show following heading when revealing a location.
  433. Org-mode often shows locations in an org-mode file which might have
  434. been invisible before. When this is set, the heading following the
  435. match is shown.
  436. Turning this off for example for sparse trees makes them very compact,
  437. but makes it harder to edit the location of the match. In such a case,
  438. use the command \\[org-reveal] to show more context.
  439. Instead of t, this can also be an alist specifying this option for different
  440. contexts. See `org-show-hierarchy-above' for valid contexts."
  441. :group 'org-reveal-location
  442. :type org-context-choice)
  443. (defcustom org-show-siblings '((default . nil) (isearch t))
  444. "Non-nil means, show all sibling heading when revealing a location.
  445. Org-mode often shows locations in an org-mode file which might have
  446. been invisible before. When this is set, the sibling of the current entry
  447. heading are all made visible. If `org-show-hierarchy-above' is t,
  448. the same happens on each level of the hierarchy above the current entry.
  449. By default this is on for the isearch context, off for all other contexts.
  450. Turning this off for example for sparse trees makes them very compact,
  451. but makes it harder to edit the location of the match. In such a case,
  452. use the command \\[org-reveal] to show more context.
  453. Instead of t, this can also be an alist specifying this option for different
  454. contexts. See `org-show-hierarchy-above' for valid contexts."
  455. :group 'org-reveal-location
  456. :type org-context-choice)
  457. (defcustom org-show-entry-below '((default . nil))
  458. "Non-nil means, show the entry below a headline when revealing a location.
  459. Org-mode often shows locations in an org-mode file which might have
  460. been invisible before. When this is set, the text below the headline that is
  461. exposed is also shown.
  462. By default this is off for all contexts.
  463. Instead of t, this can also be an alist specifying this option for different
  464. contexts. See `org-show-hierarchy-above' for valid contexts."
  465. :group 'org-reveal-location
  466. :type org-context-choice)
  467. (defcustom org-indirect-buffer-display 'other-window
  468. "How should indirect tree buffers be displayed?
  469. This applies to indirect buffers created with the commands
  470. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  471. Valid values are:
  472. current-window Display in the current window
  473. other-window Just display in another window.
  474. dedicated-frame Create one new frame, and re-use it each time.
  475. new-frame Make a new frame each time. Note that in this case
  476. previously-made indirect buffers are kept, and you need to
  477. kill these buffers yourself."
  478. :group 'org-structure
  479. :group 'org-agenda-windows
  480. :type '(choice
  481. (const :tag "In current window" current-window)
  482. (const :tag "In current frame, other window" other-window)
  483. (const :tag "Each time a new frame" new-frame)
  484. (const :tag "One dedicated frame" dedicated-frame)))
  485. (defgroup org-cycle nil
  486. "Options concerning visibility cycling in Org-mode."
  487. :tag "Org Cycle"
  488. :group 'org-structure)
  489. (defcustom org-cycle-max-level nil
  490. "Maximum level which should still be subject to visibility cycling.
  491. Levels higher than this will, for cycling, be treated as text, not a headline.
  492. When `org-odd-levels-only' is set, a value of N in this variable actually
  493. means 2N-1 stars as the limiting headline.
  494. When nil, cycle all levels.
  495. Note that the limiting level of cycling is also influenced by
  496. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  497. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  498. than its value."
  499. :group 'org-cycle
  500. :type '(choice
  501. (const :tag "No limit" nil)
  502. (integer :tag "Maximum level")))
  503. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  504. "Names of drawers. Drawers are not opened by cycling on the headline above.
  505. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  506. this:
  507. :DRAWERNAME:
  508. .....
  509. :END:
  510. The drawer \"PROPERTIES\" is special for capturing properties through
  511. the property API.
  512. Drawers can be defined on the per-file basis with a line like:
  513. #+DRAWERS: HIDDEN STATE PROPERTIES"
  514. :group 'org-structure
  515. :group 'org-cycle
  516. :type '(repeat (string :tag "Drawer Name")))
  517. (defcustom org-hide-block-startup nil
  518. "Non-nil means, , entering Org-mode will fold all blocks.
  519. This can also be set in on a per-file basis with
  520. #+STARTUP: hideblocks
  521. #+STARTUP: showblocks"
  522. :group 'org-startup
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-global-at-bob nil
  526. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  527. This makes it possible to do global cycling without having to use S-TAB or
  528. C-u TAB. For this special case to work, the first line of the buffer
  529. must not be a headline - it may be empty or some other text. When used in
  530. this way, `org-cycle-hook' is disables temporarily, to make sure the
  531. cursor stays at the beginning of the buffer.
  532. When this option is nil, don't do anything special at the beginning
  533. of the buffer."
  534. :group 'org-cycle
  535. :type 'boolean)
  536. (defcustom org-cycle-emulate-tab t
  537. "Where should `org-cycle' emulate TAB.
  538. nil Never
  539. white Only in completely white lines
  540. whitestart Only at the beginning of lines, before the first non-white char
  541. t Everywhere except in headlines
  542. exc-hl-bol Everywhere except at the start of a headline
  543. If TAB is used in a place where it does not emulate TAB, the current subtree
  544. visibility is cycled."
  545. :group 'org-cycle
  546. :type '(choice (const :tag "Never" nil)
  547. (const :tag "Only in completely white lines" white)
  548. (const :tag "Before first char in a line" whitestart)
  549. (const :tag "Everywhere except in headlines" t)
  550. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  551. ))
  552. (defcustom org-cycle-separator-lines 2
  553. "Number of empty lines needed to keep an empty line between collapsed trees.
  554. If you leave an empty line between the end of a subtree and the following
  555. headline, this empty line is hidden when the subtree is folded.
  556. Org-mode will leave (exactly) one empty line visible if the number of
  557. empty lines is equal or larger to the number given in this variable.
  558. So the default 2 means, at least 2 empty lines after the end of a subtree
  559. are needed to produce free space between a collapsed subtree and the
  560. following headline.
  561. Special case: when 0, never leave empty lines in collapsed view."
  562. :group 'org-cycle
  563. :type 'integer)
  564. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  565. (defcustom org-pre-cycle-hook nil
  566. "Hook that is run before visibility cycling is happening.
  567. The function(s) in this hook must accept a single argument which indicates
  568. the new state that will be set right after running this hook. The
  569. argument is a symbol. Before a global state change, it can have the values
  570. `overview', `content', or `all'. Before a local state change, it can have
  571. the values `folded', `children', or `subtree'."
  572. :group 'org-cycle
  573. :type 'hook)
  574. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  575. org-cycle-hide-drawers
  576. org-cycle-show-empty-lines
  577. org-optimize-window-after-visibility-change)
  578. "Hook that is run after `org-cycle' has changed the buffer visibility.
  579. The function(s) in this hook must accept a single argument which indicates
  580. the new state that was set by the most recent `org-cycle' command. The
  581. argument is a symbol. After a global state change, it can have the values
  582. `overview', `content', or `all'. After a local state change, it can have
  583. the values `folded', `children', or `subtree'."
  584. :group 'org-cycle
  585. :type 'hook)
  586. (defgroup org-edit-structure nil
  587. "Options concerning structure editing in Org-mode."
  588. :tag "Org Edit Structure"
  589. :group 'org-structure)
  590. (defcustom org-odd-levels-only nil
  591. "Non-nil means, skip even levels and only use odd levels for the outline.
  592. This has the effect that two stars are being added/taken away in
  593. promotion/demotion commands. It also influences how levels are
  594. handled by the exporters.
  595. Changing it requires restart of `font-lock-mode' to become effective
  596. for fontification also in regions already fontified.
  597. You may also set this on a per-file basis by adding one of the following
  598. lines to the buffer:
  599. #+STARTUP: odd
  600. #+STARTUP: oddeven"
  601. :group 'org-edit-structure
  602. :group 'org-font-lock
  603. :type 'boolean)
  604. (defcustom org-adapt-indentation t
  605. "Non-nil means, adapt indentation when promoting and demoting.
  606. When this is set and the *entire* text in an entry is indented, the
  607. indentation is increased by one space in a demotion command, and
  608. decreased by one in a promotion command. If any line in the entry
  609. body starts at column 0, indentation is not changed at all.
  610. This variable also influences how property drawers and planning
  611. information is inserted. When t, these lines drawers will be inserted
  612. indented. When nil, they will not be indented."
  613. :group 'org-edit-structure
  614. :type 'boolean)
  615. (defcustom org-special-ctrl-a/e nil
  616. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  617. When t, `C-a' will bring back the cursor to the beginning of the
  618. headline text, i.e. after the stars and after a possible TODO keyword.
  619. In an item, this will be the position after the bullet.
  620. When the cursor is already at that position, another `C-a' will bring
  621. it to the beginning of the line.
  622. `C-e' will jump to the end of the headline, ignoring the presence of tags
  623. in the headline. A second `C-e' will then jump to the true end of the
  624. line, after any tags.
  625. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  626. and only a directly following, identical keypress will bring the cursor
  627. to the special positions.
  628. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  629. set separately."
  630. :group 'org-edit-structure
  631. :type '(choice
  632. (const :tag "off" nil)
  633. (const :tag "after stars/bullet and before tags first" t)
  634. (const :tag "true line boundary first" reversed)
  635. (cons :tag "Set C-a and C-e separately"
  636. (choice :tag "Special C-a"
  637. (const :tag "off" nil)
  638. (const :tag "after stars/bullet first" t)
  639. (const :tag "before stars/bullet first" reversed))
  640. (choice :tag "Special C-e"
  641. (const :tag "off" nil)
  642. (const :tag "before tags first" t)
  643. (const :tag "after tags first" reversed)))))
  644. (if (fboundp 'defvaralias)
  645. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  646. (defcustom org-special-ctrl-k nil
  647. "Non-nil means `C-k' will behave specially in headlines.
  648. When nil, `C-k' will call the default `kill-line' command.
  649. When t, the following will happen while the cursor is in the headline:
  650. - When the cursor is at the beginning of a headline, kill the entire
  651. line and possible the folded subtree below the line.
  652. - When in the middle of the headline text, kill the headline up to the tags.
  653. - When after the headline text, kill the tags."
  654. :group 'org-edit-structure
  655. :type 'boolean)
  656. (defcustom org-yank-folded-subtrees t
  657. "Non-nil means, when yanking subtrees, fold them.
  658. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  659. it starts with a heading and all other headings in it are either children
  660. or siblings, then fold all the subtrees. However, do this only if no
  661. text after the yank would be swallowed into a folded tree by this action."
  662. :group 'org-edit-structure
  663. :type 'boolean)
  664. (defcustom org-yank-adjusted-subtrees nil
  665. "Non-nil means, when yanking subtrees, adjust the level.
  666. With this setting, `org-paste-subtree' is used to insert the subtree, see
  667. this function for details."
  668. :group 'org-edit-structure
  669. :type 'boolean)
  670. (defcustom org-M-RET-may-split-line '((default . t))
  671. "Non-nil means, M-RET will split the line at the cursor position.
  672. When nil, it will go to the end of the line before making a
  673. new line.
  674. You may also set this option in a different way for different
  675. contexts. Valid contexts are:
  676. headline when creating a new headline
  677. item when creating a new item
  678. table in a table field
  679. default the value to be used for all contexts not explicitly
  680. customized"
  681. :group 'org-structure
  682. :group 'org-table
  683. :type '(choice
  684. (const :tag "Always" t)
  685. (const :tag "Never" nil)
  686. (repeat :greedy t :tag "Individual contexts"
  687. (cons
  688. (choice :tag "Context"
  689. (const headline)
  690. (const item)
  691. (const table)
  692. (const default))
  693. (boolean)))))
  694. (defcustom org-insert-heading-respect-content nil
  695. "Non-nil means, insert new headings after the current subtree.
  696. When nil, the new heading is created directly after the current line.
  697. The commands \\[org-insert-heading-respect-content] and
  698. \\[org-insert-todo-heading-respect-content] turn this variable on
  699. for the duration of the command."
  700. :group 'org-structure
  701. :type 'boolean)
  702. (defcustom org-blank-before-new-entry '((heading . auto)
  703. (plain-list-item . auto))
  704. "Should `org-insert-heading' leave a blank line before new heading/item?
  705. The value is an alist, with `heading' and `plain-list-item' as car,
  706. and a boolean flag as cdr. For plain lists, if the variable
  707. `org-empty-line-terminates-plain-lists' is set, the setting here
  708. is ignored and no empty line is inserted, to keep the list in tact."
  709. :group 'org-edit-structure
  710. :type '(list
  711. (cons (const heading)
  712. (choice (const :tag "Never" nil)
  713. (const :tag "Always" t)
  714. (const :tag "Auto" auto)))
  715. (cons (const plain-list-item)
  716. (choice (const :tag "Never" nil)
  717. (const :tag "Always" t)
  718. (const :tag "Auto" auto)))))
  719. (defcustom org-insert-heading-hook nil
  720. "Hook being run after inserting a new heading."
  721. :group 'org-edit-structure
  722. :type 'hook)
  723. (defcustom org-enable-fixed-width-editor t
  724. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  725. This currently only means, they are never auto-wrapped.
  726. When nil, such lines will be treated like ordinary lines.
  727. See also the QUOTE keyword."
  728. :group 'org-edit-structure
  729. :type 'boolean)
  730. (defcustom org-goto-auto-isearch t
  731. "Non-nil means, typing characters in org-goto starts incremental search."
  732. :group 'org-edit-structure
  733. :type 'boolean)
  734. (defgroup org-sparse-trees nil
  735. "Options concerning sparse trees in Org-mode."
  736. :tag "Org Sparse Trees"
  737. :group 'org-structure)
  738. (defcustom org-highlight-sparse-tree-matches t
  739. "Non-nil means, highlight all matches that define a sparse tree.
  740. The highlights will automatically disappear the next time the buffer is
  741. changed by an edit command."
  742. :group 'org-sparse-trees
  743. :type 'boolean)
  744. (defcustom org-remove-highlights-with-change t
  745. "Non-nil means, any change to the buffer will remove temporary highlights.
  746. Such highlights are created by `org-occur' and `org-clock-display'.
  747. When nil, `C-c C-c needs to be used to get rid of the highlights.
  748. The highlights created by `org-preview-latex-fragment' always need
  749. `C-c C-c' to be removed."
  750. :group 'org-sparse-trees
  751. :group 'org-time
  752. :type 'boolean)
  753. (defcustom org-occur-hook '(org-first-headline-recenter)
  754. "Hook that is run after `org-occur' has constructed a sparse tree.
  755. This can be used to recenter the window to show as much of the structure
  756. as possible."
  757. :group 'org-sparse-trees
  758. :type 'hook)
  759. (defgroup org-imenu-and-speedbar nil
  760. "Options concerning imenu and speedbar in Org-mode."
  761. :tag "Org Imenu and Speedbar"
  762. :group 'org-structure)
  763. (defcustom org-imenu-depth 2
  764. "The maximum level for Imenu access to Org-mode headlines.
  765. This also applied for speedbar access."
  766. :group 'org-imenu-and-speedbar
  767. :type 'integer)
  768. (defgroup org-table nil
  769. "Options concerning tables in Org-mode."
  770. :tag "Org Table"
  771. :group 'org)
  772. (defcustom org-enable-table-editor 'optimized
  773. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  774. When nil, such lines will be treated like ordinary lines.
  775. When equal to the symbol `optimized', the table editor will be optimized to
  776. do the following:
  777. - Automatic overwrite mode in front of whitespace in table fields.
  778. This makes the structure of the table stay in tact as long as the edited
  779. field does not exceed the column width.
  780. - Minimize the number of realigns. Normally, the table is aligned each time
  781. TAB or RET are pressed to move to another field. With optimization this
  782. happens only if changes to a field might have changed the column width.
  783. Optimization requires replacing the functions `self-insert-command',
  784. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  785. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  786. very good at guessing when a re-align will be necessary, but you can always
  787. force one with \\[org-ctrl-c-ctrl-c].
  788. If you would like to use the optimized version in Org-mode, but the
  789. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  790. This variable can be used to turn on and off the table editor during a session,
  791. but in order to toggle optimization, a restart is required.
  792. See also the variable `org-table-auto-blank-field'."
  793. :group 'org-table
  794. :type '(choice
  795. (const :tag "off" nil)
  796. (const :tag "on" t)
  797. (const :tag "on, optimized" optimized)))
  798. (defcustom org-self-insert-cluster-for-undo t
  799. "Non-nil means cluster self-insert commands for undo when possible.
  800. If this is set, then, like in the Emacs command loop, 20 consequtive
  801. characters will be undone together.
  802. This is configurable, because there is some impact on typing performance."
  803. :group 'org-table
  804. :type 'boolean)
  805. (defcustom org-table-tab-recognizes-table.el t
  806. "Non-nil means, TAB will automatically notice a table.el table.
  807. When it sees such a table, it moves point into it and - if necessary -
  808. calls `table-recognize-table'."
  809. :group 'org-table-editing
  810. :type 'boolean)
  811. (defgroup org-link nil
  812. "Options concerning links in Org-mode."
  813. :tag "Org Link"
  814. :group 'org)
  815. (defvar org-link-abbrev-alist-local nil
  816. "Buffer-local version of `org-link-abbrev-alist', which see.
  817. The value of this is taken from the #+LINK lines.")
  818. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  819. (defcustom org-link-abbrev-alist nil
  820. "Alist of link abbreviations.
  821. The car of each element is a string, to be replaced at the start of a link.
  822. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  823. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  824. [[linkkey:tag][description]]
  825. The 'linkkey' must be a word word, starting with a letter, followed
  826. by letters, numbers, '-' or '_'.
  827. If REPLACE is a string, the tag will simply be appended to create the link.
  828. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  829. the placeholder \"%h\" will cause a url-encoded version of the tag to
  830. be inserted at that point (see the function `url-hexify-string').
  831. REPLACE may also be a function that will be called with the tag as the
  832. only argument to create the link, which should be returned as a string.
  833. See the manual for examples."
  834. :group 'org-link
  835. :type '(repeat
  836. (cons
  837. (string :tag "Protocol")
  838. (choice
  839. (string :tag "Format")
  840. (function)))))
  841. (defcustom org-descriptive-links t
  842. "Non-nil means, hide link part and only show description of bracket links.
  843. Bracket links are like [[link][description]]. This variable sets the initial
  844. state in new org-mode buffers. The setting can then be toggled on a
  845. per-buffer basis from the Org->Hyperlinks menu."
  846. :group 'org-link
  847. :type 'boolean)
  848. (defcustom org-link-file-path-type 'adaptive
  849. "How the path name in file links should be stored.
  850. Valid values are:
  851. relative Relative to the current directory, i.e. the directory of the file
  852. into which the link is being inserted.
  853. absolute Absolute path, if possible with ~ for home directory.
  854. noabbrev Absolute path, no abbreviation of home directory.
  855. adaptive Use relative path for files in the current directory and sub-
  856. directories of it. For other files, use an absolute path."
  857. :group 'org-link
  858. :type '(choice
  859. (const relative)
  860. (const absolute)
  861. (const noabbrev)
  862. (const adaptive)))
  863. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  864. "Types of links that should be activated in Org-mode files.
  865. This is a list of symbols, each leading to the activation of a certain link
  866. type. In principle, it does not hurt to turn on most link types - there may
  867. be a small gain when turning off unused link types. The types are:
  868. bracket The recommended [[link][description]] or [[link]] links with hiding.
  869. angular Links in angular brackets that may contain whitespace like
  870. <bbdb:Carsten Dominik>.
  871. plain Plain links in normal text, no whitespace, like http://google.com.
  872. radio Text that is matched by a radio target, see manual for details.
  873. tag Tag settings in a headline (link to tag search).
  874. date Time stamps (link to calendar).
  875. footnote Footnote labels.
  876. Changing this variable requires a restart of Emacs to become effective."
  877. :group 'org-link
  878. :type '(set :greedy t
  879. (const :tag "Double bracket links (new style)" bracket)
  880. (const :tag "Angular bracket links (old style)" angular)
  881. (const :tag "Plain text links" plain)
  882. (const :tag "Radio target matches" radio)
  883. (const :tag "Tags" tag)
  884. (const :tag "Timestamps" date)
  885. (const :tag "Footnotes" footnote)))
  886. (defcustom org-make-link-description-function nil
  887. "Function to use to generate link descriptions from links. If
  888. nil the link location will be used. This function must take two
  889. parameters; the first is the link and the second the description
  890. org-insert-link has generated, and should return the description
  891. to use."
  892. :group 'org-link
  893. :type 'function)
  894. (defgroup org-link-store nil
  895. "Options concerning storing links in Org-mode."
  896. :tag "Org Store Link"
  897. :group 'org-link)
  898. (defcustom org-email-link-description-format "Email %c: %.30s"
  899. "Format of the description part of a link to an email or usenet message.
  900. The following %-escapes will be replaced by corresponding information:
  901. %F full \"From\" field
  902. %f name, taken from \"From\" field, address if no name
  903. %T full \"To\" field
  904. %t first name in \"To\" field, address if no name
  905. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  906. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  907. %s subject
  908. %m message-id.
  909. You may use normal field width specification between the % and the letter.
  910. This is for example useful to limit the length of the subject.
  911. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  912. :group 'org-link-store
  913. :type 'string)
  914. (defcustom org-from-is-user-regexp
  915. (let (r1 r2)
  916. (when (and user-mail-address (not (string= user-mail-address "")))
  917. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  918. (when (and user-full-name (not (string= user-full-name "")))
  919. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  920. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  921. "Regexp matched against the \"From:\" header of an email or usenet message.
  922. It should match if the message is from the user him/herself."
  923. :group 'org-link-store
  924. :type 'regexp)
  925. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  926. "Non-nil means, storing a link to an Org file will use entry IDs.
  927. Note that before this variable is even considered, org-id must be loaded,
  928. to please customize `org-modules' and turn it on.
  929. The variable can have the following values:
  930. t Create an ID if needed to make a link to the current entry.
  931. create-if-interactive
  932. If `org-store-link' is called directly (interactively, as a user
  933. command), do create an ID to support the link. But when doing the
  934. job for remember, only use the ID if it already exists. The
  935. purpose of this setting is to avoid proliferation of unwanted
  936. IDs, just because you happen to be in an Org file when you
  937. call `org-remember' that automatically and preemptively
  938. creates a link. If you do want to get an ID link in a remember
  939. template to an entry not having an ID, create it first by
  940. explicitly creating a link to it, using `C-c C-l' first.
  941. use-existing
  942. Use existing ID, do not create one.
  943. nil Never use an ID to make a link, instead link using a text search for
  944. the headline text."
  945. :group 'org-link-store
  946. :type '(choice
  947. (const :tag "Create ID to make link" t)
  948. (const :tag "Create if storing link interactively"
  949. create-if-interactive)
  950. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  951. create-if-interactive-and-no-custom-id)
  952. (const :tag "Only use existing" use-existing)
  953. (const :tag "Do not use ID to create link" nil)))
  954. (defcustom org-context-in-file-links t
  955. "Non-nil means, file links from `org-store-link' contain context.
  956. A search string will be added to the file name with :: as separator and
  957. used to find the context when the link is activated by the command
  958. `org-open-at-point'.
  959. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  960. negates this setting for the duration of the command."
  961. :group 'org-link-store
  962. :type 'boolean)
  963. (defcustom org-keep-stored-link-after-insertion nil
  964. "Non-nil means, keep link in list for entire session.
  965. The command `org-store-link' adds a link pointing to the current
  966. location to an internal list. These links accumulate during a session.
  967. The command `org-insert-link' can be used to insert links into any
  968. Org-mode file (offering completion for all stored links). When this
  969. option is nil, every link which has been inserted once using \\[org-insert-link]
  970. will be removed from the list, to make completing the unused links
  971. more efficient."
  972. :group 'org-link-store
  973. :type 'boolean)
  974. (defgroup org-link-follow nil
  975. "Options concerning following links in Org-mode."
  976. :tag "Org Follow Link"
  977. :group 'org-link)
  978. (defcustom org-link-translation-function nil
  979. "Function to translate links with different syntax to Org syntax.
  980. This can be used to translate links created for example by the Planner
  981. or emacs-wiki packages to Org syntax.
  982. The function must accept two parameters, a TYPE containing the link
  983. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  984. which is everything after the link protocol. It should return a cons
  985. with possibly modified values of type and path.
  986. Org contains a function for this, so if you set this variable to
  987. `org-translate-link-from-planner', you should be able follow many
  988. links created by planner."
  989. :group 'org-link-follow
  990. :type 'function)
  991. (defcustom org-follow-link-hook nil
  992. "Hook that is run after a link has been followed."
  993. :group 'org-link-follow
  994. :type 'hook)
  995. (defcustom org-tab-follows-link nil
  996. "Non-nil means, on links TAB will follow the link.
  997. Needs to be set before org.el is loaded.
  998. This really should not be used, it does not make sense, and the
  999. implementation is bad."
  1000. :group 'org-link-follow
  1001. :type 'boolean)
  1002. (defcustom org-return-follows-link nil
  1003. "Non-nil means, on links RET will follow the link.
  1004. Needs to be set before org.el is loaded."
  1005. :group 'org-link-follow
  1006. :type 'boolean)
  1007. (defcustom org-mouse-1-follows-link
  1008. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1009. "Non-nil means, mouse-1 on a link will follow the link.
  1010. A longer mouse click will still set point. Does not work on XEmacs.
  1011. Needs to be set before org.el is loaded."
  1012. :group 'org-link-follow
  1013. :type 'boolean)
  1014. (defcustom org-mark-ring-length 4
  1015. "Number of different positions to be recorded in the ring
  1016. Changing this requires a restart of Emacs to work correctly."
  1017. :group 'org-link-follow
  1018. :type 'integer)
  1019. (defcustom org-link-frame-setup
  1020. '((vm . vm-visit-folder-other-frame)
  1021. (gnus . gnus-other-frame)
  1022. (file . find-file-other-window))
  1023. "Setup the frame configuration for following links.
  1024. When following a link with Emacs, it may often be useful to display
  1025. this link in another window or frame. This variable can be used to
  1026. set this up for the different types of links.
  1027. For VM, use any of
  1028. `vm-visit-folder'
  1029. `vm-visit-folder-other-frame'
  1030. For Gnus, use any of
  1031. `gnus'
  1032. `gnus-other-frame'
  1033. `org-gnus-no-new-news'
  1034. For FILE, use any of
  1035. `find-file'
  1036. `find-file-other-window'
  1037. `find-file-other-frame'
  1038. For the calendar, use the variable `calendar-setup'.
  1039. For BBDB, it is currently only possible to display the matches in
  1040. another window."
  1041. :group 'org-link-follow
  1042. :type '(list
  1043. (cons (const vm)
  1044. (choice
  1045. (const vm-visit-folder)
  1046. (const vm-visit-folder-other-window)
  1047. (const vm-visit-folder-other-frame)))
  1048. (cons (const gnus)
  1049. (choice
  1050. (const gnus)
  1051. (const gnus-other-frame)
  1052. (const org-gnus-no-new-news)))
  1053. (cons (const file)
  1054. (choice
  1055. (const find-file)
  1056. (const find-file-other-window)
  1057. (const find-file-other-frame)))))
  1058. (defcustom org-display-internal-link-with-indirect-buffer nil
  1059. "Non-nil means, use indirect buffer to display infile links.
  1060. Activating internal links (from one location in a file to another location
  1061. in the same file) normally just jumps to the location. When the link is
  1062. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1063. another window. When this option is set, the other window actually displays
  1064. an indirect buffer clone of the current buffer, to avoid any visibility
  1065. changes to the current buffer."
  1066. :group 'org-link-follow
  1067. :type 'boolean)
  1068. (defcustom org-open-non-existing-files nil
  1069. "Non-nil means, `org-open-file' will open non-existing files.
  1070. When nil, an error will be generated.
  1071. This variable applies only to external applications because they
  1072. might choke on non-existing files. If the link is to a file that
  1073. will be openend in Emacs, the variable is ignored."
  1074. :group 'org-link-follow
  1075. :type 'boolean)
  1076. (defcustom org-open-directory-means-index-dot-org nil
  1077. "Non-nil means, a link to a directory really means to index.org.
  1078. When nil, following a directory link will run dired or open a finder/explorer
  1079. window on that directory."
  1080. :group 'org-link-follow
  1081. :type 'boolean)
  1082. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1083. "Function and arguments to call for following mailto links.
  1084. This is a list with the first element being a lisp function, and the
  1085. remaining elements being arguments to the function. In string arguments,
  1086. %a will be replaced by the address, and %s will be replaced by the subject
  1087. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1088. :group 'org-link-follow
  1089. :type '(choice
  1090. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1091. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1092. (const :tag "message-mail" (message-mail "%a" "%s"))
  1093. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1094. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1095. "Non-nil means, ask for confirmation before executing shell links.
  1096. Shell links can be dangerous: just think about a link
  1097. [[shell:rm -rf ~/*][Google Search]]
  1098. This link would show up in your Org-mode document as \"Google Search\",
  1099. but really it would remove your entire home directory.
  1100. Therefore we advise against setting this variable to nil.
  1101. Just change it to `y-or-n-p' if you want to confirm with a
  1102. single keystroke rather than having to type \"yes\"."
  1103. :group 'org-link-follow
  1104. :type '(choice
  1105. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1106. (const :tag "with y-or-n (faster)" y-or-n-p)
  1107. (const :tag "no confirmation (dangerous)" nil)))
  1108. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1109. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1110. Elisp links can be dangerous: just think about a link
  1111. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1112. This link would show up in your Org-mode document as \"Google Search\",
  1113. but really it would remove your entire home directory.
  1114. Therefore we advise against setting this variable to nil.
  1115. Just change it to `y-or-n-p' if you want to confirm with a
  1116. single keystroke rather than having to type \"yes\"."
  1117. :group 'org-link-follow
  1118. :type '(choice
  1119. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1120. (const :tag "with y-or-n (faster)" y-or-n-p)
  1121. (const :tag "no confirmation (dangerous)" nil)))
  1122. (defconst org-file-apps-defaults-gnu
  1123. '((remote . emacs)
  1124. (system . mailcap)
  1125. (t . mailcap))
  1126. "Default file applications on a UNIX or GNU/Linux system.
  1127. See `org-file-apps'.")
  1128. (defconst org-file-apps-defaults-macosx
  1129. '((remote . emacs)
  1130. (t . "open %s")
  1131. (system . "open %s")
  1132. ("ps.gz" . "gv %s")
  1133. ("eps.gz" . "gv %s")
  1134. ("dvi" . "xdvi %s")
  1135. ("fig" . "xfig %s"))
  1136. "Default file applications on a MacOS X system.
  1137. The system \"open\" is known as a default, but we use X11 applications
  1138. for some files for which the OS does not have a good default.
  1139. See `org-file-apps'.")
  1140. (defconst org-file-apps-defaults-windowsnt
  1141. (list
  1142. '(remote . emacs)
  1143. (cons t
  1144. (list (if (featurep 'xemacs)
  1145. 'mswindows-shell-execute
  1146. 'w32-shell-execute)
  1147. "open" 'file))
  1148. (cons 'system
  1149. (list (if (featurep 'xemacs)
  1150. 'mswindows-shell-execute
  1151. 'w32-shell-execute)
  1152. "open" 'file)))
  1153. "Default file applications on a Windows NT system.
  1154. The system \"open\" is used for most files.
  1155. See `org-file-apps'.")
  1156. (defcustom org-file-apps
  1157. '(
  1158. (auto-mode . emacs)
  1159. ("\\.x?html?\\'" . default)
  1160. ("\\.pdf\\'" . default)
  1161. )
  1162. "External applications for opening `file:path' items in a document.
  1163. Org-mode uses system defaults for different file types, but
  1164. you can use this variable to set the application for a given file
  1165. extension. The entries in this list are cons cells where the car identifies
  1166. files and the cdr the corresponding command. Possible values for the
  1167. file identifier are
  1168. \"regex\" Regular expression matched against the file name. For backward
  1169. compatibility, this can also be a string with only alphanumeric
  1170. characters, which is then interpreted as an extension.
  1171. `directory' Matches a directory
  1172. `remote' Matches a remote file, accessible through tramp or efs.
  1173. Remote files most likely should be visited through Emacs
  1174. because external applications cannot handle such paths.
  1175. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1176. so all files Emacs knows how to handle. Using this with
  1177. command `emacs' will open most files in Emacs. Beware that this
  1178. will also open html files inside Emacs, unless you add
  1179. (\"html\" . default) to the list as well.
  1180. t Default for files not matched by any of the other options.
  1181. `system' The system command to open files, like `open' on Windows
  1182. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1183. that will be selected if you call `C-c C-o' with a double
  1184. `C-u C-u' prefix.
  1185. Possible values for the command are:
  1186. `emacs' The file will be visited by the current Emacs process.
  1187. `default' Use the default application for this file type, which is the
  1188. association for t in the list, most likely in the system-specific
  1189. part.
  1190. This can be used to overrule an unwanted setting in the
  1191. system-specific variable.
  1192. `system' Use the system command for opening files, like \"open\".
  1193. This command is specified by the entry whose car is `system'.
  1194. Most likely, the system-specific version of this variable
  1195. does define this command, but you can overrule/replace it
  1196. here.
  1197. string A command to be executed by a shell; %s will be replaced
  1198. by the path to the file.
  1199. sexp A Lisp form which will be evaluated. The file path will
  1200. be available in the Lisp variable `file'.
  1201. For more examples, see the system specific constants
  1202. `org-file-apps-defaults-macosx'
  1203. `org-file-apps-defaults-windowsnt'
  1204. `org-file-apps-defaults-gnu'."
  1205. :group 'org-link-follow
  1206. :type '(repeat
  1207. (cons (choice :value ""
  1208. (string :tag "Extension")
  1209. (const :tag "System command to open files" system)
  1210. (const :tag "Default for unrecognized files" t)
  1211. (const :tag "Remote file" remote)
  1212. (const :tag "Links to a directory" directory)
  1213. (const :tag "Any files that have Emacs modes"
  1214. auto-mode))
  1215. (choice :value ""
  1216. (const :tag "Visit with Emacs" emacs)
  1217. (const :tag "Use default" default)
  1218. (const :tag "Use the system command" system)
  1219. (string :tag "Command")
  1220. (sexp :tag "Lisp form")))))
  1221. (defgroup org-refile nil
  1222. "Options concerning refiling entries in Org-mode."
  1223. :tag "Org Refile"
  1224. :group 'org)
  1225. (defcustom org-directory "~/org"
  1226. "Directory with org files.
  1227. This is just a default location to look for Org files. There is no need
  1228. at all to put your files into this directory. It is only used in the
  1229. following situations:
  1230. 1. When a remember template specifies a target file that is not an
  1231. absolute path. The path will then be interpreted relative to
  1232. `org-directory'
  1233. 2. When a remember note is filed away in an interactive way (when exiting the
  1234. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1235. with `org-directory' as the default path."
  1236. :group 'org-refile
  1237. :group 'org-remember
  1238. :type 'directory)
  1239. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1240. "Default target for storing notes.
  1241. Used by the hooks for remember.el. This can be a string, or nil to mean
  1242. the value of `remember-data-file'.
  1243. You can set this on a per-template basis with the variable
  1244. `org-remember-templates'."
  1245. :group 'org-refile
  1246. :group 'org-remember
  1247. :type '(choice
  1248. (const :tag "Default from remember-data-file" nil)
  1249. file))
  1250. (defcustom org-goto-interface 'outline
  1251. "The default interface to be used for `org-goto'.
  1252. Allowed values are:
  1253. outline The interface shows an outline of the relevant file
  1254. and the correct heading is found by moving through
  1255. the outline or by searching with incremental search.
  1256. outline-path-completion Headlines in the current buffer are offered via
  1257. completion. This is the interface also used by
  1258. the refile command."
  1259. :group 'org-refile
  1260. :type '(choice
  1261. (const :tag "Outline" outline)
  1262. (const :tag "Outline-path-completion" outline-path-completion)))
  1263. (defcustom org-goto-max-level 5
  1264. "Maximum level to be considered when running org-goto with refile interface."
  1265. :group 'org-refile
  1266. :type 'integer)
  1267. (defcustom org-reverse-note-order nil
  1268. "Non-nil means, store new notes at the beginning of a file or entry.
  1269. When nil, new notes will be filed to the end of a file or entry.
  1270. This can also be a list with cons cells of regular expressions that
  1271. are matched against file names, and values."
  1272. :group 'org-remember
  1273. :group 'org-refile
  1274. :type '(choice
  1275. (const :tag "Reverse always" t)
  1276. (const :tag "Reverse never" nil)
  1277. (repeat :tag "By file name regexp"
  1278. (cons regexp boolean))))
  1279. (defcustom org-refile-targets nil
  1280. "Targets for refiling entries with \\[org-refile].
  1281. This is list of cons cells. Each cell contains:
  1282. - a specification of the files to be considered, either a list of files,
  1283. or a symbol whose function or variable value will be used to retrieve
  1284. a file name or a list of file names. If you use `org-agenda-files' for
  1285. that, all agenda files will be scanned for targets. Nil means, consider
  1286. headings in the current buffer.
  1287. - A specification of how to find candidate refile targets. This may be
  1288. any of:
  1289. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1290. This tag has to be present in all target headlines, inheritance will
  1291. not be considered.
  1292. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1293. todo keyword.
  1294. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1295. headlines that are refiling targets.
  1296. - a cons cell (:level . N). Any headline of level N is considered a target.
  1297. Note that, when `org-odd-levels-only' is set, level corresponds to
  1298. order in hierarchy, not to the number of stars.
  1299. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1300. Note that, when `org-odd-levels-only' is set, level corresponds to
  1301. order in hierarchy, not to the number of stars.
  1302. You can set the variable `org-refile-target-verify-function' to a function
  1303. to verify each headline found by the simple critery above.
  1304. When this variable is nil, all top-level headlines in the current buffer
  1305. are used, equivalent to the value `((nil . (:level . 1))'."
  1306. :group 'org-refile
  1307. :type '(repeat
  1308. (cons
  1309. (choice :value org-agenda-files
  1310. (const :tag "All agenda files" org-agenda-files)
  1311. (const :tag "Current buffer" nil)
  1312. (function) (variable) (file))
  1313. (choice :tag "Identify target headline by"
  1314. (cons :tag "Specific tag" (const :value :tag) (string))
  1315. (cons :tag "TODO keyword" (const :value :todo) (string))
  1316. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1317. (cons :tag "Level number" (const :value :level) (integer))
  1318. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1319. (defcustom org-refile-target-verify-function nil
  1320. "Function to verify if the headline at point should be a refile target.
  1321. The function will be called without arguments, with point at the
  1322. beginning of the headline. It should return t and leave point
  1323. where it is if the headline is a valid target for refiling.
  1324. If the target should not be selected, the function must return nil.
  1325. In addition to this, it may move point to a place from where the search
  1326. should be continued. For example, the function may decide that the entire
  1327. subtree of the current entry should be excluded and move point to the end
  1328. of the subtree."
  1329. :group 'org-refile
  1330. :type 'function)
  1331. (defcustom org-refile-use-outline-path nil
  1332. "Non-nil means, provide refile targets as paths.
  1333. So a level 3 headline will be available as level1/level2/level3.
  1334. When the value is `file', also include the file name (without directory)
  1335. into the path. In this case, you can also stop the completion after
  1336. the file name, to get entries inserted as top level in the file.
  1337. When `full-file-path', include the full file path."
  1338. :group 'org-refile
  1339. :type '(choice
  1340. (const :tag "Not" nil)
  1341. (const :tag "Yes" t)
  1342. (const :tag "Start with file name" file)
  1343. (const :tag "Start with full file path" full-file-path)))
  1344. (defcustom org-outline-path-complete-in-steps t
  1345. "Non-nil means, complete the outline path in hierarchical steps.
  1346. When Org-mode uses the refile interface to select an outline path
  1347. \(see variable `org-refile-use-outline-path'), the completion of
  1348. the path can be done is a single go, or if can be done in steps down
  1349. the headline hierarchy. Going in steps is probably the best if you
  1350. do not use a special completion package like `ido' or `icicles'.
  1351. However, when using these packages, going in one step can be very
  1352. fast, while still showing the whole path to the entry."
  1353. :group 'org-refile
  1354. :type 'boolean)
  1355. (defcustom org-refile-allow-creating-parent-nodes nil
  1356. "Non-nil means, allow to create new nodes as refile targets.
  1357. New nodes are then created by adding \"/new node name\" to the completion
  1358. of an existing node. When the value of this variable is `confirm',
  1359. new node creation must be confirmed by the user (recommended)
  1360. When nil, the completion must match an existing entry.
  1361. Note that, if the new heading is not seen by the criteria
  1362. listed in `org-refile-targets', multiple instances of the same
  1363. heading would be created by trying again to file under the new
  1364. heading."
  1365. :group 'org-refile
  1366. :type '(choice
  1367. (const :tag "Never" nil)
  1368. (const :tag "Always" t)
  1369. (const :tag "Prompt for confirmation" confirm)))
  1370. (defgroup org-todo nil
  1371. "Options concerning TODO items in Org-mode."
  1372. :tag "Org TODO"
  1373. :group 'org)
  1374. (defgroup org-progress nil
  1375. "Options concerning Progress logging in Org-mode."
  1376. :tag "Org Progress"
  1377. :group 'org-time)
  1378. (defvar org-todo-interpretation-widgets
  1379. '(
  1380. (:tag "Sequence (cycling hits every state)" sequence)
  1381. (:tag "Type (cycling directly to DONE)" type))
  1382. "The available interpretation symbols for customizing
  1383. `org-todo-keywords'.
  1384. Interested libraries should add to this list.")
  1385. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1386. "List of TODO entry keyword sequences and their interpretation.
  1387. \\<org-mode-map>This is a list of sequences.
  1388. Each sequence starts with a symbol, either `sequence' or `type',
  1389. indicating if the keywords should be interpreted as a sequence of
  1390. action steps, or as different types of TODO items. The first
  1391. keywords are states requiring action - these states will select a headline
  1392. for inclusion into the global TODO list Org-mode produces. If one of
  1393. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1394. signify that no further action is necessary. If \"|\" is not found,
  1395. the last keyword is treated as the only DONE state of the sequence.
  1396. The command \\[org-todo] cycles an entry through these states, and one
  1397. additional state where no keyword is present. For details about this
  1398. cycling, see the manual.
  1399. TODO keywords and interpretation can also be set on a per-file basis with
  1400. the special #+SEQ_TODO and #+TYP_TODO lines.
  1401. Each keyword can optionally specify a character for fast state selection
  1402. \(in combination with the variable `org-use-fast-todo-selection')
  1403. and specifiers for state change logging, using the same syntax
  1404. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1405. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1406. indicates to record a time stamp each time this state is selected.
  1407. Each keyword may also specify if a timestamp or a note should be
  1408. recorded when entering or leaving the state, by adding additional
  1409. characters in the parenthesis after the keyword. This looks like this:
  1410. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1411. record only the time of the state change. With X and Y being either
  1412. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1413. Y when leaving the state if and only if the *target* state does not
  1414. define X. You may omit any of the fast-selection key or X or /Y,
  1415. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1416. For backward compatibility, this variable may also be just a list
  1417. of keywords - in this case the interpretation (sequence or type) will be
  1418. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1419. :group 'org-todo
  1420. :group 'org-keywords
  1421. :type '(choice
  1422. (repeat :tag "Old syntax, just keywords"
  1423. (string :tag "Keyword"))
  1424. (repeat :tag "New syntax"
  1425. (cons
  1426. (choice
  1427. :tag "Interpretation"
  1428. ;;Quick and dirty way to see
  1429. ;;`org-todo-interpretations'. This takes the
  1430. ;;place of item arguments
  1431. :convert-widget
  1432. (lambda (widget)
  1433. (widget-put widget
  1434. :args (mapcar
  1435. #'(lambda (x)
  1436. (widget-convert
  1437. (cons 'const x)))
  1438. org-todo-interpretation-widgets))
  1439. widget))
  1440. (repeat
  1441. (string :tag "Keyword"))))))
  1442. (defvar org-todo-keywords-1 nil
  1443. "All TODO and DONE keywords active in a buffer.")
  1444. (make-variable-buffer-local 'org-todo-keywords-1)
  1445. (defvar org-todo-keywords-for-agenda nil)
  1446. (defvar org-done-keywords-for-agenda nil)
  1447. (defvar org-todo-keyword-alist-for-agenda nil)
  1448. (defvar org-tag-alist-for-agenda nil)
  1449. (defvar org-agenda-contributing-files nil)
  1450. (defvar org-not-done-keywords nil)
  1451. (make-variable-buffer-local 'org-not-done-keywords)
  1452. (defvar org-done-keywords nil)
  1453. (make-variable-buffer-local 'org-done-keywords)
  1454. (defvar org-todo-heads nil)
  1455. (make-variable-buffer-local 'org-todo-heads)
  1456. (defvar org-todo-sets nil)
  1457. (make-variable-buffer-local 'org-todo-sets)
  1458. (defvar org-todo-log-states nil)
  1459. (make-variable-buffer-local 'org-todo-log-states)
  1460. (defvar org-todo-kwd-alist nil)
  1461. (make-variable-buffer-local 'org-todo-kwd-alist)
  1462. (defvar org-todo-key-alist nil)
  1463. (make-variable-buffer-local 'org-todo-key-alist)
  1464. (defvar org-todo-key-trigger nil)
  1465. (make-variable-buffer-local 'org-todo-key-trigger)
  1466. (defcustom org-todo-interpretation 'sequence
  1467. "Controls how TODO keywords are interpreted.
  1468. This variable is in principle obsolete and is only used for
  1469. backward compatibility, if the interpretation of todo keywords is
  1470. not given already in `org-todo-keywords'. See that variable for
  1471. more information."
  1472. :group 'org-todo
  1473. :group 'org-keywords
  1474. :type '(choice (const sequence)
  1475. (const type)))
  1476. (defcustom org-use-fast-todo-selection t
  1477. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1478. This variable describes if and under what circumstances the cycling
  1479. mechanism for TODO keywords will be replaced by a single-key, direct
  1480. selection scheme.
  1481. When nil, fast selection is never used.
  1482. When the symbol `prefix', it will be used when `org-todo' is called with
  1483. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1484. in an agenda buffer.
  1485. When t, fast selection is used by default. In this case, the prefix
  1486. argument forces cycling instead.
  1487. In all cases, the special interface is only used if access keys have actually
  1488. been assigned by the user, i.e. if keywords in the configuration are followed
  1489. by a letter in parenthesis, like TODO(t)."
  1490. :group 'org-todo
  1491. :type '(choice
  1492. (const :tag "Never" nil)
  1493. (const :tag "By default" t)
  1494. (const :tag "Only with C-u C-c C-t" prefix)))
  1495. (defcustom org-provide-todo-statistics t
  1496. "Non-nil means, update todo statistics after insert and toggle.
  1497. When this is set, todo statistics is updated in the parent of the current
  1498. entry each time a todo state is changed."
  1499. :group 'org-todo
  1500. :type 'boolean)
  1501. (defcustom org-hierarchical-todo-statistics t
  1502. "Non-nil means, TODO statistics covers just direct children.
  1503. When nil, all entries in the subtree are considered.
  1504. This has only an effect if `org-provide-todo-statistics' is set."
  1505. :group 'org-todo
  1506. :type 'boolean)
  1507. (defcustom org-after-todo-state-change-hook nil
  1508. "Hook which is run after the state of a TODO item was changed.
  1509. The new state (a string with a TODO keyword, or nil) is available in the
  1510. Lisp variable `state'."
  1511. :group 'org-todo
  1512. :type 'hook)
  1513. (defvar org-blocker-hook nil
  1514. "Hook for functions that are allowed to block a state change.
  1515. Each function gets as its single argument a property list, see
  1516. `org-trigger-hook' for more information about this list.
  1517. If any of the functions in this hook returns nil, the state change
  1518. is blocked.")
  1519. (defvar org-trigger-hook nil
  1520. "Hook for functions that are triggered by a state change.
  1521. Each function gets as its single argument a property list with at least
  1522. the following elements:
  1523. (:type type-of-change :position pos-at-entry-start
  1524. :from old-state :to new-state)
  1525. Depending on the type, more properties may be present.
  1526. This mechanism is currently implemented for:
  1527. TODO state changes
  1528. ------------------
  1529. :type todo-state-change
  1530. :from previous state (keyword as a string), or nil, or a symbol
  1531. 'todo' or 'done', to indicate the general type of state.
  1532. :to new state, like in :from")
  1533. (defcustom org-enforce-todo-dependencies nil
  1534. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1535. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1536. be blocked if any prior sibling is not yet done.
  1537. Finally, if the parent is blocked because of ordered siblings of its own,
  1538. the child will also be blocked.
  1539. This variable needs to be set before org.el is loaded, and you need to
  1540. restart Emacs after a change to make the change effective. The only way
  1541. to change is while Emacs is running is through the customize interface."
  1542. :set (lambda (var val)
  1543. (set var val)
  1544. (if val
  1545. (add-hook 'org-blocker-hook
  1546. 'org-block-todo-from-children-or-siblings-or-parent)
  1547. (remove-hook 'org-blocker-hook
  1548. 'org-block-todo-from-children-or-siblings-or-parent)))
  1549. :group 'org-todo
  1550. :type 'boolean)
  1551. (defcustom org-enforce-todo-checkbox-dependencies nil
  1552. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1553. When this is nil, checkboxes have no influence on switching TODO states.
  1554. When non-nil, you first need to check off all check boxes before the TODO
  1555. entry can be switched to DONE.
  1556. This variable needs to be set before org.el is loaded, and you need to
  1557. restart Emacs after a change to make the change effective. The only way
  1558. to change is while Emacs is running is through the customize interface."
  1559. :set (lambda (var val)
  1560. (set var val)
  1561. (if val
  1562. (add-hook 'org-blocker-hook
  1563. 'org-block-todo-from-checkboxes)
  1564. (remove-hook 'org-blocker-hook
  1565. 'org-block-todo-from-checkboxes)))
  1566. :group 'org-todo
  1567. :type 'boolean)
  1568. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1569. "Non-nil means, inserting a TODO heading is treated as state change.
  1570. So when the command \\[org-insert-todo-heading] is used, state change
  1571. logging will apply if appropriate. When nil, the new TODO item will
  1572. be inserted directly, and no logging will take place."
  1573. :group 'org-todo
  1574. :type 'boolean)
  1575. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1576. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1577. This is the default behavior. However, setting this to nil allows a
  1578. convenient way to select a TODO state and bypass any logging associated
  1579. with that."
  1580. :group 'org-todo
  1581. :type 'boolean)
  1582. (defcustom org-todo-state-tags-triggers nil
  1583. "Tag changes that should be triggered by TODO state changes.
  1584. This is a list. Each entry is
  1585. (state-change (tag . flag) .......)
  1586. State-change can be a string with a state, and empty string to indicate the
  1587. state that has no TODO keyword, or it can be one of the symbols `todo'
  1588. or `done', meaning any not-done or done state, respectively."
  1589. :group 'org-todo
  1590. :group 'org-tags
  1591. :type '(repeat
  1592. (cons (choice :tag "When changing to"
  1593. (const :tag "Not-done state" todo)
  1594. (const :tag "Done state" done)
  1595. (string :tag "State"))
  1596. (repeat
  1597. (cons :tag "Tag action"
  1598. (string :tag "Tag")
  1599. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1600. (defcustom org-log-done nil
  1601. "Information to record when a task moves to the DONE state.
  1602. Possible values are:
  1603. nil Don't add anything, just change the keyword
  1604. time Add a time stamp to the task
  1605. note Prompt a closing note and add it with template `org-log-note-headings'
  1606. This option can also be set with on a per-file-basis with
  1607. #+STARTUP: nologdone
  1608. #+STARTUP: logdone
  1609. #+STARTUP: lognotedone
  1610. You can have local logging settings for a subtree by setting the LOGGING
  1611. property to one or more of these keywords."
  1612. :group 'org-todo
  1613. :group 'org-progress
  1614. :type '(choice
  1615. (const :tag "No logging" nil)
  1616. (const :tag "Record CLOSED timestamp" time)
  1617. (const :tag "Record CLOSED timestamp with closing note." note)))
  1618. ;; Normalize old uses of org-log-done.
  1619. (cond
  1620. ((eq org-log-done t) (setq org-log-done 'time))
  1621. ((and (listp org-log-done) (memq 'done org-log-done))
  1622. (setq org-log-done 'note)))
  1623. (defcustom org-log-note-clock-out nil
  1624. "Non-nil means, record a note when clocking out of an item.
  1625. This can also be configured on a per-file basis by adding one of
  1626. the following lines anywhere in the buffer:
  1627. #+STARTUP: lognoteclock-out
  1628. #+STARTUP: nolognoteclock-out"
  1629. :group 'org-todo
  1630. :group 'org-progress
  1631. :type 'boolean)
  1632. (defcustom org-log-done-with-time t
  1633. "Non-nil means, the CLOSED time stamp will contain date and time.
  1634. When nil, only the date will be recorded."
  1635. :group 'org-progress
  1636. :type 'boolean)
  1637. (defcustom org-log-note-headings
  1638. '((done . "CLOSING NOTE %t")
  1639. (state . "State %-12s from %-12S %t")
  1640. (note . "Note taken on %t")
  1641. (clock-out . ""))
  1642. "Headings for notes added to entries.
  1643. The value is an alist, with the car being a symbol indicating the note
  1644. context, and the cdr is the heading to be used. The heading may also be the
  1645. empty string.
  1646. %t in the heading will be replaced by a time stamp.
  1647. %s will be replaced by the new TODO state, in double quotes.
  1648. %S will be replaced by the old TODO state, in double quotes.
  1649. %u will be replaced by the user name.
  1650. %U will be replaced by the full user name."
  1651. :group 'org-todo
  1652. :group 'org-progress
  1653. :type '(list :greedy t
  1654. (cons (const :tag "Heading when closing an item" done) string)
  1655. (cons (const :tag
  1656. "Heading when changing todo state (todo sequence only)"
  1657. state) string)
  1658. (cons (const :tag "Heading when just taking a note" note) string)
  1659. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1660. (unless (assq 'note org-log-note-headings)
  1661. (push '(note . "%t") org-log-note-headings))
  1662. (defcustom org-log-into-drawer nil
  1663. "Non-nil means, insert state change notes and time stamps into a drawer.
  1664. When nil, state changes notes will be inserted after the headline and
  1665. any scheduling and clock lines, but not inside a drawer.
  1666. The value of this variable should be the name of the drawer to use.
  1667. LOGBOOK is proposed at the default drawer for this purpose, you can
  1668. also set this to a string to define the drawer of your choice.
  1669. A value of t is also allowed, representing \"LOGBOOK\".
  1670. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1671. will be ignored.
  1672. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1673. a subtree."
  1674. :group 'org-todo
  1675. :group 'org-progress
  1676. :type '(choice
  1677. (const :tag "Not into a drawer" nil)
  1678. (const :tag "LOGBOOK" t)
  1679. (string :tag "Other")))
  1680. (if (fboundp 'defvaralias)
  1681. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1682. (defun org-log-into-drawer ()
  1683. "Return the value of `org-log-into-drawer', but let properties overrule.
  1684. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1685. used instead of the default value."
  1686. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1687. (cond
  1688. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1689. ((equal p "t") "LOGBOOK")
  1690. (t p))))
  1691. (defcustom org-log-state-notes-insert-after-drawers nil
  1692. "Non-nil means, insert state change notes after any drawers in entry.
  1693. Only the drawers that *immediately* follow the headline and the
  1694. deadline/scheduled line are skipped.
  1695. When nil, insert notes right after the heading and perhaps the line
  1696. with deadline/scheduling if present.
  1697. This variable will have no effect if `org-log-into-drawer' is
  1698. set."
  1699. :group 'org-todo
  1700. :group 'org-progress
  1701. :type 'boolean)
  1702. (defcustom org-log-states-order-reversed t
  1703. "Non-nil means, the latest state change note will be directly after heading.
  1704. When nil, the notes will be orderer according to time."
  1705. :group 'org-todo
  1706. :group 'org-progress
  1707. :type 'boolean)
  1708. (defcustom org-log-repeat 'time
  1709. "Non-nil means, record moving through the DONE state when triggering repeat.
  1710. An auto-repeating tasks is immediately switched back to TODO when marked
  1711. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1712. the TODO keyword definition, or recording a closing note by setting
  1713. `org-log-done', there will be no record of the task moving through DONE.
  1714. This variable forces taking a note anyway. Possible values are:
  1715. nil Don't force a record
  1716. time Record a time stamp
  1717. note Record a note
  1718. This option can also be set with on a per-file-basis with
  1719. #+STARTUP: logrepeat
  1720. #+STARTUP: lognoterepeat
  1721. #+STARTUP: nologrepeat
  1722. You can have local logging settings for a subtree by setting the LOGGING
  1723. property to one or more of these keywords."
  1724. :group 'org-todo
  1725. :group 'org-progress
  1726. :type '(choice
  1727. (const :tag "Don't force a record" nil)
  1728. (const :tag "Force recording the DONE state" time)
  1729. (const :tag "Force recording a note with the DONE state" note)))
  1730. (defgroup org-priorities nil
  1731. "Priorities in Org-mode."
  1732. :tag "Org Priorities"
  1733. :group 'org-todo)
  1734. (defcustom org-enable-priority-commands t
  1735. "Non-nil means, priority commands are active.
  1736. When nil, these commands will be disabled, so that you never accidentally
  1737. set a priority."
  1738. :group 'org-priorities
  1739. :type 'boolean)
  1740. (defcustom org-highest-priority ?A
  1741. "The highest priority of TODO items. A character like ?A, ?B etc.
  1742. Must have a smaller ASCII number than `org-lowest-priority'."
  1743. :group 'org-priorities
  1744. :type 'character)
  1745. (defcustom org-lowest-priority ?C
  1746. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1747. Must have a larger ASCII number than `org-highest-priority'."
  1748. :group 'org-priorities
  1749. :type 'character)
  1750. (defcustom org-default-priority ?B
  1751. "The default priority of TODO items.
  1752. This is the priority an item get if no explicit priority is given."
  1753. :group 'org-priorities
  1754. :type 'character)
  1755. (defcustom org-priority-start-cycle-with-default t
  1756. "Non-nil means, start with default priority when starting to cycle.
  1757. When this is nil, the first step in the cycle will be (depending on the
  1758. command used) one higher or lower that the default priority."
  1759. :group 'org-priorities
  1760. :type 'boolean)
  1761. (defgroup org-time nil
  1762. "Options concerning time stamps and deadlines in Org-mode."
  1763. :tag "Org Time"
  1764. :group 'org)
  1765. (defcustom org-insert-labeled-timestamps-at-point nil
  1766. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1767. When nil, these labeled time stamps are forces into the second line of an
  1768. entry, just after the headline. When scheduling from the global TODO list,
  1769. the time stamp will always be forced into the second line."
  1770. :group 'org-time
  1771. :type 'boolean)
  1772. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1773. "Formats for `format-time-string' which are used for time stamps.
  1774. It is not recommended to change this constant.")
  1775. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1776. "Number of minutes to round time stamps to.
  1777. These are two values, the first applies when first creating a time stamp.
  1778. The second applies when changing it with the commands `S-up' and `S-down'.
  1779. When changing the time stamp, this means that it will change in steps
  1780. of N minutes, as given by the second value.
  1781. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1782. numbers should be factors of 60, so for example 5, 10, 15.
  1783. When this is larger than 1, you can still force an exact time-stamp by using
  1784. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1785. and by using a prefix arg to `S-up/down' to specify the exact number
  1786. of minutes to shift."
  1787. :group 'org-time
  1788. :get '(lambda (var) ; Make sure all entries have 5 elements
  1789. (if (integerp (default-value var))
  1790. (list (default-value var) 5)
  1791. (default-value var)))
  1792. :type '(list
  1793. (integer :tag "when inserting times")
  1794. (integer :tag "when modifying times")))
  1795. ;; Normalize old customizations of this variable.
  1796. (when (integerp org-time-stamp-rounding-minutes)
  1797. (setq org-time-stamp-rounding-minutes
  1798. (list org-time-stamp-rounding-minutes
  1799. org-time-stamp-rounding-minutes)))
  1800. (defcustom org-display-custom-times nil
  1801. "Non-nil means, overlay custom formats over all time stamps.
  1802. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1803. To turn this on on a per-file basis, insert anywhere in the file:
  1804. #+STARTUP: customtime"
  1805. :group 'org-time
  1806. :set 'set-default
  1807. :type 'sexp)
  1808. (make-variable-buffer-local 'org-display-custom-times)
  1809. (defcustom org-time-stamp-custom-formats
  1810. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1811. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1812. These are overlayed over the default ISO format if the variable
  1813. `org-display-custom-times' is set. Time like %H:%M should be at the
  1814. end of the second format. The custom formats are also honored by export
  1815. commands, if custom time display is turned on at the time of export."
  1816. :group 'org-time
  1817. :type 'sexp)
  1818. (defun org-time-stamp-format (&optional long inactive)
  1819. "Get the right format for a time string."
  1820. (let ((f (if long (cdr org-time-stamp-formats)
  1821. (car org-time-stamp-formats))))
  1822. (if inactive
  1823. (concat "[" (substring f 1 -1) "]")
  1824. f)))
  1825. (defcustom org-time-clocksum-format "%d:%02d"
  1826. "The format string used when creating CLOCKSUM lines, or when
  1827. org-mode generates a time duration."
  1828. :group 'org-time
  1829. :type 'string)
  1830. (defcustom org-deadline-warning-days 14
  1831. "No. of days before expiration during which a deadline becomes active.
  1832. This variable governs the display in sparse trees and in the agenda.
  1833. When 0 or negative, it means use this number (the absolute value of it)
  1834. even if a deadline has a different individual lead time specified.
  1835. Custom commands can set this variable in the options section."
  1836. :group 'org-time
  1837. :group 'org-agenda-daily/weekly
  1838. :type 'integer)
  1839. (defcustom org-read-date-prefer-future t
  1840. "Non-nil means, assume future for incomplete date input from user.
  1841. This affects the following situations:
  1842. 1. The user gives a day, but no month.
  1843. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1844. read this as the third of *next* month. However, if you enter \"17\",
  1845. it will be considered as *this* month.
  1846. 2. The user gives a month but not a year.
  1847. For example, if it is april and you enter \"feb 2\", this will be read
  1848. as feb 2, *next* year. \"May 5\", however, will be this year.
  1849. Currently this does not work for ISO week specifications.
  1850. When this option is nil, the current month and year will always be used
  1851. as defaults."
  1852. :group 'org-time
  1853. :type 'boolean)
  1854. (defcustom org-read-date-display-live t
  1855. "Non-nil means, display current interpretation of date prompt live.
  1856. This display will be in an overlay, in the minibuffer."
  1857. :group 'org-time
  1858. :type 'boolean)
  1859. (defcustom org-read-date-popup-calendar t
  1860. "Non-nil means, pop up a calendar when prompting for a date.
  1861. In the calendar, the date can be selected with mouse-1. However, the
  1862. minibuffer will also be active, and you can simply enter the date as well.
  1863. When nil, only the minibuffer will be available."
  1864. :group 'org-time
  1865. :type 'boolean)
  1866. (if (fboundp 'defvaralias)
  1867. (defvaralias 'org-popup-calendar-for-date-prompt
  1868. 'org-read-date-popup-calendar))
  1869. (defcustom org-read-date-minibuffer-setup-hook nil
  1870. "Hook to be used to set up keys for the date/time interface.
  1871. Add key definitions to `minibuffer-local-map', which will be a temporary
  1872. copy."
  1873. :group 'org-time
  1874. :type 'hook)
  1875. (defcustom org-extend-today-until 0
  1876. "The hour when your day really ends. Must be an integer.
  1877. This has influence for the following applications:
  1878. - When switching the agenda to \"today\". It it is still earlier than
  1879. the time given here, the day recognized as TODAY is actually yesterday.
  1880. - When a date is read from the user and it is still before the time given
  1881. here, the current date and time will be assumed to be yesterday, 23:59.
  1882. Also, timestamps inserted in remember templates follow this rule.
  1883. IMPORTANT: This is a feature whose implementation is and likely will
  1884. remain incomplete. Really, it is only here because past midnight seems to
  1885. be the favorite working time of John Wiegley :-)"
  1886. :group 'org-time
  1887. :type 'integer)
  1888. (defcustom org-edit-timestamp-down-means-later nil
  1889. "Non-nil means, S-down will increase the time in a time stamp.
  1890. When nil, S-up will increase."
  1891. :group 'org-time
  1892. :type 'boolean)
  1893. (defcustom org-calendar-follow-timestamp-change t
  1894. "Non-nil means, make the calendar window follow timestamp changes.
  1895. When a timestamp is modified and the calendar window is visible, it will be
  1896. moved to the new date."
  1897. :group 'org-time
  1898. :type 'boolean)
  1899. (defgroup org-tags nil
  1900. "Options concerning tags in Org-mode."
  1901. :tag "Org Tags"
  1902. :group 'org)
  1903. (defcustom org-tag-alist nil
  1904. "List of tags allowed in Org-mode files.
  1905. When this list is nil, Org-mode will base TAG input on what is already in the
  1906. buffer.
  1907. The value of this variable is an alist, the car of each entry must be a
  1908. keyword as a string, the cdr may be a character that is used to select
  1909. that tag through the fast-tag-selection interface.
  1910. See the manual for details."
  1911. :group 'org-tags
  1912. :type '(repeat
  1913. (choice
  1914. (cons (string :tag "Tag name")
  1915. (character :tag "Access char"))
  1916. (const :tag "Start radio group" (:startgroup))
  1917. (const :tag "End radio group" (:endgroup))
  1918. (const :tag "New line" (:newline)))))
  1919. (defcustom org-tag-persistent-alist nil
  1920. "List of tags that will always appear in all Org-mode files.
  1921. This is in addition to any in buffer settings or customizations
  1922. of `org-tag-alist'.
  1923. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1924. The value of this variable is an alist, the car of each entry must be a
  1925. keyword as a string, the cdr may be a character that is used to select
  1926. that tag through the fast-tag-selection interface.
  1927. See the manual for details.
  1928. To disable these tags on a per-file basis, insert anywhere in the file:
  1929. #+STARTUP: noptag"
  1930. :group 'org-tags
  1931. :type '(repeat
  1932. (choice
  1933. (cons (string :tag "Tag name")
  1934. (character :tag "Access char"))
  1935. (const :tag "Start radio group" (:startgroup))
  1936. (const :tag "End radio group" (:endgroup))
  1937. (const :tag "New line" (:newline)))))
  1938. (defvar org-file-tags nil
  1939. "List of tags that can be inherited by all entries in the file.
  1940. The tags will be inherited if the variable `org-use-tag-inheritance'
  1941. says they should be.
  1942. This variable is populated from #+TAG lines.")
  1943. (defcustom org-use-fast-tag-selection 'auto
  1944. "Non-nil means, use fast tag selection scheme.
  1945. This is a special interface to select and deselect tags with single keys.
  1946. When nil, fast selection is never used.
  1947. When the symbol `auto', fast selection is used if and only if selection
  1948. characters for tags have been configured, either through the variable
  1949. `org-tag-alist' or through a #+TAGS line in the buffer.
  1950. When t, fast selection is always used and selection keys are assigned
  1951. automatically if necessary."
  1952. :group 'org-tags
  1953. :type '(choice
  1954. (const :tag "Always" t)
  1955. (const :tag "Never" nil)
  1956. (const :tag "When selection characters are configured" 'auto)))
  1957. (defcustom org-fast-tag-selection-single-key nil
  1958. "Non-nil means, fast tag selection exits after first change.
  1959. When nil, you have to press RET to exit it.
  1960. During fast tag selection, you can toggle this flag with `C-c'.
  1961. This variable can also have the value `expert'. In this case, the window
  1962. displaying the tags menu is not even shown, until you press C-c again."
  1963. :group 'org-tags
  1964. :type '(choice
  1965. (const :tag "No" nil)
  1966. (const :tag "Yes" t)
  1967. (const :tag "Expert" expert)))
  1968. (defvar org-fast-tag-selection-include-todo nil
  1969. "Non-nil means, fast tags selection interface will also offer TODO states.
  1970. This is an undocumented feature, you should not rely on it.")
  1971. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1972. "The column to which tags should be indented in a headline.
  1973. If this number is positive, it specifies the column. If it is negative,
  1974. it means that the tags should be flushright to that column. For example,
  1975. -80 works well for a normal 80 character screen."
  1976. :group 'org-tags
  1977. :type 'integer)
  1978. (defcustom org-auto-align-tags t
  1979. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1980. These operations change the length of a headline and therefore shift
  1981. the tags around. With this options turned on, after each such operation
  1982. the tags are again aligned to `org-tags-column'."
  1983. :group 'org-tags
  1984. :type 'boolean)
  1985. (defcustom org-use-tag-inheritance t
  1986. "Non-nil means, tags in levels apply also for sublevels.
  1987. When nil, only the tags directly given in a specific line apply there.
  1988. This may also be a list of tags that should be inherited, or a regexp that
  1989. matches tags that should be inherited. Additional control is possible
  1990. with the variable `org-tags-exclude-from-inheritance' which gives an
  1991. explicit list of tags to be excluded from inheritance., even if the value of
  1992. `org-use-tag-inheritance' would select it for inheritance.
  1993. If this option is t, a match early-on in a tree can lead to a large
  1994. number of matches in the subtree when constructing the agenda or creating
  1995. a sparse tree. If you only want to see the first match in a tree during
  1996. a search, check out the variable `org-tags-match-list-sublevels'."
  1997. :group 'org-tags
  1998. :type '(choice
  1999. (const :tag "Not" nil)
  2000. (const :tag "Always" t)
  2001. (repeat :tag "Specific tags" (string :tag "Tag"))
  2002. (regexp :tag "Tags matched by regexp")))
  2003. (defcustom org-tags-exclude-from-inheritance nil
  2004. "List of tags that should never be inherited.
  2005. This is a way to exclude a few tags from inheritance. For way to do
  2006. the opposite, to actively allow inheritance for selected tags,
  2007. see the variable `org-use-tag-inheritance'."
  2008. :group 'org-tags
  2009. :type '(repeat (string :tag "Tag")))
  2010. (defun org-tag-inherit-p (tag)
  2011. "Check if TAG is one that should be inherited."
  2012. (cond
  2013. ((member tag org-tags-exclude-from-inheritance) nil)
  2014. ((eq org-use-tag-inheritance t) t)
  2015. ((not org-use-tag-inheritance) nil)
  2016. ((stringp org-use-tag-inheritance)
  2017. (string-match org-use-tag-inheritance tag))
  2018. ((listp org-use-tag-inheritance)
  2019. (member tag org-use-tag-inheritance))
  2020. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2021. (defcustom org-tags-match-list-sublevels t
  2022. "Non-nil means list also sublevels of headlines matching a search.
  2023. This variable applies to tags/property searches, and also to stuck
  2024. projects because this search is based on a tags match as well.
  2025. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2026. the sublevels of a headline matching a tag search often also match
  2027. the same search. Listing all of them can create very long lists.
  2028. Setting this variable to nil causes subtrees of a match to be skipped.
  2029. As a special case, if the tag search is restricted to TODO items, the
  2030. value of this variable is ignored and sublevels are always checked, to
  2031. make sure all corresponding TODO items find their way into the list.
  2032. This variable is semi-obsolete and probably should always be true. It
  2033. is better to limit inheritance to certain tags using the variables
  2034. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2035. :group 'org-tags
  2036. :type '(choice
  2037. (const :tag "No, don't list them" nil)
  2038. (const :tag "Yes, do list them" t)
  2039. (const :tag "List them, indented with leading dots" indented)))
  2040. (defvar org-tags-history nil
  2041. "History of minibuffer reads for tags.")
  2042. (defvar org-last-tags-completion-table nil
  2043. "The last used completion table for tags.")
  2044. (defvar org-after-tags-change-hook nil
  2045. "Hook that is run after the tags in a line have changed.")
  2046. (defgroup org-properties nil
  2047. "Options concerning properties in Org-mode."
  2048. :tag "Org Properties"
  2049. :group 'org)
  2050. (defcustom org-property-format "%-10s %s"
  2051. "How property key/value pairs should be formatted by `indent-line'.
  2052. When `indent-line' hits a property definition, it will format the line
  2053. according to this format, mainly to make sure that the values are
  2054. lined-up with respect to each other."
  2055. :group 'org-properties
  2056. :type 'string)
  2057. (defcustom org-use-property-inheritance nil
  2058. "Non-nil means, properties apply also for sublevels.
  2059. This setting is chiefly used during property searches. Turning it on can
  2060. cause significant overhead when doing a search, which is why it is not
  2061. on by default.
  2062. When nil, only the properties directly given in the current entry count.
  2063. When t, every property is inherited. The value may also be a list of
  2064. properties that should have inheritance, or a regular expression matching
  2065. properties that should be inherited.
  2066. However, note that some special properties use inheritance under special
  2067. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2068. and the properties ending in \"_ALL\" when they are used as descriptor
  2069. for valid values of a property.
  2070. Note for programmers:
  2071. When querying an entry with `org-entry-get', you can control if inheritance
  2072. should be used. By default, `org-entry-get' looks only at the local
  2073. properties. You can request inheritance by setting the inherit argument
  2074. to t (to force inheritance) or to `selective' (to respect the setting
  2075. in this variable)."
  2076. :group 'org-properties
  2077. :type '(choice
  2078. (const :tag "Not" nil)
  2079. (const :tag "Always" t)
  2080. (repeat :tag "Specific properties" (string :tag "Property"))
  2081. (regexp :tag "Properties matched by regexp")))
  2082. (defun org-property-inherit-p (property)
  2083. "Check if PROPERTY is one that should be inherited."
  2084. (cond
  2085. ((eq org-use-property-inheritance t) t)
  2086. ((not org-use-property-inheritance) nil)
  2087. ((stringp org-use-property-inheritance)
  2088. (string-match org-use-property-inheritance property))
  2089. ((listp org-use-property-inheritance)
  2090. (member property org-use-property-inheritance))
  2091. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2092. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2093. "The default column format, if no other format has been defined.
  2094. This variable can be set on the per-file basis by inserting a line
  2095. #+COLUMNS: %25ITEM ....."
  2096. :group 'org-properties
  2097. :type 'string)
  2098. (defcustom org-columns-ellipses ".."
  2099. "The ellipses to be used when a field in column view is truncated.
  2100. When this is the empty string, as many characters as possible are shown,
  2101. but then there will be no visual indication that the field has been truncated.
  2102. When this is a string of length N, the last N characters of a truncated
  2103. field are replaced by this string. If the column is narrower than the
  2104. ellipses string, only part of the ellipses string will be shown."
  2105. :group 'org-properties
  2106. :type 'string)
  2107. (defcustom org-columns-modify-value-for-display-function nil
  2108. "Function that modifies values for display in column view.
  2109. For example, it can be used to cut out a certain part from a time stamp.
  2110. The function must take 2 arguments:
  2111. column-title The title of the column (*not* the property name)
  2112. value The value that should be modified.
  2113. The function should return the value that should be displayed,
  2114. or nil if the normal value should be used."
  2115. :group 'org-properties
  2116. :type 'function)
  2117. (defcustom org-effort-property "Effort"
  2118. "The property that is being used to keep track of effort estimates.
  2119. Effort estimates given in this property need to have the format H:MM."
  2120. :group 'org-properties
  2121. :group 'org-progress
  2122. :type '(string :tag "Property"))
  2123. (defconst org-global-properties-fixed
  2124. '(("VISIBILITY_ALL" . "folded children content all")
  2125. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2126. "List of property/value pairs that can be inherited by any entry.
  2127. These are fixed values, for the preset properties. The user variable
  2128. that can be used to add to this list is `org-global-properties'.
  2129. The entries in this list are cons cells where the car is a property
  2130. name and cdr is a string with the value. If the value represents
  2131. multiple items like an \"_ALL\" property, separate the items by
  2132. spaces.")
  2133. (defcustom org-global-properties nil
  2134. "List of property/value pairs that can be inherited by any entry.
  2135. This list will be combined with the constant `org-global-properties-fixed'.
  2136. The entries in this list are cons cells where the car is a property
  2137. name and cdr is a string with the value.
  2138. You can set buffer-local values for the same purpose in the variable
  2139. `org-file-properties' this by adding lines like
  2140. #+PROPERTY: NAME VALUE"
  2141. :group 'org-properties
  2142. :type '(repeat
  2143. (cons (string :tag "Property")
  2144. (string :tag "Value"))))
  2145. (defvar org-file-properties nil
  2146. "List of property/value pairs that can be inherited by any entry.
  2147. Valid for the current buffer.
  2148. This variable is populated from #+PROPERTY lines.")
  2149. (make-variable-buffer-local 'org-file-properties)
  2150. (defgroup org-agenda nil
  2151. "Options concerning agenda views in Org-mode."
  2152. :tag "Org Agenda"
  2153. :group 'org)
  2154. (defvar org-category nil
  2155. "Variable used by org files to set a category for agenda display.
  2156. Such files should use a file variable to set it, for example
  2157. # -*- mode: org; org-category: \"ELisp\"
  2158. or contain a special line
  2159. #+CATEGORY: ELisp
  2160. If the file does not specify a category, then file's base name
  2161. is used instead.")
  2162. (make-variable-buffer-local 'org-category)
  2163. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2164. (defcustom org-agenda-files nil
  2165. "The files to be used for agenda display.
  2166. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2167. \\[org-remove-file]. You can also use customize to edit the list.
  2168. If an entry is a directory, all files in that directory that are matched by
  2169. `org-agenda-file-regexp' will be part of the file list.
  2170. If the value of the variable is not a list but a single file name, then
  2171. the list of agenda files is actually stored and maintained in that file, one
  2172. agenda file per line."
  2173. :group 'org-agenda
  2174. :type '(choice
  2175. (repeat :tag "List of files and directories" file)
  2176. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2177. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2178. "Regular expression to match files for `org-agenda-files'.
  2179. If any element in the list in that variable contains a directory instead
  2180. of a normal file, all files in that directory that are matched by this
  2181. regular expression will be included."
  2182. :group 'org-agenda
  2183. :type 'regexp)
  2184. (defcustom org-agenda-text-search-extra-files nil
  2185. "List of extra files to be searched by text search commands.
  2186. These files will be search in addition to the agenda files by the
  2187. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2188. Note that these files will only be searched for text search commands,
  2189. not for the other agenda views like todo lists, tag searches or the weekly
  2190. agenda. This variable is intended to list notes and possibly archive files
  2191. that should also be searched by these two commands.
  2192. In fact, if the first element in the list is the symbol `agenda-archives',
  2193. than all archive files of all agenda files will be added to the search
  2194. scope."
  2195. :group 'org-agenda
  2196. :type '(set :greedy t
  2197. (const :tag "Agenda Archives" agenda-archives)
  2198. (repeat :inline t (file))))
  2199. (if (fboundp 'defvaralias)
  2200. (defvaralias 'org-agenda-multi-occur-extra-files
  2201. 'org-agenda-text-search-extra-files))
  2202. (defcustom org-agenda-skip-unavailable-files nil
  2203. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2204. A nil value means to remove them, after a query, from the list."
  2205. :group 'org-agenda
  2206. :type 'boolean)
  2207. (defcustom org-calendar-to-agenda-key [?c]
  2208. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2209. The command `org-calendar-goto-agenda' will be bound to this key. The
  2210. default is the character `c' because then `c' can be used to switch back and
  2211. forth between agenda and calendar."
  2212. :group 'org-agenda
  2213. :type 'sexp)
  2214. (defcustom org-calendar-agenda-action-key [?k]
  2215. "The key to be installed in `calendar-mode-map' for agenda-action.
  2216. The command `org-agenda-action' will be bound to this key. The
  2217. default is the character `k' because we use the same key in the agenda."
  2218. :group 'org-agenda
  2219. :type 'sexp)
  2220. (eval-after-load "calendar"
  2221. '(progn
  2222. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2223. 'org-calendar-goto-agenda)
  2224. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2225. 'org-agenda-action)))
  2226. (defgroup org-latex nil
  2227. "Options for embedding LaTeX code into Org-mode."
  2228. :tag "Org LaTeX"
  2229. :group 'org)
  2230. (defcustom org-format-latex-options
  2231. '(:foreground default :background default :scale 1.0
  2232. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2233. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2234. "Options for creating images from LaTeX fragments.
  2235. This is a property list with the following properties:
  2236. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2237. `default' means use the foreground of the default face.
  2238. :background the background color, or \"Transparent\".
  2239. `default' means use the background of the default face.
  2240. :scale a scaling factor for the size of the images.
  2241. :html-foreground, :html-background, :html-scale
  2242. the same numbers for HTML export.
  2243. :matchers a list indicating which matchers should be used to
  2244. find LaTeX fragments. Valid members of this list are:
  2245. \"begin\" find environments
  2246. \"$1\" find single characters surrounded by $.$
  2247. \"$\" find math expressions surrounded by $...$
  2248. \"$$\" find math expressions surrounded by $$....$$
  2249. \"\\(\" find math expressions surrounded by \\(...\\)
  2250. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2251. :group 'org-latex
  2252. :type 'plist)
  2253. (defcustom org-format-latex-header "\\documentclass{article}
  2254. \\usepackage{fullpage} % do not remove
  2255. \\usepackage{amssymb}
  2256. \\usepackage[usenames]{color}
  2257. \\usepackage{amsmath}
  2258. \\usepackage{latexsym}
  2259. \\usepackage[mathscr]{eucal}
  2260. \\pagestyle{empty} % do not remove"
  2261. "The document header used for processing LaTeX fragments."
  2262. :group 'org-latex
  2263. :type 'string)
  2264. (defgroup org-font-lock nil
  2265. "Font-lock settings for highlighting in Org-mode."
  2266. :tag "Org Font Lock"
  2267. :group 'org)
  2268. (defcustom org-level-color-stars-only nil
  2269. "Non-nil means fontify only the stars in each headline.
  2270. When nil, the entire headline is fontified.
  2271. Changing it requires restart of `font-lock-mode' to become effective
  2272. also in regions already fontified."
  2273. :group 'org-font-lock
  2274. :type 'boolean)
  2275. (defcustom org-hide-leading-stars nil
  2276. "Non-nil means, hide the first N-1 stars in a headline.
  2277. This works by using the face `org-hide' for these stars. This
  2278. face is white for a light background, and black for a dark
  2279. background. You may have to customize the face `org-hide' to
  2280. make this work.
  2281. Changing it requires restart of `font-lock-mode' to become effective
  2282. also in regions already fontified.
  2283. You may also set this on a per-file basis by adding one of the following
  2284. lines to the buffer:
  2285. #+STARTUP: hidestars
  2286. #+STARTUP: showstars"
  2287. :group 'org-font-lock
  2288. :type 'boolean)
  2289. (defcustom org-fontify-done-headline nil
  2290. "Non-nil means, change the face of a headline if it is marked DONE.
  2291. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2292. When this is non-nil, the headline after the keyword is set to the
  2293. `org-headline-done' as an additional indication."
  2294. :group 'org-font-lock
  2295. :type 'boolean)
  2296. (defcustom org-fontify-emphasized-text t
  2297. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2298. Changing this variable requires a restart of Emacs to take effect."
  2299. :group 'org-font-lock
  2300. :type 'boolean)
  2301. (defcustom org-highlight-latex-fragments-and-specials nil
  2302. "Non-nil means, fontify what is treated specially by the exporters."
  2303. :group 'org-font-lock
  2304. :type 'boolean)
  2305. (defcustom org-hide-emphasis-markers nil
  2306. "Non-nil mean font-lock should hide the emphasis marker characters."
  2307. :group 'org-font-lock
  2308. :type 'boolean)
  2309. (defvar org-emph-re nil
  2310. "Regular expression for matching emphasis.")
  2311. (defvar org-verbatim-re nil
  2312. "Regular expression for matching verbatim text.")
  2313. (defvar org-emphasis-regexp-components) ; defined just below
  2314. (defvar org-emphasis-alist) ; defined just below
  2315. (defun org-set-emph-re (var val)
  2316. "Set variable and compute the emphasis regular expression."
  2317. (set var val)
  2318. (when (and (boundp 'org-emphasis-alist)
  2319. (boundp 'org-emphasis-regexp-components)
  2320. org-emphasis-alist org-emphasis-regexp-components)
  2321. (let* ((e org-emphasis-regexp-components)
  2322. (pre (car e))
  2323. (post (nth 1 e))
  2324. (border (nth 2 e))
  2325. (body (nth 3 e))
  2326. (nl (nth 4 e))
  2327. (body1 (concat body "*?"))
  2328. (markers (mapconcat 'car org-emphasis-alist ""))
  2329. (vmarkers (mapconcat
  2330. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2331. org-emphasis-alist "")))
  2332. ;; make sure special characters appear at the right position in the class
  2333. (if (string-match "\\^" markers)
  2334. (setq markers (concat (replace-match "" t t markers) "^")))
  2335. (if (string-match "-" markers)
  2336. (setq markers (concat (replace-match "" t t markers) "-")))
  2337. (if (string-match "\\^" vmarkers)
  2338. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2339. (if (string-match "-" vmarkers)
  2340. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2341. (if (> nl 0)
  2342. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2343. (int-to-string nl) "\\}")))
  2344. ;; Make the regexp
  2345. (setq org-emph-re
  2346. (concat "\\([" pre "]\\|^\\)"
  2347. "\\("
  2348. "\\([" markers "]\\)"
  2349. "\\("
  2350. "[^" border "]\\|"
  2351. "[^" border "]"
  2352. body1
  2353. "[^" border "]"
  2354. "\\)"
  2355. "\\3\\)"
  2356. "\\([" post "]\\|$\\)"))
  2357. (setq org-verbatim-re
  2358. (concat "\\([" pre "]\\|^\\)"
  2359. "\\("
  2360. "\\([" vmarkers "]\\)"
  2361. "\\("
  2362. "[^" border "]\\|"
  2363. "[^" border "]"
  2364. body1
  2365. "[^" border "]"
  2366. "\\)"
  2367. "\\3\\)"
  2368. "\\([" post "]\\|$\\)")))))
  2369. (defcustom org-emphasis-regexp-components
  2370. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2371. "Components used to build the regular expression for emphasis.
  2372. This is a list with 6 entries. Terminology: In an emphasis string
  2373. like \" *strong word* \", we call the initial space PREMATCH, the final
  2374. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2375. and \"trong wor\" is the body. The different components in this variable
  2376. specify what is allowed/forbidden in each part:
  2377. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2378. post Chars allowed as postmatch. End of line will be allowed too.
  2379. border The chars *forbidden* as border characters.
  2380. body-regexp A regexp like \".\" to match a body character. Don't use
  2381. non-shy groups here, and don't allow newline here.
  2382. newline The maximum number of newlines allowed in an emphasis exp.
  2383. Use customize to modify this, or restart Emacs after changing it."
  2384. :group 'org-font-lock
  2385. :set 'org-set-emph-re
  2386. :type '(list
  2387. (sexp :tag "Allowed chars in pre ")
  2388. (sexp :tag "Allowed chars in post ")
  2389. (sexp :tag "Forbidden chars in border ")
  2390. (sexp :tag "Regexp for body ")
  2391. (integer :tag "number of newlines allowed")
  2392. (option (boolean :tag "Please ignore this button"))))
  2393. (defcustom org-emphasis-alist
  2394. `(("*" bold "<b>" "</b>")
  2395. ("/" italic "<i>" "</i>")
  2396. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2397. ("=" org-code "<code>" "</code>" verbatim)
  2398. ("~" org-verbatim "<code>" "</code>" verbatim)
  2399. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2400. "<del>" "</del>")
  2401. )
  2402. "Special syntax for emphasized text.
  2403. Text starting and ending with a special character will be emphasized, for
  2404. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2405. characters, the face to be used by font-lock for highlighting in Org-mode
  2406. Emacs buffers, and the HTML tags to be used for this.
  2407. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2408. Use customize to modify this, or restart Emacs after changing it."
  2409. :group 'org-font-lock
  2410. :set 'org-set-emph-re
  2411. :type '(repeat
  2412. (list
  2413. (string :tag "Marker character")
  2414. (choice
  2415. (face :tag "Font-lock-face")
  2416. (plist :tag "Face property list"))
  2417. (string :tag "HTML start tag")
  2418. (string :tag "HTML end tag")
  2419. (option (const verbatim)))))
  2420. (defvar org-protecting-blocks
  2421. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2422. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2423. This is needed for font-lock setup.")
  2424. ;;; Miscellaneous options
  2425. (defgroup org-completion nil
  2426. "Completion in Org-mode."
  2427. :tag "Org Completion"
  2428. :group 'org)
  2429. (defcustom org-completion-use-ido nil
  2430. "Non-nil means, use ido completion wherever possible.
  2431. Note that `ido-mode' must be active for this variable to be relevant.
  2432. If you decide to turn this variable on, you might well want to turn off
  2433. `org-outline-path-complete-in-steps'."
  2434. :group 'org-completion
  2435. :type 'boolean)
  2436. (defcustom org-completion-fallback-command 'hippie-expand
  2437. "The expansion command called by \\[org-complete] in normal context.
  2438. Normal means, no org-mode-specific context."
  2439. :group 'org-completion
  2440. :type 'function)
  2441. ;;; Functions and variables from ther packages
  2442. ;; Declared here to avoid compiler warnings
  2443. ;; XEmacs only
  2444. (defvar outline-mode-menu-heading)
  2445. (defvar outline-mode-menu-show)
  2446. (defvar outline-mode-menu-hide)
  2447. (defvar zmacs-regions) ; XEmacs regions
  2448. ;; Emacs only
  2449. (defvar mark-active)
  2450. ;; Various packages
  2451. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2452. (declare-function calendar-forward-day "cal-move" (arg))
  2453. (declare-function calendar-goto-date "cal-move" (date))
  2454. (declare-function calendar-goto-today "cal-move" ())
  2455. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2456. (defvar calc-embedded-close-formula)
  2457. (defvar calc-embedded-open-formula)
  2458. (declare-function cdlatex-tab "ext:cdlatex" ())
  2459. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2460. (defvar font-lock-unfontify-region-function)
  2461. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2462. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2463. (defvar iswitchb-temp-buflist)
  2464. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2465. (defvar org-agenda-tags-todo-honor-ignore-options)
  2466. (declare-function org-agenda-skip "org-agenda" ())
  2467. (declare-function org-format-agenda-item "org-agenda"
  2468. (extra txt &optional category tags dotime noprefix remove-re))
  2469. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2470. (declare-function org-agenda-change-all-lines "org-agenda"
  2471. (newhead hdmarker &optional fixface just-this))
  2472. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2473. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2474. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2475. (beg end))
  2476. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2477. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2478. "org-agenda" (&optional end))
  2479. (declare-function parse-time-string "parse-time" (string))
  2480. (declare-function remember "remember" (&optional initial))
  2481. (declare-function remember-buffer-desc "remember" ())
  2482. (declare-function remember-finalize "remember" ())
  2483. (defvar remember-save-after-remembering)
  2484. (defvar remember-data-file)
  2485. (defvar remember-register)
  2486. (defvar remember-buffer)
  2487. (defvar remember-handler-functions)
  2488. (defvar remember-annotation-functions)
  2489. (defvar texmathp-why)
  2490. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2491. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2492. (defvar w3m-current-url)
  2493. (defvar w3m-current-title)
  2494. (defvar org-latex-regexps)
  2495. ;;; Autoload and prepare some org modules
  2496. ;; Some table stuff that needs to be defined here, because it is used
  2497. ;; by the functions setting up org-mode or checking for table context.
  2498. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2499. "Detects an org-type or table-type table.")
  2500. (defconst org-table-line-regexp "^[ \t]*|"
  2501. "Detects an org-type table line.")
  2502. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2503. "Detects an org-type table line.")
  2504. (defconst org-table-hline-regexp "^[ \t]*|-"
  2505. "Detects an org-type table hline.")
  2506. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2507. "Detects a table-type table hline.")
  2508. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2509. "Searching from within a table (any type) this finds the first line
  2510. outside the table.")
  2511. ;; Autoload the functions in org-table.el that are needed by functions here.
  2512. (eval-and-compile
  2513. (org-autoload "org-table"
  2514. '(org-table-align org-table-begin org-table-blank-field
  2515. org-table-convert org-table-convert-region org-table-copy-down
  2516. org-table-copy-region org-table-create
  2517. org-table-create-or-convert-from-region
  2518. org-table-create-with-table.el org-table-current-dline
  2519. org-table-cut-region org-table-delete-column org-table-edit-field
  2520. org-table-edit-formulas org-table-end org-table-eval-formula
  2521. org-table-export org-table-field-info
  2522. org-table-get-stored-formulas org-table-goto-column
  2523. org-table-hline-and-move org-table-import org-table-insert-column
  2524. org-table-insert-hline org-table-insert-row org-table-iterate
  2525. org-table-justify-field-maybe org-table-kill-row
  2526. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2527. org-table-move-column org-table-move-column-left
  2528. org-table-move-column-right org-table-move-row
  2529. org-table-move-row-down org-table-move-row-up
  2530. org-table-next-field org-table-next-row org-table-paste-rectangle
  2531. org-table-previous-field org-table-recalculate
  2532. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2533. org-table-toggle-coordinate-overlays
  2534. org-table-toggle-formula-debugger org-table-wrap-region
  2535. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2536. (defun org-at-table-p (&optional table-type)
  2537. "Return t if the cursor is inside an org-type table.
  2538. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2539. (if org-enable-table-editor
  2540. (save-excursion
  2541. (beginning-of-line 1)
  2542. (looking-at (if table-type org-table-any-line-regexp
  2543. org-table-line-regexp)))
  2544. nil))
  2545. (defsubst org-table-p () (org-at-table-p))
  2546. (defun org-at-table.el-p ()
  2547. "Return t if and only if we are at a table.el table."
  2548. (and (org-at-table-p 'any)
  2549. (save-excursion
  2550. (goto-char (org-table-begin 'any))
  2551. (looking-at org-table1-hline-regexp))))
  2552. (defun org-table-recognize-table.el ()
  2553. "If there is a table.el table nearby, recognize it and move into it."
  2554. (if org-table-tab-recognizes-table.el
  2555. (if (org-at-table.el-p)
  2556. (progn
  2557. (beginning-of-line 1)
  2558. (if (looking-at org-table-dataline-regexp)
  2559. nil
  2560. (if (looking-at org-table1-hline-regexp)
  2561. (progn
  2562. (beginning-of-line 2)
  2563. (if (looking-at org-table-any-border-regexp)
  2564. (beginning-of-line -1)))))
  2565. (if (re-search-forward "|" (org-table-end t) t)
  2566. (progn
  2567. (require 'table)
  2568. (if (table--at-cell-p (point))
  2569. t
  2570. (message "recognizing table.el table...")
  2571. (table-recognize-table)
  2572. (message "recognizing table.el table...done")))
  2573. (error "This should not happen..."))
  2574. t)
  2575. nil)
  2576. nil))
  2577. (defun org-at-table-hline-p ()
  2578. "Return t if the cursor is inside a hline in a table."
  2579. (if org-enable-table-editor
  2580. (save-excursion
  2581. (beginning-of-line 1)
  2582. (looking-at org-table-hline-regexp))
  2583. nil))
  2584. (defvar org-table-clean-did-remove-column nil)
  2585. (defun org-table-map-tables (function)
  2586. "Apply FUNCTION to the start of all tables in the buffer."
  2587. (save-excursion
  2588. (save-restriction
  2589. (widen)
  2590. (goto-char (point-min))
  2591. (while (re-search-forward org-table-any-line-regexp nil t)
  2592. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2593. (beginning-of-line 1)
  2594. (if (looking-at org-table-line-regexp)
  2595. (save-excursion (funcall function)))
  2596. (re-search-forward org-table-any-border-regexp nil 1))))
  2597. (message "Mapping tables: done"))
  2598. ;; Declare and autoload functions from org-exp.el & Co
  2599. (declare-function org-default-export-plist "org-exp")
  2600. (declare-function org-infile-export-plist "org-exp")
  2601. (declare-function org-get-current-options "org-exp")
  2602. (eval-and-compile
  2603. (org-autoload "org-exp"
  2604. '(org-export org-export-visible
  2605. org-insert-export-options-template
  2606. org-table-clean-before-export))
  2607. (org-autoload "org-ascii"
  2608. '(org-export-as-ascii org-export-ascii-preprocess
  2609. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2610. org-export-region-as-ascii))
  2611. (org-autoload "org-html"
  2612. '(org-export-as-html-and-open
  2613. org-export-as-html-batch org-export-as-html-to-buffer
  2614. org-replace-region-by-html org-export-region-as-html
  2615. org-export-as-html))
  2616. (org-autoload "org-icalendar"
  2617. '(org-export-icalendar-this-file
  2618. org-export-icalendar-all-agenda-files
  2619. org-export-icalendar-combine-agenda-files))
  2620. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2621. ;; Declare and autoload functions from org-agenda.el
  2622. (eval-and-compile
  2623. (org-autoload "org-agenda"
  2624. '(org-agenda org-agenda-list org-search-view
  2625. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2626. org-diary org-agenda-to-appt
  2627. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2628. ;; Autoload org-remember
  2629. (eval-and-compile
  2630. (org-autoload "org-remember"
  2631. '(org-remember-insinuate org-remember-annotation
  2632. org-remember-apply-template org-remember org-remember-handler)))
  2633. ;; Autoload org-clock.el
  2634. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2635. (beg end))
  2636. (declare-function org-clock-update-mode-line "org-clock" ())
  2637. (defvar org-clock-start-time)
  2638. (defvar org-clock-marker (make-marker)
  2639. "Marker recording the last clock-in.")
  2640. (eval-and-compile
  2641. (org-autoload
  2642. "org-clock"
  2643. '(org-clock-in org-clock-out org-clock-cancel
  2644. org-clock-goto org-clock-sum org-clock-display
  2645. org-clock-remove-overlays org-clock-report
  2646. org-clocktable-shift org-dblock-write:clocktable
  2647. org-get-clocktable)))
  2648. (defun org-clock-update-time-maybe ()
  2649. "If this is a CLOCK line, update it and return t.
  2650. Otherwise, return nil."
  2651. (interactive)
  2652. (save-excursion
  2653. (beginning-of-line 1)
  2654. (skip-chars-forward " \t")
  2655. (when (looking-at org-clock-string)
  2656. (let ((re (concat "[ \t]*" org-clock-string
  2657. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2658. "\\([ \t]*=>.*\\)?\\)?"))
  2659. ts te h m s neg)
  2660. (cond
  2661. ((not (looking-at re))
  2662. nil)
  2663. ((not (match-end 2))
  2664. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2665. (> org-clock-marker (point))
  2666. (<= org-clock-marker (point-at-eol)))
  2667. ;; The clock is running here
  2668. (setq org-clock-start-time
  2669. (apply 'encode-time
  2670. (org-parse-time-string (match-string 1))))
  2671. (org-clock-update-mode-line)))
  2672. (t
  2673. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2674. (end-of-line 1)
  2675. (setq ts (match-string 1)
  2676. te (match-string 3))
  2677. (setq s (- (time-to-seconds
  2678. (apply 'encode-time (org-parse-time-string te)))
  2679. (time-to-seconds
  2680. (apply 'encode-time (org-parse-time-string ts))))
  2681. neg (< s 0)
  2682. s (abs s)
  2683. h (floor (/ s 3600))
  2684. s (- s (* 3600 h))
  2685. m (floor (/ s 60))
  2686. s (- s (* 60 s)))
  2687. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2688. t))))))
  2689. (defun org-check-running-clock ()
  2690. "Check if the current buffer contains the running clock.
  2691. If yes, offer to stop it and to save the buffer with the changes."
  2692. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2693. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2694. (buffer-name))))
  2695. (org-clock-out)
  2696. (when (y-or-n-p "Save changed buffer?")
  2697. (save-buffer))))
  2698. (defun org-clocktable-try-shift (dir n)
  2699. "Check if this line starts a clock table, if yes, shift the time block."
  2700. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2701. (org-clocktable-shift dir n)))
  2702. ;; Autoload org-timer.el
  2703. (eval-and-compile
  2704. (org-autoload
  2705. "org-timer"
  2706. '(org-timer-start org-timer org-timer-item
  2707. org-timer-change-times-in-region)))
  2708. ;; Autoload org-feed.el
  2709. (eval-and-compile
  2710. (org-autoload
  2711. "org-feed"
  2712. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2713. ;; Autoload archiving code
  2714. ;; The stuff that is needed for cycling and tags has to be defined here.
  2715. (defgroup org-archive nil
  2716. "Options concerning archiving in Org-mode."
  2717. :tag "Org Archive"
  2718. :group 'org-structure)
  2719. (defcustom org-archive-location "%s_archive::"
  2720. "The location where subtrees should be archived.
  2721. The value of this variable is a string, consisting of two parts,
  2722. separated by a double-colon. The first part is a filename and
  2723. the second part is a headline.
  2724. When the filename is omitted, archiving happens in the same file.
  2725. %s in the filename will be replaced by the current file
  2726. name (without the directory part). Archiving to a different file
  2727. is useful to keep archived entries from contributing to the
  2728. Org-mode Agenda.
  2729. The archived entries will be filed as subtrees of the specified
  2730. headline. When the headline is omitted, the subtrees are simply
  2731. filed away at the end of the file, as top-level entries. Also in
  2732. the heading you can use %s to represent the file name, this can be
  2733. useful when using the same archive for a number of different files.
  2734. Here are a few examples:
  2735. \"%s_archive::\"
  2736. If the current file is Projects.org, archive in file
  2737. Projects.org_archive, as top-level trees. This is the default.
  2738. \"::* Archived Tasks\"
  2739. Archive in the current file, under the top-level headline
  2740. \"* Archived Tasks\".
  2741. \"~/org/archive.org::\"
  2742. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2743. \"~/org/archive.org::From %s\"
  2744. Archive in file ~/org/archive.org (absolute path), und headlines
  2745. \"From FILENAME\" where file name is the current file name.
  2746. \"basement::** Finished Tasks\"
  2747. Archive in file ./basement (relative path), as level 3 trees
  2748. below the level 2 heading \"** Finished Tasks\".
  2749. You may set this option on a per-file basis by adding to the buffer a
  2750. line like
  2751. #+ARCHIVE: basement::** Finished Tasks
  2752. You may also define it locally for a subtree by setting an ARCHIVE property
  2753. in the entry. If such a property is found in an entry, or anywhere up
  2754. the hierarchy, it will be used."
  2755. :group 'org-archive
  2756. :type 'string)
  2757. (defcustom org-archive-tag "ARCHIVE"
  2758. "The tag that marks a subtree as archived.
  2759. An archived subtree does not open during visibility cycling, and does
  2760. not contribute to the agenda listings.
  2761. After changing this, font-lock must be restarted in the relevant buffers to
  2762. get the proper fontification."
  2763. :group 'org-archive
  2764. :group 'org-keywords
  2765. :type 'string)
  2766. (defcustom org-agenda-skip-archived-trees t
  2767. "Non-nil means, the agenda will skip any items located in archived trees.
  2768. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2769. variable is no longer recommended, you should leave it at the value t.
  2770. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2771. :group 'org-archive
  2772. :group 'org-agenda-skip
  2773. :type 'boolean)
  2774. (defcustom org-columns-skip-arrchived-trees t
  2775. "Non-nil means, irgnore archived trees when creating column view."
  2776. :group 'org-archive
  2777. :group 'org-properties
  2778. :type 'boolean)
  2779. (defcustom org-cycle-open-archived-trees nil
  2780. "Non-nil means, `org-cycle' will open archived trees.
  2781. An archived tree is a tree marked with the tag ARCHIVE.
  2782. When nil, archived trees will stay folded. You can still open them with
  2783. normal outline commands like `show-all', but not with the cycling commands."
  2784. :group 'org-archive
  2785. :group 'org-cycle
  2786. :type 'boolean)
  2787. (defcustom org-sparse-tree-open-archived-trees nil
  2788. "Non-nil means sparse tree construction shows matches in archived trees.
  2789. When nil, matches in these trees are highlighted, but the trees are kept in
  2790. collapsed state."
  2791. :group 'org-archive
  2792. :group 'org-sparse-trees
  2793. :type 'boolean)
  2794. (defun org-cycle-hide-archived-subtrees (state)
  2795. "Re-hide all archived subtrees after a visibility state change."
  2796. (when (and (not org-cycle-open-archived-trees)
  2797. (not (memq state '(overview folded))))
  2798. (save-excursion
  2799. (let* ((globalp (memq state '(contents all)))
  2800. (beg (if globalp (point-min) (point)))
  2801. (end (if globalp (point-max) (org-end-of-subtree t))))
  2802. (org-hide-archived-subtrees beg end)
  2803. (goto-char beg)
  2804. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2805. (message "%s" (substitute-command-keys
  2806. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2807. (defun org-force-cycle-archived ()
  2808. "Cycle subtree even if it is archived."
  2809. (interactive)
  2810. (setq this-command 'org-cycle)
  2811. (let ((org-cycle-open-archived-trees t))
  2812. (call-interactively 'org-cycle)))
  2813. (defun org-hide-archived-subtrees (beg end)
  2814. "Re-hide all archived subtrees after a visibility state change."
  2815. (save-excursion
  2816. (let* ((re (concat ":" org-archive-tag ":")))
  2817. (goto-char beg)
  2818. (while (re-search-forward re end t)
  2819. (and (org-on-heading-p) (hide-subtree))
  2820. (org-end-of-subtree t)))))
  2821. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2822. (eval-and-compile
  2823. (org-autoload "org-archive"
  2824. '(org-add-archive-files org-archive-subtree
  2825. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2826. ;; Autoload Column View Code
  2827. (declare-function org-columns-number-to-string "org-colview")
  2828. (declare-function org-columns-get-format-and-top-level "org-colview")
  2829. (declare-function org-columns-compute "org-colview")
  2830. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2831. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2832. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2833. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2834. ;; Autoload ID code
  2835. (declare-function org-id-store-link "org-id")
  2836. (declare-function org-id-locations-load "org-id")
  2837. (declare-function org-id-locations-save "org-id")
  2838. (defvar org-id-track-globally)
  2839. (org-autoload "org-id"
  2840. '(org-id-get-create org-id-new org-id-copy org-id-get
  2841. org-id-get-with-outline-path-completion
  2842. org-id-get-with-outline-drilling
  2843. org-id-goto org-id-find org-id-store-link))
  2844. ;; Autoload Plotting Code
  2845. (org-autoload "org-plot"
  2846. '(org-plot/gnuplot))
  2847. ;;; Variables for pre-computed regular expressions, all buffer local
  2848. (defvar org-drawer-regexp nil
  2849. "Matches first line of a hidden block.")
  2850. (make-variable-buffer-local 'org-drawer-regexp)
  2851. (defvar org-todo-regexp nil
  2852. "Matches any of the TODO state keywords.")
  2853. (make-variable-buffer-local 'org-todo-regexp)
  2854. (defvar org-not-done-regexp nil
  2855. "Matches any of the TODO state keywords except the last one.")
  2856. (make-variable-buffer-local 'org-not-done-regexp)
  2857. (defvar org-not-done-heading-regexp nil
  2858. "Matches a TODO headline that is not done.")
  2859. (make-variable-buffer-local 'org-not-done-regexp)
  2860. (defvar org-todo-line-regexp nil
  2861. "Matches a headline and puts TODO state into group 2 if present.")
  2862. (make-variable-buffer-local 'org-todo-line-regexp)
  2863. (defvar org-complex-heading-regexp nil
  2864. "Matches a headline and puts everything into groups:
  2865. group 1: the stars
  2866. group 2: The todo keyword, maybe
  2867. group 3: Priority cookie
  2868. group 4: True headline
  2869. group 5: Tags")
  2870. (make-variable-buffer-local 'org-complex-heading-regexp)
  2871. (defvar org-todo-line-tags-regexp nil
  2872. "Matches a headline and puts TODO state into group 2 if present.
  2873. Also put tags into group 4 if tags are present.")
  2874. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2875. (defvar org-nl-done-regexp nil
  2876. "Matches newline followed by a headline with the DONE keyword.")
  2877. (make-variable-buffer-local 'org-nl-done-regexp)
  2878. (defvar org-looking-at-done-regexp nil
  2879. "Matches the DONE keyword a point.")
  2880. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2881. (defvar org-ds-keyword-length 12
  2882. "Maximum length of the Deadline and SCHEDULED keywords.")
  2883. (make-variable-buffer-local 'org-ds-keyword-length)
  2884. (defvar org-deadline-regexp nil
  2885. "Matches the DEADLINE keyword.")
  2886. (make-variable-buffer-local 'org-deadline-regexp)
  2887. (defvar org-deadline-time-regexp nil
  2888. "Matches the DEADLINE keyword together with a time stamp.")
  2889. (make-variable-buffer-local 'org-deadline-time-regexp)
  2890. (defvar org-deadline-line-regexp nil
  2891. "Matches the DEADLINE keyword and the rest of the line.")
  2892. (make-variable-buffer-local 'org-deadline-line-regexp)
  2893. (defvar org-scheduled-regexp nil
  2894. "Matches the SCHEDULED keyword.")
  2895. (make-variable-buffer-local 'org-scheduled-regexp)
  2896. (defvar org-scheduled-time-regexp nil
  2897. "Matches the SCHEDULED keyword together with a time stamp.")
  2898. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2899. (defvar org-closed-time-regexp nil
  2900. "Matches the CLOSED keyword together with a time stamp.")
  2901. (make-variable-buffer-local 'org-closed-time-regexp)
  2902. (defvar org-keyword-time-regexp nil
  2903. "Matches any of the 4 keywords, together with the time stamp.")
  2904. (make-variable-buffer-local 'org-keyword-time-regexp)
  2905. (defvar org-keyword-time-not-clock-regexp nil
  2906. "Matches any of the 3 keywords, together with the time stamp.")
  2907. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2908. (defvar org-maybe-keyword-time-regexp nil
  2909. "Matches a timestamp, possibly preceeded by a keyword.")
  2910. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2911. (defvar org-planning-or-clock-line-re nil
  2912. "Matches a line with planning or clock info.")
  2913. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2914. (defconst org-plain-time-of-day-regexp
  2915. (concat
  2916. "\\(\\<[012]?[0-9]"
  2917. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2918. "\\(--?"
  2919. "\\(\\<[012]?[0-9]"
  2920. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2921. "\\)?")
  2922. "Regular expression to match a plain time or time range.
  2923. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2924. groups carry important information:
  2925. 0 the full match
  2926. 1 the first time, range or not
  2927. 8 the second time, if it is a range.")
  2928. (defconst org-plain-time-extension-regexp
  2929. (concat
  2930. "\\(\\<[012]?[0-9]"
  2931. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2932. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2933. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2934. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2935. groups carry important information:
  2936. 0 the full match
  2937. 7 hours of duration
  2938. 9 minutes of duration")
  2939. (defconst org-stamp-time-of-day-regexp
  2940. (concat
  2941. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2942. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2943. "\\(--?"
  2944. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2945. "Regular expression to match a timestamp time or time range.
  2946. After a match, the following groups carry important information:
  2947. 0 the full match
  2948. 1 date plus weekday, for backreferencing to make sure both times on same day
  2949. 2 the first time, range or not
  2950. 4 the second time, if it is a range.")
  2951. (defconst org-startup-options
  2952. '(("fold" org-startup-folded t)
  2953. ("overview" org-startup-folded t)
  2954. ("nofold" org-startup-folded nil)
  2955. ("showall" org-startup-folded nil)
  2956. ("content" org-startup-folded content)
  2957. ("hidestars" org-hide-leading-stars t)
  2958. ("showstars" org-hide-leading-stars nil)
  2959. ("odd" org-odd-levels-only t)
  2960. ("oddeven" org-odd-levels-only nil)
  2961. ("align" org-startup-align-all-tables t)
  2962. ("noalign" org-startup-align-all-tables nil)
  2963. ("customtime" org-display-custom-times t)
  2964. ("logdone" org-log-done time)
  2965. ("lognotedone" org-log-done note)
  2966. ("nologdone" org-log-done nil)
  2967. ("lognoteclock-out" org-log-note-clock-out t)
  2968. ("nolognoteclock-out" org-log-note-clock-out nil)
  2969. ("logrepeat" org-log-repeat state)
  2970. ("lognoterepeat" org-log-repeat note)
  2971. ("nologrepeat" org-log-repeat nil)
  2972. ("fninline" org-footnote-define-inline t)
  2973. ("nofninline" org-footnote-define-inline nil)
  2974. ("fnlocal" org-footnote-section nil)
  2975. ("fnauto" org-footnote-auto-label t)
  2976. ("fnprompt" org-footnote-auto-label nil)
  2977. ("fnconfirm" org-footnote-auto-label confirm)
  2978. ("fnplain" org-footnote-auto-label plain)
  2979. ("constcgs" constants-unit-system cgs)
  2980. ("constSI" constants-unit-system SI)
  2981. ("noptag" org-tag-persistent-alist nil)
  2982. ("hideblocks" org-hide-block-startup t)
  2983. ("nohideblocks" org-hide-block-startup nil))
  2984. "Variable associated with STARTUP options for org-mode.
  2985. Each element is a list of three items: The startup options as written
  2986. in the #+STARTUP line, the corresponding variable, and the value to
  2987. set this variable to if the option is found. An optional forth element PUSH
  2988. means to push this value onto the list in the variable.")
  2989. (defun org-set-regexps-and-options ()
  2990. "Precompute regular expressions for current buffer."
  2991. (when (org-mode-p)
  2992. (org-set-local 'org-todo-kwd-alist nil)
  2993. (org-set-local 'org-todo-key-alist nil)
  2994. (org-set-local 'org-todo-key-trigger nil)
  2995. (org-set-local 'org-todo-keywords-1 nil)
  2996. (org-set-local 'org-done-keywords nil)
  2997. (org-set-local 'org-todo-heads nil)
  2998. (org-set-local 'org-todo-sets nil)
  2999. (org-set-local 'org-todo-log-states nil)
  3000. (org-set-local 'org-file-properties nil)
  3001. (org-set-local 'org-file-tags nil)
  3002. (let ((re (org-make-options-regexp
  3003. '("CATEGORY" "TODO" "COLUMNS"
  3004. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3005. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3006. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3007. (splitre "[ \t]+")
  3008. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3009. tail sep kws1 prio props ftags drawers
  3010. ext-setup-or-nil setup-contents (start 0))
  3011. (save-excursion
  3012. (save-restriction
  3013. (widen)
  3014. (goto-char (point-min))
  3015. (while (or (and ext-setup-or-nil
  3016. (string-match re ext-setup-or-nil start)
  3017. (setq start (match-end 0)))
  3018. (and (setq ext-setup-or-nil nil start 0)
  3019. (re-search-forward re nil t)))
  3020. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3021. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3022. (cond
  3023. ((equal key "CATEGORY")
  3024. (if (string-match "[ \t]+$" value)
  3025. (setq value (replace-match "" t t value)))
  3026. (setq cat value))
  3027. ((member key '("SEQ_TODO" "TODO"))
  3028. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3029. ((equal key "TYP_TODO")
  3030. (push (cons 'type (org-split-string value splitre)) kwds))
  3031. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3032. ;; general TODO-like setup
  3033. (push (cons (intern (downcase (match-string 1 key)))
  3034. (org-split-string value splitre)) kwds))
  3035. ((equal key "TAGS")
  3036. (setq tags (append tags (if tags '("\\n") nil)
  3037. (org-split-string value splitre))))
  3038. ((equal key "COLUMNS")
  3039. (org-set-local 'org-columns-default-format value))
  3040. ((equal key "LINK")
  3041. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3042. (push (cons (match-string 1 value)
  3043. (org-trim (match-string 2 value)))
  3044. links)))
  3045. ((equal key "PRIORITIES")
  3046. (setq prio (org-split-string value " +")))
  3047. ((equal key "PROPERTY")
  3048. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3049. (push (cons (match-string 1 value) (match-string 2 value))
  3050. props)))
  3051. ((equal key "FILETAGS")
  3052. (when (string-match "\\S-" value)
  3053. (setq ftags
  3054. (append
  3055. ftags
  3056. (apply 'append
  3057. (mapcar (lambda (x) (org-split-string x ":"))
  3058. (org-split-string value)))))))
  3059. ((equal key "DRAWERS")
  3060. (setq drawers (org-split-string value splitre)))
  3061. ((equal key "CONSTANTS")
  3062. (setq const (append const (org-split-string value splitre))))
  3063. ((equal key "STARTUP")
  3064. (let ((opts (org-split-string value splitre))
  3065. l var val)
  3066. (while (setq l (pop opts))
  3067. (when (setq l (assoc l org-startup-options))
  3068. (setq var (nth 1 l) val (nth 2 l))
  3069. (if (not (nth 3 l))
  3070. (set (make-local-variable var) val)
  3071. (if (not (listp (symbol-value var)))
  3072. (set (make-local-variable var) nil))
  3073. (set (make-local-variable var) (symbol-value var))
  3074. (add-to-list var val))))))
  3075. ((equal key "ARCHIVE")
  3076. (string-match " *$" value)
  3077. (setq arch (replace-match "" t t value))
  3078. (remove-text-properties 0 (length arch)
  3079. '(face t fontified t) arch))
  3080. ((equal key "SETUPFILE")
  3081. (setq setup-contents (org-file-contents
  3082. (expand-file-name
  3083. (org-remove-double-quotes value))
  3084. 'noerror))
  3085. (if (not ext-setup-or-nil)
  3086. (setq ext-setup-or-nil setup-contents start 0)
  3087. (setq ext-setup-or-nil
  3088. (concat (substring ext-setup-or-nil 0 start)
  3089. "\n" setup-contents "\n"
  3090. (substring ext-setup-or-nil start)))))
  3091. ))))
  3092. (when cat
  3093. (org-set-local 'org-category (intern cat))
  3094. (push (cons "CATEGORY" cat) props))
  3095. (when prio
  3096. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3097. (setq prio (mapcar 'string-to-char prio))
  3098. (org-set-local 'org-highest-priority (nth 0 prio))
  3099. (org-set-local 'org-lowest-priority (nth 1 prio))
  3100. (org-set-local 'org-default-priority (nth 2 prio)))
  3101. (and props (org-set-local 'org-file-properties (nreverse props)))
  3102. (and ftags (org-set-local 'org-file-tags
  3103. (mapcar 'org-add-prop-inherited ftags)))
  3104. (and drawers (org-set-local 'org-drawers drawers))
  3105. (and arch (org-set-local 'org-archive-location arch))
  3106. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3107. ;; Process the TODO keywords
  3108. (unless kwds
  3109. ;; Use the global values as if they had been given locally.
  3110. (setq kwds (default-value 'org-todo-keywords))
  3111. (if (stringp (car kwds))
  3112. (setq kwds (list (cons org-todo-interpretation
  3113. (default-value 'org-todo-keywords)))))
  3114. (setq kwds (reverse kwds)))
  3115. (setq kwds (nreverse kwds))
  3116. (let (inter kws kw)
  3117. (while (setq kws (pop kwds))
  3118. (let ((kws (or
  3119. (run-hook-with-args-until-success
  3120. 'org-todo-setup-filter-hook kws)
  3121. kws)))
  3122. (setq inter (pop kws) sep (member "|" kws)
  3123. kws0 (delete "|" (copy-sequence kws))
  3124. kwsa nil
  3125. kws1 (mapcar
  3126. (lambda (x)
  3127. ;; 1 2
  3128. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3129. (progn
  3130. (setq kw (match-string 1 x)
  3131. key (and (match-end 2) (match-string 2 x))
  3132. log (org-extract-log-state-settings x))
  3133. (push (cons kw (and key (string-to-char key))) kwsa)
  3134. (and log (push log org-todo-log-states))
  3135. kw)
  3136. (error "Invalid TODO keyword %s" x)))
  3137. kws0)
  3138. kwsa (if kwsa (append '((:startgroup))
  3139. (nreverse kwsa)
  3140. '((:endgroup))))
  3141. hw (car kws1)
  3142. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3143. tail (list inter hw (car dws) (org-last dws))))
  3144. (add-to-list 'org-todo-heads hw 'append)
  3145. (push kws1 org-todo-sets)
  3146. (setq org-done-keywords (append org-done-keywords dws nil))
  3147. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3148. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3149. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3150. (setq org-todo-sets (nreverse org-todo-sets)
  3151. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3152. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3153. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3154. ;; Process the constants
  3155. (when const
  3156. (let (e cst)
  3157. (while (setq e (pop const))
  3158. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3159. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3160. (setq org-table-formula-constants-local cst)))
  3161. ;; Process the tags.
  3162. (when tags
  3163. (let (e tgs)
  3164. (while (setq e (pop tags))
  3165. (cond
  3166. ((equal e "{") (push '(:startgroup) tgs))
  3167. ((equal e "}") (push '(:endgroup) tgs))
  3168. ((equal e "\\n") (push '(:newline) tgs))
  3169. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3170. (push (cons (match-string 1 e)
  3171. (string-to-char (match-string 2 e)))
  3172. tgs))
  3173. (t (push (list e) tgs))))
  3174. (org-set-local 'org-tag-alist nil)
  3175. (while (setq e (pop tgs))
  3176. (or (and (stringp (car e))
  3177. (assoc (car e) org-tag-alist))
  3178. (push e org-tag-alist)))))
  3179. ;; Compute the regular expressions and other local variables
  3180. (if (not org-done-keywords)
  3181. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3182. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3183. (length org-scheduled-string)
  3184. (length org-clock-string)
  3185. (length org-closed-string)))
  3186. org-drawer-regexp
  3187. (concat "^[ \t]*:\\("
  3188. (mapconcat 'regexp-quote org-drawers "\\|")
  3189. "\\):[ \t]*$")
  3190. org-not-done-keywords
  3191. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3192. org-todo-regexp
  3193. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3194. "\\|") "\\)\\>")
  3195. org-not-done-regexp
  3196. (concat "\\<\\("
  3197. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3198. "\\)\\>")
  3199. org-not-done-heading-regexp
  3200. (concat "^\\(\\*+\\)[ \t]+\\("
  3201. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3202. "\\)\\>")
  3203. org-todo-line-regexp
  3204. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3205. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3206. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3207. org-complex-heading-regexp
  3208. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3209. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3210. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3211. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3212. org-nl-done-regexp
  3213. (concat "\n\\*+[ \t]+"
  3214. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3215. "\\)" "\\>")
  3216. org-todo-line-tags-regexp
  3217. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3218. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3219. (org-re
  3220. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3221. org-looking-at-done-regexp
  3222. (concat "^" "\\(?:"
  3223. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3224. "\\>")
  3225. org-deadline-regexp (concat "\\<" org-deadline-string)
  3226. org-deadline-time-regexp
  3227. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3228. org-deadline-line-regexp
  3229. (concat "\\<\\(" org-deadline-string "\\).*")
  3230. org-scheduled-regexp
  3231. (concat "\\<" org-scheduled-string)
  3232. org-scheduled-time-regexp
  3233. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3234. org-closed-time-regexp
  3235. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3236. org-keyword-time-regexp
  3237. (concat "\\<\\(" org-scheduled-string
  3238. "\\|" org-deadline-string
  3239. "\\|" org-closed-string
  3240. "\\|" org-clock-string "\\)"
  3241. " *[[<]\\([^]>]+\\)[]>]")
  3242. org-keyword-time-not-clock-regexp
  3243. (concat "\\<\\(" org-scheduled-string
  3244. "\\|" org-deadline-string
  3245. "\\|" org-closed-string
  3246. "\\)"
  3247. " *[[<]\\([^]>]+\\)[]>]")
  3248. org-maybe-keyword-time-regexp
  3249. (concat "\\(\\<\\(" org-scheduled-string
  3250. "\\|" org-deadline-string
  3251. "\\|" org-closed-string
  3252. "\\|" org-clock-string "\\)\\)?"
  3253. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3254. org-planning-or-clock-line-re
  3255. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3256. "\\|" org-deadline-string
  3257. "\\|" org-closed-string "\\|" org-clock-string
  3258. "\\)\\>\\)")
  3259. )
  3260. (org-compute-latex-and-specials-regexp)
  3261. (org-set-font-lock-defaults))))
  3262. (defun org-file-contents (file &optional noerror)
  3263. "Return the contents of FILE, as a string."
  3264. (if (or (not file)
  3265. (not (file-readable-p file)))
  3266. (if noerror
  3267. (progn
  3268. (message "Cannot read file %s" file)
  3269. (ding) (sit-for 2)
  3270. "")
  3271. (error "Cannot read file %s" file))
  3272. (with-temp-buffer
  3273. (insert-file-contents file)
  3274. (buffer-string))))
  3275. (defun org-extract-log-state-settings (x)
  3276. "Extract the log state setting from a TODO keyword string.
  3277. This will extract info from a string like \"WAIT(w@/!)\"."
  3278. (let (kw key log1 log2)
  3279. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3280. (setq kw (match-string 1 x)
  3281. key (and (match-end 2) (match-string 2 x))
  3282. log1 (and (match-end 3) (match-string 3 x))
  3283. log2 (and (match-end 4) (match-string 4 x)))
  3284. (and (or log1 log2)
  3285. (list kw
  3286. (and log1 (if (equal log1 "!") 'time 'note))
  3287. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3288. (defun org-remove-keyword-keys (list)
  3289. "Remove a pair of parenthesis at the end of each string in LIST."
  3290. (mapcar (lambda (x)
  3291. (if (string-match "(.*)$" x)
  3292. (substring x 0 (match-beginning 0))
  3293. x))
  3294. list))
  3295. ;; FIXME: this could be done much better, using second characters etc.
  3296. (defun org-assign-fast-keys (alist)
  3297. "Assign fast keys to a keyword-key alist.
  3298. Respect keys that are already there."
  3299. (let (new e k c c1 c2 (char ?a))
  3300. (while (setq e (pop alist))
  3301. (cond
  3302. ((equal e '(:startgroup)) (push e new))
  3303. ((equal e '(:endgroup)) (push e new))
  3304. ((equal e '(:newline)) (push e new))
  3305. (t
  3306. (setq k (car e) c2 nil)
  3307. (if (cdr e)
  3308. (setq c (cdr e))
  3309. ;; automatically assign a character.
  3310. (setq c1 (string-to-char
  3311. (downcase (substring
  3312. k (if (= (string-to-char k) ?@) 1 0)))))
  3313. (if (or (rassoc c1 new) (rassoc c1 alist))
  3314. (while (or (rassoc char new) (rassoc char alist))
  3315. (setq char (1+ char)))
  3316. (setq c2 c1))
  3317. (setq c (or c2 char)))
  3318. (push (cons k c) new))))
  3319. (nreverse new)))
  3320. ;;; Some variables used in various places
  3321. (defvar org-window-configuration nil
  3322. "Used in various places to store a window configuration.")
  3323. (defvar org-finish-function nil
  3324. "Function to be called when `C-c C-c' is used.
  3325. This is for getting out of special buffers like remember.")
  3326. ;; FIXME: Occasionally check by commenting these, to make sure
  3327. ;; no other functions uses these, forgetting to let-bind them.
  3328. (defvar entry)
  3329. (defvar last-state)
  3330. (defvar date)
  3331. ;; Defined somewhere in this file, but used before definition.
  3332. (defvar org-html-entities)
  3333. (defvar org-struct-menu)
  3334. (defvar org-org-menu)
  3335. (defvar org-tbl-menu)
  3336. (defvar org-agenda-keymap)
  3337. ;;;; Define the Org-mode
  3338. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3339. (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."))
  3340. ;; We use a before-change function to check if a table might need
  3341. ;; an update.
  3342. (defvar org-table-may-need-update t
  3343. "Indicates that a table might need an update.
  3344. This variable is set by `org-before-change-function'.
  3345. `org-table-align' sets it back to nil.")
  3346. (defun org-before-change-function (beg end)
  3347. "Every change indicates that a table might need an update."
  3348. (setq org-table-may-need-update t))
  3349. (defvar org-mode-map)
  3350. (defvar org-mode-hook nil
  3351. "Mode hook for Org-mode, run after the mode was turned on.")
  3352. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3353. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3354. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3355. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3356. (defvar org-table-buffer-is-an nil)
  3357. (defconst org-outline-regexp "\\*+ ")
  3358. ;;;###autoload
  3359. (define-derived-mode org-mode outline-mode "Org"
  3360. "Outline-based notes management and organizer, alias
  3361. \"Carsten's outline-mode for keeping track of everything.\"
  3362. Org-mode develops organizational tasks around a NOTES file which
  3363. contains information about projects as plain text. Org-mode is
  3364. implemented on top of outline-mode, which is ideal to keep the content
  3365. of large files well structured. It supports ToDo items, deadlines and
  3366. time stamps, which magically appear in the diary listing of the Emacs
  3367. calendar. Tables are easily created with a built-in table editor.
  3368. Plain text URL-like links connect to websites, emails (VM), Usenet
  3369. messages (Gnus), BBDB entries, and any files related to the project.
  3370. For printing and sharing of notes, an Org-mode file (or a part of it)
  3371. can be exported as a structured ASCII or HTML file.
  3372. The following commands are available:
  3373. \\{org-mode-map}"
  3374. ;; Get rid of Outline menus, they are not needed
  3375. ;; Need to do this here because define-derived-mode sets up
  3376. ;; the keymap so late. Still, it is a waste to call this each time
  3377. ;; we switch another buffer into org-mode.
  3378. (if (featurep 'xemacs)
  3379. (when (boundp 'outline-mode-menu-heading)
  3380. ;; Assume this is Greg's port, it used easymenu
  3381. (easy-menu-remove outline-mode-menu-heading)
  3382. (easy-menu-remove outline-mode-menu-show)
  3383. (easy-menu-remove outline-mode-menu-hide))
  3384. (define-key org-mode-map [menu-bar headings] 'undefined)
  3385. (define-key org-mode-map [menu-bar hide] 'undefined)
  3386. (define-key org-mode-map [menu-bar show] 'undefined))
  3387. (org-load-modules-maybe)
  3388. (easy-menu-add org-org-menu)
  3389. (easy-menu-add org-tbl-menu)
  3390. (org-install-agenda-files-menu)
  3391. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3392. (org-add-to-invisibility-spec '(org-cwidth))
  3393. (org-add-to-invisibility-spec '(org-hide-block . t))
  3394. (when (featurep 'xemacs)
  3395. (org-set-local 'line-move-ignore-invisible t))
  3396. (org-set-local 'outline-regexp org-outline-regexp)
  3397. (org-set-local 'outline-level 'org-outline-level)
  3398. (when (and org-ellipsis
  3399. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3400. (fboundp 'make-glyph-code))
  3401. (unless org-display-table
  3402. (setq org-display-table (make-display-table)))
  3403. (set-display-table-slot
  3404. org-display-table 4
  3405. (vconcat (mapcar
  3406. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3407. org-ellipsis)))
  3408. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3409. (setq buffer-display-table org-display-table))
  3410. (org-set-regexps-and-options)
  3411. (when (and org-tag-faces (not org-tags-special-faces-re))
  3412. ;; tag faces set outside customize.... force initialization.
  3413. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3414. ;; Calc embedded
  3415. (org-set-local 'calc-embedded-open-mode "# ")
  3416. (modify-syntax-entry ?# "<")
  3417. (modify-syntax-entry ?@ "w")
  3418. (if org-startup-truncated (setq truncate-lines t))
  3419. (org-set-local 'font-lock-unfontify-region-function
  3420. 'org-unfontify-region)
  3421. ;; Activate before-change-function
  3422. (org-set-local 'org-table-may-need-update t)
  3423. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3424. 'local)
  3425. ;; Check for running clock before killing a buffer
  3426. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3427. ;; Paragraphs and auto-filling
  3428. (org-set-autofill-regexps)
  3429. (setq indent-line-function 'org-indent-line-function)
  3430. (org-update-radio-target-regexp)
  3431. ;; Make sure dependence stuff works reliably, even for users who set it
  3432. ;; too late :-(
  3433. (if org-enforce-todo-dependencies
  3434. (add-hook 'org-blocker-hook
  3435. 'org-block-todo-from-children-or-siblings-or-parent)
  3436. (remove-hook 'org-blocker-hook
  3437. 'org-block-todo-from-children-or-siblings-or-parent))
  3438. (if org-enforce-todo-checkbox-dependencies
  3439. (add-hook 'org-blocker-hook
  3440. 'org-block-todo-from-checkboxes)
  3441. (remove-hook 'org-blocker-hook
  3442. 'org-block-todo-from-checkboxes))
  3443. ;; Comment characters
  3444. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3445. (org-set-local 'comment-padding " ")
  3446. ;; Align options lines
  3447. (org-set-local
  3448. 'align-mode-rules-list
  3449. '((org-in-buffer-settings
  3450. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3451. (modes . '(org-mode)))))
  3452. ;; Imenu
  3453. (org-set-local 'imenu-create-index-function
  3454. 'org-imenu-get-tree)
  3455. ;; Make isearch reveal context
  3456. (if (or (featurep 'xemacs)
  3457. (not (boundp 'outline-isearch-open-invisible-function)))
  3458. ;; Emacs 21 and XEmacs make use of the hook
  3459. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3460. ;; Emacs 22 deals with this through a special variable
  3461. (org-set-local 'outline-isearch-open-invisible-function
  3462. (lambda (&rest ignore) (org-show-context 'isearch))))
  3463. ;; If empty file that did not turn on org-mode automatically, make it to.
  3464. (if (and org-insert-mode-line-in-empty-file
  3465. (interactive-p)
  3466. (= (point-min) (point-max)))
  3467. (insert "# -*- mode: org -*-\n\n"))
  3468. (unless org-inhibit-startup
  3469. (when org-startup-align-all-tables
  3470. (let ((bmp (buffer-modified-p)))
  3471. (org-table-map-tables 'org-table-align)
  3472. (set-buffer-modified-p bmp)))
  3473. (org-set-startup-visibility)))
  3474. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3475. (defun org-current-time ()
  3476. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3477. (if (> (car org-time-stamp-rounding-minutes) 1)
  3478. (let ((r (car org-time-stamp-rounding-minutes))
  3479. (time (decode-time)))
  3480. (apply 'encode-time
  3481. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3482. (nthcdr 2 time))))
  3483. (current-time)))
  3484. ;;;; Font-Lock stuff, including the activators
  3485. (defvar org-mouse-map (make-sparse-keymap))
  3486. (org-defkey org-mouse-map
  3487. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3488. (org-defkey org-mouse-map
  3489. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3490. (when org-mouse-1-follows-link
  3491. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3492. (when org-tab-follows-link
  3493. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3494. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3495. (require 'font-lock)
  3496. (defconst org-non-link-chars "]\t\n\r<>")
  3497. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3498. "shell" "elisp"))
  3499. (defvar org-link-types-re nil
  3500. "Matches a link that has a url-like prefix like \"http:\"")
  3501. (defvar org-link-re-with-space nil
  3502. "Matches a link with spaces, optional angular brackets around it.")
  3503. (defvar org-link-re-with-space2 nil
  3504. "Matches a link with spaces, optional angular brackets around it.")
  3505. (defvar org-link-re-with-space3 nil
  3506. "Matches a link with spaces, only for internal part in bracket links.")
  3507. (defvar org-angle-link-re nil
  3508. "Matches link with angular brackets, spaces are allowed.")
  3509. (defvar org-plain-link-re nil
  3510. "Matches plain link, without spaces.")
  3511. (defvar org-bracket-link-regexp nil
  3512. "Matches a link in double brackets.")
  3513. (defvar org-bracket-link-analytic-regexp nil
  3514. "Regular expression used to analyze links.
  3515. Here is what the match groups contain after a match:
  3516. 1: http:
  3517. 2: http
  3518. 3: path
  3519. 4: [desc]
  3520. 5: desc")
  3521. (defvar org-bracket-link-analytic-regexp++ nil
  3522. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3523. (defvar org-any-link-re nil
  3524. "Regular expression matching any link.")
  3525. (defun org-make-link-regexps ()
  3526. "Update the link regular expressions.
  3527. This should be called after the variable `org-link-types' has changed."
  3528. (setq org-link-types-re
  3529. (concat
  3530. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3531. org-link-re-with-space
  3532. (concat
  3533. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3534. "\\([^" org-non-link-chars " ]"
  3535. "[^" org-non-link-chars "]*"
  3536. "[^" org-non-link-chars " ]\\)>?")
  3537. org-link-re-with-space2
  3538. (concat
  3539. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3540. "\\([^" org-non-link-chars " ]"
  3541. "[^\t\n\r]*"
  3542. "[^" org-non-link-chars " ]\\)>?")
  3543. org-link-re-with-space3
  3544. (concat
  3545. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3546. "\\([^" org-non-link-chars " ]"
  3547. "[^\t\n\r]*\\)")
  3548. org-angle-link-re
  3549. (concat
  3550. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3551. "\\([^" org-non-link-chars " ]"
  3552. "[^" org-non-link-chars "]*"
  3553. "\\)>")
  3554. org-plain-link-re
  3555. (concat
  3556. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3557. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3558. org-bracket-link-regexp
  3559. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3560. org-bracket-link-analytic-regexp
  3561. (concat
  3562. "\\[\\["
  3563. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3564. "\\([^]]+\\)"
  3565. "\\]"
  3566. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3567. "\\]")
  3568. org-bracket-link-analytic-regexp++
  3569. (concat
  3570. "\\[\\["
  3571. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3572. "\\([^]]+\\)"
  3573. "\\]"
  3574. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3575. "\\]")
  3576. org-any-link-re
  3577. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3578. org-angle-link-re "\\)\\|\\("
  3579. org-plain-link-re "\\)")))
  3580. (org-make-link-regexps)
  3581. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3582. "Regular expression for fast time stamp matching.")
  3583. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3584. "Regular expression for fast time stamp matching.")
  3585. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3586. "Regular expression matching time strings for analysis.
  3587. This one does not require the space after the date, so it can be used
  3588. on a string that terminates immediately after the date.")
  3589. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3590. "Regular expression matching time strings for analysis.")
  3591. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3592. "Regular expression matching time stamps, with groups.")
  3593. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3594. "Regular expression matching time stamps (also [..]), with groups.")
  3595. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3596. "Regular expression matching a time stamp range.")
  3597. (defconst org-tr-regexp-both
  3598. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3599. "Regular expression matching a time stamp range.")
  3600. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3601. org-ts-regexp "\\)?")
  3602. "Regular expression matching a time stamp or time stamp range.")
  3603. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3604. org-ts-regexp-both "\\)?")
  3605. "Regular expression matching a time stamp or time stamp range.
  3606. The time stamps may be either active or inactive.")
  3607. (defvar org-emph-face nil)
  3608. (defun org-do-emphasis-faces (limit)
  3609. "Run through the buffer and add overlays to links."
  3610. (let (rtn a)
  3611. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3612. (if (not (= (char-after (match-beginning 3))
  3613. (char-after (match-beginning 4))))
  3614. (progn
  3615. (setq rtn t)
  3616. (setq a (assoc (match-string 3) org-emphasis-alist))
  3617. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3618. 'face
  3619. (nth 1 a))
  3620. (and (nth 4 a)
  3621. (org-remove-flyspell-overlays-in
  3622. (match-beginning 0) (match-end 0)))
  3623. (add-text-properties (match-beginning 2) (match-end 2)
  3624. '(font-lock-multiline t))
  3625. (when org-hide-emphasis-markers
  3626. (add-text-properties (match-end 4) (match-beginning 5)
  3627. '(invisible org-link))
  3628. (add-text-properties (match-beginning 3) (match-end 3)
  3629. '(invisible org-link)))))
  3630. (backward-char 1))
  3631. rtn))
  3632. (defun org-emphasize (&optional char)
  3633. "Insert or change an emphasis, i.e. a font like bold or italic.
  3634. If there is an active region, change that region to a new emphasis.
  3635. If there is no region, just insert the marker characters and position
  3636. the cursor between them.
  3637. CHAR should be either the marker character, or the first character of the
  3638. HTML tag associated with that emphasis. If CHAR is a space, the means
  3639. to remove the emphasis of the selected region.
  3640. If char is not given (for example in an interactive call) it
  3641. will be prompted for."
  3642. (interactive)
  3643. (let ((eal org-emphasis-alist) e det
  3644. (erc org-emphasis-regexp-components)
  3645. (prompt "")
  3646. (string "") beg end move tag c s)
  3647. (if (org-region-active-p)
  3648. (setq beg (region-beginning) end (region-end)
  3649. string (buffer-substring beg end))
  3650. (setq move t))
  3651. (while (setq e (pop eal))
  3652. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3653. c (aref tag 0))
  3654. (push (cons c (string-to-char (car e))) det)
  3655. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3656. (substring tag 1)))))
  3657. (setq det (nreverse det))
  3658. (unless char
  3659. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3660. (setq char (read-char-exclusive)))
  3661. (setq char (or (cdr (assoc char det)) char))
  3662. (if (equal char ?\ )
  3663. (setq s "" move nil)
  3664. (unless (assoc (char-to-string char) org-emphasis-alist)
  3665. (error "No such emphasis marker: \"%c\"" char))
  3666. (setq s (char-to-string char)))
  3667. (while (and (> (length string) 1)
  3668. (equal (substring string 0 1) (substring string -1))
  3669. (assoc (substring string 0 1) org-emphasis-alist))
  3670. (setq string (substring string 1 -1)))
  3671. (setq string (concat s string s))
  3672. (if beg (delete-region beg end))
  3673. (unless (or (bolp)
  3674. (string-match (concat "[" (nth 0 erc) "\n]")
  3675. (char-to-string (char-before (point)))))
  3676. (insert " "))
  3677. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3678. (char-to-string (char-after (point))))
  3679. (insert " ") (backward-char 1))
  3680. (insert string)
  3681. (and move (backward-char 1))))
  3682. (defconst org-nonsticky-props
  3683. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3684. (defsubst org-rear-nonsticky-at (pos)
  3685. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3686. (defun org-activate-plain-links (limit)
  3687. "Run through the buffer and add overlays to links."
  3688. (catch 'exit
  3689. (let (f)
  3690. (while (re-search-forward org-plain-link-re limit t)
  3691. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3692. (setq f (get-text-property (match-beginning 0) 'face))
  3693. (if (or (eq f 'org-tag)
  3694. (and (listp f) (memq 'org-tag f)))
  3695. nil
  3696. (add-text-properties (match-beginning 0) (match-end 0)
  3697. (list 'mouse-face 'highlight
  3698. 'keymap org-mouse-map))
  3699. (org-rear-nonsticky-at (match-end 0))
  3700. (throw 'exit t))))))
  3701. (defun org-activate-code (limit)
  3702. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3703. (progn
  3704. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3705. (remove-text-properties (match-beginning 0) (match-end 0)
  3706. '(display t invisible t intangible t))
  3707. t)))
  3708. (defun org-fontify-meta-lines-and-blocks (limit)
  3709. "Fontify #+ lines and blocks, in the correct ways."
  3710. (let ((case-fold-search t))
  3711. (if (re-search-forward
  3712. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3713. limit t)
  3714. (let ((beg (match-beginning 0))
  3715. (beg1 (line-beginning-position 2))
  3716. (dc1 (downcase (match-string 2)))
  3717. (dc3 (downcase (match-string 3)))
  3718. end end1 quoting)
  3719. (cond
  3720. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3721. ;; a single line of backend-specific content
  3722. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3723. (remove-text-properties (match-beginning 0) (match-end 0)
  3724. '(display t invisible t intangible t))
  3725. (add-text-properties (match-beginning 1) (match-end 3)
  3726. '(font-lock-fontified t face org-meta-line))
  3727. (add-text-properties (match-beginning 6) (match-end 6)
  3728. '(font-lock-fontified t face org-block))
  3729. t)
  3730. ((and (match-end 4) (equal dc3 "begin"))
  3731. ;; Truely a block
  3732. (setq quoting (member (downcase (match-string 5))
  3733. org-protecting-blocks))
  3734. (when (re-search-forward
  3735. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3736. nil t) ;; on purpose, we look further than LIMIT
  3737. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3738. (when quoting
  3739. (remove-text-properties beg end
  3740. '(display t invisible t intangible t)))
  3741. (add-text-properties
  3742. beg end
  3743. '(font-lock-fontified t font-lock-multiline t))
  3744. (add-text-properties beg beg1 '(face org-meta-line))
  3745. (add-text-properties end1 end '(face org-meta-line))
  3746. (when quoting
  3747. (add-text-properties beg1 end1 '(face org-block)))
  3748. t))
  3749. ((not (member (char-after beg) '(?\ ?\t)))
  3750. ;; just any other in-buffer setting, but not indented
  3751. (add-text-properties
  3752. beg (match-end 0)
  3753. '(font-lock-fontified t face org-meta-line))
  3754. t)
  3755. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:"))
  3756. (and (match-end 4) (equal dc3 "attr")))
  3757. (add-text-properties
  3758. beg (match-end 0)
  3759. '(font-lock-fontified t face org-meta-line))
  3760. t)
  3761. (t nil))))))
  3762. (defun org-activate-angle-links (limit)
  3763. "Run through the buffer and add overlays to links."
  3764. (if (re-search-forward org-angle-link-re limit t)
  3765. (progn
  3766. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3767. (add-text-properties (match-beginning 0) (match-end 0)
  3768. (list 'mouse-face 'highlight
  3769. 'keymap org-mouse-map))
  3770. (org-rear-nonsticky-at (match-end 0))
  3771. t)))
  3772. (defun org-activate-footnote-links (limit)
  3773. "Run through the buffer and add overlays to links."
  3774. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3775. limit t)
  3776. (progn
  3777. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3778. (add-text-properties (match-beginning 2) (match-end 2)
  3779. (list 'mouse-face 'highlight
  3780. 'keymap org-mouse-map
  3781. 'help-echo
  3782. (if (= (point-at-bol) (match-beginning 2))
  3783. "Footnote definition"
  3784. "Footnote reference")
  3785. ))
  3786. (org-rear-nonsticky-at (match-end 2))
  3787. t)))
  3788. (defun org-activate-bracket-links (limit)
  3789. "Run through the buffer and add overlays to bracketed links."
  3790. (if (re-search-forward org-bracket-link-regexp limit t)
  3791. (let* ((help (concat "LINK: "
  3792. (org-match-string-no-properties 1)))
  3793. ;; FIXME: above we should remove the escapes.
  3794. ;; but that requires another match, protecting match data,
  3795. ;; a lot of overhead for font-lock.
  3796. (ip (org-maybe-intangible
  3797. (list 'invisible 'org-link
  3798. 'keymap org-mouse-map 'mouse-face 'highlight
  3799. 'font-lock-multiline t 'help-echo help)))
  3800. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3801. 'font-lock-multiline t 'help-echo help)))
  3802. ;; We need to remove the invisible property here. Table narrowing
  3803. ;; may have made some of this invisible.
  3804. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3805. (remove-text-properties (match-beginning 0) (match-end 0)
  3806. '(invisible nil))
  3807. (if (match-end 3)
  3808. (progn
  3809. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3810. (org-rear-nonsticky-at (match-beginning 3))
  3811. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3812. (org-rear-nonsticky-at (match-end 3))
  3813. (add-text-properties (match-end 3) (match-end 0) ip)
  3814. (org-rear-nonsticky-at (match-end 0)))
  3815. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3816. (org-rear-nonsticky-at (match-beginning 1))
  3817. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3818. (org-rear-nonsticky-at (match-end 1))
  3819. (add-text-properties (match-end 1) (match-end 0) ip)
  3820. (org-rear-nonsticky-at (match-end 0)))
  3821. t)))
  3822. (defun org-activate-dates (limit)
  3823. "Run through the buffer and add overlays to dates."
  3824. (if (re-search-forward org-tsr-regexp-both limit t)
  3825. (progn
  3826. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3827. (add-text-properties (match-beginning 0) (match-end 0)
  3828. (list 'mouse-face 'highlight
  3829. 'keymap org-mouse-map))
  3830. (org-rear-nonsticky-at (match-end 0))
  3831. (when org-display-custom-times
  3832. (if (match-end 3)
  3833. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3834. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3835. t)))
  3836. (defvar org-target-link-regexp nil
  3837. "Regular expression matching radio targets in plain text.")
  3838. (make-variable-buffer-local 'org-target-link-regexp)
  3839. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3840. "Regular expression matching a link target.")
  3841. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3842. "Regular expression matching a radio target.")
  3843. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3844. "Regular expression matching any target.")
  3845. (defun org-activate-target-links (limit)
  3846. "Run through the buffer and add overlays to target matches."
  3847. (when org-target-link-regexp
  3848. (let ((case-fold-search t))
  3849. (if (re-search-forward org-target-link-regexp limit t)
  3850. (progn
  3851. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3852. (add-text-properties (match-beginning 0) (match-end 0)
  3853. (list 'mouse-face 'highlight
  3854. 'keymap org-mouse-map
  3855. 'help-echo "Radio target link"
  3856. 'org-linked-text t))
  3857. (org-rear-nonsticky-at (match-end 0))
  3858. t)))))
  3859. (defun org-update-radio-target-regexp ()
  3860. "Find all radio targets in this file and update the regular expression."
  3861. (interactive)
  3862. (when (memq 'radio org-activate-links)
  3863. (setq org-target-link-regexp
  3864. (org-make-target-link-regexp (org-all-targets 'radio)))
  3865. (org-restart-font-lock)))
  3866. (defun org-hide-wide-columns (limit)
  3867. (let (s e)
  3868. (setq s (text-property-any (point) (or limit (point-max))
  3869. 'org-cwidth t))
  3870. (when s
  3871. (setq e (next-single-property-change s 'org-cwidth))
  3872. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3873. (goto-char e)
  3874. t)))
  3875. (defvar org-latex-and-specials-regexp nil
  3876. "Regular expression for highlighting export special stuff.")
  3877. (defvar org-match-substring-regexp)
  3878. (defvar org-match-substring-with-braces-regexp)
  3879. (defvar org-export-html-special-string-regexps)
  3880. (defun org-compute-latex-and-specials-regexp ()
  3881. "Compute regular expression for stuff treated specially by exporters."
  3882. (if (not org-highlight-latex-fragments-and-specials)
  3883. (org-set-local 'org-latex-and-specials-regexp nil)
  3884. (require 'org-exp)
  3885. (let*
  3886. ((matchers (plist-get org-format-latex-options :matchers))
  3887. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3888. org-latex-regexps)))
  3889. (options (org-combine-plists (org-default-export-plist)
  3890. (org-infile-export-plist)))
  3891. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3892. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3893. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3894. (org-export-html-expand (plist-get options :expand-quoted-html))
  3895. (org-export-with-special-strings (plist-get options :special-strings))
  3896. (re-sub
  3897. (cond
  3898. ((equal org-export-with-sub-superscripts '{})
  3899. (list org-match-substring-with-braces-regexp))
  3900. (org-export-with-sub-superscripts
  3901. (list org-match-substring-regexp))
  3902. (t nil)))
  3903. (re-latex
  3904. (if org-export-with-LaTeX-fragments
  3905. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3906. (re-macros
  3907. (if org-export-with-TeX-macros
  3908. (list (concat "\\\\"
  3909. (regexp-opt
  3910. (append (mapcar 'car org-html-entities)
  3911. (if (boundp 'org-latex-entities)
  3912. (mapcar (lambda (x)
  3913. (or (car-safe x) x))
  3914. org-latex-entities)
  3915. nil))
  3916. 'words))) ; FIXME
  3917. ))
  3918. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3919. (re-special (if org-export-with-special-strings
  3920. (mapcar (lambda (x) (car x))
  3921. org-export-html-special-string-regexps)))
  3922. (re-rest
  3923. (delq nil
  3924. (list
  3925. (if org-export-html-expand "@<[^>\n]+>")
  3926. ))))
  3927. (org-set-local
  3928. 'org-latex-and-specials-regexp
  3929. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3930. re-rest) "\\|")))))
  3931. (defun org-do-latex-and-special-faces (limit)
  3932. "Run through the buffer and add overlays to links."
  3933. (when org-latex-and-specials-regexp
  3934. (let (rtn d)
  3935. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3936. limit t))
  3937. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3938. 'face))
  3939. '(org-code org-verbatim underline)))
  3940. (progn
  3941. (setq rtn t
  3942. d (cond ((member (char-after (1+ (match-beginning 0)))
  3943. '(?_ ?^)) 1)
  3944. (t 0)))
  3945. (font-lock-prepend-text-property
  3946. (+ d (match-beginning 0)) (match-end 0)
  3947. 'face 'org-latex-and-export-specials)
  3948. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3949. '(font-lock-multiline t)))))
  3950. rtn)))
  3951. (defun org-restart-font-lock ()
  3952. "Restart font-lock-mode, to force refontification."
  3953. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3954. (font-lock-mode -1)
  3955. (font-lock-mode 1)))
  3956. (defun org-all-targets (&optional radio)
  3957. "Return a list of all targets in this file.
  3958. With optional argument RADIO, only find radio targets."
  3959. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3960. rtn)
  3961. (save-excursion
  3962. (goto-char (point-min))
  3963. (while (re-search-forward re nil t)
  3964. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3965. rtn)))
  3966. (defun org-make-target-link-regexp (targets)
  3967. "Make regular expression matching all strings in TARGETS.
  3968. The regular expression finds the targets also if there is a line break
  3969. between words."
  3970. (and targets
  3971. (concat
  3972. "\\<\\("
  3973. (mapconcat
  3974. (lambda (x)
  3975. (while (string-match " +" x)
  3976. (setq x (replace-match "\\s-+" t t x)))
  3977. x)
  3978. targets
  3979. "\\|")
  3980. "\\)\\>")))
  3981. (defun org-activate-tags (limit)
  3982. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3983. (progn
  3984. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3985. (add-text-properties (match-beginning 1) (match-end 1)
  3986. (list 'mouse-face 'highlight
  3987. 'keymap org-mouse-map))
  3988. (org-rear-nonsticky-at (match-end 1))
  3989. t)))
  3990. (defun org-outline-level ()
  3991. (save-excursion
  3992. (looking-at outline-regexp)
  3993. (if (match-beginning 1)
  3994. (+ (org-get-string-indentation (match-string 1)) 1000)
  3995. (1- (- (match-end 0) (match-beginning 0))))))
  3996. (defvar org-font-lock-keywords nil)
  3997. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3998. "Regular expression matching a property line.")
  3999. (defvar org-font-lock-hook nil
  4000. "Functions to be called for special font lock stuff.")
  4001. (defun org-font-lock-hook (limit)
  4002. (run-hook-with-args 'org-font-lock-hook limit))
  4003. (defun org-set-font-lock-defaults ()
  4004. (let* ((em org-fontify-emphasized-text)
  4005. (lk org-activate-links)
  4006. (org-font-lock-extra-keywords
  4007. (list
  4008. ;; Call the hook
  4009. '(org-font-lock-hook)
  4010. ;; Headlines
  4011. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4012. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4013. ;; Table lines
  4014. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4015. (1 'org-table t))
  4016. ;; Table internals
  4017. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4018. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4019. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4020. ;; Drawers
  4021. (list org-drawer-regexp '(0 'org-special-keyword t))
  4022. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4023. ;; Properties
  4024. (list org-property-re
  4025. '(1 'org-special-keyword t)
  4026. '(3 'org-property-value t))
  4027. (if org-format-transports-properties-p
  4028. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4029. ;; Links
  4030. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4031. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4032. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4033. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4034. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4035. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4036. (if (memq 'footnote lk) '(org-activate-footnote-links
  4037. (2 'org-footnote t)))
  4038. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4039. '(org-hide-wide-columns (0 nil append))
  4040. ;; TODO lines
  4041. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4042. '(1 (org-get-todo-face 1) t))
  4043. ;; DONE
  4044. (if org-fontify-done-headline
  4045. (list (concat "^[*]+ +\\<\\("
  4046. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4047. "\\)\\(.*\\)")
  4048. '(2 'org-headline-done t))
  4049. nil)
  4050. ;; Priorities
  4051. '(org-font-lock-add-priority-faces)
  4052. ;; Tags
  4053. '(org-font-lock-add-tag-faces)
  4054. ;; Special keywords
  4055. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4056. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4057. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4058. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4059. ;; Emphasis
  4060. (if em
  4061. (if (featurep 'xemacs)
  4062. '(org-do-emphasis-faces (0 nil append))
  4063. '(org-do-emphasis-faces)))
  4064. ;; Checkboxes
  4065. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4066. 2 'org-checkbox prepend)
  4067. (if org-provide-checkbox-statistics
  4068. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4069. (0 (org-get-checkbox-statistics-face) t)))
  4070. ;; Description list items
  4071. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4072. 2 'bold prepend)
  4073. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4074. '(1 'org-archived prepend))
  4075. ;; Specials
  4076. '(org-do-latex-and-special-faces)
  4077. ;; Code
  4078. '(org-activate-code (1 'org-code t))
  4079. ;; COMMENT
  4080. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4081. "\\|" org-quote-string "\\)\\>")
  4082. '(1 'org-special-keyword t))
  4083. '("^#.*" (0 'font-lock-comment-face t))
  4084. ;; Blocks and meta lines
  4085. '(org-fontify-meta-lines-and-blocks)
  4086. )))
  4087. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4088. ;; Now set the full font-lock-keywords
  4089. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4090. (org-set-local 'font-lock-defaults
  4091. '(org-font-lock-keywords t nil nil backward-paragraph))
  4092. (kill-local-variable 'font-lock-keywords) nil))
  4093. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4094. "Fontify string S like in Org-mode"
  4095. (with-temp-buffer
  4096. (insert s)
  4097. (let ((org-odd-levels-only odd-levels))
  4098. (org-mode)
  4099. (font-lock-fontify-buffer)
  4100. (buffer-string))))
  4101. (defvar org-m nil)
  4102. (defvar org-l nil)
  4103. (defvar org-f nil)
  4104. (defun org-get-level-face (n)
  4105. "Get the right face for match N in font-lock matching of headlines."
  4106. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4107. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4108. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4109. (cond
  4110. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4111. ((eq n 2) org-f)
  4112. (t (if org-level-color-stars-only nil org-f))))
  4113. (defun org-get-todo-face (kwd)
  4114. "Get the right face for a TODO keyword KWD.
  4115. If KWD is a number, get the corresponding match group."
  4116. (if (numberp kwd) (setq kwd (match-string kwd)))
  4117. (or (cdr (assoc kwd org-todo-keyword-faces))
  4118. (and (member kwd org-done-keywords) 'org-done)
  4119. 'org-todo))
  4120. (defun org-font-lock-add-tag-faces (limit)
  4121. "Add the special tag faces."
  4122. (when (and org-tag-faces org-tags-special-faces-re)
  4123. (while (re-search-forward org-tags-special-faces-re limit t)
  4124. (add-text-properties (match-beginning 1) (match-end 1)
  4125. (list 'face (org-get-tag-face 1)
  4126. 'font-lock-fontified t))
  4127. (backward-char 1))))
  4128. (defun org-font-lock-add-priority-faces (limit)
  4129. "Add the special priority faces."
  4130. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4131. (add-text-properties
  4132. (match-beginning 0) (match-end 0)
  4133. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4134. org-priority-faces))
  4135. 'org-special-keyword)
  4136. 'font-lock-fontified t))))
  4137. (defun org-get-tag-face (kwd)
  4138. "Get the right face for a TODO keyword KWD.
  4139. If KWD is a number, get the corresponding match group."
  4140. (if (numberp kwd) (setq kwd (match-string kwd)))
  4141. (or (cdr (assoc kwd org-tag-faces))
  4142. 'org-tag))
  4143. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4144. "Remove fontification and activation overlays from links."
  4145. (font-lock-default-unfontify-region beg end)
  4146. (let* ((buffer-undo-list t)
  4147. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4148. (inhibit-modification-hooks t)
  4149. deactivate-mark buffer-file-name buffer-file-truename)
  4150. (remove-text-properties beg end
  4151. '(mouse-face t keymap t org-linked-text t
  4152. invisible t intangible t
  4153. org-no-flyspell t))))
  4154. ;;;; Visibility cycling, including org-goto and indirect buffer
  4155. ;;; Cycling
  4156. (defvar org-cycle-global-status nil)
  4157. (make-variable-buffer-local 'org-cycle-global-status)
  4158. (defvar org-cycle-subtree-status nil)
  4159. (make-variable-buffer-local 'org-cycle-subtree-status)
  4160. ;;;###autoload
  4161. (defvar org-inlinetask-min-level)
  4162. (defun org-cycle (&optional arg)
  4163. "TAB-action and visibility cycling for Org-mode.
  4164. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4165. is outine visibility cycling, but it also invokes other actions
  4166. in special contexts.
  4167. - When this function is called with a prefix argument, rotate the entire
  4168. buffer through 3 states (global cycling)
  4169. 1. OVERVIEW: Show only top-level headlines.
  4170. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4171. 3. SHOW ALL: Show everything.
  4172. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4173. determined by the variable `org-startup-folded', and by any VISIBILITY
  4174. properties in the buffer.
  4175. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4176. including any drawers.
  4177. - When inside a table, re-align the table and move to the next field.
  4178. - When point is at the beginning of a headline, rotate the subtree started
  4179. by this line through 3 different states (local cycling)
  4180. 1. FOLDED: Only the main headline is shown.
  4181. 2. CHILDREN: The main headline and the direct children are shown.
  4182. From this state, you can move to one of the children
  4183. and zoom in further.
  4184. 3. SUBTREE: Show the entire subtree, including body text.
  4185. - When there is a numeric prefix, go up to a heading with level ARG, do
  4186. a `show-subtree' and return to the previous cursor position. If ARG
  4187. is negative, go up that many levels.
  4188. - When point is not at the beginning of a headline, execute the global
  4189. binding for TAB, which is re-indenting the line. See the option
  4190. `org-cycle-emulate-tab' for details.
  4191. - Special case: if point is at the beginning of the buffer and there is
  4192. no headline in line 1, this function will act as if called with prefix arg.
  4193. But only if also the variable `org-cycle-global-at-bob' is t."
  4194. (interactive "P")
  4195. (org-load-modules-maybe)
  4196. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4197. (let* ((limit-level
  4198. (or org-cycle-max-level
  4199. (and (boundp 'org-inlinetask-min-level)
  4200. org-inlinetask-min-level
  4201. (1- org-inlinetask-min-level))))
  4202. (nstars (and limit-level
  4203. (if org-odd-levels-only
  4204. (and limit-level (1- (* limit-level 2)))
  4205. limit-level)))
  4206. (outline-regexp
  4207. (cond
  4208. ((not (org-mode-p)) outline-regexp)
  4209. (org-cycle-include-plain-lists
  4210. (concat "\\(?:\\*"
  4211. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4212. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4213. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4214. (bob-special (and org-cycle-global-at-bob (bobp)
  4215. (not (looking-at outline-regexp))))
  4216. (org-cycle-hook
  4217. (if bob-special
  4218. (delq 'org-optimize-window-after-visibility-change
  4219. (copy-sequence org-cycle-hook))
  4220. org-cycle-hook))
  4221. (pos (point)))
  4222. (if (or bob-special (equal arg '(4)))
  4223. ;; special case: use global cycling
  4224. (setq arg t))
  4225. (cond
  4226. ((equal arg '(16))
  4227. (org-set-startup-visibility)
  4228. (message "Startup visibility, plus VISIBILITY properties"))
  4229. ((equal arg '(64))
  4230. (show-all)
  4231. (message "Entire buffer visible, including drawers"))
  4232. ((org-at-table-p 'any)
  4233. ;; Enter the table or move to the next field in the table
  4234. (or (org-table-recognize-table.el)
  4235. (progn
  4236. (if arg (org-table-edit-field t)
  4237. (org-table-justify-field-maybe)
  4238. (call-interactively 'org-table-next-field)))))
  4239. ((run-hook-with-args-until-success
  4240. 'org-tab-after-check-for-table-hook))
  4241. ((eq arg t) ;; Global cycling
  4242. (org-cycle-internal-global))
  4243. ((and org-drawers org-drawer-regexp
  4244. (save-excursion
  4245. (beginning-of-line 1)
  4246. (looking-at org-drawer-regexp)))
  4247. ;; Toggle block visibility
  4248. (org-flag-drawer
  4249. (not (get-char-property (match-end 0) 'invisible))))
  4250. ((integerp arg)
  4251. ;; Show-subtree, ARG levels up from here.
  4252. (save-excursion
  4253. (org-back-to-heading)
  4254. (outline-up-heading (if (< arg 0) (- arg)
  4255. (- (funcall outline-level) arg)))
  4256. (org-show-subtree)))
  4257. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4258. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4259. (org-cycle-internal-local))
  4260. ;; TAB emulation and template completion
  4261. (buffer-read-only (org-back-to-heading))
  4262. ((run-hook-with-args-until-success
  4263. 'org-tab-after-check-for-cycling-hook))
  4264. ((org-try-structure-completion))
  4265. ((org-try-cdlatex-tab))
  4266. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4267. (or (not (bolp))
  4268. (not (looking-at outline-regexp))))
  4269. (call-interactively (global-key-binding "\t")))
  4270. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4271. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4272. (or (and (eq org-cycle-emulate-tab 'white)
  4273. (= (match-end 0) (point-at-eol)))
  4274. (and (eq org-cycle-emulate-tab 'whitestart)
  4275. (>= (match-end 0) pos))))
  4276. t
  4277. (eq org-cycle-emulate-tab t))
  4278. (call-interactively (global-key-binding "\t")))
  4279. (t (save-excursion
  4280. (org-back-to-heading)
  4281. (org-cycle)))))))
  4282. (defun org-cycle-internal-global ()
  4283. "Do the global cycling action."
  4284. (cond
  4285. ((and (eq last-command this-command)
  4286. (eq org-cycle-global-status 'overview))
  4287. ;; We just created the overview - now do table of contents
  4288. ;; This can be slow in very large buffers, so indicate action
  4289. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4290. (message "CONTENTS...")
  4291. (org-content)
  4292. (message "CONTENTS...done")
  4293. (setq org-cycle-global-status 'contents)
  4294. (run-hook-with-args 'org-cycle-hook 'contents))
  4295. ((and (eq last-command this-command)
  4296. (eq org-cycle-global-status 'contents))
  4297. ;; We just showed the table of contents - now show everything
  4298. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4299. (show-all)
  4300. (message "SHOW ALL")
  4301. (setq org-cycle-global-status 'all)
  4302. (run-hook-with-args 'org-cycle-hook 'all))
  4303. (t
  4304. ;; Default action: go to overview
  4305. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4306. (org-overview)
  4307. (message "OVERVIEW")
  4308. (setq org-cycle-global-status 'overview)
  4309. (run-hook-with-args 'org-cycle-hook 'overview))))
  4310. (defun org-cycle-internal-local ()
  4311. "Do the local cycling action."
  4312. (org-back-to-heading)
  4313. (let ((goal-column 0) eoh eol eos)
  4314. ;; First, some boundaries
  4315. (save-excursion
  4316. (org-back-to-heading)
  4317. (save-excursion
  4318. (beginning-of-line 2)
  4319. (while (and (not (eobp)) ;; this is like `next-line'
  4320. (get-char-property (1- (point)) 'invisible))
  4321. (beginning-of-line 2)) (setq eol (point)))
  4322. (outline-end-of-heading) (setq eoh (point))
  4323. (org-end-of-subtree t)
  4324. (unless (eobp)
  4325. (skip-chars-forward " \t\n")
  4326. (beginning-of-line 1) ; in case this is an item
  4327. )
  4328. (setq eos (1- (point))))
  4329. ;; Find out what to do next and set `this-command'
  4330. (cond
  4331. ((= eos eoh)
  4332. ;; Nothing is hidden behind this heading
  4333. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4334. (message "EMPTY ENTRY")
  4335. (setq org-cycle-subtree-status nil)
  4336. (save-excursion
  4337. (goto-char eos)
  4338. (outline-next-heading)
  4339. (if (org-invisible-p) (org-flag-heading nil))))
  4340. ((or (>= eol eos)
  4341. (not (string-match "\\S-" (buffer-substring eol eos))))
  4342. ;; Entire subtree is hidden in one line: open it
  4343. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4344. (org-show-entry)
  4345. (show-children)
  4346. (message "CHILDREN")
  4347. (save-excursion
  4348. (goto-char eos)
  4349. (outline-next-heading)
  4350. (if (org-invisible-p) (org-flag-heading nil)))
  4351. (setq org-cycle-subtree-status 'children)
  4352. (run-hook-with-args 'org-cycle-hook 'children))
  4353. ((and (eq last-command this-command)
  4354. (eq org-cycle-subtree-status 'children))
  4355. ;; We just showed the children, now show everything.
  4356. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4357. (org-show-subtree)
  4358. (message "SUBTREE")
  4359. (setq org-cycle-subtree-status 'subtree)
  4360. (run-hook-with-args 'org-cycle-hook 'subtree))
  4361. (t
  4362. ;; Default action: hide the subtree.
  4363. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4364. (hide-subtree)
  4365. (message "FOLDED")
  4366. (setq org-cycle-subtree-status 'folded)
  4367. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4368. ;;;###autoload
  4369. (defun org-global-cycle (&optional arg)
  4370. "Cycle the global visibility. For details see `org-cycle'.
  4371. With C-u prefix arg, switch to startup visibility.
  4372. With a numeric prefix, show all headlines up to that level."
  4373. (interactive "P")
  4374. (let ((org-cycle-include-plain-lists
  4375. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4376. (cond
  4377. ((integerp arg)
  4378. (show-all)
  4379. (hide-sublevels arg)
  4380. (setq org-cycle-global-status 'contents))
  4381. ((equal arg '(4))
  4382. (org-set-startup-visibility)
  4383. (message "Startup visibility, plus VISIBILITY properties."))
  4384. (t
  4385. (org-cycle '(4))))))
  4386. (defun org-set-startup-visibility ()
  4387. "Set the visibility required by startup options and properties."
  4388. (cond
  4389. ((eq org-startup-folded t)
  4390. (org-cycle '(4)))
  4391. ((eq org-startup-folded 'content)
  4392. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4393. (org-cycle '(4)) (org-cycle '(4)))))
  4394. (if org-hide-block-startup (org-hide-block-all))
  4395. (org-set-visibility-according-to-property 'no-cleanup)
  4396. (org-cycle-hide-archived-subtrees 'all)
  4397. (org-cycle-hide-drawers 'all)
  4398. (org-cycle-show-empty-lines 'all))
  4399. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4400. "Switch subtree visibilities according to :VISIBILITY: property."
  4401. (interactive)
  4402. (let (org-show-entry-below state)
  4403. (save-excursion
  4404. (goto-char (point-min))
  4405. (while (re-search-forward
  4406. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4407. nil t)
  4408. (setq state (match-string 1))
  4409. (save-excursion
  4410. (org-back-to-heading t)
  4411. (hide-subtree)
  4412. (org-reveal)
  4413. (cond
  4414. ((equal state '("fold" "folded"))
  4415. (hide-subtree))
  4416. ((equal state "children")
  4417. (org-show-hidden-entry)
  4418. (show-children))
  4419. ((equal state "content")
  4420. (save-excursion
  4421. (save-restriction
  4422. (org-narrow-to-subtree)
  4423. (org-content))))
  4424. ((member state '("all" "showall"))
  4425. (show-subtree)))))
  4426. (unless no-cleanup
  4427. (org-cycle-hide-archived-subtrees 'all)
  4428. (org-cycle-hide-drawers 'all)
  4429. (org-cycle-show-empty-lines 'all)))))
  4430. (defun org-overview ()
  4431. "Switch to overview mode, showing only top-level headlines.
  4432. Really, this shows all headlines with level equal or greater than the level
  4433. of the first headline in the buffer. This is important, because if the
  4434. first headline is not level one, then (hide-sublevels 1) gives confusing
  4435. results."
  4436. (interactive)
  4437. (let ((level (save-excursion
  4438. (goto-char (point-min))
  4439. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4440. (progn
  4441. (goto-char (match-beginning 0))
  4442. (funcall outline-level))))))
  4443. (and level (hide-sublevels level))))
  4444. (defun org-content (&optional arg)
  4445. "Show all headlines in the buffer, like a table of contents.
  4446. With numerical argument N, show content up to level N."
  4447. (interactive "P")
  4448. (save-excursion
  4449. ;; Visit all headings and show their offspring
  4450. (and (integerp arg) (org-overview))
  4451. (goto-char (point-max))
  4452. (catch 'exit
  4453. (while (and (progn (condition-case nil
  4454. (outline-previous-visible-heading 1)
  4455. (error (goto-char (point-min))))
  4456. t)
  4457. (looking-at outline-regexp))
  4458. (if (integerp arg)
  4459. (show-children (1- arg))
  4460. (show-branches))
  4461. (if (bobp) (throw 'exit nil))))))
  4462. (defun org-optimize-window-after-visibility-change (state)
  4463. "Adjust the window after a change in outline visibility.
  4464. This function is the default value of the hook `org-cycle-hook'."
  4465. (when (get-buffer-window (current-buffer))
  4466. (cond
  4467. ((eq state 'content) nil)
  4468. ((eq state 'all) nil)
  4469. ((eq state 'folded) nil)
  4470. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4471. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4472. (defun org-compact-display-after-subtree-move ()
  4473. "Show a compacter version of the tree of the entry's parent."
  4474. (save-excursion
  4475. (if (org-up-heading-safe)
  4476. (progn
  4477. (hide-subtree)
  4478. (show-entry)
  4479. (show-children)
  4480. (org-cycle-show-empty-lines 'children)
  4481. (org-cycle-hide-drawers 'children))
  4482. (org-overview))))
  4483. (defun org-cycle-show-empty-lines (state)
  4484. "Show empty lines above all visible headlines.
  4485. The region to be covered depends on STATE when called through
  4486. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4487. entire buffer covered. Note that an empty line is only shown if there
  4488. are at least `org-cycle-separator-lines' empty lines before the headline."
  4489. (when (> org-cycle-separator-lines 0)
  4490. (save-excursion
  4491. (let* ((n org-cycle-separator-lines)
  4492. (re (cond
  4493. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4494. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4495. (t (let ((ns (number-to-string (- n 2))))
  4496. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4497. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4498. beg end)
  4499. (cond
  4500. ((memq state '(overview contents t))
  4501. (setq beg (point-min) end (point-max)))
  4502. ((memq state '(children folded))
  4503. (setq beg (point) end (progn (org-end-of-subtree t t)
  4504. (beginning-of-line 2)
  4505. (point)))))
  4506. (when beg
  4507. (goto-char beg)
  4508. (while (re-search-forward re end t)
  4509. (if (not (get-char-property (match-end 1) 'invisible))
  4510. (outline-flag-region
  4511. (match-beginning 1) (match-end 1) nil)))))))
  4512. ;; Never hide empty lines at the end of the file.
  4513. (save-excursion
  4514. (goto-char (point-max))
  4515. (outline-previous-heading)
  4516. (outline-end-of-heading)
  4517. (if (and (looking-at "[ \t\n]+")
  4518. (= (match-end 0) (point-max)))
  4519. (outline-flag-region (point) (match-end 0) nil))))
  4520. (defun org-show-empty-lines-in-parent ()
  4521. "Move to the parent and re-show empty lines before visible headlines."
  4522. (save-excursion
  4523. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4524. (org-cycle-show-empty-lines context))))
  4525. (defun org-cycle-hide-drawers (state)
  4526. "Re-hide all drawers after a visibility state change."
  4527. (when (and (org-mode-p)
  4528. (not (memq state '(overview folded))))
  4529. (save-excursion
  4530. (let* ((globalp (memq state '(contents all)))
  4531. (beg (if globalp (point-min) (point)))
  4532. (end (if globalp (point-max) (org-end-of-subtree t))))
  4533. (goto-char beg)
  4534. (while (re-search-forward org-drawer-regexp end t)
  4535. (org-flag-drawer t))))))
  4536. (defun org-flag-drawer (flag)
  4537. (save-excursion
  4538. (beginning-of-line 1)
  4539. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4540. (let ((b (match-end 0))
  4541. (outline-regexp org-outline-regexp))
  4542. (if (re-search-forward
  4543. "^[ \t]*:END:"
  4544. (save-excursion (outline-next-heading) (point)) t)
  4545. (outline-flag-region b (point-at-eol) flag)
  4546. (error ":END: line missing"))))))
  4547. (defun org-subtree-end-visible-p ()
  4548. "Is the end of the current subtree visible?"
  4549. (pos-visible-in-window-p
  4550. (save-excursion (org-end-of-subtree t) (point))))
  4551. (defun org-first-headline-recenter (&optional N)
  4552. "Move cursor to the first headline and recenter the headline.
  4553. Optional argument N means, put the headline into the Nth line of the window."
  4554. (goto-char (point-min))
  4555. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4556. (beginning-of-line)
  4557. (recenter (prefix-numeric-value N))))
  4558. ;;; Folding of blocks
  4559. (defconst org-block-regexp
  4560. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4561. "Regular expression for hiding blocks.")
  4562. (defvar org-hide-block-overlays nil
  4563. "Overays hiding blocks.")
  4564. (make-variable-buffer-local 'org-hide-block-overlays)
  4565. (defun org-block-map (function &optional start end)
  4566. "Call func at the head of all source blocks in the current
  4567. buffer. Optional arguments START and END can be used to limit
  4568. the range."
  4569. (let ((start (or start (point-min)))
  4570. (end (or end (point-max))))
  4571. (save-excursion
  4572. (goto-char start)
  4573. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4574. (save-excursion
  4575. (save-match-data
  4576. (goto-char (match-beginning 0))
  4577. (funcall function)))))))
  4578. (defun org-hide-block-toggle-all ()
  4579. "Toggle the visibility of all blocks in the current buffer."
  4580. (org-block-map #'org-hide-block-toggle))
  4581. (defun org-hide-block-all ()
  4582. "Fold all blocks in the current buffer."
  4583. (interactive)
  4584. (org-show-block-all)
  4585. (org-block-map #'org-hide-block-toggle-maybe))
  4586. (defun org-show-block-all ()
  4587. "Unfold all blocks in the current buffer."
  4588. (mapc 'org-delete-overlay org-hide-block-overlays)
  4589. (setq org-hide-block-overlays nil))
  4590. (defun org-hide-block-toggle-maybe ()
  4591. "Toggle visibility of block at point."
  4592. (interactive)
  4593. (let ((case-fold-search t))
  4594. (if (save-excursion
  4595. (beginning-of-line 1)
  4596. (looking-at org-block-regexp))
  4597. (progn (org-hide-block-toggle)
  4598. t) ;; to signal that we took action
  4599. nil))) ;; to signal that we did not
  4600. (defun org-hide-block-toggle (&optional force)
  4601. "Toggle the visibility of the current block."
  4602. (interactive)
  4603. (save-excursion
  4604. (beginning-of-line)
  4605. (if (re-search-forward org-block-regexp nil t)
  4606. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4607. (end (match-end 0))
  4608. ov) ;; end of entire body
  4609. (if (memq t (mapcar (lambda (overlay)
  4610. (eq (org-overlay-get overlay 'invisible)
  4611. 'org-hide-block))
  4612. (org-overlays-at start)))
  4613. (if (or (not force) (eq force 'off))
  4614. (mapc (lambda (ov)
  4615. (when (member ov org-hide-block-overlays)
  4616. (setq org-hide-block-overlays
  4617. (delq ov org-hide-block-overlays)))
  4618. (when (eq (org-overlay-get ov 'invisible)
  4619. 'org-hide-block)
  4620. (org-delete-overlay ov)))
  4621. (org-overlays-at start)))
  4622. (setq ov (make-overlay start end))
  4623. (org-overlay-put ov 'invisible 'org-hide-block)
  4624. (push ov org-hide-block-overlays)))
  4625. (error "Not looking at a source block"))))
  4626. ;; org-tab-after-check-for-cycling-hook
  4627. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4628. ;; Remove overlays when changing major mode
  4629. (add-hook 'org-mode-hook
  4630. (lambda () (org-add-hook 'change-major-mode-hook
  4631. 'org-show-block-all 'append 'local)))
  4632. ;;; Org-goto
  4633. (defvar org-goto-window-configuration nil)
  4634. (defvar org-goto-marker nil)
  4635. (defvar org-goto-map
  4636. (let ((map (make-sparse-keymap)))
  4637. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4638. (while (setq cmd (pop cmds))
  4639. (substitute-key-definition cmd cmd map global-map)))
  4640. (suppress-keymap map)
  4641. (org-defkey map "\C-m" 'org-goto-ret)
  4642. (org-defkey map [(return)] 'org-goto-ret)
  4643. (org-defkey map [(left)] 'org-goto-left)
  4644. (org-defkey map [(right)] 'org-goto-right)
  4645. (org-defkey map [(control ?g)] 'org-goto-quit)
  4646. (org-defkey map "\C-i" 'org-cycle)
  4647. (org-defkey map [(tab)] 'org-cycle)
  4648. (org-defkey map [(down)] 'outline-next-visible-heading)
  4649. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4650. (if org-goto-auto-isearch
  4651. (if (fboundp 'define-key-after)
  4652. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4653. nil)
  4654. (org-defkey map "q" 'org-goto-quit)
  4655. (org-defkey map "n" 'outline-next-visible-heading)
  4656. (org-defkey map "p" 'outline-previous-visible-heading)
  4657. (org-defkey map "f" 'outline-forward-same-level)
  4658. (org-defkey map "b" 'outline-backward-same-level)
  4659. (org-defkey map "u" 'outline-up-heading))
  4660. (org-defkey map "/" 'org-occur)
  4661. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4662. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4663. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4664. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4665. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4666. map))
  4667. (defconst org-goto-help
  4668. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4669. RET=jump to location [Q]uit and return to previous location
  4670. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4671. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4672. ;; FIXME: Docstring doe not mention both interfaces
  4673. (defun org-goto (&optional alternative-interface)
  4674. "Look up a different location in the current file, keeping current visibility.
  4675. When you want look-up or go to a different location in a document, the
  4676. fastest way is often to fold the entire buffer and then dive into the tree.
  4677. This method has the disadvantage, that the previous location will be folded,
  4678. which may not be what you want.
  4679. This command works around this by showing a copy of the current buffer
  4680. in an indirect buffer, in overview mode. You can dive into the tree in
  4681. that copy, use org-occur and incremental search to find a location.
  4682. When pressing RET or `Q', the command returns to the original buffer in
  4683. which the visibility is still unchanged. After RET is will also jump to
  4684. the location selected in the indirect buffer and expose the
  4685. the headline hierarchy above."
  4686. (interactive "P")
  4687. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4688. (org-refile-use-outline-path t)
  4689. (org-refile-target-verify-function nil)
  4690. (interface
  4691. (if (not alternative-interface)
  4692. org-goto-interface
  4693. (if (eq org-goto-interface 'outline)
  4694. 'outline-path-completion
  4695. 'outline)))
  4696. (org-goto-start-pos (point))
  4697. (selected-point
  4698. (if (eq interface 'outline)
  4699. (car (org-get-location (current-buffer) org-goto-help))
  4700. (nth 3 (org-refile-get-location "Goto: ")))))
  4701. (if selected-point
  4702. (progn
  4703. (org-mark-ring-push org-goto-start-pos)
  4704. (goto-char selected-point)
  4705. (if (or (org-invisible-p) (org-invisible-p2))
  4706. (org-show-context 'org-goto)))
  4707. (message "Quit"))))
  4708. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4709. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4710. (defvar org-goto-local-auto-isearch-map) ; defined below
  4711. (defun org-get-location (buf help)
  4712. "Let the user select a location in the Org-mode buffer BUF.
  4713. This function uses a recursive edit. It returns the selected position
  4714. or nil."
  4715. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4716. (isearch-hide-immediately nil)
  4717. (isearch-search-fun-function
  4718. (lambda () 'org-goto-local-search-headings))
  4719. (org-goto-selected-point org-goto-exit-command))
  4720. (save-excursion
  4721. (save-window-excursion
  4722. (delete-other-windows)
  4723. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4724. (switch-to-buffer
  4725. (condition-case nil
  4726. (make-indirect-buffer (current-buffer) "*org-goto*")
  4727. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4728. (with-output-to-temp-buffer "*Help*"
  4729. (princ help))
  4730. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4731. (setq buffer-read-only nil)
  4732. (let ((org-startup-truncated t)
  4733. (org-startup-folded nil)
  4734. (org-startup-align-all-tables nil))
  4735. (org-mode)
  4736. (org-overview))
  4737. (setq buffer-read-only t)
  4738. (if (and (boundp 'org-goto-start-pos)
  4739. (integer-or-marker-p org-goto-start-pos))
  4740. (let ((org-show-hierarchy-above t)
  4741. (org-show-siblings t)
  4742. (org-show-following-heading t))
  4743. (goto-char org-goto-start-pos)
  4744. (and (org-invisible-p) (org-show-context)))
  4745. (goto-char (point-min)))
  4746. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4747. (message "Select location and press RET")
  4748. (use-local-map org-goto-map)
  4749. (recursive-edit)
  4750. ))
  4751. (kill-buffer "*org-goto*")
  4752. (cons org-goto-selected-point org-goto-exit-command)))
  4753. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4754. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4755. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4756. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4757. (defun org-goto-local-search-headings (string bound noerror)
  4758. "Search and make sure that any matches are in headlines."
  4759. (catch 'return
  4760. (while (if isearch-forward
  4761. (search-forward string bound noerror)
  4762. (search-backward string bound noerror))
  4763. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4764. (and (member :headline context)
  4765. (not (member :tags context))))
  4766. (throw 'return (point))))))
  4767. (defun org-goto-local-auto-isearch ()
  4768. "Start isearch."
  4769. (interactive)
  4770. (goto-char (point-min))
  4771. (let ((keys (this-command-keys)))
  4772. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4773. (isearch-mode t)
  4774. (isearch-process-search-char (string-to-char keys)))))
  4775. (defun org-goto-ret (&optional arg)
  4776. "Finish `org-goto' by going to the new location."
  4777. (interactive "P")
  4778. (setq org-goto-selected-point (point)
  4779. org-goto-exit-command 'return)
  4780. (throw 'exit nil))
  4781. (defun org-goto-left ()
  4782. "Finish `org-goto' by going to the new location."
  4783. (interactive)
  4784. (if (org-on-heading-p)
  4785. (progn
  4786. (beginning-of-line 1)
  4787. (setq org-goto-selected-point (point)
  4788. org-goto-exit-command 'left)
  4789. (throw 'exit nil))
  4790. (error "Not on a heading")))
  4791. (defun org-goto-right ()
  4792. "Finish `org-goto' by going to the new location."
  4793. (interactive)
  4794. (if (org-on-heading-p)
  4795. (progn
  4796. (setq org-goto-selected-point (point)
  4797. org-goto-exit-command 'right)
  4798. (throw 'exit nil))
  4799. (error "Not on a heading")))
  4800. (defun org-goto-quit ()
  4801. "Finish `org-goto' without cursor motion."
  4802. (interactive)
  4803. (setq org-goto-selected-point nil)
  4804. (setq org-goto-exit-command 'quit)
  4805. (throw 'exit nil))
  4806. ;;; Indirect buffer display of subtrees
  4807. (defvar org-indirect-dedicated-frame nil
  4808. "This is the frame being used for indirect tree display.")
  4809. (defvar org-last-indirect-buffer nil)
  4810. (defun org-tree-to-indirect-buffer (&optional arg)
  4811. "Create indirect buffer and narrow it to current subtree.
  4812. With numerical prefix ARG, go up to this level and then take that tree.
  4813. If ARG is negative, go up that many levels.
  4814. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4815. indirect buffer previously made with this command, to avoid proliferation of
  4816. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4817. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4818. is kept so that you can work with several indirect buffers at the same time.
  4819. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4820. requests that a new frame be made for the new buffer, so that the dedicated
  4821. frame is not changed."
  4822. (interactive "P")
  4823. (let ((cbuf (current-buffer))
  4824. (cwin (selected-window))
  4825. (pos (point))
  4826. beg end level heading ibuf)
  4827. (save-excursion
  4828. (org-back-to-heading t)
  4829. (when (numberp arg)
  4830. (setq level (org-outline-level))
  4831. (if (< arg 0) (setq arg (+ level arg)))
  4832. (while (> (setq level (org-outline-level)) arg)
  4833. (outline-up-heading 1 t)))
  4834. (setq beg (point)
  4835. heading (org-get-heading))
  4836. (org-end-of-subtree t) (setq end (point)))
  4837. (if (and (buffer-live-p org-last-indirect-buffer)
  4838. (not (eq org-indirect-buffer-display 'new-frame))
  4839. (not arg))
  4840. (kill-buffer org-last-indirect-buffer))
  4841. (setq ibuf (org-get-indirect-buffer cbuf)
  4842. org-last-indirect-buffer ibuf)
  4843. (cond
  4844. ((or (eq org-indirect-buffer-display 'new-frame)
  4845. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4846. (select-frame (make-frame))
  4847. (delete-other-windows)
  4848. (switch-to-buffer ibuf)
  4849. (org-set-frame-title heading))
  4850. ((eq org-indirect-buffer-display 'dedicated-frame)
  4851. (raise-frame
  4852. (select-frame (or (and org-indirect-dedicated-frame
  4853. (frame-live-p org-indirect-dedicated-frame)
  4854. org-indirect-dedicated-frame)
  4855. (setq org-indirect-dedicated-frame (make-frame)))))
  4856. (delete-other-windows)
  4857. (switch-to-buffer ibuf)
  4858. (org-set-frame-title (concat "Indirect: " heading)))
  4859. ((eq org-indirect-buffer-display 'current-window)
  4860. (switch-to-buffer ibuf))
  4861. ((eq org-indirect-buffer-display 'other-window)
  4862. (pop-to-buffer ibuf))
  4863. (t (error "Invalid value.")))
  4864. (if (featurep 'xemacs)
  4865. (save-excursion (org-mode) (turn-on-font-lock)))
  4866. (narrow-to-region beg end)
  4867. (show-all)
  4868. (goto-char pos)
  4869. (and (window-live-p cwin) (select-window cwin))))
  4870. (defun org-get-indirect-buffer (&optional buffer)
  4871. (setq buffer (or buffer (current-buffer)))
  4872. (let ((n 1) (base (buffer-name buffer)) bname)
  4873. (while (buffer-live-p
  4874. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4875. (setq n (1+ n)))
  4876. (condition-case nil
  4877. (make-indirect-buffer buffer bname 'clone)
  4878. (error (make-indirect-buffer buffer bname)))))
  4879. (defun org-set-frame-title (title)
  4880. "Set the title of the current frame to the string TITLE."
  4881. ;; FIXME: how to name a single frame in XEmacs???
  4882. (unless (featurep 'xemacs)
  4883. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4884. ;;;; Structure editing
  4885. ;;; Inserting headlines
  4886. (defun org-previous-line-empty-p ()
  4887. (save-excursion
  4888. (and (not (bobp))
  4889. (or (beginning-of-line 0) t)
  4890. (save-match-data
  4891. (looking-at "[ \t]*$")))))
  4892. (defun org-insert-heading (&optional force-heading)
  4893. "Insert a new heading or item with same depth at point.
  4894. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4895. If point is at the beginning of a headline, insert a sibling before the
  4896. current headline. If point is not at the beginning, do not split the line,
  4897. but create the new headline after the current line."
  4898. (interactive "P")
  4899. (if (= (buffer-size) 0)
  4900. (insert "\n* ")
  4901. (when (or force-heading (not (org-insert-item)))
  4902. (let* ((empty-line-p nil)
  4903. (head (save-excursion
  4904. (condition-case nil
  4905. (progn
  4906. (org-back-to-heading)
  4907. (setq empty-line-p (org-previous-line-empty-p))
  4908. (match-string 0))
  4909. (error "*"))))
  4910. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4911. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4912. pos hide-previous previous-pos)
  4913. (cond
  4914. ((and (org-on-heading-p) (bolp)
  4915. (or (bobp)
  4916. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4917. ;; insert before the current line
  4918. (open-line (if blank 2 1)))
  4919. ((and (bolp)
  4920. (or (bobp)
  4921. (save-excursion
  4922. (backward-char 1) (not (org-invisible-p)))))
  4923. ;; insert right here
  4924. nil)
  4925. (t
  4926. ;; somewhere in the line
  4927. (save-excursion
  4928. (setq previous-pos (point-at-bol))
  4929. (end-of-line)
  4930. (setq hide-previous (org-invisible-p)))
  4931. (and org-insert-heading-respect-content (org-show-subtree))
  4932. (let ((split
  4933. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4934. (save-excursion
  4935. (let ((p (point)))
  4936. (goto-char (point-at-bol))
  4937. (and (looking-at org-complex-heading-regexp)
  4938. (> p (match-beginning 4)))))))
  4939. tags pos)
  4940. (cond
  4941. (org-insert-heading-respect-content
  4942. (org-end-of-subtree nil t)
  4943. (or (bolp) (newline))
  4944. (or (org-previous-line-empty-p)
  4945. (and blank (newline)))
  4946. (open-line 1))
  4947. ((org-on-heading-p)
  4948. (when hide-previous
  4949. (show-children)
  4950. (org-show-entry))
  4951. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4952. (setq tags (and (match-end 2) (match-string 2)))
  4953. (and (match-end 1)
  4954. (delete-region (match-beginning 1) (match-end 1)))
  4955. (setq pos (point-at-bol))
  4956. (or split (end-of-line 1))
  4957. (delete-horizontal-space)
  4958. (newline (if blank 2 1))
  4959. (when tags
  4960. (save-excursion
  4961. (goto-char pos)
  4962. (end-of-line 1)
  4963. (insert " " tags)
  4964. (org-set-tags nil 'align))))
  4965. (t
  4966. (or split (end-of-line 1))
  4967. (newline (if blank 2 1)))))))
  4968. (insert head) (just-one-space)
  4969. (setq pos (point))
  4970. (end-of-line 1)
  4971. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4972. (when (and org-insert-heading-respect-content hide-previous)
  4973. (save-excursion
  4974. (goto-char previous-pos)
  4975. (hide-subtree)))
  4976. (run-hooks 'org-insert-heading-hook)))))
  4977. (defun org-get-heading (&optional no-tags)
  4978. "Return the heading of the current entry, without the stars."
  4979. (save-excursion
  4980. (org-back-to-heading t)
  4981. (if (looking-at
  4982. (if no-tags
  4983. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4984. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4985. (match-string 1) "")))
  4986. (defun org-heading-components ()
  4987. "Return the components of the current heading.
  4988. This is a list with the following elements:
  4989. - the level as an integer
  4990. - the reduced level, different if `org-odd-levels-only' is set.
  4991. - the TODO keyword, or nil
  4992. - the priority character, like ?A, or nil if no priority is given
  4993. - the headline text itself, or the tags string if no headline text
  4994. - the tags string, or nil."
  4995. (save-excursion
  4996. (org-back-to-heading t)
  4997. (if (looking-at org-complex-heading-regexp)
  4998. (list (length (match-string 1))
  4999. (org-reduced-level (length (match-string 1)))
  5000. (org-match-string-no-properties 2)
  5001. (and (match-end 3) (aref (match-string 3) 2))
  5002. (org-match-string-no-properties 4)
  5003. (org-match-string-no-properties 5)))))
  5004. (defun org-get-entry ()
  5005. "Get the entry text, after heading, entire subtree."
  5006. (save-excursion
  5007. (org-back-to-heading t)
  5008. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5009. (defun org-insert-heading-after-current ()
  5010. "Insert a new heading with same level as current, after current subtree."
  5011. (interactive)
  5012. (org-back-to-heading)
  5013. (org-insert-heading)
  5014. (org-move-subtree-down)
  5015. (end-of-line 1))
  5016. (defun org-insert-heading-respect-content ()
  5017. (interactive)
  5018. (let ((org-insert-heading-respect-content t))
  5019. (org-insert-heading t)))
  5020. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5021. (interactive "P")
  5022. (let ((org-insert-heading-respect-content t))
  5023. (org-insert-todo-heading force-state t)))
  5024. (defun org-insert-todo-heading (arg &optional force-heading)
  5025. "Insert a new heading with the same level and TODO state as current heading.
  5026. If the heading has no TODO state, or if the state is DONE, use the first
  5027. state (TODO by default). Also with prefix arg, force first state."
  5028. (interactive "P")
  5029. (when (or force-heading (not (org-insert-item 'checkbox)))
  5030. (org-insert-heading force-heading)
  5031. (save-excursion
  5032. (org-back-to-heading)
  5033. (outline-previous-heading)
  5034. (looking-at org-todo-line-regexp))
  5035. (let*
  5036. ((new-mark-x
  5037. (if (or arg
  5038. (not (match-beginning 2))
  5039. (member (match-string 2) org-done-keywords))
  5040. (car org-todo-keywords-1)
  5041. (match-string 2)))
  5042. (new-mark
  5043. (or
  5044. (run-hook-with-args-until-success
  5045. 'org-todo-get-default-hook new-mark-x nil)
  5046. new-mark-x)))
  5047. (beginning-of-line 1)
  5048. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5049. (if org-treat-insert-todo-heading-as-state-change
  5050. (org-todo new-mark)
  5051. (insert new-mark " "))))
  5052. (when org-provide-todo-statistics
  5053. (org-update-parent-todo-statistics))))
  5054. (defun org-insert-subheading (arg)
  5055. "Insert a new subheading and demote it.
  5056. Works for outline headings and for plain lists alike."
  5057. (interactive "P")
  5058. (org-insert-heading arg)
  5059. (cond
  5060. ((org-on-heading-p) (org-do-demote))
  5061. ((org-at-item-p) (org-indent-item 1))))
  5062. (defun org-insert-todo-subheading (arg)
  5063. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5064. Works for outline headings and for plain lists alike."
  5065. (interactive "P")
  5066. (org-insert-todo-heading arg)
  5067. (cond
  5068. ((org-on-heading-p) (org-do-demote))
  5069. ((org-at-item-p) (org-indent-item 1))))
  5070. ;;; Promotion and Demotion
  5071. (defun org-promote-subtree ()
  5072. "Promote the entire subtree.
  5073. See also `org-promote'."
  5074. (interactive)
  5075. (save-excursion
  5076. (org-map-tree 'org-promote))
  5077. (org-fix-position-after-promote))
  5078. (defun org-demote-subtree ()
  5079. "Demote the entire subtree. See `org-demote'.
  5080. See also `org-promote'."
  5081. (interactive)
  5082. (save-excursion
  5083. (org-map-tree 'org-demote))
  5084. (org-fix-position-after-promote))
  5085. (defun org-do-promote ()
  5086. "Promote the current heading higher up the tree.
  5087. If the region is active in `transient-mark-mode', promote all headings
  5088. in the region."
  5089. (interactive)
  5090. (save-excursion
  5091. (if (org-region-active-p)
  5092. (org-map-region 'org-promote (region-beginning) (region-end))
  5093. (org-promote)))
  5094. (org-fix-position-after-promote))
  5095. (defun org-do-demote ()
  5096. "Demote the current heading lower down the tree.
  5097. If the region is active in `transient-mark-mode', demote all headings
  5098. in the region."
  5099. (interactive)
  5100. (save-excursion
  5101. (if (org-region-active-p)
  5102. (org-map-region 'org-demote (region-beginning) (region-end))
  5103. (org-demote)))
  5104. (org-fix-position-after-promote))
  5105. (defun org-fix-position-after-promote ()
  5106. "Make sure that after pro/demotion cursor position is right."
  5107. (let ((pos (point)))
  5108. (when (save-excursion
  5109. (beginning-of-line 1)
  5110. (looking-at org-todo-line-regexp)
  5111. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5112. (cond ((eobp) (insert " "))
  5113. ((eolp) (insert " "))
  5114. ((equal (char-after) ?\ ) (forward-char 1))))))
  5115. (defun org-reduced-level (l)
  5116. "Compute the effective level of a heading.
  5117. This takes into account the setting of `org-odd-levels-only'."
  5118. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5119. (defun org-get-valid-level (level &optional change)
  5120. "Rectify a level change under the influence of `org-odd-levels-only'
  5121. LEVEL is a current level, CHANGE is by how much the level should be
  5122. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5123. even level numbers will become the next higher odd number."
  5124. (if org-odd-levels-only
  5125. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5126. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5127. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5128. (max 1 (+ level (or change 0)))))
  5129. (if (boundp 'define-obsolete-function-alias)
  5130. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5131. (define-obsolete-function-alias 'org-get-legal-level
  5132. 'org-get-valid-level)
  5133. (define-obsolete-function-alias 'org-get-legal-level
  5134. 'org-get-valid-level "23.1")))
  5135. (defun org-promote ()
  5136. "Promote the current heading higher up the tree.
  5137. If the region is active in `transient-mark-mode', promote all headings
  5138. in the region."
  5139. (org-back-to-heading t)
  5140. (let* ((level (save-match-data (funcall outline-level)))
  5141. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5142. (diff (abs (- level (length up-head) -1))))
  5143. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5144. (replace-match up-head nil t)
  5145. ;; Fixup tag positioning
  5146. (and org-auto-align-tags (org-set-tags nil t))
  5147. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5148. (defun org-demote ()
  5149. "Demote the current heading lower down the tree.
  5150. If the region is active in `transient-mark-mode', demote all headings
  5151. in the region."
  5152. (org-back-to-heading t)
  5153. (let* ((level (save-match-data (funcall outline-level)))
  5154. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5155. (diff (abs (- level (length down-head) -1))))
  5156. (replace-match down-head nil t)
  5157. ;; Fixup tag positioning
  5158. (and org-auto-align-tags (org-set-tags nil t))
  5159. (if org-adapt-indentation (org-fixup-indentation diff))))
  5160. (defun org-map-tree (fun)
  5161. "Call FUN for every heading underneath the current one."
  5162. (org-back-to-heading)
  5163. (let ((level (funcall outline-level)))
  5164. (save-excursion
  5165. (funcall fun)
  5166. (while (and (progn
  5167. (outline-next-heading)
  5168. (> (funcall outline-level) level))
  5169. (not (eobp)))
  5170. (funcall fun)))))
  5171. (defun org-map-region (fun beg end)
  5172. "Call FUN for every heading between BEG and END."
  5173. (let ((org-ignore-region t))
  5174. (save-excursion
  5175. (setq end (copy-marker end))
  5176. (goto-char beg)
  5177. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5178. (< (point) end))
  5179. (funcall fun))
  5180. (while (and (progn
  5181. (outline-next-heading)
  5182. (< (point) end))
  5183. (not (eobp)))
  5184. (funcall fun)))))
  5185. (defun org-fixup-indentation (diff)
  5186. "Change the indentation in the current entry by DIFF
  5187. However, if any line in the current entry has no indentation, or if it
  5188. would end up with no indentation after the change, nothing at all is done."
  5189. (save-excursion
  5190. (let ((end (save-excursion (outline-next-heading)
  5191. (point-marker)))
  5192. (prohibit (if (> diff 0)
  5193. "^\\S-"
  5194. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5195. col)
  5196. (unless (save-excursion (end-of-line 1)
  5197. (re-search-forward prohibit end t))
  5198. (while (and (< (point) end)
  5199. (re-search-forward "^[ \t]+" end t))
  5200. (goto-char (match-end 0))
  5201. (setq col (current-column))
  5202. (if (< diff 0) (replace-match ""))
  5203. (org-indent-to-column (+ diff col))))
  5204. (move-marker end nil))))
  5205. (defun org-convert-to-odd-levels ()
  5206. "Convert an org-mode file with all levels allowed to one with odd levels.
  5207. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5208. level 5 etc."
  5209. (interactive)
  5210. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5211. (let ((org-odd-levels-only nil) n)
  5212. (save-excursion
  5213. (goto-char (point-min))
  5214. (while (re-search-forward "^\\*\\*+ " nil t)
  5215. (setq n (- (length (match-string 0)) 2))
  5216. (while (>= (setq n (1- n)) 0)
  5217. (org-demote))
  5218. (end-of-line 1))))))
  5219. (defun org-convert-to-oddeven-levels ()
  5220. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5221. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5222. section with an even level, conversion would destroy the structure of the file. An error
  5223. is signaled in this case."
  5224. (interactive)
  5225. (goto-char (point-min))
  5226. ;; First check if there are no even levels
  5227. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5228. (org-show-context t)
  5229. (error "Not all levels are odd in this file. Conversion not possible."))
  5230. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5231. (let ((org-odd-levels-only nil) n)
  5232. (save-excursion
  5233. (goto-char (point-min))
  5234. (while (re-search-forward "^\\*\\*+ " nil t)
  5235. (setq n (/ (1- (length (match-string 0))) 2))
  5236. (while (>= (setq n (1- n)) 0)
  5237. (org-promote))
  5238. (end-of-line 1))))))
  5239. (defun org-tr-level (n)
  5240. "Make N odd if required."
  5241. (if org-odd-levels-only (1+ (/ n 2)) n))
  5242. ;;; Vertical tree motion, cutting and pasting of subtrees
  5243. (defun org-move-subtree-up (&optional arg)
  5244. "Move the current subtree up past ARG headlines of the same level."
  5245. (interactive "p")
  5246. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5247. (defun org-move-subtree-down (&optional arg)
  5248. "Move the current subtree down past ARG headlines of the same level."
  5249. (interactive "p")
  5250. (setq arg (prefix-numeric-value arg))
  5251. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5252. 'outline-get-last-sibling))
  5253. (ins-point (make-marker))
  5254. (cnt (abs arg))
  5255. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5256. ;; Select the tree
  5257. (org-back-to-heading)
  5258. (setq beg0 (point))
  5259. (save-excursion
  5260. (setq ne-beg (org-back-over-empty-lines))
  5261. (setq beg (point)))
  5262. (save-match-data
  5263. (save-excursion (outline-end-of-heading)
  5264. (setq folded (org-invisible-p)))
  5265. (outline-end-of-subtree))
  5266. (outline-next-heading)
  5267. (setq ne-end (org-back-over-empty-lines))
  5268. (setq end (point))
  5269. (goto-char beg0)
  5270. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5271. ;; include less whitespace
  5272. (save-excursion
  5273. (goto-char beg)
  5274. (forward-line (- ne-beg ne-end))
  5275. (setq beg (point))))
  5276. ;; Find insertion point, with error handling
  5277. (while (> cnt 0)
  5278. (or (and (funcall movfunc) (looking-at outline-regexp))
  5279. (progn (goto-char beg0)
  5280. (error "Cannot move past superior level or buffer limit")))
  5281. (setq cnt (1- cnt)))
  5282. (if (> arg 0)
  5283. ;; Moving forward - still need to move over subtree
  5284. (progn (org-end-of-subtree t t)
  5285. (save-excursion
  5286. (org-back-over-empty-lines)
  5287. (or (bolp) (newline)))))
  5288. (setq ne-ins (org-back-over-empty-lines))
  5289. (move-marker ins-point (point))
  5290. (setq txt (buffer-substring beg end))
  5291. (org-save-markers-in-region beg end)
  5292. (delete-region beg end)
  5293. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5294. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5295. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5296. (let ((bbb (point)))
  5297. (insert-before-markers txt)
  5298. (org-reinstall-markers-in-region bbb)
  5299. (move-marker ins-point bbb))
  5300. (or (bolp) (insert "\n"))
  5301. (setq ins-end (point))
  5302. (goto-char ins-point)
  5303. (org-skip-whitespace)
  5304. (when (and (< arg 0)
  5305. (org-first-sibling-p)
  5306. (> ne-ins ne-beg))
  5307. ;; Move whitespace back to beginning
  5308. (save-excursion
  5309. (goto-char ins-end)
  5310. (let ((kill-whole-line t))
  5311. (kill-line (- ne-ins ne-beg)) (point)))
  5312. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5313. (move-marker ins-point nil)
  5314. (org-compact-display-after-subtree-move)
  5315. (org-show-empty-lines-in-parent)
  5316. (unless folded
  5317. (org-show-entry)
  5318. (show-children)
  5319. (org-cycle-hide-drawers 'children))))
  5320. (defvar org-subtree-clip ""
  5321. "Clipboard for cut and paste of subtrees.
  5322. This is actually only a copy of the kill, because we use the normal kill
  5323. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5324. (defvar org-subtree-clip-folded nil
  5325. "Was the last copied subtree folded?
  5326. This is used to fold the tree back after pasting.")
  5327. (defun org-cut-subtree (&optional n)
  5328. "Cut the current subtree into the clipboard.
  5329. With prefix arg N, cut this many sequential subtrees.
  5330. This is a short-hand for marking the subtree and then cutting it."
  5331. (interactive "p")
  5332. (org-copy-subtree n 'cut))
  5333. (defun org-copy-subtree (&optional n cut force-store-markers)
  5334. "Cut the current subtree into the clipboard.
  5335. With prefix arg N, cut this many sequential subtrees.
  5336. This is a short-hand for marking the subtree and then copying it.
  5337. If CUT is non-nil, actually cut the subtree.
  5338. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5339. of some markers in the region, even if CUT is non-nil. This is
  5340. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5341. (interactive "p")
  5342. (let (beg end folded (beg0 (point)))
  5343. (if (interactive-p)
  5344. (org-back-to-heading nil) ; take what looks like a subtree
  5345. (org-back-to-heading t)) ; take what is really there
  5346. (org-back-over-empty-lines)
  5347. (setq beg (point))
  5348. (skip-chars-forward " \t\r\n")
  5349. (save-match-data
  5350. (save-excursion (outline-end-of-heading)
  5351. (setq folded (org-invisible-p)))
  5352. (condition-case nil
  5353. (outline-forward-same-level (1- n))
  5354. (error nil))
  5355. (org-end-of-subtree t t))
  5356. (org-back-over-empty-lines)
  5357. (setq end (point))
  5358. (goto-char beg0)
  5359. (when (> end beg)
  5360. (setq org-subtree-clip-folded folded)
  5361. (when (or cut force-store-markers)
  5362. (org-save-markers-in-region beg end))
  5363. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5364. (setq org-subtree-clip (current-kill 0))
  5365. (message "%s: Subtree(s) with %d characters"
  5366. (if cut "Cut" "Copied")
  5367. (length org-subtree-clip)))))
  5368. (defun org-paste-subtree (&optional level tree for-yank)
  5369. "Paste the clipboard as a subtree, with modification of headline level.
  5370. The entire subtree is promoted or demoted in order to match a new headline
  5371. level.
  5372. If the cursor is at the beginning of a headline, the same level as
  5373. that headline is used to paste the tree
  5374. If not, the new level is derived from the *visible* headings
  5375. before and after the insertion point, and taken to be the inferior headline
  5376. level of the two. So if the previous visible heading is level 3 and the
  5377. next is level 4 (or vice versa), level 4 will be used for insertion.
  5378. This makes sure that the subtree remains an independent subtree and does
  5379. not swallow low level entries.
  5380. You can also force a different level, either by using a numeric prefix
  5381. argument, or by inserting the heading marker by hand. For example, if the
  5382. cursor is after \"*****\", then the tree will be shifted to level 5.
  5383. If optional TREE is given, use this text instead of the kill ring.
  5384. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5385. move back over whitespace before inserting, and move point to the end of
  5386. the inserted text when done."
  5387. (interactive "P")
  5388. (unless (org-kill-is-subtree-p tree)
  5389. (error "%s"
  5390. (substitute-command-keys
  5391. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5392. (let* ((visp (not (org-invisible-p)))
  5393. (txt (or tree (and kill-ring (current-kill 0))))
  5394. (^re (concat "^\\(" outline-regexp "\\)"))
  5395. (re (concat "\\(" outline-regexp "\\)"))
  5396. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5397. (old-level (if (string-match ^re txt)
  5398. (- (match-end 0) (match-beginning 0) 1)
  5399. -1))
  5400. (force-level (cond (level (prefix-numeric-value level))
  5401. ((and (looking-at "[ \t]*$")
  5402. (string-match
  5403. ^re_ (buffer-substring
  5404. (point-at-bol) (point))))
  5405. (- (match-end 1) (match-beginning 1)))
  5406. ((and (bolp)
  5407. (looking-at org-outline-regexp))
  5408. (- (match-end 0) (point) 1))
  5409. (t nil)))
  5410. (previous-level (save-excursion
  5411. (condition-case nil
  5412. (progn
  5413. (outline-previous-visible-heading 1)
  5414. (if (looking-at re)
  5415. (- (match-end 0) (match-beginning 0) 1)
  5416. 1))
  5417. (error 1))))
  5418. (next-level (save-excursion
  5419. (condition-case nil
  5420. (progn
  5421. (or (looking-at outline-regexp)
  5422. (outline-next-visible-heading 1))
  5423. (if (looking-at re)
  5424. (- (match-end 0) (match-beginning 0) 1)
  5425. 1))
  5426. (error 1))))
  5427. (new-level (or force-level (max previous-level next-level)))
  5428. (shift (if (or (= old-level -1)
  5429. (= new-level -1)
  5430. (= old-level new-level))
  5431. 0
  5432. (- new-level old-level)))
  5433. (delta (if (> shift 0) -1 1))
  5434. (func (if (> shift 0) 'org-demote 'org-promote))
  5435. (org-odd-levels-only nil)
  5436. beg end newend)
  5437. ;; Remove the forced level indicator
  5438. (if force-level
  5439. (delete-region (point-at-bol) (point)))
  5440. ;; Paste
  5441. (beginning-of-line 1)
  5442. (unless for-yank (org-back-over-empty-lines))
  5443. (setq beg (point))
  5444. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5445. (insert-before-markers txt)
  5446. (unless (string-match "\n\\'" txt) (insert "\n"))
  5447. (setq newend (point))
  5448. (org-reinstall-markers-in-region beg)
  5449. (setq end (point))
  5450. (goto-char beg)
  5451. (skip-chars-forward " \t\n\r")
  5452. (setq beg (point))
  5453. (if (and (org-invisible-p) visp)
  5454. (save-excursion (outline-show-heading)))
  5455. ;; Shift if necessary
  5456. (unless (= shift 0)
  5457. (save-restriction
  5458. (narrow-to-region beg end)
  5459. (while (not (= shift 0))
  5460. (org-map-region func (point-min) (point-max))
  5461. (setq shift (+ delta shift)))
  5462. (goto-char (point-min))
  5463. (setq newend (point-max))))
  5464. (when (or (interactive-p) for-yank)
  5465. (message "Clipboard pasted as level %d subtree" new-level))
  5466. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5467. kill-ring
  5468. (eq org-subtree-clip (current-kill 0))
  5469. org-subtree-clip-folded)
  5470. ;; The tree was folded before it was killed/copied
  5471. (hide-subtree))
  5472. (and for-yank (goto-char newend))))
  5473. (defun org-kill-is-subtree-p (&optional txt)
  5474. "Check if the current kill is an outline subtree, or a set of trees.
  5475. Returns nil if kill does not start with a headline, or if the first
  5476. headline level is not the largest headline level in the tree.
  5477. So this will actually accept several entries of equal levels as well,
  5478. which is OK for `org-paste-subtree'.
  5479. If optional TXT is given, check this string instead of the current kill."
  5480. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5481. (start-level (and kill
  5482. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5483. org-outline-regexp "\\)")
  5484. kill)
  5485. (- (match-end 2) (match-beginning 2) 1)))
  5486. (re (concat "^" org-outline-regexp))
  5487. (start (1+ (or (match-beginning 2) -1))))
  5488. (if (not start-level)
  5489. (progn
  5490. nil) ;; does not even start with a heading
  5491. (catch 'exit
  5492. (while (setq start (string-match re kill (1+ start)))
  5493. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5494. (throw 'exit nil)))
  5495. t))))
  5496. (defvar org-markers-to-move nil
  5497. "Markers that should be moved with a cut-and-paste operation.
  5498. Those markers are stored together with their positions relative to
  5499. the start of the region.")
  5500. (defun org-save-markers-in-region (beg end)
  5501. "Check markers in region.
  5502. If these markers are between BEG and END, record their position relative
  5503. to BEG, so that after moving the block of text, we can put the markers back
  5504. into place.
  5505. This function gets called just before an entry or tree gets cut from the
  5506. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5507. called immediately, to move the markers with the entries."
  5508. (setq org-markers-to-move nil)
  5509. (when (featurep 'org-clock)
  5510. (org-clock-save-markers-for-cut-and-paste beg end))
  5511. (when (featurep 'org-agenda)
  5512. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5513. (defun org-check-and-save-marker (marker beg end)
  5514. "Check if MARKER is between BEG and END.
  5515. If yes, remember the marker and the distance to BEG."
  5516. (when (and (marker-buffer marker)
  5517. (equal (marker-buffer marker) (current-buffer)))
  5518. (if (and (>= marker beg) (< marker end))
  5519. (push (cons marker (- marker beg)) org-markers-to-move))))
  5520. (defun org-reinstall-markers-in-region (beg)
  5521. "Move all remembered markers to their position relative to BEG."
  5522. (mapc (lambda (x)
  5523. (move-marker (car x) (+ beg (cdr x))))
  5524. org-markers-to-move)
  5525. (setq org-markers-to-move nil))
  5526. (defun org-narrow-to-subtree ()
  5527. "Narrow buffer to the current subtree."
  5528. (interactive)
  5529. (save-excursion
  5530. (save-match-data
  5531. (narrow-to-region
  5532. (progn (org-back-to-heading) (point))
  5533. (progn (org-end-of-subtree t) (point))))))
  5534. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5535. "Clone the task (subtree) at point N times.
  5536. The clones will be inserted as siblings.
  5537. In interactive use, the user will be prompted for the number of clones
  5538. to be produced, and for a time SHIFT, which may be a repeater as used
  5539. in time stamps, for example `+3d'.
  5540. When a valid repeater is given and the entry contains any time stamps,
  5541. the clones will become a sequence in time, with time stamps in the
  5542. subtree shifted for each clone produced. If SHIFT is nil or the
  5543. empty string, time stamps will be left alone.
  5544. If the original subtree did contain time stamps with a repeater,
  5545. the following will happen:
  5546. - the repeater will be removed in each clone
  5547. - an additional clone will be produced, with the current, unshifted
  5548. date(s) in the entry.
  5549. - the original entry will be placed *after* all the clones, with
  5550. repeater intact.
  5551. - the start days in the repeater in the original entry will be shifted
  5552. to past the last clone.
  5553. I this way you can spell out a number of instances of a repeating task,
  5554. and still retain the repeater to cover future instances of the task."
  5555. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5556. (let (beg end template task
  5557. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5558. (if (not (and (integerp n) (> n 0)))
  5559. (error "Invalid number of replications %s" n))
  5560. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5561. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5562. shift)))
  5563. (error "Invalid shift specification %s" shift))
  5564. (when doshift
  5565. (setq shift-n (string-to-number (match-string 1 shift))
  5566. shift-what (cdr (assoc (match-string 2 shift)
  5567. '(("d" . day) ("w" . week)
  5568. ("m" . month) ("y" . year))))))
  5569. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5570. (setq nmin 1 nmax n)
  5571. (org-back-to-heading t)
  5572. (setq beg (point))
  5573. (org-end-of-subtree t t)
  5574. (setq end (point))
  5575. (setq template (buffer-substring beg end))
  5576. (when (and doshift
  5577. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5578. (delete-region beg end)
  5579. (setq end beg)
  5580. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5581. (goto-char end)
  5582. (loop for n from nmin to nmax do
  5583. (if (not doshift)
  5584. (setq task template)
  5585. (with-temp-buffer
  5586. (insert template)
  5587. (org-mode)
  5588. (goto-char (point-min))
  5589. (while (re-search-forward org-ts-regexp nil t)
  5590. (org-timestamp-change (* n shift-n) shift-what))
  5591. (unless (= n n-no-remove)
  5592. (goto-char (point-min))
  5593. (while (re-search-forward org-ts-regexp nil t)
  5594. (save-excursion
  5595. (goto-char (match-beginning 0))
  5596. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5597. (delete-region (match-beginning 1) (match-end 1))))))
  5598. (setq task (buffer-string))))
  5599. (insert task))
  5600. (goto-char beg)))
  5601. ;;; Outline Sorting
  5602. (defun org-sort (with-case)
  5603. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5604. Optional argument WITH-CASE means sort case-sensitively.
  5605. With a double prefix argument, also remove duplicate entries."
  5606. (interactive "P")
  5607. (if (org-at-table-p)
  5608. (org-call-with-arg 'org-table-sort-lines with-case)
  5609. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5610. (defun org-sort-remove-invisible (s)
  5611. (remove-text-properties 0 (length s) org-rm-props s)
  5612. (while (string-match org-bracket-link-regexp s)
  5613. (setq s (replace-match (if (match-end 2)
  5614. (match-string 3 s)
  5615. (match-string 1 s)) t t s)))
  5616. s)
  5617. (defvar org-priority-regexp) ; defined later in the file
  5618. (defvar org-after-sorting-entries-or-items-hook nil
  5619. "Hook that is run after a bunch of entries or items have been sorted.
  5620. When children are sorted, the cursor is in the parent line when this
  5621. hook gets called. When a region or a plain list is sorted, the cursor
  5622. will be in the first entry of the sorted region/list.")
  5623. (defun org-sort-entries-or-items
  5624. (&optional with-case sorting-type getkey-func compare-func property)
  5625. "Sort entries on a certain level of an outline tree, or plain list items.
  5626. If there is an active region, the entries in the region are sorted.
  5627. Else, if the cursor is before the first entry, sort the top-level items.
  5628. Else, the children of the entry at point are sorted.
  5629. If the cursor is at the first item in a plain list, the list items will be
  5630. sorted.
  5631. Sorting can be alphabetically, numerically, by date/time as given by
  5632. a time stamp, by a property or by priority.
  5633. The command prompts for the sorting type unless it has been given to the
  5634. function through the SORTING-TYPE argument, which needs to a character,
  5635. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5636. precise meaning of each character:
  5637. n Numerically, by converting the beginning of the entry/item to a number.
  5638. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5639. t By date/time, either the first active time stamp in the entry, or, if
  5640. none exist, by the first inactive one.
  5641. In items, only the first line will be chekced.
  5642. s By the scheduled date/time.
  5643. d By deadline date/time.
  5644. c By creation time, which is assumed to be the first inactive time stamp
  5645. at the beginning of a line.
  5646. p By priority according to the cookie.
  5647. r By the value of a property.
  5648. Capital letters will reverse the sort order.
  5649. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5650. called with point at the beginning of the record. It must return either
  5651. a string or a number that should serve as the sorting key for that record.
  5652. Comparing entries ignores case by default. However, with an optional argument
  5653. WITH-CASE, the sorting considers case as well."
  5654. (interactive "P")
  5655. (let ((case-func (if with-case 'identity 'downcase))
  5656. start beg end stars re re2
  5657. txt what tmp plain-list-p)
  5658. ;; Find beginning and end of region to sort
  5659. (cond
  5660. ((org-region-active-p)
  5661. ;; we will sort the region
  5662. (setq end (region-end)
  5663. what "region")
  5664. (goto-char (region-beginning))
  5665. (if (not (org-on-heading-p)) (outline-next-heading))
  5666. (setq start (point)))
  5667. ((org-at-item-p)
  5668. ;; we will sort this plain list
  5669. (org-beginning-of-item-list) (setq start (point))
  5670. (org-end-of-item-list) (setq end (point))
  5671. (goto-char start)
  5672. (setq plain-list-p t
  5673. what "plain list"))
  5674. ((or (org-on-heading-p)
  5675. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5676. ;; we will sort the children of the current headline
  5677. (org-back-to-heading)
  5678. (setq start (point)
  5679. end (progn (org-end-of-subtree t t)
  5680. (org-back-over-empty-lines)
  5681. (point))
  5682. what "children")
  5683. (goto-char start)
  5684. (show-subtree)
  5685. (outline-next-heading))
  5686. (t
  5687. ;; we will sort the top-level entries in this file
  5688. (goto-char (point-min))
  5689. (or (org-on-heading-p) (outline-next-heading))
  5690. (setq start (point) end (point-max) what "top-level")
  5691. (goto-char start)
  5692. (show-all)))
  5693. (setq beg (point))
  5694. (if (>= beg end) (error "Nothing to sort"))
  5695. (unless plain-list-p
  5696. (looking-at "\\(\\*+\\)")
  5697. (setq stars (match-string 1)
  5698. re (concat "^" (regexp-quote stars) " +")
  5699. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5700. txt (buffer-substring beg end))
  5701. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5702. (if (and (not (equal stars "*")) (string-match re2 txt))
  5703. (error "Region to sort contains a level above the first entry")))
  5704. (unless sorting-type
  5705. (message
  5706. (if plain-list-p
  5707. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5708. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5709. [t]ime [s]cheduled [d]eadline [c]reated
  5710. A/N/T/S/D/C/P/O/F means reversed:")
  5711. what)
  5712. (setq sorting-type (read-char-exclusive))
  5713. (and (= (downcase sorting-type) ?f)
  5714. (setq getkey-func
  5715. (org-ido-completing-read "Sort using function: "
  5716. obarray 'fboundp t nil nil))
  5717. (setq getkey-func (intern getkey-func)))
  5718. (and (= (downcase sorting-type) ?r)
  5719. (setq property
  5720. (org-ido-completing-read "Property: "
  5721. (mapcar 'list (org-buffer-property-keys t))
  5722. nil t))))
  5723. (message "Sorting entries...")
  5724. (save-restriction
  5725. (narrow-to-region start end)
  5726. (let ((dcst (downcase sorting-type))
  5727. (case-fold-search nil)
  5728. (now (current-time)))
  5729. (sort-subr
  5730. (/= dcst sorting-type)
  5731. ;; This function moves to the beginning character of the "record" to
  5732. ;; be sorted.
  5733. (if plain-list-p
  5734. (lambda nil
  5735. (if (org-at-item-p) t (goto-char (point-max))))
  5736. (lambda nil
  5737. (if (re-search-forward re nil t)
  5738. (goto-char (match-beginning 0))
  5739. (goto-char (point-max)))))
  5740. ;; This function moves to the last character of the "record" being
  5741. ;; sorted.
  5742. (if plain-list-p
  5743. 'org-end-of-item
  5744. (lambda nil
  5745. (save-match-data
  5746. (condition-case nil
  5747. (outline-forward-same-level 1)
  5748. (error
  5749. (goto-char (point-max)))))))
  5750. ;; This function returns the value that gets sorted against.
  5751. (if plain-list-p
  5752. (lambda nil
  5753. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5754. (cond
  5755. ((= dcst ?n)
  5756. (string-to-number (buffer-substring (match-end 0)
  5757. (point-at-eol))))
  5758. ((= dcst ?a)
  5759. (buffer-substring (match-end 0) (point-at-eol)))
  5760. ((= dcst ?t)
  5761. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5762. (re-search-forward org-ts-regexp-both
  5763. (point-at-eol) t))
  5764. (org-time-string-to-seconds (match-string 0))
  5765. (time-to-seconds now)))
  5766. ((= dcst ?f)
  5767. (if getkey-func
  5768. (progn
  5769. (setq tmp (funcall getkey-func))
  5770. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5771. tmp)
  5772. (error "Invalid key function `%s'" getkey-func)))
  5773. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5774. (lambda nil
  5775. (cond
  5776. ((= dcst ?n)
  5777. (if (looking-at org-complex-heading-regexp)
  5778. (string-to-number (match-string 4))
  5779. nil))
  5780. ((= dcst ?a)
  5781. (if (looking-at org-complex-heading-regexp)
  5782. (funcall case-func (match-string 4))
  5783. nil))
  5784. ((= dcst ?t)
  5785. (let ((end (save-excursion (outline-next-heading) (point))))
  5786. (if (or (re-search-forward org-ts-regexp end t)
  5787. (re-search-forward org-ts-regexp-both end t))
  5788. (org-time-string-to-seconds (match-string 0))
  5789. (time-to-seconds now))))
  5790. ((= dcst ?c)
  5791. (let ((end (save-excursion (outline-next-heading) (point))))
  5792. (if (re-search-forward
  5793. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5794. end t)
  5795. (org-time-string-to-seconds (match-string 0))
  5796. (time-to-seconds now))))
  5797. ((= dcst ?s)
  5798. (let ((end (save-excursion (outline-next-heading) (point))))
  5799. (if (re-search-forward org-scheduled-time-regexp end t)
  5800. (org-time-string-to-seconds (match-string 1))
  5801. (time-to-seconds now))))
  5802. ((= dcst ?d)
  5803. (let ((end (save-excursion (outline-next-heading) (point))))
  5804. (if (re-search-forward org-deadline-time-regexp end t)
  5805. (org-time-string-to-seconds (match-string 1))
  5806. (time-to-seconds now))))
  5807. ((= dcst ?p)
  5808. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5809. (string-to-char (match-string 2))
  5810. org-default-priority))
  5811. ((= dcst ?r)
  5812. (or (org-entry-get nil property) ""))
  5813. ((= dcst ?o)
  5814. (if (looking-at org-complex-heading-regexp)
  5815. (- 9999 (length (member (match-string 2)
  5816. org-todo-keywords-1)))))
  5817. ((= dcst ?f)
  5818. (if getkey-func
  5819. (progn
  5820. (setq tmp (funcall getkey-func))
  5821. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5822. tmp)
  5823. (error "Invalid key function `%s'" getkey-func)))
  5824. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5825. nil
  5826. (cond
  5827. ((= dcst ?a) 'string<)
  5828. ((= dcst ?f) compare-func)
  5829. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  5830. (t nil)))))
  5831. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5832. (message "Sorting entries...done")))
  5833. (defun org-do-sort (table what &optional with-case sorting-type)
  5834. "Sort TABLE of WHAT according to SORTING-TYPE.
  5835. The user will be prompted for the SORTING-TYPE if the call to this
  5836. function does not specify it. WHAT is only for the prompt, to indicate
  5837. what is being sorted. The sorting key will be extracted from
  5838. the car of the elements of the table.
  5839. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5840. (unless sorting-type
  5841. (message
  5842. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5843. what)
  5844. (setq sorting-type (read-char-exclusive)))
  5845. (let ((dcst (downcase sorting-type))
  5846. extractfun comparefun)
  5847. ;; Define the appropriate functions
  5848. (cond
  5849. ((= dcst ?n)
  5850. (setq extractfun 'string-to-number
  5851. comparefun (if (= dcst sorting-type) '< '>)))
  5852. ((= dcst ?a)
  5853. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5854. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5855. comparefun (if (= dcst sorting-type)
  5856. 'string<
  5857. (lambda (a b) (and (not (string< a b))
  5858. (not (string= a b)))))))
  5859. ((= dcst ?t)
  5860. (setq extractfun
  5861. (lambda (x)
  5862. (if (or (string-match org-ts-regexp x)
  5863. (string-match org-ts-regexp-both x))
  5864. (time-to-seconds
  5865. (org-time-string-to-time (match-string 0 x)))
  5866. 0))
  5867. comparefun (if (= dcst sorting-type) '< '>)))
  5868. (t (error "Invalid sorting type `%c'" sorting-type)))
  5869. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5870. table)
  5871. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5872. ;;; The orgstruct minor mode
  5873. ;; Define a minor mode which can be used in other modes in order to
  5874. ;; integrate the org-mode structure editing commands.
  5875. ;; This is really a hack, because the org-mode structure commands use
  5876. ;; keys which normally belong to the major mode. Here is how it
  5877. ;; works: The minor mode defines all the keys necessary to operate the
  5878. ;; structure commands, but wraps the commands into a function which
  5879. ;; tests if the cursor is currently at a headline or a plain list
  5880. ;; item. If that is the case, the structure command is used,
  5881. ;; temporarily setting many Org-mode variables like regular
  5882. ;; expressions for filling etc. However, when any of those keys is
  5883. ;; used at a different location, function uses `key-binding' to look
  5884. ;; up if the key has an associated command in another currently active
  5885. ;; keymap (minor modes, major mode, global), and executes that
  5886. ;; command. There might be problems if any of the keys is otherwise
  5887. ;; used as a prefix key.
  5888. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5889. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5890. ;; addresses this by checking explicitly for both bindings.
  5891. (defvar orgstruct-mode-map (make-sparse-keymap)
  5892. "Keymap for the minor `orgstruct-mode'.")
  5893. (defvar org-local-vars nil
  5894. "List of local variables, for use by `orgstruct-mode'")
  5895. ;;;###autoload
  5896. (define-minor-mode orgstruct-mode
  5897. "Toggle the minor more `orgstruct-mode'.
  5898. This mode is for using Org-mode structure commands in other modes.
  5899. The following key behave as if Org-mode was active, if the cursor
  5900. is on a headline, or on a plain list item (both in the definition
  5901. of Org-mode).
  5902. M-up Move entry/item up
  5903. M-down Move entry/item down
  5904. M-left Promote
  5905. M-right Demote
  5906. M-S-up Move entry/item up
  5907. M-S-down Move entry/item down
  5908. M-S-left Promote subtree
  5909. M-S-right Demote subtree
  5910. M-q Fill paragraph and items like in Org-mode
  5911. C-c ^ Sort entries
  5912. C-c - Cycle list bullet
  5913. TAB Cycle item visibility
  5914. M-RET Insert new heading/item
  5915. S-M-RET Insert new TODO heading / Checkbox item
  5916. C-c C-c Set tags / toggle checkbox"
  5917. nil " OrgStruct" nil
  5918. (org-load-modules-maybe)
  5919. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5920. ;;;###autoload
  5921. (defun turn-on-orgstruct ()
  5922. "Unconditionally turn on `orgstruct-mode'."
  5923. (orgstruct-mode 1))
  5924. (defun orgstruct++-mode (&optional arg)
  5925. "Toggle `orgstruct-mode', the enhanced version of it.
  5926. In addition to setting orgstruct-mode, this also exports all indentation and
  5927. autofilling variables from org-mode into the buffer. It will also
  5928. recognize item context in multiline items.
  5929. Note that turning off orgstruct-mode will *not* remove the
  5930. indentation/paragraph settings. This can only be done by refreshing the
  5931. major mode, for example with \[normal-mode]."
  5932. (interactive "P")
  5933. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5934. (if (< arg 1)
  5935. (orgstruct-mode -1)
  5936. (orgstruct-mode 1)
  5937. (let (var val)
  5938. (mapc
  5939. (lambda (x)
  5940. (when (string-match
  5941. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5942. (symbol-name (car x)))
  5943. (setq var (car x) val (nth 1 x))
  5944. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5945. org-local-vars)
  5946. (org-set-local 'orgstruct-is-++ t))))
  5947. (defvar orgstruct-is-++ nil
  5948. "Is orgstruct-mode in ++ version in the current-buffer?")
  5949. (make-variable-buffer-local 'orgstruct-is-++)
  5950. ;;;###autoload
  5951. (defun turn-on-orgstruct++ ()
  5952. "Unconditionally turn on `orgstruct++-mode'."
  5953. (orgstruct++-mode 1))
  5954. (defun orgstruct-error ()
  5955. "Error when there is no default binding for a structure key."
  5956. (interactive)
  5957. (error "This key has no function outside structure elements"))
  5958. (defun orgstruct-setup ()
  5959. "Setup orgstruct keymaps."
  5960. (let ((nfunc 0)
  5961. (bindings
  5962. (list
  5963. '([(meta up)] org-metaup)
  5964. '([(meta down)] org-metadown)
  5965. '([(meta left)] org-metaleft)
  5966. '([(meta right)] org-metaright)
  5967. '([(meta shift up)] org-shiftmetaup)
  5968. '([(meta shift down)] org-shiftmetadown)
  5969. '([(meta shift left)] org-shiftmetaleft)
  5970. '([(meta shift right)] org-shiftmetaright)
  5971. '([?\e (up)] org-metaup)
  5972. '([?\e (down)] org-metadown)
  5973. '([?\e (left)] org-metaleft)
  5974. '([?\e (right)] org-metaright)
  5975. '([?\e (shift up)] org-shiftmetaup)
  5976. '([?\e (shift down)] org-shiftmetadown)
  5977. '([?\e (shift left)] org-shiftmetaleft)
  5978. '([?\e (shift right)] org-shiftmetaright)
  5979. '([(shift up)] org-shiftup)
  5980. '([(shift down)] org-shiftdown)
  5981. '([(shift left)] org-shiftleft)
  5982. '([(shift right)] org-shiftright)
  5983. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5984. '("\M-q" fill-paragraph)
  5985. '("\C-c^" org-sort)
  5986. '("\C-c-" org-cycle-list-bullet)))
  5987. elt key fun cmd)
  5988. (while (setq elt (pop bindings))
  5989. (setq nfunc (1+ nfunc))
  5990. (setq key (org-key (car elt))
  5991. fun (nth 1 elt)
  5992. cmd (orgstruct-make-binding fun nfunc key))
  5993. (org-defkey orgstruct-mode-map key cmd))
  5994. ;; Special treatment needed for TAB and RET
  5995. (org-defkey orgstruct-mode-map [(tab)]
  5996. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5997. (org-defkey orgstruct-mode-map "\C-i"
  5998. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5999. (org-defkey orgstruct-mode-map "\M-\C-m"
  6000. (orgstruct-make-binding 'org-insert-heading 105
  6001. "\M-\C-m" [(meta return)]))
  6002. (org-defkey orgstruct-mode-map [(meta return)]
  6003. (orgstruct-make-binding 'org-insert-heading 106
  6004. [(meta return)] "\M-\C-m"))
  6005. (org-defkey orgstruct-mode-map [(shift meta return)]
  6006. (orgstruct-make-binding 'org-insert-todo-heading 107
  6007. [(meta return)] "\M-\C-m"))
  6008. (org-defkey orgstruct-mode-map "\e\C-m"
  6009. (orgstruct-make-binding 'org-insert-heading 108
  6010. "\e\C-m" [?\e (return)]))
  6011. (org-defkey orgstruct-mode-map [?\e (return)]
  6012. (orgstruct-make-binding 'org-insert-heading 109
  6013. [?\e (return)] "\e\C-m"))
  6014. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6015. (orgstruct-make-binding 'org-insert-todo-heading 110
  6016. [?\e (return)] "\e\C-m"))
  6017. (unless org-local-vars
  6018. (setq org-local-vars (org-get-local-variables)))
  6019. t))
  6020. (defun orgstruct-make-binding (fun n &rest keys)
  6021. "Create a function for binding in the structure minor mode.
  6022. FUN is the command to call inside a table. N is used to create a unique
  6023. command name. KEYS are keys that should be checked in for a command
  6024. to execute outside of tables."
  6025. (eval
  6026. (list 'defun
  6027. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6028. '(arg)
  6029. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6030. "Outside of structure, run the binding of `"
  6031. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6032. "'.")
  6033. '(interactive "p")
  6034. (list 'if
  6035. `(org-context-p 'headline 'item
  6036. (and orgstruct-is-++
  6037. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6038. 'item-body))
  6039. (list 'org-run-like-in-org-mode (list 'quote fun))
  6040. (list 'let '(orgstruct-mode)
  6041. (list 'call-interactively
  6042. (append '(or)
  6043. (mapcar (lambda (k)
  6044. (list 'key-binding k))
  6045. keys)
  6046. '('orgstruct-error))))))))
  6047. (defun org-context-p (&rest contexts)
  6048. "Check if local context is any of CONTEXTS.
  6049. Possible values in the list of contexts are `table', `headline', and `item'."
  6050. (let ((pos (point)))
  6051. (goto-char (point-at-bol))
  6052. (prog1 (or (and (memq 'table contexts)
  6053. (looking-at "[ \t]*|"))
  6054. (and (memq 'headline contexts)
  6055. ;;????????? (looking-at "\\*+"))
  6056. (looking-at outline-regexp))
  6057. (and (memq 'item contexts)
  6058. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6059. (and (memq 'item-body contexts)
  6060. (org-in-item-p)))
  6061. (goto-char pos))))
  6062. (defun org-get-local-variables ()
  6063. "Return a list of all local variables in an org-mode buffer."
  6064. (let (varlist)
  6065. (with-current-buffer (get-buffer-create "*Org tmp*")
  6066. (erase-buffer)
  6067. (org-mode)
  6068. (setq varlist (buffer-local-variables)))
  6069. (kill-buffer "*Org tmp*")
  6070. (delq nil
  6071. (mapcar
  6072. (lambda (x)
  6073. (setq x
  6074. (if (symbolp x)
  6075. (list x)
  6076. (list (car x) (list 'quote (cdr x)))))
  6077. (if (string-match
  6078. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6079. (symbol-name (car x)))
  6080. x nil))
  6081. varlist))))
  6082. ;;;###autoload
  6083. (defun org-run-like-in-org-mode (cmd)
  6084. "Run a command, pretending that the current buffer is in Org-mode.
  6085. This will temporarily bind local variables that are typically bound in
  6086. Org-mode to the values they have in Org-mode, and then interactively
  6087. call CMD."
  6088. (org-load-modules-maybe)
  6089. (unless org-local-vars
  6090. (setq org-local-vars (org-get-local-variables)))
  6091. (eval (list 'let org-local-vars
  6092. (list 'call-interactively (list 'quote cmd)))))
  6093. ;;;; Archiving
  6094. (defun org-get-category (&optional pos)
  6095. "Get the category applying to position POS."
  6096. (get-text-property (or pos (point)) 'org-category))
  6097. (defun org-refresh-category-properties ()
  6098. "Refresh category text properties in the buffer."
  6099. (let ((def-cat (cond
  6100. ((null org-category)
  6101. (if buffer-file-name
  6102. (file-name-sans-extension
  6103. (file-name-nondirectory buffer-file-name))
  6104. "???"))
  6105. ((symbolp org-category) (symbol-name org-category))
  6106. (t org-category)))
  6107. beg end cat pos optionp)
  6108. (org-unmodified
  6109. (save-excursion
  6110. (save-restriction
  6111. (widen)
  6112. (goto-char (point-min))
  6113. (put-text-property (point) (point-max) 'org-category def-cat)
  6114. (while (re-search-forward
  6115. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6116. (setq pos (match-end 0)
  6117. optionp (equal (char-after (match-beginning 0)) ?#)
  6118. cat (org-trim (match-string 2)))
  6119. (if optionp
  6120. (setq beg (point-at-bol) end (point-max))
  6121. (org-back-to-heading t)
  6122. (setq beg (point) end (org-end-of-subtree t t)))
  6123. (put-text-property beg end 'org-category cat)
  6124. (goto-char pos)))))))
  6125. ;;;; Link Stuff
  6126. ;;; Link abbreviations
  6127. (defun org-link-expand-abbrev (link)
  6128. "Apply replacements as defined in `org-link-abbrev-alist."
  6129. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6130. (let* ((key (match-string 1 link))
  6131. (as (or (assoc key org-link-abbrev-alist-local)
  6132. (assoc key org-link-abbrev-alist)))
  6133. (tag (and (match-end 2) (match-string 3 link)))
  6134. rpl)
  6135. (if (not as)
  6136. link
  6137. (setq rpl (cdr as))
  6138. (cond
  6139. ((symbolp rpl) (funcall rpl tag))
  6140. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6141. ((string-match "%h" rpl)
  6142. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6143. (t (concat rpl tag)))))
  6144. link))
  6145. ;;; Storing and inserting links
  6146. (defvar org-insert-link-history nil
  6147. "Minibuffer history for links inserted with `org-insert-link'.")
  6148. (defvar org-stored-links nil
  6149. "Contains the links stored with `org-store-link'.")
  6150. (defvar org-store-link-plist nil
  6151. "Plist with info about the most recently link created with `org-store-link'.")
  6152. (defvar org-link-protocols nil
  6153. "Link protocols added to Org-mode using `org-add-link-type'.")
  6154. (defvar org-store-link-functions nil
  6155. "List of functions that are called to create and store a link.
  6156. Each function will be called in turn until one returns a non-nil
  6157. value. Each function should check if it is responsible for creating
  6158. this link (for example by looking at the major mode).
  6159. If not, it must exit and return nil.
  6160. If yes, it should return a non-nil value after a calling
  6161. `org-store-link-props' with a list of properties and values.
  6162. Special properties are:
  6163. :type The link prefix. like \"http\". This must be given.
  6164. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6165. This is obligatory as well.
  6166. :description Optional default description for the second pair
  6167. of brackets in an Org-mode link. The user can still change
  6168. this when inserting this link into an Org-mode buffer.
  6169. In addition to these, any additional properties can be specified
  6170. and then used in remember templates.")
  6171. (defun org-add-link-type (type &optional follow export)
  6172. "Add TYPE to the list of `org-link-types'.
  6173. Re-compute all regular expressions depending on `org-link-types'
  6174. FOLLOW and EXPORT are two functions.
  6175. FOLLOW should take the link path as the single argument and do whatever
  6176. is necessary to follow the link, for example find a file or display
  6177. a mail message.
  6178. EXPORT should format the link path for export to one of the export formats.
  6179. It should be a function accepting three arguments:
  6180. path the path of the link, the text after the prefix (like \"http:\")
  6181. desc the description of the link, if any, nil if there was no description
  6182. format the export format, a symbol like `html' or `latex'.
  6183. The function may use the FORMAT information to return different values
  6184. depending on the format. The return value will be put literally into
  6185. the exported file.
  6186. Org-mode has a built-in default for exporting links. If you are happy with
  6187. this default, there is no need to define an export function for the link
  6188. type. For a simple example of an export function, see `org-bbdb.el'."
  6189. (add-to-list 'org-link-types type t)
  6190. (org-make-link-regexps)
  6191. (if (assoc type org-link-protocols)
  6192. (setcdr (assoc type org-link-protocols) (list follow export))
  6193. (push (list type follow export) org-link-protocols)))
  6194. ;;;###autoload
  6195. (defun org-store-link (arg)
  6196. "\\<org-mode-map>Store an org-link to the current location.
  6197. This link is added to `org-stored-links' and can later be inserted
  6198. into an org-buffer with \\[org-insert-link].
  6199. For some link types, a prefix arg is interpreted:
  6200. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6201. For file links, arg negates `org-context-in-file-links'."
  6202. (interactive "P")
  6203. (org-load-modules-maybe)
  6204. (setq org-store-link-plist nil) ; reset
  6205. (let (link cpltxt desc description search txt custom-id)
  6206. (cond
  6207. ((run-hook-with-args-until-success 'org-store-link-functions)
  6208. (setq link (plist-get org-store-link-plist :link)
  6209. desc (or (plist-get org-store-link-plist :description) link)))
  6210. ((equal (buffer-name) "*Org Edit Src Example*")
  6211. (let (label gc)
  6212. (while (or (not label)
  6213. (save-excursion
  6214. (save-restriction
  6215. (widen)
  6216. (goto-char (point-min))
  6217. (re-search-forward
  6218. (regexp-quote (format org-coderef-label-format label))
  6219. nil t))))
  6220. (when label (message "Label exists already") (sit-for 2))
  6221. (setq label (read-string "Code line label: " label)))
  6222. (end-of-line 1)
  6223. (setq link (format org-coderef-label-format label))
  6224. (setq gc (- 79 (length link)))
  6225. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6226. (insert link)
  6227. (setq link (concat "(" label ")") desc nil)))
  6228. ((eq major-mode 'calendar-mode)
  6229. (let ((cd (calendar-cursor-to-date)))
  6230. (setq link
  6231. (format-time-string
  6232. (car org-time-stamp-formats)
  6233. (apply 'encode-time
  6234. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6235. nil nil nil))))
  6236. (org-store-link-props :type "calendar" :date cd)))
  6237. ((eq major-mode 'w3-mode)
  6238. (setq cpltxt (if (and (buffer-name)
  6239. (not (string-match "Untitled" (buffer-name))))
  6240. (buffer-name)
  6241. (url-view-url t))
  6242. link (org-make-link (url-view-url t)))
  6243. (org-store-link-props :type "w3" :url (url-view-url t)))
  6244. ((eq major-mode 'w3m-mode)
  6245. (setq cpltxt (or w3m-current-title w3m-current-url)
  6246. link (org-make-link w3m-current-url))
  6247. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6248. ((setq search (run-hook-with-args-until-success
  6249. 'org-create-file-search-functions))
  6250. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6251. "::" search))
  6252. (setq cpltxt (or description link)))
  6253. ((eq major-mode 'image-mode)
  6254. (setq cpltxt (concat "file:"
  6255. (abbreviate-file-name buffer-file-name))
  6256. link (org-make-link cpltxt))
  6257. (org-store-link-props :type "image" :file buffer-file-name))
  6258. ((eq major-mode 'dired-mode)
  6259. ;; link to the file in the current line
  6260. (setq cpltxt (concat "file:"
  6261. (abbreviate-file-name
  6262. (expand-file-name
  6263. (dired-get-filename nil t))))
  6264. link (org-make-link cpltxt)))
  6265. ((and buffer-file-name (org-mode-p))
  6266. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6267. (cond
  6268. ((org-in-regexp "<<\\(.*?\\)>>")
  6269. (setq cpltxt
  6270. (concat "file:"
  6271. (abbreviate-file-name buffer-file-name)
  6272. "::" (match-string 1))
  6273. link (org-make-link cpltxt)))
  6274. ((and (featurep 'org-id)
  6275. (or (eq org-link-to-org-use-id t)
  6276. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6277. (interactive-p))
  6278. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6279. (interactive-p)
  6280. (not custom-id))
  6281. (and org-link-to-org-use-id
  6282. (condition-case nil
  6283. (org-entry-get nil "ID")
  6284. (error nil)))))
  6285. ;; We can make a link using the ID.
  6286. (setq link (condition-case nil
  6287. (prog1 (org-id-store-link)
  6288. (setq desc (plist-get org-store-link-plist
  6289. :description)))
  6290. (error
  6291. ;; probably before first headline, link to file only
  6292. (concat "file:"
  6293. (abbreviate-file-name buffer-file-name))))))
  6294. (t
  6295. ;; Just link to current headline
  6296. (setq cpltxt (concat "file:"
  6297. (abbreviate-file-name buffer-file-name)))
  6298. ;; Add a context search string
  6299. (when (org-xor org-context-in-file-links arg)
  6300. (setq txt (cond
  6301. ((org-on-heading-p) nil)
  6302. ((org-region-active-p)
  6303. (buffer-substring (region-beginning) (region-end)))
  6304. (t nil)))
  6305. (when (or (null txt) (string-match "\\S-" txt))
  6306. (setq cpltxt
  6307. (concat cpltxt "::"
  6308. (condition-case nil
  6309. (org-make-org-heading-search-string txt)
  6310. (error "")))
  6311. desc "NONE")))
  6312. (if (string-match "::\\'" cpltxt)
  6313. (setq cpltxt (substring cpltxt 0 -2)))
  6314. (setq link (org-make-link cpltxt)))))
  6315. ((buffer-file-name (buffer-base-buffer))
  6316. ;; Just link to this file here.
  6317. (setq cpltxt (concat "file:"
  6318. (abbreviate-file-name
  6319. (buffer-file-name (buffer-base-buffer)))))
  6320. ;; Add a context string
  6321. (when (org-xor org-context-in-file-links arg)
  6322. (setq txt (if (org-region-active-p)
  6323. (buffer-substring (region-beginning) (region-end))
  6324. (buffer-substring (point-at-bol) (point-at-eol))))
  6325. ;; Only use search option if there is some text.
  6326. (when (string-match "\\S-" txt)
  6327. (setq cpltxt
  6328. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6329. desc "NONE")))
  6330. (setq link (org-make-link cpltxt)))
  6331. ((interactive-p)
  6332. (error "Cannot link to a buffer which is not visiting a file"))
  6333. (t (setq link nil)))
  6334. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6335. (setq link (or link cpltxt)
  6336. desc (or desc cpltxt))
  6337. (if (equal desc "NONE") (setq desc nil))
  6338. (if (and (interactive-p) link)
  6339. (progn
  6340. (setq org-stored-links
  6341. (cons (list link desc) org-stored-links))
  6342. (message "Stored: %s" (or desc link))
  6343. (when custom-id
  6344. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6345. "::#" custom-id))
  6346. (setq org-stored-links
  6347. (cons (list link desc) org-stored-links))))
  6348. (and link (org-make-link-string link desc)))))
  6349. (defun org-store-link-props (&rest plist)
  6350. "Store link properties, extract names and addresses."
  6351. (let (x adr)
  6352. (when (setq x (plist-get plist :from))
  6353. (setq adr (mail-extract-address-components x))
  6354. (setq plist (plist-put plist :fromname (car adr)))
  6355. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6356. (when (setq x (plist-get plist :to))
  6357. (setq adr (mail-extract-address-components x))
  6358. (setq plist (plist-put plist :toname (car adr)))
  6359. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6360. (let ((from (plist-get plist :from))
  6361. (to (plist-get plist :to)))
  6362. (when (and from to org-from-is-user-regexp)
  6363. (setq plist
  6364. (plist-put plist :fromto
  6365. (if (string-match org-from-is-user-regexp from)
  6366. (concat "to %t")
  6367. (concat "from %f"))))))
  6368. (setq org-store-link-plist plist))
  6369. (defun org-add-link-props (&rest plist)
  6370. "Add these properties to the link property list."
  6371. (let (key value)
  6372. (while plist
  6373. (setq key (pop plist) value (pop plist))
  6374. (setq org-store-link-plist
  6375. (plist-put org-store-link-plist key value)))))
  6376. (defun org-email-link-description (&optional fmt)
  6377. "Return the description part of an email link.
  6378. This takes information from `org-store-link-plist' and formats it
  6379. according to FMT (default from `org-email-link-description-format')."
  6380. (setq fmt (or fmt org-email-link-description-format))
  6381. (let* ((p org-store-link-plist)
  6382. (to (plist-get p :toaddress))
  6383. (from (plist-get p :fromaddress))
  6384. (table
  6385. (list
  6386. (cons "%c" (plist-get p :fromto))
  6387. (cons "%F" (plist-get p :from))
  6388. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6389. (cons "%T" (plist-get p :to))
  6390. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6391. (cons "%s" (plist-get p :subject))
  6392. (cons "%m" (plist-get p :message-id)))))
  6393. (when (string-match "%c" fmt)
  6394. ;; Check if the user wrote this message
  6395. (if (and org-from-is-user-regexp from to
  6396. (save-match-data (string-match org-from-is-user-regexp from)))
  6397. (setq fmt (replace-match "to %t" t t fmt))
  6398. (setq fmt (replace-match "from %f" t t fmt))))
  6399. (org-replace-escapes fmt table)))
  6400. (defun org-make-org-heading-search-string (&optional string heading)
  6401. "Make search string for STRING or current headline."
  6402. (interactive)
  6403. (let ((s (or string (org-get-heading))))
  6404. (unless (and string (not heading))
  6405. ;; We are using a headline, clean up garbage in there.
  6406. (if (string-match org-todo-regexp s)
  6407. (setq s (replace-match "" t t s)))
  6408. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6409. (setq s (replace-match "" t t s)))
  6410. (setq s (org-trim s))
  6411. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6412. org-comment-string "\\)") s)
  6413. (setq s (replace-match "" t t s)))
  6414. (while (string-match org-ts-regexp s)
  6415. (setq s (replace-match "" t t s))))
  6416. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6417. (setq s (replace-match " " t t s)))
  6418. (or string (setq s (concat "*" s))) ; Add * for headlines
  6419. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6420. (defun org-make-link (&rest strings)
  6421. "Concatenate STRINGS."
  6422. (apply 'concat strings))
  6423. (defun org-make-link-string (link &optional description)
  6424. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6425. (unless (string-match "\\S-" link)
  6426. (error "Empty link"))
  6427. (when (stringp description)
  6428. ;; Remove brackets from the description, they are fatal.
  6429. (while (string-match "\\[" description)
  6430. (setq description (replace-match "{" t t description)))
  6431. (while (string-match "\\]" description)
  6432. (setq description (replace-match "}" t t description))))
  6433. (when (equal (org-link-escape link) description)
  6434. ;; No description needed, it is identical
  6435. (setq description nil))
  6436. (when (and (not description)
  6437. (not (equal link (org-link-escape link))))
  6438. (setq description (org-extract-attributes link)))
  6439. (concat "[[" (org-link-escape link) "]"
  6440. (if description (concat "[" description "]") "")
  6441. "]"))
  6442. (defconst org-link-escape-chars
  6443. '((?\ . "%20")
  6444. (?\[ . "%5B")
  6445. (?\] . "%5D")
  6446. (?\340 . "%E0") ; `a
  6447. (?\342 . "%E2") ; ^a
  6448. (?\347 . "%E7") ; ,c
  6449. (?\350 . "%E8") ; `e
  6450. (?\351 . "%E9") ; 'e
  6451. (?\352 . "%EA") ; ^e
  6452. (?\356 . "%EE") ; ^i
  6453. (?\364 . "%F4") ; ^o
  6454. (?\371 . "%F9") ; `u
  6455. (?\373 . "%FB") ; ^u
  6456. (?\; . "%3B")
  6457. (?? . "%3F")
  6458. (?= . "%3D")
  6459. (?+ . "%2B")
  6460. )
  6461. "Association list of escapes for some characters problematic in links.
  6462. This is the list that is used for internal purposes.")
  6463. (defvar org-url-encoding-use-url-hexify nil)
  6464. (defconst org-link-escape-chars-browser
  6465. '((?\ . "%20")) ; 32 for the SPC char
  6466. "Association list of escapes for some characters problematic in links.
  6467. This is the list that is used before handing over to the browser.")
  6468. (defun org-link-escape (text &optional table)
  6469. "Escape characters in TEXT that are problematic for links."
  6470. (if org-url-encoding-use-url-hexify
  6471. (url-hexify-string text)
  6472. (setq table (or table org-link-escape-chars))
  6473. (when text
  6474. (let ((re (mapconcat (lambda (x) (regexp-quote
  6475. (char-to-string (car x))))
  6476. table "\\|")))
  6477. (while (string-match re text)
  6478. (setq text
  6479. (replace-match
  6480. (cdr (assoc (string-to-char (match-string 0 text))
  6481. table))
  6482. t t text)))
  6483. text))))
  6484. (defun org-link-unescape (text &optional table)
  6485. "Reverse the action of `org-link-escape'."
  6486. (if org-url-encoding-use-url-hexify
  6487. (url-unhex-string text)
  6488. (setq table (or table org-link-escape-chars))
  6489. (when text
  6490. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6491. table "\\|")))
  6492. (while (string-match re text)
  6493. (setq text
  6494. (replace-match
  6495. (char-to-string (car (rassoc (match-string 0 text) table)))
  6496. t t text)))
  6497. text))))
  6498. (defun org-xor (a b)
  6499. "Exclusive or."
  6500. (if a (not b) b))
  6501. (defun org-fixup-message-id-for-http (s)
  6502. "Replace special characters in a message id, so it can be used in an http query."
  6503. (while (string-match "<" s)
  6504. (setq s (replace-match "%3C" t t s)))
  6505. (while (string-match ">" s)
  6506. (setq s (replace-match "%3E" t t s)))
  6507. (while (string-match "@" s)
  6508. (setq s (replace-match "%40" t t s)))
  6509. s)
  6510. ;;;###autoload
  6511. (defun org-insert-link-global ()
  6512. "Insert a link like Org-mode does.
  6513. This command can be called in any mode to insert a link in Org-mode syntax."
  6514. (interactive)
  6515. (org-load-modules-maybe)
  6516. (org-run-like-in-org-mode 'org-insert-link))
  6517. (defun org-insert-link (&optional complete-file link-location)
  6518. "Insert a link. At the prompt, enter the link.
  6519. Completion can be used to insert any of the link protocol prefixes like
  6520. http or ftp in use.
  6521. The history can be used to select a link previously stored with
  6522. `org-store-link'. When the empty string is entered (i.e. if you just
  6523. press RET at the prompt), the link defaults to the most recently
  6524. stored link. As SPC triggers completion in the minibuffer, you need to
  6525. use M-SPC or C-q SPC to force the insertion of a space character.
  6526. You will also be prompted for a description, and if one is given, it will
  6527. be displayed in the buffer instead of the link.
  6528. If there is already a link at point, this command will allow you to edit link
  6529. and description parts.
  6530. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6531. be selected using completion. The path to the file will be relative to the
  6532. current directory if the file is in the current directory or a subdirectory.
  6533. Otherwise, the link will be the absolute path as completed in the minibuffer
  6534. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6535. option `org-link-file-path-type'.
  6536. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6537. the current directory or below.
  6538. With three \\[universal-argument] prefixes, negate the meaning of
  6539. `org-keep-stored-link-after-insertion'.
  6540. If `org-make-link-description-function' is non-nil, this function will be
  6541. called with the link target, and the result will be the default
  6542. link description.
  6543. If the LINK-LOCATION parameter is non-nil, this value will be
  6544. used as the link location instead of reading one interactively."
  6545. (interactive "P")
  6546. (let* ((wcf (current-window-configuration))
  6547. (region (if (org-region-active-p)
  6548. (buffer-substring (region-beginning) (region-end))))
  6549. (remove (and region (list (region-beginning) (region-end))))
  6550. (desc region)
  6551. tmphist ; byte-compile incorrectly complains about this
  6552. (link link-location)
  6553. entry file all-prefixes)
  6554. (cond
  6555. (link-location) ; specified by arg, just use it.
  6556. ((org-in-regexp org-bracket-link-regexp 1)
  6557. ;; We do have a link at point, and we are going to edit it.
  6558. (setq remove (list (match-beginning 0) (match-end 0)))
  6559. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6560. (setq link (read-string "Link: "
  6561. (org-link-unescape
  6562. (org-match-string-no-properties 1)))))
  6563. ((or (org-in-regexp org-angle-link-re)
  6564. (org-in-regexp org-plain-link-re))
  6565. ;; Convert to bracket link
  6566. (setq remove (list (match-beginning 0) (match-end 0))
  6567. link (read-string "Link: "
  6568. (org-remove-angle-brackets (match-string 0)))))
  6569. ((member complete-file '((4) (16)))
  6570. ;; Completing read for file names.
  6571. (setq link (org-file-complete-link complete-file)))
  6572. (t
  6573. ;; Read link, with completion for stored links.
  6574. (with-output-to-temp-buffer "*Org Links*"
  6575. (princ "Insert a link.
  6576. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6577. (when org-stored-links
  6578. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6579. (princ (mapconcat
  6580. (lambda (x)
  6581. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6582. (reverse org-stored-links) "\n"))))
  6583. (let ((cw (selected-window)))
  6584. (select-window (get-buffer-window "*Org Links*"))
  6585. (setq truncate-lines t)
  6586. (unless (pos-visible-in-window-p (point-max))
  6587. (org-fit-window-to-buffer))
  6588. (and (window-live-p cw) (select-window cw)))
  6589. ;; Fake a link history, containing the stored links.
  6590. (setq tmphist (append (mapcar 'car org-stored-links)
  6591. org-insert-link-history))
  6592. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6593. (mapcar 'car org-link-abbrev-alist)
  6594. org-link-types))
  6595. (unwind-protect
  6596. (progn
  6597. (setq link
  6598. (let ((org-completion-use-ido nil))
  6599. (org-completing-read
  6600. "Link: "
  6601. (mapcar (lambda (x) (list (concat x ":")))
  6602. all-prefixes)
  6603. nil nil nil
  6604. 'tmphist
  6605. (or (car (car org-stored-links))))))
  6606. (if (or (member link all-prefixes)
  6607. (and (equal ":" (substring link -1))
  6608. (member (substring link 0 -1) all-prefixes)
  6609. (setq link (substring link 0 -1))))
  6610. (setq link (org-link-try-special-completion link))))
  6611. (set-window-configuration wcf)
  6612. (kill-buffer "*Org Links*"))
  6613. (setq entry (assoc link org-stored-links))
  6614. (or entry (push link org-insert-link-history))
  6615. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6616. (not org-keep-stored-link-after-insertion))
  6617. (setq org-stored-links (delq (assoc link org-stored-links)
  6618. org-stored-links)))
  6619. (setq desc (or desc (nth 1 entry)))))
  6620. (if (string-match org-plain-link-re link)
  6621. ;; URL-like link, normalize the use of angular brackets.
  6622. (setq link (org-make-link (org-remove-angle-brackets link))))
  6623. ;; Check if we are linking to the current file with a search option
  6624. ;; If yes, simplify the link by using only the search option.
  6625. (when (and buffer-file-name
  6626. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6627. (let* ((path (match-string 1 link))
  6628. (case-fold-search nil)
  6629. (search (match-string 2 link)))
  6630. (save-match-data
  6631. (if (equal (file-truename buffer-file-name) (file-truename path))
  6632. ;; We are linking to this same file, with a search option
  6633. (setq link search)))))
  6634. ;; Check if we can/should use a relative path. If yes, simplify the link
  6635. (when (string-match "^file:\\(.*\\)" link)
  6636. (let* ((path (match-string 1 link))
  6637. (origpath path)
  6638. (case-fold-search nil))
  6639. (cond
  6640. ((or (eq org-link-file-path-type 'absolute)
  6641. (equal complete-file '(16)))
  6642. (setq path (abbreviate-file-name (expand-file-name path))))
  6643. ((eq org-link-file-path-type 'noabbrev)
  6644. (setq path (expand-file-name path)))
  6645. ((eq org-link-file-path-type 'relative)
  6646. (setq path (file-relative-name path)))
  6647. (t
  6648. (save-match-data
  6649. (if (string-match (concat "^" (regexp-quote
  6650. (file-name-as-directory
  6651. (expand-file-name "."))))
  6652. (expand-file-name path))
  6653. ;; We are linking a file with relative path name.
  6654. (setq path (substring (expand-file-name path)
  6655. (match-end 0)))
  6656. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6657. (setq link (concat "file:" path))
  6658. (if (equal desc origpath)
  6659. (setq desc path))))
  6660. (if org-make-link-description-function
  6661. (setq desc (funcall org-make-link-description-function link desc)))
  6662. (setq desc (read-string "Description: " desc))
  6663. (unless (string-match "\\S-" desc) (setq desc nil))
  6664. (if remove (apply 'delete-region remove))
  6665. (insert (org-make-link-string link desc))))
  6666. (defun org-link-try-special-completion (type)
  6667. "If there is completion support for link type TAPE, offer it."
  6668. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6669. (if (functionp fun)
  6670. (funcall fun)
  6671. (read-string "Link (no completion support): " (concat type ":")))))
  6672. (defun org-file-complete-link (&optional arg)
  6673. "Create a file link using completion."
  6674. (let (file link)
  6675. (setq file (read-file-name "File: "))
  6676. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6677. (pwd1 (file-name-as-directory (abbreviate-file-name
  6678. (expand-file-name ".")))))
  6679. (cond
  6680. ((equal arg '(16))
  6681. (setq link (org-make-link
  6682. "file:"
  6683. (abbreviate-file-name (expand-file-name file)))))
  6684. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6685. (setq link (org-make-link "file:" (match-string 1 file))))
  6686. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6687. (expand-file-name file))
  6688. (setq link (org-make-link
  6689. "file:" (match-string 1 (expand-file-name file)))))
  6690. (t (setq link (org-make-link "file:" file)))))
  6691. link))
  6692. (defun org-completing-read (&rest args)
  6693. "Completing-read with SPACE being a normal character."
  6694. (let ((minibuffer-local-completion-map
  6695. (copy-keymap minibuffer-local-completion-map)))
  6696. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6697. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6698. (apply 'org-ido-completing-read args)))
  6699. (defun org-completing-read-no-ido (&rest args)
  6700. (let (org-completion-use-ido)
  6701. (apply 'org-completing-read args)))
  6702. (defun org-ido-completing-read (&rest args)
  6703. "Completing-read using `ido-mode' speedups if available"
  6704. (if (and org-completion-use-ido
  6705. (fboundp 'ido-completing-read)
  6706. (boundp 'ido-mode) ido-mode
  6707. (listp (second args)))
  6708. (let ((ido-enter-matching-directory nil))
  6709. (apply 'ido-completing-read (concat (car args))
  6710. (mapcar (lambda (x) (car x)) (nth 1 args))
  6711. (cddr args)))
  6712. (apply 'completing-read args)))
  6713. (defun org-extract-attributes (s)
  6714. "Extract the attributes cookie from a string and set as text property."
  6715. (let (a attr (start 0) key value)
  6716. (save-match-data
  6717. (when (string-match "{{\\([^}]+\\)}}$" s)
  6718. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6719. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6720. (setq key (match-string 1 a) value (match-string 2 a)
  6721. start (match-end 0)
  6722. attr (plist-put attr (intern key) value))))
  6723. (org-add-props s nil 'org-attr attr))
  6724. s))
  6725. (defun org-extract-attributes-from-string (tag)
  6726. (let (key value attr)
  6727. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6728. (setq key (match-string 1 tag) value (match-string 2 tag)
  6729. tag (replace-match "" t t tag)
  6730. attr (plist-put attr (intern key) value)))
  6731. (cons tag attr)))
  6732. (defun org-attributes-to-string (plist)
  6733. "Format a property list into an HTML attribute list."
  6734. (let ((s "") key value)
  6735. (while plist
  6736. (setq key (pop plist) value (pop plist))
  6737. (and value
  6738. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6739. s))
  6740. ;;; Opening/following a link
  6741. (defvar org-link-search-failed nil)
  6742. (defun org-next-link ()
  6743. "Move forward to the next link.
  6744. If the link is in hidden text, expose it."
  6745. (interactive)
  6746. (when (and org-link-search-failed (eq this-command last-command))
  6747. (goto-char (point-min))
  6748. (message "Link search wrapped back to beginning of buffer"))
  6749. (setq org-link-search-failed nil)
  6750. (let* ((pos (point))
  6751. (ct (org-context))
  6752. (a (assoc :link ct)))
  6753. (if a (goto-char (nth 2 a)))
  6754. (if (re-search-forward org-any-link-re nil t)
  6755. (progn
  6756. (goto-char (match-beginning 0))
  6757. (if (org-invisible-p) (org-show-context)))
  6758. (goto-char pos)
  6759. (setq org-link-search-failed t)
  6760. (error "No further link found"))))
  6761. (defun org-previous-link ()
  6762. "Move backward to the previous link.
  6763. If the link is in hidden text, expose it."
  6764. (interactive)
  6765. (when (and org-link-search-failed (eq this-command last-command))
  6766. (goto-char (point-max))
  6767. (message "Link search wrapped back to end of buffer"))
  6768. (setq org-link-search-failed nil)
  6769. (let* ((pos (point))
  6770. (ct (org-context))
  6771. (a (assoc :link ct)))
  6772. (if a (goto-char (nth 1 a)))
  6773. (if (re-search-backward org-any-link-re nil t)
  6774. (progn
  6775. (goto-char (match-beginning 0))
  6776. (if (org-invisible-p) (org-show-context)))
  6777. (goto-char pos)
  6778. (setq org-link-search-failed t)
  6779. (error "No further link found"))))
  6780. (defun org-translate-link (s)
  6781. "Translate a link string if a translation function has been defined."
  6782. (if (and org-link-translation-function
  6783. (fboundp org-link-translation-function)
  6784. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6785. (progn
  6786. (setq s (funcall org-link-translation-function
  6787. (match-string 1) (match-string 2)))
  6788. (concat (car s) ":" (cdr s)))
  6789. s))
  6790. (defun org-translate-link-from-planner (type path)
  6791. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6792. This is still an experimental function, your mileage may vary."
  6793. (cond
  6794. ((member type '("http" "https" "news" "ftp"))
  6795. ;; standard Internet links are the same.
  6796. nil)
  6797. ((and (equal type "irc") (string-match "^//" path))
  6798. ;; Planner has two / at the beginning of an irc link, we have 1.
  6799. ;; We should have zero, actually....
  6800. (setq path (substring path 1)))
  6801. ((and (equal type "lisp") (string-match "^/" path))
  6802. ;; Planner has a slash, we do not.
  6803. (setq type "elisp" path (substring path 1)))
  6804. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6805. ;; A typical message link. Planner has the id after the fina slash,
  6806. ;; we separate it with a hash mark
  6807. (setq path (concat (match-string 1 path) "#"
  6808. (org-remove-angle-brackets (match-string 2 path)))))
  6809. )
  6810. (cons type path))
  6811. (defun org-find-file-at-mouse (ev)
  6812. "Open file link or URL at mouse."
  6813. (interactive "e")
  6814. (mouse-set-point ev)
  6815. (org-open-at-point 'in-emacs))
  6816. (defun org-open-at-mouse (ev)
  6817. "Open file link or URL at mouse."
  6818. (interactive "e")
  6819. (mouse-set-point ev)
  6820. (if (eq major-mode 'org-agenda-mode)
  6821. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6822. (org-open-at-point))
  6823. (defvar org-window-config-before-follow-link nil
  6824. "The window configuration before following a link.
  6825. This is saved in case the need arises to restore it.")
  6826. (defvar org-open-link-marker (make-marker)
  6827. "Marker pointing to the location where `org-open-at-point; was called.")
  6828. ;;;###autoload
  6829. (defun org-open-at-point-global ()
  6830. "Follow a link like Org-mode does.
  6831. This command can be called in any mode to follow a link that has
  6832. Org-mode syntax."
  6833. (interactive)
  6834. (org-run-like-in-org-mode 'org-open-at-point))
  6835. ;;;###autoload
  6836. (defun org-open-link-from-string (s &optional arg)
  6837. "Open a link in the string S, as if it was in Org-mode."
  6838. (interactive "sLink: \nP")
  6839. (let ((reference-buffer (current-buffer)))
  6840. (with-temp-buffer
  6841. (let ((org-inhibit-startup t))
  6842. (org-mode)
  6843. (insert s)
  6844. (goto-char (point-min))
  6845. (org-open-at-point arg reference-buffer)))))
  6846. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6847. "Open link at or after point.
  6848. If there is no link at point, this function will search forward up to
  6849. the end of the current line.
  6850. Normally, files will be opened by an appropriate application. If the
  6851. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6852. With a double prefix argument, try to open outside of Emacs, in the
  6853. application the system uses for this file type."
  6854. (interactive "P")
  6855. (org-load-modules-maybe)
  6856. (move-marker org-open-link-marker (point))
  6857. (setq org-window-config-before-follow-link (current-window-configuration))
  6858. (org-remove-occur-highlights nil nil t)
  6859. (cond
  6860. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6861. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6862. (org-footnote-action))
  6863. (t
  6864. (let (type path link line search (pos (point)))
  6865. (catch 'match
  6866. (save-excursion
  6867. (skip-chars-forward "^]\n\r")
  6868. (when (org-in-regexp org-bracket-link-regexp)
  6869. (setq link (org-extract-attributes
  6870. (org-link-unescape (org-match-string-no-properties 1))))
  6871. (while (string-match " *\n *" link)
  6872. (setq link (replace-match " " t t link)))
  6873. (setq link (org-link-expand-abbrev link))
  6874. (cond
  6875. ((or (file-name-absolute-p link)
  6876. (string-match "^\\.\\.?/" link))
  6877. (setq type "file" path link))
  6878. ((string-match org-link-re-with-space3 link)
  6879. (setq type (match-string 1 link) path (match-string 2 link)))
  6880. (t (setq type "thisfile" path link)))
  6881. (throw 'match t)))
  6882. (when (get-text-property (point) 'org-linked-text)
  6883. (setq type "thisfile"
  6884. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6885. (1+ (point)) (point))
  6886. path (buffer-substring
  6887. (previous-single-property-change pos 'org-linked-text)
  6888. (next-single-property-change pos 'org-linked-text)))
  6889. (throw 'match t))
  6890. (save-excursion
  6891. (when (or (org-in-regexp org-angle-link-re)
  6892. (org-in-regexp org-plain-link-re))
  6893. (setq type (match-string 1) path (match-string 2))
  6894. (throw 'match t)))
  6895. (save-excursion
  6896. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6897. (setq type "tags"
  6898. path (match-string 1))
  6899. (while (string-match ":" path)
  6900. (setq path (replace-match "+" t t path)))
  6901. (throw 'match t)))
  6902. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6903. (setq type "tree-match"
  6904. path (match-string 1))
  6905. (throw 'match t)))
  6906. (unless path
  6907. (error "No link found"))
  6908. ;; switch back to reference buffer
  6909. ;; needed when if called in a temporary buffer through
  6910. ;; org-open-link-from-string
  6911. (and reference-buffer (switch-to-buffer reference-buffer))
  6912. ;; Remove any trailing spaces in path
  6913. (if (string-match " +\\'" path)
  6914. (setq path (replace-match "" t t path)))
  6915. (if (and org-link-translation-function
  6916. (fboundp org-link-translation-function))
  6917. ;; Check if we need to translate the link
  6918. (let ((tmp (funcall org-link-translation-function type path)))
  6919. (setq type (car tmp) path (cdr tmp))))
  6920. (cond
  6921. ((assoc type org-link-protocols)
  6922. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6923. ((equal type "mailto")
  6924. (let ((cmd (car org-link-mailto-program))
  6925. (args (cdr org-link-mailto-program)) args1
  6926. (address path) (subject "") a)
  6927. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6928. (setq address (match-string 1 path)
  6929. subject (org-link-escape (match-string 2 path))))
  6930. (while args
  6931. (cond
  6932. ((not (stringp (car args))) (push (pop args) args1))
  6933. (t (setq a (pop args))
  6934. (if (string-match "%a" a)
  6935. (setq a (replace-match address t t a)))
  6936. (if (string-match "%s" a)
  6937. (setq a (replace-match subject t t a)))
  6938. (push a args1))))
  6939. (apply cmd (nreverse args1))))
  6940. ((member type '("http" "https" "ftp" "news"))
  6941. (browse-url (concat type ":" (org-link-escape
  6942. path org-link-escape-chars-browser))))
  6943. ((member type '("message"))
  6944. (browse-url (concat type ":" path)))
  6945. ((string= type "tags")
  6946. (org-tags-view in-emacs path))
  6947. ((string= type "thisfile")
  6948. (if in-emacs
  6949. (switch-to-buffer-other-window
  6950. (org-get-buffer-for-internal-link (current-buffer)))
  6951. (org-mark-ring-push))
  6952. (let ((cmd `(org-link-search
  6953. ,path
  6954. ,(cond ((equal in-emacs '(4)) 'occur)
  6955. ((equal in-emacs '(16)) 'org-occur)
  6956. (t nil))
  6957. ,pos)))
  6958. (condition-case nil (eval cmd)
  6959. (error (progn (widen) (eval cmd))))))
  6960. ((string= type "tree-match")
  6961. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6962. ((string= type "file")
  6963. (if (string-match "::\\([0-9]+\\)\\'" path)
  6964. (setq line (string-to-number (match-string 1 path))
  6965. path (substring path 0 (match-beginning 0)))
  6966. (if (string-match "::\\(.+\\)\\'" path)
  6967. (setq search (match-string 1 path)
  6968. path (substring path 0 (match-beginning 0)))))
  6969. (if (string-match "[*?{]" (file-name-nondirectory path))
  6970. (dired path)
  6971. (org-open-file path in-emacs line search)))
  6972. ((string= type "news")
  6973. (require 'org-gnus)
  6974. (org-gnus-follow-link path))
  6975. ((string= type "shell")
  6976. (let ((cmd path))
  6977. (if (or (not org-confirm-shell-link-function)
  6978. (funcall org-confirm-shell-link-function
  6979. (format "Execute \"%s\" in shell? "
  6980. (org-add-props cmd nil
  6981. 'face 'org-warning))))
  6982. (progn
  6983. (message "Executing %s" cmd)
  6984. (shell-command cmd))
  6985. (error "Abort"))))
  6986. ((string= type "elisp")
  6987. (let ((cmd path))
  6988. (if (or (not org-confirm-elisp-link-function)
  6989. (funcall org-confirm-elisp-link-function
  6990. (format "Execute \"%s\" as elisp? "
  6991. (org-add-props cmd nil
  6992. 'face 'org-warning))))
  6993. (message "%s => %s" cmd
  6994. (if (equal (string-to-char cmd) ?\()
  6995. (eval (read cmd))
  6996. (call-interactively (read cmd))))
  6997. (error "Abort"))))
  6998. (t
  6999. (browse-url-at-point))))))
  7000. (move-marker org-open-link-marker nil)
  7001. (run-hook-with-args 'org-follow-link-hook))
  7002. ;;;; Time estimates
  7003. (defun org-get-effort (&optional pom)
  7004. "Get the effort estimate for the current entry."
  7005. (org-entry-get pom org-effort-property))
  7006. ;;; File search
  7007. (defvar org-create-file-search-functions nil
  7008. "List of functions to construct the right search string for a file link.
  7009. These functions are called in turn with point at the location to
  7010. which the link should point.
  7011. A function in the hook should first test if it would like to
  7012. handle this file type, for example by checking the major-mode or
  7013. the file extension. If it decides not to handle this file, it
  7014. should just return nil to give other functions a chance. If it
  7015. does handle the file, it must return the search string to be used
  7016. when following the link. The search string will be part of the
  7017. file link, given after a double colon, and `org-open-at-point'
  7018. will automatically search for it. If special measures must be
  7019. taken to make the search successful, another function should be
  7020. added to the companion hook `org-execute-file-search-functions',
  7021. which see.
  7022. A function in this hook may also use `setq' to set the variable
  7023. `description' to provide a suggestion for the descriptive text to
  7024. be used for this link when it gets inserted into an Org-mode
  7025. buffer with \\[org-insert-link].")
  7026. (defvar org-execute-file-search-functions nil
  7027. "List of functions to execute a file search triggered by a link.
  7028. Functions added to this hook must accept a single argument, the
  7029. search string that was part of the file link, the part after the
  7030. double colon. The function must first check if it would like to
  7031. handle this search, for example by checking the major-mode or the
  7032. file extension. If it decides not to handle this search, it
  7033. should just return nil to give other functions a chance. If it
  7034. does handle the search, it must return a non-nil value to keep
  7035. other functions from trying.
  7036. Each function can access the current prefix argument through the
  7037. variable `current-prefix-argument'. Note that a single prefix is
  7038. used to force opening a link in Emacs, so it may be good to only
  7039. use a numeric or double prefix to guide the search function.
  7040. In case this is needed, a function in this hook can also restore
  7041. the window configuration before `org-open-at-point' was called using:
  7042. (set-window-configuration org-window-config-before-follow-link)")
  7043. (defun org-link-search (s &optional type avoid-pos)
  7044. "Search for a link search option.
  7045. If S is surrounded by forward slashes, it is interpreted as a
  7046. regular expression. In org-mode files, this will create an `org-occur'
  7047. sparse tree. In ordinary files, `occur' will be used to list matches.
  7048. If the current buffer is in `dired-mode', grep will be used to search
  7049. in all files. If AVOID-POS is given, ignore matches near that position."
  7050. (let ((case-fold-search t)
  7051. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7052. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7053. (append '(("") (" ") ("\t") ("\n"))
  7054. org-emphasis-alist)
  7055. "\\|") "\\)"))
  7056. (pos (point))
  7057. (pre nil) (post nil)
  7058. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7059. (cond
  7060. ;; First check if there are any special
  7061. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7062. ;; Now try the builtin stuff
  7063. ((and (equal (string-to-char s0) ?#)
  7064. (> (length s0) 1)
  7065. (save-excursion
  7066. (goto-char (point-min))
  7067. (and
  7068. (re-search-forward
  7069. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7070. (setq type 'dedicated
  7071. pos (match-beginning 0))))
  7072. ;; There is an exact target for this
  7073. (goto-char pos)
  7074. (org-back-to-heading t)))
  7075. ((save-excursion
  7076. (goto-char (point-min))
  7077. (and
  7078. (re-search-forward
  7079. (concat "<<" (regexp-quote s0) ">>") nil t)
  7080. (setq type 'dedicated
  7081. pos (match-beginning 0))))
  7082. ;; There is an exact target for this
  7083. (goto-char pos))
  7084. ((and (string-match "^(\\(.*\\))$" s0)
  7085. (save-excursion
  7086. (goto-char (point-min))
  7087. (and
  7088. (re-search-forward
  7089. (concat "[^[]" (regexp-quote
  7090. (format org-coderef-label-format
  7091. (match-string 1 s0))))
  7092. nil t)
  7093. (setq type 'dedicated
  7094. pos (1+ (match-beginning 0))))))
  7095. ;; There is a coderef target for this
  7096. (goto-char pos))
  7097. ((string-match "^/\\(.*\\)/$" s)
  7098. ;; A regular expression
  7099. (cond
  7100. ((org-mode-p)
  7101. (org-occur (match-string 1 s)))
  7102. ;;((eq major-mode 'dired-mode)
  7103. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7104. (t (org-do-occur (match-string 1 s)))))
  7105. (t
  7106. ;; A normal search strings
  7107. (when (equal (string-to-char s) ?*)
  7108. ;; Anchor on headlines, post may include tags.
  7109. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7110. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7111. s (substring s 1)))
  7112. (remove-text-properties
  7113. 0 (length s)
  7114. '(face nil mouse-face nil keymap nil fontified nil) s)
  7115. ;; Make a series of regular expressions to find a match
  7116. (setq words (org-split-string s "[ \n\r\t]+")
  7117. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7118. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7119. "\\)" markers)
  7120. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7121. re2a (concat "[ \t\r\n]" re2a_)
  7122. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7123. re4 (concat "[^a-zA-Z_]" re4_)
  7124. re1 (concat pre re2 post)
  7125. re3 (concat pre (if pre re4_ re4) post)
  7126. re5 (concat pre ".*" re4)
  7127. re2 (concat pre re2)
  7128. re2a (concat pre (if pre re2a_ re2a))
  7129. re4 (concat pre (if pre re4_ re4))
  7130. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7131. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7132. re5 "\\)"
  7133. ))
  7134. (cond
  7135. ((eq type 'org-occur) (org-occur reall))
  7136. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7137. (t (goto-char (point-min))
  7138. (setq type 'fuzzy)
  7139. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7140. (org-search-not-self 1 re1 nil t)
  7141. (org-search-not-self 1 re2 nil t)
  7142. (org-search-not-self 1 re2a nil t)
  7143. (org-search-not-self 1 re3 nil t)
  7144. (org-search-not-self 1 re4 nil t)
  7145. (org-search-not-self 1 re5 nil t)
  7146. )
  7147. (goto-char (match-beginning 1))
  7148. (goto-char pos)
  7149. (error "No match")))))
  7150. (t
  7151. ;; Normal string-search
  7152. (goto-char (point-min))
  7153. (if (search-forward s nil t)
  7154. (goto-char (match-beginning 0))
  7155. (error "No match"))))
  7156. (and (org-mode-p) (org-show-context 'link-search))
  7157. type))
  7158. (defun org-search-not-self (group &rest args)
  7159. "Execute `re-search-forward', but only accept matches that do not
  7160. enclose the position of `org-open-link-marker'."
  7161. (let ((m org-open-link-marker))
  7162. (catch 'exit
  7163. (while (apply 're-search-forward args)
  7164. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7165. (goto-char (match-end group))
  7166. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7167. (> (match-beginning 0) (marker-position m))
  7168. (< (match-end 0) (marker-position m)))
  7169. (save-match-data
  7170. (or (not (org-in-regexp
  7171. org-bracket-link-analytic-regexp 1))
  7172. (not (match-end 4)) ; no description
  7173. (and (<= (match-beginning 4) (point))
  7174. (>= (match-end 4) (point))))))
  7175. (throw 'exit (point))))))))
  7176. (defun org-get-buffer-for-internal-link (buffer)
  7177. "Return a buffer to be used for displaying the link target of internal links."
  7178. (cond
  7179. ((not org-display-internal-link-with-indirect-buffer)
  7180. buffer)
  7181. ((string-match "(Clone)$" (buffer-name buffer))
  7182. (message "Buffer is already a clone, not making another one")
  7183. ;; we also do not modify visibility in this case
  7184. buffer)
  7185. (t ; make a new indirect buffer for displaying the link
  7186. (let* ((bn (buffer-name buffer))
  7187. (ibn (concat bn "(Clone)"))
  7188. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7189. (with-current-buffer ib (org-overview))
  7190. ib))))
  7191. (defun org-do-occur (regexp &optional cleanup)
  7192. "Call the Emacs command `occur'.
  7193. If CLEANUP is non-nil, remove the printout of the regular expression
  7194. in the *Occur* buffer. This is useful if the regex is long and not useful
  7195. to read."
  7196. (occur regexp)
  7197. (when cleanup
  7198. (let ((cwin (selected-window)) win beg end)
  7199. (when (setq win (get-buffer-window "*Occur*"))
  7200. (select-window win))
  7201. (goto-char (point-min))
  7202. (when (re-search-forward "match[a-z]+" nil t)
  7203. (setq beg (match-end 0))
  7204. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7205. (setq end (1- (match-beginning 0)))))
  7206. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7207. (goto-char (point-min))
  7208. (select-window cwin))))
  7209. ;;; The mark ring for links jumps
  7210. (defvar org-mark-ring nil
  7211. "Mark ring for positions before jumps in Org-mode.")
  7212. (defvar org-mark-ring-last-goto nil
  7213. "Last position in the mark ring used to go back.")
  7214. ;; Fill and close the ring
  7215. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7216. (loop for i from 1 to org-mark-ring-length do
  7217. (push (make-marker) org-mark-ring))
  7218. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7219. org-mark-ring)
  7220. (defun org-mark-ring-push (&optional pos buffer)
  7221. "Put the current position or POS into the mark ring and rotate it."
  7222. (interactive)
  7223. (setq pos (or pos (point)))
  7224. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7225. (move-marker (car org-mark-ring)
  7226. (or pos (point))
  7227. (or buffer (current-buffer)))
  7228. (message "%s"
  7229. (substitute-command-keys
  7230. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7231. (defun org-mark-ring-goto (&optional n)
  7232. "Jump to the previous position in the mark ring.
  7233. With prefix arg N, jump back that many stored positions. When
  7234. called several times in succession, walk through the entire ring.
  7235. Org-mode commands jumping to a different position in the current file,
  7236. or to another Org-mode file, automatically push the old position
  7237. onto the ring."
  7238. (interactive "p")
  7239. (let (p m)
  7240. (if (eq last-command this-command)
  7241. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7242. (setq p org-mark-ring))
  7243. (setq org-mark-ring-last-goto p)
  7244. (setq m (car p))
  7245. (switch-to-buffer (marker-buffer m))
  7246. (goto-char m)
  7247. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7248. (defun org-remove-angle-brackets (s)
  7249. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7250. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7251. s)
  7252. (defun org-add-angle-brackets (s)
  7253. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7254. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7255. s)
  7256. (defun org-remove-double-quotes (s)
  7257. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7258. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7259. s)
  7260. ;;; Following specific links
  7261. (defun org-follow-timestamp-link ()
  7262. (cond
  7263. ((org-at-date-range-p t)
  7264. (let ((org-agenda-start-on-weekday)
  7265. (t1 (match-string 1))
  7266. (t2 (match-string 2)))
  7267. (setq t1 (time-to-days (org-time-string-to-time t1))
  7268. t2 (time-to-days (org-time-string-to-time t2)))
  7269. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7270. ((org-at-timestamp-p t)
  7271. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7272. (substring (match-string 1) 0 10)))
  7273. 1))
  7274. (t (error "This should not happen"))))
  7275. ;;; Following file links
  7276. (defvar org-wait nil)
  7277. (defun org-open-file (path &optional in-emacs line search)
  7278. "Open the file at PATH.
  7279. First, this expands any special file name abbreviations. Then the
  7280. configuration variable `org-file-apps' is checked if it contains an
  7281. entry for this file type, and if yes, the corresponding command is launched.
  7282. If no application is found, Emacs simply visits the file.
  7283. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7284. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7285. and o use an external application to visit the file.
  7286. Optional LINE specifies a line to go to, optional SEARCH a string to
  7287. search for. If LINE or SEARCH is given, the file will always be
  7288. opened in Emacs.
  7289. If the file does not exist, an error is thrown."
  7290. (setq in-emacs (or in-emacs line search))
  7291. (let* ((file (if (equal path "")
  7292. buffer-file-name
  7293. (substitute-in-file-name (expand-file-name path))))
  7294. (apps (append org-file-apps (org-default-apps)))
  7295. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7296. (dirp (if remp nil (file-directory-p file)))
  7297. (file (if (and dirp org-open-directory-means-index-dot-org)
  7298. (concat (file-name-as-directory file) "index.org")
  7299. file))
  7300. (a-m-a-p (assq 'auto-mode apps))
  7301. (dfile (downcase file))
  7302. (old-buffer (current-buffer))
  7303. (old-pos (point))
  7304. (old-mode major-mode)
  7305. ext cmd)
  7306. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7307. (setq ext (match-string 1 dfile))
  7308. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7309. (setq ext (match-string 1 dfile))))
  7310. (cond
  7311. ((equal in-emacs '(16))
  7312. (setq cmd (cdr (assoc 'system apps))))
  7313. (in-emacs (setq cmd 'emacs))
  7314. (t
  7315. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7316. (and dirp (cdr (assoc 'directory apps)))
  7317. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7318. 'string-match)
  7319. (cdr (assoc ext apps))
  7320. (cdr (assoc t apps))))))
  7321. (when (eq cmd 'system)
  7322. (setq cmd (cdr (assoc 'system apps))))
  7323. (when (eq cmd 'default)
  7324. (setq cmd (cdr (assoc t apps))))
  7325. (when (eq cmd 'mailcap)
  7326. (require 'mailcap)
  7327. (mailcap-parse-mailcaps)
  7328. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7329. (command (mailcap-mime-info mime-type)))
  7330. (if (stringp command)
  7331. (setq cmd command)
  7332. (setq cmd 'emacs))))
  7333. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7334. (not (file-exists-p file))
  7335. (not org-open-non-existing-files))
  7336. (error "No such file: %s" file))
  7337. (cond
  7338. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7339. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7340. (while (string-match "['\"]%s['\"]" cmd)
  7341. (setq cmd (replace-match "%s" t t cmd)))
  7342. (while (string-match "%s" cmd)
  7343. (setq cmd (replace-match
  7344. (save-match-data
  7345. (shell-quote-argument
  7346. (convert-standard-filename file)))
  7347. t t cmd)))
  7348. (save-window-excursion
  7349. (start-process-shell-command cmd nil cmd)
  7350. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7351. ))
  7352. ((or (stringp cmd)
  7353. (eq cmd 'emacs))
  7354. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7355. (widen)
  7356. (if line (goto-line line)
  7357. (if search (org-link-search search))))
  7358. ((consp cmd)
  7359. (let ((file (convert-standard-filename file)))
  7360. (eval cmd)))
  7361. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7362. (and (org-mode-p) (eq old-mode 'org-mode)
  7363. (or (not (equal old-buffer (current-buffer)))
  7364. (not (equal old-pos (point))))
  7365. (org-mark-ring-push old-pos old-buffer))))
  7366. (defun org-default-apps ()
  7367. "Return the default applications for this operating system."
  7368. (cond
  7369. ((eq system-type 'darwin)
  7370. org-file-apps-defaults-macosx)
  7371. ((eq system-type 'windows-nt)
  7372. org-file-apps-defaults-windowsnt)
  7373. (t org-file-apps-defaults-gnu)))
  7374. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7375. "Convert extensions to regular expressions in the cars of LIST.
  7376. Also, weed out any non-string entries, because the return value is used
  7377. only for regexp matching.
  7378. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7379. point to the symbol `emacs', indicating that the file should
  7380. be opened in Emacs."
  7381. (append
  7382. (delq nil
  7383. (mapcar (lambda (x)
  7384. (if (not (stringp (car x)))
  7385. nil
  7386. (if (string-match "\\W" (car x))
  7387. x
  7388. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7389. list))
  7390. (if add-auto-mode
  7391. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7392. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7393. (defun org-file-remote-p (file)
  7394. "Test whether FILE specifies a location on a remote system.
  7395. Return non-nil if the location is indeed remote.
  7396. For example, the filename \"/user@host:/foo\" specifies a location
  7397. on the system \"/user@host:\"."
  7398. (cond ((fboundp 'file-remote-p)
  7399. (file-remote-p file))
  7400. ((fboundp 'tramp-handle-file-remote-p)
  7401. (tramp-handle-file-remote-p file))
  7402. ((and (boundp 'ange-ftp-name-format)
  7403. (string-match (car ange-ftp-name-format) file))
  7404. t)
  7405. (t nil)))
  7406. ;;;; Refiling
  7407. (defun org-get-org-file ()
  7408. "Read a filename, with default directory `org-directory'."
  7409. (let ((default (or org-default-notes-file remember-data-file)))
  7410. (read-file-name (format "File name [%s]: " default)
  7411. (file-name-as-directory org-directory)
  7412. default)))
  7413. (defun org-notes-order-reversed-p ()
  7414. "Check if the current file should receive notes in reversed order."
  7415. (cond
  7416. ((not org-reverse-note-order) nil)
  7417. ((eq t org-reverse-note-order) t)
  7418. ((not (listp org-reverse-note-order)) nil)
  7419. (t (catch 'exit
  7420. (let ((all org-reverse-note-order)
  7421. entry)
  7422. (while (setq entry (pop all))
  7423. (if (string-match (car entry) buffer-file-name)
  7424. (throw 'exit (cdr entry))))
  7425. nil)))))
  7426. (defvar org-refile-target-table nil
  7427. "The list of refile targets, created by `org-refile'.")
  7428. (defvar org-agenda-new-buffers nil
  7429. "Buffers created to visit agenda files.")
  7430. (defun org-get-refile-targets (&optional default-buffer)
  7431. "Produce a table with refile targets."
  7432. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7433. targets txt re files f desc descre fast-path-p level pos0)
  7434. (message "Getting targets...")
  7435. (with-current-buffer (or default-buffer (current-buffer))
  7436. (while (setq entry (pop entries))
  7437. (setq files (car entry) desc (cdr entry))
  7438. (setq fast-path-p nil)
  7439. (cond
  7440. ((null files) (setq files (list (current-buffer))))
  7441. ((eq files 'org-agenda-files)
  7442. (setq files (org-agenda-files 'unrestricted)))
  7443. ((and (symbolp files) (fboundp files))
  7444. (setq files (funcall files)))
  7445. ((and (symbolp files) (boundp files))
  7446. (setq files (symbol-value files))))
  7447. (if (stringp files) (setq files (list files)))
  7448. (cond
  7449. ((eq (car desc) :tag)
  7450. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7451. ((eq (car desc) :todo)
  7452. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7453. ((eq (car desc) :regexp)
  7454. (setq descre (cdr desc)))
  7455. ((eq (car desc) :level)
  7456. (setq descre (concat "^\\*\\{" (number-to-string
  7457. (if org-odd-levels-only
  7458. (1- (* 2 (cdr desc)))
  7459. (cdr desc)))
  7460. "\\}[ \t]")))
  7461. ((eq (car desc) :maxlevel)
  7462. (setq fast-path-p t)
  7463. (setq descre (concat "^\\*\\{1," (number-to-string
  7464. (if org-odd-levels-only
  7465. (1- (* 2 (cdr desc)))
  7466. (cdr desc)))
  7467. "\\}[ \t]")))
  7468. (t (error "Bad refiling target description %s" desc)))
  7469. (while (setq f (pop files))
  7470. (save-excursion
  7471. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7472. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7473. (setq f (expand-file-name f))
  7474. (if (eq org-refile-use-outline-path 'file)
  7475. (push (list (file-name-nondirectory f) f nil nil) targets))
  7476. (save-excursion
  7477. (save-restriction
  7478. (widen)
  7479. (goto-char (point-min))
  7480. (while (re-search-forward descre nil t)
  7481. (goto-char (setq pos0 (point-at-bol)))
  7482. (catch 'next
  7483. (when org-refile-target-verify-function
  7484. (save-match-data
  7485. (or (funcall org-refile-target-verify-function)
  7486. (throw 'next t))))
  7487. (when (looking-at org-complex-heading-regexp)
  7488. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7489. txt (org-link-display-format (match-string 4))
  7490. re (concat "^" (regexp-quote
  7491. (buffer-substring (match-beginning 1)
  7492. (match-end 4)))))
  7493. (if (match-end 5) (setq re (concat re "[ \t]+"
  7494. (regexp-quote
  7495. (match-string 5)))))
  7496. (setq re (concat re "[ \t]*$"))
  7497. (when org-refile-use-outline-path
  7498. (setq txt (mapconcat 'org-protect-slash
  7499. (append
  7500. (if (eq org-refile-use-outline-path 'file)
  7501. (list (file-name-nondirectory
  7502. (buffer-file-name (buffer-base-buffer))))
  7503. (if (eq org-refile-use-outline-path 'full-file-path)
  7504. (list (buffer-file-name (buffer-base-buffer)))))
  7505. (org-get-outline-path fast-path-p level txt)
  7506. (list txt))
  7507. "/")))
  7508. (push (list txt f re (point)) targets)))
  7509. (when (= (point) pos0)
  7510. ;; verification function has not moved point
  7511. (goto-char (point-at-eol))))))))))
  7512. (message "Getting targets...done")
  7513. (nreverse targets)))
  7514. (defun org-protect-slash (s)
  7515. (while (string-match "/" s)
  7516. (setq s (replace-match "\\" t t s)))
  7517. s)
  7518. (defvar org-olpa (make-vector 20 nil))
  7519. (defun org-get-outline-path (&optional fastp level heading)
  7520. "Return the outline path to the current entry, as a list."
  7521. (if fastp
  7522. (progn
  7523. (if (> level 19)
  7524. (error "Outline path failure, more than 19 levels."))
  7525. (loop for i from level upto 19 do
  7526. (aset org-olpa i nil))
  7527. (prog1
  7528. (delq nil (append org-olpa nil))
  7529. (aset org-olpa level heading)))
  7530. (let (rtn)
  7531. (save-excursion
  7532. (while (org-up-heading-safe)
  7533. (when (looking-at org-complex-heading-regexp)
  7534. (push (org-match-string-no-properties 4) rtn)))
  7535. rtn))))
  7536. (defvar org-refile-history nil
  7537. "History for refiling operations.")
  7538. (defvar org-after-refile-insert-hook nil
  7539. "Hook run after `org-refile' has inserted its stuff at the new location.
  7540. Note that this is still *before* the stuff will be removed from
  7541. the *old* location.")
  7542. (defun org-refile (&optional goto default-buffer)
  7543. "Move the entry at point to another heading.
  7544. The list of target headings is compiled using the information in
  7545. `org-refile-targets', which see. This list is created before each use
  7546. and will therefore always be up-to-date.
  7547. At the target location, the entry is filed as a subitem of the target heading.
  7548. Depending on `org-reverse-note-order', the new subitem will either be the
  7549. first or the last subitem.
  7550. If there is an active region, all entries in that region will be moved.
  7551. However, the region must fulfil the requirement that the first heading
  7552. is the first one sets the top-level of the moved text - at most siblings
  7553. below it are allowed.
  7554. With prefix arg GOTO, the command will only visit the target location,
  7555. not actually move anything.
  7556. With a double prefix `C-u C-u', go to the location where the last refiling
  7557. operation has put the subtree.
  7558. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7559. (interactive "P")
  7560. (let* ((cbuf (current-buffer))
  7561. (regionp (org-region-active-p))
  7562. (region-start (and regionp (region-beginning)))
  7563. (region-end (and regionp (region-end)))
  7564. (region-length (and regionp (- region-end region-start)))
  7565. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7566. pos it nbuf file re level reversed)
  7567. (when regionp (goto-char region-start)
  7568. (unless (org-kill-is-subtree-p
  7569. (buffer-substring region-start region-end))
  7570. (error "The region is not a (sequence of) subtree(s)")))
  7571. (if (equal goto '(16))
  7572. (org-refile-goto-last-stored)
  7573. (when (setq it (org-refile-get-location
  7574. (if goto "Goto: " "Refile to: ") default-buffer
  7575. org-refile-allow-creating-parent-nodes))
  7576. (setq file (nth 1 it)
  7577. re (nth 2 it)
  7578. pos (nth 3 it))
  7579. (if (and (not goto)
  7580. pos
  7581. (equal (buffer-file-name) file)
  7582. (if regionp
  7583. (and (>= pos region-start)
  7584. (<= pos region-end))
  7585. (and (>= pos (point))
  7586. (< pos (save-excursion
  7587. (org-end-of-subtree t t))))))
  7588. (error "Cannot refile to position inside the tree or region"))
  7589. (setq nbuf (or (find-buffer-visiting file)
  7590. (find-file-noselect file)))
  7591. (if goto
  7592. (progn
  7593. (switch-to-buffer nbuf)
  7594. (goto-char pos)
  7595. (org-show-context 'org-goto))
  7596. (if regionp
  7597. (progn
  7598. (kill-new (buffer-substring region-start region-end))
  7599. (org-save-markers-in-region region-start region-end))
  7600. (org-copy-subtree 1 nil t))
  7601. (save-excursion
  7602. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7603. (find-file-noselect file))))
  7604. (setq reversed (org-notes-order-reversed-p))
  7605. (save-excursion
  7606. (save-restriction
  7607. (widen)
  7608. (if pos
  7609. (progn
  7610. (goto-char pos)
  7611. (looking-at outline-regexp)
  7612. (setq level (org-get-valid-level (funcall outline-level) 1))
  7613. (goto-char
  7614. (if reversed
  7615. (or (outline-next-heading) (point-max))
  7616. (or (save-excursion (outline-get-next-sibling))
  7617. (org-end-of-subtree t t)
  7618. (point-max)))))
  7619. (setq level 1)
  7620. (if (not reversed)
  7621. (goto-char (point-max))
  7622. (goto-char (point-min))
  7623. (or (outline-next-heading) (goto-char (point-max)))))
  7624. (if (not (bolp)) (newline))
  7625. (bookmark-set "org-refile-last-stored")
  7626. (org-paste-subtree level)
  7627. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7628. (run-hooks 'org-after-refile-insert-hook))))
  7629. (if regionp
  7630. (delete-region (point) (+ (point) region-length))
  7631. (org-cut-subtree))
  7632. (setq org-markers-to-move nil)
  7633. (message "Refiled to \"%s\"" (car it))))))
  7634. (org-reveal))
  7635. (defun org-refile-goto-last-stored ()
  7636. "Go to the location where the last refile was stored."
  7637. (interactive)
  7638. (bookmark-jump "org-refile-last-stored")
  7639. (message "This is the location of the last refile"))
  7640. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7641. "Prompt the user for a refile location, using PROMPT."
  7642. (let ((org-refile-targets org-refile-targets)
  7643. (org-refile-use-outline-path org-refile-use-outline-path))
  7644. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7645. (unless org-refile-target-table
  7646. (error "No refile targets"))
  7647. (let* ((cbuf (current-buffer))
  7648. (partial-completion-mode nil)
  7649. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7650. (cfunc (if (and org-refile-use-outline-path
  7651. org-outline-path-complete-in-steps)
  7652. 'org-olpath-completing-read
  7653. 'org-ido-completing-read))
  7654. (extra (if org-refile-use-outline-path "/" ""))
  7655. (filename (and cfn (expand-file-name cfn)))
  7656. (tbl (mapcar
  7657. (lambda (x)
  7658. (if (and (not (member org-refile-use-outline-path
  7659. '(file full-file-path)))
  7660. (not (equal filename (nth 1 x))))
  7661. (cons (concat (car x) extra " ("
  7662. (file-name-nondirectory (nth 1 x)) ")")
  7663. (cdr x))
  7664. (cons (concat (car x) extra) (cdr x))))
  7665. org-refile-target-table))
  7666. (completion-ignore-case t)
  7667. pa answ parent-target child parent)
  7668. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7669. nil 'org-refile-history))
  7670. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7671. (if pa
  7672. pa
  7673. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7674. (setq parent (match-string 1 answ)
  7675. child (match-string 2 answ))
  7676. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7677. (when (and parent-target
  7678. (or (eq new-nodes t)
  7679. (and (eq new-nodes 'confirm)
  7680. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7681. (org-refile-new-child parent-target child))))))
  7682. (defun org-refile-new-child (parent-target child)
  7683. "Use refile target PARENT-TARGET to add new CHILD below it."
  7684. (unless parent-target
  7685. (error "Cannot find parent for new node"))
  7686. (let ((file (nth 1 parent-target))
  7687. (pos (nth 3 parent-target))
  7688. level)
  7689. (with-current-buffer (or (find-buffer-visiting file)
  7690. (find-file-noselect file))
  7691. (save-excursion
  7692. (save-restriction
  7693. (widen)
  7694. (if pos
  7695. (goto-char pos)
  7696. (goto-char (point-max))
  7697. (if (not (bolp)) (newline)))
  7698. (when (looking-at outline-regexp)
  7699. (setq level (funcall outline-level))
  7700. (org-end-of-subtree t t))
  7701. (org-back-over-empty-lines)
  7702. (insert "\n" (make-string
  7703. (if pos (org-get-valid-level level 1) 1) ?*)
  7704. " " child "\n")
  7705. (beginning-of-line 0)
  7706. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7707. (defun org-olpath-completing-read (prompt collection &rest args)
  7708. "Read an outline path like a file name."
  7709. (let ((thetable collection)
  7710. (org-completion-use-ido nil)) ; does not work with ido.
  7711. (apply
  7712. 'org-ido-completing-read prompt
  7713. (lambda (string predicate &optional flag)
  7714. (let (rtn r f (l (length string)))
  7715. (cond
  7716. ((eq flag nil)
  7717. ;; try completion
  7718. (try-completion string thetable))
  7719. ((eq flag t)
  7720. ;; all-completions
  7721. (setq rtn (all-completions string thetable predicate))
  7722. (mapcar
  7723. (lambda (x)
  7724. (setq r (substring x l))
  7725. (if (string-match " ([^)]*)$" x)
  7726. (setq f (match-string 0 x))
  7727. (setq f ""))
  7728. (if (string-match "/" r)
  7729. (concat string (substring r 0 (match-end 0)) f)
  7730. x))
  7731. rtn))
  7732. ((eq flag 'lambda)
  7733. ;; exact match?
  7734. (assoc string thetable)))
  7735. ))
  7736. args)))
  7737. ;;;; Dynamic blocks
  7738. (defun org-find-dblock (name)
  7739. "Find the first dynamic block with name NAME in the buffer.
  7740. If not found, stay at current position and return nil."
  7741. (let (pos)
  7742. (save-excursion
  7743. (goto-char (point-min))
  7744. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7745. nil t)
  7746. (match-beginning 0))))
  7747. (if pos (goto-char pos))
  7748. pos))
  7749. (defconst org-dblock-start-re
  7750. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7751. "Matches the startline of a dynamic block, with parameters.")
  7752. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7753. "Matches the end of a dynamic block.")
  7754. (defun org-create-dblock (plist)
  7755. "Create a dynamic block section, with parameters taken from PLIST.
  7756. PLIST must contain a :name entry which is used as name of the block."
  7757. (unless (bolp) (newline))
  7758. (let ((name (plist-get plist :name)))
  7759. (insert "#+BEGIN: " name)
  7760. (while plist
  7761. (if (eq (car plist) :name)
  7762. (setq plist (cddr plist))
  7763. (insert " " (prin1-to-string (pop plist)))))
  7764. (insert "\n\n#+END:\n")
  7765. (beginning-of-line -2)))
  7766. (defun org-prepare-dblock ()
  7767. "Prepare dynamic block for refresh.
  7768. This empties the block, puts the cursor at the insert position and returns
  7769. the property list including an extra property :name with the block name."
  7770. (unless (looking-at org-dblock-start-re)
  7771. (error "Not at a dynamic block"))
  7772. (let* ((begdel (1+ (match-end 0)))
  7773. (name (org-no-properties (match-string 1)))
  7774. (params (append (list :name name)
  7775. (read (concat "(" (match-string 3) ")")))))
  7776. (unless (re-search-forward org-dblock-end-re nil t)
  7777. (error "Dynamic block not terminated"))
  7778. (setq params
  7779. (append params
  7780. (list :content (buffer-substring
  7781. begdel (match-beginning 0)))))
  7782. (delete-region begdel (match-beginning 0))
  7783. (goto-char begdel)
  7784. (open-line 1)
  7785. params))
  7786. (defun org-map-dblocks (&optional command)
  7787. "Apply COMMAND to all dynamic blocks in the current buffer.
  7788. If COMMAND is not given, use `org-update-dblock'."
  7789. (let ((cmd (or command 'org-update-dblock))
  7790. pos)
  7791. (save-excursion
  7792. (goto-char (point-min))
  7793. (while (re-search-forward org-dblock-start-re nil t)
  7794. (goto-char (setq pos (match-beginning 0)))
  7795. (condition-case nil
  7796. (funcall cmd)
  7797. (error (message "Error during update of dynamic block")))
  7798. (goto-char pos)
  7799. (unless (re-search-forward org-dblock-end-re nil t)
  7800. (error "Dynamic block not terminated"))))))
  7801. (defun org-dblock-update (&optional arg)
  7802. "User command for updating dynamic blocks.
  7803. Update the dynamic block at point. With prefix ARG, update all dynamic
  7804. blocks in the buffer."
  7805. (interactive "P")
  7806. (if arg
  7807. (org-update-all-dblocks)
  7808. (or (looking-at org-dblock-start-re)
  7809. (org-beginning-of-dblock))
  7810. (org-update-dblock)))
  7811. (defun org-update-dblock ()
  7812. "Update the dynamic block at point
  7813. This means to empty the block, parse for parameters and then call
  7814. the correct writing function."
  7815. (save-window-excursion
  7816. (let* ((pos (point))
  7817. (line (org-current-line))
  7818. (params (org-prepare-dblock))
  7819. (name (plist-get params :name))
  7820. (cmd (intern (concat "org-dblock-write:" name))))
  7821. (message "Updating dynamic block `%s' at line %d..." name line)
  7822. (funcall cmd params)
  7823. (message "Updating dynamic block `%s' at line %d...done" name line)
  7824. (goto-char pos))))
  7825. (defun org-beginning-of-dblock ()
  7826. "Find the beginning of the dynamic block at point.
  7827. Error if there is no such block at point."
  7828. (let ((pos (point))
  7829. beg)
  7830. (end-of-line 1)
  7831. (if (and (re-search-backward org-dblock-start-re nil t)
  7832. (setq beg (match-beginning 0))
  7833. (re-search-forward org-dblock-end-re nil t)
  7834. (> (match-end 0) pos))
  7835. (goto-char beg)
  7836. (goto-char pos)
  7837. (error "Not in a dynamic block"))))
  7838. (defun org-update-all-dblocks ()
  7839. "Update all dynamic blocks in the buffer.
  7840. This function can be used in a hook."
  7841. (when (org-mode-p)
  7842. (org-map-dblocks 'org-update-dblock)))
  7843. ;;;; Completion
  7844. (defconst org-additional-option-like-keywords
  7845. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7846. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7847. "LATEX_HEADER:"
  7848. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7849. "BEGIN_QUOTE" "END_QUOTE"
  7850. "BEGIN_VERSE" "END_VERSE"
  7851. "BEGIN_CENTER" "END_CENTER"
  7852. "BEGIN_SRC" "END_SRC"
  7853. "CATEGORY" "COLUMNS"
  7854. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7855. (defcustom org-structure-template-alist
  7856. '(
  7857. ("s" "#+begin_src ?\n\n#+end_src"
  7858. "<src lang=\"?\">\n\n</src>")
  7859. ("e" "#+begin_example\n?\n#+end_example"
  7860. "<example>\n?\n</example>")
  7861. ("q" "#+begin_quote\n?\n#+end_quote"
  7862. "<quote>\n?\n</quote>")
  7863. ("v" "#+begin_verse\n?\n#+end_verse"
  7864. "<verse>\n?\n/verse>")
  7865. ("c" "#+begin_center\n?\n#+end_center"
  7866. "<center>\n?\n/center>")
  7867. ("l" "#+begin_latex\n?\n#+end_latex"
  7868. "<literal style=\"latex\">\n?\n</literal>")
  7869. ("L" "#+latex: "
  7870. "<literal style=\"latex\">?</literal>")
  7871. ("h" "#+begin_html\n?\n#+end_html"
  7872. "<literal style=\"html\">\n?\n</literal>")
  7873. ("H" "#+html: "
  7874. "<literal style=\"html\">?</literal>")
  7875. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7876. ("A" "#+ascii: ")
  7877. ("i" "#+include %file ?"
  7878. "<include file=%file markup=\"?\">")
  7879. )
  7880. "Structure completion elements.
  7881. This is a list of abbreviation keys and values. The value gets inserted
  7882. it you type @samp{.} followed by the key and then the completion key,
  7883. usually `M-TAB'. %file will be replaced by a file name after prompting
  7884. for the file using completion.
  7885. There are two templates for each key, the first uses the original Org syntax,
  7886. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7887. the default when the /org-mtags.el/ module has been loaded. See also the
  7888. variable `org-mtags-prefer-muse-templates'.
  7889. This is an experimental feature, it is undecided if it is going to stay in."
  7890. :group 'org-completion
  7891. :type '(repeat
  7892. (string :tag "Key")
  7893. (string :tag "Template")
  7894. (string :tag "Muse Template")))
  7895. (defun org-try-structure-completion ()
  7896. "Try to complete a structure template before point.
  7897. This looks for strings like \"<e\" on an otherwise empty line and
  7898. expands them."
  7899. (let ((l (buffer-substring (point-at-bol) (point)))
  7900. a)
  7901. (when (and (looking-at "[ \t]*$")
  7902. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7903. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7904. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7905. (match-beginning 1)) a)
  7906. t)))
  7907. (defun org-complete-expand-structure-template (start cell)
  7908. "Expand a structure template."
  7909. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7910. (rpl (nth (if musep 2 1) cell))
  7911. (ind ""))
  7912. (delete-region start (point))
  7913. (when (string-match "\\`#\\+" rpl)
  7914. (cond
  7915. ((bolp))
  7916. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7917. (setq ind (buffer-substring (point-at-bol) (point))))
  7918. (t (newline))))
  7919. (setq start (point))
  7920. (if (string-match "%file" rpl)
  7921. (setq rpl (replace-match
  7922. (concat
  7923. "\""
  7924. (save-match-data
  7925. (abbreviate-file-name (read-file-name "Include file: ")))
  7926. "\"")
  7927. t t rpl)))
  7928. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  7929. (concat "\n" ind)))
  7930. (insert rpl)
  7931. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7932. (defun org-complete (&optional arg)
  7933. "Perform completion on word at point.
  7934. At the beginning of a headline, this completes TODO keywords as given in
  7935. `org-todo-keywords'.
  7936. If the current word is preceded by a backslash, completes the TeX symbols
  7937. that are supported for HTML support.
  7938. If the current word is preceded by \"#+\", completes special words for
  7939. setting file options.
  7940. In the line after \"#+STARTUP:, complete valid keywords.\"
  7941. At all other locations, this simply calls the value of
  7942. `org-completion-fallback-command'."
  7943. (interactive "P")
  7944. (org-without-partial-completion
  7945. (catch 'exit
  7946. (let* ((a nil)
  7947. (end (point))
  7948. (beg1 (save-excursion
  7949. (skip-chars-backward (org-re "[:alnum:]_@"))
  7950. (point)))
  7951. (beg (save-excursion
  7952. (skip-chars-backward "a-zA-Z0-9_:$")
  7953. (point)))
  7954. (confirm (lambda (x) (stringp (car x))))
  7955. (searchhead (equal (char-before beg) ?*))
  7956. (struct
  7957. (when (and (member (char-before beg1) '(?. ?<))
  7958. (setq a (assoc (buffer-substring beg1 (point))
  7959. org-structure-template-alist)))
  7960. (org-complete-expand-structure-template (1- beg1) a)
  7961. (throw 'exit t)))
  7962. (tag (and (equal (char-before beg1) ?:)
  7963. (equal (char-after (point-at-bol)) ?*)))
  7964. (prop (and (equal (char-before beg1) ?:)
  7965. (not (equal (char-after (point-at-bol)) ?*))))
  7966. (texp (equal (char-before beg) ?\\))
  7967. (link (equal (char-before beg) ?\[))
  7968. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7969. beg)
  7970. "#+"))
  7971. (startup (string-match "^#\\+STARTUP:.*"
  7972. (buffer-substring (point-at-bol) (point))))
  7973. (completion-ignore-case opt)
  7974. (type nil)
  7975. (tbl nil)
  7976. (table (cond
  7977. (opt
  7978. (setq type :opt)
  7979. (require 'org-exp)
  7980. (append
  7981. (mapcar
  7982. (lambda (x)
  7983. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7984. (cons (match-string 2 x) (match-string 1 x)))
  7985. (org-split-string (org-get-current-options) "\n"))
  7986. (mapcar 'list org-additional-option-like-keywords)))
  7987. (startup
  7988. (setq type :startup)
  7989. org-startup-options)
  7990. (link (append org-link-abbrev-alist-local
  7991. org-link-abbrev-alist))
  7992. (texp
  7993. (setq type :tex)
  7994. org-html-entities)
  7995. ((string-match "\\`\\*+[ \t]+\\'"
  7996. (buffer-substring (point-at-bol) beg))
  7997. (setq type :todo)
  7998. (mapcar 'list org-todo-keywords-1))
  7999. (searchhead
  8000. (setq type :searchhead)
  8001. (save-excursion
  8002. (goto-char (point-min))
  8003. (while (re-search-forward org-todo-line-regexp nil t)
  8004. (push (list
  8005. (org-make-org-heading-search-string
  8006. (match-string 3) t))
  8007. tbl)))
  8008. tbl)
  8009. (tag (setq type :tag beg beg1)
  8010. (or org-tag-alist (org-get-buffer-tags)))
  8011. (prop (setq type :prop beg beg1)
  8012. (mapcar 'list (org-buffer-property-keys nil t t)))
  8013. (t (progn
  8014. (call-interactively org-completion-fallback-command)
  8015. (throw 'exit nil)))))
  8016. (pattern (buffer-substring-no-properties beg end))
  8017. (completion (try-completion pattern table confirm)))
  8018. (cond ((eq completion t)
  8019. (if (not (assoc (upcase pattern) table))
  8020. (message "Already complete")
  8021. (if (and (equal type :opt)
  8022. (not (member (car (assoc (upcase pattern) table))
  8023. org-additional-option-like-keywords)))
  8024. (insert (substring (cdr (assoc (upcase pattern) table))
  8025. (length pattern)))
  8026. (if (memq type '(:tag :prop)) (insert ":")))))
  8027. ((null completion)
  8028. (message "Can't find completion for \"%s\"" pattern)
  8029. (ding))
  8030. ((not (string= pattern completion))
  8031. (delete-region beg end)
  8032. (if (string-match " +$" completion)
  8033. (setq completion (replace-match "" t t completion)))
  8034. (insert completion)
  8035. (if (get-buffer-window "*Completions*")
  8036. (delete-window (get-buffer-window "*Completions*")))
  8037. (if (assoc completion table)
  8038. (if (eq type :todo) (insert " ")
  8039. (if (memq type '(:tag :prop)) (insert ":"))))
  8040. (if (and (equal type :opt) (assoc completion table))
  8041. (message "%s" (substitute-command-keys
  8042. "Press \\[org-complete] again to insert example settings"))))
  8043. (t
  8044. (message "Making completion list...")
  8045. (let ((list (sort (all-completions pattern table confirm)
  8046. 'string<)))
  8047. (with-output-to-temp-buffer "*Completions*"
  8048. (condition-case nil
  8049. ;; Protection needed for XEmacs and emacs 21
  8050. (display-completion-list list pattern)
  8051. (error (display-completion-list list)))))
  8052. (message "Making completion list...%s" "done")))))))
  8053. ;;;; TODO, DEADLINE, Comments
  8054. (defun org-toggle-comment ()
  8055. "Change the COMMENT state of an entry."
  8056. (interactive)
  8057. (save-excursion
  8058. (org-back-to-heading)
  8059. (let (case-fold-search)
  8060. (if (looking-at (concat outline-regexp
  8061. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8062. (replace-match "" t t nil 1)
  8063. (if (looking-at outline-regexp)
  8064. (progn
  8065. (goto-char (match-end 0))
  8066. (insert org-comment-string " ")))))))
  8067. (defvar org-last-todo-state-is-todo nil
  8068. "This is non-nil when the last TODO state change led to a TODO state.
  8069. If the last change removed the TODO tag or switched to DONE, then
  8070. this is nil.")
  8071. (defvar org-setting-tags nil) ; dynamically skipped
  8072. (defun org-parse-local-options (string var)
  8073. "Parse STRING for startup setting relevant for variable VAR."
  8074. (let ((rtn (symbol-value var))
  8075. e opts)
  8076. (save-match-data
  8077. (if (or (not string) (not (string-match "\\S-" string)))
  8078. rtn
  8079. (setq opts (delq nil (mapcar (lambda (x)
  8080. (setq e (assoc x org-startup-options))
  8081. (if (eq (nth 1 e) var) e nil))
  8082. (org-split-string string "[ \t]+"))))
  8083. (if (not opts)
  8084. rtn
  8085. (setq rtn nil)
  8086. (while (setq e (pop opts))
  8087. (if (not (nth 3 e))
  8088. (setq rtn (nth 2 e))
  8089. (if (not (listp rtn)) (setq rtn nil))
  8090. (push (nth 2 e) rtn)))
  8091. rtn)))))
  8092. (defvar org-todo-setup-filter-hook nil
  8093. "Hook for functions that pre-filter todo specs.
  8094. Each function takes a todo spec and returns either `nil' or the spec
  8095. transformed into canonical form." )
  8096. (defvar org-todo-get-default-hook nil
  8097. "Hook for functions that get a default item for todo.
  8098. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8099. `nil' or a string to be used for the todo mark." )
  8100. (defvar org-agenda-headline-snapshot-before-repeat)
  8101. (defun org-todo (&optional arg)
  8102. "Change the TODO state of an item.
  8103. The state of an item is given by a keyword at the start of the heading,
  8104. like
  8105. *** TODO Write paper
  8106. *** DONE Call mom
  8107. The different keywords are specified in the variable `org-todo-keywords'.
  8108. By default the available states are \"TODO\" and \"DONE\".
  8109. So for this example: when the item starts with TODO, it is changed to DONE.
  8110. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8111. DONE are present, add TODO at the beginning of the heading.
  8112. With C-u prefix arg, use completion to determine the new state.
  8113. With numeric prefix arg, switch to that state.
  8114. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8115. With a tripple C-u prefix, circumvent any state blocking.
  8116. For calling through lisp, arg is also interpreted in the following way:
  8117. 'none -> empty state
  8118. \"\"(empty string) -> switch to empty state
  8119. 'done -> switch to DONE
  8120. 'nextset -> switch to the next set of keywords
  8121. 'previousset -> switch to the previous set of keywords
  8122. \"WAITING\" -> switch to the specified keyword, but only if it
  8123. really is a member of `org-todo-keywords'."
  8124. (interactive "P")
  8125. (if (equal arg '(16)) (setq arg 'nextset))
  8126. (let ((org-blocker-hook org-blocker-hook)
  8127. (case-fold-search nil))
  8128. (when (equal arg '(64))
  8129. (setq arg nil org-blocker-hook nil))
  8130. (when (and org-blocker-hook
  8131. (or org-inhibit-blocking
  8132. (org-entry-get nil "NOBLOCKING")))
  8133. (setq org-blocker-hook nil))
  8134. (save-excursion
  8135. (catch 'exit
  8136. (org-back-to-heading)
  8137. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8138. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8139. (looking-at " *"))
  8140. (let* ((match-data (match-data))
  8141. (startpos (point-at-bol))
  8142. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8143. (org-log-done org-log-done)
  8144. (org-log-repeat org-log-repeat)
  8145. (org-todo-log-states org-todo-log-states)
  8146. (this (match-string 1))
  8147. (hl-pos (match-beginning 0))
  8148. (head (org-get-todo-sequence-head this))
  8149. (ass (assoc head org-todo-kwd-alist))
  8150. (interpret (nth 1 ass))
  8151. (done-word (nth 3 ass))
  8152. (final-done-word (nth 4 ass))
  8153. (last-state (or this ""))
  8154. (completion-ignore-case t)
  8155. (member (member this org-todo-keywords-1))
  8156. (tail (cdr member))
  8157. (state (cond
  8158. ((and org-todo-key-trigger
  8159. (or (and (equal arg '(4))
  8160. (eq org-use-fast-todo-selection 'prefix))
  8161. (and (not arg) org-use-fast-todo-selection
  8162. (not (eq org-use-fast-todo-selection
  8163. 'prefix)))))
  8164. ;; Use fast selection
  8165. (org-fast-todo-selection))
  8166. ((and (equal arg '(4))
  8167. (or (not org-use-fast-todo-selection)
  8168. (not org-todo-key-trigger)))
  8169. ;; Read a state with completion
  8170. (org-ido-completing-read
  8171. "State: " (mapcar (lambda(x) (list x))
  8172. org-todo-keywords-1)
  8173. nil t))
  8174. ((eq arg 'right)
  8175. (if this
  8176. (if tail (car tail) nil)
  8177. (car org-todo-keywords-1)))
  8178. ((eq arg 'left)
  8179. (if (equal member org-todo-keywords-1)
  8180. nil
  8181. (if this
  8182. (nth (- (length org-todo-keywords-1)
  8183. (length tail) 2)
  8184. org-todo-keywords-1)
  8185. (org-last org-todo-keywords-1))))
  8186. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8187. (setq arg nil))) ; hack to fall back to cycling
  8188. (arg
  8189. ;; user or caller requests a specific state
  8190. (cond
  8191. ((equal arg "") nil)
  8192. ((eq arg 'none) nil)
  8193. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8194. ((eq arg 'nextset)
  8195. (or (car (cdr (member head org-todo-heads)))
  8196. (car org-todo-heads)))
  8197. ((eq arg 'previousset)
  8198. (let ((org-todo-heads (reverse org-todo-heads)))
  8199. (or (car (cdr (member head org-todo-heads)))
  8200. (car org-todo-heads))))
  8201. ((car (member arg org-todo-keywords-1)))
  8202. ((nth (1- (prefix-numeric-value arg))
  8203. org-todo-keywords-1))))
  8204. ((null member) (or head (car org-todo-keywords-1)))
  8205. ((equal this final-done-word) nil) ;; -> make empty
  8206. ((null tail) nil) ;; -> first entry
  8207. ((memq interpret '(type priority))
  8208. (if (eq this-command last-command)
  8209. (car tail)
  8210. (if (> (length tail) 0)
  8211. (or done-word (car org-done-keywords))
  8212. nil)))
  8213. (t
  8214. (car tail))))
  8215. (state (or
  8216. (run-hook-with-args-until-success
  8217. 'org-todo-get-default-hook state last-state)
  8218. state))
  8219. (next (if state (concat " " state " ") " "))
  8220. (change-plist (list :type 'todo-state-change :from this :to state
  8221. :position startpos))
  8222. dolog now-done-p)
  8223. (when org-blocker-hook
  8224. (setq org-last-todo-state-is-todo
  8225. (not (member this org-done-keywords)))
  8226. (unless (save-excursion
  8227. (save-match-data
  8228. (run-hook-with-args-until-failure
  8229. 'org-blocker-hook change-plist)))
  8230. (if (interactive-p)
  8231. (error "TODO state change from %s to %s blocked" this state)
  8232. ;; fail silently
  8233. (message "TODO state change from %s to %s blocked" this state)
  8234. (throw 'exit nil))))
  8235. (store-match-data match-data)
  8236. (replace-match next t t)
  8237. (unless (pos-visible-in-window-p hl-pos)
  8238. (message "TODO state changed to %s" (org-trim next)))
  8239. (unless head
  8240. (setq head (org-get-todo-sequence-head state)
  8241. ass (assoc head org-todo-kwd-alist)
  8242. interpret (nth 1 ass)
  8243. done-word (nth 3 ass)
  8244. final-done-word (nth 4 ass)))
  8245. (when (memq arg '(nextset previousset))
  8246. (message "Keyword-Set %d/%d: %s"
  8247. (- (length org-todo-sets) -1
  8248. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8249. (length org-todo-sets)
  8250. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8251. (setq org-last-todo-state-is-todo
  8252. (not (member state org-done-keywords)))
  8253. (setq now-done-p (and (member state org-done-keywords)
  8254. (not (member this org-done-keywords))))
  8255. (and logging (org-local-logging logging))
  8256. (when (and (or org-todo-log-states org-log-done)
  8257. (not org-inhibit-logging)
  8258. (not (memq arg '(nextset previousset))))
  8259. ;; we need to look at recording a time and note
  8260. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8261. (nth 2 (assoc this org-todo-log-states))))
  8262. (when (and state
  8263. (member state org-not-done-keywords)
  8264. (not (member this org-not-done-keywords)))
  8265. ;; This is now a todo state and was not one before
  8266. ;; If there was a CLOSED time stamp, get rid of it.
  8267. (org-add-planning-info nil nil 'closed))
  8268. (when (and now-done-p org-log-done)
  8269. ;; It is now done, and it was not done before
  8270. (org-add-planning-info 'closed (org-current-time))
  8271. (if (and (not dolog) (eq 'note org-log-done))
  8272. (org-add-log-setup 'done state this 'findpos 'note)))
  8273. (when (and state dolog)
  8274. ;; This is a non-nil state, and we need to log it
  8275. (org-add-log-setup 'state state this 'findpos dolog)))
  8276. ;; Fixup tag positioning
  8277. (org-todo-trigger-tag-changes state)
  8278. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8279. (when org-provide-todo-statistics
  8280. (org-update-parent-todo-statistics))
  8281. (run-hooks 'org-after-todo-state-change-hook)
  8282. (if (and arg (not (member state org-done-keywords)))
  8283. (setq head (org-get-todo-sequence-head state)))
  8284. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8285. ;; Do we need to trigger a repeat?
  8286. (when now-done-p
  8287. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8288. ;; This is for the agenda, take a snapshot of the headline.
  8289. (save-match-data
  8290. (setq org-agenda-headline-snapshot-before-repeat
  8291. (org-get-heading))))
  8292. (org-auto-repeat-maybe state))
  8293. ;; Fixup cursor location if close to the keyword
  8294. (if (and (outline-on-heading-p)
  8295. (not (bolp))
  8296. (save-excursion (beginning-of-line 1)
  8297. (looking-at org-todo-line-regexp))
  8298. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8299. (progn
  8300. (goto-char (or (match-end 2) (match-end 1)))
  8301. (and (looking-at " ") (just-one-space))))
  8302. (when org-trigger-hook
  8303. (save-excursion
  8304. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8305. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8306. "Block turning an entry into a TODO, using the hierarchy.
  8307. This checks whether the current task should be blocked from state
  8308. changes. Such blocking occurs when:
  8309. 1. The task has children which are not all in a completed state.
  8310. 2. A task has a parent with the property :ORDERED:, and there
  8311. are siblings prior to the current task with incomplete
  8312. status.
  8313. 3. The parent of the task is blocked because it has siblings that should
  8314. be done first, or is child of a block grandparent TODO entry."
  8315. (catch 'dont-block
  8316. ;; If this is not a todo state change, or if this entry is already DONE,
  8317. ;; do not block
  8318. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8319. (member (plist-get change-plist :from)
  8320. (cons 'done org-done-keywords))
  8321. (member (plist-get change-plist :to)
  8322. (cons 'todo org-not-done-keywords)))
  8323. (throw 'dont-block t))
  8324. ;; If this task has children, and any are undone, it's blocked
  8325. (save-excursion
  8326. (org-back-to-heading t)
  8327. (let ((this-level (funcall outline-level)))
  8328. (outline-next-heading)
  8329. (let ((child-level (funcall outline-level)))
  8330. (while (and (not (eobp))
  8331. (> child-level this-level))
  8332. ;; this todo has children, check whether they are all
  8333. ;; completed
  8334. (if (and (not (org-entry-is-done-p))
  8335. (org-entry-is-todo-p))
  8336. (throw 'dont-block nil))
  8337. (outline-next-heading)
  8338. (setq child-level (funcall outline-level))))))
  8339. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8340. ;; any previous siblings are undone, it's blocked
  8341. (save-excursion
  8342. (org-back-to-heading t)
  8343. (let* ((pos (point))
  8344. (parent-pos (and (org-up-heading-safe) (point))))
  8345. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8346. (when (and (org-entry-get (point) "ORDERED")
  8347. (forward-line 1)
  8348. (re-search-forward org-not-done-heading-regexp pos t))
  8349. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8350. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8351. (while t
  8352. (goto-char parent-pos)
  8353. (if (not (looking-at org-not-done-heading-regexp))
  8354. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8355. (setq pos (point))
  8356. (setq parent-pos (and (org-up-heading-safe) (point)))
  8357. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8358. (when (and (org-entry-get (point) "ORDERED")
  8359. (forward-line 1)
  8360. (re-search-forward org-not-done-heading-regexp pos t))
  8361. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8362. (defcustom org-track-ordered-property-with-tag nil
  8363. "Should the ORDERED property also be shown as a tag?
  8364. The ORDERED property decides if an entry should require subtasks to be
  8365. completed in sequence. Since a property is not very visible, setting
  8366. this option means that toggling the ORDERED property with the command
  8367. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8368. not relevant for the behavior, but it makes things more visible.
  8369. Note that toggling the tag with tags commands will not change the property
  8370. and therefore not influence behavior!
  8371. This can be t, meaning the tag ORDERED should be used, It can also be a
  8372. string to select a different tag for this task."
  8373. :group 'org-todo
  8374. :type '(choice
  8375. (const :tag "No tracking" nil)
  8376. (const :tag "Track with ORDERED tag" t)
  8377. (string :tag "Use other tag")))
  8378. (defun org-toggle-ordered-property ()
  8379. "Toggle the ORDERED property of the current entry.
  8380. For better visibility, you can track the value of this property with a tag.
  8381. See variable `org-track-ordered-property-with-tag'."
  8382. (interactive)
  8383. (let* ((t1 org-track-ordered-property-with-tag)
  8384. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8385. (save-excursion
  8386. (org-back-to-heading)
  8387. (if (org-entry-get nil "ORDERED")
  8388. (progn
  8389. (org-delete-property "ORDERED")
  8390. (and tag (org-toggle-tag tag 'off))
  8391. (message "Subtasks can be completed in arbitrary order"))
  8392. (org-entry-put nil "ORDERED" "t")
  8393. (and tag (org-toggle-tag tag 'on))
  8394. (message "Subtasks must be completed in sequence")))))
  8395. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8396. (defun org-block-todo-from-checkboxes (change-plist)
  8397. "Block turning an entry into a TODO, using checkboxes.
  8398. This checks whether the current task should be blocked from state
  8399. changes because there are uncheckd boxes in this entry."
  8400. (catch 'dont-block
  8401. ;; If this is not a todo state change, or if this entry is already DONE,
  8402. ;; do not block
  8403. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8404. (member (plist-get change-plist :from)
  8405. (cons 'done org-done-keywords))
  8406. (member (plist-get change-plist :to)
  8407. (cons 'todo org-not-done-keywords)))
  8408. (throw 'dont-block t))
  8409. ;; If this task has checkboxes that are not checked, it's blocked
  8410. (save-excursion
  8411. (org-back-to-heading t)
  8412. (let ((beg (point)) end)
  8413. (outline-next-heading)
  8414. (setq end (point))
  8415. (goto-char beg)
  8416. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8417. end t)
  8418. (progn
  8419. (if (boundp 'org-blocked-by-checkboxes)
  8420. (setq org-blocked-by-checkboxes t))
  8421. (throw 'dont-block nil)))))
  8422. t)) ; do not block
  8423. (defvar org-entry-property-inherited-from) ;; defined below
  8424. (defun org-update-parent-todo-statistics ()
  8425. "Update any statistics cookie in the parent of the current headline.
  8426. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8427. the entire subtree and this will travel up the hierarchy and update
  8428. statistics everywhere."
  8429. (interactive)
  8430. (let* ((lim 0) prop
  8431. (recursive (or (not org-hierarchical-todo-statistics)
  8432. (string-match
  8433. "\\<recursive\\>"
  8434. (or (setq prop (org-entry-get
  8435. nil "COOKIE_DATA" 'inherit)) ""))))
  8436. (lim (or (and prop (marker-position
  8437. org-entry-property-inherited-from))
  8438. lim))
  8439. (first t)
  8440. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8441. level ltoggle l1
  8442. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8443. (catch 'exit
  8444. (save-excursion
  8445. (beginning-of-line 1)
  8446. (if (org-at-heading-p)
  8447. (setq ltoggle (funcall outline-level))
  8448. (error "This should not happen"))
  8449. (while (and (setq level (org-up-heading-safe))
  8450. (or recursive first)
  8451. (>= (point) lim))
  8452. (setq first nil)
  8453. (unless (and level
  8454. (not (string-match
  8455. "\\<checkbox\\>"
  8456. (downcase
  8457. (or (org-entry-get
  8458. nil "COOKIE_DATA")
  8459. "")))))
  8460. (throw 'exit nil))
  8461. (while (re-search-forward box-re (point-at-eol) t)
  8462. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8463. (setq is-percent (match-end 2))
  8464. (save-match-data
  8465. (unless (outline-next-heading) (throw 'exit nil))
  8466. (while (and (looking-at org-complex-heading-regexp)
  8467. (> (setq l1 (length (match-string 1))) level))
  8468. (setq kwd (and (or recursive (= l1 ltoggle))
  8469. (match-string 2)))
  8470. (and kwd (setq cnt-all (1+ cnt-all)))
  8471. (and (member kwd org-done-keywords)
  8472. (setq cnt-done (1+ cnt-done)))
  8473. (outline-next-heading)))
  8474. (replace-match
  8475. (if is-percent
  8476. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8477. (format "[%d/%d]" cnt-done cnt-all)))))
  8478. (when cookie-present
  8479. (run-hook-with-args 'org-after-todo-statistics-hook
  8480. cnt-done (- cnt-all cnt-done)))))
  8481. (run-hooks 'org-todo-statistics-hook)))
  8482. (defvar org-after-todo-statistics-hook nil
  8483. "Hook that is called after a TODO statistics cookie has been updated.
  8484. Each function is called with two arguments: the number of not-done entries
  8485. and the number of done entries.
  8486. For example, the following function, when added to this hook, will switch
  8487. an entry to DONE when all children are done, and back to TODO when new
  8488. entries are set to a TODO status. Note that this hook is only called
  8489. when there is a statistics cookie in the headline!
  8490. (defun org-summary-todo (n-done n-not-done)
  8491. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8492. (let (org-log-done org-log-states) ; turn off logging
  8493. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8494. ")
  8495. (defvar org-todo-statistics-hook nil
  8496. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8497. This hook runs even if there is no statisics cookie present, in which case
  8498. `org-after-todo-statistics-hook' would not run.")
  8499. (defun org-todo-trigger-tag-changes (state)
  8500. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8501. (let ((l org-todo-state-tags-triggers)
  8502. changes)
  8503. (when (or (not state) (equal state ""))
  8504. (setq changes (append changes (cdr (assoc "" l)))))
  8505. (when (and (stringp state) (> (length state) 0))
  8506. (setq changes (append changes (cdr (assoc state l)))))
  8507. (when (member state org-not-done-keywords)
  8508. (setq changes (append changes (cdr (assoc 'todo l)))))
  8509. (when (member state org-done-keywords)
  8510. (setq changes (append changes (cdr (assoc 'done l)))))
  8511. (dolist (c changes)
  8512. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8513. (defun org-local-logging (value)
  8514. "Get logging settings from a property VALUE."
  8515. (let* (words w a)
  8516. ;; directly set the variables, they are already local.
  8517. (setq org-log-done nil
  8518. org-log-repeat nil
  8519. org-todo-log-states nil)
  8520. (setq words (org-split-string value))
  8521. (while (setq w (pop words))
  8522. (cond
  8523. ((setq a (assoc w org-startup-options))
  8524. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8525. (set (nth 1 a) (nth 2 a))))
  8526. ((setq a (org-extract-log-state-settings w))
  8527. (and (member (car a) org-todo-keywords-1)
  8528. (push a org-todo-log-states)))))))
  8529. (defun org-get-todo-sequence-head (kwd)
  8530. "Return the head of the TODO sequence to which KWD belongs.
  8531. If KWD is not set, check if there is a text property remembering the
  8532. right sequence."
  8533. (let (p)
  8534. (cond
  8535. ((not kwd)
  8536. (or (get-text-property (point-at-bol) 'org-todo-head)
  8537. (progn
  8538. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8539. nil (point-at-eol)))
  8540. (get-text-property p 'org-todo-head))))
  8541. ((not (member kwd org-todo-keywords-1))
  8542. (car org-todo-keywords-1))
  8543. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8544. (defun org-fast-todo-selection ()
  8545. "Fast TODO keyword selection with single keys.
  8546. Returns the new TODO keyword, or nil if no state change should occur."
  8547. (let* ((fulltable org-todo-key-alist)
  8548. (done-keywords org-done-keywords) ;; needed for the faces.
  8549. (maxlen (apply 'max (mapcar
  8550. (lambda (x)
  8551. (if (stringp (car x)) (string-width (car x)) 0))
  8552. fulltable)))
  8553. (expert nil)
  8554. (fwidth (+ maxlen 3 1 3))
  8555. (ncol (/ (- (window-width) 4) fwidth))
  8556. tg cnt e c tbl
  8557. groups ingroup)
  8558. (save-excursion
  8559. (save-window-excursion
  8560. (if expert
  8561. (set-buffer (get-buffer-create " *Org todo*"))
  8562. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8563. (erase-buffer)
  8564. (org-set-local 'org-done-keywords done-keywords)
  8565. (setq tbl fulltable cnt 0)
  8566. (while (setq e (pop tbl))
  8567. (cond
  8568. ((equal e '(:startgroup))
  8569. (push '() groups) (setq ingroup t)
  8570. (when (not (= cnt 0))
  8571. (setq cnt 0)
  8572. (insert "\n"))
  8573. (insert "{ "))
  8574. ((equal e '(:endgroup))
  8575. (setq ingroup nil cnt 0)
  8576. (insert "}\n"))
  8577. ((equal e '(:newline))
  8578. (when (not (= cnt 0))
  8579. (setq cnt 0)
  8580. (insert "\n")
  8581. (setq e (car tbl))
  8582. (while (equal (car tbl) '(:newline))
  8583. (insert "\n")
  8584. (setq tbl (cdr tbl)))))
  8585. (t
  8586. (setq tg (car e) c (cdr e))
  8587. (if ingroup (push tg (car groups)))
  8588. (setq tg (org-add-props tg nil 'face
  8589. (org-get-todo-face tg)))
  8590. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8591. (insert "[" c "] " tg (make-string
  8592. (- fwidth 4 (length tg)) ?\ ))
  8593. (when (= (setq cnt (1+ cnt)) ncol)
  8594. (insert "\n")
  8595. (if ingroup (insert " "))
  8596. (setq cnt 0)))))
  8597. (insert "\n")
  8598. (goto-char (point-min))
  8599. (if (not expert) (org-fit-window-to-buffer))
  8600. (message "[a-z..]:Set [SPC]:clear")
  8601. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8602. (cond
  8603. ((or (= c ?\C-g)
  8604. (and (= c ?q) (not (rassoc c fulltable))))
  8605. (setq quit-flag t))
  8606. ((= c ?\ ) nil)
  8607. ((setq e (rassoc c fulltable) tg (car e))
  8608. tg)
  8609. (t (setq quit-flag t)))))))
  8610. (defun org-entry-is-todo-p ()
  8611. (member (org-get-todo-state) org-not-done-keywords))
  8612. (defun org-entry-is-done-p ()
  8613. (member (org-get-todo-state) org-done-keywords))
  8614. (defun org-get-todo-state ()
  8615. (save-excursion
  8616. (org-back-to-heading t)
  8617. (and (looking-at org-todo-line-regexp)
  8618. (match-end 2)
  8619. (match-string 2))))
  8620. (defun org-at-date-range-p (&optional inactive-ok)
  8621. "Is the cursor inside a date range?"
  8622. (interactive)
  8623. (save-excursion
  8624. (catch 'exit
  8625. (let ((pos (point)))
  8626. (skip-chars-backward "^[<\r\n")
  8627. (skip-chars-backward "<[")
  8628. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8629. (>= (match-end 0) pos)
  8630. (throw 'exit t))
  8631. (skip-chars-backward "^<[\r\n")
  8632. (skip-chars-backward "<[")
  8633. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8634. (>= (match-end 0) pos)
  8635. (throw 'exit t)))
  8636. nil)))
  8637. (defun org-get-repeat ()
  8638. "Check if there is a deadline/schedule with repeater in this entry."
  8639. (save-match-data
  8640. (save-excursion
  8641. (org-back-to-heading t)
  8642. (if (re-search-forward
  8643. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8644. (match-string 1)))))
  8645. (defvar org-last-changed-timestamp)
  8646. (defvar org-last-inserted-timestamp)
  8647. (defvar org-log-post-message)
  8648. (defvar org-log-note-purpose)
  8649. (defvar org-log-note-how)
  8650. (defvar org-log-note-extra)
  8651. (defun org-auto-repeat-maybe (done-word)
  8652. "Check if the current headline contains a repeated deadline/schedule.
  8653. If yes, set TODO state back to what it was and change the base date
  8654. of repeating deadline/scheduled time stamps to new date.
  8655. This function is run automatically after each state change to a DONE state."
  8656. ;; last-state is dynamically scoped into this function
  8657. (let* ((repeat (org-get-repeat))
  8658. (aa (assoc last-state org-todo-kwd-alist))
  8659. (interpret (nth 1 aa))
  8660. (head (nth 2 aa))
  8661. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8662. (msg "Entry repeats: ")
  8663. (org-log-done nil)
  8664. (org-todo-log-states nil)
  8665. (nshiftmax 10) (nshift 0)
  8666. re type n what ts time)
  8667. (when repeat
  8668. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8669. (org-todo (if (eq interpret 'type) last-state head))
  8670. (org-entry-put nil "LAST_REPEAT" (format-time-string
  8671. (org-time-stamp-format t t)))
  8672. (when org-log-repeat
  8673. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8674. (memq 'org-add-log-note post-command-hook))
  8675. ;; OK, we are already setup for some record
  8676. (if (eq org-log-repeat 'note)
  8677. ;; make sure we take a note, not only a time stamp
  8678. (setq org-log-note-how 'note))
  8679. ;; Set up for taking a record
  8680. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8681. last-state
  8682. 'findpos org-log-repeat)))
  8683. (org-back-to-heading t)
  8684. (org-add-planning-info nil nil 'closed)
  8685. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8686. org-deadline-time-regexp "\\)\\|\\("
  8687. org-ts-regexp "\\)"))
  8688. (while (re-search-forward
  8689. re (save-excursion (outline-next-heading) (point)) t)
  8690. (setq type (if (match-end 1) org-scheduled-string
  8691. (if (match-end 3) org-deadline-string "Plain:"))
  8692. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8693. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8694. (setq n (string-to-number (match-string 2 ts))
  8695. what (match-string 3 ts))
  8696. (if (equal what "w") (setq n (* n 7) what "d"))
  8697. ;; Preparation, see if we need to modify the start date for the change
  8698. (when (match-end 1)
  8699. (setq time (save-match-data (org-time-string-to-time ts)))
  8700. (cond
  8701. ((equal (match-string 1 ts) ".")
  8702. ;; Shift starting date to today
  8703. (org-timestamp-change
  8704. (- (time-to-days (current-time)) (time-to-days time))
  8705. 'day))
  8706. ((equal (match-string 1 ts) "+")
  8707. (while (or (= nshift 0)
  8708. (<= (time-to-days time) (time-to-days (current-time))))
  8709. (when (= (incf nshift) nshiftmax)
  8710. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8711. (error "Abort")))
  8712. (org-timestamp-change n (cdr (assoc what whata)))
  8713. (org-at-timestamp-p t)
  8714. (setq ts (match-string 1))
  8715. (setq time (save-match-data (org-time-string-to-time ts))))
  8716. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8717. ;; rematch, so that we have everything in place for the real shift
  8718. (org-at-timestamp-p t)
  8719. (setq ts (match-string 1))
  8720. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8721. (org-timestamp-change n (cdr (assoc what whata)))
  8722. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8723. (setq org-log-post-message msg)
  8724. (message "%s" msg))))
  8725. (defun org-show-todo-tree (arg)
  8726. "Make a compact tree which shows all headlines marked with TODO.
  8727. The tree will show the lines where the regexp matches, and all higher
  8728. headlines above the match.
  8729. With a \\[universal-argument] prefix, prompt for a regexp to match.
  8730. With a numeric prefix N, construct a sparse tree for the Nth element
  8731. of `org-todo-keywords-1'."
  8732. (interactive "P")
  8733. (let ((case-fold-search nil)
  8734. (kwd-re
  8735. (cond ((null arg) org-not-done-regexp)
  8736. ((equal arg '(4))
  8737. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8738. (mapcar 'list org-todo-keywords-1))))
  8739. (concat "\\("
  8740. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8741. "\\)\\>")))
  8742. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8743. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8744. org-todo-keywords-1)))
  8745. (t (error "Invalid prefix argument: %s" arg)))))
  8746. (message "%d TODO entries found"
  8747. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8748. (defun org-deadline (&optional remove time)
  8749. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8750. With argument REMOVE, remove any deadline from the item.
  8751. When TIME is set, it should be an internal time specification, and the
  8752. scheduling will use the corresponding date."
  8753. (interactive "P")
  8754. (if remove
  8755. (progn
  8756. (org-remove-timestamp-with-keyword org-deadline-string)
  8757. (message "Item no longer has a deadline."))
  8758. (if (org-get-repeat)
  8759. (error "Cannot change deadline on task with repeater, please do that by hand")
  8760. (org-add-planning-info 'deadline time 'closed)
  8761. (message "Deadline on %s" org-last-inserted-timestamp))))
  8762. (defun org-schedule (&optional remove time)
  8763. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8764. With argument REMOVE, remove any scheduling date from the item.
  8765. When TIME is set, it should be an internal time specification, and the
  8766. scheduling will use the corresponding date."
  8767. (interactive "P")
  8768. (if remove
  8769. (progn
  8770. (org-remove-timestamp-with-keyword org-scheduled-string)
  8771. (message "Item is no longer scheduled."))
  8772. (if (org-get-repeat)
  8773. (error "Cannot reschedule task with repeater, please do that by hand")
  8774. (org-add-planning-info 'scheduled time 'closed)
  8775. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8776. (defun org-get-scheduled-time (pom &optional inherit)
  8777. "Get the scheduled time as a time tuple, of a format suitable
  8778. for calling org-schedule with, or if there is no scheduling,
  8779. returns nil."
  8780. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8781. (when time
  8782. (apply 'encode-time (org-parse-time-string time)))))
  8783. (defun org-get-deadline-time (pom &optional inherit)
  8784. "Get the deadine as a time tuple, of a format suitable for
  8785. calling org-deadlin with, or if there is no scheduling, returns
  8786. nil."
  8787. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8788. (when time
  8789. (apply 'encode-time (org-parse-time-string time)))))
  8790. (defun org-remove-timestamp-with-keyword (keyword)
  8791. "Remove all time stamps with KEYWORD in the current entry."
  8792. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8793. beg)
  8794. (save-excursion
  8795. (org-back-to-heading t)
  8796. (setq beg (point))
  8797. (org-end-of-subtree t t)
  8798. (while (re-search-backward re beg t)
  8799. (replace-match "")
  8800. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8801. (equal (char-before) ?\ ))
  8802. (backward-delete-char 1)
  8803. (if (string-match "^[ \t]*$" (buffer-substring
  8804. (point-at-bol) (point-at-eol)))
  8805. (delete-region (point-at-bol)
  8806. (min (point-max) (1+ (point-at-eol))))))))))
  8807. (defun org-add-planning-info (what &optional time &rest remove)
  8808. "Insert new timestamp with keyword in the line directly after the headline.
  8809. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8810. If non is given, the user is prompted for a date.
  8811. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8812. be removed."
  8813. (interactive)
  8814. (let (org-time-was-given org-end-time-was-given ts
  8815. end default-time default-input)
  8816. (catch 'exit
  8817. (when (and (not time) (memq what '(scheduled deadline)))
  8818. ;; Try to get a default date/time from existing timestamp
  8819. (save-excursion
  8820. (org-back-to-heading t)
  8821. (setq end (save-excursion (outline-next-heading) (point)))
  8822. (when (re-search-forward (if (eq what 'scheduled)
  8823. org-scheduled-time-regexp
  8824. org-deadline-time-regexp)
  8825. end t)
  8826. (setq ts (match-string 1)
  8827. default-time
  8828. (apply 'encode-time (org-parse-time-string ts))
  8829. default-input (and ts (org-get-compact-tod ts))))))
  8830. (when what
  8831. ;; If necessary, get the time from the user
  8832. (setq time (or time (org-read-date nil 'to-time nil nil
  8833. default-time default-input))))
  8834. (when (and org-insert-labeled-timestamps-at-point
  8835. (member what '(scheduled deadline)))
  8836. (insert
  8837. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8838. (org-insert-time-stamp time org-time-was-given
  8839. nil nil nil (list org-end-time-was-given))
  8840. (setq what nil))
  8841. (save-excursion
  8842. (save-restriction
  8843. (let (col list elt ts buffer-invisibility-spec)
  8844. (org-back-to-heading t)
  8845. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8846. (goto-char (match-end 1))
  8847. (setq col (current-column))
  8848. (goto-char (match-end 0))
  8849. (if (eobp) (insert "\n") (forward-char 1))
  8850. (when (and (not what)
  8851. (not (looking-at
  8852. (concat "[ \t]*"
  8853. org-keyword-time-not-clock-regexp))))
  8854. ;; Nothing to add, nothing to remove...... :-)
  8855. (throw 'exit nil))
  8856. (if (and (not (looking-at outline-regexp))
  8857. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8858. "[^\r\n]*"))
  8859. (not (equal (match-string 1) org-clock-string)))
  8860. (narrow-to-region (match-beginning 0) (match-end 0))
  8861. (insert-before-markers "\n")
  8862. (backward-char 1)
  8863. (narrow-to-region (point) (point))
  8864. (and org-adapt-indentation (org-indent-to-column col)))
  8865. ;; Check if we have to remove something.
  8866. (setq list (cons what remove))
  8867. (while list
  8868. (setq elt (pop list))
  8869. (goto-char (point-min))
  8870. (when (or (and (eq elt 'scheduled)
  8871. (re-search-forward org-scheduled-time-regexp nil t))
  8872. (and (eq elt 'deadline)
  8873. (re-search-forward org-deadline-time-regexp nil t))
  8874. (and (eq elt 'closed)
  8875. (re-search-forward org-closed-time-regexp nil t)))
  8876. (replace-match "")
  8877. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8878. (if (looking-at " +") (replace-match ""))))
  8879. (goto-char (point-max))
  8880. (when what
  8881. (insert
  8882. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8883. (cond ((eq what 'scheduled) org-scheduled-string)
  8884. ((eq what 'deadline) org-deadline-string)
  8885. ((eq what 'closed) org-closed-string))
  8886. " ")
  8887. (setq ts (org-insert-time-stamp
  8888. time
  8889. (or org-time-was-given
  8890. (and (eq what 'closed) org-log-done-with-time))
  8891. (eq what 'closed)
  8892. nil nil (list org-end-time-was-given)))
  8893. (end-of-line 1))
  8894. (goto-char (point-min))
  8895. (widen)
  8896. (if (and (looking-at "[ \t]+\n")
  8897. (equal (char-before) ?\n))
  8898. (delete-region (1- (point)) (point-at-eol)))
  8899. ts))))))
  8900. (defvar org-log-note-marker (make-marker))
  8901. (defvar org-log-note-purpose nil)
  8902. (defvar org-log-note-state nil)
  8903. (defvar org-log-note-previous-state nil)
  8904. (defvar org-log-note-how nil)
  8905. (defvar org-log-note-extra nil)
  8906. (defvar org-log-note-window-configuration nil)
  8907. (defvar org-log-note-return-to (make-marker))
  8908. (defvar org-log-post-message nil
  8909. "Message to be displayed after a log note has been stored.
  8910. The auto-repeater uses this.")
  8911. (defun org-add-note ()
  8912. "Add a note to the current entry.
  8913. This is done in the same way as adding a state change note."
  8914. (interactive)
  8915. (org-add-log-setup 'note nil nil 'findpos nil))
  8916. (defvar org-property-end-re)
  8917. (defun org-add-log-setup (&optional purpose state prev-state
  8918. findpos how &optional extra)
  8919. "Set up the post command hook to take a note.
  8920. If this is about to TODO state change, the new state is expected in STATE.
  8921. When FINDPOS is non-nil, find the correct position for the note in
  8922. the current entry. If not, assume that it can be inserted at point.
  8923. HOW is an indicator what kind of note should be created.
  8924. EXTRA is additional text that will be inserted into the notes buffer."
  8925. (let* ((org-log-into-drawer (org-log-into-drawer))
  8926. (drawer (cond ((stringp org-log-into-drawer)
  8927. org-log-into-drawer)
  8928. (org-log-into-drawer "LOGBOOK")
  8929. (t nil))))
  8930. (save-restriction
  8931. (save-excursion
  8932. (when findpos
  8933. (org-back-to-heading t)
  8934. (narrow-to-region (point) (save-excursion
  8935. (outline-next-heading) (point)))
  8936. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8937. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8938. "[^\r\n]*\\)?"))
  8939. (goto-char (match-end 0))
  8940. (cond
  8941. (drawer
  8942. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8943. nil t)
  8944. (progn
  8945. (goto-char (match-end 0))
  8946. (or org-log-states-order-reversed
  8947. (and (re-search-forward org-property-end-re nil t)
  8948. (goto-char (1- (match-beginning 0))))))
  8949. (insert "\n:" drawer ":\n:END:")
  8950. (beginning-of-line 0)
  8951. (org-indent-line-function)
  8952. (beginning-of-line 2)
  8953. (org-indent-line-function)
  8954. (end-of-line 0)))
  8955. ((and org-log-state-notes-insert-after-drawers
  8956. (save-excursion
  8957. (forward-line) (looking-at org-drawer-regexp)))
  8958. (forward-line)
  8959. (while (looking-at org-drawer-regexp)
  8960. (goto-char (match-end 0))
  8961. (re-search-forward org-property-end-re (point-max) t)
  8962. (forward-line))
  8963. (forward-line -1)))
  8964. (unless org-log-states-order-reversed
  8965. (and (= (char-after) ?\n) (forward-char 1))
  8966. (org-skip-over-state-notes)
  8967. (skip-chars-backward " \t\n\r")))
  8968. (move-marker org-log-note-marker (point))
  8969. (setq org-log-note-purpose purpose
  8970. org-log-note-state state
  8971. org-log-note-previous-state prev-state
  8972. org-log-note-how how
  8973. org-log-note-extra extra)
  8974. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8975. (defun org-skip-over-state-notes ()
  8976. "Skip past the list of State notes in an entry."
  8977. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8978. (while (looking-at "[ \t]*- State")
  8979. (condition-case nil
  8980. (org-next-item)
  8981. (error (org-end-of-item)))))
  8982. (defun org-add-log-note (&optional purpose)
  8983. "Pop up a window for taking a note, and add this note later at point."
  8984. (remove-hook 'post-command-hook 'org-add-log-note)
  8985. (setq org-log-note-window-configuration (current-window-configuration))
  8986. (delete-other-windows)
  8987. (move-marker org-log-note-return-to (point))
  8988. (switch-to-buffer (marker-buffer org-log-note-marker))
  8989. (goto-char org-log-note-marker)
  8990. (org-switch-to-buffer-other-window "*Org Note*")
  8991. (erase-buffer)
  8992. (if (memq org-log-note-how '(time state))
  8993. (let (current-prefix-arg) (org-store-log-note))
  8994. (let ((org-inhibit-startup t)) (org-mode))
  8995. (insert (format "# Insert note for %s.
  8996. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8997. (cond
  8998. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8999. ((eq org-log-note-purpose 'done) "closed todo item")
  9000. ((eq org-log-note-purpose 'state)
  9001. (format "state change from \"%s\" to \"%s\""
  9002. (or org-log-note-previous-state "")
  9003. (or org-log-note-state "")))
  9004. ((eq org-log-note-purpose 'note)
  9005. "this entry")
  9006. (t (error "This should not happen")))))
  9007. (if org-log-note-extra (insert org-log-note-extra))
  9008. (org-set-local 'org-finish-function 'org-store-log-note)))
  9009. (defvar org-note-abort nil) ; dynamically scoped
  9010. (defun org-store-log-note ()
  9011. "Finish taking a log note, and insert it to where it belongs."
  9012. (let ((txt (buffer-string))
  9013. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9014. lines ind)
  9015. (kill-buffer (current-buffer))
  9016. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9017. (setq txt (replace-match "" t t txt)))
  9018. (if (string-match "\\s-+\\'" txt)
  9019. (setq txt (replace-match "" t t txt)))
  9020. (setq lines (org-split-string txt "\n"))
  9021. (when (and note (string-match "\\S-" note))
  9022. (setq note
  9023. (org-replace-escapes
  9024. note
  9025. (list (cons "%u" (user-login-name))
  9026. (cons "%U" user-full-name)
  9027. (cons "%t" (format-time-string
  9028. (org-time-stamp-format 'long 'inactive)
  9029. (current-time)))
  9030. (cons "%s" (if org-log-note-state
  9031. (concat "\"" org-log-note-state "\"")
  9032. ""))
  9033. (cons "%S" (if org-log-note-previous-state
  9034. (concat "\"" org-log-note-previous-state "\"")
  9035. "\"\"")))))
  9036. (if lines (setq note (concat note " \\\\")))
  9037. (push note lines))
  9038. (when (or current-prefix-arg org-note-abort)
  9039. (when org-log-into-drawer
  9040. (org-remove-empty-drawer-at
  9041. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9042. org-log-note-marker))
  9043. (setq lines nil))
  9044. (when lines
  9045. (save-excursion
  9046. (set-buffer (marker-buffer org-log-note-marker))
  9047. (save-excursion
  9048. (goto-char org-log-note-marker)
  9049. (move-marker org-log-note-marker nil)
  9050. (end-of-line 1)
  9051. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9052. (insert "- " (pop lines))
  9053. (org-indent-line-function)
  9054. (beginning-of-line 1)
  9055. (looking-at "[ \t]*")
  9056. (setq ind (concat (match-string 0) " "))
  9057. (end-of-line 1)
  9058. (while lines (insert "\n" ind (pop lines)))
  9059. (message "Note stored")
  9060. (org-back-to-heading t)
  9061. (org-cycle-hide-drawers 'children)))))
  9062. (set-window-configuration org-log-note-window-configuration)
  9063. (with-current-buffer (marker-buffer org-log-note-return-to)
  9064. (goto-char org-log-note-return-to))
  9065. (move-marker org-log-note-return-to nil)
  9066. (and org-log-post-message (message "%s" org-log-post-message)))
  9067. (defun org-remove-empty-drawer-at (drawer pos)
  9068. "Remove an emptyr DARWER drawer at position POS.
  9069. POS may also be a marker."
  9070. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9071. (save-excursion
  9072. (save-restriction
  9073. (widen)
  9074. (goto-char pos)
  9075. (if (org-in-regexp
  9076. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9077. (replace-match ""))))))
  9078. (defun org-sparse-tree (&optional arg)
  9079. "Create a sparse tree, prompt for the details.
  9080. This command can create sparse trees. You first need to select the type
  9081. of match used to create the tree:
  9082. t Show entries with a specific TODO keyword.
  9083. m Show entries selected by a tags/property match.
  9084. p Enter a property name and its value (both with completion on existing
  9085. names/values) and show entries with that property.
  9086. r Show entries matching a regular expression.
  9087. d Show deadlines due within `org-deadline-warning-days'.
  9088. b Show deadlines and scheduled items before a date.
  9089. a Show deadlines and scheduled items after a date."
  9090. (interactive "P")
  9091. (let (ans kwd value)
  9092. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9093. (setq ans (read-char-exclusive))
  9094. (cond
  9095. ((equal ans ?d)
  9096. (call-interactively 'org-check-deadlines))
  9097. ((equal ans ?b)
  9098. (call-interactively 'org-check-before-date))
  9099. ((equal ans ?a)
  9100. (call-interactively 'org-check-after-date))
  9101. ((equal ans ?t)
  9102. (org-show-todo-tree '(4)))
  9103. ((member ans '(?T ?m))
  9104. (call-interactively 'org-match-sparse-tree))
  9105. ((member ans '(?p ?P))
  9106. (setq kwd (org-ido-completing-read "Property: "
  9107. (mapcar 'list (org-buffer-property-keys))))
  9108. (setq value (org-ido-completing-read "Value: "
  9109. (mapcar 'list (org-property-values kwd))))
  9110. (unless (string-match "\\`{.*}\\'" value)
  9111. (setq value (concat "\"" value "\"")))
  9112. (org-match-sparse-tree arg (concat kwd "=" value)))
  9113. ((member ans '(?r ?R ?/))
  9114. (call-interactively 'org-occur))
  9115. (t (error "No such sparse tree command \"%c\"" ans)))))
  9116. (defvar org-occur-highlights nil
  9117. "List of overlays used for occur matches.")
  9118. (make-variable-buffer-local 'org-occur-highlights)
  9119. (defvar org-occur-parameters nil
  9120. "Parameters of the active org-occur calls.
  9121. This is a list, each call to org-occur pushes as cons cell,
  9122. containing the regular expression and the callback, onto the list.
  9123. The list can contain several entries if `org-occur' has been called
  9124. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9125. will only contain one set of parameters. When the highlights are
  9126. removed (for example with `C-c C-c', or with the next edit (depending
  9127. on `org-remove-highlights-with-change'), this variable is emptied
  9128. as well.")
  9129. (make-variable-buffer-local 'org-occur-parameters)
  9130. (defun org-occur (regexp &optional keep-previous callback)
  9131. "Make a compact tree which shows all matches of REGEXP.
  9132. The tree will show the lines where the regexp matches, and all higher
  9133. headlines above the match. It will also show the heading after the match,
  9134. to make sure editing the matching entry is easy.
  9135. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9136. call to `org-occur' will be kept, to allow stacking of calls to this
  9137. command.
  9138. If CALLBACK is non-nil, it is a function which is called to confirm
  9139. that the match should indeed be shown."
  9140. (interactive "sRegexp: \nP")
  9141. (unless keep-previous
  9142. (org-remove-occur-highlights nil nil t))
  9143. (push (cons regexp callback) org-occur-parameters)
  9144. (let ((cnt 0))
  9145. (save-excursion
  9146. (goto-char (point-min))
  9147. (if (or (not keep-previous) ; do not want to keep
  9148. (not org-occur-highlights)) ; no previous matches
  9149. ;; hide everything
  9150. (org-overview))
  9151. (while (re-search-forward regexp nil t)
  9152. (when (or (not callback)
  9153. (save-match-data (funcall callback)))
  9154. (setq cnt (1+ cnt))
  9155. (when org-highlight-sparse-tree-matches
  9156. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9157. (org-show-context 'occur-tree))))
  9158. (when org-remove-highlights-with-change
  9159. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9160. nil 'local))
  9161. (unless org-sparse-tree-open-archived-trees
  9162. (org-hide-archived-subtrees (point-min) (point-max)))
  9163. (run-hooks 'org-occur-hook)
  9164. (if (interactive-p)
  9165. (message "%d match(es) for regexp %s" cnt regexp))
  9166. cnt))
  9167. (defun org-show-context (&optional key)
  9168. "Make sure point and context and visible.
  9169. How much context is shown depends upon the variables
  9170. `org-show-hierarchy-above', `org-show-following-heading'. and
  9171. `org-show-siblings'."
  9172. (let ((heading-p (org-on-heading-p t))
  9173. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9174. (following-p (org-get-alist-option org-show-following-heading key))
  9175. (entry-p (org-get-alist-option org-show-entry-below key))
  9176. (siblings-p (org-get-alist-option org-show-siblings key)))
  9177. (catch 'exit
  9178. ;; Show heading or entry text
  9179. (if (and heading-p (not entry-p))
  9180. (org-flag-heading nil) ; only show the heading
  9181. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9182. (org-show-hidden-entry))) ; show entire entry
  9183. (when following-p
  9184. ;; Show next sibling, or heading below text
  9185. (save-excursion
  9186. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9187. (org-flag-heading nil))))
  9188. (when siblings-p (org-show-siblings))
  9189. (when hierarchy-p
  9190. ;; show all higher headings, possibly with siblings
  9191. (save-excursion
  9192. (while (and (condition-case nil
  9193. (progn (org-up-heading-all 1) t)
  9194. (error nil))
  9195. (not (bobp)))
  9196. (org-flag-heading nil)
  9197. (when siblings-p (org-show-siblings))))))))
  9198. (defun org-reveal (&optional siblings)
  9199. "Show current entry, hierarchy above it, and the following headline.
  9200. This can be used to show a consistent set of context around locations
  9201. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9202. not t for the search context.
  9203. With optional argument SIBLINGS, on each level of the hierarchy all
  9204. siblings are shown. This repairs the tree structure to what it would
  9205. look like when opened with hierarchical calls to `org-cycle'."
  9206. (interactive "P")
  9207. (let ((org-show-hierarchy-above t)
  9208. (org-show-following-heading t)
  9209. (org-show-siblings (if siblings t org-show-siblings)))
  9210. (org-show-context nil)))
  9211. (defun org-highlight-new-match (beg end)
  9212. "Highlight from BEG to END and mark the highlight is an occur headline."
  9213. (let ((ov (org-make-overlay beg end)))
  9214. (org-overlay-put ov 'face 'secondary-selection)
  9215. (push ov org-occur-highlights)))
  9216. (defun org-remove-occur-highlights (&optional beg end noremove)
  9217. "Remove the occur highlights from the buffer.
  9218. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9219. from the `before-change-functions' in the current buffer."
  9220. (interactive)
  9221. (unless org-inhibit-highlight-removal
  9222. (mapc 'org-delete-overlay org-occur-highlights)
  9223. (setq org-occur-highlights nil)
  9224. (setq org-occur-parameters nil)
  9225. (unless noremove
  9226. (remove-hook 'before-change-functions
  9227. 'org-remove-occur-highlights 'local))))
  9228. ;;;; Priorities
  9229. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9230. "Regular expression matching the priority indicator.")
  9231. (defvar org-remove-priority-next-time nil)
  9232. (defun org-priority-up ()
  9233. "Increase the priority of the current item."
  9234. (interactive)
  9235. (org-priority 'up))
  9236. (defun org-priority-down ()
  9237. "Decrease the priority of the current item."
  9238. (interactive)
  9239. (org-priority 'down))
  9240. (defun org-priority (&optional action)
  9241. "Change the priority of an item by ARG.
  9242. ACTION can be `set', `up', `down', or a character."
  9243. (interactive)
  9244. (unless org-enable-priority-commands
  9245. (error "Priority commands are disabled"))
  9246. (setq action (or action 'set))
  9247. (let (current new news have remove)
  9248. (save-excursion
  9249. (org-back-to-heading t)
  9250. (if (looking-at org-priority-regexp)
  9251. (setq current (string-to-char (match-string 2))
  9252. have t)
  9253. (setq current org-default-priority))
  9254. (cond
  9255. ((or (eq action 'set)
  9256. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9257. (if (not (eq action 'set))
  9258. (setq new action)
  9259. (message "Priority %c-%c, SPC to remove: "
  9260. org-highest-priority org-lowest-priority)
  9261. (setq new (read-char-exclusive)))
  9262. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9263. (= (upcase org-lowest-priority) org-lowest-priority))
  9264. (setq new (upcase new)))
  9265. (cond ((equal new ?\ ) (setq remove t))
  9266. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9267. (error "Priority must be between `%c' and `%c'"
  9268. org-highest-priority org-lowest-priority))))
  9269. ((eq action 'up)
  9270. (if (and (not have) (eq last-command this-command))
  9271. (setq new org-lowest-priority)
  9272. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9273. org-default-priority (1- current)))))
  9274. ((eq action 'down)
  9275. (if (and (not have) (eq last-command this-command))
  9276. (setq new org-highest-priority)
  9277. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9278. org-default-priority (1+ current)))))
  9279. (t (error "Invalid action")))
  9280. (if (or (< (upcase new) org-highest-priority)
  9281. (> (upcase new) org-lowest-priority))
  9282. (setq remove t))
  9283. (setq news (format "%c" new))
  9284. (if have
  9285. (if remove
  9286. (replace-match "" t t nil 1)
  9287. (replace-match news t t nil 2))
  9288. (if remove
  9289. (error "No priority cookie found in line")
  9290. (let ((case-fold-search nil))
  9291. (looking-at org-todo-line-regexp))
  9292. (if (match-end 2)
  9293. (progn
  9294. (goto-char (match-end 2))
  9295. (insert " [#" news "]"))
  9296. (goto-char (match-beginning 3))
  9297. (insert "[#" news "] ")))))
  9298. (org-preserve-lc (org-set-tags nil 'align))
  9299. (if remove
  9300. (message "Priority removed")
  9301. (message "Priority of current item set to %s" news))))
  9302. (defun org-get-priority (s)
  9303. "Find priority cookie and return priority."
  9304. (save-match-data
  9305. (if (not (string-match org-priority-regexp s))
  9306. (* 1000 (- org-lowest-priority org-default-priority))
  9307. (* 1000 (- org-lowest-priority
  9308. (string-to-char (match-string 2 s)))))))
  9309. ;;;; Tags
  9310. (defvar org-agenda-archives-mode)
  9311. (defvar org-map-continue-from nil
  9312. "Position from where mapping should continue.
  9313. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9314. (defvar org-scanner-tags nil
  9315. "The current tag list while the tags scanner is running.")
  9316. (defvar org-trust-scanner-tags nil
  9317. "Should `org-get-tags-at' use the tags fro the scanner.
  9318. This is for internal dynamical scoping only.
  9319. When this is non-nil, the function `org-get-tags-at' will return the value
  9320. of `org-scanner-tags' instead of building the list by itself. This
  9321. can lead to large speed-ups when the tags scanner is used in a file with
  9322. many entries, and when the list of tags is retrieved, for example to
  9323. obtain a list of properties. Building the tags list for each entry in such
  9324. a file becomes an N^2 operation - but with this variable set, it scales
  9325. as N.")
  9326. (defun org-scan-tags (action matcher &optional todo-only)
  9327. "Scan headline tags with inheritance and produce output ACTION.
  9328. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9329. or `agenda' to produce an entry list for an agenda view. It can also be
  9330. a Lisp form or a function that should be called at each matched headline, in
  9331. this case the return value is a list of all return values from these calls.
  9332. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9333. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9334. only lines with a TODO keyword are included in the output."
  9335. (require 'org-agenda)
  9336. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9337. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9338. (org-re
  9339. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9340. (props (list 'face 'default
  9341. 'done-face 'org-agenda-done
  9342. 'undone-face 'default
  9343. 'mouse-face 'highlight
  9344. 'org-not-done-regexp org-not-done-regexp
  9345. 'org-todo-regexp org-todo-regexp
  9346. 'keymap org-agenda-keymap
  9347. 'help-echo
  9348. (format "mouse-2 or RET jump to org file %s"
  9349. (abbreviate-file-name
  9350. (or (buffer-file-name (buffer-base-buffer))
  9351. (buffer-name (buffer-base-buffer)))))))
  9352. (case-fold-search nil)
  9353. (org-map-continue-from nil)
  9354. lspos tags tags-list
  9355. (tags-alist (list (cons 0 org-file-tags)))
  9356. (llast 0) rtn rtn1 level category i txt
  9357. todo marker entry priority)
  9358. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9359. (setq action (list 'lambda nil action)))
  9360. (save-excursion
  9361. (goto-char (point-min))
  9362. (when (eq action 'sparse-tree)
  9363. (org-overview)
  9364. (org-remove-occur-highlights))
  9365. (while (re-search-forward re nil t)
  9366. (catch :skip
  9367. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9368. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9369. (goto-char (setq lspos (match-beginning 0)))
  9370. (setq level (org-reduced-level (funcall outline-level))
  9371. category (org-get-category))
  9372. (setq i llast llast level)
  9373. ;; remove tag lists from same and sublevels
  9374. (while (>= i level)
  9375. (when (setq entry (assoc i tags-alist))
  9376. (setq tags-alist (delete entry tags-alist)))
  9377. (setq i (1- i)))
  9378. ;; add the next tags
  9379. (when tags
  9380. (setq tags (org-split-string tags ":")
  9381. tags-alist
  9382. (cons (cons level tags) tags-alist)))
  9383. ;; compile tags for current headline
  9384. (setq tags-list
  9385. (if org-use-tag-inheritance
  9386. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9387. tags)
  9388. org-scanner-tags tags-list)
  9389. (when org-use-tag-inheritance
  9390. (setcdr (car tags-alist)
  9391. (mapcar (lambda (x)
  9392. (setq x (copy-sequence x))
  9393. (org-add-prop-inherited x))
  9394. (cdar tags-alist))))
  9395. (when (and tags org-use-tag-inheritance
  9396. (or (not (eq t org-use-tag-inheritance))
  9397. org-tags-exclude-from-inheritance))
  9398. ;; selective inheritance, remove uninherited ones
  9399. (setcdr (car tags-alist)
  9400. (org-remove-uniherited-tags (cdar tags-alist))))
  9401. (when (and (or (not todo-only)
  9402. (and (member todo org-not-done-keywords)
  9403. (or (not org-agenda-tags-todo-honor-ignore-options)
  9404. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9405. (let ((case-fold-search t)) (eval matcher))
  9406. (or
  9407. (not (member org-archive-tag tags-list))
  9408. ;; we have an archive tag, should we use this anyway?
  9409. (or (not org-agenda-skip-archived-trees)
  9410. (and (eq action 'agenda) org-agenda-archives-mode))))
  9411. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9412. ;; select this headline
  9413. (cond
  9414. ((eq action 'sparse-tree)
  9415. (and org-highlight-sparse-tree-matches
  9416. (org-get-heading) (match-end 0)
  9417. (org-highlight-new-match
  9418. (match-beginning 0) (match-beginning 1)))
  9419. (org-show-context 'tags-tree))
  9420. ((eq action 'agenda)
  9421. (setq txt (org-format-agenda-item
  9422. ""
  9423. (concat
  9424. (if (eq org-tags-match-list-sublevels 'indented)
  9425. (make-string (1- level) ?.) "")
  9426. (org-get-heading))
  9427. category
  9428. tags-list
  9429. )
  9430. priority (org-get-priority txt))
  9431. (goto-char lspos)
  9432. (setq marker (org-agenda-new-marker))
  9433. (org-add-props txt props
  9434. 'org-marker marker 'org-hd-marker marker 'org-category category
  9435. 'todo-state todo
  9436. 'priority priority 'type "tagsmatch")
  9437. (push txt rtn))
  9438. ((functionp action)
  9439. (setq org-map-continue-from nil)
  9440. (save-excursion
  9441. (setq rtn1 (funcall action))
  9442. (push rtn1 rtn)))
  9443. (t (error "Invalid action")))
  9444. ;; if we are to skip sublevels, jump to end of subtree
  9445. (unless org-tags-match-list-sublevels
  9446. (org-end-of-subtree t)
  9447. (backward-char 1))))
  9448. ;; Get the correct position from where to continue
  9449. (if org-map-continue-from
  9450. (goto-char org-map-continue-from)
  9451. (and (= (point) lspos) (end-of-line 1)))))
  9452. (when (and (eq action 'sparse-tree)
  9453. (not org-sparse-tree-open-archived-trees))
  9454. (org-hide-archived-subtrees (point-min) (point-max)))
  9455. (nreverse rtn)))
  9456. (defun org-remove-uniherited-tags (tags)
  9457. "Remove all tags that are not inherited from the list TAGS."
  9458. (cond
  9459. ((eq org-use-tag-inheritance t)
  9460. (if org-tags-exclude-from-inheritance
  9461. (org-delete-all org-tags-exclude-from-inheritance tags)
  9462. tags))
  9463. ((not org-use-tag-inheritance) nil)
  9464. ((stringp org-use-tag-inheritance)
  9465. (delq nil (mapcar
  9466. (lambda (x)
  9467. (if (and (string-match org-use-tag-inheritance x)
  9468. (not (member x org-tags-exclude-from-inheritance)))
  9469. x nil))
  9470. tags)))
  9471. ((listp org-use-tag-inheritance)
  9472. (delq nil (mapcar
  9473. (lambda (x)
  9474. (if (member x org-use-tag-inheritance) x nil))
  9475. tags)))))
  9476. (defvar todo-only) ;; dynamically scoped
  9477. (defun org-match-sparse-tree (&optional todo-only match)
  9478. "Create a sparse tree according to tags string MATCH.
  9479. MATCH can contain positive and negative selection of tags, like
  9480. \"+WORK+URGENT-WITHBOSS\".
  9481. If optional argument TODO-ONLY is non-nil, only select lines that are
  9482. also TODO lines."
  9483. (interactive "P")
  9484. (org-prepare-agenda-buffers (list (current-buffer)))
  9485. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9486. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9487. (defvar org-cached-props nil)
  9488. (defun org-cached-entry-get (pom property)
  9489. (if (or (eq t org-use-property-inheritance)
  9490. (and (stringp org-use-property-inheritance)
  9491. (string-match org-use-property-inheritance property))
  9492. (and (listp org-use-property-inheritance)
  9493. (member property org-use-property-inheritance)))
  9494. ;; Caching is not possible, check it directly
  9495. (org-entry-get pom property 'inherit)
  9496. ;; Get all properties, so that we can do complicated checks easily
  9497. (cdr (assoc property (or org-cached-props
  9498. (setq org-cached-props
  9499. (org-entry-properties pom)))))))
  9500. (defun org-global-tags-completion-table (&optional files)
  9501. "Return the list of all tags in all agenda buffer/files."
  9502. (save-excursion
  9503. (org-uniquify
  9504. (delq nil
  9505. (apply 'append
  9506. (mapcar
  9507. (lambda (file)
  9508. (set-buffer (find-file-noselect file))
  9509. (append (org-get-buffer-tags)
  9510. (mapcar (lambda (x) (if (stringp (car-safe x))
  9511. (list (car-safe x)) nil))
  9512. org-tag-alist)))
  9513. (if (and files (car files))
  9514. files
  9515. (org-agenda-files))))))))
  9516. (defun org-make-tags-matcher (match)
  9517. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9518. ;; todo-only is scoped dynamically into this function, and the function
  9519. ;; may change it if the matcher asks for it.
  9520. (unless match
  9521. ;; Get a new match request, with completion
  9522. (let ((org-last-tags-completion-table
  9523. (org-global-tags-completion-table)))
  9524. (setq match (org-completing-read-no-ido
  9525. "Match: " 'org-tags-completion-function nil nil nil
  9526. 'org-tags-history))))
  9527. ;; Parse the string and create a lisp form
  9528. (let ((match0 match)
  9529. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9530. minus tag mm
  9531. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9532. orterms term orlist re-p str-p level-p level-op time-p
  9533. prop-p pn pv po cat-p gv rest)
  9534. (if (string-match "/+" match)
  9535. ;; match contains also a todo-matching request
  9536. (progn
  9537. (setq tagsmatch (substring match 0 (match-beginning 0))
  9538. todomatch (substring match (match-end 0)))
  9539. (if (string-match "^!" todomatch)
  9540. (setq todo-only t todomatch (substring todomatch 1)))
  9541. (if (string-match "^\\s-*$" todomatch)
  9542. (setq todomatch nil)))
  9543. ;; only matching tags
  9544. (setq tagsmatch match todomatch nil))
  9545. ;; Make the tags matcher
  9546. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9547. (setq tagsmatcher t)
  9548. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9549. (while (setq term (pop orterms))
  9550. (while (and (equal (substring term -1) "\\") orterms)
  9551. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9552. (while (string-match re term)
  9553. (setq rest (substring term (match-end 0))
  9554. minus (and (match-end 1)
  9555. (equal (match-string 1 term) "-"))
  9556. tag (match-string 2 term)
  9557. re-p (equal (string-to-char tag) ?{)
  9558. level-p (match-end 4)
  9559. prop-p (match-end 5)
  9560. mm (cond
  9561. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9562. (level-p
  9563. (setq level-op (org-op-to-function (match-string 3 term)))
  9564. `(,level-op level ,(string-to-number
  9565. (match-string 4 term))))
  9566. (prop-p
  9567. (setq pn (match-string 5 term)
  9568. po (match-string 6 term)
  9569. pv (match-string 7 term)
  9570. cat-p (equal pn "CATEGORY")
  9571. re-p (equal (string-to-char pv) ?{)
  9572. str-p (equal (string-to-char pv) ?\")
  9573. time-p (save-match-data
  9574. (string-match "^\"[[<].*[]>]\"$" pv))
  9575. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9576. (if time-p (setq pv (org-matcher-time pv)))
  9577. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9578. (cond
  9579. ((equal pn "CATEGORY")
  9580. (setq gv '(get-text-property (point) 'org-category)))
  9581. ((equal pn "TODO")
  9582. (setq gv 'todo))
  9583. (t
  9584. (setq gv `(org-cached-entry-get nil ,pn))))
  9585. (if re-p
  9586. (if (eq po 'org<>)
  9587. `(not (string-match ,pv (or ,gv "")))
  9588. `(string-match ,pv (or ,gv "")))
  9589. (if str-p
  9590. `(,po (or ,gv "") ,pv)
  9591. `(,po (string-to-number (or ,gv ""))
  9592. ,(string-to-number pv) ))))
  9593. (t `(member ,tag tags-list)))
  9594. mm (if minus (list 'not mm) mm)
  9595. term rest)
  9596. (push mm tagsmatcher))
  9597. (push (if (> (length tagsmatcher) 1)
  9598. (cons 'and tagsmatcher)
  9599. (car tagsmatcher))
  9600. orlist)
  9601. (setq tagsmatcher nil))
  9602. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9603. (setq tagsmatcher
  9604. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9605. ;; Make the todo matcher
  9606. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9607. (setq todomatcher t)
  9608. (setq orterms (org-split-string todomatch "|") orlist nil)
  9609. (while (setq term (pop orterms))
  9610. (while (string-match re term)
  9611. (setq minus (and (match-end 1)
  9612. (equal (match-string 1 term) "-"))
  9613. kwd (match-string 2 term)
  9614. re-p (equal (string-to-char kwd) ?{)
  9615. term (substring term (match-end 0))
  9616. mm (if re-p
  9617. `(string-match ,(substring kwd 1 -1) todo)
  9618. (list 'equal 'todo kwd))
  9619. mm (if minus (list 'not mm) mm))
  9620. (push mm todomatcher))
  9621. (push (if (> (length todomatcher) 1)
  9622. (cons 'and todomatcher)
  9623. (car todomatcher))
  9624. orlist)
  9625. (setq todomatcher nil))
  9626. (setq todomatcher (if (> (length orlist) 1)
  9627. (cons 'or orlist) (car orlist))))
  9628. ;; Return the string and lisp forms of the matcher
  9629. (setq matcher (if todomatcher
  9630. (list 'and tagsmatcher todomatcher)
  9631. tagsmatcher))
  9632. (cons match0 matcher)))
  9633. (defun org-op-to-function (op &optional stringp)
  9634. "Turn an operator into the appropriate function."
  9635. (setq op
  9636. (cond
  9637. ((equal op "<" ) '(< string< org-time<))
  9638. ((equal op ">" ) '(> org-string> org-time>))
  9639. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9640. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9641. ((member op '("=" "==")) '(= string= org-time=))
  9642. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9643. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9644. (defun org<> (a b) (not (= a b)))
  9645. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9646. (defun org-string>= (a b) (not (string< a b)))
  9647. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9648. (defun org-string<> (a b) (not (string= a b)))
  9649. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9650. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9651. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9652. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9653. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9654. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9655. (defun org-2ft (s)
  9656. "Convert S to a floating point time.
  9657. If S is already a number, just return it. If it is a string, parse
  9658. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9659. (cond
  9660. ((numberp s) s)
  9661. ((stringp s)
  9662. (condition-case nil
  9663. (float-time (apply 'encode-time (org-parse-time-string s)))
  9664. (error 0.)))
  9665. (t 0.)))
  9666. (defun org-time-today ()
  9667. "Time in seconds today at 0:00.
  9668. Returns the float number of seconds since the beginning of the
  9669. epoch to the beginning of today (00:00)."
  9670. (float-time (apply 'encode-time
  9671. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9672. (defun org-matcher-time (s)
  9673. "Interpret a time comparison value."
  9674. (save-match-data
  9675. (cond
  9676. ((string= s "<now>") (float-time))
  9677. ((string= s "<today>") (org-time-today))
  9678. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9679. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9680. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9681. (+ (org-time-today)
  9682. (* (string-to-number (match-string 1 s))
  9683. (cdr (assoc (match-string 2 s)
  9684. '(("d" . 86400.0) ("w" . 604800.0)
  9685. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9686. (t (org-2ft s)))))
  9687. (defun org-match-any-p (re list)
  9688. "Does re match any element of list?"
  9689. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9690. (delq nil list))
  9691. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9692. (defvar org-tags-overlay (org-make-overlay 1 1))
  9693. (org-detach-overlay org-tags-overlay)
  9694. (defun org-get-local-tags-at (&optional pos)
  9695. "Get a list of tags defined in the current headline."
  9696. (org-get-tags-at pos 'local))
  9697. (defun org-get-local-tags ()
  9698. "Get a list of tags defined in the current headline."
  9699. (org-get-tags-at nil 'local))
  9700. (defun org-get-tags-at (&optional pos local)
  9701. "Get a list of all headline tags applicable at POS.
  9702. POS defaults to point. If tags are inherited, the list contains
  9703. the targets in the same sequence as the headlines appear, i.e.
  9704. the tags of the current headline come last.
  9705. When LOCAL is non-nil, only return tags from the current headline,
  9706. ignore inherited ones."
  9707. (interactive)
  9708. (if (and org-trust-scanner-tags
  9709. (or (not pos) (equal pos (point)))
  9710. (not local))
  9711. org-scanner-tags
  9712. (let (tags ltags lastpos parent)
  9713. (save-excursion
  9714. (save-restriction
  9715. (widen)
  9716. (goto-char (or pos (point)))
  9717. (save-match-data
  9718. (catch 'done
  9719. (condition-case nil
  9720. (progn
  9721. (org-back-to-heading t)
  9722. (while (not (equal lastpos (point)))
  9723. (setq lastpos (point))
  9724. (when (looking-at
  9725. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9726. (setq ltags (org-split-string
  9727. (org-match-string-no-properties 1) ":"))
  9728. (when parent
  9729. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9730. (setq tags (append
  9731. (if parent
  9732. (org-remove-uniherited-tags ltags)
  9733. ltags)
  9734. tags)))
  9735. (or org-use-tag-inheritance (throw 'done t))
  9736. (if local (throw 'done t))
  9737. (or (org-up-heading-safe) (error nil))
  9738. (setq parent t)))
  9739. (error nil)))))
  9740. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9741. (defun org-add-prop-inherited (s)
  9742. (add-text-properties 0 (length s) '(inherited t) s)
  9743. s)
  9744. (defun org-toggle-tag (tag &optional onoff)
  9745. "Toggle the tag TAG for the current line.
  9746. If ONOFF is `on' or `off', don't toggle but set to this state."
  9747. (let (res current)
  9748. (save-excursion
  9749. (org-back-to-heading t)
  9750. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9751. (point-at-eol) t)
  9752. (progn
  9753. (setq current (match-string 1))
  9754. (replace-match ""))
  9755. (setq current ""))
  9756. (setq current (nreverse (org-split-string current ":")))
  9757. (cond
  9758. ((eq onoff 'on)
  9759. (setq res t)
  9760. (or (member tag current) (push tag current)))
  9761. ((eq onoff 'off)
  9762. (or (not (member tag current)) (setq current (delete tag current))))
  9763. (t (if (member tag current)
  9764. (setq current (delete tag current))
  9765. (setq res t)
  9766. (push tag current))))
  9767. (end-of-line 1)
  9768. (if current
  9769. (progn
  9770. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9771. (org-set-tags nil t))
  9772. (delete-horizontal-space))
  9773. (run-hooks 'org-after-tags-change-hook))
  9774. res))
  9775. (defun org-align-tags-here (to-col)
  9776. ;; Assumes that this is a headline
  9777. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9778. (beginning-of-line 1)
  9779. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9780. (< pos (match-beginning 2)))
  9781. (progn
  9782. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9783. (goto-char (match-beginning 1))
  9784. (insert " ")
  9785. (delete-region (point) (1+ (match-beginning 2)))
  9786. (setq ncol (max (1+ (current-column))
  9787. (1+ col)
  9788. (if (> to-col 0)
  9789. to-col
  9790. (- (abs to-col) tags-l))))
  9791. (setq p (point))
  9792. (insert (make-string (- ncol (current-column)) ?\ ))
  9793. (setq ncol (current-column))
  9794. (when indent-tabs-mode (tabify p (point-at-eol)))
  9795. (org-move-to-column (min ncol col) t))
  9796. (goto-char pos))))
  9797. (defun org-set-tags-command (&optional arg just-align)
  9798. "Call the set-tags command for the current entry."
  9799. (interactive "P")
  9800. (if (org-on-heading-p)
  9801. (org-set-tags arg just-align)
  9802. (save-excursion
  9803. (org-back-to-heading t)
  9804. (org-set-tags arg just-align))))
  9805. (defun org-set-tags (&optional arg just-align)
  9806. "Set the tags for the current headline.
  9807. With prefix ARG, realign all tags in headings in the current buffer."
  9808. (interactive "P")
  9809. (let* ((re (concat "^" outline-regexp))
  9810. (current (org-get-tags-string))
  9811. (col (current-column))
  9812. (org-setting-tags t)
  9813. table current-tags inherited-tags ; computed below when needed
  9814. tags p0 c0 c1 rpl)
  9815. (if arg
  9816. (save-excursion
  9817. (goto-char (point-min))
  9818. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9819. (while (re-search-forward re nil t)
  9820. (org-set-tags nil t)
  9821. (end-of-line 1)))
  9822. (message "All tags realigned to column %d" org-tags-column))
  9823. (if just-align
  9824. (setq tags current)
  9825. ;; Get a new set of tags from the user
  9826. (save-excursion
  9827. (setq table (append org-tag-persistent-alist
  9828. (or org-tag-alist (org-get-buffer-tags)))
  9829. org-last-tags-completion-table table
  9830. current-tags (org-split-string current ":")
  9831. inherited-tags (nreverse
  9832. (nthcdr (length current-tags)
  9833. (nreverse (org-get-tags-at))))
  9834. tags
  9835. (if (or (eq t org-use-fast-tag-selection)
  9836. (and org-use-fast-tag-selection
  9837. (delq nil (mapcar 'cdr table))))
  9838. (org-fast-tag-selection
  9839. current-tags inherited-tags table
  9840. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9841. (let ((org-add-colon-after-tag-completion t))
  9842. (org-trim
  9843. (org-without-partial-completion
  9844. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9845. nil nil current 'org-tags-history)))))))
  9846. (while (string-match "[-+&]+" tags)
  9847. ;; No boolean logic, just a list
  9848. (setq tags (replace-match ":" t t tags))))
  9849. (if (string-match "\\`[\t ]*\\'" tags)
  9850. (setq tags "")
  9851. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9852. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9853. ;; Insert new tags at the correct column
  9854. (beginning-of-line 1)
  9855. (cond
  9856. ((and (equal current "") (equal tags "")))
  9857. ((re-search-forward
  9858. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9859. (point-at-eol) t)
  9860. (if (equal tags "")
  9861. (setq rpl "")
  9862. (goto-char (match-beginning 0))
  9863. (setq c0 (current-column) p0 (point)
  9864. c1 (max (1+ c0) (if (> org-tags-column 0)
  9865. org-tags-column
  9866. (- (- org-tags-column) (length tags))))
  9867. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9868. (replace-match rpl t t)
  9869. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9870. tags)
  9871. (t (error "Tags alignment failed")))
  9872. (org-move-to-column col)
  9873. (unless just-align
  9874. (run-hooks 'org-after-tags-change-hook)))))
  9875. (defun org-change-tag-in-region (beg end tag off)
  9876. "Add or remove TAG for each entry in the region.
  9877. This works in the agenda, and also in an org-mode buffer."
  9878. (interactive
  9879. (list (region-beginning) (region-end)
  9880. (let ((org-last-tags-completion-table
  9881. (if (org-mode-p)
  9882. (org-get-buffer-tags)
  9883. (org-global-tags-completion-table))))
  9884. (org-ido-completing-read
  9885. "Tag: " 'org-tags-completion-function nil nil nil
  9886. 'org-tags-history))
  9887. (progn
  9888. (message "[s]et or [r]emove? ")
  9889. (equal (read-char-exclusive) ?r))))
  9890. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9891. (let ((agendap (equal major-mode 'org-agenda-mode))
  9892. l1 l2 m buf pos newhead (cnt 0))
  9893. (goto-char end)
  9894. (setq l2 (1- (org-current-line)))
  9895. (goto-char beg)
  9896. (setq l1 (org-current-line))
  9897. (loop for l from l1 to l2 do
  9898. (goto-line l)
  9899. (setq m (get-text-property (point) 'org-hd-marker))
  9900. (when (or (and (org-mode-p) (org-on-heading-p))
  9901. (and agendap m))
  9902. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9903. pos (if agendap m (point)))
  9904. (with-current-buffer buf
  9905. (save-excursion
  9906. (save-restriction
  9907. (goto-char pos)
  9908. (setq cnt (1+ cnt))
  9909. (org-toggle-tag tag (if off 'off 'on))
  9910. (setq newhead (org-get-heading)))))
  9911. (and agendap (org-agenda-change-all-lines newhead m))))
  9912. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9913. (defun org-tags-completion-function (string predicate &optional flag)
  9914. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9915. (confirm (lambda (x) (stringp (car x)))))
  9916. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9917. (setq s1 (match-string 1 string)
  9918. s2 (match-string 2 string))
  9919. (setq s1 "" s2 string))
  9920. (cond
  9921. ((eq flag nil)
  9922. ;; try completion
  9923. (setq rtn (try-completion s2 ctable confirm))
  9924. (if (stringp rtn)
  9925. (setq rtn
  9926. (concat s1 s2 (substring rtn (length s2))
  9927. (if (and org-add-colon-after-tag-completion
  9928. (assoc rtn ctable))
  9929. ":" ""))))
  9930. rtn)
  9931. ((eq flag t)
  9932. ;; all-completions
  9933. (all-completions s2 ctable confirm)
  9934. )
  9935. ((eq flag 'lambda)
  9936. ;; exact match?
  9937. (assoc s2 ctable)))
  9938. ))
  9939. (defun org-fast-tag-insert (kwd tags face &optional end)
  9940. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9941. (insert (format "%-12s" (concat kwd ":"))
  9942. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9943. (or end "")))
  9944. (defun org-fast-tag-show-exit (flag)
  9945. (save-excursion
  9946. (goto-line 3)
  9947. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9948. (replace-match ""))
  9949. (when flag
  9950. (end-of-line 1)
  9951. (org-move-to-column (- (window-width) 19) t)
  9952. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9953. (defun org-set-current-tags-overlay (current prefix)
  9954. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9955. (if (featurep 'xemacs)
  9956. (org-overlay-display org-tags-overlay (concat prefix s)
  9957. 'secondary-selection)
  9958. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9959. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9960. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9961. "Fast tag selection with single keys.
  9962. CURRENT is the current list of tags in the headline, INHERITED is the
  9963. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9964. possibly with grouping information. TODO-TABLE is a similar table with
  9965. TODO keywords, should these have keys assigned to them.
  9966. If the keys are nil, a-z are automatically assigned.
  9967. Returns the new tags string, or nil to not change the current settings."
  9968. (let* ((fulltable (append table todo-table))
  9969. (maxlen (apply 'max (mapcar
  9970. (lambda (x)
  9971. (if (stringp (car x)) (string-width (car x)) 0))
  9972. fulltable)))
  9973. (buf (current-buffer))
  9974. (expert (eq org-fast-tag-selection-single-key 'expert))
  9975. (buffer-tags nil)
  9976. (fwidth (+ maxlen 3 1 3))
  9977. (ncol (/ (- (window-width) 4) fwidth))
  9978. (i-face 'org-done)
  9979. (c-face 'org-todo)
  9980. tg cnt e c char c1 c2 ntable tbl rtn
  9981. ov-start ov-end ov-prefix
  9982. (exit-after-next org-fast-tag-selection-single-key)
  9983. (done-keywords org-done-keywords)
  9984. groups ingroup)
  9985. (save-excursion
  9986. (beginning-of-line 1)
  9987. (if (looking-at
  9988. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9989. (setq ov-start (match-beginning 1)
  9990. ov-end (match-end 1)
  9991. ov-prefix "")
  9992. (setq ov-start (1- (point-at-eol))
  9993. ov-end (1+ ov-start))
  9994. (skip-chars-forward "^\n\r")
  9995. (setq ov-prefix
  9996. (concat
  9997. (buffer-substring (1- (point)) (point))
  9998. (if (> (current-column) org-tags-column)
  9999. " "
  10000. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10001. (org-move-overlay org-tags-overlay ov-start ov-end)
  10002. (save-window-excursion
  10003. (if expert
  10004. (set-buffer (get-buffer-create " *Org tags*"))
  10005. (delete-other-windows)
  10006. (split-window-vertically)
  10007. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10008. (erase-buffer)
  10009. (org-set-local 'org-done-keywords done-keywords)
  10010. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10011. (org-fast-tag-insert "Current" current c-face "\n\n")
  10012. (org-fast-tag-show-exit exit-after-next)
  10013. (org-set-current-tags-overlay current ov-prefix)
  10014. (setq tbl fulltable char ?a cnt 0)
  10015. (while (setq e (pop tbl))
  10016. (cond
  10017. ((equal e '(:startgroup))
  10018. (push '() groups) (setq ingroup t)
  10019. (when (not (= cnt 0))
  10020. (setq cnt 0)
  10021. (insert "\n"))
  10022. (insert "{ "))
  10023. ((equal e '(:endgroup))
  10024. (setq ingroup nil cnt 0)
  10025. (insert "}\n"))
  10026. ((equal e '(:newline))
  10027. (when (not (= cnt 0))
  10028. (setq cnt 0)
  10029. (insert "\n")
  10030. (setq e (car tbl))
  10031. (while (equal (car tbl) '(:newline))
  10032. (insert "\n")
  10033. (setq tbl (cdr tbl)))))
  10034. (t
  10035. (setq tg (car e) c2 nil)
  10036. (if (cdr e)
  10037. (setq c (cdr e))
  10038. ;; automatically assign a character.
  10039. (setq c1 (string-to-char
  10040. (downcase (substring
  10041. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10042. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10043. (while (or (rassoc char ntable) (rassoc char table))
  10044. (setq char (1+ char)))
  10045. (setq c2 c1))
  10046. (setq c (or c2 char)))
  10047. (if ingroup (push tg (car groups)))
  10048. (setq tg (org-add-props tg nil 'face
  10049. (cond
  10050. ((not (assoc tg table))
  10051. (org-get-todo-face tg))
  10052. ((member tg current) c-face)
  10053. ((member tg inherited) i-face)
  10054. (t nil))))
  10055. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10056. (insert "[" c "] " tg (make-string
  10057. (- fwidth 4 (length tg)) ?\ ))
  10058. (push (cons tg c) ntable)
  10059. (when (= (setq cnt (1+ cnt)) ncol)
  10060. (insert "\n")
  10061. (if ingroup (insert " "))
  10062. (setq cnt 0)))))
  10063. (setq ntable (nreverse ntable))
  10064. (insert "\n")
  10065. (goto-char (point-min))
  10066. (if (not expert) (org-fit-window-to-buffer))
  10067. (setq rtn
  10068. (catch 'exit
  10069. (while t
  10070. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10071. (if groups " [!] no groups" " [!]groups")
  10072. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10073. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10074. (cond
  10075. ((= c ?\r) (throw 'exit t))
  10076. ((= c ?!)
  10077. (setq groups (not groups))
  10078. (goto-char (point-min))
  10079. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10080. ((= c ?\C-c)
  10081. (if (not expert)
  10082. (org-fast-tag-show-exit
  10083. (setq exit-after-next (not exit-after-next)))
  10084. (setq expert nil)
  10085. (delete-other-windows)
  10086. (split-window-vertically)
  10087. (org-switch-to-buffer-other-window " *Org tags*")
  10088. (org-fit-window-to-buffer)))
  10089. ((or (= c ?\C-g)
  10090. (and (= c ?q) (not (rassoc c ntable))))
  10091. (org-detach-overlay org-tags-overlay)
  10092. (setq quit-flag t))
  10093. ((= c ?\ )
  10094. (setq current nil)
  10095. (if exit-after-next (setq exit-after-next 'now)))
  10096. ((= c ?\t)
  10097. (condition-case nil
  10098. (setq tg (org-ido-completing-read
  10099. "Tag: "
  10100. (or buffer-tags
  10101. (with-current-buffer buf
  10102. (org-get-buffer-tags)))))
  10103. (quit (setq tg "")))
  10104. (when (string-match "\\S-" tg)
  10105. (add-to-list 'buffer-tags (list tg))
  10106. (if (member tg current)
  10107. (setq current (delete tg current))
  10108. (push tg current)))
  10109. (if exit-after-next (setq exit-after-next 'now)))
  10110. ((setq e (rassoc c todo-table) tg (car e))
  10111. (with-current-buffer buf
  10112. (save-excursion (org-todo tg)))
  10113. (if exit-after-next (setq exit-after-next 'now)))
  10114. ((setq e (rassoc c ntable) tg (car e))
  10115. (if (member tg current)
  10116. (setq current (delete tg current))
  10117. (loop for g in groups do
  10118. (if (member tg g)
  10119. (mapc (lambda (x)
  10120. (setq current (delete x current)))
  10121. g)))
  10122. (push tg current))
  10123. (if exit-after-next (setq exit-after-next 'now))))
  10124. ;; Create a sorted list
  10125. (setq current
  10126. (sort current
  10127. (lambda (a b)
  10128. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10129. (if (eq exit-after-next 'now) (throw 'exit t))
  10130. (goto-char (point-min))
  10131. (beginning-of-line 2)
  10132. (delete-region (point) (point-at-eol))
  10133. (org-fast-tag-insert "Current" current c-face)
  10134. (org-set-current-tags-overlay current ov-prefix)
  10135. (while (re-search-forward
  10136. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10137. (setq tg (match-string 1))
  10138. (add-text-properties
  10139. (match-beginning 1) (match-end 1)
  10140. (list 'face
  10141. (cond
  10142. ((member tg current) c-face)
  10143. ((member tg inherited) i-face)
  10144. (t (get-text-property (match-beginning 1) 'face))))))
  10145. (goto-char (point-min)))))
  10146. (org-detach-overlay org-tags-overlay)
  10147. (if rtn
  10148. (mapconcat 'identity current ":")
  10149. nil))))
  10150. (defun org-get-tags-string ()
  10151. "Get the TAGS string in the current headline."
  10152. (unless (org-on-heading-p t)
  10153. (error "Not on a heading"))
  10154. (save-excursion
  10155. (beginning-of-line 1)
  10156. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10157. (org-match-string-no-properties 1)
  10158. "")))
  10159. (defun org-get-tags ()
  10160. "Get the list of tags specified in the current headline."
  10161. (org-split-string (org-get-tags-string) ":"))
  10162. (defun org-get-buffer-tags ()
  10163. "Get a table of all tags used in the buffer, for completion."
  10164. (let (tags)
  10165. (save-excursion
  10166. (goto-char (point-min))
  10167. (while (re-search-forward
  10168. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10169. (when (equal (char-after (point-at-bol 0)) ?*)
  10170. (mapc (lambda (x) (add-to-list 'tags x))
  10171. (org-split-string (org-match-string-no-properties 1) ":")))))
  10172. (mapcar 'list tags)))
  10173. ;;;; The mapping API
  10174. ;;;###autoload
  10175. (defun org-map-entries (func &optional match scope &rest skip)
  10176. "Call FUNC at each headline selected by MATCH in SCOPE.
  10177. FUNC is a function or a lisp form. The function will be called without
  10178. arguments, with the cursor positioned at the beginning of the headline.
  10179. The return values of all calls to the function will be collected and
  10180. returned as a list.
  10181. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10182. does not need to preserve point. After evaluaton, the cursor will be
  10183. moved to the end of the line (presumably of the headline of the
  10184. processed entry) and search continues from there. Under some
  10185. circumstances, this may not produce the wanted results. For example,
  10186. if you have removed (e.g. archived) the current (sub)tree it could
  10187. mean that the next entry will be skipped entirely. In such cases, you
  10188. can specify the position from where search should continue by making
  10189. FUNC set the variable `org-map-continue-from' to the desired buffer
  10190. position.
  10191. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10192. Only headlines that are matched by this query will be considered during
  10193. the iteration. When MATCH is nil or t, all headlines will be
  10194. visited by the iteration.
  10195. SCOPE determines the scope of this command. It can be any of:
  10196. nil The current buffer, respecting the restriction if any
  10197. tree The subtree started with the entry at point
  10198. file The current buffer, without restriction
  10199. file-with-archives
  10200. The current buffer, and any archives associated with it
  10201. agenda All agenda files
  10202. agenda-with-archives
  10203. All agenda files with any archive files associated with them
  10204. \(file1 file2 ...)
  10205. If this is a list, all files in the list will be scanned
  10206. The remaining args are treated as settings for the skipping facilities of
  10207. the scanner. The following items can be given here:
  10208. archive skip trees with the archive tag.
  10209. comment skip trees with the COMMENT keyword
  10210. function or Emacs Lisp form:
  10211. will be used as value for `org-agenda-skip-function', so whenever
  10212. the the function returns t, FUNC will not be called for that
  10213. entry and search will continue from the point where the
  10214. function leaves it.
  10215. If your function needs to retrieve the tags including inherited tags
  10216. at the *current* entry, you can use the value of the variable
  10217. `org-scanner-tags' which will be much faster than getting the value
  10218. with `org-get-tags-at'. If your function gets properties with
  10219. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10220. to t around the call to `org-entry-properties' to get the same speedup.
  10221. Note that if your function moves around to retrieve tags and properties at
  10222. a *different* entry, you cannot use these techniques."
  10223. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10224. (org-agenda-skip-archived-trees (memq 'archive skip))
  10225. (org-agenda-skip-comment-trees (memq 'comment skip))
  10226. (org-agenda-skip-function
  10227. (car (org-delete-all '(comment archive) skip)))
  10228. (org-tags-match-list-sublevels t)
  10229. matcher file res
  10230. org-todo-keywords-for-agenda
  10231. org-done-keywords-for-agenda
  10232. org-todo-keyword-alist-for-agenda
  10233. org-tag-alist-for-agenda)
  10234. (cond
  10235. ((eq match t) (setq matcher t))
  10236. ((eq match nil) (setq matcher t))
  10237. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10238. (save-excursion
  10239. (save-restriction
  10240. (when (eq scope 'tree)
  10241. (org-back-to-heading t)
  10242. (org-narrow-to-subtree)
  10243. (setq scope nil))
  10244. (if (not scope)
  10245. (progn
  10246. (org-prepare-agenda-buffers
  10247. (list (buffer-file-name (current-buffer))))
  10248. (setq res (org-scan-tags func matcher)))
  10249. ;; Get the right scope
  10250. (cond
  10251. ((and scope (listp scope) (symbolp (car scope)))
  10252. (setq scope (eval scope)))
  10253. ((eq scope 'agenda)
  10254. (setq scope (org-agenda-files t)))
  10255. ((eq scope 'agenda-with-archives)
  10256. (setq scope (org-agenda-files t))
  10257. (setq scope (org-add-archive-files scope)))
  10258. ((eq scope 'file)
  10259. (setq scope (list (buffer-file-name))))
  10260. ((eq scope 'file-with-archives)
  10261. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10262. (org-prepare-agenda-buffers scope)
  10263. (while (setq file (pop scope))
  10264. (with-current-buffer (org-find-base-buffer-visiting file)
  10265. (save-excursion
  10266. (save-restriction
  10267. (widen)
  10268. (goto-char (point-min))
  10269. (setq res (append res (org-scan-tags func matcher))))))))))
  10270. res))
  10271. ;;;; Properties
  10272. ;;; Setting and retrieving properties
  10273. (defconst org-special-properties
  10274. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10275. "TIMESTAMP" "TIMESTAMP_IA")
  10276. "The special properties valid in Org-mode.
  10277. These are properties that are not defined in the property drawer,
  10278. but in some other way.")
  10279. (defconst org-default-properties
  10280. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10281. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10282. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10283. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10284. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10285. "CLOCK_MODELINE_TOTAL")
  10286. "Some properties that are used by Org-mode for various purposes.
  10287. Being in this list makes sure that they are offered for completion.")
  10288. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10289. "Regular expression matching the first line of a property drawer.")
  10290. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10291. "Regular expression matching the first line of a property drawer.")
  10292. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10293. "Regular expression matching the first line of a property drawer.")
  10294. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10295. "Regular expression matching the first line of a property drawer.")
  10296. (defconst org-property-drawer-re
  10297. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10298. org-property-end-re "\\)\n?")
  10299. "Matches an entire property drawer.")
  10300. (defconst org-clock-drawer-re
  10301. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10302. org-property-end-re "\\)\n?")
  10303. "Matches an entire clock drawer.")
  10304. (defun org-property-action ()
  10305. "Do an action on properties."
  10306. (interactive)
  10307. (let (c)
  10308. (org-at-property-p)
  10309. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10310. (setq c (read-char-exclusive))
  10311. (cond
  10312. ((equal c ?s)
  10313. (call-interactively 'org-set-property))
  10314. ((equal c ?d)
  10315. (call-interactively 'org-delete-property))
  10316. ((equal c ?D)
  10317. (call-interactively 'org-delete-property-globally))
  10318. ((equal c ?c)
  10319. (call-interactively 'org-compute-property-at-point))
  10320. (t (error "No such property action %c" c)))))
  10321. (defun org-at-property-p ()
  10322. "Is the cursor in a property line?"
  10323. ;; FIXME: Does not check if we are actually in the drawer.
  10324. ;; FIXME: also returns true on any drawers.....
  10325. ;; This is used by C-c C-c for property action.
  10326. (save-excursion
  10327. (beginning-of-line 1)
  10328. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10329. (defun org-get-property-block (&optional beg end force)
  10330. "Return the (beg . end) range of the body of the property drawer.
  10331. BEG and END can be beginning and end of subtree, if not given
  10332. they will be found.
  10333. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10334. (catch 'exit
  10335. (save-excursion
  10336. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10337. (end (or end (progn (outline-next-heading) (point)))))
  10338. (goto-char beg)
  10339. (if (re-search-forward org-property-start-re end t)
  10340. (setq beg (1+ (match-end 0)))
  10341. (if force
  10342. (save-excursion
  10343. (org-insert-property-drawer)
  10344. (setq end (progn (outline-next-heading) (point))))
  10345. (throw 'exit nil))
  10346. (goto-char beg)
  10347. (if (re-search-forward org-property-start-re end t)
  10348. (setq beg (1+ (match-end 0)))))
  10349. (if (re-search-forward org-property-end-re end t)
  10350. (setq end (match-beginning 0))
  10351. (or force (throw 'exit nil))
  10352. (goto-char beg)
  10353. (setq end beg)
  10354. (org-indent-line-function)
  10355. (insert ":END:\n"))
  10356. (cons beg end)))))
  10357. (defun org-entry-properties (&optional pom which)
  10358. "Get all properties of the entry at point-or-marker POM.
  10359. This includes the TODO keyword, the tags, time strings for deadline,
  10360. scheduled, and clocking, and any additional properties defined in the
  10361. entry. The return value is an alist, keys may occur multiple times
  10362. if the property key was used several times.
  10363. POM may also be nil, in which case the current entry is used.
  10364. If WHICH is nil or `all', get all properties. If WHICH is
  10365. `special' or `standard', only get that subclass."
  10366. (setq which (or which 'all))
  10367. (org-with-point-at pom
  10368. (let ((clockstr (substring org-clock-string 0 -1))
  10369. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10370. beg end range props sum-props key value string clocksum)
  10371. (save-excursion
  10372. (when (condition-case nil
  10373. (and (org-mode-p) (org-back-to-heading t))
  10374. (error nil))
  10375. (setq beg (point))
  10376. (setq sum-props (get-text-property (point) 'org-summaries))
  10377. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10378. (outline-next-heading)
  10379. (setq end (point))
  10380. (when (memq which '(all special))
  10381. ;; Get the special properties, like TODO and tags
  10382. (goto-char beg)
  10383. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10384. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10385. (when (looking-at org-priority-regexp)
  10386. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10387. (when (and (setq value (org-get-tags-string))
  10388. (string-match "\\S-" value))
  10389. (push (cons "TAGS" value) props))
  10390. (when (setq value (org-get-tags-at))
  10391. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10392. props))
  10393. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10394. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10395. string (if (equal key clockstr)
  10396. (org-no-properties
  10397. (org-trim
  10398. (buffer-substring
  10399. (match-beginning 3) (goto-char (point-at-eol)))))
  10400. (substring (org-match-string-no-properties 3) 1 -1)))
  10401. (unless key
  10402. (if (= (char-after (match-beginning 3)) ?\[)
  10403. (setq key "TIMESTAMP_IA")
  10404. (setq key "TIMESTAMP")))
  10405. (when (or (equal key clockstr) (not (assoc key props)))
  10406. (push (cons key string) props)))
  10407. )
  10408. (when (memq which '(all standard))
  10409. ;; Get the standard properties, like :PROP: ...
  10410. (setq range (org-get-property-block beg end))
  10411. (when range
  10412. (goto-char (car range))
  10413. (while (re-search-forward
  10414. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10415. (cdr range) t)
  10416. (setq key (org-match-string-no-properties 1)
  10417. value (org-trim (or (org-match-string-no-properties 2) "")))
  10418. (unless (member key excluded)
  10419. (push (cons key (or value "")) props)))))
  10420. (if clocksum
  10421. (push (cons "CLOCKSUM"
  10422. (org-columns-number-to-string (/ (float clocksum) 60.)
  10423. 'add_times))
  10424. props))
  10425. (unless (assoc "CATEGORY" props)
  10426. (setq value (or (org-get-category)
  10427. (progn (org-refresh-category-properties)
  10428. (org-get-category))))
  10429. (push (cons "CATEGORY" value) props))
  10430. (append sum-props (nreverse props)))))))
  10431. (defun org-entry-get (pom property &optional inherit)
  10432. "Get value of PROPERTY for entry at point-or-marker POM.
  10433. If INHERIT is non-nil and the entry does not have the property,
  10434. then also check higher levels of the hierarchy.
  10435. If INHERIT is the symbol `selective', use inheritance only if the setting
  10436. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10437. If the property is present but empty, the return value is the empty string.
  10438. If the property is not present at all, nil is returned."
  10439. (org-with-point-at pom
  10440. (if (and inherit (if (eq inherit 'selective)
  10441. (org-property-inherit-p property)
  10442. t))
  10443. (org-entry-get-with-inheritance property)
  10444. (if (member property org-special-properties)
  10445. ;; We need a special property. Use brute force, get all properties.
  10446. (cdr (assoc property (org-entry-properties nil 'special)))
  10447. (let ((range (org-get-property-block)))
  10448. (if (and range
  10449. (goto-char (car range))
  10450. (re-search-forward
  10451. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10452. (cdr range) t))
  10453. ;; Found the property, return it.
  10454. (if (match-end 1)
  10455. (org-match-string-no-properties 1)
  10456. "")))))))
  10457. (defun org-property-or-variable-value (var &optional inherit)
  10458. "Check if there is a property fixing the value of VAR.
  10459. If yes, return this value. If not, return the current value of the variable."
  10460. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10461. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10462. (read prop)
  10463. (symbol-value var))))
  10464. (defun org-entry-delete (pom property)
  10465. "Delete the property PROPERTY from entry at point-or-marker POM."
  10466. (org-with-point-at pom
  10467. (if (member property org-special-properties)
  10468. nil ; cannot delete these properties.
  10469. (let ((range (org-get-property-block)))
  10470. (if (and range
  10471. (goto-char (car range))
  10472. (re-search-forward
  10473. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10474. (cdr range) t))
  10475. (progn
  10476. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10477. t)
  10478. nil)))))
  10479. ;; Multi-values properties are properties that contain multiple values
  10480. ;; These values are assumed to be single words, separated by whitespace.
  10481. (defun org-entry-add-to-multivalued-property (pom property value)
  10482. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10483. (let* ((old (org-entry-get pom property))
  10484. (values (and old (org-split-string old "[ \t]"))))
  10485. (setq value (org-entry-protect-space value))
  10486. (unless (member value values)
  10487. (setq values (cons value values))
  10488. (org-entry-put pom property
  10489. (mapconcat 'identity values " ")))))
  10490. (defun org-entry-remove-from-multivalued-property (pom property value)
  10491. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10492. (let* ((old (org-entry-get pom property))
  10493. (values (and old (org-split-string old "[ \t]"))))
  10494. (setq value (org-entry-protect-space value))
  10495. (when (member value values)
  10496. (setq values (delete value values))
  10497. (org-entry-put pom property
  10498. (mapconcat 'identity values " ")))))
  10499. (defun org-entry-member-in-multivalued-property (pom property value)
  10500. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10501. (let* ((old (org-entry-get pom property))
  10502. (values (and old (org-split-string old "[ \t]"))))
  10503. (setq value (org-entry-protect-space value))
  10504. (member value values)))
  10505. (defun org-entry-get-multivalued-property (pom property)
  10506. "Return a list of values in a multivalued property."
  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-put-multivalued-property (pom property &rest values)
  10511. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10512. VALUES should be a list of strings. Spaces will be protected."
  10513. (org-entry-put pom property
  10514. (mapconcat 'org-entry-protect-space values " "))
  10515. (let* ((value (org-entry-get pom property))
  10516. (values (and value (org-split-string value "[ \t]"))))
  10517. (mapcar 'org-entry-restore-space values)))
  10518. (defun org-entry-protect-space (s)
  10519. "Protect spaces and newline in string S."
  10520. (while (string-match " " s)
  10521. (setq s (replace-match "%20" t t s)))
  10522. (while (string-match "\n" s)
  10523. (setq s (replace-match "%0A" t t s)))
  10524. s)
  10525. (defun org-entry-restore-space (s)
  10526. "Restore spaces and newline in string S."
  10527. (while (string-match "%20" s)
  10528. (setq s (replace-match " " t t s)))
  10529. (while (string-match "%0A" s)
  10530. (setq s (replace-match "\n" t t s)))
  10531. s)
  10532. (defvar org-entry-property-inherited-from (make-marker)
  10533. "Marker pointing to the entry from where a property was inherited.
  10534. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10535. location of the entry where the inheritance search matched. If there was
  10536. no match, the marker will point nowhere.
  10537. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10538. is set.")
  10539. (defun org-entry-get-with-inheritance (property)
  10540. "Get entry property, and search higher levels if not present."
  10541. (move-marker org-entry-property-inherited-from nil)
  10542. (let (tmp)
  10543. (save-excursion
  10544. (save-restriction
  10545. (widen)
  10546. (catch 'ex
  10547. (while t
  10548. (when (setq tmp (org-entry-get nil property))
  10549. (org-back-to-heading t)
  10550. (move-marker org-entry-property-inherited-from (point))
  10551. (throw 'ex tmp))
  10552. (or (org-up-heading-safe) (throw 'ex nil)))))
  10553. (or tmp
  10554. (cdr (assoc property org-file-properties))
  10555. (cdr (assoc property org-global-properties))
  10556. (cdr (assoc property org-global-properties-fixed))))))
  10557. (defun org-entry-put (pom property value)
  10558. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10559. (org-with-point-at pom
  10560. (org-back-to-heading t)
  10561. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10562. range)
  10563. (cond
  10564. ((equal property "TODO")
  10565. (when (and (stringp value) (string-match "\\S-" value)
  10566. (not (member value org-todo-keywords-1)))
  10567. (error "\"%s\" is not a valid TODO state" value))
  10568. (if (or (not value)
  10569. (not (string-match "\\S-" value)))
  10570. (setq value 'none))
  10571. (org-todo value)
  10572. (org-set-tags nil 'align))
  10573. ((equal property "PRIORITY")
  10574. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10575. (string-to-char value) ?\ ))
  10576. (org-set-tags nil 'align))
  10577. ((equal property "SCHEDULED")
  10578. (if (re-search-forward org-scheduled-time-regexp end t)
  10579. (cond
  10580. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10581. ((eq value 'later) (org-timestamp-change 1 'day))
  10582. (t (call-interactively 'org-schedule)))
  10583. (call-interactively 'org-schedule)))
  10584. ((equal property "DEADLINE")
  10585. (if (re-search-forward org-deadline-time-regexp end t)
  10586. (cond
  10587. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10588. ((eq value 'later) (org-timestamp-change 1 'day))
  10589. (t (call-interactively 'org-deadline)))
  10590. (call-interactively 'org-deadline)))
  10591. ((member property org-special-properties)
  10592. (error "The %s property can not yet be set with `org-entry-put'"
  10593. property))
  10594. (t ; a non-special property
  10595. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10596. (setq range (org-get-property-block beg end 'force))
  10597. (goto-char (car range))
  10598. (if (re-search-forward
  10599. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10600. (progn
  10601. (delete-region (match-beginning 1) (match-end 1))
  10602. (goto-char (match-beginning 1)))
  10603. (goto-char (cdr range))
  10604. (insert "\n")
  10605. (backward-char 1)
  10606. (org-indent-line-function)
  10607. (insert ":" property ":"))
  10608. (and value (insert " " value))
  10609. (org-indent-line-function)))))))
  10610. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10611. "Get all property keys in the current buffer.
  10612. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10613. like tags and TODO state.
  10614. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10615. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10616. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10617. formats in the current buffer."
  10618. (let (rtn range cfmt s p)
  10619. (save-excursion
  10620. (save-restriction
  10621. (widen)
  10622. (goto-char (point-min))
  10623. (while (re-search-forward org-property-start-re nil t)
  10624. (setq range (org-get-property-block))
  10625. (goto-char (car range))
  10626. (while (re-search-forward
  10627. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10628. (cdr range) t)
  10629. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10630. (outline-next-heading))))
  10631. (when include-specials
  10632. (setq rtn (append org-special-properties rtn)))
  10633. (when include-defaults
  10634. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10635. (add-to-list 'rtn org-effort-property))
  10636. (when include-columns
  10637. (save-excursion
  10638. (save-restriction
  10639. (widen)
  10640. (goto-char (point-min))
  10641. (while (re-search-forward
  10642. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10643. nil t)
  10644. (setq cfmt (match-string 2) s 0)
  10645. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10646. cfmt s)
  10647. (setq s (match-end 0)
  10648. p (match-string 1 cfmt))
  10649. (unless (or (equal p "ITEM")
  10650. (member p org-special-properties))
  10651. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10652. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10653. (defun org-property-values (key)
  10654. "Return a list of all values of property KEY."
  10655. (save-excursion
  10656. (save-restriction
  10657. (widen)
  10658. (goto-char (point-min))
  10659. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10660. values)
  10661. (while (re-search-forward re nil t)
  10662. (add-to-list 'values (org-trim (match-string 1))))
  10663. (delete "" values)))))
  10664. (defun org-insert-property-drawer ()
  10665. "Insert a property drawer into the current entry."
  10666. (interactive)
  10667. (org-back-to-heading t)
  10668. (looking-at outline-regexp)
  10669. (let ((indent (if org-adapt-indentation
  10670. (- (match-end 0)(match-beginning 0))
  10671. 0))
  10672. (beg (point))
  10673. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10674. end hiddenp)
  10675. (outline-next-heading)
  10676. (setq end (point))
  10677. (goto-char beg)
  10678. (while (re-search-forward re end t))
  10679. (setq hiddenp (org-invisible-p))
  10680. (end-of-line 1)
  10681. (and (equal (char-after) ?\n) (forward-char 1))
  10682. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10683. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10684. ;; just skip this line
  10685. (beginning-of-line 2)
  10686. ;; Drawer start, find the end
  10687. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10688. (beginning-of-line 1)))
  10689. (org-skip-over-state-notes)
  10690. (skip-chars-backward " \t\n\r")
  10691. (if (eq (char-before) ?*) (forward-char 1))
  10692. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10693. (beginning-of-line 0)
  10694. (org-indent-to-column indent)
  10695. (beginning-of-line 2)
  10696. (org-indent-to-column indent)
  10697. (beginning-of-line 0)
  10698. (if hiddenp
  10699. (save-excursion
  10700. (org-back-to-heading t)
  10701. (hide-entry))
  10702. (org-flag-drawer t))))
  10703. (defun org-set-property (property value)
  10704. "In the current entry, set PROPERTY to VALUE.
  10705. When called interactively, this will prompt for a property name, offering
  10706. completion on existing and default properties. And then it will prompt
  10707. for a value, offering completion either on allowed values (via an inherited
  10708. xxx_ALL property) or on existing values in other instances of this property
  10709. in the current file."
  10710. (interactive
  10711. (let* ((completion-ignore-case t)
  10712. (keys (org-buffer-property-keys nil t t))
  10713. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10714. (prop (if (member prop0 keys)
  10715. prop0
  10716. (or (cdr (assoc (downcase prop0)
  10717. (mapcar (lambda (x) (cons (downcase x) x))
  10718. keys)))
  10719. prop0)))
  10720. (cur (org-entry-get nil prop))
  10721. (allowed (org-property-get-allowed-values nil prop 'table))
  10722. (existing (mapcar 'list (org-property-values prop)))
  10723. (val (if allowed
  10724. (org-completing-read "Value: " allowed nil 'req-match)
  10725. (let (org-completion-use-ido)
  10726. (org-completing-read
  10727. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10728. (concat "[" cur "]") "")
  10729. ": ")
  10730. existing nil nil "" nil cur)))))
  10731. (list prop (if (equal val "") cur val))))
  10732. (unless (equal (org-entry-get nil property) value)
  10733. (org-entry-put nil property value)))
  10734. (defun org-delete-property (property)
  10735. "In the current entry, delete PROPERTY."
  10736. (interactive
  10737. (let* ((completion-ignore-case t)
  10738. (prop (org-ido-completing-read
  10739. "Property: " (org-entry-properties nil 'standard))))
  10740. (list prop)))
  10741. (message "Property %s %s" property
  10742. (if (org-entry-delete nil property)
  10743. "deleted"
  10744. "was not present in the entry")))
  10745. (defun org-delete-property-globally (property)
  10746. "Remove PROPERTY globally, from all entries."
  10747. (interactive
  10748. (let* ((completion-ignore-case t)
  10749. (prop (org-ido-completing-read
  10750. "Globally remove property: "
  10751. (mapcar 'list (org-buffer-property-keys)))))
  10752. (list prop)))
  10753. (save-excursion
  10754. (save-restriction
  10755. (widen)
  10756. (goto-char (point-min))
  10757. (let ((cnt 0))
  10758. (while (re-search-forward
  10759. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10760. nil t)
  10761. (setq cnt (1+ cnt))
  10762. (replace-match ""))
  10763. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10764. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10765. (defun org-compute-property-at-point ()
  10766. "Compute the property at point.
  10767. This looks for an enclosing column format, extracts the operator and
  10768. then applies it to the property in the column format's scope."
  10769. (interactive)
  10770. (unless (org-at-property-p)
  10771. (error "Not at a property"))
  10772. (let ((prop (org-match-string-no-properties 2)))
  10773. (org-columns-get-format-and-top-level)
  10774. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10775. (error "No operator defined for property %s" prop))
  10776. (org-columns-compute prop)))
  10777. (defun org-property-get-allowed-values (pom property &optional table)
  10778. "Get allowed values for the property PROPERTY.
  10779. When TABLE is non-nil, return an alist that can directly be used for
  10780. completion."
  10781. (let (vals)
  10782. (cond
  10783. ((equal property "TODO")
  10784. (setq vals (org-with-point-at pom
  10785. (append org-todo-keywords-1 '("")))))
  10786. ((equal property "PRIORITY")
  10787. (let ((n org-lowest-priority))
  10788. (while (>= n org-highest-priority)
  10789. (push (char-to-string n) vals)
  10790. (setq n (1- n)))))
  10791. ((member property org-special-properties))
  10792. (t
  10793. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10794. (when (and vals (string-match "\\S-" vals))
  10795. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10796. (setq vals (mapcar (lambda (x)
  10797. (cond ((stringp x) x)
  10798. ((numberp x) (number-to-string x))
  10799. ((symbolp x) (symbol-name x))
  10800. (t "???")))
  10801. vals)))))
  10802. (if table (mapcar 'list vals) vals)))
  10803. (defun org-property-previous-allowed-value (&optional previous)
  10804. "Switch to the next allowed value for this property."
  10805. (interactive)
  10806. (org-property-next-allowed-value t))
  10807. (defun org-property-next-allowed-value (&optional previous)
  10808. "Switch to the next allowed value for this property."
  10809. (interactive)
  10810. (unless (org-at-property-p)
  10811. (error "Not at a property"))
  10812. (let* ((key (match-string 2))
  10813. (value (match-string 3))
  10814. (allowed (or (org-property-get-allowed-values (point) key)
  10815. (and (member value '("[ ]" "[-]" "[X]"))
  10816. '("[ ]" "[X]"))))
  10817. nval)
  10818. (unless allowed
  10819. (error "Allowed values for this property have not been defined"))
  10820. (if previous (setq allowed (reverse allowed)))
  10821. (if (member value allowed)
  10822. (setq nval (car (cdr (member value allowed)))))
  10823. (setq nval (or nval (car allowed)))
  10824. (if (equal nval value)
  10825. (error "Only one allowed value for this property"))
  10826. (org-at-property-p)
  10827. (replace-match (concat " :" key ": " nval) t t)
  10828. (org-indent-line-function)
  10829. (beginning-of-line 1)
  10830. (skip-chars-forward " \t")))
  10831. (defun org-find-entry-with-id (ident)
  10832. "Locate the entry that contains the ID property with exact value IDENT.
  10833. IDENT can be a string, a symbol or a number, this function will search for
  10834. the string representation of it.
  10835. Return the position where this entry starts, or nil if there is no such entry."
  10836. (interactive "sID: ")
  10837. (let ((id (cond
  10838. ((stringp ident) ident)
  10839. ((symbol-name ident) (symbol-name ident))
  10840. ((numberp ident) (number-to-string ident))
  10841. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10842. (case-fold-search nil))
  10843. (save-excursion
  10844. (save-restriction
  10845. (widen)
  10846. (goto-char (point-min))
  10847. (when (re-search-forward
  10848. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10849. nil t)
  10850. (org-back-to-heading)
  10851. (point))))))
  10852. ;;;; Timestamps
  10853. (defvar org-last-changed-timestamp nil)
  10854. (defvar org-last-inserted-timestamp nil
  10855. "The last time stamp inserted with `org-insert-time-stamp'.")
  10856. (defvar org-time-was-given) ; dynamically scoped parameter
  10857. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10858. (defvar org-ts-what) ; dynamically scoped parameter
  10859. (defun org-time-stamp (arg &optional inactive)
  10860. "Prompt for a date/time and insert a time stamp.
  10861. If the user specifies a time like HH:MM, or if this command is called
  10862. with a prefix argument, the time stamp will contain date and time.
  10863. Otherwise, only the date will be included. All parts of a date not
  10864. specified by the user will be filled in from the current date/time.
  10865. So if you press just return without typing anything, the time stamp
  10866. will represent the current date/time. If there is already a timestamp
  10867. at the cursor, it will be modified."
  10868. (interactive "P")
  10869. (let* ((ts nil)
  10870. (default-time
  10871. ;; Default time is either today, or, when entering a range,
  10872. ;; the range start.
  10873. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10874. (save-excursion
  10875. (re-search-backward
  10876. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10877. (- (point) 20) t)))
  10878. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10879. (current-time)))
  10880. (default-input (and ts (org-get-compact-tod ts)))
  10881. org-time-was-given org-end-time-was-given time)
  10882. (cond
  10883. ((and (org-at-timestamp-p t)
  10884. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10885. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10886. (insert "--")
  10887. (setq time (let ((this-command this-command))
  10888. (org-read-date arg 'totime nil nil
  10889. default-time default-input)))
  10890. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10891. ((org-at-timestamp-p t)
  10892. (setq time (let ((this-command this-command))
  10893. (org-read-date arg 'totime nil nil default-time default-input)))
  10894. (when (org-at-timestamp-p t) ; just to get the match data
  10895. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10896. (replace-match "")
  10897. (setq org-last-changed-timestamp
  10898. (org-insert-time-stamp
  10899. time (or org-time-was-given arg)
  10900. inactive nil nil (list org-end-time-was-given))))
  10901. (message "Timestamp updated"))
  10902. (t
  10903. (setq time (let ((this-command this-command))
  10904. (org-read-date arg 'totime nil nil default-time default-input)))
  10905. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10906. nil nil (list org-end-time-was-given))))))
  10907. ;; FIXME: can we use this for something else, like computing time differences?
  10908. (defun org-get-compact-tod (s)
  10909. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10910. (let* ((t1 (match-string 1 s))
  10911. (h1 (string-to-number (match-string 2 s)))
  10912. (m1 (string-to-number (match-string 3 s)))
  10913. (t2 (and (match-end 4) (match-string 5 s)))
  10914. (h2 (and t2 (string-to-number (match-string 6 s))))
  10915. (m2 (and t2 (string-to-number (match-string 7 s))))
  10916. dh dm)
  10917. (if (not t2)
  10918. t1
  10919. (setq dh (- h2 h1) dm (- m2 m1))
  10920. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10921. (concat t1 "+" (number-to-string dh)
  10922. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10923. (defun org-time-stamp-inactive (&optional arg)
  10924. "Insert an inactive time stamp.
  10925. An inactive time stamp is enclosed in square brackets instead of angle
  10926. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10927. does not link to the calendar and cannot be changed with the S-cursor keys.
  10928. So these are more for recording a certain time/date."
  10929. (interactive "P")
  10930. (org-time-stamp arg 'inactive))
  10931. (defvar org-date-ovl (org-make-overlay 1 1))
  10932. (org-overlay-put org-date-ovl 'face 'org-warning)
  10933. (org-detach-overlay org-date-ovl)
  10934. (defvar org-ans1) ; dynamically scoped parameter
  10935. (defvar org-ans2) ; dynamically scoped parameter
  10936. (defvar org-plain-time-of-day-regexp) ; defined below
  10937. (defvar org-overriding-default-time nil) ; dynamically scoped
  10938. (defvar org-read-date-overlay nil)
  10939. (defvar org-dcst nil) ; dynamically scoped
  10940. (defvar org-read-date-history nil)
  10941. (defun org-read-date (&optional with-time to-time from-string prompt
  10942. default-time default-input)
  10943. "Read a date, possibly a time, and make things smooth for the user.
  10944. The prompt will suggest to enter an ISO date, but you can also enter anything
  10945. which will at least partially be understood by `parse-time-string'.
  10946. Unrecognized parts of the date will default to the current day, month, year,
  10947. hour and minute. If this command is called to replace a timestamp at point,
  10948. of to enter the second timestamp of a range, the default time is taken from the
  10949. existing stamp. For example,
  10950. 3-2-5 --> 2003-02-05
  10951. feb 15 --> currentyear-02-15
  10952. sep 12 9 --> 2009-09-12
  10953. 12:45 --> today 12:45
  10954. 22 sept 0:34 --> currentyear-09-22 0:34
  10955. 12 --> currentyear-currentmonth-12
  10956. Fri --> nearest Friday (today or later)
  10957. etc.
  10958. Furthermore you can specify a relative date by giving, as the *first* thing
  10959. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10960. change in days weeks, months, years.
  10961. With a single plus or minus, the date is relative to today. With a double
  10962. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10963. +4d --> four days from today
  10964. +4 --> same as above
  10965. +2w --> two weeks from today
  10966. ++5 --> five days from default date
  10967. The function understands only English month and weekday abbreviations,
  10968. but this can be configured with the variables `parse-time-months' and
  10969. `parse-time-weekdays'.
  10970. While prompting, a calendar is popped up - you can also select the
  10971. date with the mouse (button 1). The calendar shows a period of three
  10972. months. To scroll it to other months, use the keys `>' and `<'.
  10973. If you don't like the calendar, turn it off with
  10974. \(setq org-read-date-popup-calendar nil)
  10975. With optional argument TO-TIME, the date will immediately be converted
  10976. to an internal time.
  10977. With an optional argument WITH-TIME, the prompt will suggest to also
  10978. insert a time. Note that when WITH-TIME is not set, you can still
  10979. enter a time, and this function will inform the calling routine about
  10980. this change. The calling routine may then choose to change the format
  10981. used to insert the time stamp into the buffer to include the time.
  10982. With optional argument FROM-STRING, read from this string instead from
  10983. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10984. the time/date that is used for everything that is not specified by the
  10985. user."
  10986. (require 'parse-time)
  10987. (let* ((org-time-stamp-rounding-minutes
  10988. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10989. (org-dcst org-display-custom-times)
  10990. (ct (org-current-time))
  10991. (def (or org-overriding-default-time default-time ct))
  10992. (defdecode (decode-time def))
  10993. (dummy (progn
  10994. (when (< (nth 2 defdecode) org-extend-today-until)
  10995. (setcar (nthcdr 2 defdecode) -1)
  10996. (setcar (nthcdr 1 defdecode) 59)
  10997. (setq def (apply 'encode-time defdecode)
  10998. defdecode (decode-time def)))))
  10999. (calendar-move-hook nil)
  11000. (calendar-view-diary-initially-flag nil)
  11001. (view-diary-entries-initially nil)
  11002. (calendar-view-holidays-initially-flag nil)
  11003. (view-calendar-holidays-initially nil)
  11004. (timestr (format-time-string
  11005. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11006. (prompt (concat (if prompt (concat prompt " ") "")
  11007. (format "Date+time [%s]: " timestr)))
  11008. ans (org-ans0 "") org-ans1 org-ans2 final)
  11009. (cond
  11010. (from-string (setq ans from-string))
  11011. (org-read-date-popup-calendar
  11012. (save-excursion
  11013. (save-window-excursion
  11014. (calendar)
  11015. (calendar-forward-day (- (time-to-days def)
  11016. (calendar-absolute-from-gregorian
  11017. (calendar-current-date))))
  11018. (org-eval-in-calendar nil t)
  11019. (let* ((old-map (current-local-map))
  11020. (map (copy-keymap calendar-mode-map))
  11021. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11022. (org-defkey map (kbd "RET") 'org-calendar-select)
  11023. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11024. 'org-calendar-select-mouse)
  11025. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11026. 'org-calendar-select-mouse)
  11027. (org-defkey minibuffer-local-map [(meta shift left)]
  11028. (lambda () (interactive)
  11029. (org-eval-in-calendar '(calendar-backward-month 1))))
  11030. (org-defkey minibuffer-local-map [(meta shift right)]
  11031. (lambda () (interactive)
  11032. (org-eval-in-calendar '(calendar-forward-month 1))))
  11033. (org-defkey minibuffer-local-map [(meta shift up)]
  11034. (lambda () (interactive)
  11035. (org-eval-in-calendar '(calendar-backward-year 1))))
  11036. (org-defkey minibuffer-local-map [(meta shift down)]
  11037. (lambda () (interactive)
  11038. (org-eval-in-calendar '(calendar-forward-year 1))))
  11039. (org-defkey minibuffer-local-map [?\e (shift left)]
  11040. (lambda () (interactive)
  11041. (org-eval-in-calendar '(calendar-backward-month 1))))
  11042. (org-defkey minibuffer-local-map [?\e (shift right)]
  11043. (lambda () (interactive)
  11044. (org-eval-in-calendar '(calendar-forward-month 1))))
  11045. (org-defkey minibuffer-local-map [?\e (shift up)]
  11046. (lambda () (interactive)
  11047. (org-eval-in-calendar '(calendar-backward-year 1))))
  11048. (org-defkey minibuffer-local-map [?\e (shift down)]
  11049. (lambda () (interactive)
  11050. (org-eval-in-calendar '(calendar-forward-year 1))))
  11051. (org-defkey minibuffer-local-map [(shift up)]
  11052. (lambda () (interactive)
  11053. (org-eval-in-calendar '(calendar-backward-week 1))))
  11054. (org-defkey minibuffer-local-map [(shift down)]
  11055. (lambda () (interactive)
  11056. (org-eval-in-calendar '(calendar-forward-week 1))))
  11057. (org-defkey minibuffer-local-map [(shift left)]
  11058. (lambda () (interactive)
  11059. (org-eval-in-calendar '(calendar-backward-day 1))))
  11060. (org-defkey minibuffer-local-map [(shift right)]
  11061. (lambda () (interactive)
  11062. (org-eval-in-calendar '(calendar-forward-day 1))))
  11063. (org-defkey minibuffer-local-map ">"
  11064. (lambda () (interactive)
  11065. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11066. (org-defkey minibuffer-local-map "<"
  11067. (lambda () (interactive)
  11068. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11069. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11070. (unwind-protect
  11071. (progn
  11072. (use-local-map map)
  11073. (add-hook 'post-command-hook 'org-read-date-display)
  11074. (setq org-ans0 (read-string prompt default-input
  11075. 'org-read-date-history nil))
  11076. ;; org-ans0: from prompt
  11077. ;; org-ans1: from mouse click
  11078. ;; org-ans2: from calendar motion
  11079. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11080. (remove-hook 'post-command-hook 'org-read-date-display)
  11081. (use-local-map old-map)
  11082. (when org-read-date-overlay
  11083. (org-delete-overlay org-read-date-overlay)
  11084. (setq org-read-date-overlay nil)))))))
  11085. (t ; Naked prompt only
  11086. (unwind-protect
  11087. (setq ans (read-string prompt default-input
  11088. 'org-read-date-history timestr))
  11089. (when org-read-date-overlay
  11090. (org-delete-overlay org-read-date-overlay)
  11091. (setq org-read-date-overlay nil)))))
  11092. (setq final (org-read-date-analyze ans def defdecode))
  11093. (if to-time
  11094. (apply 'encode-time final)
  11095. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11096. (format "%04d-%02d-%02d %02d:%02d"
  11097. (nth 5 final) (nth 4 final) (nth 3 final)
  11098. (nth 2 final) (nth 1 final))
  11099. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11100. (defvar def)
  11101. (defvar defdecode)
  11102. (defvar with-time)
  11103. (defun org-read-date-display ()
  11104. "Display the current date prompt interpretation in the minibuffer."
  11105. (when org-read-date-display-live
  11106. (when org-read-date-overlay
  11107. (org-delete-overlay org-read-date-overlay))
  11108. (let ((p (point)))
  11109. (end-of-line 1)
  11110. (while (not (equal (buffer-substring
  11111. (max (point-min) (- (point) 4)) (point))
  11112. " "))
  11113. (insert " "))
  11114. (goto-char p))
  11115. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11116. " " (or org-ans1 org-ans2)))
  11117. (org-end-time-was-given nil)
  11118. (f (org-read-date-analyze ans def defdecode))
  11119. (fmts (if org-dcst
  11120. org-time-stamp-custom-formats
  11121. org-time-stamp-formats))
  11122. (fmt (if (or with-time
  11123. (and (boundp 'org-time-was-given) org-time-was-given))
  11124. (cdr fmts)
  11125. (car fmts)))
  11126. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11127. (when (and org-end-time-was-given
  11128. (string-match org-plain-time-of-day-regexp txt))
  11129. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11130. org-end-time-was-given
  11131. (substring txt (match-end 0)))))
  11132. (setq org-read-date-overlay
  11133. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11134. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11135. (defun org-read-date-analyze (ans def defdecode)
  11136. "Analyse the combined answer of the date prompt."
  11137. ;; FIXME: cleanup and comment
  11138. (let (delta deltan deltaw deltadef year month day
  11139. hour minute second wday pm h2 m2 tl wday1
  11140. iso-year iso-weekday iso-week iso-year iso-date)
  11141. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11142. (setq ans "+0"))
  11143. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11144. (setq ans (replace-match "" t t ans)
  11145. deltan (car delta)
  11146. deltaw (nth 1 delta)
  11147. deltadef (nth 2 delta)))
  11148. ;; Check if there is an iso week date in there
  11149. ;; If yes, sore the info and postpone interpreting it until the rest
  11150. ;; of the parsing is done
  11151. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11152. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11153. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11154. iso-week (string-to-number (match-string 2 ans)))
  11155. (setq ans (replace-match "" t t ans)))
  11156. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11157. (when (string-match
  11158. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11159. (setq year (if (match-end 2)
  11160. (string-to-number (match-string 2 ans))
  11161. (string-to-number (format-time-string "%Y")))
  11162. month (string-to-number (match-string 3 ans))
  11163. day (string-to-number (match-string 4 ans)))
  11164. (if (< year 100) (setq year (+ 2000 year)))
  11165. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11166. t nil ans)))
  11167. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11168. ;; If there is a time with am/pm, and *no* time without it, we convert
  11169. ;; so that matching will be successful.
  11170. (loop for i from 1 to 2 do ; twice, for end time as well
  11171. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11172. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11173. (setq hour (string-to-number (match-string 1 ans))
  11174. minute (if (match-end 3)
  11175. (string-to-number (match-string 3 ans))
  11176. 0)
  11177. pm (equal ?p
  11178. (string-to-char (downcase (match-string 4 ans)))))
  11179. (if (and (= hour 12) (not pm))
  11180. (setq hour 0)
  11181. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11182. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11183. t t ans))))
  11184. ;; Check if a time range is given as a duration
  11185. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11186. (setq hour (string-to-number (match-string 1 ans))
  11187. h2 (+ hour (string-to-number (match-string 3 ans)))
  11188. minute (string-to-number (match-string 2 ans))
  11189. m2 (+ minute (if (match-end 5) (string-to-number
  11190. (match-string 5 ans))0)))
  11191. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11192. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11193. t t ans)))
  11194. ;; Check if there is a time range
  11195. (when (boundp 'org-end-time-was-given)
  11196. (setq org-time-was-given nil)
  11197. (when (and (string-match org-plain-time-of-day-regexp ans)
  11198. (match-end 8))
  11199. (setq org-end-time-was-given (match-string 8 ans))
  11200. (setq ans (concat (substring ans 0 (match-beginning 7))
  11201. (substring ans (match-end 7))))))
  11202. (setq tl (parse-time-string ans)
  11203. day (or (nth 3 tl) (nth 3 defdecode))
  11204. month (or (nth 4 tl)
  11205. (if (and org-read-date-prefer-future
  11206. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11207. (1+ (nth 4 defdecode))
  11208. (nth 4 defdecode)))
  11209. year (or (nth 5 tl)
  11210. (if (and org-read-date-prefer-future
  11211. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11212. (1+ (nth 5 defdecode))
  11213. (nth 5 defdecode)))
  11214. hour (or (nth 2 tl) (nth 2 defdecode))
  11215. minute (or (nth 1 tl) (nth 1 defdecode))
  11216. second (or (nth 0 tl) 0)
  11217. wday (nth 6 tl))
  11218. ;; Special date definitions below
  11219. (cond
  11220. (iso-week
  11221. ;; There was an iso week
  11222. (setq year (or iso-year year)
  11223. day (or iso-weekday wday 1)
  11224. wday nil ; to make sure that the trigger below does not match
  11225. iso-date (calendar-gregorian-from-absolute
  11226. (calendar-absolute-from-iso
  11227. (list iso-week day year))))
  11228. ; FIXME: Should we also push ISO weeks into the future?
  11229. ; (when (and org-read-date-prefer-future
  11230. ; (not iso-year)
  11231. ; (< (calendar-absolute-from-gregorian iso-date)
  11232. ; (time-to-days (current-time))))
  11233. ; (setq year (1+ year)
  11234. ; iso-date (calendar-gregorian-from-absolute
  11235. ; (calendar-absolute-from-iso
  11236. ; (list iso-week day year)))))
  11237. (setq month (car iso-date)
  11238. year (nth 2 iso-date)
  11239. day (nth 1 iso-date)))
  11240. (deltan
  11241. (unless deltadef
  11242. (let ((now (decode-time (current-time))))
  11243. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11244. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11245. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11246. ((equal deltaw "m") (setq month (+ month deltan)))
  11247. ((equal deltaw "y") (setq year (+ year deltan)))))
  11248. ((and wday (not (nth 3 tl)))
  11249. ;; Weekday was given, but no day, so pick that day in the week
  11250. ;; on or after the derived date.
  11251. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11252. (unless (equal wday wday1)
  11253. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11254. (if (and (boundp 'org-time-was-given)
  11255. (nth 2 tl))
  11256. (setq org-time-was-given t))
  11257. (if (< year 100) (setq year (+ 2000 year)))
  11258. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11259. (list second minute hour day month year)))
  11260. (defvar parse-time-weekdays)
  11261. (defun org-read-date-get-relative (s today default)
  11262. "Check string S for special relative date string.
  11263. TODAY and DEFAULT are internal times, for today and for a default.
  11264. Return shift list (N what def-flag)
  11265. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11266. N is the number of WHATs to shift.
  11267. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11268. the DEFAULT date rather than TODAY."
  11269. (when (and
  11270. (string-match
  11271. (concat
  11272. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11273. "\\([0-9]+\\)?"
  11274. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11275. "\\([ \t]\\|$\\)") s)
  11276. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11277. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11278. (string-to-char (substring (match-string 1 s) -1))
  11279. ?+))
  11280. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11281. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11282. (what (if (match-end 3) (match-string 3 s) "d"))
  11283. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11284. (date (if rel default today))
  11285. (wday (nth 6 (decode-time date)))
  11286. delta)
  11287. (if wday1
  11288. (progn
  11289. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11290. (if (= dir ?-) (setq delta (- delta 7)))
  11291. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11292. (list delta "d" rel))
  11293. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11294. (defun org-eval-in-calendar (form &optional keepdate)
  11295. "Eval FORM in the calendar window and return to current window.
  11296. Also, store the cursor date in variable org-ans2."
  11297. (let ((sw (selected-window)))
  11298. (select-window (get-buffer-window "*Calendar*"))
  11299. (eval form)
  11300. (when (and (not keepdate) (calendar-cursor-to-date))
  11301. (let* ((date (calendar-cursor-to-date))
  11302. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11303. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11304. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11305. (select-window sw)))
  11306. (defun org-calendar-select ()
  11307. "Return to `org-read-date' with the date currently selected.
  11308. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11309. (interactive)
  11310. (when (calendar-cursor-to-date)
  11311. (let* ((date (calendar-cursor-to-date))
  11312. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11313. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11314. (if (active-minibuffer-window) (exit-minibuffer))))
  11315. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11316. "Insert a date stamp for the date given by the internal TIME.
  11317. WITH-HM means, use the stamp format that includes the time of the day.
  11318. INACTIVE means use square brackets instead of angular ones, so that the
  11319. stamp will not contribute to the agenda.
  11320. PRE and POST are optional strings to be inserted before and after the
  11321. stamp.
  11322. The command returns the inserted time stamp."
  11323. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11324. stamp)
  11325. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11326. (insert-before-markers (or pre ""))
  11327. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11328. (when (listp extra)
  11329. (setq extra (car extra))
  11330. (if (and (stringp extra)
  11331. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11332. (setq extra (format "-%02d:%02d"
  11333. (string-to-number (match-string 1 extra))
  11334. (string-to-number (match-string 2 extra))))
  11335. (setq extra nil)))
  11336. (when extra
  11337. (backward-char 1)
  11338. (insert-before-markers extra)
  11339. (forward-char 1))
  11340. (insert-before-markers (or post ""))
  11341. (setq org-last-inserted-timestamp stamp)))
  11342. (defun org-toggle-time-stamp-overlays ()
  11343. "Toggle the use of custom time stamp formats."
  11344. (interactive)
  11345. (setq org-display-custom-times (not org-display-custom-times))
  11346. (unless org-display-custom-times
  11347. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11348. (while (setq p (next-single-property-change p 'display))
  11349. (if (and (get-text-property p 'display)
  11350. (eq (get-text-property p 'face) 'org-date))
  11351. (remove-text-properties
  11352. p (setq p (next-single-property-change p 'display))
  11353. '(display t))))
  11354. (set-buffer-modified-p bmp)))
  11355. (if (featurep 'xemacs)
  11356. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11357. (org-restart-font-lock)
  11358. (setq org-table-may-need-update t)
  11359. (if org-display-custom-times
  11360. (message "Time stamps are overlayed with custom format")
  11361. (message "Time stamp overlays removed")))
  11362. (defun org-display-custom-time (beg end)
  11363. "Overlay modified time stamp format over timestamp between BEG and END."
  11364. (let* ((ts (buffer-substring beg end))
  11365. t1 w1 with-hm tf time str w2 (off 0))
  11366. (save-match-data
  11367. (setq t1 (org-parse-time-string ts t))
  11368. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11369. (setq off (- (match-end 0) (match-beginning 0)))))
  11370. (setq end (- end off))
  11371. (setq w1 (- end beg)
  11372. with-hm (and (nth 1 t1) (nth 2 t1))
  11373. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11374. time (org-fix-decoded-time t1)
  11375. str (org-add-props
  11376. (format-time-string
  11377. (substring tf 1 -1) (apply 'encode-time time))
  11378. nil 'mouse-face 'highlight)
  11379. w2 (length str))
  11380. (if (not (= w2 w1))
  11381. (add-text-properties (1+ beg) (+ 2 beg)
  11382. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11383. (if (featurep 'xemacs)
  11384. (progn
  11385. (put-text-property beg end 'invisible t)
  11386. (put-text-property beg end 'end-glyph (make-glyph str)))
  11387. (put-text-property beg end 'display str))))
  11388. (defun org-translate-time (string)
  11389. "Translate all timestamps in STRING to custom format.
  11390. But do this only if the variable `org-display-custom-times' is set."
  11391. (when org-display-custom-times
  11392. (save-match-data
  11393. (let* ((start 0)
  11394. (re org-ts-regexp-both)
  11395. t1 with-hm inactive tf time str beg end)
  11396. (while (setq start (string-match re string start))
  11397. (setq beg (match-beginning 0)
  11398. end (match-end 0)
  11399. t1 (save-match-data
  11400. (org-parse-time-string (substring string beg end) t))
  11401. with-hm (and (nth 1 t1) (nth 2 t1))
  11402. inactive (equal (substring string beg (1+ beg)) "[")
  11403. tf (funcall (if with-hm 'cdr 'car)
  11404. org-time-stamp-custom-formats)
  11405. time (org-fix-decoded-time t1)
  11406. str (format-time-string
  11407. (concat
  11408. (if inactive "[" "<") (substring tf 1 -1)
  11409. (if inactive "]" ">"))
  11410. (apply 'encode-time time))
  11411. string (replace-match str t t string)
  11412. start (+ start (length str)))))))
  11413. string)
  11414. (defun org-fix-decoded-time (time)
  11415. "Set 0 instead of nil for the first 6 elements of time.
  11416. Don't touch the rest."
  11417. (let ((n 0))
  11418. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11419. (defun org-days-to-time (timestamp-string)
  11420. "Difference between TIMESTAMP-STRING and now in days."
  11421. (- (time-to-days (org-time-string-to-time timestamp-string))
  11422. (time-to-days (current-time))))
  11423. (defun org-deadline-close (timestamp-string &optional ndays)
  11424. "Is the time in TIMESTAMP-STRING close to the current date?"
  11425. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11426. (and (< (org-days-to-time timestamp-string) ndays)
  11427. (not (org-entry-is-done-p))))
  11428. (defun org-get-wdays (ts)
  11429. "Get the deadline lead time appropriate for timestring TS."
  11430. (cond
  11431. ((<= org-deadline-warning-days 0)
  11432. ;; 0 or negative, enforce this value no matter what
  11433. (- org-deadline-warning-days))
  11434. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11435. ;; lead time is specified.
  11436. (floor (* (string-to-number (match-string 1 ts))
  11437. (cdr (assoc (match-string 2 ts)
  11438. '(("d" . 1) ("w" . 7)
  11439. ("m" . 30.4) ("y" . 365.25)))))))
  11440. ;; go for the default.
  11441. (t org-deadline-warning-days)))
  11442. (defun org-calendar-select-mouse (ev)
  11443. "Return to `org-read-date' with the date currently selected.
  11444. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11445. (interactive "e")
  11446. (mouse-set-point ev)
  11447. (when (calendar-cursor-to-date)
  11448. (let* ((date (calendar-cursor-to-date))
  11449. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11450. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11451. (if (active-minibuffer-window) (exit-minibuffer))))
  11452. (defun org-check-deadlines (ndays)
  11453. "Check if there are any deadlines due or past due.
  11454. A deadline is considered due if it happens within `org-deadline-warning-days'
  11455. days from today's date. If the deadline appears in an entry marked DONE,
  11456. it is not shown. The prefix arg NDAYS can be used to test that many
  11457. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11458. (interactive "P")
  11459. (let* ((org-warn-days
  11460. (cond
  11461. ((equal ndays '(4)) 100000)
  11462. (ndays (prefix-numeric-value ndays))
  11463. (t (abs org-deadline-warning-days))))
  11464. (case-fold-search nil)
  11465. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11466. (callback
  11467. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11468. (message "%d deadlines past-due or due within %d days"
  11469. (org-occur regexp nil callback)
  11470. org-warn-days)))
  11471. (defun org-check-before-date (date)
  11472. "Check if there are deadlines or scheduled entries before DATE."
  11473. (interactive (list (org-read-date)))
  11474. (let ((case-fold-search nil)
  11475. (regexp (concat "\\<\\(" org-deadline-string
  11476. "\\|" org-scheduled-string
  11477. "\\) *<\\([^>]+\\)>"))
  11478. (callback
  11479. (lambda () (time-less-p
  11480. (org-time-string-to-time (match-string 2))
  11481. (org-time-string-to-time date)))))
  11482. (message "%d entries before %s"
  11483. (org-occur regexp nil callback) date)))
  11484. (defun org-check-after-date (date)
  11485. "Check if there are deadlines or scheduled entries after DATE."
  11486. (interactive (list (org-read-date)))
  11487. (let ((case-fold-search nil)
  11488. (regexp (concat "\\<\\(" org-deadline-string
  11489. "\\|" org-scheduled-string
  11490. "\\) *<\\([^>]+\\)>"))
  11491. (callback
  11492. (lambda () (not
  11493. (time-less-p
  11494. (org-time-string-to-time (match-string 2))
  11495. (org-time-string-to-time date))))))
  11496. (message "%d entries after %s"
  11497. (org-occur regexp nil callback) date)))
  11498. (defun org-evaluate-time-range (&optional to-buffer)
  11499. "Evaluate a time range by computing the difference between start and end.
  11500. Normally the result is just printed in the echo area, but with prefix arg
  11501. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11502. If the time range is actually in a table, the result is inserted into the
  11503. next column.
  11504. For time difference computation, a year is assumed to be exactly 365
  11505. days in order to avoid rounding problems."
  11506. (interactive "P")
  11507. (or
  11508. (org-clock-update-time-maybe)
  11509. (save-excursion
  11510. (unless (org-at-date-range-p t)
  11511. (goto-char (point-at-bol))
  11512. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11513. (if (not (org-at-date-range-p t))
  11514. (error "Not at a time-stamp range, and none found in current line")))
  11515. (let* ((ts1 (match-string 1))
  11516. (ts2 (match-string 2))
  11517. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11518. (match-end (match-end 0))
  11519. (time1 (org-time-string-to-time ts1))
  11520. (time2 (org-time-string-to-time ts2))
  11521. (t1 (time-to-seconds time1))
  11522. (t2 (time-to-seconds time2))
  11523. (diff (abs (- t2 t1)))
  11524. (negative (< (- t2 t1) 0))
  11525. ;; (ys (floor (* 365 24 60 60)))
  11526. (ds (* 24 60 60))
  11527. (hs (* 60 60))
  11528. (fy "%dy %dd %02d:%02d")
  11529. (fy1 "%dy %dd")
  11530. (fd "%dd %02d:%02d")
  11531. (fd1 "%dd")
  11532. (fh "%02d:%02d")
  11533. y d h m align)
  11534. (if havetime
  11535. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11536. y 0
  11537. d (floor (/ diff ds)) diff (mod diff ds)
  11538. h (floor (/ diff hs)) diff (mod diff hs)
  11539. m (floor (/ diff 60)))
  11540. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11541. y 0
  11542. d (floor (+ (/ diff ds) 0.5))
  11543. h 0 m 0))
  11544. (if (not to-buffer)
  11545. (message "%s" (org-make-tdiff-string y d h m))
  11546. (if (org-at-table-p)
  11547. (progn
  11548. (goto-char match-end)
  11549. (setq align t)
  11550. (and (looking-at " *|") (goto-char (match-end 0))))
  11551. (goto-char match-end))
  11552. (if (looking-at
  11553. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11554. (replace-match ""))
  11555. (if negative (insert " -"))
  11556. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11557. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11558. (insert " " (format fh h m))))
  11559. (if align (org-table-align))
  11560. (message "Time difference inserted")))))
  11561. (defun org-make-tdiff-string (y d h m)
  11562. (let ((fmt "")
  11563. (l nil))
  11564. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11565. l (push y l)))
  11566. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11567. l (push d l)))
  11568. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11569. l (push h l)))
  11570. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11571. l (push m l)))
  11572. (apply 'format fmt (nreverse l))))
  11573. (defun org-time-string-to-time (s)
  11574. (apply 'encode-time (org-parse-time-string s)))
  11575. (defun org-time-string-to-seconds (s)
  11576. (time-to-seconds (org-time-string-to-time s)))
  11577. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11578. "Convert a time stamp to an absolute day number.
  11579. If there is a specifyer for a cyclic time stamp, get the closest date to
  11580. DAYNR.
  11581. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  11582. the variable date is bound by the calendar when this is called."
  11583. (cond
  11584. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11585. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11586. daynr
  11587. (+ daynr 1000)))
  11588. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11589. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11590. (time-to-days (current-time))) (match-string 0 s)
  11591. prefer show-all))
  11592. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11593. (defun org-days-to-iso-week (days)
  11594. "Return the iso week number."
  11595. (require 'cal-iso)
  11596. (car (calendar-iso-from-absolute days)))
  11597. (defun org-small-year-to-year (year)
  11598. "Convert 2-digit years into 4-digit years.
  11599. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11600. The year 2000 cannot be abbreviated. Any year larger than 99
  11601. is returned unchanged."
  11602. (if (< year 38)
  11603. (setq year (+ 2000 year))
  11604. (if (< year 100)
  11605. (setq year (+ 1900 year))))
  11606. year)
  11607. (defun org-time-from-absolute (d)
  11608. "Return the time corresponding to date D.
  11609. D may be an absolute day number, or a calendar-type list (month day year)."
  11610. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11611. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11612. (defun org-calendar-holiday ()
  11613. "List of holidays, for Diary display in Org-mode."
  11614. (require 'holidays)
  11615. (let ((hl (funcall
  11616. (if (fboundp 'calendar-check-holidays)
  11617. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11618. (if hl (mapconcat 'identity hl "; "))))
  11619. (defun org-diary-sexp-entry (sexp entry date)
  11620. "Process a SEXP diary ENTRY for DATE."
  11621. (require 'diary-lib)
  11622. (let ((result (if calendar-debug-sexp
  11623. (let ((stack-trace-on-error t))
  11624. (eval (car (read-from-string sexp))))
  11625. (condition-case nil
  11626. (eval (car (read-from-string sexp)))
  11627. (error
  11628. (beep)
  11629. (message "Bad sexp at line %d in %s: %s"
  11630. (org-current-line)
  11631. (buffer-file-name) sexp)
  11632. (sleep-for 2))))))
  11633. (cond ((stringp result) result)
  11634. ((and (consp result)
  11635. (stringp (cdr result))) (cdr result))
  11636. (result entry)
  11637. (t nil))))
  11638. (defun org-diary-to-ical-string (frombuf)
  11639. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11640. This uses the icalendar.el library."
  11641. (let* ((tmpdir (if (featurep 'xemacs)
  11642. (temp-directory)
  11643. temporary-file-directory))
  11644. (tmpfile (make-temp-name
  11645. (expand-file-name "orgics" tmpdir)))
  11646. buf rtn b e)
  11647. (save-excursion
  11648. (set-buffer frombuf)
  11649. (icalendar-export-region (point-min) (point-max) tmpfile)
  11650. (setq buf (find-buffer-visiting tmpfile))
  11651. (set-buffer buf)
  11652. (goto-char (point-min))
  11653. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11654. (setq b (match-beginning 0)))
  11655. (goto-char (point-max))
  11656. (if (re-search-backward "^END:VEVENT" nil t)
  11657. (setq e (match-end 0)))
  11658. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11659. (kill-buffer buf)
  11660. (delete-file tmpfile)
  11661. rtn))
  11662. (defun org-closest-date (start current change prefer show-all)
  11663. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11664. When PREFER is `past' return a date that is either CURRENT or past.
  11665. When PREFER is `future', return a date that is either CURRENT or future.
  11666. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11667. ;; Make the proper lists from the dates
  11668. (catch 'exit
  11669. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11670. dn dw sday cday n1 n2 n0
  11671. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11672. (setq start (org-date-to-gregorian start)
  11673. current (org-date-to-gregorian
  11674. (if show-all
  11675. current
  11676. (time-to-days (current-time))))
  11677. sday (calendar-absolute-from-gregorian start)
  11678. cday (calendar-absolute-from-gregorian current))
  11679. (if (<= cday sday) (throw 'exit sday))
  11680. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11681. (setq dn (string-to-number (match-string 1 change))
  11682. dw (cdr (assoc (match-string 2 change) a1)))
  11683. (error "Invalid change specifyer: %s" change))
  11684. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11685. (cond
  11686. ((eq dw 'day)
  11687. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11688. n2 (+ n1 dn)))
  11689. ((eq dw 'year)
  11690. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11691. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11692. (setq date1 (list m d y1)
  11693. n1 (calendar-absolute-from-gregorian date1)
  11694. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11695. n2 (calendar-absolute-from-gregorian date2)))
  11696. ((eq dw 'month)
  11697. ;; approx number of month between the two dates
  11698. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11699. ;; How often does dn fit in there?
  11700. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11701. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11702. m (+ m nm)
  11703. ny (floor (/ m 12))
  11704. y (+ y ny)
  11705. m (- m (* ny 12)))
  11706. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11707. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11708. (setq m2 (+ m dn) y2 y)
  11709. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11710. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11711. (while (<= n2 cday)
  11712. (setq n1 n2 m m2 y y2)
  11713. (setq m2 (+ m dn) y2 y)
  11714. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11715. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11716. ;; Make sure n1 is the earlier date
  11717. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11718. (if show-all
  11719. (cond
  11720. ((eq prefer 'past) n1)
  11721. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11722. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11723. (cond
  11724. ((eq prefer 'past) n1)
  11725. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11726. (t (if (= cday n1) n1 n2)))))))
  11727. (defun org-date-to-gregorian (date)
  11728. "Turn any specification of DATE into a gregorian date for the calendar."
  11729. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11730. ((and (listp date) (= (length date) 3)) date)
  11731. ((stringp date)
  11732. (setq date (org-parse-time-string date))
  11733. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11734. ((listp date)
  11735. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11736. (defun org-parse-time-string (s &optional nodefault)
  11737. "Parse the standard Org-mode time string.
  11738. This should be a lot faster than the normal `parse-time-string'.
  11739. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11740. hour and minute fields will be nil if not given."
  11741. (if (string-match org-ts-regexp0 s)
  11742. (list 0
  11743. (if (or (match-beginning 8) (not nodefault))
  11744. (string-to-number (or (match-string 8 s) "0")))
  11745. (if (or (match-beginning 7) (not nodefault))
  11746. (string-to-number (or (match-string 7 s) "0")))
  11747. (string-to-number (match-string 4 s))
  11748. (string-to-number (match-string 3 s))
  11749. (string-to-number (match-string 2 s))
  11750. nil nil nil)
  11751. (make-list 9 0)))
  11752. (defun org-timestamp-up (&optional arg)
  11753. "Increase the date item at the cursor by one.
  11754. If the cursor is on the year, change the year. If it is on the month or
  11755. the day, change that.
  11756. With prefix ARG, change by that many units."
  11757. (interactive "p")
  11758. (org-timestamp-change (prefix-numeric-value arg)))
  11759. (defun org-timestamp-down (&optional arg)
  11760. "Decrease the date item at the cursor by one.
  11761. If the cursor is on the year, change the year. If it is on the month or
  11762. the day, change that.
  11763. With prefix ARG, change by that many units."
  11764. (interactive "p")
  11765. (org-timestamp-change (- (prefix-numeric-value arg))))
  11766. (defun org-timestamp-up-day (&optional arg)
  11767. "Increase 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 'up)
  11773. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11774. (defun org-timestamp-down-day (&optional arg)
  11775. "Decrease the date in the time stamp by one day.
  11776. With prefix ARG, change that many days."
  11777. (interactive "p")
  11778. (if (and (not (org-at-timestamp-p t))
  11779. (org-on-heading-p))
  11780. (org-todo 'down)
  11781. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11782. (defun org-at-timestamp-p (&optional inactive-ok)
  11783. "Determine if the cursor is in or at a timestamp."
  11784. (interactive)
  11785. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11786. (pos (point))
  11787. (ans (or (looking-at tsr)
  11788. (save-excursion
  11789. (skip-chars-backward "^[<\n\r\t")
  11790. (if (> (point) (point-min)) (backward-char 1))
  11791. (and (looking-at tsr)
  11792. (> (- (match-end 0) pos) -1))))))
  11793. (and ans
  11794. (boundp 'org-ts-what)
  11795. (setq org-ts-what
  11796. (cond
  11797. ((= pos (match-beginning 0)) 'bracket)
  11798. ((= pos (1- (match-end 0))) 'bracket)
  11799. ((org-pos-in-match-range pos 2) 'year)
  11800. ((org-pos-in-match-range pos 3) 'month)
  11801. ((org-pos-in-match-range pos 7) 'hour)
  11802. ((org-pos-in-match-range pos 8) 'minute)
  11803. ((or (org-pos-in-match-range pos 4)
  11804. (org-pos-in-match-range pos 5)) 'day)
  11805. ((and (> pos (or (match-end 8) (match-end 5)))
  11806. (< pos (match-end 0)))
  11807. (- pos (or (match-end 8) (match-end 5))))
  11808. (t 'day))))
  11809. ans))
  11810. (defun org-toggle-timestamp-type ()
  11811. "Toggle the type (<active> or [inactive]) of a time stamp."
  11812. (interactive)
  11813. (when (org-at-timestamp-p t)
  11814. (let ((beg (match-beginning 0)) (end (match-end 0))
  11815. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11816. (save-excursion
  11817. (goto-char beg)
  11818. (while (re-search-forward "[][<>]" end t)
  11819. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11820. t t)))
  11821. (message "Timestamp is now %sactive"
  11822. (if (equal (char-after beg) ?<) "" "in")))))
  11823. (defun org-timestamp-change (n &optional what)
  11824. "Change the date in the time stamp at point.
  11825. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11826. `year', `minute', `second'. If WHAT is not given, the cursor position
  11827. in the timestamp determines what will be changed."
  11828. (let ((pos (point))
  11829. with-hm inactive
  11830. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11831. org-ts-what
  11832. extra rem
  11833. ts time time0)
  11834. (if (not (org-at-timestamp-p t))
  11835. (error "Not at a timestamp"))
  11836. (if (and (not what) (eq org-ts-what 'bracket))
  11837. (org-toggle-timestamp-type)
  11838. (if (and (not what) (not (eq org-ts-what 'day))
  11839. org-display-custom-times
  11840. (get-text-property (point) 'display)
  11841. (not (get-text-property (1- (point)) 'display)))
  11842. (setq org-ts-what 'day))
  11843. (setq org-ts-what (or what org-ts-what)
  11844. inactive (= (char-after (match-beginning 0)) ?\[)
  11845. ts (match-string 0))
  11846. (replace-match "")
  11847. (if (string-match
  11848. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11849. ts)
  11850. (setq extra (match-string 1 ts)))
  11851. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11852. (setq with-hm t))
  11853. (setq time0 (org-parse-time-string ts))
  11854. (when (and (eq org-ts-what 'minute)
  11855. (eq current-prefix-arg nil))
  11856. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11857. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11858. (setcar (cdr time0) (+ (nth 1 time0)
  11859. (if (> n 0) (- rem) (- dm rem))))))
  11860. (setq time
  11861. (encode-time (or (car time0) 0)
  11862. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11863. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11864. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11865. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11866. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11867. (nthcdr 6 time0)))
  11868. (when (and (member org-ts-what '(hour minute))
  11869. extra
  11870. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11871. (setq extra (org-modify-ts-extra
  11872. extra
  11873. (if (eq org-ts-what 'hour) 2 5)
  11874. n dm)))
  11875. (when (integerp org-ts-what)
  11876. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11877. (if (eq what 'calendar)
  11878. (let ((cal-date (org-get-date-from-calendar)))
  11879. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11880. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11881. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11882. (setcar time0 (or (car time0) 0))
  11883. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11884. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11885. (setq time (apply 'encode-time time0))))
  11886. (setq org-last-changed-timestamp
  11887. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11888. (org-clock-update-time-maybe)
  11889. (goto-char pos)
  11890. ;; Try to recenter the calendar window, if any
  11891. (if (and org-calendar-follow-timestamp-change
  11892. (get-buffer-window "*Calendar*" t)
  11893. (memq org-ts-what '(day month year)))
  11894. (org-recenter-calendar (time-to-days time))))))
  11895. (defun org-modify-ts-extra (s pos n dm)
  11896. "Change the different parts of the lead-time and repeat fields in timestamp."
  11897. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11898. ng h m new rem)
  11899. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11900. (cond
  11901. ((or (org-pos-in-match-range pos 2)
  11902. (org-pos-in-match-range pos 3))
  11903. (setq m (string-to-number (match-string 3 s))
  11904. h (string-to-number (match-string 2 s)))
  11905. (if (org-pos-in-match-range pos 2)
  11906. (setq h (+ h n))
  11907. (setq n (* dm (org-no-warnings (signum n))))
  11908. (when (not (= 0 (setq rem (% m dm))))
  11909. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11910. (setq m (+ m n)))
  11911. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11912. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11913. (setq h (min 24 (max 0 h)))
  11914. (setq ng 1 new (format "-%02d:%02d" h m)))
  11915. ((org-pos-in-match-range pos 6)
  11916. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11917. ((org-pos-in-match-range pos 5)
  11918. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11919. ((org-pos-in-match-range pos 9)
  11920. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11921. ((org-pos-in-match-range pos 8)
  11922. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11923. (when ng
  11924. (setq s (concat
  11925. (substring s 0 (match-beginning ng))
  11926. new
  11927. (substring s (match-end ng))))))
  11928. s))
  11929. (defun org-recenter-calendar (date)
  11930. "If the calendar is visible, recenter it to DATE."
  11931. (let* ((win (selected-window))
  11932. (cwin (get-buffer-window "*Calendar*" t))
  11933. (calendar-move-hook nil))
  11934. (when cwin
  11935. (select-window cwin)
  11936. (calendar-goto-date (if (listp date) date
  11937. (calendar-gregorian-from-absolute date)))
  11938. (select-window win))))
  11939. (defun org-goto-calendar (&optional arg)
  11940. "Go to the Emacs calendar at the current date.
  11941. If there is a time stamp in the current line, go to that date.
  11942. A prefix ARG can be used to force the current date."
  11943. (interactive "P")
  11944. (let ((tsr org-ts-regexp) diff
  11945. (calendar-move-hook nil)
  11946. (calendar-view-holidays-initially-flag nil)
  11947. (view-calendar-holidays-initially nil)
  11948. (calendar-view-diary-initially-flag nil)
  11949. (view-diary-entries-initially nil))
  11950. (if (or (org-at-timestamp-p)
  11951. (save-excursion
  11952. (beginning-of-line 1)
  11953. (looking-at (concat ".*" tsr))))
  11954. (let ((d1 (time-to-days (current-time)))
  11955. (d2 (time-to-days
  11956. (org-time-string-to-time (match-string 1)))))
  11957. (setq diff (- d2 d1))))
  11958. (calendar)
  11959. (calendar-goto-today)
  11960. (if (and diff (not arg)) (calendar-forward-day diff))))
  11961. (defun org-get-date-from-calendar ()
  11962. "Return a list (month day year) of date at point in calendar."
  11963. (with-current-buffer "*Calendar*"
  11964. (save-match-data
  11965. (calendar-cursor-to-date))))
  11966. (defun org-date-from-calendar ()
  11967. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11968. If there is already a time stamp at the cursor position, update it."
  11969. (interactive)
  11970. (if (org-at-timestamp-p t)
  11971. (org-timestamp-change 0 'calendar)
  11972. (let ((cal-date (org-get-date-from-calendar)))
  11973. (org-insert-time-stamp
  11974. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11975. (defun org-minutes-to-hh:mm-string (m)
  11976. "Compute H:MM from a number of minutes."
  11977. (let ((h (/ m 60)))
  11978. (setq m (- m (* 60 h)))
  11979. (format org-time-clocksum-format h m)))
  11980. (defun org-hh:mm-string-to-minutes (s)
  11981. "Convert a string H:MM to a number of minutes."
  11982. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11983. (+ (* (string-to-number (match-string 1 s)) 60)
  11984. (string-to-number (match-string 2 s)))
  11985. 0))
  11986. ;;;; Files
  11987. (defun org-save-all-org-buffers ()
  11988. "Save all Org-mode buffers without user confirmation."
  11989. (interactive)
  11990. (message "Saving all Org-mode buffers...")
  11991. (save-some-buffers t 'org-mode-p)
  11992. (when (featurep 'org-id) (org-id-locations-save))
  11993. (message "Saving all Org-mode buffers... done"))
  11994. (defun org-revert-all-org-buffers ()
  11995. "Revert all Org-mode buffers.
  11996. Prompt for confirmation when there are unsaved changes.
  11997. Be sure you know what you are doing before letting this function
  11998. overwrite your changes.
  11999. This function is useful in a setup where one tracks org files
  12000. with a version control system, to revert on one machine after pulling
  12001. changes from another. I believe the procedure must be like this:
  12002. 1. M-x org-save-all-org-buffers
  12003. 2. Pull changes from the other machine, resolve conflicts
  12004. 3. M-x org-revert-all-org-buffers"
  12005. (interactive)
  12006. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12007. (error "Abort"))
  12008. (save-excursion
  12009. (save-window-excursion
  12010. (mapc
  12011. (lambda (b)
  12012. (when (and (with-current-buffer b (org-mode-p))
  12013. (with-current-buffer b buffer-file-name))
  12014. (switch-to-buffer b)
  12015. (revert-buffer t 'no-confirm)))
  12016. (buffer-list))
  12017. (when (and (featurep 'org-id) org-id-track-globally)
  12018. (org-id-locations-load)))))
  12019. ;;;; Agenda files
  12020. ;;;###autoload
  12021. (defun org-iswitchb (&optional arg)
  12022. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12023. With a prefix argument, restrict available to files.
  12024. With two prefix arguments, restrict available buffers to agenda files.
  12025. Due to some yet unresolved reason, the global function
  12026. `iswitchb-mode' needs to be active for this function to work."
  12027. (interactive "P")
  12028. (require 'iswitchb)
  12029. (let ((enabled iswitchb-mode) blist)
  12030. (or enabled (iswitchb-mode 1))
  12031. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12032. ((equal arg '(16)) (org-buffer-list 'agenda))
  12033. (t (org-buffer-list))))
  12034. (unwind-protect
  12035. (let ((iswitchb-make-buflist-hook
  12036. (lambda ()
  12037. (setq iswitchb-temp-buflist
  12038. (mapcar 'buffer-name blist)))))
  12039. (switch-to-buffer
  12040. (iswitchb-read-buffer
  12041. "Switch-to: " nil t))
  12042. (or enabled (iswitchb-mode -1))))))
  12043. ;;;###autoload
  12044. (defun org-ido-switchb (&optional arg)
  12045. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12046. With a prefix argument, restrict available to files.
  12047. With two prefix arguments, restrict available buffers to agenda files."
  12048. (interactive "P")
  12049. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12050. ((equal arg '(16)) (org-buffer-list 'agenda))
  12051. (t (org-buffer-list)))))
  12052. (switch-to-buffer
  12053. (org-ido-completing-read "Org buffer: "
  12054. (mapcar 'list (mapcar 'buffer-name blist))
  12055. nil t))))
  12056. (defun org-buffer-list (&optional predicate exclude-tmp)
  12057. "Return a list of Org buffers.
  12058. PREDICATE can be `export', `files' or `agenda'.
  12059. export restrict the list to Export buffers.
  12060. files restrict the list to buffers visiting Org files.
  12061. agenda restrict the list to buffers visiting agenda files.
  12062. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12063. (let* ((bfn nil)
  12064. (agenda-files (and (eq predicate 'agenda)
  12065. (mapcar 'file-truename (org-agenda-files t))))
  12066. (filter
  12067. (cond
  12068. ((eq predicate 'files)
  12069. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12070. ((eq predicate 'export)
  12071. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12072. ((eq predicate 'agenda)
  12073. (lambda (b)
  12074. (with-current-buffer b
  12075. (and (eq major-mode 'org-mode)
  12076. (setq bfn (buffer-file-name b))
  12077. (member (file-truename bfn) agenda-files)))))
  12078. (t (lambda (b) (with-current-buffer b
  12079. (or (eq major-mode 'org-mode)
  12080. (string-match "\*Org .*Export"
  12081. (buffer-name b)))))))))
  12082. (delq nil
  12083. (mapcar
  12084. (lambda(b)
  12085. (if (and (funcall filter b)
  12086. (or (not exclude-tmp)
  12087. (not (string-match "tmp" (buffer-name b)))))
  12088. b
  12089. nil))
  12090. (buffer-list)))))
  12091. (defun org-agenda-files (&optional unrestricted archives)
  12092. "Get the list of agenda files.
  12093. Optional UNRESTRICTED means return the full list even if a restriction
  12094. is currently in place.
  12095. When ARCHIVES is t, include all archive files hat are really being
  12096. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12097. `org-agenda-archives-mode' is t."
  12098. (let ((files
  12099. (cond
  12100. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12101. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12102. ((listp org-agenda-files) org-agenda-files)
  12103. (t (error "Invalid value of `org-agenda-files'")))))
  12104. (setq files (apply 'append
  12105. (mapcar (lambda (f)
  12106. (if (file-directory-p f)
  12107. (directory-files
  12108. f t org-agenda-file-regexp)
  12109. (list f)))
  12110. files)))
  12111. (when org-agenda-skip-unavailable-files
  12112. (setq files (delq nil
  12113. (mapcar (function
  12114. (lambda (file)
  12115. (and (file-readable-p file) file)))
  12116. files))))
  12117. (when (or (eq archives t)
  12118. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12119. (setq files (org-add-archive-files files)))
  12120. files))
  12121. (defun org-edit-agenda-file-list ()
  12122. "Edit the list of agenda files.
  12123. Depending on setup, this either uses customize to edit the variable
  12124. `org-agenda-files', or it visits the file that is holding the list. In the
  12125. latter case, the buffer is set up in a way that saving it automatically kills
  12126. the buffer and restores the previous window configuration."
  12127. (interactive)
  12128. (if (stringp org-agenda-files)
  12129. (let ((cw (current-window-configuration)))
  12130. (find-file org-agenda-files)
  12131. (org-set-local 'org-window-configuration cw)
  12132. (org-add-hook 'after-save-hook
  12133. (lambda ()
  12134. (set-window-configuration
  12135. (prog1 org-window-configuration
  12136. (kill-buffer (current-buffer))))
  12137. (org-install-agenda-files-menu)
  12138. (message "New agenda file list installed"))
  12139. nil 'local)
  12140. (message "%s" (substitute-command-keys
  12141. "Edit list and finish with \\[save-buffer]")))
  12142. (customize-variable 'org-agenda-files)))
  12143. (defun org-store-new-agenda-file-list (list)
  12144. "Set new value for the agenda file list and save it correctly."
  12145. (if (stringp org-agenda-files)
  12146. (let ((f org-agenda-files) b)
  12147. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12148. (with-temp-file f
  12149. (insert (mapconcat 'identity list "\n") "\n")))
  12150. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12151. (setq org-agenda-files list)
  12152. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12153. (defun org-read-agenda-file-list ()
  12154. "Read the list of agenda files from a file."
  12155. (when (file-directory-p org-agenda-files)
  12156. (error "`org-agenda-files' cannot be a single directory"))
  12157. (when (stringp org-agenda-files)
  12158. (with-temp-buffer
  12159. (insert-file-contents org-agenda-files)
  12160. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12161. ;;;###autoload
  12162. (defun org-cycle-agenda-files ()
  12163. "Cycle through the files in `org-agenda-files'.
  12164. If the current buffer visits an agenda file, find the next one in the list.
  12165. If the current buffer does not, find the first agenda file."
  12166. (interactive)
  12167. (let* ((fs (org-agenda-files t))
  12168. (files (append fs (list (car fs))))
  12169. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12170. file)
  12171. (unless files (error "No agenda files"))
  12172. (catch 'exit
  12173. (while (setq file (pop files))
  12174. (if (equal (file-truename file) tcf)
  12175. (when (car files)
  12176. (find-file (car files))
  12177. (throw 'exit t))))
  12178. (find-file (car fs)))
  12179. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12180. (defun org-agenda-file-to-front (&optional to-end)
  12181. "Move/add the current file to the top of the agenda file list.
  12182. If the file is not present in the list, it is added to the front. If it is
  12183. present, it is moved there. With optional argument TO-END, add/move to the
  12184. end of the list."
  12185. (interactive "P")
  12186. (let ((org-agenda-skip-unavailable-files nil)
  12187. (file-alist (mapcar (lambda (x)
  12188. (cons (file-truename x) x))
  12189. (org-agenda-files t)))
  12190. (ctf (file-truename buffer-file-name))
  12191. x had)
  12192. (setq x (assoc ctf file-alist) had x)
  12193. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12194. (if to-end
  12195. (setq file-alist (append (delq x file-alist) (list x)))
  12196. (setq file-alist (cons x (delq x file-alist))))
  12197. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12198. (org-install-agenda-files-menu)
  12199. (message "File %s to %s of agenda file list"
  12200. (if had "moved" "added") (if to-end "end" "front"))))
  12201. (defun org-remove-file (&optional file)
  12202. "Remove current file from the list of files in variable `org-agenda-files'.
  12203. These are the files which are being checked for agenda entries.
  12204. Optional argument FILE means, use this file instead of the current."
  12205. (interactive)
  12206. (let* ((org-agenda-skip-unavailable-files nil)
  12207. (file (or file buffer-file-name))
  12208. (true-file (file-truename file))
  12209. (afile (abbreviate-file-name file))
  12210. (files (delq nil (mapcar
  12211. (lambda (x)
  12212. (if (equal true-file
  12213. (file-truename x))
  12214. nil x))
  12215. (org-agenda-files t)))))
  12216. (if (not (= (length files) (length (org-agenda-files t))))
  12217. (progn
  12218. (org-store-new-agenda-file-list files)
  12219. (org-install-agenda-files-menu)
  12220. (message "Removed file: %s" afile))
  12221. (message "File was not in list: %s (not removed)" afile))))
  12222. (defun org-file-menu-entry (file)
  12223. (vector file (list 'find-file file) t))
  12224. (defun org-check-agenda-file (file)
  12225. "Make sure FILE exists. If not, ask user what to do."
  12226. (when (not (file-exists-p file))
  12227. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12228. (abbreviate-file-name file))
  12229. (let ((r (downcase (read-char-exclusive))))
  12230. (cond
  12231. ((equal r ?r)
  12232. (org-remove-file file)
  12233. (throw 'nextfile t))
  12234. (t (error "Abort"))))))
  12235. (defun org-get-agenda-file-buffer (file)
  12236. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12237. it to the list of buffers which might be released later."
  12238. (let ((buf (org-find-base-buffer-visiting file)))
  12239. (if buf
  12240. buf ; just return it
  12241. ;; Make a new buffer and remember it
  12242. (setq buf (find-file-noselect file))
  12243. (if buf (push buf org-agenda-new-buffers))
  12244. buf)))
  12245. (defun org-release-buffers (blist)
  12246. "Release all buffers in list, asking the user for confirmation when needed.
  12247. When a buffer is unmodified, it is just killed. When modified, it is saved
  12248. \(if the user agrees) and then killed."
  12249. (let (buf file)
  12250. (while (setq buf (pop blist))
  12251. (setq file (buffer-file-name buf))
  12252. (when (and (buffer-modified-p buf)
  12253. file
  12254. (y-or-n-p (format "Save file %s? " file)))
  12255. (with-current-buffer buf (save-buffer)))
  12256. (kill-buffer buf))))
  12257. (defun org-prepare-agenda-buffers (files)
  12258. "Create buffers for all agenda files, protect archived trees and comments."
  12259. (interactive)
  12260. (let ((pa '(:org-archived t))
  12261. (pc '(:org-comment t))
  12262. (pall '(:org-archived t :org-comment t))
  12263. (inhibit-read-only t)
  12264. (rea (concat ":" org-archive-tag ":"))
  12265. bmp file re)
  12266. (save-excursion
  12267. (save-restriction
  12268. (while (setq file (pop files))
  12269. (catch 'nextfile
  12270. (if (bufferp file)
  12271. (set-buffer file)
  12272. (org-check-agenda-file file)
  12273. (set-buffer (org-get-agenda-file-buffer file)))
  12274. (widen)
  12275. (setq bmp (buffer-modified-p))
  12276. (org-refresh-category-properties)
  12277. (setq org-todo-keywords-for-agenda
  12278. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12279. (setq org-done-keywords-for-agenda
  12280. (append org-done-keywords-for-agenda org-done-keywords))
  12281. (setq org-todo-keyword-alist-for-agenda
  12282. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12283. (setq org-tag-alist-for-agenda
  12284. (append org-tag-alist-for-agenda org-tag-alist))
  12285. (save-excursion
  12286. (remove-text-properties (point-min) (point-max) pall)
  12287. (when org-agenda-skip-archived-trees
  12288. (goto-char (point-min))
  12289. (while (re-search-forward rea nil t)
  12290. (if (org-on-heading-p t)
  12291. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12292. (goto-char (point-min))
  12293. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12294. (while (re-search-forward re nil t)
  12295. (add-text-properties
  12296. (match-beginning 0) (org-end-of-subtree t) pc)))
  12297. (set-buffer-modified-p bmp)))))
  12298. (setq org-todo-keyword-alist-for-agenda
  12299. (org-uniquify org-todo-keyword-alist-for-agenda)
  12300. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12301. ;;;; Embedded LaTeX
  12302. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12303. "Keymap for the minor `org-cdlatex-mode'.")
  12304. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12305. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12306. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12307. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12308. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12309. (defvar org-cdlatex-texmathp-advice-is-done nil
  12310. "Flag remembering if we have applied the advice to texmathp already.")
  12311. (define-minor-mode org-cdlatex-mode
  12312. "Toggle the minor `org-cdlatex-mode'.
  12313. This mode supports entering LaTeX environment and math in LaTeX fragments
  12314. in Org-mode.
  12315. \\{org-cdlatex-mode-map}"
  12316. nil " OCDL" nil
  12317. (when org-cdlatex-mode (require 'cdlatex))
  12318. (unless org-cdlatex-texmathp-advice-is-done
  12319. (setq org-cdlatex-texmathp-advice-is-done t)
  12320. (defadvice texmathp (around org-math-always-on activate)
  12321. "Always return t in org-mode buffers.
  12322. This is because we want to insert math symbols without dollars even outside
  12323. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12324. an embedded LaTeX fragment, let texmathp do its job.
  12325. \\[org-cdlatex-mode-map]"
  12326. (interactive)
  12327. (let (p)
  12328. (cond
  12329. ((not (org-mode-p)) ad-do-it)
  12330. ((eq this-command 'cdlatex-math-symbol)
  12331. (setq ad-return-value t
  12332. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12333. (t
  12334. (let ((p (org-inside-LaTeX-fragment-p)))
  12335. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12336. (setq ad-return-value t
  12337. texmathp-why '("Org-mode embedded math" . 0))
  12338. (if p ad-do-it)))))))))
  12339. (defun turn-on-org-cdlatex ()
  12340. "Unconditionally turn on `org-cdlatex-mode'."
  12341. (org-cdlatex-mode 1))
  12342. (defun org-inside-LaTeX-fragment-p ()
  12343. "Test if point is inside a LaTeX fragment.
  12344. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12345. sequence appearing also before point.
  12346. Even though the matchers for math are configurable, this function assumes
  12347. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12348. delimiters are skipped when they have been removed by customization.
  12349. The return value is nil, or a cons cell with the delimiter and
  12350. and the position of this delimiter.
  12351. This function does a reasonably good job, but can locally be fooled by
  12352. for example currency specifications. For example it will assume being in
  12353. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12354. fragments that are properly closed, but during editing, we have to live
  12355. with the uncertainty caused by missing closing delimiters. This function
  12356. looks only before point, not after."
  12357. (catch 'exit
  12358. (let ((pos (point))
  12359. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12360. (lim (progn
  12361. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12362. (point)))
  12363. dd-on str (start 0) m re)
  12364. (goto-char pos)
  12365. (when dodollar
  12366. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12367. re (nth 1 (assoc "$" org-latex-regexps)))
  12368. (while (string-match re str start)
  12369. (cond
  12370. ((= (match-end 0) (length str))
  12371. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12372. ((= (match-end 0) (- (length str) 5))
  12373. (throw 'exit nil))
  12374. (t (setq start (match-end 0))))))
  12375. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12376. (goto-char pos)
  12377. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12378. (and (match-beginning 2) (throw 'exit nil))
  12379. ;; count $$
  12380. (while (re-search-backward "\\$\\$" lim t)
  12381. (setq dd-on (not dd-on)))
  12382. (goto-char pos)
  12383. (if dd-on (cons "$$" m))))))
  12384. (defun org-try-cdlatex-tab ()
  12385. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12386. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12387. - inside a LaTeX fragment, or
  12388. - after the first word in a line, where an abbreviation expansion could
  12389. insert a LaTeX environment."
  12390. (when org-cdlatex-mode
  12391. (cond
  12392. ((save-excursion
  12393. (skip-chars-backward "a-zA-Z0-9*")
  12394. (skip-chars-backward " \t")
  12395. (bolp))
  12396. (cdlatex-tab) t)
  12397. ((org-inside-LaTeX-fragment-p)
  12398. (cdlatex-tab) t)
  12399. (t nil))))
  12400. (defun org-cdlatex-underscore-caret (&optional arg)
  12401. "Execute `cdlatex-sub-superscript' 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-sub-superscript)
  12406. (let (org-cdlatex-mode)
  12407. (call-interactively (key-binding (vector last-input-event))))))
  12408. (defun org-cdlatex-math-modify (&optional arg)
  12409. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12410. Revert to the normal definition outside of these fragments."
  12411. (interactive "P")
  12412. (if (org-inside-LaTeX-fragment-p)
  12413. (call-interactively 'cdlatex-math-modify)
  12414. (let (org-cdlatex-mode)
  12415. (call-interactively (key-binding (vector last-input-event))))))
  12416. (defvar org-latex-fragment-image-overlays nil
  12417. "List of overlays carrying the images of latex fragments.")
  12418. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12419. (defun org-remove-latex-fragment-image-overlays ()
  12420. "Remove all overlays with LaTeX fragment images in current buffer."
  12421. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12422. (setq org-latex-fragment-image-overlays nil))
  12423. (defun org-preview-latex-fragment (&optional subtree)
  12424. "Preview the LaTeX fragment at point, or all locally or globally.
  12425. If the cursor is in a LaTeX fragment, create the image and overlay
  12426. it over the source code. If there is no fragment at point, display
  12427. all fragments in the current text, from one headline to the next. With
  12428. prefix SUBTREE, display all fragments in the current subtree. With a
  12429. double prefix `C-u C-u', or when the cursor is before the first headline,
  12430. display all fragments in the buffer.
  12431. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12432. (interactive "P")
  12433. (org-remove-latex-fragment-image-overlays)
  12434. (save-excursion
  12435. (save-restriction
  12436. (let (beg end at msg)
  12437. (cond
  12438. ((or (equal subtree '(16))
  12439. (not (save-excursion
  12440. (re-search-backward (concat "^" outline-regexp) nil t))))
  12441. (setq beg (point-min) end (point-max)
  12442. msg "Creating images for buffer...%s"))
  12443. ((equal subtree '(4))
  12444. (org-back-to-heading)
  12445. (setq beg (point) end (org-end-of-subtree t)
  12446. msg "Creating images for subtree...%s"))
  12447. (t
  12448. (if (setq at (org-inside-LaTeX-fragment-p))
  12449. (goto-char (max (point-min) (- (cdr at) 2)))
  12450. (org-back-to-heading))
  12451. (setq beg (point) end (progn (outline-next-heading) (point))
  12452. msg (if at "Creating image...%s"
  12453. "Creating images for entry...%s"))))
  12454. (message msg "")
  12455. (narrow-to-region beg end)
  12456. (goto-char beg)
  12457. (org-format-latex
  12458. (concat "ltxpng/" (file-name-sans-extension
  12459. (file-name-nondirectory
  12460. buffer-file-name)))
  12461. default-directory 'overlays msg at 'forbuffer)
  12462. (message msg "done. Use `C-c C-c' to remove images.")))))
  12463. (defvar org-latex-regexps
  12464. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12465. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12466. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12467. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12468. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12469. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12470. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12471. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12472. "Regular expressions for matching embedded LaTeX.")
  12473. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12474. "Replace LaTeX fragments with links to an image, and produce images."
  12475. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12476. (let* ((prefixnodir (file-name-nondirectory prefix))
  12477. (absprefix (expand-file-name prefix dir))
  12478. (todir (file-name-directory absprefix))
  12479. (opt org-format-latex-options)
  12480. (matchers (plist-get opt :matchers))
  12481. (re-list org-latex-regexps)
  12482. (cnt 0) txt link beg end re e checkdir
  12483. executables-checked
  12484. m n block linkfile movefile ov)
  12485. ;; Check if there are old images files with this prefix, and remove them
  12486. (when (file-directory-p todir)
  12487. (mapc 'delete-file
  12488. (directory-files
  12489. todir 'full
  12490. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12491. ;; Check the different regular expressions
  12492. (while (setq e (pop re-list))
  12493. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12494. block (if (nth 3 e) "\n\n" ""))
  12495. (when (member m matchers)
  12496. (goto-char (point-min))
  12497. (while (re-search-forward re nil t)
  12498. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12499. (not (get-text-property (match-beginning n)
  12500. 'org-protected)))
  12501. (setq txt (match-string n)
  12502. beg (match-beginning n) end (match-end n)
  12503. cnt (1+ cnt)
  12504. linkfile (format "%s_%04d.png" prefix cnt)
  12505. movefile (format "%s_%04d.png" absprefix cnt)
  12506. link (concat block "[[file:" linkfile "]]" block))
  12507. (if msg (message msg cnt))
  12508. (goto-char beg)
  12509. (unless checkdir ; make sure the directory exists
  12510. (setq checkdir t)
  12511. (or (file-directory-p todir) (make-directory todir)))
  12512. (unless executables-checked
  12513. (org-check-external-command
  12514. "latex" "needed to convert LaTeX fragments to images")
  12515. (org-check-external-command
  12516. "dvipng" "needed to convert LaTeX fragments to images")
  12517. (setq executables-checked t))
  12518. (org-create-formula-image
  12519. txt movefile opt forbuffer)
  12520. (if overlays
  12521. (progn
  12522. (setq ov (org-make-overlay beg end))
  12523. (if (featurep 'xemacs)
  12524. (progn
  12525. (org-overlay-put ov 'invisible t)
  12526. (org-overlay-put
  12527. ov 'end-glyph
  12528. (make-glyph (vector 'png :file movefile))))
  12529. (org-overlay-put
  12530. ov 'display
  12531. (list 'image :type 'png :file movefile :ascent 'center)))
  12532. (push ov org-latex-fragment-image-overlays)
  12533. (goto-char end))
  12534. (delete-region beg end)
  12535. (insert link))))))))
  12536. ;; This function borrows from Ganesh Swami's latex2png.el
  12537. (defun org-create-formula-image (string tofile options buffer)
  12538. (let* ((tmpdir (if (featurep 'xemacs)
  12539. (temp-directory)
  12540. temporary-file-directory))
  12541. (texfilebase (make-temp-name
  12542. (expand-file-name "orgtex" tmpdir)))
  12543. (texfile (concat texfilebase ".tex"))
  12544. (dvifile (concat texfilebase ".dvi"))
  12545. (pngfile (concat texfilebase ".png"))
  12546. (fnh (if (featurep 'xemacs)
  12547. (font-height (get-face-font 'default))
  12548. (face-attribute 'default :height nil)))
  12549. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12550. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12551. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12552. "Black"))
  12553. (bg (or (plist-get options (if buffer :background :html-background))
  12554. "Transparent")))
  12555. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12556. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12557. (with-temp-file texfile
  12558. (insert org-format-latex-header
  12559. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12560. (let ((dir default-directory))
  12561. (condition-case nil
  12562. (progn
  12563. (cd tmpdir)
  12564. (call-process "latex" nil nil nil texfile))
  12565. (error nil))
  12566. (cd dir))
  12567. (if (not (file-exists-p dvifile))
  12568. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12569. (condition-case nil
  12570. (call-process "dvipng" nil nil nil
  12571. "-E" "-fg" fg "-bg" bg
  12572. "-D" dpi
  12573. ;;"-x" scale "-y" scale
  12574. "-T" "tight"
  12575. "-o" pngfile
  12576. dvifile)
  12577. (error nil))
  12578. (if (not (file-exists-p pngfile))
  12579. (progn (message "Failed to create png file from %s" texfile) nil)
  12580. ;; Use the requested file name and clean up
  12581. (copy-file pngfile tofile 'replace)
  12582. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12583. (delete-file (concat texfilebase e)))
  12584. pngfile))))
  12585. (defun org-dvipng-color (attr)
  12586. "Return an rgb color specification for dvipng."
  12587. (apply 'format "rgb %s %s %s"
  12588. (mapcar 'org-normalize-color
  12589. (color-values (face-attribute 'default attr nil)))))
  12590. (defun org-normalize-color (value)
  12591. "Return string to be used as color value for an RGB component."
  12592. (format "%g" (/ value 65535.0)))
  12593. ;;;; Key bindings
  12594. ;; Make `C-c C-x' a prefix key
  12595. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12596. ;; TAB key with modifiers
  12597. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12598. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12599. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12600. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12601. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12602. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12603. ;; The following line is necessary under Suse GNU/Linux
  12604. (unless (featurep 'xemacs)
  12605. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12606. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12607. (define-key org-mode-map [backtab] 'org-shifttab)
  12608. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12609. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12610. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12611. ;; Cursor keys with modifiers
  12612. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12613. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12614. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12615. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12616. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12617. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12618. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12619. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12620. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12621. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12622. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12623. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12624. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12625. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12626. ;;; Extra keys for tty access.
  12627. ;; We only set them when really needed because otherwise the
  12628. ;; menus don't show the simple keys
  12629. (when (or org-use-extra-keys
  12630. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12631. (not window-system))
  12632. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12633. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12634. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12635. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12636. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12637. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12638. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12639. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12640. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12641. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12642. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12643. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12644. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12645. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12646. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12647. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12648. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12649. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12650. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12651. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12652. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12653. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  12654. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  12655. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  12656. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  12657. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  12658. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  12659. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  12660. ;; All the other keys
  12661. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12662. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12663. (if (boundp 'narrow-map)
  12664. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12665. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12666. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12667. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12668. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12669. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12670. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12671. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12672. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12673. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12674. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12675. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12676. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12677. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12678. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12679. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12680. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12681. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12682. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12683. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12684. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12685. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12686. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12687. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12688. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12689. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12690. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12691. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12692. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12693. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12694. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12695. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12696. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12697. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12698. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12699. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12700. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12701. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12702. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12703. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12704. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12705. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12706. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12707. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12708. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12709. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12710. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12711. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12712. (org-defkey org-mode-map "\C-m" 'org-return)
  12713. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12714. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12715. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12716. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12717. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12718. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12719. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12720. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12721. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12722. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12723. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12724. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12725. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12726. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12727. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12728. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12729. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12730. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12731. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12732. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12733. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12734. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12735. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12736. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12737. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12738. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12739. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12740. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12741. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12742. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12743. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12744. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12745. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12746. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12747. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12748. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12749. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12750. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12751. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12752. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  12753. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12754. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12755. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12756. (when (featurep 'xemacs)
  12757. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12758. (defvar org-self-insert-command-undo-counter 0)
  12759. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12760. (defun org-self-insert-command (N)
  12761. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12762. If the cursor is in a table looking at whitespace, the whitespace is
  12763. overwritten, and the table is not marked as requiring realignment."
  12764. (interactive "p")
  12765. (if (and
  12766. (org-table-p)
  12767. (progn
  12768. ;; check if we blank the field, and if that triggers align
  12769. (and (featurep 'org-table) org-table-auto-blank-field
  12770. (member last-command
  12771. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12772. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12773. ;; got extra space, this field does not determine column width
  12774. (let (org-table-may-need-update) (org-table-blank-field))
  12775. ;; no extra space, this field may determine column width
  12776. (org-table-blank-field)))
  12777. t)
  12778. (eq N 1)
  12779. (looking-at "[^|\n]* |"))
  12780. (let (org-table-may-need-update)
  12781. (goto-char (1- (match-end 0)))
  12782. (delete-backward-char 1)
  12783. (goto-char (match-beginning 0))
  12784. (self-insert-command N))
  12785. (setq org-table-may-need-update t)
  12786. (self-insert-command N)
  12787. (org-fix-tags-on-the-fly)
  12788. (if org-self-insert-cluster-for-undo
  12789. (if (not (eq last-command 'org-self-insert-command))
  12790. (setq org-self-insert-command-undo-counter 1)
  12791. (if (>= org-self-insert-command-undo-counter 20)
  12792. (setq org-self-insert-command-undo-counter 1)
  12793. (and (> org-self-insert-command-undo-counter 0)
  12794. buffer-undo-list
  12795. (not (cadr buffer-undo-list)) ; remove nil entry
  12796. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12797. (setq org-self-insert-command-undo-counter
  12798. (1+ org-self-insert-command-undo-counter)))))))
  12799. (defun org-fix-tags-on-the-fly ()
  12800. (when (and (equal (char-after (point-at-bol)) ?*)
  12801. (org-on-heading-p))
  12802. (org-align-tags-here org-tags-column)))
  12803. (defun org-delete-backward-char (N)
  12804. "Like `delete-backward-char', insert whitespace at field end in tables.
  12805. When deleting backwards, in tables this function will insert whitespace in
  12806. front of the next \"|\" separator, to keep the table aligned. The table will
  12807. still be marked for re-alignment if the field did fill the entire column,
  12808. because, in this case the deletion might narrow the column."
  12809. (interactive "p")
  12810. (if (and (org-table-p)
  12811. (eq N 1)
  12812. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12813. (looking-at ".*?|"))
  12814. (let ((pos (point))
  12815. (noalign (looking-at "[^|\n\r]* |"))
  12816. (c org-table-may-need-update))
  12817. (backward-delete-char N)
  12818. (skip-chars-forward "^|")
  12819. (insert " ")
  12820. (goto-char (1- pos))
  12821. ;; noalign: if there were two spaces at the end, this field
  12822. ;; does not determine the width of the column.
  12823. (if noalign (setq org-table-may-need-update c)))
  12824. (backward-delete-char N)
  12825. (org-fix-tags-on-the-fly)))
  12826. (defun org-delete-char (N)
  12827. "Like `delete-char', but insert whitespace at field end in tables.
  12828. When deleting characters, in tables this function will insert whitespace in
  12829. front of the next \"|\" separator, to keep the table aligned. The table will
  12830. still be marked for re-alignment if the field did fill the entire column,
  12831. because, in this case the deletion might narrow the column."
  12832. (interactive "p")
  12833. (if (and (org-table-p)
  12834. (not (bolp))
  12835. (not (= (char-after) ?|))
  12836. (eq N 1))
  12837. (if (looking-at ".*?|")
  12838. (let ((pos (point))
  12839. (noalign (looking-at "[^|\n\r]* |"))
  12840. (c org-table-may-need-update))
  12841. (replace-match (concat
  12842. (substring (match-string 0) 1 -1)
  12843. " |"))
  12844. (goto-char pos)
  12845. ;; noalign: if there were two spaces at the end, this field
  12846. ;; does not determine the width of the column.
  12847. (if noalign (setq org-table-may-need-update c)))
  12848. (delete-char N))
  12849. (delete-char N)
  12850. (org-fix-tags-on-the-fly)))
  12851. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12852. (put 'org-self-insert-command 'delete-selection t)
  12853. (put 'orgtbl-self-insert-command 'delete-selection t)
  12854. (put 'org-delete-char 'delete-selection 'supersede)
  12855. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12856. (put 'org-yank 'delete-selection 'yank)
  12857. ;; Make `flyspell-mode' delay after some commands
  12858. (put 'org-self-insert-command 'flyspell-delayed t)
  12859. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12860. (put 'org-delete-char 'flyspell-delayed t)
  12861. (put 'org-delete-backward-char 'flyspell-delayed t)
  12862. ;; Make pabbrev-mode expand after org-mode commands
  12863. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12864. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12865. ;; How to do this: Measure non-white length of current string
  12866. ;; If equal to column width, we should realign.
  12867. (defun org-remap (map &rest commands)
  12868. "In MAP, remap the functions given in COMMANDS.
  12869. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12870. (let (new old)
  12871. (while commands
  12872. (setq old (pop commands) new (pop commands))
  12873. (if (fboundp 'command-remapping)
  12874. (org-defkey map (vector 'remap old) new)
  12875. (substitute-key-definition old new map global-map)))))
  12876. (when (eq org-enable-table-editor 'optimized)
  12877. ;; If the user wants maximum table support, we need to hijack
  12878. ;; some standard editing functions
  12879. (org-remap org-mode-map
  12880. 'self-insert-command 'org-self-insert-command
  12881. 'delete-char 'org-delete-char
  12882. 'delete-backward-char 'org-delete-backward-char)
  12883. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12884. (defvar org-ctrl-c-ctrl-c-hook nil
  12885. "Hook for functions attaching themselves to `C-c C-c'.
  12886. This can be used to add additional functionality to the C-c C-c key which
  12887. executes context-dependent commands.
  12888. Each function will be called with no arguments. The function must check
  12889. if the context is appropriate for it to act. If yes, it should do its
  12890. thing and then return a non-nil value. If the context is wrong,
  12891. just do nothing and return nil.")
  12892. (defvar org-tab-first-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 as the first action when TAB is pressed, even before
  12896. `org-cycle' messes around with the `outline-regexp' to cater for
  12897. inline tasks and plain list item folding.
  12898. If any function in this hook returns t, not other actions like table
  12899. field motion visibility cycling will be done.")
  12900. (defvar org-tab-after-check-for-table-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 the cursor is not in a
  12904. table, but before checking if the cursor is in a headline or if global cycling
  12905. should be done.
  12906. If any function in this hook returns t, not other actions like visibility
  12907. cycling will be done.")
  12908. (defvar org-tab-after-check-for-cycling-hook nil
  12909. "Hook for functions to attach themselves to TAB.
  12910. See `org-ctrl-c-ctrl-c-hook' for more information.
  12911. This hook runs after it has been established that not table field motion and
  12912. not visibility should be done because of current context. This is probably
  12913. the place where a package like yasnippets can hook in.")
  12914. (defvar org-metaleft-hook nil
  12915. "Hook for functions attaching themselves to `M-left'.
  12916. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12917. (defvar org-metaright-hook nil
  12918. "Hook for functions attaching themselves to `M-right'.
  12919. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12920. (defvar org-metaup-hook nil
  12921. "Hook for functions attaching themselves to `M-up'.
  12922. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12923. (defvar org-metadown-hook nil
  12924. "Hook for functions attaching themselves to `M-down'.
  12925. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12926. (defvar org-shiftmetaleft-hook nil
  12927. "Hook for functions attaching themselves to `M-S-left'.
  12928. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12929. (defvar org-shiftmetaright-hook nil
  12930. "Hook for functions attaching themselves to `M-S-right'.
  12931. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12932. (defvar org-shiftmetaup-hook nil
  12933. "Hook for functions attaching themselves to `M-S-up'.
  12934. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12935. (defvar org-shiftmetadown-hook nil
  12936. "Hook for functions attaching themselves to `M-S-down'.
  12937. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12938. (defvar org-metareturn-hook nil
  12939. "Hook for functions attaching themselves to `M-RET'.
  12940. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12941. (defun org-modifier-cursor-error ()
  12942. "Throw an error, a modified cursor command was applied in wrong context."
  12943. (error "This command is active in special context like tables, headlines or items"))
  12944. (defun org-shiftselect-error ()
  12945. "Throw an error because Shift-Cursor command was applied in wrong context."
  12946. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12947. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12948. (error "This command works only in special context like headlines or timestamps.")))
  12949. (defun org-call-for-shift-select (cmd)
  12950. (let ((this-command-keys-shift-translated t))
  12951. (call-interactively cmd)))
  12952. (defun org-shifttab (&optional arg)
  12953. "Global visibility cycling or move to previous table field.
  12954. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12955. on context.
  12956. See the individual commands for more information."
  12957. (interactive "P")
  12958. (cond
  12959. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12960. ((integerp arg)
  12961. (message "Content view to level: %d" arg)
  12962. (org-content (prefix-numeric-value arg))
  12963. (setq org-cycle-global-status 'overview))
  12964. (t (call-interactively 'org-global-cycle))))
  12965. (defun org-shiftmetaleft ()
  12966. "Promote subtree or delete table column.
  12967. Calls `org-promote-subtree', `org-outdent-item',
  12968. or `org-table-delete-column', depending on context.
  12969. See the individual commands for more information."
  12970. (interactive)
  12971. (cond
  12972. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12973. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12974. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12975. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12976. (t (org-modifier-cursor-error))))
  12977. (defun org-shiftmetaright ()
  12978. "Demote subtree or insert table column.
  12979. Calls `org-demote-subtree', `org-indent-item',
  12980. or `org-table-insert-column', depending on context.
  12981. See the individual commands for more information."
  12982. (interactive)
  12983. (cond
  12984. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12985. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12986. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12987. ((org-at-item-p) (call-interactively 'org-indent-item))
  12988. (t (org-modifier-cursor-error))))
  12989. (defun org-shiftmetaup (&optional arg)
  12990. "Move subtree up or kill table row.
  12991. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12992. `org-move-item-up' depending on context. See the individual commands
  12993. for more information."
  12994. (interactive "P")
  12995. (cond
  12996. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12997. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12998. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12999. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13000. (t (org-modifier-cursor-error))))
  13001. (defun org-shiftmetadown (&optional arg)
  13002. "Move subtree down or insert table row.
  13003. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13004. `org-move-item-down', depending on context. See the individual
  13005. commands for more information."
  13006. (interactive "P")
  13007. (cond
  13008. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13009. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13010. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13011. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13012. (t (org-modifier-cursor-error))))
  13013. (defun org-metaleft (&optional arg)
  13014. "Promote heading or move table column to left.
  13015. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13016. With no specific context, calls the Emacs default `backward-word'.
  13017. See the individual commands for more information."
  13018. (interactive "P")
  13019. (cond
  13020. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13021. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13022. ((or (org-on-heading-p)
  13023. (and (org-region-active-p)
  13024. (save-excursion
  13025. (goto-char (region-beginning))
  13026. (org-on-heading-p))))
  13027. (call-interactively 'org-do-promote))
  13028. ((or (org-at-item-p)
  13029. (and (org-region-active-p)
  13030. (save-excursion
  13031. (goto-char (region-beginning))
  13032. (org-at-item-p))))
  13033. (call-interactively 'org-outdent-item))
  13034. (t (call-interactively 'backward-word))))
  13035. (defun org-metaright (&optional arg)
  13036. "Demote subtree or move table column to right.
  13037. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13038. With no specific context, calls the Emacs default `forward-word'.
  13039. See the individual commands for more information."
  13040. (interactive "P")
  13041. (cond
  13042. ((run-hook-with-args-until-success 'org-metaright-hook))
  13043. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13044. ((or (org-on-heading-p)
  13045. (and (org-region-active-p)
  13046. (save-excursion
  13047. (goto-char (region-beginning))
  13048. (org-on-heading-p))))
  13049. (call-interactively 'org-do-demote))
  13050. ((or (org-at-item-p)
  13051. (and (org-region-active-p)
  13052. (save-excursion
  13053. (goto-char (region-beginning))
  13054. (org-at-item-p))))
  13055. (call-interactively 'org-indent-item))
  13056. (t (call-interactively 'forward-word))))
  13057. (defun org-metaup (&optional arg)
  13058. "Move subtree up or move table row up.
  13059. Calls `org-move-subtree-up' or `org-table-move-row' or
  13060. `org-move-item-up', depending on context. See the individual commands
  13061. for more information."
  13062. (interactive "P")
  13063. (cond
  13064. ((run-hook-with-args-until-success 'org-metaup-hook))
  13065. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13066. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13067. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13068. (t (transpose-lines 1) (beginning-of-line -1))))
  13069. (defun org-metadown (&optional arg)
  13070. "Move subtree down or move table row down.
  13071. Calls `org-move-subtree-down' or `org-table-move-row' or
  13072. `org-move-item-down', depending on context. See the individual
  13073. commands for more information."
  13074. (interactive "P")
  13075. (cond
  13076. ((run-hook-with-args-until-success 'org-metadown-hook))
  13077. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13078. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13079. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13080. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13081. (defun org-shiftup (&optional arg)
  13082. "Increase item in timestamp or increase priority of current headline.
  13083. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13084. depending on context. See the individual commands for more information."
  13085. (interactive "P")
  13086. (cond
  13087. ((and org-support-shift-select (org-region-active-p))
  13088. (org-call-for-shift-select 'previous-line))
  13089. ((org-at-timestamp-p t)
  13090. (call-interactively (if org-edit-timestamp-down-means-later
  13091. 'org-timestamp-down 'org-timestamp-up)))
  13092. ((and (not (eq org-support-shift-select 'always))
  13093. org-enable-priority-commands
  13094. (org-on-heading-p))
  13095. (call-interactively 'org-priority-up))
  13096. ((and (not org-support-shift-select) (org-at-item-p))
  13097. (call-interactively 'org-previous-item))
  13098. ((org-clocktable-try-shift 'up arg))
  13099. (org-support-shift-select
  13100. (org-call-for-shift-select 'previous-line))
  13101. (t (org-shiftselect-error))))
  13102. (defun org-shiftdown (&optional arg)
  13103. "Decrease item in timestamp or decrease priority of current headline.
  13104. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13105. depending on context. See the individual commands for more information."
  13106. (interactive "P")
  13107. (cond
  13108. ((and org-support-shift-select (org-region-active-p))
  13109. (org-call-for-shift-select 'next-line))
  13110. ((org-at-timestamp-p t)
  13111. (call-interactively (if org-edit-timestamp-down-means-later
  13112. 'org-timestamp-up 'org-timestamp-down)))
  13113. ((and (not (eq org-support-shift-select 'always))
  13114. org-enable-priority-commands
  13115. (org-on-heading-p))
  13116. (call-interactively 'org-priority-down))
  13117. ((and (not org-support-shift-select) (org-at-item-p))
  13118. (call-interactively 'org-next-item))
  13119. ((org-clocktable-try-shift 'down arg))
  13120. (org-support-shift-select
  13121. (org-call-for-shift-select 'next-line))
  13122. (t (org-shiftselect-error))))
  13123. (defun org-shiftright (&optional arg)
  13124. "Cycle the thing at point or in the current line, depending on context.
  13125. Depending on context, this does one of the following:
  13126. - switch a timestamp at point one day into the future
  13127. - on a headline, switch to the next TODO keyword.
  13128. - on an item, switch entire list to the next bullet type
  13129. - on a property line, switch to the next allowed value
  13130. - on a clocktable definition line, move time block into the future"
  13131. (interactive "P")
  13132. (cond
  13133. ((and org-support-shift-select (org-region-active-p))
  13134. (org-call-for-shift-select 'forward-char))
  13135. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13136. ((and (not (eq org-support-shift-select 'always))
  13137. (org-on-heading-p))
  13138. (let ((org-inhibit-logging
  13139. (not org-treat-S-cursor-todo-selection-as-state-change))
  13140. (org-inhibit-blocking
  13141. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13142. (org-call-with-arg 'org-todo 'right)))
  13143. ((or (and org-support-shift-select
  13144. (not (eq org-support-shift-select 'always))
  13145. (org-at-item-bullet-p))
  13146. (and (not org-support-shift-select) (org-at-item-p)))
  13147. (org-call-with-arg 'org-cycle-list-bullet nil))
  13148. ((and (not (eq org-support-shift-select 'always))
  13149. (org-at-property-p))
  13150. (call-interactively 'org-property-next-allowed-value))
  13151. ((org-clocktable-try-shift 'right arg))
  13152. (org-support-shift-select
  13153. (org-call-for-shift-select 'forward-char))
  13154. (t (org-shiftselect-error))))
  13155. (defun org-shiftleft (&optional arg)
  13156. "Cycle the thing at point or in the current line, depending on context.
  13157. Depending on context, this does one of the following:
  13158. - switch a timestamp at point one day into the past
  13159. - on a headline, switch to the previous TODO keyword.
  13160. - on an item, switch entire list to the previous bullet type
  13161. - on a property line, switch to the previous allowed value
  13162. - on a clocktable definition line, move time block into the past"
  13163. (interactive "P")
  13164. (cond
  13165. ((and org-support-shift-select (org-region-active-p))
  13166. (org-call-for-shift-select 'backward-char))
  13167. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13168. ((and (not (eq org-support-shift-select 'always))
  13169. (org-on-heading-p))
  13170. (let ((org-inhibit-logging
  13171. (not org-treat-S-cursor-todo-selection-as-state-change))
  13172. (org-inhibit-blocking
  13173. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13174. (org-call-with-arg 'org-todo 'left)))
  13175. ((or (and org-support-shift-select
  13176. (not (eq org-support-shift-select 'always))
  13177. (org-at-item-bullet-p))
  13178. (and (not org-support-shift-select) (org-at-item-p)))
  13179. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13180. ((and (not (eq org-support-shift-select 'always))
  13181. (org-at-property-p))
  13182. (call-interactively 'org-property-previous-allowed-value))
  13183. ((org-clocktable-try-shift 'left arg))
  13184. (org-support-shift-select
  13185. (org-call-for-shift-select 'backward-char))
  13186. (t (org-shiftselect-error))))
  13187. (defun org-shiftcontrolright ()
  13188. "Switch to next TODO set."
  13189. (interactive)
  13190. (cond
  13191. ((and org-support-shift-select (org-region-active-p))
  13192. (org-call-for-shift-select 'forward-word))
  13193. ((and (not (eq org-support-shift-select 'always))
  13194. (org-on-heading-p))
  13195. (org-call-with-arg 'org-todo 'nextset))
  13196. (org-support-shift-select
  13197. (org-call-for-shift-select 'forward-word))
  13198. (t (org-shiftselect-error))))
  13199. (defun org-shiftcontrolleft ()
  13200. "Switch to previous TODO set."
  13201. (interactive)
  13202. (cond
  13203. ((and org-support-shift-select (org-region-active-p))
  13204. (org-call-for-shift-select 'backward-word))
  13205. ((and (not (eq org-support-shift-select 'always))
  13206. (org-on-heading-p))
  13207. (org-call-with-arg 'org-todo 'previousset))
  13208. (org-support-shift-select
  13209. (org-call-for-shift-select 'backward-word))
  13210. (t (org-shiftselect-error))))
  13211. (defun org-ctrl-c-ret ()
  13212. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13213. (interactive)
  13214. (cond
  13215. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13216. (t (call-interactively 'org-insert-heading))))
  13217. (defun org-copy-special ()
  13218. "Copy region in table or copy current subtree.
  13219. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13220. See the individual commands for more information."
  13221. (interactive)
  13222. (call-interactively
  13223. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13224. (defun org-cut-special ()
  13225. "Cut region in table or cut current subtree.
  13226. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13227. See the individual commands for more information."
  13228. (interactive)
  13229. (call-interactively
  13230. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13231. (defun org-paste-special (arg)
  13232. "Paste rectangular region into table, or past subtree relative to level.
  13233. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13234. See the individual commands for more information."
  13235. (interactive "P")
  13236. (if (org-at-table-p)
  13237. (org-table-paste-rectangle)
  13238. (org-paste-subtree arg)))
  13239. (defun org-edit-special ()
  13240. "Call a special editor for the stuff at point.
  13241. When at a table, call the formula editor with `org-table-edit-formulas'.
  13242. When at the first line of an src example, call `org-edit-src-code'.
  13243. When in an #+include line, visit the include file. Otherwise call
  13244. `ffap' to visit the file at point."
  13245. (interactive)
  13246. (cond
  13247. ((org-at-table-p)
  13248. (call-interactively 'org-table-edit-formulas))
  13249. ((save-excursion
  13250. (beginning-of-line 1)
  13251. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13252. (find-file (org-trim (match-string 1))))
  13253. ((org-edit-src-code))
  13254. ((org-edit-fixed-width-region))
  13255. (t (call-interactively 'ffap))))
  13256. (defun org-ctrl-c-ctrl-c (&optional arg)
  13257. "Set tags in headline, or update according to changed information at point.
  13258. This command does many different things, depending on context:
  13259. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13260. this is what we do.
  13261. - If the cursor is in a headline, prompt for tags and insert them
  13262. into the current line, aligned to `org-tags-column'. When called
  13263. with prefix arg, realign all tags in the current buffer.
  13264. - If the cursor is in one of the special #+KEYWORD lines, this
  13265. triggers scanning the buffer for these lines and updating the
  13266. information.
  13267. - If the cursor is inside a table, realign the table. This command
  13268. works even if the automatic table editor has been turned off.
  13269. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13270. the entire table.
  13271. - If the cursor is at a footnote reference or definition, jump to
  13272. the corresponding definition or references, respectively.
  13273. - If the cursor is a the beginning of a dynamic block, update it.
  13274. - If the cursor is inside a table created by the table.el package,
  13275. activate that table.
  13276. - If the current buffer is a remember buffer, close note and file
  13277. it. A prefix argument of 1 files to the default location
  13278. without further interaction. A prefix argument of 2 files to
  13279. the currently clocking task.
  13280. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13281. links in this buffer.
  13282. - If the cursor is on a numbered item in a plain list, renumber the
  13283. ordered list.
  13284. - If the cursor is on a checkbox, toggle it."
  13285. (interactive "P")
  13286. (let ((org-enable-table-editor t))
  13287. (cond
  13288. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13289. org-occur-highlights
  13290. org-latex-fragment-image-overlays)
  13291. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13292. (org-remove-occur-highlights)
  13293. (org-remove-latex-fragment-image-overlays)
  13294. (message "Temporary highlights/overlays removed from current buffer"))
  13295. ((and (local-variable-p 'org-finish-function (current-buffer))
  13296. (fboundp org-finish-function))
  13297. (funcall org-finish-function))
  13298. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13299. ((org-at-property-p)
  13300. (call-interactively 'org-property-action))
  13301. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13302. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13303. ((org-at-table.el-p)
  13304. (require 'table)
  13305. (beginning-of-line 1)
  13306. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13307. (call-interactively 'table-recognize-table))
  13308. ((org-at-table-p)
  13309. (org-table-maybe-eval-formula)
  13310. (if arg
  13311. (call-interactively 'org-table-recalculate)
  13312. (org-table-maybe-recalculate-line))
  13313. (call-interactively 'org-table-align))
  13314. ((or (org-footnote-at-reference-p)
  13315. (org-footnote-at-definition-p))
  13316. (call-interactively 'org-footnote-action))
  13317. ((org-at-item-checkbox-p)
  13318. (call-interactively 'org-toggle-checkbox))
  13319. ((org-at-item-p)
  13320. (if arg
  13321. (call-interactively 'org-toggle-checkbox)
  13322. (call-interactively 'org-maybe-renumber-ordered-list)))
  13323. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13324. ;; Dynamic block
  13325. (beginning-of-line 1)
  13326. (save-excursion (org-update-dblock)))
  13327. ((save-excursion
  13328. (beginning-of-line 1)
  13329. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13330. (cond
  13331. ((equal (match-string 1) "TBLFM")
  13332. ;; Recalculate the table before this line
  13333. (save-excursion
  13334. (beginning-of-line 1)
  13335. (skip-chars-backward " \r\n\t")
  13336. (if (org-at-table-p)
  13337. (org-call-with-arg 'org-table-recalculate t))))
  13338. (t
  13339. ; (org-set-regexps-and-options)
  13340. ; (org-restart-font-lock)
  13341. (let ((org-inhibit-startup t)) (org-mode-restart))
  13342. (message "Local setup has been refreshed"))))
  13343. ((org-clock-update-time-maybe))
  13344. (t (error "C-c C-c can do nothing useful at this location.")))))
  13345. (defun org-mode-restart ()
  13346. "Restart Org-mode, to scan again for special lines.
  13347. Also updates the keyword regular expressions."
  13348. (interactive)
  13349. (org-mode)
  13350. (message "Org-mode restarted"))
  13351. (defun org-kill-note-or-show-branches ()
  13352. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13353. (interactive)
  13354. (if (not org-finish-function)
  13355. (call-interactively 'show-branches)
  13356. (let ((org-note-abort t))
  13357. (funcall org-finish-function))))
  13358. (defun org-return (&optional indent)
  13359. "Goto next table row or insert a newline.
  13360. Calls `org-table-next-row' or `newline', depending on context.
  13361. See the individual commands for more information."
  13362. (interactive)
  13363. (cond
  13364. ((bobp) (if indent (newline-and-indent) (newline)))
  13365. ((org-at-table-p)
  13366. (org-table-justify-field-maybe)
  13367. (call-interactively 'org-table-next-row))
  13368. ((and org-return-follows-link
  13369. (eq (get-text-property (point) 'face) 'org-link))
  13370. (call-interactively 'org-open-at-point))
  13371. ((and (org-at-heading-p)
  13372. (looking-at
  13373. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13374. (org-show-entry)
  13375. (end-of-line 1)
  13376. (newline))
  13377. (t (if indent (newline-and-indent) (newline)))))
  13378. (defun org-return-indent ()
  13379. "Goto next table row or insert a newline and indent.
  13380. Calls `org-table-next-row' or `newline-and-indent', depending on
  13381. context. See the individual commands for more information."
  13382. (interactive)
  13383. (org-return t))
  13384. (defun org-ctrl-c-star ()
  13385. "Compute table, or change heading status of lines.
  13386. Calls `org-table-recalculate' or `org-toggle-heading',
  13387. depending on context."
  13388. (interactive)
  13389. (cond
  13390. ((org-at-table-p)
  13391. (call-interactively 'org-table-recalculate))
  13392. (t
  13393. ;; Convert all lines in region to list items
  13394. (call-interactively 'org-toggle-heading))))
  13395. (defun org-ctrl-c-minus ()
  13396. "Insert separator line in table or modify bullet status of line.
  13397. Also turns a plain line or a region of lines into list items.
  13398. Calls `org-table-insert-hline', `org-toggle-item', or
  13399. `org-cycle-list-bullet', depending on context."
  13400. (interactive)
  13401. (cond
  13402. ((org-at-table-p)
  13403. (call-interactively 'org-table-insert-hline))
  13404. ((org-region-active-p)
  13405. (call-interactively 'org-toggle-item))
  13406. ((org-in-item-p)
  13407. (call-interactively 'org-cycle-list-bullet))
  13408. (t
  13409. (call-interactively 'org-toggle-item))))
  13410. (defun org-toggle-item ()
  13411. "Convert headings or normal lines to items, items to normal lines.
  13412. If there is no active region, only the current line is considered.
  13413. If the first line in the region is a headline, convert all headlines to items.
  13414. If the first line in the region is an item, convert all items to normal lines.
  13415. If the first line is normal text, add an item bullet to each line."
  13416. (interactive)
  13417. (let (l2 l beg end)
  13418. (if (org-region-active-p)
  13419. (setq beg (region-beginning) end (region-end))
  13420. (setq beg (point-at-bol)
  13421. end (min (1+ (point-at-eol)) (point-max))))
  13422. (save-excursion
  13423. (goto-char end)
  13424. (setq l2 (org-current-line))
  13425. (goto-char beg)
  13426. (beginning-of-line 1)
  13427. (setq l (1- (org-current-line)))
  13428. (if (org-at-item-p)
  13429. ;; We already have items, de-itemize
  13430. (while (< (setq l (1+ l)) l2)
  13431. (when (org-at-item-p)
  13432. (goto-char (match-beginning 2))
  13433. (delete-region (match-beginning 2) (match-end 2))
  13434. (and (looking-at "[ \t]+") (replace-match "")))
  13435. (beginning-of-line 2))
  13436. (if (org-on-heading-p)
  13437. ;; Headings, convert to items
  13438. (while (< (setq l (1+ l)) l2)
  13439. (if (looking-at org-outline-regexp)
  13440. (replace-match "- " t t))
  13441. (beginning-of-line 2))
  13442. ;; normal lines, turn them into items
  13443. (while (< (setq l (1+ l)) l2)
  13444. (unless (org-at-item-p)
  13445. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13446. (replace-match "\\1- \\2")))
  13447. (beginning-of-line 2)))))))
  13448. (defun org-toggle-heading (&optional nstars)
  13449. "Convert headings to normal text, or items or text to headings.
  13450. If there is no active region, only the current line is considered.
  13451. If the first line is a heading, remove the stars from all headlines
  13452. in the region.
  13453. If the first line is a plain list item, turn all plain list items into
  13454. headings.
  13455. If the first line is a normal line, turn each and every line in the region
  13456. into a heading.
  13457. When converting a line into a heading, the number of stars is chosen
  13458. such that the lines become children of the current entry. However, when
  13459. a prefix argument is given, its value determines the number of stars to add."
  13460. (interactive "P")
  13461. (let (l2 l itemp beg end)
  13462. (if (org-region-active-p)
  13463. (setq beg (region-beginning) end (region-end))
  13464. (setq beg (point-at-bol)
  13465. end (min (1+ (point-at-eol)) (point-max))))
  13466. (save-excursion
  13467. (goto-char end)
  13468. (setq l2 (org-current-line))
  13469. (goto-char beg)
  13470. (beginning-of-line 1)
  13471. (setq l (1- (org-current-line)))
  13472. (if (org-on-heading-p)
  13473. ;; We already have headlines, de-star them
  13474. (while (< (setq l (1+ l)) l2)
  13475. (when (org-on-heading-p t)
  13476. (and (looking-at outline-regexp) (replace-match "")))
  13477. (beginning-of-line 2))
  13478. (setq itemp (org-at-item-p))
  13479. (let* ((stars
  13480. (if nstars
  13481. (make-string (prefix-numeric-value current-prefix-arg)
  13482. ?*)
  13483. (save-excursion
  13484. (re-search-backward org-complex-heading-regexp nil t)
  13485. (or (match-string 1) ""))))
  13486. (add-stars (cond (nstars "")
  13487. ((equal stars "") "*")
  13488. (org-odd-levels-only "**")
  13489. (t "*")))
  13490. (rpl (concat stars add-stars " ")))
  13491. (while (< (setq l (1+ l)) l2)
  13492. (if itemp
  13493. (and (org-at-item-p) (replace-match rpl t t))
  13494. (unless (org-on-heading-p)
  13495. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13496. (replace-match (concat rpl (match-string 2))))))
  13497. (beginning-of-line 2)))))))
  13498. (defun org-meta-return (&optional arg)
  13499. "Insert a new heading or wrap a region in a table.
  13500. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13501. See the individual commands for more information."
  13502. (interactive "P")
  13503. (cond
  13504. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13505. ((org-at-table-p)
  13506. (call-interactively 'org-table-wrap-region))
  13507. (t (call-interactively 'org-insert-heading))))
  13508. ;;; Menu entries
  13509. ;; Define the Org-mode menus
  13510. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13511. '("Tbl"
  13512. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13513. ["Next Field" org-cycle (org-at-table-p)]
  13514. ["Previous Field" org-shifttab (org-at-table-p)]
  13515. ["Next Row" org-return (org-at-table-p)]
  13516. "--"
  13517. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13518. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13519. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13520. "--"
  13521. ("Column"
  13522. ["Move Column Left" org-metaleft (org-at-table-p)]
  13523. ["Move Column Right" org-metaright (org-at-table-p)]
  13524. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13525. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13526. ("Row"
  13527. ["Move Row Up" org-metaup (org-at-table-p)]
  13528. ["Move Row Down" org-metadown (org-at-table-p)]
  13529. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13530. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13531. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13532. "--"
  13533. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13534. ("Rectangle"
  13535. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13536. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13537. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13538. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13539. "--"
  13540. ("Calculate"
  13541. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13542. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13543. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13544. "--"
  13545. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13546. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13547. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13548. "--"
  13549. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13550. "--"
  13551. ["Sum Column/Rectangle" org-table-sum
  13552. (or (org-at-table-p) (org-region-active-p))]
  13553. ["Which Column?" org-table-current-column (org-at-table-p)])
  13554. ["Debug Formulas"
  13555. org-table-toggle-formula-debugger
  13556. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13557. ["Show Col/Row Numbers"
  13558. org-table-toggle-coordinate-overlays
  13559. :style toggle
  13560. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13561. "--"
  13562. ["Create" org-table-create (and (not (org-at-table-p))
  13563. org-enable-table-editor)]
  13564. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13565. ["Import from File" org-table-import (not (org-at-table-p))]
  13566. ["Export to File" org-table-export (org-at-table-p)]
  13567. "--"
  13568. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13569. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13570. '("Org"
  13571. ("Show/Hide"
  13572. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13573. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13574. ["Sparse Tree..." org-sparse-tree t]
  13575. ["Reveal Context" org-reveal t]
  13576. ["Show All" show-all t]
  13577. "--"
  13578. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13579. "--"
  13580. ["New Heading" org-insert-heading t]
  13581. ("Navigate Headings"
  13582. ["Up" outline-up-heading t]
  13583. ["Next" outline-next-visible-heading t]
  13584. ["Previous" outline-previous-visible-heading t]
  13585. ["Next Same Level" outline-forward-same-level t]
  13586. ["Previous Same Level" outline-backward-same-level t]
  13587. "--"
  13588. ["Jump" org-goto t])
  13589. ("Edit Structure"
  13590. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13591. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13592. "--"
  13593. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13594. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13595. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13596. "--"
  13597. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13598. "--"
  13599. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13600. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13601. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13602. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13603. "--"
  13604. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13605. "--"
  13606. ["Convert to odd levels" org-convert-to-odd-levels t]
  13607. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13608. ("Editing"
  13609. ["Emphasis..." org-emphasize t]
  13610. ["Edit Source Example" org-edit-special t]
  13611. "--"
  13612. ["Footnote new/jump" org-footnote-action t]
  13613. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13614. ("Archive"
  13615. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13616. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13617. ; :active t :keys "C-u C-c C-x C-a"]
  13618. ["Sparse trees open ARCHIVE trees"
  13619. (setq org-sparse-tree-open-archived-trees
  13620. (not org-sparse-tree-open-archived-trees))
  13621. :style toggle :selected org-sparse-tree-open-archived-trees]
  13622. ["Cycling opens ARCHIVE trees"
  13623. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13624. :style toggle :selected org-cycle-open-archived-trees]
  13625. "--"
  13626. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13627. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13628. ; ["Check and Move Children" (org-archive-subtree '(4))
  13629. ; :active t :keys "C-u C-c C-x C-s"]
  13630. )
  13631. "--"
  13632. ("Hyperlinks"
  13633. ["Store Link (Global)" org-store-link t]
  13634. ["Insert Link" org-insert-link t]
  13635. ["Follow Link" org-open-at-point t]
  13636. "--"
  13637. ["Next link" org-next-link t]
  13638. ["Previous link" org-previous-link t]
  13639. "--"
  13640. ["Descriptive Links"
  13641. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13642. :style radio
  13643. :selected (member '(org-link) buffer-invisibility-spec)]
  13644. ["Literal Links"
  13645. (progn
  13646. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13647. :style radio
  13648. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13649. "--"
  13650. ("TODO Lists"
  13651. ["TODO/DONE/-" org-todo t]
  13652. ("Select keyword"
  13653. ["Next keyword" org-shiftright (org-on-heading-p)]
  13654. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13655. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13656. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13657. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13658. ["Show TODO Tree" org-show-todo-tree t]
  13659. ["Global TODO list" org-todo-list t]
  13660. "--"
  13661. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13662. :selected org-enforce-todo-dependencies :style toggle :active t]
  13663. "Settings for tree at point"
  13664. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13665. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13666. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13667. ["Do Children parallel" org-toggle-ordered-property :style radio
  13668. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13669. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13670. "--"
  13671. ["Set Priority" org-priority t]
  13672. ["Priority Up" org-shiftup t]
  13673. ["Priority Down" org-shiftdown t]
  13674. "--"
  13675. ["Get news from all feeds" org-feed-update-all t]
  13676. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13677. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13678. ("TAGS and Properties"
  13679. ["Set Tags" org-set-tags-command t]
  13680. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13681. "--"
  13682. ["Set property" org-set-property t]
  13683. ["Column view of properties" org-columns t]
  13684. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13685. ("Dates and Scheduling"
  13686. ["Timestamp" org-time-stamp t]
  13687. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13688. ("Change Date"
  13689. ["1 Day Later" org-shiftright t]
  13690. ["1 Day Earlier" org-shiftleft t]
  13691. ["1 ... Later" org-shiftup t]
  13692. ["1 ... Earlier" org-shiftdown t])
  13693. ["Compute Time Range" org-evaluate-time-range t]
  13694. ["Schedule Item" org-schedule t]
  13695. ["Deadline" org-deadline t]
  13696. "--"
  13697. ["Custom time format" org-toggle-time-stamp-overlays
  13698. :style radio :selected org-display-custom-times]
  13699. "--"
  13700. ["Goto Calendar" org-goto-calendar t]
  13701. ["Date from Calendar" org-date-from-calendar t]
  13702. "--"
  13703. ["Start/Restart Timer" org-timer-start t]
  13704. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13705. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13706. ["Insert Timer String" org-timer t]
  13707. ["Insert Timer Item" org-timer-item t])
  13708. ("Logging work"
  13709. ["Clock in" org-clock-in t]
  13710. ["Clock out" org-clock-out t]
  13711. ["Clock cancel" org-clock-cancel t]
  13712. ["Goto running clock" org-clock-goto t]
  13713. ["Display times" org-clock-display t]
  13714. ["Create clock table" org-clock-report t]
  13715. "--"
  13716. ["Record DONE time"
  13717. (progn (setq org-log-done (not org-log-done))
  13718. (message "Switching to %s will %s record a timestamp"
  13719. (car org-done-keywords)
  13720. (if org-log-done "automatically" "not")))
  13721. :style toggle :selected org-log-done])
  13722. "--"
  13723. ["Agenda Command..." org-agenda t]
  13724. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13725. ("File List for Agenda")
  13726. ("Special views current file"
  13727. ["TODO Tree" org-show-todo-tree t]
  13728. ["Check Deadlines" org-check-deadlines t]
  13729. ["Timeline" org-timeline t]
  13730. ["Tags/Property tree" org-match-sparse-tree t])
  13731. "--"
  13732. ["Export/Publish..." org-export t]
  13733. ("LaTeX"
  13734. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13735. :selected org-cdlatex-mode]
  13736. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13737. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13738. ["Modify math symbol" org-cdlatex-math-modify
  13739. (org-inside-LaTeX-fragment-p)]
  13740. ["Insert citation" org-reftex-citation t]
  13741. "--"
  13742. ["Export LaTeX fragments as images"
  13743. (if (featurep 'org-exp)
  13744. (setq org-export-with-LaTeX-fragments
  13745. (not org-export-with-LaTeX-fragments))
  13746. (require 'org-exp))
  13747. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13748. org-export-with-LaTeX-fragments)])
  13749. "--"
  13750. ("Documentation"
  13751. ["Show Version" org-version t]
  13752. ["Info Documentation" org-info t])
  13753. ("Customize"
  13754. ["Browse Org Group" org-customize t]
  13755. "--"
  13756. ["Expand This Menu" org-create-customize-menu
  13757. (fboundp 'customize-menu-create)])
  13758. "--"
  13759. ("Refresh/Reload"
  13760. ["Refresh setup current buffer" org-mode-restart t]
  13761. ["Reload Org (after update)" org-reload t]
  13762. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13763. ))
  13764. (defun org-info (&optional node)
  13765. "Read documentation for Org-mode in the info system.
  13766. With optional NODE, go directly to that node."
  13767. (interactive)
  13768. (info (format "(org)%s" (or node ""))))
  13769. (defun org-install-agenda-files-menu ()
  13770. (let ((bl (buffer-list)))
  13771. (save-excursion
  13772. (while bl
  13773. (set-buffer (pop bl))
  13774. (if (org-mode-p) (setq bl nil)))
  13775. (when (org-mode-p)
  13776. (easy-menu-change
  13777. '("Org") "File List for Agenda"
  13778. (append
  13779. (list
  13780. ["Edit File List" (org-edit-agenda-file-list) t]
  13781. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13782. ["Remove Current File from List" org-remove-file t]
  13783. ["Cycle through agenda files" org-cycle-agenda-files t]
  13784. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13785. "--")
  13786. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13787. ;;;; Documentation
  13788. ;;;###autoload
  13789. (defun org-require-autoloaded-modules ()
  13790. (interactive)
  13791. (mapc 'require
  13792. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13793. org-docbook org-exp org-html org-icalendar
  13794. org-id org-latex
  13795. org-publish org-remember org-table
  13796. org-timer org-xoxo)))
  13797. ;;;###autoload
  13798. (defun org-reload (&optional uncompiled)
  13799. "Reload all org lisp files.
  13800. With prefix arg UNCOMPILED, load the uncompiled versions."
  13801. (interactive "P")
  13802. (require 'find-func)
  13803. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13804. (dir-org (file-name-directory (org-find-library-name "org")))
  13805. (dir-org-contrib (ignore-errors
  13806. (file-name-directory
  13807. (org-find-library-name "org-contribdir"))))
  13808. (files
  13809. (append (directory-files dir-org t file-re)
  13810. (and dir-org-contrib
  13811. (directory-files dir-org-contrib t file-re))))
  13812. (remove-re (concat (if (featurep 'xemacs)
  13813. "org-colview" "org-colview-xemacs")
  13814. "\\'")))
  13815. (setq files (mapcar 'file-name-sans-extension files))
  13816. (setq files (mapcar
  13817. (lambda (x) (if (string-match remove-re x) nil x))
  13818. files))
  13819. (setq files (delq nil files))
  13820. (mapc
  13821. (lambda (f)
  13822. (when (featurep (intern (file-name-nondirectory f)))
  13823. (if (and (not uncompiled)
  13824. (file-exists-p (concat f ".elc")))
  13825. (load (concat f ".elc") nil nil t)
  13826. (load (concat f ".el") nil nil t))))
  13827. files))
  13828. (org-version))
  13829. ;;;###autoload
  13830. (defun org-customize ()
  13831. "Call the customize function with org as argument."
  13832. (interactive)
  13833. (org-load-modules-maybe)
  13834. (org-require-autoloaded-modules)
  13835. (customize-browse 'org))
  13836. (defun org-create-customize-menu ()
  13837. "Create a full customization menu for Org-mode, insert it into the menu."
  13838. (interactive)
  13839. (org-load-modules-maybe)
  13840. (org-require-autoloaded-modules)
  13841. (if (fboundp 'customize-menu-create)
  13842. (progn
  13843. (easy-menu-change
  13844. '("Org") "Customize"
  13845. `(["Browse Org group" org-customize t]
  13846. "--"
  13847. ,(customize-menu-create 'org)
  13848. ["Set" Custom-set t]
  13849. ["Save" Custom-save t]
  13850. ["Reset to Current" Custom-reset-current t]
  13851. ["Reset to Saved" Custom-reset-saved t]
  13852. ["Reset to Standard Settings" Custom-reset-standard t]))
  13853. (message "\"Org\"-menu now contains full customization menu"))
  13854. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13855. ;;;; Miscellaneous stuff
  13856. ;;; Generally useful functions
  13857. (defun org-find-text-property-in-string (prop s)
  13858. "Return the first non-nil value of property PROP in string S."
  13859. (or (get-text-property 0 prop s)
  13860. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13861. prop s)))
  13862. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13863. "Display the given MESSAGE as a warning."
  13864. (if (fboundp 'display-warning)
  13865. (display-warning 'org message
  13866. (if (featurep 'xemacs)
  13867. 'warning
  13868. :warning))
  13869. (let ((buf (get-buffer-create "*Org warnings*")))
  13870. (with-current-buffer buf
  13871. (goto-char (point-max))
  13872. (insert "Warning (Org): " message)
  13873. (unless (bolp)
  13874. (newline)))
  13875. (display-buffer buf)
  13876. (sit-for 0))))
  13877. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13878. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13879. (if (and marker (marker-buffer marker)
  13880. (buffer-live-p (marker-buffer marker)))
  13881. (progn
  13882. (switch-to-buffer (marker-buffer marker))
  13883. (if (or (> marker (point-max)) (< marker (point-min)))
  13884. (widen))
  13885. (goto-char marker)
  13886. (org-show-context 'org-goto))
  13887. (if bookmark
  13888. (bookmark-jump bookmark)
  13889. (error "Cannot find location"))))
  13890. (defun org-quote-csv-field (s)
  13891. "Quote field for inclusion in CSV material."
  13892. (if (string-match "[\",]" s)
  13893. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13894. s))
  13895. (defun org-plist-delete (plist property)
  13896. "Delete PROPERTY from PLIST.
  13897. This is in contrast to merely setting it to 0."
  13898. (let (p)
  13899. (while plist
  13900. (if (not (eq property (car plist)))
  13901. (setq p (plist-put p (car plist) (nth 1 plist))))
  13902. (setq plist (cddr plist)))
  13903. p))
  13904. (defun org-force-self-insert (N)
  13905. "Needed to enforce self-insert under remapping."
  13906. (interactive "p")
  13907. (self-insert-command N))
  13908. (defun org-string-width (s)
  13909. "Compute width of string, ignoring invisible characters.
  13910. This ignores character with invisibility property `org-link', and also
  13911. characters with property `org-cwidth', because these will become invisible
  13912. upon the next fontification round."
  13913. (let (b l)
  13914. (when (or (eq t buffer-invisibility-spec)
  13915. (assq 'org-link buffer-invisibility-spec))
  13916. (while (setq b (text-property-any 0 (length s)
  13917. 'invisible 'org-link s))
  13918. (setq s (concat (substring s 0 b)
  13919. (substring s (or (next-single-property-change
  13920. b 'invisible s) (length s)))))))
  13921. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13922. (setq s (concat (substring s 0 b)
  13923. (substring s (or (next-single-property-change
  13924. b 'org-cwidth s) (length s))))))
  13925. (setq l (string-width s) b -1)
  13926. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13927. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13928. l))
  13929. (defun org-get-indentation (&optional line)
  13930. "Get the indentation of the current line, interpreting tabs.
  13931. When LINE is given, assume it represents a line and compute its indentation."
  13932. (if line
  13933. (if (string-match "^ *" (org-remove-tabs line))
  13934. (match-end 0))
  13935. (save-excursion
  13936. (beginning-of-line 1)
  13937. (skip-chars-forward " \t")
  13938. (current-column))))
  13939. (defun org-remove-tabs (s &optional width)
  13940. "Replace tabulators in S with spaces.
  13941. Assumes that s is a single line, starting in column 0."
  13942. (setq width (or width tab-width))
  13943. (while (string-match "\t" s)
  13944. (setq s (replace-match
  13945. (make-string
  13946. (- (* width (/ (+ (match-beginning 0) width) width))
  13947. (match-beginning 0)) ?\ )
  13948. t t s)))
  13949. s)
  13950. (defun org-fix-indentation (line ind)
  13951. "Fix indentation in LINE.
  13952. IND is a cons cell with target and minimum indentation.
  13953. If the current indentation in LINE is smaller than the minimum,
  13954. leave it alone. If it is larger than ind, set it to the target."
  13955. (let* ((l (org-remove-tabs line))
  13956. (i (org-get-indentation l))
  13957. (i1 (car ind)) (i2 (cdr ind)))
  13958. (if (>= i i2) (setq l (substring line i2)))
  13959. (if (> i1 0)
  13960. (concat (make-string i1 ?\ ) l)
  13961. l)))
  13962. (defun org-remove-indentation (code &optional n)
  13963. "Remove the maximum common indentation from the lines in CODE.
  13964. N may optionally be the number of spaces to remove."
  13965. (with-temp-buffer
  13966. (insert code)
  13967. (org-do-remove-indentation n)
  13968. (buffer-string)))
  13969. (defun org-do-remove-indentation (&optional n)
  13970. "Remove the maximum common indentation from the buffer."
  13971. (untabify (point-min) (point-max))
  13972. (let ((min 10000) re)
  13973. (if n
  13974. (setq min n)
  13975. (goto-char (point-min))
  13976. (while (re-search-forward "^ *[^ \n]" nil t)
  13977. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  13978. (unless (or (= min 0) (= min 10000))
  13979. (setq re (format "^ \\{%d\\}" min))
  13980. (goto-char (point-min))
  13981. (while (re-search-forward re nil t)
  13982. (replace-match "")
  13983. (end-of-line 1))
  13984. min)))
  13985. (defun org-base-buffer (buffer)
  13986. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13987. (if (not buffer)
  13988. buffer
  13989. (or (buffer-base-buffer buffer)
  13990. buffer)))
  13991. (defun org-trim (s)
  13992. "Remove whitespace at beginning and end of string."
  13993. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13994. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13995. s)
  13996. (defun org-wrap (string &optional width lines)
  13997. "Wrap string to either a number of lines, or a width in characters.
  13998. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13999. that costs. If there is a word longer than WIDTH, the text is actually
  14000. wrapped to the length of that word.
  14001. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14002. many lines, whatever width that takes.
  14003. The return value is a list of lines, without newlines at the end."
  14004. (let* ((words (org-split-string string "[ \t\n]+"))
  14005. (maxword (apply 'max (mapcar 'org-string-width words)))
  14006. w ll)
  14007. (cond (width
  14008. (org-do-wrap words (max maxword width)))
  14009. (lines
  14010. (setq w maxword)
  14011. (setq ll (org-do-wrap words maxword))
  14012. (if (<= (length ll) lines)
  14013. ll
  14014. (setq ll words)
  14015. (while (> (length ll) lines)
  14016. (setq w (1+ w))
  14017. (setq ll (org-do-wrap words w)))
  14018. ll))
  14019. (t (error "Cannot wrap this")))))
  14020. (defun org-do-wrap (words width)
  14021. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14022. (let (lines line)
  14023. (while words
  14024. (setq line (pop words))
  14025. (while (and words (< (+ (length line) (length (car words))) width))
  14026. (setq line (concat line " " (pop words))))
  14027. (setq lines (push line lines)))
  14028. (nreverse lines)))
  14029. (defun org-split-string (string &optional separators)
  14030. "Splits STRING into substrings at SEPARATORS.
  14031. No empty strings are returned if there are matches at the beginning
  14032. and end of string."
  14033. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14034. (start 0)
  14035. notfirst
  14036. (list nil))
  14037. (while (and (string-match rexp string
  14038. (if (and notfirst
  14039. (= start (match-beginning 0))
  14040. (< start (length string)))
  14041. (1+ start) start))
  14042. (< (match-beginning 0) (length string)))
  14043. (setq notfirst t)
  14044. (or (eq (match-beginning 0) 0)
  14045. (and (eq (match-beginning 0) (match-end 0))
  14046. (eq (match-beginning 0) start))
  14047. (setq list
  14048. (cons (substring string start (match-beginning 0))
  14049. list)))
  14050. (setq start (match-end 0)))
  14051. (or (eq start (length string))
  14052. (setq list
  14053. (cons (substring string start)
  14054. list)))
  14055. (nreverse list)))
  14056. (defun org-uuidgen-p (s)
  14057. "Is S an ID created by UUIDGEN?"
  14058. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14059. (defun org-context ()
  14060. "Return a list of contexts of the current cursor position.
  14061. If several contexts apply, all are returned.
  14062. Each context entry is a list with a symbol naming the context, and
  14063. two positions indicating start and end of the context. Possible
  14064. contexts are:
  14065. :headline anywhere in a headline
  14066. :headline-stars on the leading stars in a headline
  14067. :todo-keyword on a TODO keyword (including DONE) in a headline
  14068. :tags on the TAGS in a headline
  14069. :priority on the priority cookie in a headline
  14070. :item on the first line of a plain list item
  14071. :item-bullet on the bullet/number of a plain list item
  14072. :checkbox on the checkbox in a plain list item
  14073. :table in an org-mode table
  14074. :table-special on a special filed in a table
  14075. :table-table in a table.el table
  14076. :link on a hyperlink
  14077. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14078. :target on a <<target>>
  14079. :radio-target on a <<<radio-target>>>
  14080. :latex-fragment on a LaTeX fragment
  14081. :latex-preview on a LaTeX fragment with overlayed preview image
  14082. This function expects the position to be visible because it uses font-lock
  14083. faces as a help to recognize the following contexts: :table-special, :link,
  14084. and :keyword."
  14085. (let* ((f (get-text-property (point) 'face))
  14086. (faces (if (listp f) f (list f)))
  14087. (p (point)) clist o)
  14088. ;; First the large context
  14089. (cond
  14090. ((org-on-heading-p t)
  14091. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14092. (when (progn
  14093. (beginning-of-line 1)
  14094. (looking-at org-todo-line-tags-regexp))
  14095. (push (org-point-in-group p 1 :headline-stars) clist)
  14096. (push (org-point-in-group p 2 :todo-keyword) clist)
  14097. (push (org-point-in-group p 4 :tags) clist))
  14098. (goto-char p)
  14099. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14100. (if (looking-at "\\[#[A-Z0-9]\\]")
  14101. (push (org-point-in-group p 0 :priority) clist)))
  14102. ((org-at-item-p)
  14103. (push (org-point-in-group p 2 :item-bullet) clist)
  14104. (push (list :item (point-at-bol)
  14105. (save-excursion (org-end-of-item) (point)))
  14106. clist)
  14107. (and (org-at-item-checkbox-p)
  14108. (push (org-point-in-group p 0 :checkbox) clist)))
  14109. ((org-at-table-p)
  14110. (push (list :table (org-table-begin) (org-table-end)) clist)
  14111. (if (memq 'org-formula faces)
  14112. (push (list :table-special
  14113. (previous-single-property-change p 'face)
  14114. (next-single-property-change p 'face)) clist)))
  14115. ((org-at-table-p 'any)
  14116. (push (list :table-table) clist)))
  14117. (goto-char p)
  14118. ;; Now the small context
  14119. (cond
  14120. ((org-at-timestamp-p)
  14121. (push (org-point-in-group p 0 :timestamp) clist))
  14122. ((memq 'org-link faces)
  14123. (push (list :link
  14124. (previous-single-property-change p 'face)
  14125. (next-single-property-change p 'face)) clist))
  14126. ((memq 'org-special-keyword faces)
  14127. (push (list :keyword
  14128. (previous-single-property-change p 'face)
  14129. (next-single-property-change p 'face)) clist))
  14130. ((org-on-target-p)
  14131. (push (org-point-in-group p 0 :target) clist)
  14132. (goto-char (1- (match-beginning 0)))
  14133. (if (looking-at org-radio-target-regexp)
  14134. (push (org-point-in-group p 0 :radio-target) clist))
  14135. (goto-char p))
  14136. ((setq o (car (delq nil
  14137. (mapcar
  14138. (lambda (x)
  14139. (if (memq x org-latex-fragment-image-overlays) x))
  14140. (org-overlays-at (point))))))
  14141. (push (list :latex-fragment
  14142. (org-overlay-start o) (org-overlay-end o)) clist)
  14143. (push (list :latex-preview
  14144. (org-overlay-start o) (org-overlay-end o)) clist))
  14145. ((org-inside-LaTeX-fragment-p)
  14146. ;; FIXME: positions wrong.
  14147. (push (list :latex-fragment (point) (point)) clist)))
  14148. (setq clist (nreverse (delq nil clist)))
  14149. clist))
  14150. ;; FIXME: Compare with at-regexp-p Do we need both?
  14151. (defun org-in-regexp (re &optional nlines visually)
  14152. "Check if point is inside a match of regexp.
  14153. Normally only the current line is checked, but you can include NLINES extra
  14154. lines both before and after point into the search.
  14155. If VISUALLY is set, require that the cursor is not after the match but
  14156. really on, so that the block visually is on the match."
  14157. (catch 'exit
  14158. (let ((pos (point))
  14159. (eol (point-at-eol (+ 1 (or nlines 0))))
  14160. (inc (if visually 1 0)))
  14161. (save-excursion
  14162. (beginning-of-line (- 1 (or nlines 0)))
  14163. (while (re-search-forward re eol t)
  14164. (if (and (<= (match-beginning 0) pos)
  14165. (>= (+ inc (match-end 0)) pos))
  14166. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14167. (defun org-at-regexp-p (regexp)
  14168. "Is point inside a match of REGEXP in the current line?"
  14169. (catch 'exit
  14170. (save-excursion
  14171. (let ((pos (point)) (end (point-at-eol)))
  14172. (beginning-of-line 1)
  14173. (while (re-search-forward regexp end t)
  14174. (if (and (<= (match-beginning 0) pos)
  14175. (>= (match-end 0) pos))
  14176. (throw 'exit t)))
  14177. nil))))
  14178. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14179. "Call `multi-occur' with buffers for all agenda files."
  14180. (interactive "sOrg-files matching: \np")
  14181. (let* ((files (org-agenda-files))
  14182. (tnames (mapcar 'file-truename files))
  14183. (extra org-agenda-text-search-extra-files)
  14184. f)
  14185. (when (eq (car extra) 'agenda-archives)
  14186. (setq extra (cdr extra))
  14187. (setq files (org-add-archive-files files)))
  14188. (while (setq f (pop extra))
  14189. (unless (member (file-truename f) tnames)
  14190. (add-to-list 'files f 'append)
  14191. (add-to-list 'tnames (file-truename f) 'append)))
  14192. (multi-occur
  14193. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14194. regexp)))
  14195. (if (boundp 'occur-mode-find-occurrence-hook)
  14196. ;; Emacs 23
  14197. (add-hook 'occur-mode-find-occurrence-hook
  14198. (lambda ()
  14199. (when (org-mode-p)
  14200. (org-reveal))))
  14201. ;; Emacs 22
  14202. (defadvice occur-mode-goto-occurrence
  14203. (after org-occur-reveal activate)
  14204. (and (org-mode-p) (org-reveal)))
  14205. (defadvice occur-mode-goto-occurrence-other-window
  14206. (after org-occur-reveal activate)
  14207. (and (org-mode-p) (org-reveal)))
  14208. (defadvice occur-mode-display-occurrence
  14209. (after org-occur-reveal activate)
  14210. (when (org-mode-p)
  14211. (let ((pos (occur-mode-find-occurrence)))
  14212. (with-current-buffer (marker-buffer pos)
  14213. (save-excursion
  14214. (goto-char pos)
  14215. (org-reveal)))))))
  14216. (defun org-uniquify (list)
  14217. "Remove duplicate elements from LIST."
  14218. (let (res)
  14219. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14220. res))
  14221. (defun org-delete-all (elts list)
  14222. "Remove all elements in ELTS from LIST."
  14223. (while elts
  14224. (setq list (delete (pop elts) list)))
  14225. list)
  14226. (defun org-back-over-empty-lines ()
  14227. "Move backwards over whitespace, to the beginning of the first empty line.
  14228. Returns the number of empty lines passed."
  14229. (let ((pos (point)))
  14230. (skip-chars-backward " \t\n\r")
  14231. (beginning-of-line 2)
  14232. (goto-char (min (point) pos))
  14233. (count-lines (point) pos)))
  14234. (defun org-skip-whitespace ()
  14235. (skip-chars-forward " \t\n\r"))
  14236. (defun org-point-in-group (point group &optional context)
  14237. "Check if POINT is in match-group GROUP.
  14238. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14239. match. If the match group does ot exist or point is not inside it,
  14240. return nil."
  14241. (and (match-beginning group)
  14242. (>= point (match-beginning group))
  14243. (<= point (match-end group))
  14244. (if context
  14245. (list context (match-beginning group) (match-end group))
  14246. t)))
  14247. (defun org-switch-to-buffer-other-window (&rest args)
  14248. "Switch to buffer in a second window on the current frame.
  14249. In particular, do not allow pop-up frames."
  14250. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14251. special-display-function)
  14252. (apply 'switch-to-buffer-other-window args)))
  14253. (defun org-combine-plists (&rest plists)
  14254. "Create a single property list from all plists in PLISTS.
  14255. The process starts by copying the first list, and then setting properties
  14256. from the other lists. Settings in the last list are the most significant
  14257. ones and overrule settings in the other lists."
  14258. (let ((rtn (copy-sequence (pop plists)))
  14259. p v ls)
  14260. (while plists
  14261. (setq ls (pop plists))
  14262. (while ls
  14263. (setq p (pop ls) v (pop ls))
  14264. (setq rtn (plist-put rtn p v))))
  14265. rtn))
  14266. (defun org-move-line-down (arg)
  14267. "Move the current line down. 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 (+ 1 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-move-line-up (arg)
  14279. "Move the current line up. With prefix argument, move it past ARG lines."
  14280. (interactive "p")
  14281. (let ((col (current-column))
  14282. beg end pos)
  14283. (beginning-of-line 1) (setq beg (point))
  14284. (beginning-of-line 2) (setq end (point))
  14285. (beginning-of-line (- arg))
  14286. (setq pos (move-marker (make-marker) (point)))
  14287. (insert (delete-and-extract-region beg end))
  14288. (goto-char pos)
  14289. (org-move-to-column col)))
  14290. (defun org-replace-escapes (string table)
  14291. "Replace %-escapes in STRING with values in TABLE.
  14292. TABLE is an association list with keys like \"%a\" and string values.
  14293. The sequences in STRING may contain normal field width and padding information,
  14294. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14295. so values can contain further %-escapes if they are define later in TABLE."
  14296. (let ((case-fold-search nil)
  14297. e re rpl)
  14298. (while (setq e (pop table))
  14299. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14300. (while (string-match re string)
  14301. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14302. (cdr e)))
  14303. (setq string (replace-match rpl t t string))))
  14304. string))
  14305. (defun org-sublist (list start end)
  14306. "Return a section of LIST, from START to END.
  14307. Counting starts at 1."
  14308. (let (rtn (c start))
  14309. (setq list (nthcdr (1- start) list))
  14310. (while (and list (<= c end))
  14311. (push (pop list) rtn)
  14312. (setq c (1+ c)))
  14313. (nreverse rtn)))
  14314. (defun org-find-base-buffer-visiting (file)
  14315. "Like `find-buffer-visiting' but always return the base buffer and
  14316. not an indirect buffer."
  14317. (let ((buf (or (get-file-buffer file)
  14318. (find-buffer-visiting file))))
  14319. (if buf
  14320. (or (buffer-base-buffer buf) buf)
  14321. nil)))
  14322. (defun org-image-file-name-regexp (&optional extensions)
  14323. "Return regexp matching the file names of images.
  14324. If EXTENSIONS is given, only match these."
  14325. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14326. (image-file-name-regexp)
  14327. (let ((image-file-name-extensions
  14328. (or extensions
  14329. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14330. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14331. (concat "\\."
  14332. (regexp-opt (nconc (mapcar 'upcase
  14333. image-file-name-extensions)
  14334. image-file-name-extensions)
  14335. t)
  14336. "\\'"))))
  14337. (defun org-file-image-p (file &optional extensions)
  14338. "Return non-nil if FILE is an image."
  14339. (save-match-data
  14340. (string-match (org-image-file-name-regexp extensions) file)))
  14341. (defun org-get-cursor-date ()
  14342. "Return the date at cursor in as a time.
  14343. This works in the calendar and in the agenda, anywhere else it just
  14344. returns the current time."
  14345. (let (date day defd)
  14346. (cond
  14347. ((eq major-mode 'calendar-mode)
  14348. (setq date (calendar-cursor-to-date)
  14349. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14350. ((eq major-mode 'org-agenda-mode)
  14351. (setq day (get-text-property (point) 'day))
  14352. (if day
  14353. (setq date (calendar-gregorian-from-absolute day)
  14354. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14355. (nth 2 date))))))
  14356. (or defd (current-time))))
  14357. (defvar org-agenda-action-marker (make-marker)
  14358. "Marker pointing to the entry for the next agenda action.")
  14359. (defun org-mark-entry-for-agenda-action ()
  14360. "Mark the current entry as target of an agenda action.
  14361. Agenda actions are actions executed from the agenda with the key `k',
  14362. which make use of the date at the cursor."
  14363. (interactive)
  14364. (move-marker org-agenda-action-marker
  14365. (save-excursion (org-back-to-heading t) (point))
  14366. (current-buffer))
  14367. (message
  14368. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14369. ;;; Paragraph filling stuff.
  14370. ;; We want this to be just right, so use the full arsenal.
  14371. (defun org-indent-line-function ()
  14372. "Indent line like previous, but further if previous was headline or item."
  14373. (interactive)
  14374. (let* ((pos (point))
  14375. (itemp (org-at-item-p))
  14376. (case-fold-search t)
  14377. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14378. column bpos bcol tpos tcol bullet btype bullet-type)
  14379. ;; Find the previous relevant line
  14380. (beginning-of-line 1)
  14381. (cond
  14382. ((looking-at "#") (setq column 0))
  14383. ((looking-at "\\*+ ") (setq column 0))
  14384. ((and (looking-at "[ \t]*:END:")
  14385. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14386. (save-excursion
  14387. (goto-char (1- (match-beginning 1)))
  14388. (setq column (current-column))))
  14389. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14390. (save-excursion
  14391. (re-search-backward
  14392. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14393. (setq column (org-get-indentation (match-string 0))))
  14394. (t
  14395. (beginning-of-line 0)
  14396. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14397. (not (looking-at "[ \t]*:END:"))
  14398. (not (looking-at org-drawer-regexp)))
  14399. (beginning-of-line 0))
  14400. (cond
  14401. ((looking-at "\\*+[ \t]+")
  14402. (if (not org-adapt-indentation)
  14403. (setq column 0)
  14404. (goto-char (match-end 0))
  14405. (setq column (current-column))))
  14406. ((looking-at org-drawer-regexp)
  14407. (goto-char (1- (match-beginning 1)))
  14408. (setq column (current-column)))
  14409. ((looking-at "\\([ \t]*\\):END:")
  14410. (goto-char (match-end 1))
  14411. (setq column (current-column)))
  14412. ((org-in-item-p)
  14413. (org-beginning-of-item)
  14414. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14415. (setq bpos (match-beginning 1) tpos (match-end 0)
  14416. bcol (progn (goto-char bpos) (current-column))
  14417. tcol (progn (goto-char tpos) (current-column))
  14418. bullet (match-string 1)
  14419. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14420. (if (> tcol (+ bcol org-description-max-indent))
  14421. (setq tcol (+ bcol 5)))
  14422. (if (not itemp)
  14423. (setq column tcol)
  14424. (goto-char pos)
  14425. (beginning-of-line 1)
  14426. (if (looking-at "\\S-")
  14427. (progn
  14428. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14429. (setq bullet (match-string 1)
  14430. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14431. (setq column (if (equal btype bullet-type) bcol tcol)))
  14432. (setq column (org-get-indentation)))))
  14433. (t (setq column (org-get-indentation))))))
  14434. (goto-char pos)
  14435. (if (<= (current-column) (current-indentation))
  14436. (org-indent-line-to column)
  14437. (save-excursion (org-indent-line-to column)))
  14438. (setq column (current-column))
  14439. (beginning-of-line 1)
  14440. (if (looking-at
  14441. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14442. (replace-match (concat "\\1" (format org-property-format
  14443. (match-string 2) (match-string 3)))
  14444. t nil))
  14445. (org-move-to-column column)))
  14446. (defun org-set-autofill-regexps ()
  14447. (interactive)
  14448. ;; In the paragraph separator we include headlines, because filling
  14449. ;; text in a line directly attached to a headline would otherwise
  14450. ;; fill the headline as well.
  14451. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14452. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14453. ;; The paragraph starter includes hand-formatted lists.
  14454. (org-set-local 'paragraph-start
  14455. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14456. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14457. ;; But only if the user has not turned off tables or fixed-width regions
  14458. (org-set-local
  14459. 'auto-fill-inhibit-regexp
  14460. (concat "\\*+ \\|#\\+"
  14461. "\\|[ \t]*" org-keyword-time-regexp
  14462. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14463. (concat
  14464. "\\|[ \t]*["
  14465. (if org-enable-table-editor "|" "")
  14466. (if org-enable-fixed-width-editor ":" "")
  14467. "]"))))
  14468. ;; We use our own fill-paragraph function, to make sure that tables
  14469. ;; and fixed-width regions are not wrapped. That function will pass
  14470. ;; through to `fill-paragraph' when appropriate.
  14471. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14472. ; Adaptive filling: To get full control, first make sure that
  14473. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14474. (org-set-local 'adaptive-fill-regexp "\000")
  14475. (org-set-local 'adaptive-fill-function
  14476. 'org-adaptive-fill-function)
  14477. (org-set-local
  14478. 'align-mode-rules-list
  14479. '((org-in-buffer-settings
  14480. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14481. (modes . '(org-mode))))))
  14482. (defun org-fill-paragraph (&optional justify)
  14483. "Re-align a table, pass through to fill-paragraph if no table."
  14484. (let ((table-p (org-at-table-p))
  14485. (table.el-p (org-at-table.el-p)))
  14486. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14487. (save-excursion (goto-char (point-at-bol))
  14488. (looking-at outline-regexp)))
  14489. t) ; skip headlines
  14490. (table.el-p t) ; skip table.el tables
  14491. (table-p (org-table-align) t) ; align org-mode tables
  14492. (t nil)))) ; call paragraph-fill
  14493. ;; For reference, this is the default value of adaptive-fill-regexp
  14494. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14495. (defun org-adaptive-fill-function ()
  14496. "Return a fill prefix for org-mode files.
  14497. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14498. work correctly."
  14499. (cond ((looking-at "#[ \t]+")
  14500. (match-string 0))
  14501. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14502. (save-excursion
  14503. (if (> (match-end 1) (+ (match-beginning 1)
  14504. org-description-max-indent))
  14505. (goto-char (+ (match-beginning 1) 5))
  14506. (goto-char (match-end 0)))
  14507. (make-string (current-column) ?\ )))
  14508. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14509. (save-excursion
  14510. (goto-char (match-end 0))
  14511. (make-string (current-column) ?\ )))
  14512. (t nil)))
  14513. ;;; Other stuff.
  14514. (defun org-toggle-fixed-width-section (arg)
  14515. "Toggle the fixed-width export.
  14516. If there is no active region, the QUOTE keyword at the current headline is
  14517. inserted or removed. When present, it causes the text between this headline
  14518. and the next to be exported as fixed-width text, and unmodified.
  14519. If there is an active region, this command adds or removes a colon as the
  14520. first character of this line. If the first character of a line is a colon,
  14521. this line is also exported in fixed-width font."
  14522. (interactive "P")
  14523. (let* ((cc 0)
  14524. (regionp (org-region-active-p))
  14525. (beg (if regionp (region-beginning) (point)))
  14526. (end (if regionp (region-end)))
  14527. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14528. (case-fold-search nil)
  14529. (re "[ \t]*\\(: \\)")
  14530. off)
  14531. (if regionp
  14532. (save-excursion
  14533. (goto-char beg)
  14534. (setq cc (current-column))
  14535. (beginning-of-line 1)
  14536. (setq off (looking-at re))
  14537. (while (> nlines 0)
  14538. (setq nlines (1- nlines))
  14539. (beginning-of-line 1)
  14540. (cond
  14541. (arg
  14542. (org-move-to-column cc t)
  14543. (insert ": \n")
  14544. (forward-line -1))
  14545. ((and off (looking-at re))
  14546. (replace-match "" t t nil 1))
  14547. ((not off) (org-move-to-column cc t) (insert ": ")))
  14548. (forward-line 1)))
  14549. (save-excursion
  14550. (org-back-to-heading)
  14551. (if (looking-at (concat outline-regexp
  14552. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14553. (replace-match "" t t nil 1)
  14554. (if (looking-at outline-regexp)
  14555. (progn
  14556. (goto-char (match-end 0))
  14557. (insert org-quote-string " "))))))))
  14558. (defun org-reftex-citation ()
  14559. "Use reftex-citation to insert a citation into the buffer.
  14560. This looks for a line like
  14561. #+BIBLIOGRAPHY: foo plain option:-d
  14562. and derives from it that foo.bib is the bbliography file relevant
  14563. for this document. It then installs the necessary environment for RefTeX
  14564. to work in this buffer and calls `reftex-citation' to insert a citation
  14565. into the buffer.
  14566. Export of such citations to both LaTeX and HTML is handled by the contributed
  14567. package org-exp-bibtex by Taru Karttunen."
  14568. (interactive)
  14569. (let ((reftex-docstruct-symbol 'rds)
  14570. (reftex-cite-format "\\cite{%l}")
  14571. rds bib)
  14572. (save-excursion
  14573. (save-restriction
  14574. (widen)
  14575. (let ((case-fold-search t)
  14576. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14577. (if (not (save-excursion
  14578. (or (re-search-forward re nil t)
  14579. (re-search-backward re nil t))))
  14580. (error "No bibliography defined in file")
  14581. (setq bib (concat (match-string 1) ".bib")
  14582. rds (list (list 'bib bib)))))))
  14583. (call-interactively 'reftex-citation)))
  14584. ;;;; Functions extending outline functionality
  14585. (defun org-beginning-of-line (&optional arg)
  14586. "Go to the beginning of the current line. If that is invisible, continue
  14587. to a visible line beginning. This makes the function of C-a more intuitive.
  14588. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14589. first attempt, and only move to after the tags when the cursor is already
  14590. beyond the end of the headline."
  14591. (interactive "P")
  14592. (let ((pos (point))
  14593. (special (if (consp org-special-ctrl-a/e)
  14594. (car org-special-ctrl-a/e)
  14595. org-special-ctrl-a/e))
  14596. refpos)
  14597. (beginning-of-line 1)
  14598. (if (and arg (fboundp 'move-beginning-of-line))
  14599. (call-interactively 'move-beginning-of-line)
  14600. (if (bobp)
  14601. nil
  14602. (backward-char 1)
  14603. (if (org-invisible-p)
  14604. (while (and (not (bobp)) (org-invisible-p))
  14605. (backward-char 1)
  14606. (beginning-of-line 1))
  14607. (forward-char 1))))
  14608. (when special
  14609. (cond
  14610. ((and (looking-at org-complex-heading-regexp)
  14611. (= (char-after (match-end 1)) ?\ ))
  14612. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14613. (point-at-eol)))
  14614. (goto-char
  14615. (if (eq special t)
  14616. (cond ((> pos refpos) refpos)
  14617. ((= pos (point)) refpos)
  14618. (t (point)))
  14619. (cond ((> pos (point)) (point))
  14620. ((not (eq last-command this-command)) (point))
  14621. (t refpos)))))
  14622. ((org-at-item-p)
  14623. (goto-char
  14624. (if (eq special t)
  14625. (cond ((> pos (match-end 4)) (match-end 4))
  14626. ((= pos (point)) (match-end 4))
  14627. (t (point)))
  14628. (cond ((> pos (point)) (point))
  14629. ((not (eq last-command this-command)) (point))
  14630. (t (match-end 4))))))))
  14631. (org-no-warnings
  14632. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14633. (defun org-end-of-line (&optional arg)
  14634. "Go to the end of the line.
  14635. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14636. first attempt, and only move to after the tags when the cursor is already
  14637. beyond the end of the headline."
  14638. (interactive "P")
  14639. (let ((special (if (consp org-special-ctrl-a/e)
  14640. (cdr org-special-ctrl-a/e)
  14641. org-special-ctrl-a/e)))
  14642. (if (or (not special)
  14643. (not (org-on-heading-p))
  14644. arg)
  14645. (call-interactively (if (fboundp 'move-end-of-line)
  14646. 'move-end-of-line
  14647. 'end-of-line))
  14648. (let ((pos (point)))
  14649. (beginning-of-line 1)
  14650. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14651. (if (eq special t)
  14652. (if (or (< pos (match-beginning 1))
  14653. (= pos (match-end 0)))
  14654. (goto-char (match-beginning 1))
  14655. (goto-char (match-end 0)))
  14656. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14657. (goto-char (match-end 0))
  14658. (goto-char (match-beginning 1))))
  14659. (call-interactively (if (fboundp 'move-end-of-line)
  14660. 'move-end-of-line
  14661. 'end-of-line)))))
  14662. (org-no-warnings
  14663. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14664. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14665. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14666. (defun org-backward-sentence (&optional arg)
  14667. "Go to beginning of sentence, or beginning of table field.
  14668. This will call `backward-sentence' or `org-table-beginning-of-field',
  14669. depending on context."
  14670. (interactive "P")
  14671. (cond
  14672. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14673. (t (call-interactively 'backward-sentence))))
  14674. (defun org-forward-sentence (&optional arg)
  14675. "Go to end of sentence, or end of table field.
  14676. This will call `forward-sentence' or `org-table-end-of-field',
  14677. depending on context."
  14678. (interactive "P")
  14679. (cond
  14680. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14681. (t (call-interactively 'forward-sentence))))
  14682. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14683. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14684. (defun org-kill-line (&optional arg)
  14685. "Kill line, to tags or end of line."
  14686. (interactive "P")
  14687. (cond
  14688. ((or (not org-special-ctrl-k)
  14689. (bolp)
  14690. (not (org-on-heading-p)))
  14691. (call-interactively 'kill-line))
  14692. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14693. (kill-region (point) (match-beginning 1))
  14694. (org-set-tags nil t))
  14695. (t (kill-region (point) (point-at-eol)))))
  14696. (define-key org-mode-map "\C-k" 'org-kill-line)
  14697. (defun org-yank (&optional arg)
  14698. "Yank. If the kill is a subtree, treat it specially.
  14699. This command will look at the current kill and check if is a single
  14700. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14701. cursor is at the beginning of a line or after the stars of a currently
  14702. empty headline, then the yank is handled specially. How exactly depends
  14703. on the value of the following variables, both set by default.
  14704. org-yank-folded-subtrees
  14705. When set, the subtree(s) will be folded after insertion, but only
  14706. if doing so would now swallow text after the yanked text.
  14707. org-yank-adjusted-subtrees
  14708. When set, the subtree will be promoted or demoted in order to
  14709. fit into the local outline tree structure, which means that the level
  14710. will be adjusted so that it becomes the smaller one of the two
  14711. *visible* surrounding headings.
  14712. Any prefix to this command will cause `yank' to be called directly with
  14713. no special treatment. In particular, a simple `C-u' prefix will just
  14714. plainly yank the text as it is.
  14715. \[1] The test checks if the first non-white line is a heading
  14716. and if there are no other headings with fewer stars."
  14717. (interactive "P")
  14718. (org-yank-generic 'yank arg))
  14719. (defun org-yank-generic (command arg)
  14720. "Perform some yank-like command.
  14721. This function implements the behavior described in the `org-yank'
  14722. documentation. However, it has been generalized to work for any
  14723. interactive command with similar behavior."
  14724. ;; pretend to be command COMMAND
  14725. (setq this-command command)
  14726. (if arg
  14727. (call-interactively command)
  14728. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14729. (and (org-kill-is-subtree-p)
  14730. (or (bolp)
  14731. (and (looking-at "[ \t]*$")
  14732. (string-match
  14733. "\\`\\*+\\'"
  14734. (buffer-substring (point-at-bol) (point)))))))
  14735. swallowp)
  14736. (cond
  14737. ((and subtreep org-yank-folded-subtrees)
  14738. (let ((beg (point))
  14739. end)
  14740. (if (and subtreep org-yank-adjusted-subtrees)
  14741. (org-paste-subtree nil nil 'for-yank)
  14742. (call-interactively command))
  14743. (setq end (point))
  14744. (goto-char beg)
  14745. (when (and (bolp) subtreep
  14746. (not (setq swallowp
  14747. (org-yank-folding-would-swallow-text beg end))))
  14748. (or (looking-at outline-regexp)
  14749. (re-search-forward (concat "^" outline-regexp) end t))
  14750. (while (and (< (point) end) (looking-at outline-regexp))
  14751. (hide-subtree)
  14752. (org-cycle-show-empty-lines 'folded)
  14753. (condition-case nil
  14754. (outline-forward-same-level 1)
  14755. (error (goto-char end)))))
  14756. (when swallowp
  14757. (message
  14758. "Inserted text not folded because that would swallow text"))
  14759. (goto-char end)
  14760. (skip-chars-forward " \t\n\r")
  14761. (beginning-of-line 1)
  14762. (push-mark beg 'nomsg)))
  14763. ((and subtreep org-yank-adjusted-subtrees)
  14764. (let ((beg (point-at-bol)))
  14765. (org-paste-subtree nil nil 'for-yank)
  14766. (push-mark beg 'nomsg)))
  14767. (t
  14768. (call-interactively command))))))
  14769. (defun org-yank-folding-would-swallow-text (beg end)
  14770. "Would hide-subtree at BEG swallow any text after END?"
  14771. (let (level)
  14772. (save-excursion
  14773. (goto-char beg)
  14774. (when (or (looking-at outline-regexp)
  14775. (re-search-forward (concat "^" outline-regexp) end t))
  14776. (setq level (org-outline-level)))
  14777. (goto-char end)
  14778. (skip-chars-forward " \t\r\n\v\f")
  14779. (if (or (eobp)
  14780. (and (bolp) (looking-at org-outline-regexp)
  14781. (<= (org-outline-level) level)))
  14782. nil ; Nothing would be swallowed
  14783. t)))) ; something would swallow
  14784. (define-key org-mode-map "\C-y" 'org-yank)
  14785. (defun org-invisible-p ()
  14786. "Check if point is at a character currently not visible."
  14787. ;; Early versions of noutline don't have `outline-invisible-p'.
  14788. (if (fboundp 'outline-invisible-p)
  14789. (outline-invisible-p)
  14790. (get-char-property (point) 'invisible)))
  14791. (defun org-invisible-p2 ()
  14792. "Check if point is at a character currently not visible."
  14793. (save-excursion
  14794. (if (and (eolp) (not (bobp))) (backward-char 1))
  14795. ;; Early versions of noutline don't have `outline-invisible-p'.
  14796. (if (fboundp 'outline-invisible-p)
  14797. (outline-invisible-p)
  14798. (get-char-property (point) 'invisible))))
  14799. (defun org-back-to-heading (&optional invisible-ok)
  14800. "Call `outline-back-to-heading', but provide a better error message."
  14801. (condition-case nil
  14802. (outline-back-to-heading invisible-ok)
  14803. (error (error "Before first headline at position %d in buffer %s"
  14804. (point) (current-buffer)))))
  14805. (defun org-before-first-heading-p ()
  14806. "Before first heading?"
  14807. (save-excursion
  14808. (null (re-search-backward "^\\*+ " nil t))))
  14809. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14810. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14811. (defun org-at-heading-or-item-p ()
  14812. (or (org-on-heading-p) (org-at-item-p)))
  14813. (defun org-on-target-p ()
  14814. (or (org-in-regexp org-radio-target-regexp)
  14815. (org-in-regexp org-target-regexp)))
  14816. (defun org-up-heading-all (arg)
  14817. "Move to the heading line of which the present line is a subheading.
  14818. This function considers both visible and invisible heading lines.
  14819. With argument, move up ARG levels."
  14820. (if (fboundp 'outline-up-heading-all)
  14821. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14822. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14823. (defun org-up-heading-safe ()
  14824. "Move to the heading line of which the present line is a subheading.
  14825. This version will not throw an error. It will return the level of the
  14826. headline found, or nil if no higher level is found.
  14827. Also, this function will be a lot faster than `outline-up-heading',
  14828. because it relies on stars being the outline starters. This can really
  14829. make a significant difference in outlines with very many siblings."
  14830. (let (start-level re)
  14831. (org-back-to-heading t)
  14832. (setq start-level (funcall outline-level))
  14833. (if (equal start-level 1)
  14834. nil
  14835. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14836. (if (re-search-backward re nil t)
  14837. (funcall outline-level)))))
  14838. (defun org-first-sibling-p ()
  14839. "Is this heading the first child of its parents?"
  14840. (interactive)
  14841. (let ((re (concat "^" outline-regexp))
  14842. level l)
  14843. (unless (org-at-heading-p t)
  14844. (error "Not at a heading"))
  14845. (setq level (funcall outline-level))
  14846. (save-excursion
  14847. (if (not (re-search-backward re nil t))
  14848. t
  14849. (setq l (funcall outline-level))
  14850. (< l level)))))
  14851. (defun org-goto-sibling (&optional previous)
  14852. "Goto the next sibling, even if it is invisible.
  14853. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14854. when a sibling was found. When none is found, return nil and don't
  14855. move point."
  14856. (let ((fun (if previous 're-search-backward 're-search-forward))
  14857. (pos (point))
  14858. (re (concat "^" outline-regexp))
  14859. level l)
  14860. (when (condition-case nil (org-back-to-heading t) (error nil))
  14861. (setq level (funcall outline-level))
  14862. (catch 'exit
  14863. (or previous (forward-char 1))
  14864. (while (funcall fun re nil t)
  14865. (setq l (funcall outline-level))
  14866. (when (< l level) (goto-char pos) (throw 'exit nil))
  14867. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14868. (goto-char pos)
  14869. nil))))
  14870. (defun org-show-siblings ()
  14871. "Show all siblings of the current headline."
  14872. (save-excursion
  14873. (while (org-goto-sibling) (org-flag-heading nil)))
  14874. (save-excursion
  14875. (while (org-goto-sibling 'previous)
  14876. (org-flag-heading nil))))
  14877. (defun org-show-hidden-entry ()
  14878. "Show an entry where even the heading is hidden."
  14879. (save-excursion
  14880. (org-show-entry)))
  14881. (defun org-flag-heading (flag &optional entry)
  14882. "Flag the current heading. FLAG non-nil means make invisible.
  14883. When ENTRY is non-nil, show the entire entry."
  14884. (save-excursion
  14885. (org-back-to-heading t)
  14886. ;; Check if we should show the entire entry
  14887. (if entry
  14888. (progn
  14889. (org-show-entry)
  14890. (save-excursion
  14891. (and (outline-next-heading)
  14892. (org-flag-heading nil))))
  14893. (outline-flag-region (max (point-min) (1- (point)))
  14894. (save-excursion (outline-end-of-heading) (point))
  14895. flag))))
  14896. (defun org-forward-same-level (arg)
  14897. "Move forward to the ARG'th subheading at same level as this one.
  14898. Stop at the first and last subheadings of a superior heading.
  14899. This is like outline-forward-same-level, but invisible headings are ok."
  14900. (interactive "p")
  14901. (org-back-to-heading t)
  14902. (while (> arg 0)
  14903. (let ((point-to-move-to (save-excursion
  14904. (org-get-next-sibling))))
  14905. (if point-to-move-to
  14906. (progn
  14907. (goto-char point-to-move-to)
  14908. (setq arg (1- arg)))
  14909. (progn
  14910. (setq arg 0)
  14911. (error "No following same-level heading"))))))
  14912. (defun org-get-next-sibling ()
  14913. "Move to next heading of the same level, and return point.
  14914. If there is no such heading, return nil.
  14915. This is like outline-next-sibling, but invisible headings are ok."
  14916. (let ((level (funcall outline-level)))
  14917. (outline-next-heading)
  14918. (while (and (not (eobp)) (> (funcall outline-level) level))
  14919. (outline-next-heading))
  14920. (if (or (eobp) (< (funcall outline-level) level))
  14921. nil
  14922. (point))))
  14923. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14924. ;; This contains an exact copy of the original function, but it uses
  14925. ;; `org-back-to-heading', to make it work also in invisible
  14926. ;; trees. And is uses an invisible-OK argument.
  14927. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14928. ;; Furthermore, when used inside Org, finding the end of a large subtree
  14929. ;; with many children and grandchildren etc, this can be much faster
  14930. ;; than the outline version.
  14931. (org-back-to-heading invisible-OK)
  14932. (let ((first t)
  14933. (level (funcall outline-level)))
  14934. (if (and (org-mode-p) (< level 1000))
  14935. ;; A true heading (not a plain list item), in Org-mode
  14936. ;; This means we can easily find the end by looking
  14937. ;; only for the right number of stars. Using a regexp to do
  14938. ;; this is so much faster than using a Lisp loop.
  14939. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  14940. (forward-char 1)
  14941. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  14942. ;; something else, do it the slow way
  14943. (while (and (not (eobp))
  14944. (or first (> (funcall outline-level) level)))
  14945. (setq first nil)
  14946. (outline-next-heading)))
  14947. (unless to-heading
  14948. (if (memq (preceding-char) '(?\n ?\^M))
  14949. (progn
  14950. ;; Go to end of line before heading
  14951. (forward-char -1)
  14952. (if (memq (preceding-char) '(?\n ?\^M))
  14953. ;; leave blank line before heading
  14954. (forward-char -1))))))
  14955. (point))
  14956. (defun org-show-subtree ()
  14957. "Show everything after this heading at deeper levels."
  14958. (outline-flag-region
  14959. (point)
  14960. (save-excursion
  14961. (outline-end-of-subtree) (outline-next-heading) (point))
  14962. nil))
  14963. (defun org-show-entry ()
  14964. "Show the body directly following this heading.
  14965. Show the heading too, if it is currently invisible."
  14966. (interactive)
  14967. (save-excursion
  14968. (condition-case nil
  14969. (progn
  14970. (org-back-to-heading t)
  14971. (outline-flag-region
  14972. (max (point-min) (1- (point)))
  14973. (save-excursion
  14974. (if (re-search-forward
  14975. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14976. (match-beginning 1)
  14977. (point-max)))
  14978. nil))
  14979. (error nil))))
  14980. (defun org-make-options-regexp (kwds &optional extra)
  14981. "Make a regular expression for keyword lines."
  14982. (concat
  14983. "^"
  14984. "#?[ \t]*\\+\\("
  14985. (mapconcat 'regexp-quote kwds "\\|")
  14986. (if extra (concat "\\|" extra))
  14987. "\\):[ \t]*"
  14988. "\\(.+\\)"))
  14989. ;; Make isearch reveal the necessary context
  14990. (defun org-isearch-end ()
  14991. "Reveal context after isearch exits."
  14992. (when isearch-success ; only if search was successful
  14993. (if (featurep 'xemacs)
  14994. ;; Under XEmacs, the hook is run in the correct place,
  14995. ;; we directly show the context.
  14996. (org-show-context 'isearch)
  14997. ;; In Emacs the hook runs *before* restoring the overlays.
  14998. ;; So we have to use a one-time post-command-hook to do this.
  14999. ;; (Emacs 22 has a special variable, see function `org-mode')
  15000. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15001. isearch-mode-end-hook-quit)
  15002. ;; Only when the isearch was not quitted.
  15003. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15004. 'append 'local)))))
  15005. (defun org-isearch-post-command ()
  15006. "Remove self from hook, and show context."
  15007. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15008. (org-show-context 'isearch))
  15009. ;;;; Integration with and fixes for other packages
  15010. ;;; Imenu support
  15011. (defvar org-imenu-markers nil
  15012. "All markers currently used by Imenu.")
  15013. (make-variable-buffer-local 'org-imenu-markers)
  15014. (defun org-imenu-new-marker (&optional pos)
  15015. "Return a new marker for use by Imenu, and remember the marker."
  15016. (let ((m (make-marker)))
  15017. (move-marker m (or pos (point)))
  15018. (push m org-imenu-markers)
  15019. m))
  15020. (defun org-imenu-get-tree ()
  15021. "Produce the index for Imenu."
  15022. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15023. (setq org-imenu-markers nil)
  15024. (let* ((n org-imenu-depth)
  15025. (re (concat "^" outline-regexp))
  15026. (subs (make-vector (1+ n) nil))
  15027. (last-level 0)
  15028. m level head)
  15029. (save-excursion
  15030. (save-restriction
  15031. (widen)
  15032. (goto-char (point-max))
  15033. (while (re-search-backward re nil t)
  15034. (setq level (org-reduced-level (funcall outline-level)))
  15035. (when (<= level n)
  15036. (looking-at org-complex-heading-regexp)
  15037. (setq head (org-link-display-format
  15038. (org-match-string-no-properties 4))
  15039. m (org-imenu-new-marker))
  15040. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15041. (if (>= level last-level)
  15042. (push (cons head m) (aref subs level))
  15043. (push (cons head (aref subs (1+ level))) (aref subs level))
  15044. (loop for i from (1+ level) to n do (aset subs i nil)))
  15045. (setq last-level level)))))
  15046. (aref subs 1)))
  15047. (eval-after-load "imenu"
  15048. '(progn
  15049. (add-hook 'imenu-after-jump-hook
  15050. (lambda ()
  15051. (if (eq major-mode 'org-mode)
  15052. (org-show-context 'org-goto))))))
  15053. (defun org-link-display-format (link)
  15054. "Replace a link with either the description, or the link target
  15055. if no description is present"
  15056. (save-match-data
  15057. (if (string-match org-bracket-link-analytic-regexp link)
  15058. (replace-match (or (match-string 5 link)
  15059. (concat (match-string 1 link)
  15060. (match-string 3 link)))
  15061. nil nil link)
  15062. link)))
  15063. ;; Speedbar support
  15064. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15065. "Overlay marking the agenda restriction line in speedbar.")
  15066. (org-overlay-put org-speedbar-restriction-lock-overlay
  15067. 'face 'org-agenda-restriction-lock)
  15068. (org-overlay-put org-speedbar-restriction-lock-overlay
  15069. 'help-echo "Agendas are currently limited to this item.")
  15070. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15071. (defun org-speedbar-set-agenda-restriction ()
  15072. "Restrict future agenda commands to the location at point in speedbar.
  15073. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15074. (interactive)
  15075. (require 'org-agenda)
  15076. (let (p m tp np dir txt)
  15077. (cond
  15078. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15079. 'org-imenu t))
  15080. (setq m (get-text-property p 'org-imenu-marker))
  15081. (save-excursion
  15082. (save-restriction
  15083. (set-buffer (marker-buffer m))
  15084. (goto-char m)
  15085. (org-agenda-set-restriction-lock 'subtree))))
  15086. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15087. 'speedbar-function 'speedbar-find-file))
  15088. (setq tp (previous-single-property-change
  15089. (1+ p) 'speedbar-function)
  15090. np (next-single-property-change
  15091. tp 'speedbar-function)
  15092. dir (speedbar-line-directory)
  15093. txt (buffer-substring-no-properties (or tp (point-min))
  15094. (or np (point-max))))
  15095. (save-excursion
  15096. (save-restriction
  15097. (set-buffer (find-file-noselect
  15098. (let ((default-directory dir))
  15099. (expand-file-name txt))))
  15100. (unless (org-mode-p)
  15101. (error "Cannot restrict to non-Org-mode file"))
  15102. (org-agenda-set-restriction-lock 'file))))
  15103. (t (error "Don't know how to restrict Org-mode's agenda")))
  15104. (org-move-overlay org-speedbar-restriction-lock-overlay
  15105. (point-at-bol) (point-at-eol))
  15106. (setq current-prefix-arg nil)
  15107. (org-agenda-maybe-redo)))
  15108. (eval-after-load "speedbar"
  15109. '(progn
  15110. (speedbar-add-supported-extension ".org")
  15111. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15112. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15113. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15114. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15115. (add-hook 'speedbar-visiting-tag-hook
  15116. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15117. ;;; Fixes and Hacks for problems with other packages
  15118. ;; Make flyspell not check words in links, to not mess up our keymap
  15119. (defun org-mode-flyspell-verify ()
  15120. "Don't let flyspell put overlays at active buttons."
  15121. (and (not (get-text-property (point) 'keymap))
  15122. (not (get-text-property (point) 'org-no-flyspell))))
  15123. (defun org-remove-flyspell-overlays-in (beg end)
  15124. "Remove flyspell overlays in region."
  15125. (and (org-bound-and-true-p flyspell-mode)
  15126. (fboundp 'flyspell-delete-region-overlays)
  15127. (flyspell-delete-region-overlays beg end))
  15128. (add-text-properties beg end '(org-no-flyspell t)))
  15129. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15130. (eval-after-load "bookmark"
  15131. '(if (boundp 'bookmark-after-jump-hook)
  15132. ;; We can use the hook
  15133. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15134. ;; Hook not available, use advice
  15135. (defadvice bookmark-jump (after org-make-visible activate)
  15136. "Make the position visible."
  15137. (org-bookmark-jump-unhide))))
  15138. ;; Make sure saveplace show the location if it was hidden
  15139. (eval-after-load "saveplace"
  15140. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15141. "Make the position visible."
  15142. (org-bookmark-jump-unhide)))
  15143. (defun org-bookmark-jump-unhide ()
  15144. "Unhide the current position, to show the bookmark location."
  15145. (and (org-mode-p)
  15146. (or (org-invisible-p)
  15147. (save-excursion (goto-char (max (point-min) (1- (point))))
  15148. (org-invisible-p)))
  15149. (org-show-context 'bookmark-jump)))
  15150. ;; Make session.el ignore our circular variable
  15151. (eval-after-load "session"
  15152. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15153. ;;;; Experimental code
  15154. (defun org-closed-in-range ()
  15155. "Sparse tree of items closed in a certain time range.
  15156. Still experimental, may disappear in the future."
  15157. (interactive)
  15158. ;; Get the time interval from the user.
  15159. (let* ((time1 (time-to-seconds
  15160. (org-read-date nil 'to-time nil "Starting date: ")))
  15161. (time2 (time-to-seconds
  15162. (org-read-date nil 'to-time nil "End date:")))
  15163. ;; callback function
  15164. (callback (lambda ()
  15165. (let ((time
  15166. (time-to-seconds
  15167. (apply 'encode-time
  15168. (org-parse-time-string
  15169. (match-string 1))))))
  15170. ;; check if time in interval
  15171. (and (>= time time1) (<= time time2))))))
  15172. ;; make tree, check each match with the callback
  15173. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15174. ;;;; Finish up
  15175. (provide 'org)
  15176. (run-hooks 'org-load-hook)
  15177. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15178. ;;; org.el ends here