org.el 765 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424
  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, 2010, 2011
  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: 7.6
  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. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-pcomplete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  97. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  98. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  99. ;; babel
  100. (require 'ob)
  101. (require 'ob-table)
  102. (require 'ob-lob)
  103. (require 'ob-ref)
  104. (require 'ob-tangle)
  105. (require 'ob-comint)
  106. (require 'ob-keys)
  107. ;; load languages based on value of `org-babel-load-languages'
  108. (defvar org-babel-load-languages)
  109. ;;;###autoload
  110. (defun org-babel-do-load-languages (sym value)
  111. "Load the languages defined in `org-babel-load-languages'."
  112. (set-default sym value)
  113. (mapc (lambda (pair)
  114. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  115. (if active
  116. (progn
  117. (require (intern (concat "ob-" lang))))
  118. (progn
  119. (funcall 'fmakunbound
  120. (intern (concat "org-babel-execute:" lang)))
  121. (funcall 'fmakunbound
  122. (intern (concat "org-babel-expand-body:" lang)))))))
  123. org-babel-load-languages))
  124. (defcustom org-babel-load-languages '((emacs-lisp . t))
  125. "Languages which can be evaluated in Org-mode buffers.
  126. This list can be used to load support for any of the languages
  127. below, note that each language will depend on a different set of
  128. system executables and/or Emacs modes. When a language is
  129. \"loaded\", then code blocks in that language can be evaluated
  130. with `org-babel-execute-src-block' bound by default to C-c
  131. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  132. be set to remove code block evaluation from the C-c C-c
  133. keybinding. By default only Emacs Lisp (which has no
  134. requirements) is loaded."
  135. :group 'org-babel
  136. :set 'org-babel-do-load-languages
  137. :type '(alist :tag "Babel Languages"
  138. :key-type
  139. (choice
  140. (const :tag "Awk" awk)
  141. (const :tag "C" C)
  142. (const :tag "R" R)
  143. (const :tag "Asymptote" asymptote)
  144. (const :tag "Calc" calc)
  145. (const :tag "Clojure" clojure)
  146. (const :tag "CSS" css)
  147. (const :tag "Ditaa" ditaa)
  148. (const :tag "Dot" dot)
  149. (const :tag "Emacs Lisp" emacs-lisp)
  150. (const :tag "Gnuplot" gnuplot)
  151. (const :tag "Haskell" haskell)
  152. (const :tag "Java" java)
  153. (const :tag "Javascript" js)
  154. (const :tag "Latex" latex)
  155. (const :tag "Ledger" ledger)
  156. (const :tag "Lilypond" lilypond)
  157. (const :tag "Maxima" maxima)
  158. (const :tag "Matlab" matlab)
  159. (const :tag "Mscgen" mscgen)
  160. (const :tag "Ocaml" ocaml)
  161. (const :tag "Octave" octave)
  162. (const :tag "Org" org)
  163. (const :tag "Perl" perl)
  164. (const :tag "PlantUML" plantuml)
  165. (const :tag "Python" python)
  166. (const :tag "Ruby" ruby)
  167. (const :tag "Sass" sass)
  168. (const :tag "Scheme" scheme)
  169. (const :tag "Screen" screen)
  170. (const :tag "Shell Script" sh)
  171. (const :tag "Sql" sql)
  172. (const :tag "Sqlite" sqlite))
  173. :value-type (boolean :tag "Activate" :value t)))
  174. ;;;; Customization variables
  175. (defcustom org-clone-delete-id nil
  176. "Remove ID property of clones of a subtree.
  177. When non-nil, clones of a subtree don't inherit the ID property.
  178. Otherwise they inherit the ID property with a new unique
  179. identifier."
  180. :type 'boolean
  181. :group 'org-id)
  182. ;;; Version
  183. (defconst org-version "7.6"
  184. "The version number of the file org.el.")
  185. (defun org-version (&optional here)
  186. "Show the org-mode version in the echo area.
  187. With prefix arg HERE, insert it at point."
  188. (interactive "P")
  189. (let* ((origin default-directory)
  190. (version org-version)
  191. (git-version)
  192. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  193. (when (and (file-exists-p (expand-file-name ".git" dir))
  194. (executable-find "git"))
  195. (unwind-protect
  196. (progn
  197. (cd dir)
  198. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  199. (with-current-buffer "*Shell Command Output*"
  200. (goto-char (point-min))
  201. (setq git-version (buffer-substring (point) (point-at-eol))))
  202. (subst-char-in-string ?- ?. git-version t)
  203. (when (string-match "\\S-"
  204. (shell-command-to-string
  205. "git diff-index --name-only HEAD --"))
  206. (setq git-version (concat git-version ".dirty")))
  207. (setq version (concat version " (" git-version ")"))))
  208. (cd origin)))
  209. (setq version (format "Org-mode version %s" version))
  210. (if here (insert version))
  211. (message version)))
  212. ;;; Compatibility constants
  213. ;;; The custom variables
  214. (defgroup org nil
  215. "Outline-based notes management and organizer."
  216. :tag "Org"
  217. :group 'outlines
  218. :group 'calendar)
  219. (defcustom org-mode-hook nil
  220. "Mode hook for Org-mode, run after the mode was turned on."
  221. :group 'org
  222. :type 'hook)
  223. (defcustom org-load-hook nil
  224. "Hook that is run after org.el has been loaded."
  225. :group 'org
  226. :type 'hook)
  227. (defvar org-modules) ; defined below
  228. (defvar org-modules-loaded nil
  229. "Have the modules been loaded already?")
  230. (defun org-load-modules-maybe (&optional force)
  231. "Load all extensions listed in `org-modules'."
  232. (when (or force (not org-modules-loaded))
  233. (mapc (lambda (ext)
  234. (condition-case nil (require ext)
  235. (error (message "Problems while trying to load feature `%s'" ext))))
  236. org-modules)
  237. (setq org-modules-loaded t)))
  238. (defun org-set-modules (var value)
  239. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  240. (set var value)
  241. (when (featurep 'org)
  242. (org-load-modules-maybe 'force)))
  243. (when (org-bound-and-true-p org-modules)
  244. (let ((a (member 'org-infojs org-modules)))
  245. (and a (setcar a 'org-jsinfo))))
  246. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  247. "Modules that should always be loaded together with org.el.
  248. If a description starts with <C>, the file is not part of Emacs
  249. and loading it will require that you have downloaded and properly installed
  250. the org-mode distribution.
  251. You can also use this system to load external packages (i.e. neither Org
  252. core modules, nor modules from the CONTRIB directory). Just add symbols
  253. to the end of the list. If the package is called org-xyz.el, then you need
  254. to add the symbol `xyz', and the package must have a call to
  255. (provide 'org-xyz)"
  256. :group 'org
  257. :set 'org-set-modules
  258. :type
  259. '(set :greedy t
  260. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  261. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  262. (const :tag " crypt: Encryption of subtrees" org-crypt)
  263. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  264. (const :tag " docview: Links to doc-view buffers" org-docview)
  265. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  266. (const :tag " id: Global IDs for identifying entries" org-id)
  267. (const :tag " info: Links to Info nodes" org-info)
  268. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  269. (const :tag " habit: Track your consistency with habits" org-habit)
  270. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  271. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  272. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  273. (const :tag " mew Links to Mew folders/messages" org-mew)
  274. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  275. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  276. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  277. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  278. (const :tag " vm: Links to VM folders/messages" org-vm)
  279. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  280. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  281. (const :tag " mouse: Additional mouse support" org-mouse)
  282. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  283. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  284. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  285. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  286. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  287. (const :tag "C collector: Collect properties into tables" org-collector)
  288. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  289. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  290. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  291. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  292. (const :tag "C eval: Include command output as text" org-eval)
  293. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  294. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  295. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  296. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  297. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  298. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  299. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  300. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  301. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  302. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  303. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  304. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  305. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  306. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  307. (const :tag "C odt: OpenDocumentText exporter for Org-mode" org-odt)
  308. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  309. (const :tag "C registry: A registry for Org-mode links" org-registry)
  310. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  311. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  312. (const :tag "C secretary: Team management with org-mode" org-secretary)
  313. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  314. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  315. (const :tag "C track: Keep up with Org-mode development" org-track)
  316. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  317. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  318. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  319. (defcustom org-support-shift-select nil
  320. "Non-nil means make shift-cursor commands select text when possible.
  321. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  322. selecting a region, or enlarge regions started in this way.
  323. In Org-mode, in special contexts, these same keys are used for other
  324. purposes, important enough to compete with shift selection. Org tries
  325. to balance these needs by supporting `shift-select-mode' outside these
  326. special contexts, under control of this variable.
  327. The default of this variable is nil, to avoid confusing behavior. Shifted
  328. cursor keys will then execute Org commands in the following contexts:
  329. - on a headline, changing TODO state (left/right) and priority (up/down)
  330. - on a time stamp, changing the time
  331. - in a plain list item, changing the bullet type
  332. - in a property definition line, switching between allowed values
  333. - in the BEGIN line of a clock table (changing the time block).
  334. Outside these contexts, the commands will throw an error.
  335. When this variable is t and the cursor is not in a special context,
  336. Org-mode will support shift-selection for making and enlarging regions.
  337. To make this more effective, the bullet cycling will no longer happen
  338. anywhere in an item line, but only if the cursor is exactly on the bullet.
  339. If you set this variable to the symbol `always', then the keys
  340. will not be special in headlines, property lines, and item lines, to make
  341. shift selection work there as well. If this is what you want, you can
  342. use the following alternative commands: `C-c C-t' and `C-c ,' to
  343. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  344. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  345. edited by hand or in column view.
  346. However, when the cursor is on a timestamp, shift-cursor commands
  347. will still edit the time stamp - this is just too good to give up.
  348. XEmacs user should have this variable set to nil, because shift-select-mode
  349. is Emacs 23 only."
  350. :group 'org
  351. :type '(choice
  352. (const :tag "Never" nil)
  353. (const :tag "When outside special context" t)
  354. (const :tag "Everywhere except timestamps" always)))
  355. (defgroup org-startup nil
  356. "Options concerning startup of Org-mode."
  357. :tag "Org Startup"
  358. :group 'org)
  359. (defcustom org-startup-folded t
  360. "Non-nil means entering Org-mode will switch to OVERVIEW.
  361. This can also be configured on a per-file basis by adding one of
  362. the following lines anywhere in the buffer:
  363. #+STARTUP: fold (or `overview', this is equivalent)
  364. #+STARTUP: nofold (or `showall', this is equivalent)
  365. #+STARTUP: content
  366. #+STARTUP: showeverything"
  367. :group 'org-startup
  368. :type '(choice
  369. (const :tag "nofold: show all" nil)
  370. (const :tag "fold: overview" t)
  371. (const :tag "content: all headlines" content)
  372. (const :tag "show everything, even drawers" showeverything)))
  373. (defcustom org-startup-truncated t
  374. "Non-nil means entering Org-mode will set `truncate-lines'.
  375. This is useful since some lines containing links can be very long and
  376. uninteresting. Also tables look terrible when wrapped."
  377. :group 'org-startup
  378. :type 'boolean)
  379. (defcustom org-startup-indented nil
  380. "Non-nil means turn on `org-indent-mode' on startup.
  381. This can also be configured on a per-file basis by adding one of
  382. the following lines anywhere in the buffer:
  383. #+STARTUP: indent
  384. #+STARTUP: noindent"
  385. :group 'org-structure
  386. :type '(choice
  387. (const :tag "Not" nil)
  388. (const :tag "Globally (slow on startup in large files)" t)))
  389. (defcustom org-use-sub-superscripts t
  390. "Non-nil means interpret \"_\" and \"^\" for export.
  391. When this option is turned on, you can use TeX-like syntax for sub- and
  392. superscripts. Several characters after \"_\" or \"^\" will be
  393. considered as a single item - so grouping with {} is normally not
  394. needed. For example, the following things will be parsed as single
  395. sub- or superscripts.
  396. 10^24 or 10^tau several digits will be considered 1 item.
  397. 10^-12 or 10^-tau a leading sign with digits or a word
  398. x^2-y^3 will be read as x^2 - y^3, because items are
  399. terminated by almost any nonword/nondigit char.
  400. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  401. Still, ambiguity is possible - so when in doubt use {} to enclose the
  402. sub/superscript. If you set this variable to the symbol `{}',
  403. the braces are *required* in order to trigger interpretations as
  404. sub/superscript. This can be helpful in documents that need \"_\"
  405. frequently in plain text.
  406. Not all export backends support this, but HTML does.
  407. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  408. :group 'org-startup
  409. :group 'org-export-translation
  410. :type '(choice
  411. (const :tag "Always interpret" t)
  412. (const :tag "Only with braces" {})
  413. (const :tag "Never interpret" nil)))
  414. (if (fboundp 'defvaralias)
  415. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  416. (defcustom org-startup-with-beamer-mode nil
  417. "Non-nil means turn on `org-beamer-mode' on startup.
  418. This can also be configured on a per-file basis by adding one of
  419. the following lines anywhere in the buffer:
  420. #+STARTUP: beamer"
  421. :group 'org-startup
  422. :type 'boolean)
  423. (defcustom org-startup-align-all-tables nil
  424. "Non-nil means align all tables when visiting a file.
  425. This is useful when the column width in tables is forced with <N> cookies
  426. in table fields. Such tables will look correct only after the first re-align.
  427. This can also be configured on a per-file basis by adding one of
  428. the following lines anywhere in the buffer:
  429. #+STARTUP: align
  430. #+STARTUP: noalign"
  431. :group 'org-startup
  432. :type 'boolean)
  433. (defcustom org-startup-with-inline-images nil
  434. "Non-nil means show inline images when loading a new Org file.
  435. This can also be configured on a per-file basis by adding one of
  436. the following lines anywhere in the buffer:
  437. #+STARTUP: inlineimages
  438. #+STARTUP: noinlineimages"
  439. :group 'org-startup
  440. :type 'boolean)
  441. (defcustom org-insert-mode-line-in-empty-file nil
  442. "Non-nil means insert the first line setting Org-mode in empty files.
  443. When the function `org-mode' is called interactively in an empty file, this
  444. normally means that the file name does not automatically trigger Org-mode.
  445. To ensure that the file will always be in Org-mode in the future, a
  446. line enforcing Org-mode will be inserted into the buffer, if this option
  447. has been set."
  448. :group 'org-startup
  449. :type 'boolean)
  450. (defcustom org-replace-disputed-keys nil
  451. "Non-nil means use alternative key bindings for some keys.
  452. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  453. These keys are also used by other packages like shift-selection-mode'
  454. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  455. If you want to use Org-mode together with one of these other modes,
  456. or more generally if you would like to move some Org-mode commands to
  457. other keys, set this variable and configure the keys with the variable
  458. `org-disputed-keys'.
  459. This option is only relevant at load-time of Org-mode, and must be set
  460. *before* org.el is loaded. Changing it requires a restart of Emacs to
  461. become effective."
  462. :group 'org-startup
  463. :type 'boolean)
  464. (defcustom org-use-extra-keys nil
  465. "Non-nil means use extra key sequence definitions for certain commands.
  466. This happens automatically if you run XEmacs or if `window-system'
  467. is nil. This variable lets you do the same manually. You must
  468. set it before loading org.
  469. Example: on Carbon Emacs 22 running graphically, with an external
  470. keyboard on a Powerbook, the default way of setting M-left might
  471. not work for either Alt or ESC. Setting this variable will make
  472. it work for ESC."
  473. :group 'org-startup
  474. :type 'boolean)
  475. (if (fboundp 'defvaralias)
  476. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  477. (defcustom org-disputed-keys
  478. '(([(shift up)] . [(meta p)])
  479. ([(shift down)] . [(meta n)])
  480. ([(shift left)] . [(meta -)])
  481. ([(shift right)] . [(meta +)])
  482. ([(control shift right)] . [(meta shift +)])
  483. ([(control shift left)] . [(meta shift -)]))
  484. "Keys for which Org-mode and other modes compete.
  485. This is an alist, cars are the default keys, second element specifies
  486. the alternative to use when `org-replace-disputed-keys' is t.
  487. Keys can be specified in any syntax supported by `define-key'.
  488. The value of this option takes effect only at Org-mode's startup,
  489. therefore you'll have to restart Emacs to apply it after changing."
  490. :group 'org-startup
  491. :type 'alist)
  492. (defun org-key (key)
  493. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  494. Or return the original if not disputed.
  495. Also apply the translations defined in `org-xemacs-key-equivalents'."
  496. (when org-replace-disputed-keys
  497. (let* ((nkey (key-description key))
  498. (x (org-find-if (lambda (x)
  499. (equal (key-description (car x)) nkey))
  500. org-disputed-keys)))
  501. (setq key (if x (cdr x) key))))
  502. (when (featurep 'xemacs)
  503. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  504. key)
  505. (defun org-find-if (predicate seq)
  506. (catch 'exit
  507. (while seq
  508. (if (funcall predicate (car seq))
  509. (throw 'exit (car seq))
  510. (pop seq)))))
  511. (defun org-defkey (keymap key def)
  512. "Define a key, possibly translated, as returned by `org-key'."
  513. (define-key keymap (org-key key) def))
  514. (defcustom org-ellipsis nil
  515. "The ellipsis to use in the Org-mode outline.
  516. When nil, just use the standard three dots. When a string, use that instead,
  517. When a face, use the standard 3 dots, but with the specified face.
  518. The change affects only Org-mode (which will then use its own display table).
  519. Changing this requires executing `M-x org-mode' in a buffer to become
  520. effective."
  521. :group 'org-startup
  522. :type '(choice (const :tag "Default" nil)
  523. (face :tag "Face" :value org-warning)
  524. (string :tag "String" :value "...#")))
  525. (defvar org-display-table nil
  526. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  527. (defgroup org-keywords nil
  528. "Keywords in Org-mode."
  529. :tag "Org Keywords"
  530. :group 'org)
  531. (defcustom org-deadline-string "DEADLINE:"
  532. "String to mark deadline entries.
  533. A deadline is this string, followed by a time stamp. Should be a word,
  534. terminated by a colon. You can insert a schedule keyword and
  535. a timestamp with \\[org-deadline].
  536. Changes become only effective after restarting Emacs."
  537. :group 'org-keywords
  538. :type 'string)
  539. (defcustom org-scheduled-string "SCHEDULED:"
  540. "String to mark scheduled TODO entries.
  541. A schedule is this string, followed by a time stamp. Should be a word,
  542. terminated by a colon. You can insert a schedule keyword and
  543. a timestamp with \\[org-schedule].
  544. Changes become only effective after restarting Emacs."
  545. :group 'org-keywords
  546. :type 'string)
  547. (defcustom org-closed-string "CLOSED:"
  548. "String used as the prefix for timestamps logging closing a TODO entry."
  549. :group 'org-keywords
  550. :type 'string)
  551. (defcustom org-clock-string "CLOCK:"
  552. "String used as prefix for timestamps clocking work hours on an item."
  553. :group 'org-keywords
  554. :type 'string)
  555. (defcustom org-comment-string "COMMENT"
  556. "Entries starting with this keyword will never be exported.
  557. An entry can be toggled between COMMENT and normal with
  558. \\[org-toggle-comment].
  559. Changes become only effective after restarting Emacs."
  560. :group 'org-keywords
  561. :type 'string)
  562. (defcustom org-quote-string "QUOTE"
  563. "Entries starting with this keyword will be exported in fixed-width font.
  564. Quoting applies only to the text in the entry following the headline, and does
  565. not extend beyond the next headline, even if that is lower level.
  566. An entry can be toggled between QUOTE and normal with
  567. \\[org-toggle-fixed-width-section]."
  568. :group 'org-keywords
  569. :type 'string)
  570. (defconst org-repeat-re
  571. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  572. "Regular expression for specifying repeated events.
  573. After a match, group 1 contains the repeat expression.")
  574. (defgroup org-structure nil
  575. "Options concerning the general structure of Org-mode files."
  576. :tag "Org Structure"
  577. :group 'org)
  578. (defgroup org-reveal-location nil
  579. "Options about how to make context of a location visible."
  580. :tag "Org Reveal Location"
  581. :group 'org-structure)
  582. (defconst org-context-choice
  583. '(choice
  584. (const :tag "Always" t)
  585. (const :tag "Never" nil)
  586. (repeat :greedy t :tag "Individual contexts"
  587. (cons
  588. (choice :tag "Context"
  589. (const agenda)
  590. (const org-goto)
  591. (const occur-tree)
  592. (const tags-tree)
  593. (const link-search)
  594. (const mark-goto)
  595. (const bookmark-jump)
  596. (const isearch)
  597. (const default))
  598. (boolean))))
  599. "Contexts for the reveal options.")
  600. (defcustom org-show-hierarchy-above '((default . t))
  601. "Non-nil means show full hierarchy when revealing a location.
  602. Org-mode often shows locations in an org-mode file which might have
  603. been invisible before. When this is set, the hierarchy of headings
  604. above the exposed location is shown.
  605. Turning this off for example for sparse trees makes them very compact.
  606. Instead of t, this can also be an alist specifying this option for different
  607. contexts. Valid contexts are
  608. agenda when exposing an entry from the agenda
  609. org-goto when using the command `org-goto' on key C-c C-j
  610. occur-tree when using the command `org-occur' on key C-c /
  611. tags-tree when constructing a sparse tree based on tags matches
  612. link-search when exposing search matches associated with a link
  613. mark-goto when exposing the jump goal of a mark
  614. bookmark-jump when exposing a bookmark location
  615. isearch when exiting from an incremental search
  616. default default for all contexts not set explicitly"
  617. :group 'org-reveal-location
  618. :type org-context-choice)
  619. (defcustom org-show-following-heading '((default . nil))
  620. "Non-nil means show following heading when revealing a location.
  621. Org-mode often shows locations in an org-mode file which might have
  622. been invisible before. When this is set, the heading following the
  623. match is shown.
  624. Turning this off for example for sparse trees makes them very compact,
  625. but makes it harder to edit the location of the match. In such a case,
  626. use the command \\[org-reveal] to show more context.
  627. Instead of t, this can also be an alist specifying this option for different
  628. contexts. See `org-show-hierarchy-above' for valid contexts."
  629. :group 'org-reveal-location
  630. :type org-context-choice)
  631. (defcustom org-show-siblings '((default . nil) (isearch t))
  632. "Non-nil means show all sibling heading when revealing a location.
  633. Org-mode often shows locations in an org-mode file which might have
  634. been invisible before. When this is set, the sibling of the current entry
  635. heading are all made visible. If `org-show-hierarchy-above' is t,
  636. the same happens on each level of the hierarchy above the current entry.
  637. By default this is on for the isearch context, off for all other contexts.
  638. Turning this off for example for sparse trees makes them very compact,
  639. but makes it harder to edit the location of the match. In such a case,
  640. use the command \\[org-reveal] to show more context.
  641. Instead of t, this can also be an alist specifying this option for different
  642. contexts. See `org-show-hierarchy-above' for valid contexts."
  643. :group 'org-reveal-location
  644. :type org-context-choice)
  645. (defcustom org-show-entry-below '((default . nil))
  646. "Non-nil means show the entry below a headline when revealing a location.
  647. Org-mode often shows locations in an org-mode file which might have
  648. been invisible before. When this is set, the text below the headline that is
  649. exposed is also shown.
  650. By default this is off for all contexts.
  651. Instead of t, this can also be an alist specifying this option for different
  652. contexts. See `org-show-hierarchy-above' for valid contexts."
  653. :group 'org-reveal-location
  654. :type org-context-choice)
  655. (defcustom org-indirect-buffer-display 'other-window
  656. "How should indirect tree buffers be displayed?
  657. This applies to indirect buffers created with the commands
  658. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  659. Valid values are:
  660. current-window Display in the current window
  661. other-window Just display in another window.
  662. dedicated-frame Create one new frame, and re-use it each time.
  663. new-frame Make a new frame each time. Note that in this case
  664. previously-made indirect buffers are kept, and you need to
  665. kill these buffers yourself."
  666. :group 'org-structure
  667. :group 'org-agenda-windows
  668. :type '(choice
  669. (const :tag "In current window" current-window)
  670. (const :tag "In current frame, other window" other-window)
  671. (const :tag "Each time a new frame" new-frame)
  672. (const :tag "One dedicated frame" dedicated-frame)))
  673. (defcustom org-use-speed-commands nil
  674. "Non-nil means activate single letter commands at beginning of a headline.
  675. This may also be a function to test for appropriate locations where speed
  676. commands should be active."
  677. :group 'org-structure
  678. :type '(choice
  679. (const :tag "Never" nil)
  680. (const :tag "At beginning of headline stars" t)
  681. (function)))
  682. (defcustom org-speed-commands-user nil
  683. "Alist of additional speed commands.
  684. This list will be checked before `org-speed-commands-default'
  685. when the variable `org-use-speed-commands' is non-nil
  686. and when the cursor is at the beginning of a headline.
  687. The car if each entry is a string with a single letter, which must
  688. be assigned to `self-insert-command' in the global map.
  689. The cdr is either a command to be called interactively, a function
  690. to be called, or a form to be evaluated.
  691. An entry that is just a list with a single string will be interpreted
  692. as a descriptive headline that will be added when listing the speed
  693. commands in the Help buffer using the `?' speed command."
  694. :group 'org-structure
  695. :type '(repeat :value ("k" . ignore)
  696. (choice :value ("k" . ignore)
  697. (list :tag "Descriptive Headline" (string :tag "Headline"))
  698. (cons :tag "Letter and Command"
  699. (string :tag "Command letter")
  700. (choice
  701. (function)
  702. (sexp))))))
  703. (defgroup org-cycle nil
  704. "Options concerning visibility cycling in Org-mode."
  705. :tag "Org Cycle"
  706. :group 'org-structure)
  707. (defcustom org-cycle-skip-children-state-if-no-children t
  708. "Non-nil means skip CHILDREN state in entries that don't have any."
  709. :group 'org-cycle
  710. :type 'boolean)
  711. (defcustom org-cycle-max-level nil
  712. "Maximum level which should still be subject to visibility cycling.
  713. Levels higher than this will, for cycling, be treated as text, not a headline.
  714. When `org-odd-levels-only' is set, a value of N in this variable actually
  715. means 2N-1 stars as the limiting headline.
  716. When nil, cycle all levels.
  717. Note that the limiting level of cycling is also influenced by
  718. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  719. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  720. than its value."
  721. :group 'org-cycle
  722. :type '(choice
  723. (const :tag "No limit" nil)
  724. (integer :tag "Maximum level")))
  725. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  726. "Names of drawers. Drawers are not opened by cycling on the headline above.
  727. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  728. this:
  729. :DRAWERNAME:
  730. .....
  731. :END:
  732. The drawer \"PROPERTIES\" is special for capturing properties through
  733. the property API.
  734. Drawers can be defined on the per-file basis with a line like:
  735. #+DRAWERS: HIDDEN STATE PROPERTIES"
  736. :group 'org-structure
  737. :group 'org-cycle
  738. :type '(repeat (string :tag "Drawer Name")))
  739. (defcustom org-hide-block-startup nil
  740. "Non-nil means entering Org-mode will fold all blocks.
  741. This can also be set in on a per-file basis with
  742. #+STARTUP: hideblocks
  743. #+STARTUP: showblocks"
  744. :group 'org-startup
  745. :group 'org-cycle
  746. :type 'boolean)
  747. (defcustom org-cycle-global-at-bob nil
  748. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  749. This makes it possible to do global cycling without having to use S-TAB or
  750. \\[universal-argument] TAB. For this special case to work, the first line \
  751. of the buffer
  752. must not be a headline - it may be empty or some other text. When used in
  753. this way, `org-cycle-hook' is disables temporarily, to make sure the
  754. cursor stays at the beginning of the buffer.
  755. When this option is nil, don't do anything special at the beginning
  756. of the buffer."
  757. :group 'org-cycle
  758. :type 'boolean)
  759. (defcustom org-cycle-level-after-item/entry-creation t
  760. "Non-nil means cycle entry level or item indentation in new empty entries.
  761. When the cursor is at the end of an empty headline, i.e with only stars
  762. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  763. and then all possible ancestor states, before returning to the original state.
  764. This makes data entry extremely fast: M-RET to create a new headline,
  765. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  766. When the cursor is at the end of an empty plain list item, one TAB will
  767. make it a subitem, two or more tabs will back up to make this an item
  768. higher up in the item hierarchy."
  769. :group 'org-cycle
  770. :type 'boolean)
  771. (defcustom org-cycle-emulate-tab t
  772. "Where should `org-cycle' emulate TAB.
  773. nil Never
  774. white Only in completely white lines
  775. whitestart Only at the beginning of lines, before the first non-white char
  776. t Everywhere except in headlines
  777. exc-hl-bol Everywhere except at the start of a headline
  778. If TAB is used in a place where it does not emulate TAB, the current subtree
  779. visibility is cycled."
  780. :group 'org-cycle
  781. :type '(choice (const :tag "Never" nil)
  782. (const :tag "Only in completely white lines" white)
  783. (const :tag "Before first char in a line" whitestart)
  784. (const :tag "Everywhere except in headlines" t)
  785. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  786. ))
  787. (defcustom org-cycle-separator-lines 2
  788. "Number of empty lines needed to keep an empty line between collapsed trees.
  789. If you leave an empty line between the end of a subtree and the following
  790. headline, this empty line is hidden when the subtree is folded.
  791. Org-mode will leave (exactly) one empty line visible if the number of
  792. empty lines is equal or larger to the number given in this variable.
  793. So the default 2 means at least 2 empty lines after the end of a subtree
  794. are needed to produce free space between a collapsed subtree and the
  795. following headline.
  796. If the number is negative, and the number of empty lines is at least -N,
  797. all empty lines are shown.
  798. Special case: when 0, never leave empty lines in collapsed view."
  799. :group 'org-cycle
  800. :type 'integer)
  801. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  802. (defcustom org-pre-cycle-hook nil
  803. "Hook that is run before visibility cycling is happening.
  804. The function(s) in this hook must accept a single argument which indicates
  805. the new state that will be set right after running this hook. The
  806. argument is a symbol. Before a global state change, it can have the values
  807. `overview', `content', or `all'. Before a local state change, it can have
  808. the values `folded', `children', or `subtree'."
  809. :group 'org-cycle
  810. :type 'hook)
  811. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  812. org-cycle-hide-drawers
  813. org-cycle-show-empty-lines
  814. org-optimize-window-after-visibility-change)
  815. "Hook that is run after `org-cycle' has changed the buffer visibility.
  816. The function(s) in this hook must accept a single argument which indicates
  817. the new state that was set by the most recent `org-cycle' command. The
  818. argument is a symbol. After a global state change, it can have the values
  819. `overview', `content', or `all'. After a local state change, it can have
  820. the values `folded', `children', or `subtree'."
  821. :group 'org-cycle
  822. :type 'hook)
  823. (defgroup org-edit-structure nil
  824. "Options concerning structure editing in Org-mode."
  825. :tag "Org Edit Structure"
  826. :group 'org-structure)
  827. (defcustom org-odd-levels-only nil
  828. "Non-nil means skip even levels and only use odd levels for the outline.
  829. This has the effect that two stars are being added/taken away in
  830. promotion/demotion commands. It also influences how levels are
  831. handled by the exporters.
  832. Changing it requires restart of `font-lock-mode' to become effective
  833. for fontification also in regions already fontified.
  834. You may also set this on a per-file basis by adding one of the following
  835. lines to the buffer:
  836. #+STARTUP: odd
  837. #+STARTUP: oddeven"
  838. :group 'org-edit-structure
  839. :group 'org-appearance
  840. :type 'boolean)
  841. (defcustom org-adapt-indentation t
  842. "Non-nil means adapt indentation to outline node level.
  843. When this variable is set, Org assumes that you write outlines by
  844. indenting text in each node to align with the headline (after the stars).
  845. The following issues are influenced by this variable:
  846. - When this is set and the *entire* text in an entry is indented, the
  847. indentation is increased by one space in a demotion command, and
  848. decreased by one in a promotion command. If any line in the entry
  849. body starts with text at column 0, indentation is not changed at all.
  850. - Property drawers and planning information is inserted indented when
  851. this variable s set. When nil, they will not be indented.
  852. - TAB indents a line relative to context. The lines below a headline
  853. will be indented when this variable is set.
  854. Note that this is all about true indentation, by adding and removing
  855. space characters. See also `org-indent.el' which does level-dependent
  856. indentation in a virtual way, i.e. at display time in Emacs."
  857. :group 'org-edit-structure
  858. :type 'boolean)
  859. (defcustom org-special-ctrl-a/e nil
  860. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  861. When t, `C-a' will bring back the cursor to the beginning of the
  862. headline text, i.e. after the stars and after a possible TODO keyword.
  863. In an item, this will be the position after the bullet.
  864. When the cursor is already at that position, another `C-a' will bring
  865. it to the beginning of the line.
  866. `C-e' will jump to the end of the headline, ignoring the presence of tags
  867. in the headline. A second `C-e' will then jump to the true end of the
  868. line, after any tags. This also means that, when this variable is
  869. non-nil, `C-e' also will never jump beyond the end of the heading of a
  870. folded section, i.e. not after the ellipses.
  871. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  872. going to the true line boundary first. Only a directly following, identical
  873. keypress will bring the cursor to the special positions.
  874. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  875. set separately."
  876. :group 'org-edit-structure
  877. :type '(choice
  878. (const :tag "off" nil)
  879. (const :tag "on: after stars/bullet and before tags first" t)
  880. (const :tag "reversed: true line boundary first" reversed)
  881. (cons :tag "Set C-a and C-e separately"
  882. (choice :tag "Special C-a"
  883. (const :tag "off" nil)
  884. (const :tag "on: after stars/bullet first" t)
  885. (const :tag "reversed: before stars/bullet first" reversed))
  886. (choice :tag "Special C-e"
  887. (const :tag "off" nil)
  888. (const :tag "on: before tags first" t)
  889. (const :tag "reversed: after tags first" reversed)))))
  890. (if (fboundp 'defvaralias)
  891. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  892. (defcustom org-special-ctrl-k nil
  893. "Non-nil means `C-k' will behave specially in headlines.
  894. When nil, `C-k' will call the default `kill-line' command.
  895. When t, the following will happen while the cursor is in the headline:
  896. - When the cursor is at the beginning of a headline, kill the entire
  897. line and possible the folded subtree below the line.
  898. - When in the middle of the headline text, kill the headline up to the tags.
  899. - When after the headline text, kill the tags."
  900. :group 'org-edit-structure
  901. :type 'boolean)
  902. (defcustom org-ctrl-k-protect-subtree nil
  903. "Non-nil means, do not delete a hidden subtree with C-k.
  904. When set to the symbol `error', simply throw an error when C-k is
  905. used to kill (part-of) a headline that has hidden text behind it.
  906. Any other non-nil value will result in a query to the user, if it is
  907. OK to kill that hidden subtree. When nil, kill without remorse."
  908. :group 'org-edit-structure
  909. :type '(choice
  910. (const :tag "Do not protect hidden subtrees" nil)
  911. (const :tag "Protect hidden subtrees with a security query" t)
  912. (const :tag "Never kill a hidden subtree with C-k" error)))
  913. (defcustom org-yank-folded-subtrees t
  914. "Non-nil means when yanking subtrees, fold them.
  915. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  916. it starts with a heading and all other headings in it are either children
  917. or siblings, then fold all the subtrees. However, do this only if no
  918. text after the yank would be swallowed into a folded tree by this action."
  919. :group 'org-edit-structure
  920. :type 'boolean)
  921. (defcustom org-yank-adjusted-subtrees nil
  922. "Non-nil means when yanking subtrees, adjust the level.
  923. With this setting, `org-paste-subtree' is used to insert the subtree, see
  924. this function for details."
  925. :group 'org-edit-structure
  926. :type 'boolean)
  927. (defcustom org-M-RET-may-split-line '((default . t))
  928. "Non-nil means M-RET will split the line at the cursor position.
  929. When nil, it will go to the end of the line before making a
  930. new line.
  931. You may also set this option in a different way for different
  932. contexts. Valid contexts are:
  933. headline when creating a new headline
  934. item when creating a new item
  935. table in a table field
  936. default the value to be used for all contexts not explicitly
  937. customized"
  938. :group 'org-structure
  939. :group 'org-table
  940. :type '(choice
  941. (const :tag "Always" t)
  942. (const :tag "Never" nil)
  943. (repeat :greedy t :tag "Individual contexts"
  944. (cons
  945. (choice :tag "Context"
  946. (const headline)
  947. (const item)
  948. (const table)
  949. (const default))
  950. (boolean)))))
  951. (defcustom org-insert-heading-respect-content nil
  952. "Non-nil means insert new headings after the current subtree.
  953. When nil, the new heading is created directly after the current line.
  954. The commands \\[org-insert-heading-respect-content] and
  955. \\[org-insert-todo-heading-respect-content] turn this variable on
  956. for the duration of the command."
  957. :group 'org-structure
  958. :type 'boolean)
  959. (defcustom org-blank-before-new-entry '((heading . auto)
  960. (plain-list-item . auto))
  961. "Should `org-insert-heading' leave a blank line before new heading/item?
  962. The value is an alist, with `heading' and `plain-list-item' as CAR,
  963. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  964. which case Org will look at the surrounding headings/items and try to
  965. make an intelligent decision whether to insert a blank line or not.
  966. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  967. set, the setting here is ignored and no empty line is inserted, to avoid
  968. breaking the list structure."
  969. :group 'org-edit-structure
  970. :type '(list
  971. (cons (const heading)
  972. (choice (const :tag "Never" nil)
  973. (const :tag "Always" t)
  974. (const :tag "Auto" auto)))
  975. (cons (const plain-list-item)
  976. (choice (const :tag "Never" nil)
  977. (const :tag "Always" t)
  978. (const :tag "Auto" auto)))))
  979. (defcustom org-insert-heading-hook nil
  980. "Hook being run after inserting a new heading."
  981. :group 'org-edit-structure
  982. :type 'hook)
  983. (defcustom org-enable-fixed-width-editor t
  984. "Non-nil means lines starting with \":\" are treated as fixed-width.
  985. This currently only means they are never auto-wrapped.
  986. When nil, such lines will be treated like ordinary lines.
  987. See also the QUOTE keyword."
  988. :group 'org-edit-structure
  989. :type 'boolean)
  990. (defcustom org-goto-auto-isearch t
  991. "Non-nil means typing characters in `org-goto' starts incremental search."
  992. :group 'org-edit-structure
  993. :type 'boolean)
  994. (defgroup org-sparse-trees nil
  995. "Options concerning sparse trees in Org-mode."
  996. :tag "Org Sparse Trees"
  997. :group 'org-structure)
  998. (defcustom org-highlight-sparse-tree-matches t
  999. "Non-nil means highlight all matches that define a sparse tree.
  1000. The highlights will automatically disappear the next time the buffer is
  1001. changed by an edit command."
  1002. :group 'org-sparse-trees
  1003. :type 'boolean)
  1004. (defcustom org-remove-highlights-with-change t
  1005. "Non-nil means any change to the buffer will remove temporary highlights.
  1006. Such highlights are created by `org-occur' and `org-clock-display'.
  1007. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1008. The highlights created by `org-preview-latex-fragment' always need
  1009. `C-c C-c' to be removed."
  1010. :group 'org-sparse-trees
  1011. :group 'org-time
  1012. :type 'boolean)
  1013. (defcustom org-occur-hook '(org-first-headline-recenter)
  1014. "Hook that is run after `org-occur' has constructed a sparse tree.
  1015. This can be used to recenter the window to show as much of the structure
  1016. as possible."
  1017. :group 'org-sparse-trees
  1018. :type 'hook)
  1019. (defgroup org-imenu-and-speedbar nil
  1020. "Options concerning imenu and speedbar in Org-mode."
  1021. :tag "Org Imenu and Speedbar"
  1022. :group 'org-structure)
  1023. (defcustom org-imenu-depth 2
  1024. "The maximum level for Imenu access to Org-mode headlines.
  1025. This also applied for speedbar access."
  1026. :group 'org-imenu-and-speedbar
  1027. :type 'integer)
  1028. (defgroup org-table nil
  1029. "Options concerning tables in Org-mode."
  1030. :tag "Org Table"
  1031. :group 'org)
  1032. (defcustom org-enable-table-editor 'optimized
  1033. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1034. When nil, such lines will be treated like ordinary lines.
  1035. When equal to the symbol `optimized', the table editor will be optimized to
  1036. do the following:
  1037. - Automatic overwrite mode in front of whitespace in table fields.
  1038. This makes the structure of the table stay in tact as long as the edited
  1039. field does not exceed the column width.
  1040. - Minimize the number of realigns. Normally, the table is aligned each time
  1041. TAB or RET are pressed to move to another field. With optimization this
  1042. happens only if changes to a field might have changed the column width.
  1043. Optimization requires replacing the functions `self-insert-command',
  1044. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1045. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1046. very good at guessing when a re-align will be necessary, but you can always
  1047. force one with \\[org-ctrl-c-ctrl-c].
  1048. If you would like to use the optimized version in Org-mode, but the
  1049. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1050. This variable can be used to turn on and off the table editor during a session,
  1051. but in order to toggle optimization, a restart is required.
  1052. See also the variable `org-table-auto-blank-field'."
  1053. :group 'org-table
  1054. :type '(choice
  1055. (const :tag "off" nil)
  1056. (const :tag "on" t)
  1057. (const :tag "on, optimized" optimized)))
  1058. (defcustom org-self-insert-cluster-for-undo t
  1059. "Non-nil means cluster self-insert commands for undo when possible.
  1060. If this is set, then, like in the Emacs command loop, 20 consecutive
  1061. characters will be undone together.
  1062. This is configurable, because there is some impact on typing performance."
  1063. :group 'org-table
  1064. :type 'boolean)
  1065. (defcustom org-table-tab-recognizes-table.el t
  1066. "Non-nil means TAB will automatically notice a table.el table.
  1067. When it sees such a table, it moves point into it and - if necessary -
  1068. calls `table-recognize-table'."
  1069. :group 'org-table-editing
  1070. :type 'boolean)
  1071. (defgroup org-link nil
  1072. "Options concerning links in Org-mode."
  1073. :tag "Org Link"
  1074. :group 'org)
  1075. (defvar org-link-abbrev-alist-local nil
  1076. "Buffer-local version of `org-link-abbrev-alist', which see.
  1077. The value of this is taken from the #+LINK lines.")
  1078. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1079. (defcustom org-link-abbrev-alist nil
  1080. "Alist of link abbreviations.
  1081. The car of each element is a string, to be replaced at the start of a link.
  1082. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1083. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1084. [[linkkey:tag][description]]
  1085. The 'linkkey' must be a word word, starting with a letter, followed
  1086. by letters, numbers, '-' or '_'.
  1087. If REPLACE is a string, the tag will simply be appended to create the link.
  1088. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1089. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1090. be inserted at that point (see the function `url-hexify-string').
  1091. REPLACE may also be a function that will be called with the tag as the
  1092. only argument to create the link, which should be returned as a string.
  1093. See the manual for examples."
  1094. :group 'org-link
  1095. :type '(repeat
  1096. (cons
  1097. (string :tag "Protocol")
  1098. (choice
  1099. (string :tag "Format")
  1100. (function)))))
  1101. (defcustom org-descriptive-links t
  1102. "Non-nil means hide link part and only show description of bracket links.
  1103. Bracket links are like [[link][description]]. This variable sets the initial
  1104. state in new org-mode buffers. The setting can then be toggled on a
  1105. per-buffer basis from the Org->Hyperlinks menu."
  1106. :group 'org-link
  1107. :type 'boolean)
  1108. (defcustom org-link-file-path-type 'adaptive
  1109. "How the path name in file links should be stored.
  1110. Valid values are:
  1111. relative Relative to the current directory, i.e. the directory of the file
  1112. into which the link is being inserted.
  1113. absolute Absolute path, if possible with ~ for home directory.
  1114. noabbrev Absolute path, no abbreviation of home directory.
  1115. adaptive Use relative path for files in the current directory and sub-
  1116. directories of it. For other files, use an absolute path."
  1117. :group 'org-link
  1118. :type '(choice
  1119. (const relative)
  1120. (const absolute)
  1121. (const noabbrev)
  1122. (const adaptive)))
  1123. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1124. "Types of links that should be activated in Org-mode files.
  1125. This is a list of symbols, each leading to the activation of a certain link
  1126. type. In principle, it does not hurt to turn on most link types - there may
  1127. be a small gain when turning off unused link types. The types are:
  1128. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1129. angle Links in angular brackets that may contain whitespace like
  1130. <bbdb:Carsten Dominik>.
  1131. plain Plain links in normal text, no whitespace, like http://google.com.
  1132. radio Text that is matched by a radio target, see manual for details.
  1133. tag Tag settings in a headline (link to tag search).
  1134. date Time stamps (link to calendar).
  1135. footnote Footnote labels.
  1136. Changing this variable requires a restart of Emacs to become effective."
  1137. :group 'org-link
  1138. :type '(set :greedy t
  1139. (const :tag "Double bracket links" bracket)
  1140. (const :tag "Angular bracket links" angle)
  1141. (const :tag "Plain text links" plain)
  1142. (const :tag "Radio target matches" radio)
  1143. (const :tag "Tags" tag)
  1144. (const :tag "Timestamps" date)
  1145. (const :tag "Footnotes" footnote)))
  1146. (defcustom org-make-link-description-function nil
  1147. "Function to use to generate link descriptions from links.
  1148. If nil the link location will be used. This function must take
  1149. two parameters; the first is the link and the second the
  1150. description `org-insert-link' has generated, and should return the
  1151. description to use."
  1152. :group 'org-link
  1153. :type 'function)
  1154. (defgroup org-link-store nil
  1155. "Options concerning storing links in Org-mode."
  1156. :tag "Org Store Link"
  1157. :group 'org-link)
  1158. (defcustom org-email-link-description-format "Email %c: %.30s"
  1159. "Format of the description part of a link to an email or usenet message.
  1160. The following %-escapes will be replaced by corresponding information:
  1161. %F full \"From\" field
  1162. %f name, taken from \"From\" field, address if no name
  1163. %T full \"To\" field
  1164. %t first name in \"To\" field, address if no name
  1165. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1166. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1167. %s subject
  1168. %d date
  1169. %m message-id.
  1170. You may use normal field width specification between the % and the letter.
  1171. This is for example useful to limit the length of the subject.
  1172. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1173. :group 'org-link-store
  1174. :type 'string)
  1175. (defcustom org-from-is-user-regexp
  1176. (let (r1 r2)
  1177. (when (and user-mail-address (not (string= user-mail-address "")))
  1178. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1179. (when (and user-full-name (not (string= user-full-name "")))
  1180. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1181. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1182. "Regexp matched against the \"From:\" header of an email or usenet message.
  1183. It should match if the message is from the user him/herself."
  1184. :group 'org-link-store
  1185. :type 'regexp)
  1186. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1187. "Non-nil means storing a link to an Org file will use entry IDs.
  1188. Note that before this variable is even considered, org-id must be loaded,
  1189. so please customize `org-modules' and turn it on.
  1190. The variable can have the following values:
  1191. t Create an ID if needed to make a link to the current entry.
  1192. create-if-interactive
  1193. If `org-store-link' is called directly (interactively, as a user
  1194. command), do create an ID to support the link. But when doing the
  1195. job for remember, only use the ID if it already exists. The
  1196. purpose of this setting is to avoid proliferation of unwanted
  1197. IDs, just because you happen to be in an Org file when you
  1198. call `org-remember' that automatically and preemptively
  1199. creates a link. If you do want to get an ID link in a remember
  1200. template to an entry not having an ID, create it first by
  1201. explicitly creating a link to it, using `C-c C-l' first.
  1202. create-if-interactive-and-no-custom-id
  1203. Like create-if-interactive, but do not create an ID if there is
  1204. a CUSTOM_ID property defined in the entry. This is the default.
  1205. use-existing
  1206. Use existing ID, do not create one.
  1207. nil Never use an ID to make a link, instead link using a text search for
  1208. the headline text."
  1209. :group 'org-link-store
  1210. :type '(choice
  1211. (const :tag "Create ID to make link" t)
  1212. (const :tag "Create if storing link interactively"
  1213. create-if-interactive)
  1214. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1215. create-if-interactive-and-no-custom-id)
  1216. (const :tag "Only use existing" use-existing)
  1217. (const :tag "Do not use ID to create link" nil)))
  1218. (defcustom org-context-in-file-links t
  1219. "Non-nil means file links from `org-store-link' contain context.
  1220. A search string will be added to the file name with :: as separator and
  1221. used to find the context when the link is activated by the command
  1222. `org-open-at-point'. When this option is t, the entire active region
  1223. will be placed in the search string of the file link. If set to a
  1224. positive integer, only the first n lines of context will be stored.
  1225. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1226. negates this setting for the duration of the command."
  1227. :group 'org-link-store
  1228. :type '(choice boolean integer))
  1229. (defcustom org-keep-stored-link-after-insertion nil
  1230. "Non-nil means keep link in list for entire session.
  1231. The command `org-store-link' adds a link pointing to the current
  1232. location to an internal list. These links accumulate during a session.
  1233. The command `org-insert-link' can be used to insert links into any
  1234. Org-mode file (offering completion for all stored links). When this
  1235. option is nil, every link which has been inserted once using \\[org-insert-link]
  1236. will be removed from the list, to make completing the unused links
  1237. more efficient."
  1238. :group 'org-link-store
  1239. :type 'boolean)
  1240. (defgroup org-link-follow nil
  1241. "Options concerning following links in Org-mode."
  1242. :tag "Org Follow Link"
  1243. :group 'org-link)
  1244. (defcustom org-link-translation-function nil
  1245. "Function to translate links with different syntax to Org syntax.
  1246. This can be used to translate links created for example by the Planner
  1247. or emacs-wiki packages to Org syntax.
  1248. The function must accept two parameters, a TYPE containing the link
  1249. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1250. which is everything after the link protocol. It should return a cons
  1251. with possibly modified values of type and path.
  1252. Org contains a function for this, so if you set this variable to
  1253. `org-translate-link-from-planner', you should be able follow many
  1254. links created by planner."
  1255. :group 'org-link-follow
  1256. :type 'function)
  1257. (defcustom org-follow-link-hook nil
  1258. "Hook that is run after a link has been followed."
  1259. :group 'org-link-follow
  1260. :type 'hook)
  1261. (defcustom org-tab-follows-link nil
  1262. "Non-nil means on links TAB will follow the link.
  1263. Needs to be set before org.el is loaded.
  1264. This really should not be used, it does not make sense, and the
  1265. implementation is bad."
  1266. :group 'org-link-follow
  1267. :type 'boolean)
  1268. (defcustom org-return-follows-link nil
  1269. "Non-nil means on links RET will follow the link."
  1270. :group 'org-link-follow
  1271. :type 'boolean)
  1272. (defcustom org-mouse-1-follows-link
  1273. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1274. "Non-nil means mouse-1 on a link will follow the link.
  1275. A longer mouse click will still set point. Does not work on XEmacs.
  1276. Needs to be set before org.el is loaded."
  1277. :group 'org-link-follow
  1278. :type 'boolean)
  1279. (defcustom org-mark-ring-length 4
  1280. "Number of different positions to be recorded in the ring.
  1281. Changing this requires a restart of Emacs to work correctly."
  1282. :group 'org-link-follow
  1283. :type 'integer)
  1284. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1285. "Non-nil means internal links in Org files must exactly match a headline.
  1286. When nil, the link search tries to match a phrase with all words
  1287. in the search text."
  1288. :group 'org-link-follow
  1289. :type '(choice
  1290. (const :tag "Use fuzy text search" nil)
  1291. (const :tag "Match only exact headline" t)
  1292. (const :tag "Match extact headline or query to create it"
  1293. query-to-create)))
  1294. (defcustom org-link-frame-setup
  1295. '((vm . vm-visit-folder-other-frame)
  1296. (gnus . org-gnus-no-new-news)
  1297. (file . find-file-other-window)
  1298. (wl . wl-other-frame))
  1299. "Setup the frame configuration for following links.
  1300. When following a link with Emacs, it may often be useful to display
  1301. this link in another window or frame. This variable can be used to
  1302. set this up for the different types of links.
  1303. For VM, use any of
  1304. `vm-visit-folder'
  1305. `vm-visit-folder-other-window'
  1306. `vm-visit-folder-other-frame'
  1307. For Gnus, use any of
  1308. `gnus'
  1309. `gnus-other-frame'
  1310. `org-gnus-no-new-news'
  1311. For FILE, use any of
  1312. `find-file'
  1313. `find-file-other-window'
  1314. `find-file-other-frame'
  1315. For Wanderlust use any of
  1316. `wl'
  1317. `wl-other-frame'
  1318. For the calendar, use the variable `calendar-setup'.
  1319. For BBDB, it is currently only possible to display the matches in
  1320. another window."
  1321. :group 'org-link-follow
  1322. :type '(list
  1323. (cons (const vm)
  1324. (choice
  1325. (const vm-visit-folder)
  1326. (const vm-visit-folder-other-window)
  1327. (const vm-visit-folder-other-frame)))
  1328. (cons (const gnus)
  1329. (choice
  1330. (const gnus)
  1331. (const gnus-other-frame)
  1332. (const org-gnus-no-new-news)))
  1333. (cons (const file)
  1334. (choice
  1335. (const find-file)
  1336. (const find-file-other-window)
  1337. (const find-file-other-frame)))
  1338. (cons (const wl)
  1339. (choice
  1340. (const wl)
  1341. (const wl-other-frame)))))
  1342. (defcustom org-display-internal-link-with-indirect-buffer nil
  1343. "Non-nil means use indirect buffer to display infile links.
  1344. Activating internal links (from one location in a file to another location
  1345. in the same file) normally just jumps to the location. When the link is
  1346. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1347. is displayed in
  1348. another window. When this option is set, the other window actually displays
  1349. an indirect buffer clone of the current buffer, to avoid any visibility
  1350. changes to the current buffer."
  1351. :group 'org-link-follow
  1352. :type 'boolean)
  1353. (defcustom org-open-non-existing-files nil
  1354. "Non-nil means `org-open-file' will open non-existing files.
  1355. When nil, an error will be generated.
  1356. This variable applies only to external applications because they
  1357. might choke on non-existing files. If the link is to a file that
  1358. will be opened in Emacs, the variable is ignored."
  1359. :group 'org-link-follow
  1360. :type 'boolean)
  1361. (defcustom org-open-directory-means-index-dot-org nil
  1362. "Non-nil means a link to a directory really means to index.org.
  1363. When nil, following a directory link will run dired or open a finder/explorer
  1364. window on that directory."
  1365. :group 'org-link-follow
  1366. :type 'boolean)
  1367. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1368. "Function and arguments to call for following mailto links.
  1369. This is a list with the first element being a Lisp function, and the
  1370. remaining elements being arguments to the function. In string arguments,
  1371. %a will be replaced by the address, and %s will be replaced by the subject
  1372. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1373. :group 'org-link-follow
  1374. :type '(choice
  1375. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1376. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1377. (const :tag "message-mail" (message-mail "%a" "%s"))
  1378. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1379. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1380. "Non-nil means ask for confirmation before executing shell links.
  1381. Shell links can be dangerous: just think about a link
  1382. [[shell:rm -rf ~/*][Google Search]]
  1383. This link would show up in your Org-mode document as \"Google Search\",
  1384. but really it would remove your entire home directory.
  1385. Therefore we advise against setting this variable to nil.
  1386. Just change it to `y-or-n-p' if you want to confirm with a
  1387. single keystroke rather than having to type \"yes\"."
  1388. :group 'org-link-follow
  1389. :type '(choice
  1390. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1391. (const :tag "with y-or-n (faster)" y-or-n-p)
  1392. (const :tag "no confirmation (dangerous)" nil)))
  1393. (put 'org-confirm-shell-link-function
  1394. 'safe-local-variable
  1395. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1396. (defcustom org-confirm-shell-link-not-regexp ""
  1397. "A regexp to skip confirmation for shell links."
  1398. :group 'org-link-follow
  1399. :type 'regexp)
  1400. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1401. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1402. Elisp links can be dangerous: just think about a link
  1403. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1404. This link would show up in your Org-mode document as \"Google Search\",
  1405. but really it would remove your entire home directory.
  1406. Therefore we advise against setting this variable to nil.
  1407. Just change it to `y-or-n-p' if you want to confirm with a
  1408. single keystroke rather than having to type \"yes\"."
  1409. :group 'org-link-follow
  1410. :type '(choice
  1411. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1412. (const :tag "with y-or-n (faster)" y-or-n-p)
  1413. (const :tag "no confirmation (dangerous)" nil)))
  1414. (put 'org-confirm-shell-link-function
  1415. 'safe-local-variable
  1416. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1417. (defcustom org-confirm-elisp-link-not-regexp ""
  1418. "A regexp to skip confirmation for Elisp links."
  1419. :group 'org-link-follow
  1420. :type 'regexp)
  1421. (defconst org-file-apps-defaults-gnu
  1422. '((remote . emacs)
  1423. (system . mailcap)
  1424. (t . mailcap))
  1425. "Default file applications on a UNIX or GNU/Linux system.
  1426. See `org-file-apps'.")
  1427. (defconst org-file-apps-defaults-macosx
  1428. '((remote . emacs)
  1429. (t . "open %s")
  1430. (system . "open %s")
  1431. ("ps.gz" . "gv %s")
  1432. ("eps.gz" . "gv %s")
  1433. ("dvi" . "xdvi %s")
  1434. ("fig" . "xfig %s"))
  1435. "Default file applications on a MacOS X system.
  1436. The system \"open\" is known as a default, but we use X11 applications
  1437. for some files for which the OS does not have a good default.
  1438. See `org-file-apps'.")
  1439. (defconst org-file-apps-defaults-windowsnt
  1440. (list
  1441. '(remote . emacs)
  1442. (cons t
  1443. (list (if (featurep 'xemacs)
  1444. 'mswindows-shell-execute
  1445. 'w32-shell-execute)
  1446. "open" 'file))
  1447. (cons 'system
  1448. (list (if (featurep 'xemacs)
  1449. 'mswindows-shell-execute
  1450. 'w32-shell-execute)
  1451. "open" 'file)))
  1452. "Default file applications on a Windows NT system.
  1453. The system \"open\" is used for most files.
  1454. See `org-file-apps'.")
  1455. (defcustom org-file-apps
  1456. '(
  1457. (auto-mode . emacs)
  1458. ("\\.mm\\'" . default)
  1459. ("\\.x?html?\\'" . default)
  1460. ("\\.pdf\\'" . default)
  1461. )
  1462. "External applications for opening `file:path' items in a document.
  1463. Org-mode uses system defaults for different file types, but
  1464. you can use this variable to set the application for a given file
  1465. extension. The entries in this list are cons cells where the car identifies
  1466. files and the cdr the corresponding command. Possible values for the
  1467. file identifier are
  1468. \"string\" A string as a file identifier can be interpreted in different
  1469. ways, depending on its contents:
  1470. - Alphanumeric characters only:
  1471. Match links with this file extension.
  1472. Example: (\"pdf\" . \"evince %s\")
  1473. to open PDFs with evince.
  1474. - Regular expression: Match links where the
  1475. filename matches the regexp. If you want to
  1476. use groups here, use shy groups.
  1477. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1478. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1479. to open *.html and *.xhtml with firefox.
  1480. - Regular expression which contains (non-shy) groups:
  1481. Match links where the whole link, including \"::\", and
  1482. anything after that, matches the regexp.
  1483. In a custom command string, %1, %2, etc. are replaced with
  1484. the parts of the link that were matched by the groups.
  1485. For backwards compatibility, if a command string is given
  1486. that does not use any of the group matches, this case is
  1487. handled identically to the second one (i.e. match against
  1488. file name only).
  1489. In a custom lisp form, you can access the group matches with
  1490. (match-string n link).
  1491. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1492. to open [[file:document.pdf::5]] with evince at page 5.
  1493. `directory' Matches a directory
  1494. `remote' Matches a remote file, accessible through tramp or efs.
  1495. Remote files most likely should be visited through Emacs
  1496. because external applications cannot handle such paths.
  1497. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1498. so all files Emacs knows how to handle. Using this with
  1499. command `emacs' will open most files in Emacs. Beware that this
  1500. will also open html files inside Emacs, unless you add
  1501. (\"html\" . default) to the list as well.
  1502. t Default for files not matched by any of the other options.
  1503. `system' The system command to open files, like `open' on Windows
  1504. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1505. that will be selected if you call `C-c C-o' with a double
  1506. \\[universal-argument] \\[universal-argument] prefix.
  1507. Possible values for the command are:
  1508. `emacs' The file will be visited by the current Emacs process.
  1509. `default' Use the default application for this file type, which is the
  1510. association for t in the list, most likely in the system-specific
  1511. part.
  1512. This can be used to overrule an unwanted setting in the
  1513. system-specific variable.
  1514. `system' Use the system command for opening files, like \"open\".
  1515. This command is specified by the entry whose car is `system'.
  1516. Most likely, the system-specific version of this variable
  1517. does define this command, but you can overrule/replace it
  1518. here.
  1519. string A command to be executed by a shell; %s will be replaced
  1520. by the path to the file.
  1521. sexp A Lisp form which will be evaluated. The file path will
  1522. be available in the Lisp variable `file'.
  1523. For more examples, see the system specific constants
  1524. `org-file-apps-defaults-macosx'
  1525. `org-file-apps-defaults-windowsnt'
  1526. `org-file-apps-defaults-gnu'."
  1527. :group 'org-link-follow
  1528. :type '(repeat
  1529. (cons (choice :value ""
  1530. (string :tag "Extension")
  1531. (const :tag "System command to open files" system)
  1532. (const :tag "Default for unrecognized files" t)
  1533. (const :tag "Remote file" remote)
  1534. (const :tag "Links to a directory" directory)
  1535. (const :tag "Any files that have Emacs modes"
  1536. auto-mode))
  1537. (choice :value ""
  1538. (const :tag "Visit with Emacs" emacs)
  1539. (const :tag "Use default" default)
  1540. (const :tag "Use the system command" system)
  1541. (string :tag "Command")
  1542. (sexp :tag "Lisp form")))))
  1543. (defgroup org-refile nil
  1544. "Options concerning refiling entries in Org-mode."
  1545. :tag "Org Refile"
  1546. :group 'org)
  1547. (defcustom org-directory "~/org"
  1548. "Directory with org files.
  1549. This is just a default location to look for Org files. There is no need
  1550. at all to put your files into this directory. It is only used in the
  1551. following situations:
  1552. 1. When a remember template specifies a target file that is not an
  1553. absolute path. The path will then be interpreted relative to
  1554. `org-directory'
  1555. 2. When a remember note is filed away in an interactive way (when exiting the
  1556. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1557. with `org-directory' as the default path."
  1558. :group 'org-refile
  1559. :group 'org-remember
  1560. :type 'directory)
  1561. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1562. "Default target for storing notes.
  1563. Used as a fall back file for org-remember.el and org-capture.el, for
  1564. templates that do not specify a target file."
  1565. :group 'org-refile
  1566. :group 'org-remember
  1567. :type '(choice
  1568. (const :tag "Default from remember-data-file" nil)
  1569. file))
  1570. (defcustom org-goto-interface 'outline
  1571. "The default interface to be used for `org-goto'.
  1572. Allowed values are:
  1573. outline The interface shows an outline of the relevant file
  1574. and the correct heading is found by moving through
  1575. the outline or by searching with incremental search.
  1576. outline-path-completion Headlines in the current buffer are offered via
  1577. completion. This is the interface also used by
  1578. the refile command."
  1579. :group 'org-refile
  1580. :type '(choice
  1581. (const :tag "Outline" outline)
  1582. (const :tag "Outline-path-completion" outline-path-completion)))
  1583. (defcustom org-goto-max-level 5
  1584. "Maximum target level when running `org-goto' with refile interface."
  1585. :group 'org-refile
  1586. :type 'integer)
  1587. (defcustom org-reverse-note-order nil
  1588. "Non-nil means store new notes at the beginning of a file or entry.
  1589. When nil, new notes will be filed to the end of a file or entry.
  1590. This can also be a list with cons cells of regular expressions that
  1591. are matched against file names, and values."
  1592. :group 'org-remember
  1593. :group 'org-refile
  1594. :type '(choice
  1595. (const :tag "Reverse always" t)
  1596. (const :tag "Reverse never" nil)
  1597. (repeat :tag "By file name regexp"
  1598. (cons regexp boolean))))
  1599. (defcustom org-log-refile nil
  1600. "Information to record when a task is refiled.
  1601. Possible values are:
  1602. nil Don't add anything
  1603. time Add a time stamp to the task
  1604. note Prompt for a note and add it with template `org-log-note-headings'
  1605. This option can also be set with on a per-file-basis with
  1606. #+STARTUP: nologrefile
  1607. #+STARTUP: logrefile
  1608. #+STARTUP: lognoterefile
  1609. You can have local logging settings for a subtree by setting the LOGGING
  1610. property to one or more of these keywords.
  1611. When bulk-refiling from the agenda, the value `note' is forbidden and
  1612. will temporarily be changed to `time'."
  1613. :group 'org-refile
  1614. :group 'org-progress
  1615. :type '(choice
  1616. (const :tag "No logging" nil)
  1617. (const :tag "Record timestamp" time)
  1618. (const :tag "Record timestamp with note." note)))
  1619. (defcustom org-refile-targets nil
  1620. "Targets for refiling entries with \\[org-refile].
  1621. This is list of cons cells. Each cell contains:
  1622. - a specification of the files to be considered, either a list of files,
  1623. or a symbol whose function or variable value will be used to retrieve
  1624. a file name or a list of file names. If you use `org-agenda-files' for
  1625. that, all agenda files will be scanned for targets. Nil means consider
  1626. headings in the current buffer.
  1627. - A specification of how to find candidate refile targets. This may be
  1628. any of:
  1629. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1630. This tag has to be present in all target headlines, inheritance will
  1631. not be considered.
  1632. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1633. todo keyword.
  1634. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1635. headlines that are refiling targets.
  1636. - a cons cell (:level . N). Any headline of level N is considered a target.
  1637. Note that, when `org-odd-levels-only' is set, level corresponds to
  1638. order in hierarchy, not to the number of stars.
  1639. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1640. Note that, when `org-odd-levels-only' is set, level corresponds to
  1641. order in hierarchy, not to the number of stars.
  1642. You can set the variable `org-refile-target-verify-function' to a function
  1643. to verify each headline found by the simple criteria above.
  1644. When this variable is nil, all top-level headlines in the current buffer
  1645. are used, equivalent to the value `((nil . (:level . 1))'."
  1646. :group 'org-refile
  1647. :type '(repeat
  1648. (cons
  1649. (choice :value org-agenda-files
  1650. (const :tag "All agenda files" org-agenda-files)
  1651. (const :tag "Current buffer" nil)
  1652. (function) (variable) (file))
  1653. (choice :tag "Identify target headline by"
  1654. (cons :tag "Specific tag" (const :value :tag) (string))
  1655. (cons :tag "TODO keyword" (const :value :todo) (string))
  1656. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1657. (cons :tag "Level number" (const :value :level) (integer))
  1658. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1659. (defcustom org-refile-target-verify-function nil
  1660. "Function to verify if the headline at point should be a refile target.
  1661. The function will be called without arguments, with point at the
  1662. beginning of the headline. It should return t and leave point
  1663. where it is if the headline is a valid target for refiling.
  1664. If the target should not be selected, the function must return nil.
  1665. In addition to this, it may move point to a place from where the search
  1666. should be continued. For example, the function may decide that the entire
  1667. subtree of the current entry should be excluded and move point to the end
  1668. of the subtree."
  1669. :group 'org-refile
  1670. :type 'function)
  1671. (defcustom org-refile-use-cache nil
  1672. "Non-nil means cache refile targets to speed up the process.
  1673. The cache for a particular file will be updated automatically when
  1674. the buffer has been killed, or when any of the marker used for flagging
  1675. refile targets no longer points at a live buffer.
  1676. If you have added new entries to a buffer that might themselves be targets,
  1677. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1678. find that easier, `C-u C-u C-u C-c C-w'."
  1679. :group 'org-refile
  1680. :type 'boolean)
  1681. (defcustom org-refile-use-outline-path nil
  1682. "Non-nil means provide refile targets as paths.
  1683. So a level 3 headline will be available as level1/level2/level3.
  1684. When the value is `file', also include the file name (without directory)
  1685. into the path. In this case, you can also stop the completion after
  1686. the file name, to get entries inserted as top level in the file.
  1687. When `full-file-path', include the full file path."
  1688. :group 'org-refile
  1689. :type '(choice
  1690. (const :tag "Not" nil)
  1691. (const :tag "Yes" t)
  1692. (const :tag "Start with file name" file)
  1693. (const :tag "Start with full file path" full-file-path)))
  1694. (defcustom org-outline-path-complete-in-steps t
  1695. "Non-nil means complete the outline path in hierarchical steps.
  1696. When Org-mode uses the refile interface to select an outline path
  1697. \(see variable `org-refile-use-outline-path'), the completion of
  1698. the path can be done is a single go, or if can be done in steps down
  1699. the headline hierarchy. Going in steps is probably the best if you
  1700. do not use a special completion package like `ido' or `icicles'.
  1701. However, when using these packages, going in one step can be very
  1702. fast, while still showing the whole path to the entry."
  1703. :group 'org-refile
  1704. :type 'boolean)
  1705. (defcustom org-refile-allow-creating-parent-nodes nil
  1706. "Non-nil means allow to create new nodes as refile targets.
  1707. New nodes are then created by adding \"/new node name\" to the completion
  1708. of an existing node. When the value of this variable is `confirm',
  1709. new node creation must be confirmed by the user (recommended)
  1710. When nil, the completion must match an existing entry.
  1711. Note that, if the new heading is not seen by the criteria
  1712. listed in `org-refile-targets', multiple instances of the same
  1713. heading would be created by trying again to file under the new
  1714. heading."
  1715. :group 'org-refile
  1716. :type '(choice
  1717. (const :tag "Never" nil)
  1718. (const :tag "Always" t)
  1719. (const :tag "Prompt for confirmation" confirm)))
  1720. (defgroup org-todo nil
  1721. "Options concerning TODO items in Org-mode."
  1722. :tag "Org TODO"
  1723. :group 'org)
  1724. (defgroup org-progress nil
  1725. "Options concerning Progress logging in Org-mode."
  1726. :tag "Org Progress"
  1727. :group 'org-time)
  1728. (defvar org-todo-interpretation-widgets
  1729. '((:tag "Sequence (cycling hits every state)" sequence)
  1730. (:tag "Type (cycling directly to DONE)" type))
  1731. "The available interpretation symbols for customizing `org-todo-keywords'.
  1732. Interested libraries should add to this list.")
  1733. (defcustom org-loop-over-siblings-within-active-region-p nil
  1734. "Shall some commands act upon siblings in the active region?
  1735. The list of commands is:
  1736. - `org-schedule'
  1737. - `org-deadline'
  1738. - `org-archive-subtree'
  1739. - `org-archive-to-archive-sibling'
  1740. - `org-archive-set-tag'
  1741. - `org-toggle-archive-tag'"
  1742. :group 'org-todo
  1743. :group 'org-archive)
  1744. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1745. "List of TODO entry keyword sequences and their interpretation.
  1746. \\<org-mode-map>This is a list of sequences.
  1747. Each sequence starts with a symbol, either `sequence' or `type',
  1748. indicating if the keywords should be interpreted as a sequence of
  1749. action steps, or as different types of TODO items. The first
  1750. keywords are states requiring action - these states will select a headline
  1751. for inclusion into the global TODO list Org-mode produces. If one of
  1752. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1753. signify that no further action is necessary. If \"|\" is not found,
  1754. the last keyword is treated as the only DONE state of the sequence.
  1755. The command \\[org-todo] cycles an entry through these states, and one
  1756. additional state where no keyword is present. For details about this
  1757. cycling, see the manual.
  1758. TODO keywords and interpretation can also be set on a per-file basis with
  1759. the special #+SEQ_TODO and #+TYP_TODO lines.
  1760. Each keyword can optionally specify a character for fast state selection
  1761. \(in combination with the variable `org-use-fast-todo-selection')
  1762. and specifiers for state change logging, using the same syntax
  1763. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1764. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1765. indicates to record a time stamp each time this state is selected.
  1766. Each keyword may also specify if a timestamp or a note should be
  1767. recorded when entering or leaving the state, by adding additional
  1768. characters in the parenthesis after the keyword. This looks like this:
  1769. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1770. record only the time of the state change. With X and Y being either
  1771. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1772. Y when leaving the state if and only if the *target* state does not
  1773. define X. You may omit any of the fast-selection key or X or /Y,
  1774. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1775. For backward compatibility, this variable may also be just a list
  1776. of keywords - in this case the interpretation (sequence or type) will be
  1777. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1778. :group 'org-todo
  1779. :group 'org-keywords
  1780. :type '(choice
  1781. (repeat :tag "Old syntax, just keywords"
  1782. (string :tag "Keyword"))
  1783. (repeat :tag "New syntax"
  1784. (cons
  1785. (choice
  1786. :tag "Interpretation"
  1787. ;;Quick and dirty way to see
  1788. ;;`org-todo-interpretations'. This takes the
  1789. ;;place of item arguments
  1790. :convert-widget
  1791. (lambda (widget)
  1792. (widget-put widget
  1793. :args (mapcar
  1794. #'(lambda (x)
  1795. (widget-convert
  1796. (cons 'const x)))
  1797. org-todo-interpretation-widgets))
  1798. widget))
  1799. (repeat
  1800. (string :tag "Keyword"))))))
  1801. (defvar org-todo-keywords-1 nil
  1802. "All TODO and DONE keywords active in a buffer.")
  1803. (make-variable-buffer-local 'org-todo-keywords-1)
  1804. (defvar org-todo-keywords-for-agenda nil)
  1805. (defvar org-done-keywords-for-agenda nil)
  1806. (defvar org-drawers-for-agenda nil)
  1807. (defvar org-todo-keyword-alist-for-agenda nil)
  1808. (defvar org-tag-alist-for-agenda nil)
  1809. (defvar org-agenda-contributing-files nil)
  1810. (defvar org-not-done-keywords nil)
  1811. (make-variable-buffer-local 'org-not-done-keywords)
  1812. (defvar org-done-keywords nil)
  1813. (make-variable-buffer-local 'org-done-keywords)
  1814. (defvar org-todo-heads nil)
  1815. (make-variable-buffer-local 'org-todo-heads)
  1816. (defvar org-todo-sets nil)
  1817. (make-variable-buffer-local 'org-todo-sets)
  1818. (defvar org-todo-log-states nil)
  1819. (make-variable-buffer-local 'org-todo-log-states)
  1820. (defvar org-todo-kwd-alist nil)
  1821. (make-variable-buffer-local 'org-todo-kwd-alist)
  1822. (defvar org-todo-key-alist nil)
  1823. (make-variable-buffer-local 'org-todo-key-alist)
  1824. (defvar org-todo-key-trigger nil)
  1825. (make-variable-buffer-local 'org-todo-key-trigger)
  1826. (defcustom org-todo-interpretation 'sequence
  1827. "Controls how TODO keywords are interpreted.
  1828. This variable is in principle obsolete and is only used for
  1829. backward compatibility, if the interpretation of todo keywords is
  1830. not given already in `org-todo-keywords'. See that variable for
  1831. more information."
  1832. :group 'org-todo
  1833. :group 'org-keywords
  1834. :type '(choice (const sequence)
  1835. (const type)))
  1836. (defcustom org-use-fast-todo-selection t
  1837. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1838. This variable describes if and under what circumstances the cycling
  1839. mechanism for TODO keywords will be replaced by a single-key, direct
  1840. selection scheme.
  1841. When nil, fast selection is never used.
  1842. When the symbol `prefix', it will be used when `org-todo' is called with
  1843. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1844. in an agenda buffer.
  1845. When t, fast selection is used by default. In this case, the prefix
  1846. argument forces cycling instead.
  1847. In all cases, the special interface is only used if access keys have actually
  1848. been assigned by the user, i.e. if keywords in the configuration are followed
  1849. by a letter in parenthesis, like TODO(t)."
  1850. :group 'org-todo
  1851. :type '(choice
  1852. (const :tag "Never" nil)
  1853. (const :tag "By default" t)
  1854. (const :tag "Only with C-u C-c C-t" prefix)))
  1855. (defcustom org-provide-todo-statistics t
  1856. "Non-nil means update todo statistics after insert and toggle.
  1857. ALL-HEADLINES means update todo statistics by including headlines
  1858. with no TODO keyword as well, counting them as not done.
  1859. A list of TODO keywords means the same, but skip keywords that are
  1860. not in this list.
  1861. When this is set, todo statistics is updated in the parent of the
  1862. current entry each time a todo state is changed."
  1863. :group 'org-todo
  1864. :type '(choice
  1865. (const :tag "Yes, only for TODO entries" t)
  1866. (const :tag "Yes, including all entries" 'all-headlines)
  1867. (repeat :tag "Yes, for TODOs in this list"
  1868. (string :tag "TODO keyword"))
  1869. (other :tag "No TODO statistics" nil)))
  1870. (defcustom org-hierarchical-todo-statistics t
  1871. "Non-nil means TODO statistics covers just direct children.
  1872. When nil, all entries in the subtree are considered.
  1873. This has only an effect if `org-provide-todo-statistics' is set.
  1874. To set this to nil for only a single subtree, use a COOKIE_DATA
  1875. property and include the word \"recursive\" into the value."
  1876. :group 'org-todo
  1877. :type 'boolean)
  1878. (defcustom org-after-todo-state-change-hook nil
  1879. "Hook which is run after the state of a TODO item was changed.
  1880. The new state (a string with a TODO keyword, or nil) is available in the
  1881. Lisp variable `state'."
  1882. :group 'org-todo
  1883. :type 'hook)
  1884. (defvar org-blocker-hook nil
  1885. "Hook for functions that are allowed to block a state change.
  1886. Each function gets as its single argument a property list, see
  1887. `org-trigger-hook' for more information about this list.
  1888. If any of the functions in this hook returns nil, the state change
  1889. is blocked.")
  1890. (defvar org-trigger-hook nil
  1891. "Hook for functions that are triggered by a state change.
  1892. Each function gets as its single argument a property list with at least
  1893. the following elements:
  1894. (:type type-of-change :position pos-at-entry-start
  1895. :from old-state :to new-state)
  1896. Depending on the type, more properties may be present.
  1897. This mechanism is currently implemented for:
  1898. TODO state changes
  1899. ------------------
  1900. :type todo-state-change
  1901. :from previous state (keyword as a string), or nil, or a symbol
  1902. 'todo' or 'done', to indicate the general type of state.
  1903. :to new state, like in :from")
  1904. (defcustom org-enforce-todo-dependencies nil
  1905. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1906. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1907. be blocked if any prior sibling is not yet done.
  1908. Finally, if the parent is blocked because of ordered siblings of its own,
  1909. the child will also be blocked.
  1910. This variable needs to be set before org.el is loaded, and you need to
  1911. restart Emacs after a change to make the change effective. The only way
  1912. to change is while Emacs is running is through the customize interface."
  1913. :set (lambda (var val)
  1914. (set var val)
  1915. (if val
  1916. (add-hook 'org-blocker-hook
  1917. 'org-block-todo-from-children-or-siblings-or-parent)
  1918. (remove-hook 'org-blocker-hook
  1919. 'org-block-todo-from-children-or-siblings-or-parent)))
  1920. :group 'org-todo
  1921. :type 'boolean)
  1922. (defcustom org-enforce-todo-checkbox-dependencies nil
  1923. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1924. When this is nil, checkboxes have no influence on switching TODO states.
  1925. When non-nil, you first need to check off all check boxes before the TODO
  1926. entry can be switched to DONE.
  1927. This variable needs to be set before org.el is loaded, and you need to
  1928. restart Emacs after a change to make the change effective. The only way
  1929. to change is while Emacs is running is through the customize interface."
  1930. :set (lambda (var val)
  1931. (set var val)
  1932. (if val
  1933. (add-hook 'org-blocker-hook
  1934. 'org-block-todo-from-checkboxes)
  1935. (remove-hook 'org-blocker-hook
  1936. 'org-block-todo-from-checkboxes)))
  1937. :group 'org-todo
  1938. :type 'boolean)
  1939. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1940. "Non-nil means inserting a TODO heading is treated as state change.
  1941. So when the command \\[org-insert-todo-heading] is used, state change
  1942. logging will apply if appropriate. When nil, the new TODO item will
  1943. be inserted directly, and no logging will take place."
  1944. :group 'org-todo
  1945. :type 'boolean)
  1946. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1947. "Non-nil means switching TODO states with S-cursor counts as state change.
  1948. This is the default behavior. However, setting this to nil allows a
  1949. convenient way to select a TODO state and bypass any logging associated
  1950. with that."
  1951. :group 'org-todo
  1952. :type 'boolean)
  1953. (defcustom org-todo-state-tags-triggers nil
  1954. "Tag changes that should be triggered by TODO state changes.
  1955. This is a list. Each entry is
  1956. (state-change (tag . flag) .......)
  1957. State-change can be a string with a state, and empty string to indicate the
  1958. state that has no TODO keyword, or it can be one of the symbols `todo'
  1959. or `done', meaning any not-done or done state, respectively."
  1960. :group 'org-todo
  1961. :group 'org-tags
  1962. :type '(repeat
  1963. (cons (choice :tag "When changing to"
  1964. (const :tag "Not-done state" todo)
  1965. (const :tag "Done state" done)
  1966. (string :tag "State"))
  1967. (repeat
  1968. (cons :tag "Tag action"
  1969. (string :tag "Tag")
  1970. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1971. (defcustom org-log-done nil
  1972. "Information to record when a task moves to the DONE state.
  1973. Possible values are:
  1974. nil Don't add anything, just change the keyword
  1975. time Add a time stamp to the task
  1976. note Prompt for a note and add it with template `org-log-note-headings'
  1977. This option can also be set with on a per-file-basis with
  1978. #+STARTUP: nologdone
  1979. #+STARTUP: logdone
  1980. #+STARTUP: lognotedone
  1981. You can have local logging settings for a subtree by setting the LOGGING
  1982. property to one or more of these keywords."
  1983. :group 'org-todo
  1984. :group 'org-progress
  1985. :type '(choice
  1986. (const :tag "No logging" nil)
  1987. (const :tag "Record CLOSED timestamp" time)
  1988. (const :tag "Record CLOSED timestamp with note." note)))
  1989. ;; Normalize old uses of org-log-done.
  1990. (cond
  1991. ((eq org-log-done t) (setq org-log-done 'time))
  1992. ((and (listp org-log-done) (memq 'done org-log-done))
  1993. (setq org-log-done 'note)))
  1994. (defcustom org-log-reschedule nil
  1995. "Information to record when the scheduling date of a tasks is modified.
  1996. Possible values are:
  1997. nil Don't add anything, just change the date
  1998. time Add a time stamp to the task
  1999. note Prompt for a note and add it with template `org-log-note-headings'
  2000. This option can also be set with on a per-file-basis with
  2001. #+STARTUP: nologreschedule
  2002. #+STARTUP: logreschedule
  2003. #+STARTUP: lognotereschedule"
  2004. :group 'org-todo
  2005. :group 'org-progress
  2006. :type '(choice
  2007. (const :tag "No logging" nil)
  2008. (const :tag "Record timestamp" time)
  2009. (const :tag "Record timestamp with note." note)))
  2010. (defcustom org-log-redeadline nil
  2011. "Information to record when the deadline date of a tasks is modified.
  2012. Possible values are:
  2013. nil Don't add anything, just change the date
  2014. time Add a time stamp to the task
  2015. note Prompt for a note and add it with template `org-log-note-headings'
  2016. This option can also be set with on a per-file-basis with
  2017. #+STARTUP: nologredeadline
  2018. #+STARTUP: logredeadline
  2019. #+STARTUP: lognoteredeadline
  2020. You can have local logging settings for a subtree by setting the LOGGING
  2021. property to one or more of these keywords."
  2022. :group 'org-todo
  2023. :group 'org-progress
  2024. :type '(choice
  2025. (const :tag "No logging" nil)
  2026. (const :tag "Record timestamp" time)
  2027. (const :tag "Record timestamp with note." note)))
  2028. (defcustom org-log-note-clock-out nil
  2029. "Non-nil means record a note when clocking out of an item.
  2030. This can also be configured on a per-file basis by adding one of
  2031. the following lines anywhere in the buffer:
  2032. #+STARTUP: lognoteclock-out
  2033. #+STARTUP: nolognoteclock-out"
  2034. :group 'org-todo
  2035. :group 'org-progress
  2036. :type 'boolean)
  2037. (defcustom org-log-done-with-time t
  2038. "Non-nil means the CLOSED time stamp will contain date and time.
  2039. When nil, only the date will be recorded."
  2040. :group 'org-progress
  2041. :type 'boolean)
  2042. (defcustom org-log-note-headings
  2043. '((done . "CLOSING NOTE %t")
  2044. (state . "State %-12s from %-12S %t")
  2045. (note . "Note taken on %t")
  2046. (reschedule . "Rescheduled from %S on %t")
  2047. (delschedule . "Not scheduled, was %S on %t")
  2048. (redeadline . "New deadline from %S on %t")
  2049. (deldeadline . "Removed deadline, was %S on %t")
  2050. (refile . "Refiled on %t")
  2051. (clock-out . ""))
  2052. "Headings for notes added to entries.
  2053. The value is an alist, with the car being a symbol indicating the note
  2054. context, and the cdr is the heading to be used. The heading may also be the
  2055. empty string.
  2056. %t in the heading will be replaced by a time stamp.
  2057. %T will be an active time stamp instead the default inactive one
  2058. %s will be replaced by the new TODO state, in double quotes.
  2059. %S will be replaced by the old TODO state, in double quotes.
  2060. %u will be replaced by the user name.
  2061. %U will be replaced by the full user name.
  2062. In fact, it is not a good idea to change the `state' entry, because
  2063. agenda log mode depends on the format of these entries."
  2064. :group 'org-todo
  2065. :group 'org-progress
  2066. :type '(list :greedy t
  2067. (cons (const :tag "Heading when closing an item" done) string)
  2068. (cons (const :tag
  2069. "Heading when changing todo state (todo sequence only)"
  2070. state) string)
  2071. (cons (const :tag "Heading when just taking a note" note) string)
  2072. (cons (const :tag "Heading when clocking out" clock-out) string)
  2073. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2074. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2075. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2076. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2077. (cons (const :tag "Heading when refiling" refile) string)))
  2078. (unless (assq 'note org-log-note-headings)
  2079. (push '(note . "%t") org-log-note-headings))
  2080. (defcustom org-log-into-drawer nil
  2081. "Non-nil means insert state change notes and time stamps into a drawer.
  2082. When nil, state changes notes will be inserted after the headline and
  2083. any scheduling and clock lines, but not inside a drawer.
  2084. The value of this variable should be the name of the drawer to use.
  2085. LOGBOOK is proposed as the default drawer for this purpose, you can
  2086. also set this to a string to define the drawer of your choice.
  2087. A value of t is also allowed, representing \"LOGBOOK\".
  2088. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2089. will be ignored.
  2090. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2091. a subtree."
  2092. :group 'org-todo
  2093. :group 'org-progress
  2094. :type '(choice
  2095. (const :tag "Not into a drawer" nil)
  2096. (const :tag "LOGBOOK" t)
  2097. (string :tag "Other")))
  2098. (if (fboundp 'defvaralias)
  2099. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2100. (defun org-log-into-drawer ()
  2101. "Return the value of `org-log-into-drawer', but let properties overrule.
  2102. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2103. used instead of the default value."
  2104. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2105. (cond
  2106. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2107. ((equal p "t") "LOGBOOK")
  2108. (t p))))
  2109. (defcustom org-log-state-notes-insert-after-drawers nil
  2110. "Non-nil means insert state change notes after any drawers in entry.
  2111. Only the drawers that *immediately* follow the headline and the
  2112. deadline/scheduled line are skipped.
  2113. When nil, insert notes right after the heading and perhaps the line
  2114. with deadline/scheduling if present.
  2115. This variable will have no effect if `org-log-into-drawer' is
  2116. set."
  2117. :group 'org-todo
  2118. :group 'org-progress
  2119. :type 'boolean)
  2120. (defcustom org-log-states-order-reversed t
  2121. "Non-nil means the latest state note will be directly after heading.
  2122. When nil, the state change notes will be ordered according to time."
  2123. :group 'org-todo
  2124. :group 'org-progress
  2125. :type 'boolean)
  2126. (defcustom org-todo-repeat-to-state nil
  2127. "The TODO state to which a repeater should return the repeating task.
  2128. By default this is the first task in a TODO sequence, or the previous state
  2129. in a TODO_TYP set. But you can specify another task here.
  2130. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2131. :group 'org-todo
  2132. :type '(choice (const :tag "Head of sequence" nil)
  2133. (string :tag "Specific state")))
  2134. (defcustom org-log-repeat 'time
  2135. "Non-nil means record moving through the DONE state when triggering repeat.
  2136. An auto-repeating task is immediately switched back to TODO when
  2137. marked DONE. If you are not logging state changes (by adding \"@\"
  2138. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2139. record a closing note, there will be no record of the task moving
  2140. through DONE. This variable forces taking a note anyway.
  2141. nil Don't force a record
  2142. time Record a time stamp
  2143. note Record a note
  2144. This option can also be set with on a per-file-basis with
  2145. #+STARTUP: logrepeat
  2146. #+STARTUP: lognoterepeat
  2147. #+STARTUP: nologrepeat
  2148. You can have local logging settings for a subtree by setting the LOGGING
  2149. property to one or more of these keywords."
  2150. :group 'org-todo
  2151. :group 'org-progress
  2152. :type '(choice
  2153. (const :tag "Don't force a record" nil)
  2154. (const :tag "Force recording the DONE state" time)
  2155. (const :tag "Force recording a note with the DONE state" note)))
  2156. (defgroup org-priorities nil
  2157. "Priorities in Org-mode."
  2158. :tag "Org Priorities"
  2159. :group 'org-todo)
  2160. (defcustom org-enable-priority-commands t
  2161. "Non-nil means priority commands are active.
  2162. When nil, these commands will be disabled, so that you never accidentally
  2163. set a priority."
  2164. :group 'org-priorities
  2165. :type 'boolean)
  2166. (defcustom org-highest-priority ?A
  2167. "The highest priority of TODO items. A character like ?A, ?B etc.
  2168. Must have a smaller ASCII number than `org-lowest-priority'."
  2169. :group 'org-priorities
  2170. :type 'character)
  2171. (defcustom org-lowest-priority ?C
  2172. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2173. Must have a larger ASCII number than `org-highest-priority'."
  2174. :group 'org-priorities
  2175. :type 'character)
  2176. (defcustom org-default-priority ?B
  2177. "The default priority of TODO items.
  2178. This is the priority an item gets if no explicit priority is given.
  2179. When starting to cycle on an empty priority the first step in the cycle
  2180. depends on `org-priority-start-cycle-with-default'. The resulting first
  2181. step priority must not exceed the range from `org-highest-priority' to
  2182. `org-lowest-priority' which means that `org-default-priority' has to be
  2183. in this range exclusive or inclusive the range boundaries. Else the
  2184. first step refuses to set the default and the second will fall back
  2185. to (depending on the command used) the highest or lowest priority."
  2186. :group 'org-priorities
  2187. :type 'character)
  2188. (defcustom org-priority-start-cycle-with-default t
  2189. "Non-nil means start with default priority when starting to cycle.
  2190. When this is nil, the first step in the cycle will be (depending on the
  2191. command used) one higher or lower than the default priority.
  2192. See also `org-default-priority'."
  2193. :group 'org-priorities
  2194. :type 'boolean)
  2195. (defcustom org-get-priority-function nil
  2196. "Function to extract the priority from a string.
  2197. The string is normally the headline. If this is nil Org computes the
  2198. priority from the priority cookie like [#A] in the headline. It returns
  2199. an integer, increasing by 1000 for each priority level.
  2200. The user can set a different function here, which should take a string
  2201. as an argument and return the numeric priority."
  2202. :group 'org-priorities
  2203. :type 'function)
  2204. (defgroup org-time nil
  2205. "Options concerning time stamps and deadlines in Org-mode."
  2206. :tag "Org Time"
  2207. :group 'org)
  2208. (defcustom org-insert-labeled-timestamps-at-point nil
  2209. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2210. When nil, these labeled time stamps are forces into the second line of an
  2211. entry, just after the headline. When scheduling from the global TODO list,
  2212. the time stamp will always be forced into the second line."
  2213. :group 'org-time
  2214. :type 'boolean)
  2215. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2216. "Formats for `format-time-string' which are used for time stamps.
  2217. It is not recommended to change this constant.")
  2218. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2219. "Number of minutes to round time stamps to.
  2220. These are two values, the first applies when first creating a time stamp.
  2221. The second applies when changing it with the commands `S-up' and `S-down'.
  2222. When changing the time stamp, this means that it will change in steps
  2223. of N minutes, as given by the second value.
  2224. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2225. numbers should be factors of 60, so for example 5, 10, 15.
  2226. When this is larger than 1, you can still force an exact time stamp by using
  2227. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2228. and by using a prefix arg to `S-up/down' to specify the exact number
  2229. of minutes to shift."
  2230. :group 'org-time
  2231. :get #'(lambda (var) ; Make sure both elements are there
  2232. (if (integerp (default-value var))
  2233. (list (default-value var) 5)
  2234. (default-value var)))
  2235. :type '(list
  2236. (integer :tag "when inserting times")
  2237. (integer :tag "when modifying times")))
  2238. ;; Normalize old customizations of this variable.
  2239. (when (integerp org-time-stamp-rounding-minutes)
  2240. (setq org-time-stamp-rounding-minutes
  2241. (list org-time-stamp-rounding-minutes
  2242. org-time-stamp-rounding-minutes)))
  2243. (defcustom org-display-custom-times nil
  2244. "Non-nil means overlay custom formats over all time stamps.
  2245. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2246. To turn this on on a per-file basis, insert anywhere in the file:
  2247. #+STARTUP: customtime"
  2248. :group 'org-time
  2249. :set 'set-default
  2250. :type 'sexp)
  2251. (make-variable-buffer-local 'org-display-custom-times)
  2252. (defcustom org-time-stamp-custom-formats
  2253. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2254. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2255. These are overlayed over the default ISO format if the variable
  2256. `org-display-custom-times' is set. Time like %H:%M should be at the
  2257. end of the second format. The custom formats are also honored by export
  2258. commands, if custom time display is turned on at the time of export."
  2259. :group 'org-time
  2260. :type 'sexp)
  2261. (defun org-time-stamp-format (&optional long inactive)
  2262. "Get the right format for a time string."
  2263. (let ((f (if long (cdr org-time-stamp-formats)
  2264. (car org-time-stamp-formats))))
  2265. (if inactive
  2266. (concat "[" (substring f 1 -1) "]")
  2267. f)))
  2268. (defcustom org-time-clocksum-format "%d:%02d"
  2269. "The format string used when creating CLOCKSUM lines.
  2270. This is also used when org-mode generates a time duration."
  2271. :group 'org-time
  2272. :type 'string)
  2273. (defcustom org-time-clocksum-use-fractional nil
  2274. "If non-nil, \\[org-clock-display] uses fractional times.
  2275. org-mode generates a time duration."
  2276. :group 'org-time
  2277. :type 'boolean)
  2278. (defcustom org-time-clocksum-fractional-format "%.2f"
  2279. "The format string used when creating CLOCKSUM lines, or when
  2280. org-mode generates a time duration."
  2281. :group 'org-time
  2282. :type 'string)
  2283. (defcustom org-deadline-warning-days 14
  2284. "No. of days before expiration during which a deadline becomes active.
  2285. This variable governs the display in sparse trees and in the agenda.
  2286. When 0 or negative, it means use this number (the absolute value of it)
  2287. even if a deadline has a different individual lead time specified.
  2288. Custom commands can set this variable in the options section."
  2289. :group 'org-time
  2290. :group 'org-agenda-daily/weekly
  2291. :type 'integer)
  2292. (defcustom org-read-date-prefer-future t
  2293. "Non-nil means assume future for incomplete date input from user.
  2294. This affects the following situations:
  2295. 1. The user gives a month but not a year.
  2296. For example, if it is April and you enter \"feb 2\", this will be read
  2297. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2298. 2. The user gives a day, but no month.
  2299. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2300. read this as the third of *next* month. However, if you enter \"17\",
  2301. it will be considered as *this* month.
  2302. If you set this variable to the symbol `time', then also the following
  2303. will work:
  2304. 3. If the user gives a time, but no day. If the time is before now,
  2305. to will be interpreted as tomorrow.
  2306. Currently none of this works for ISO week specifications.
  2307. When this option is nil, the current day, month and year will always be
  2308. used as defaults.
  2309. See also `org-agenda-jump-prefer-future'."
  2310. :group 'org-time
  2311. :type '(choice
  2312. (const :tag "Never" nil)
  2313. (const :tag "Check month and day" t)
  2314. (const :tag "Check month, day, and time" time)))
  2315. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2316. "Should the agenda jump command prefer the future for incomplete dates?
  2317. The default is to do the same as configured in `org-read-date-prefer-future'.
  2318. But you can also set a deviating value here.
  2319. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2320. :group 'org-agenda
  2321. :group 'org-time
  2322. :type '(choice
  2323. (const :tag "Use org-read-date-prefer-future"
  2324. org-read-date-prefer-future)
  2325. (const :tag "Never" nil)
  2326. (const :tag "Always" t)))
  2327. (defcustom org-read-date-force-compatible-dates t
  2328. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2329. Depending on the system Emacs is running on, certain dates cannot
  2330. be represented with the type used internally to represent time.
  2331. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2332. correctly. Some systems allow for earlier dates, some for later,
  2333. some for both. One way to find out it to insert any date into an
  2334. Org buffer, putting the cursor on the year and hitting S-up and
  2335. S-down to test the range.
  2336. When this variable is set to t, the date/time prompt will not let
  2337. you specify dates outside the 1970-2037 range, so it is certain that
  2338. these dates will work in whatever version of Emacs you are
  2339. running, and also that you can move a file from one Emacs implementation
  2340. to another. WHenever Org is forcing the year for you, it will display
  2341. a message and beep.
  2342. When this variable is nil, Org will check if the date is
  2343. representable in the specific Emacs implementation you are using.
  2344. If not, it will force a year, usually the current year, and beep
  2345. to remind you. Currently this setting is not recommended because
  2346. the likelihood that you will open your Org files in an Emacs that
  2347. has limited date range is not negligible.
  2348. A workaround for this problem is to use diary sexp dates for time
  2349. stamps outside of this range."
  2350. :group 'org-time
  2351. :type 'boolean)
  2352. (defcustom org-read-date-display-live t
  2353. "Non-nil means display current interpretation of date prompt live.
  2354. This display will be in an overlay, in the minibuffer."
  2355. :group 'org-time
  2356. :type 'boolean)
  2357. (defcustom org-read-date-popup-calendar t
  2358. "Non-nil means pop up a calendar when prompting for a date.
  2359. In the calendar, the date can be selected with mouse-1. However, the
  2360. minibuffer will also be active, and you can simply enter the date as well.
  2361. When nil, only the minibuffer will be available."
  2362. :group 'org-time
  2363. :type 'boolean)
  2364. (if (fboundp 'defvaralias)
  2365. (defvaralias 'org-popup-calendar-for-date-prompt
  2366. 'org-read-date-popup-calendar))
  2367. (defcustom org-read-date-minibuffer-setup-hook nil
  2368. "Hook to be used to set up keys for the date/time interface.
  2369. Add key definitions to `minibuffer-local-map', which will be a temporary
  2370. copy."
  2371. :group 'org-time
  2372. :type 'hook)
  2373. (defcustom org-extend-today-until 0
  2374. "The hour when your day really ends. Must be an integer.
  2375. This has influence for the following applications:
  2376. - When switching the agenda to \"today\". It it is still earlier than
  2377. the time given here, the day recognized as TODAY is actually yesterday.
  2378. - When a date is read from the user and it is still before the time given
  2379. here, the current date and time will be assumed to be yesterday, 23:59.
  2380. Also, timestamps inserted in remember templates follow this rule.
  2381. IMPORTANT: This is a feature whose implementation is and likely will
  2382. remain incomplete. Really, it is only here because past midnight seems to
  2383. be the favorite working time of John Wiegley :-)"
  2384. :group 'org-time
  2385. :type 'integer)
  2386. (defcustom org-edit-timestamp-down-means-later nil
  2387. "Non-nil means S-down will increase the time in a time stamp.
  2388. When nil, S-up will increase."
  2389. :group 'org-time
  2390. :type 'boolean)
  2391. (defcustom org-calendar-follow-timestamp-change t
  2392. "Non-nil means make the calendar window follow timestamp changes.
  2393. When a timestamp is modified and the calendar window is visible, it will be
  2394. moved to the new date."
  2395. :group 'org-time
  2396. :type 'boolean)
  2397. (defgroup org-tags nil
  2398. "Options concerning tags in Org-mode."
  2399. :tag "Org Tags"
  2400. :group 'org)
  2401. (defcustom org-tag-alist nil
  2402. "List of tags allowed in Org-mode files.
  2403. When this list is nil, Org-mode will base TAG input on what is already in the
  2404. buffer.
  2405. The value of this variable is an alist, the car of each entry must be a
  2406. keyword as a string, the cdr may be a character that is used to select
  2407. that tag through the fast-tag-selection interface.
  2408. See the manual for details."
  2409. :group 'org-tags
  2410. :type '(repeat
  2411. (choice
  2412. (cons (string :tag "Tag name")
  2413. (character :tag "Access char"))
  2414. (list :tag "Start radio group"
  2415. (const :startgroup)
  2416. (option (string :tag "Group description")))
  2417. (list :tag "End radio group"
  2418. (const :endgroup)
  2419. (option (string :tag "Group description")))
  2420. (const :tag "New line" (:newline)))))
  2421. (defcustom org-tag-persistent-alist nil
  2422. "List of tags that will always appear in all Org-mode files.
  2423. This is in addition to any in buffer settings or customizations
  2424. of `org-tag-alist'.
  2425. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2426. The value of this variable is an alist, the car of each entry must be a
  2427. keyword as a string, the cdr may be a character that is used to select
  2428. that tag through the fast-tag-selection interface.
  2429. See the manual for details.
  2430. To disable these tags on a per-file basis, insert anywhere in the file:
  2431. #+STARTUP: noptag"
  2432. :group 'org-tags
  2433. :type '(repeat
  2434. (choice
  2435. (cons (string :tag "Tag name")
  2436. (character :tag "Access char"))
  2437. (const :tag "Start radio group" (:startgroup))
  2438. (const :tag "End radio group" (:endgroup))
  2439. (const :tag "New line" (:newline)))))
  2440. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2441. "If non-nil, always offer completion for all tags of all agenda files.
  2442. Instead of customizing this variable directly, you might want to
  2443. set it locally for capture buffers, because there no list of
  2444. tags in that file can be created dynamically (there are none).
  2445. (add-hook 'org-capture-mode-hook
  2446. (lambda ()
  2447. (set (make-local-variable
  2448. 'org-complete-tags-always-offer-all-agenda-tags)
  2449. t)))"
  2450. :group 'org-tags
  2451. :type 'boolean)
  2452. (defvar org-file-tags nil
  2453. "List of tags that can be inherited by all entries in the file.
  2454. The tags will be inherited if the variable `org-use-tag-inheritance'
  2455. says they should be.
  2456. This variable is populated from #+FILETAGS lines.")
  2457. (defcustom org-use-fast-tag-selection 'auto
  2458. "Non-nil means use fast tag selection scheme.
  2459. This is a special interface to select and deselect tags with single keys.
  2460. When nil, fast selection is never used.
  2461. When the symbol `auto', fast selection is used if and only if selection
  2462. characters for tags have been configured, either through the variable
  2463. `org-tag-alist' or through a #+TAGS line in the buffer.
  2464. When t, fast selection is always used and selection keys are assigned
  2465. automatically if necessary."
  2466. :group 'org-tags
  2467. :type '(choice
  2468. (const :tag "Always" t)
  2469. (const :tag "Never" nil)
  2470. (const :tag "When selection characters are configured" 'auto)))
  2471. (defcustom org-fast-tag-selection-single-key nil
  2472. "Non-nil means fast tag selection exits after first change.
  2473. When nil, you have to press RET to exit it.
  2474. During fast tag selection, you can toggle this flag with `C-c'.
  2475. This variable can also have the value `expert'. In this case, the window
  2476. displaying the tags menu is not even shown, until you press C-c again."
  2477. :group 'org-tags
  2478. :type '(choice
  2479. (const :tag "No" nil)
  2480. (const :tag "Yes" t)
  2481. (const :tag "Expert" expert)))
  2482. (defvar org-fast-tag-selection-include-todo nil
  2483. "Non-nil means fast tags selection interface will also offer TODO states.
  2484. This is an undocumented feature, you should not rely on it.")
  2485. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2486. "The column to which tags should be indented in a headline.
  2487. If this number is positive, it specifies the column. If it is negative,
  2488. it means that the tags should be flushright to that column. For example,
  2489. -80 works well for a normal 80 character screen."
  2490. :group 'org-tags
  2491. :type 'integer)
  2492. (defcustom org-auto-align-tags t
  2493. "Non-nil keeps tags aligned when modifying headlines.
  2494. Some operations (i.e. demoting) change the length of a headline and
  2495. therefore shift the tags around. With this option turned on, after
  2496. each such operation the tags are again aligned to `org-tags-column'."
  2497. :group 'org-tags
  2498. :type 'boolean)
  2499. (defcustom org-use-tag-inheritance t
  2500. "Non-nil means tags in levels apply also for sublevels.
  2501. When nil, only the tags directly given in a specific line apply there.
  2502. This may also be a list of tags that should be inherited, or a regexp that
  2503. matches tags that should be inherited. Additional control is possible
  2504. with the variable `org-tags-exclude-from-inheritance' which gives an
  2505. explicit list of tags to be excluded from inheritance., even if the value of
  2506. `org-use-tag-inheritance' would select it for inheritance.
  2507. If this option is t, a match early-on in a tree can lead to a large
  2508. number of matches in the subtree when constructing the agenda or creating
  2509. a sparse tree. If you only want to see the first match in a tree during
  2510. a search, check out the variable `org-tags-match-list-sublevels'."
  2511. :group 'org-tags
  2512. :type '(choice
  2513. (const :tag "Not" nil)
  2514. (const :tag "Always" t)
  2515. (repeat :tag "Specific tags" (string :tag "Tag"))
  2516. (regexp :tag "Tags matched by regexp")))
  2517. (defcustom org-tags-exclude-from-inheritance nil
  2518. "List of tags that should never be inherited.
  2519. This is a way to exclude a few tags from inheritance. For way to do
  2520. the opposite, to actively allow inheritance for selected tags,
  2521. see the variable `org-use-tag-inheritance'."
  2522. :group 'org-tags
  2523. :type '(repeat (string :tag "Tag")))
  2524. (defun org-tag-inherit-p (tag)
  2525. "Check if TAG is one that should be inherited."
  2526. (cond
  2527. ((member tag org-tags-exclude-from-inheritance) nil)
  2528. ((eq org-use-tag-inheritance t) t)
  2529. ((not org-use-tag-inheritance) nil)
  2530. ((stringp org-use-tag-inheritance)
  2531. (string-match org-use-tag-inheritance tag))
  2532. ((listp org-use-tag-inheritance)
  2533. (member tag org-use-tag-inheritance))
  2534. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2535. (defcustom org-tags-match-list-sublevels t
  2536. "Non-nil means list also sublevels of headlines matching a search.
  2537. This variable applies to tags/property searches, and also to stuck
  2538. projects because this search is based on a tags match as well.
  2539. When set to the symbol `indented', sublevels are indented with
  2540. leading dots.
  2541. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2542. the sublevels of a headline matching a tag search often also match
  2543. the same search. Listing all of them can create very long lists.
  2544. Setting this variable to nil causes subtrees of a match to be skipped.
  2545. This variable is semi-obsolete and probably should always be true. It
  2546. is better to limit inheritance to certain tags using the variables
  2547. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2548. :group 'org-tags
  2549. :type '(choice
  2550. (const :tag "No, don't list them" nil)
  2551. (const :tag "Yes, do list them" t)
  2552. (const :tag "List them, indented with leading dots" indented)))
  2553. (defcustom org-tags-sort-function nil
  2554. "When set, tags are sorted using this function as a comparator."
  2555. :group 'org-tags
  2556. :type '(choice
  2557. (const :tag "No sorting" nil)
  2558. (const :tag "Alphabetical" string<)
  2559. (const :tag "Reverse alphabetical" string>)
  2560. (function :tag "Custom function" nil)))
  2561. (defvar org-tags-history nil
  2562. "History of minibuffer reads for tags.")
  2563. (defvar org-last-tags-completion-table nil
  2564. "The last used completion table for tags.")
  2565. (defvar org-after-tags-change-hook nil
  2566. "Hook that is run after the tags in a line have changed.")
  2567. (defgroup org-properties nil
  2568. "Options concerning properties in Org-mode."
  2569. :tag "Org Properties"
  2570. :group 'org)
  2571. (defcustom org-property-format "%-10s %s"
  2572. "How property key/value pairs should be formatted by `indent-line'.
  2573. When `indent-line' hits a property definition, it will format the line
  2574. according to this format, mainly to make sure that the values are
  2575. lined-up with respect to each other."
  2576. :group 'org-properties
  2577. :type 'string)
  2578. (defcustom org-use-property-inheritance nil
  2579. "Non-nil means properties apply also for sublevels.
  2580. This setting is chiefly used during property searches. Turning it on can
  2581. cause significant overhead when doing a search, which is why it is not
  2582. on by default.
  2583. When nil, only the properties directly given in the current entry count.
  2584. When t, every property is inherited. The value may also be a list of
  2585. properties that should have inheritance, or a regular expression matching
  2586. properties that should be inherited.
  2587. However, note that some special properties use inheritance under special
  2588. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2589. and the properties ending in \"_ALL\" when they are used as descriptor
  2590. for valid values of a property.
  2591. Note for programmers:
  2592. When querying an entry with `org-entry-get', you can control if inheritance
  2593. should be used. By default, `org-entry-get' looks only at the local
  2594. properties. You can request inheritance by setting the inherit argument
  2595. to t (to force inheritance) or to `selective' (to respect the setting
  2596. in this variable)."
  2597. :group 'org-properties
  2598. :type '(choice
  2599. (const :tag "Not" nil)
  2600. (const :tag "Always" t)
  2601. (repeat :tag "Specific properties" (string :tag "Property"))
  2602. (regexp :tag "Properties matched by regexp")))
  2603. (defun org-property-inherit-p (property)
  2604. "Check if PROPERTY is one that should be inherited."
  2605. (cond
  2606. ((eq org-use-property-inheritance t) t)
  2607. ((not org-use-property-inheritance) nil)
  2608. ((stringp org-use-property-inheritance)
  2609. (string-match org-use-property-inheritance property))
  2610. ((listp org-use-property-inheritance)
  2611. (member property org-use-property-inheritance))
  2612. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2613. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2614. "The default column format, if no other format has been defined.
  2615. This variable can be set on the per-file basis by inserting a line
  2616. #+COLUMNS: %25ITEM ....."
  2617. :group 'org-properties
  2618. :type 'string)
  2619. (defcustom org-columns-ellipses ".."
  2620. "The ellipses to be used when a field in column view is truncated.
  2621. When this is the empty string, as many characters as possible are shown,
  2622. but then there will be no visual indication that the field has been truncated.
  2623. When this is a string of length N, the last N characters of a truncated
  2624. field are replaced by this string. If the column is narrower than the
  2625. ellipses string, only part of the ellipses string will be shown."
  2626. :group 'org-properties
  2627. :type 'string)
  2628. (defcustom org-columns-modify-value-for-display-function nil
  2629. "Function that modifies values for display in column view.
  2630. For example, it can be used to cut out a certain part from a time stamp.
  2631. The function must take 2 arguments:
  2632. column-title The title of the column (*not* the property name)
  2633. value The value that should be modified.
  2634. The function should return the value that should be displayed,
  2635. or nil if the normal value should be used."
  2636. :group 'org-properties
  2637. :type 'function)
  2638. (defcustom org-effort-property "Effort"
  2639. "The property that is being used to keep track of effort estimates.
  2640. Effort estimates given in this property need to have the format H:MM."
  2641. :group 'org-properties
  2642. :group 'org-progress
  2643. :type '(string :tag "Property"))
  2644. (defconst org-global-properties-fixed
  2645. '(("VISIBILITY_ALL" . "folded children content all")
  2646. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2647. "List of property/value pairs that can be inherited by any entry.
  2648. These are fixed values, for the preset properties. The user variable
  2649. that can be used to add to this list is `org-global-properties'.
  2650. The entries in this list are cons cells where the car is a property
  2651. name and cdr is a string with the value. If the value represents
  2652. multiple items like an \"_ALL\" property, separate the items by
  2653. spaces.")
  2654. (defcustom org-global-properties nil
  2655. "List of property/value pairs that can be inherited by any entry.
  2656. This list will be combined with the constant `org-global-properties-fixed'.
  2657. The entries in this list are cons cells where the car is a property
  2658. name and cdr is a string with the value.
  2659. You can set buffer-local values for the same purpose in the variable
  2660. `org-file-properties' this by adding lines like
  2661. #+PROPERTY: NAME VALUE"
  2662. :group 'org-properties
  2663. :type '(repeat
  2664. (cons (string :tag "Property")
  2665. (string :tag "Value"))))
  2666. (defvar org-file-properties nil
  2667. "List of property/value pairs that can be inherited by any entry.
  2668. Valid for the current buffer.
  2669. This variable is populated from #+PROPERTY lines.")
  2670. (make-variable-buffer-local 'org-file-properties)
  2671. (defgroup org-agenda nil
  2672. "Options concerning agenda views in Org-mode."
  2673. :tag "Org Agenda"
  2674. :group 'org)
  2675. (defvar org-category nil
  2676. "Variable used by org files to set a category for agenda display.
  2677. Such files should use a file variable to set it, for example
  2678. # -*- mode: org; org-category: \"ELisp\"
  2679. or contain a special line
  2680. #+CATEGORY: ELisp
  2681. If the file does not specify a category, then file's base name
  2682. is used instead.")
  2683. (make-variable-buffer-local 'org-category)
  2684. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2685. (defcustom org-agenda-files nil
  2686. "The files to be used for agenda display.
  2687. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2688. \\[org-remove-file]. You can also use customize to edit the list.
  2689. If an entry is a directory, all files in that directory that are matched by
  2690. `org-agenda-file-regexp' will be part of the file list.
  2691. If the value of the variable is not a list but a single file name, then
  2692. the list of agenda files is actually stored and maintained in that file, one
  2693. agenda file per line. In this file paths can be given relative to
  2694. `org-directory'. Tilde expansion and environment variable substitution
  2695. are also made."
  2696. :group 'org-agenda
  2697. :type '(choice
  2698. (repeat :tag "List of files and directories" file)
  2699. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2700. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2701. "Regular expression to match files for `org-agenda-files'.
  2702. If any element in the list in that variable contains a directory instead
  2703. of a normal file, all files in that directory that are matched by this
  2704. regular expression will be included."
  2705. :group 'org-agenda
  2706. :type 'regexp)
  2707. (defcustom org-agenda-text-search-extra-files nil
  2708. "List of extra files to be searched by text search commands.
  2709. These files will be search in addition to the agenda files by the
  2710. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2711. Note that these files will only be searched for text search commands,
  2712. not for the other agenda views like todo lists, tag searches or the weekly
  2713. agenda. This variable is intended to list notes and possibly archive files
  2714. that should also be searched by these two commands.
  2715. In fact, if the first element in the list is the symbol `agenda-archives',
  2716. than all archive files of all agenda files will be added to the search
  2717. scope."
  2718. :group 'org-agenda
  2719. :type '(set :greedy t
  2720. (const :tag "Agenda Archives" agenda-archives)
  2721. (repeat :inline t (file))))
  2722. (if (fboundp 'defvaralias)
  2723. (defvaralias 'org-agenda-multi-occur-extra-files
  2724. 'org-agenda-text-search-extra-files))
  2725. (defcustom org-agenda-skip-unavailable-files nil
  2726. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2727. A nil value means to remove them, after a query, from the list."
  2728. :group 'org-agenda
  2729. :type 'boolean)
  2730. (defcustom org-calendar-to-agenda-key [?c]
  2731. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2732. The command `org-calendar-goto-agenda' will be bound to this key. The
  2733. default is the character `c' because then `c' can be used to switch back and
  2734. forth between agenda and calendar."
  2735. :group 'org-agenda
  2736. :type 'sexp)
  2737. (defcustom org-calendar-agenda-action-key [?k]
  2738. "The key to be installed in `calendar-mode-map' for agenda-action.
  2739. The command `org-agenda-action' will be bound to this key. The
  2740. default is the character `k' because we use the same key in the agenda."
  2741. :group 'org-agenda
  2742. :type 'sexp)
  2743. (defcustom org-calendar-insert-diary-entry-key [?i]
  2744. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2745. This option is irrelevant until `org-agenda-diary-file' has been configured
  2746. to point to an Org-mode file. When that is the case, the command
  2747. `org-agenda-diary-entry' will be bound to the key given here, by default
  2748. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2749. if you want to continue doing this, you need to change this to a different
  2750. key."
  2751. :group 'org-agenda
  2752. :type 'sexp)
  2753. (defcustom org-agenda-diary-file 'diary-file
  2754. "File to which to add new entries with the `i' key in agenda and calendar.
  2755. When this is the symbol `diary-file', the functionality in the Emacs
  2756. calendar will be used to add entries to the `diary-file'. But when this
  2757. points to a file, `org-agenda-diary-entry' will be used instead."
  2758. :group 'org-agenda
  2759. :type '(choice
  2760. (const :tag "The standard Emacs diary file" diary-file)
  2761. (file :tag "Special Org file diary entries")))
  2762. (eval-after-load "calendar"
  2763. '(progn
  2764. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2765. 'org-calendar-goto-agenda)
  2766. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2767. 'org-agenda-action)
  2768. (add-hook 'calendar-mode-hook
  2769. (lambda ()
  2770. (unless (eq org-agenda-diary-file 'diary-file)
  2771. (define-key calendar-mode-map
  2772. org-calendar-insert-diary-entry-key
  2773. 'org-agenda-diary-entry))))))
  2774. (defgroup org-latex nil
  2775. "Options for embedding LaTeX code into Org-mode."
  2776. :tag "Org LaTeX"
  2777. :group 'org)
  2778. (defcustom org-format-latex-options
  2779. '(:foreground default :background default :scale 1.0
  2780. :html-foreground "Black" :html-background "Transparent"
  2781. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2782. "Options for creating images from LaTeX fragments.
  2783. This is a property list with the following properties:
  2784. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2785. `default' means use the foreground of the default face.
  2786. :background the background color, or \"Transparent\".
  2787. `default' means use the background of the default face.
  2788. :scale a scaling factor for the size of the images, to get more pixels
  2789. :html-foreground, :html-background, :html-scale
  2790. the same numbers for HTML export.
  2791. :matchers a list indicating which matchers should be used to
  2792. find LaTeX fragments. Valid members of this list are:
  2793. \"begin\" find environments
  2794. \"$1\" find single characters surrounded by $.$
  2795. \"$\" find math expressions surrounded by $...$
  2796. \"$$\" find math expressions surrounded by $$....$$
  2797. \"\\(\" find math expressions surrounded by \\(...\\)
  2798. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2799. :group 'org-latex
  2800. :type 'plist)
  2801. (defcustom org-format-latex-signal-error t
  2802. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2803. When nil, just push out a message."
  2804. :group 'org-latex
  2805. :type 'boolean)
  2806. (defcustom org-format-latex-header "\\documentclass{article}
  2807. \\usepackage[usenames]{color}
  2808. \\usepackage{amsmath}
  2809. \\usepackage[mathscr]{eucal}
  2810. \\pagestyle{empty} % do not remove
  2811. \[PACKAGES]
  2812. \[DEFAULT-PACKAGES]
  2813. % The settings below are copied from fullpage.sty
  2814. \\setlength{\\textwidth}{\\paperwidth}
  2815. \\addtolength{\\textwidth}{-3cm}
  2816. \\setlength{\\oddsidemargin}{1.5cm}
  2817. \\addtolength{\\oddsidemargin}{-2.54cm}
  2818. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2819. \\setlength{\\textheight}{\\paperheight}
  2820. \\addtolength{\\textheight}{-\\headheight}
  2821. \\addtolength{\\textheight}{-\\headsep}
  2822. \\addtolength{\\textheight}{-\\footskip}
  2823. \\addtolength{\\textheight}{-3cm}
  2824. \\setlength{\\topmargin}{1.5cm}
  2825. \\addtolength{\\topmargin}{-2.54cm}"
  2826. "The document header used for processing LaTeX fragments.
  2827. It is imperative that this header make sure that no page number
  2828. appears on the page. The package defined in the variables
  2829. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2830. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2831. will be appended."
  2832. :group 'org-latex
  2833. :type 'string)
  2834. (defvar org-format-latex-header-extra nil)
  2835. (defun org-set-packages-alist (var val)
  2836. "Set the packages alist and make sure it has 3 elements per entry."
  2837. (set var (mapcar (lambda (x)
  2838. (if (and (consp x) (= (length x) 2))
  2839. (list (car x) (nth 1 x) t)
  2840. x))
  2841. val)))
  2842. (defun org-get-packages-alist (var)
  2843. "Get the packages alist and make sure it has 3 elements per entry."
  2844. (mapcar (lambda (x)
  2845. (if (and (consp x) (= (length x) 2))
  2846. (list (car x) (nth 1 x) t)
  2847. x))
  2848. (default-value var)))
  2849. ;; The following variables are defined here because is it also used
  2850. ;; when formatting latex fragments. Originally it was part of the
  2851. ;; LaTeX exporter, which is why the name includes "export".
  2852. (defcustom org-export-latex-default-packages-alist
  2853. '(("AUTO" "inputenc" t)
  2854. ("T1" "fontenc" t)
  2855. ("" "fixltx2e" nil)
  2856. ("" "graphicx" t)
  2857. ("" "longtable" nil)
  2858. ("" "float" nil)
  2859. ("" "wrapfig" nil)
  2860. ("" "soul" t)
  2861. ("" "textcomp" t)
  2862. ("" "marvosym" t)
  2863. ("" "wasysym" t)
  2864. ("" "latexsym" t)
  2865. ("" "amssymb" t)
  2866. ("" "hyperref" nil)
  2867. "\\tolerance=1000"
  2868. )
  2869. "Alist of default packages to be inserted in the header.
  2870. Change this only if one of the packages here causes an incompatibility
  2871. with another package you are using.
  2872. The packages in this list are needed by one part or another of Org-mode
  2873. to function properly.
  2874. - inputenc, fontenc: for basic font and character selection
  2875. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2876. for interpreting the entities in `org-entities'. You can skip some of these
  2877. packages if you don't use any of the symbols in it.
  2878. - graphicx: for including images
  2879. - float, wrapfig: for figure placement
  2880. - longtable: for long tables
  2881. - hyperref: for cross references
  2882. Therefore you should not modify this variable unless you know what you
  2883. are doing. The one reason to change it anyway is that you might be loading
  2884. some other package that conflicts with one of the default packages.
  2885. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2886. If SNIPPET-FLAG is t, the package also needs to be included when
  2887. compiling LaTeX snippets into images for inclusion into HTML."
  2888. :group 'org-export-latex
  2889. :set 'org-set-packages-alist
  2890. :get 'org-get-packages-alist
  2891. :type '(repeat
  2892. (choice
  2893. (list :tag "options/package pair"
  2894. (string :tag "options")
  2895. (string :tag "package")
  2896. (boolean :tag "Snippet"))
  2897. (string :tag "A line of LaTeX"))))
  2898. (defcustom org-export-latex-packages-alist nil
  2899. "Alist of packages to be inserted in every LaTeX header.
  2900. These will be inserted after `org-export-latex-default-packages-alist'.
  2901. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2902. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2903. turning LaTeX snippets into images for inclusion into HTML.
  2904. Make sure that you only list packages here which:
  2905. - you want in every file
  2906. - do not conflict with the default packages in
  2907. `org-export-latex-default-packages-alist'
  2908. - do not conflict with the setup in `org-format-latex-header'."
  2909. :group 'org-export-latex
  2910. :set 'org-set-packages-alist
  2911. :get 'org-get-packages-alist
  2912. :type '(repeat
  2913. (choice
  2914. (list :tag "options/package pair"
  2915. (string :tag "options")
  2916. (string :tag "package")
  2917. (boolean :tag "Snippet"))
  2918. (string :tag "A line of LaTeX"))))
  2919. (defgroup org-appearance nil
  2920. "Settings for Org-mode appearance."
  2921. :tag "Org Appearance"
  2922. :group 'org)
  2923. (defcustom org-level-color-stars-only nil
  2924. "Non-nil means fontify only the stars in each headline.
  2925. When nil, the entire headline is fontified.
  2926. Changing it requires restart of `font-lock-mode' to become effective
  2927. also in regions already fontified."
  2928. :group 'org-appearance
  2929. :type 'boolean)
  2930. (defcustom org-hide-leading-stars nil
  2931. "Non-nil means hide the first N-1 stars in a headline.
  2932. This works by using the face `org-hide' for these stars. This
  2933. face is white for a light background, and black for a dark
  2934. background. You may have to customize the face `org-hide' to
  2935. make this work.
  2936. Changing it requires restart of `font-lock-mode' to become effective
  2937. also in regions already fontified.
  2938. You may also set this on a per-file basis by adding one of the following
  2939. lines to the buffer:
  2940. #+STARTUP: hidestars
  2941. #+STARTUP: showstars"
  2942. :group 'org-appearance
  2943. :type 'boolean)
  2944. (defcustom org-hidden-keywords nil
  2945. "List of symbols corresponding to keywords to be hidden the org buffer.
  2946. For example, a value '(title) for this list will make the document's title
  2947. appear in the buffer without the initial #+TITLE: keyword."
  2948. :group 'org-appearance
  2949. :type '(set (const :tag "#+AUTHOR" author)
  2950. (const :tag "#+DATE" date)
  2951. (const :tag "#+EMAIL" email)
  2952. (const :tag "#+TITLE" title)))
  2953. (defcustom org-fontify-done-headline nil
  2954. "Non-nil means change the face of a headline if it is marked DONE.
  2955. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2956. When this is non-nil, the headline after the keyword is set to the
  2957. `org-headline-done' as an additional indication."
  2958. :group 'org-appearance
  2959. :type 'boolean)
  2960. (defcustom org-fontify-emphasized-text t
  2961. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2962. Changing this variable requires a restart of Emacs to take effect."
  2963. :group 'org-appearance
  2964. :type 'boolean)
  2965. (defcustom org-fontify-whole-heading-line nil
  2966. "Non-nil means fontify the whole line for headings.
  2967. This is useful when setting a background color for the
  2968. org-level-* faces."
  2969. :group 'org-appearance
  2970. :type 'boolean)
  2971. (defcustom org-highlight-latex-fragments-and-specials nil
  2972. "Non-nil means fontify what is treated specially by the exporters."
  2973. :group 'org-appearance
  2974. :type 'boolean)
  2975. (defcustom org-hide-emphasis-markers nil
  2976. "Non-nil mean font-lock should hide the emphasis marker characters."
  2977. :group 'org-appearance
  2978. :type 'boolean)
  2979. (defcustom org-pretty-entities nil
  2980. "Non-nil means show entities as UTF8 characters.
  2981. When nil, the \\name form remains in the buffer."
  2982. :group 'org-appearance
  2983. :type 'boolean)
  2984. (defcustom org-pretty-entities-include-sub-superscripts t
  2985. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2986. :group 'org-appearance
  2987. :type 'boolean)
  2988. (defvar org-emph-re nil
  2989. "Regular expression for matching emphasis.
  2990. After a match, the match groups contain these elements:
  2991. 0 The match of the full regular expression, including the characters
  2992. before and after the proper match
  2993. 1 The character before the proper match, or empty at beginning of line
  2994. 2 The proper match, including the leading and trailing markers
  2995. 3 The leading marker like * or /, indicating the type of highlighting
  2996. 4 The text between the emphasis markers, not including the markers
  2997. 5 The character after the match, empty at the end of a line")
  2998. (defvar org-verbatim-re nil
  2999. "Regular expression for matching verbatim text.")
  3000. (defvar org-emphasis-regexp-components) ; defined just below
  3001. (defvar org-emphasis-alist) ; defined just below
  3002. (defun org-set-emph-re (var val)
  3003. "Set variable and compute the emphasis regular expression."
  3004. (set var val)
  3005. (when (and (boundp 'org-emphasis-alist)
  3006. (boundp 'org-emphasis-regexp-components)
  3007. org-emphasis-alist org-emphasis-regexp-components)
  3008. (let* ((e org-emphasis-regexp-components)
  3009. (pre (car e))
  3010. (post (nth 1 e))
  3011. (border (nth 2 e))
  3012. (body (nth 3 e))
  3013. (nl (nth 4 e))
  3014. (body1 (concat body "*?"))
  3015. (markers (mapconcat 'car org-emphasis-alist ""))
  3016. (vmarkers (mapconcat
  3017. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3018. org-emphasis-alist "")))
  3019. ;; make sure special characters appear at the right position in the class
  3020. (if (string-match "\\^" markers)
  3021. (setq markers (concat (replace-match "" t t markers) "^")))
  3022. (if (string-match "-" markers)
  3023. (setq markers (concat (replace-match "" t t markers) "-")))
  3024. (if (string-match "\\^" vmarkers)
  3025. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3026. (if (string-match "-" vmarkers)
  3027. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3028. (if (> nl 0)
  3029. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3030. (int-to-string nl) "\\}")))
  3031. ;; Make the regexp
  3032. (setq org-emph-re
  3033. (concat "\\([" pre "]\\|^\\)"
  3034. "\\("
  3035. "\\([" markers "]\\)"
  3036. "\\("
  3037. "[^" border "]\\|"
  3038. "[^" border "]"
  3039. body1
  3040. "[^" border "]"
  3041. "\\)"
  3042. "\\3\\)"
  3043. "\\([" post "]\\|$\\)"))
  3044. (setq org-verbatim-re
  3045. (concat "\\([" pre "]\\|^\\)"
  3046. "\\("
  3047. "\\([" vmarkers "]\\)"
  3048. "\\("
  3049. "[^" border "]\\|"
  3050. "[^" border "]"
  3051. body1
  3052. "[^" border "]"
  3053. "\\)"
  3054. "\\3\\)"
  3055. "\\([" post "]\\|$\\)")))))
  3056. (defcustom org-emphasis-regexp-components
  3057. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3058. "Components used to build the regular expression for emphasis.
  3059. This is a list with five entries. Terminology: In an emphasis string
  3060. like \" *strong word* \", we call the initial space PREMATCH, the final
  3061. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3062. and \"trong wor\" is the body. The different components in this variable
  3063. specify what is allowed/forbidden in each part:
  3064. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3065. post Chars allowed as postmatch. End of line will be allowed too.
  3066. border The chars *forbidden* as border characters.
  3067. body-regexp A regexp like \".\" to match a body character. Don't use
  3068. non-shy groups here, and don't allow newline here.
  3069. newline The maximum number of newlines allowed in an emphasis exp.
  3070. Use customize to modify this, or restart Emacs after changing it."
  3071. :group 'org-appearance
  3072. :set 'org-set-emph-re
  3073. :type '(list
  3074. (sexp :tag "Allowed chars in pre ")
  3075. (sexp :tag "Allowed chars in post ")
  3076. (sexp :tag "Forbidden chars in border ")
  3077. (sexp :tag "Regexp for body ")
  3078. (integer :tag "number of newlines allowed")
  3079. (option (boolean :tag "Please ignore this button"))))
  3080. (defcustom org-emphasis-alist
  3081. `(("*" bold "<b>" "</b>")
  3082. ("/" italic "<i>" "</i>")
  3083. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3084. ("=" org-code "<code>" "</code>" verbatim)
  3085. ("~" org-verbatim "<code>" "</code>" verbatim)
  3086. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3087. "<del>" "</del>")
  3088. )
  3089. "Special syntax for emphasized text.
  3090. Text starting and ending with a special character will be emphasized, for
  3091. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3092. characters, the face to be used by font-lock for highlighting in Org-mode
  3093. Emacs buffers, and the HTML tags to be used for this.
  3094. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3095. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3096. Use customize to modify this, or restart Emacs after changing it."
  3097. :group 'org-appearance
  3098. :set 'org-set-emph-re
  3099. :type '(repeat
  3100. (list
  3101. (string :tag "Marker character")
  3102. (choice
  3103. (face :tag "Font-lock-face")
  3104. (plist :tag "Face property list"))
  3105. (string :tag "HTML start tag")
  3106. (string :tag "HTML end tag")
  3107. (option (const verbatim)))))
  3108. (defvar org-protecting-blocks
  3109. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3110. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3111. This is needed for font-lock setup.")
  3112. ;;; Miscellaneous options
  3113. (defgroup org-completion nil
  3114. "Completion in Org-mode."
  3115. :tag "Org Completion"
  3116. :group 'org)
  3117. (defcustom org-completion-use-ido nil
  3118. "Non-nil means use ido completion wherever possible.
  3119. Note that `ido-mode' must be active for this variable to be relevant.
  3120. If you decide to turn this variable on, you might well want to turn off
  3121. `org-outline-path-complete-in-steps'.
  3122. See also `org-completion-use-iswitchb'."
  3123. :group 'org-completion
  3124. :type 'boolean)
  3125. (defcustom org-completion-use-iswitchb nil
  3126. "Non-nil means use iswitchb completion wherever possible.
  3127. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3128. If you decide to turn this variable on, you might well want to turn off
  3129. `org-outline-path-complete-in-steps'.
  3130. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3131. :group 'org-completion
  3132. :type 'boolean)
  3133. (defcustom org-completion-fallback-command 'hippie-expand
  3134. "The expansion command called by \\[pcomplete] in normal context.
  3135. Normal means, no org-mode-specific context."
  3136. :group 'org-completion
  3137. :type 'function)
  3138. ;;; Functions and variables from their packages
  3139. ;; Declared here to avoid compiler warnings
  3140. ;; XEmacs only
  3141. (defvar outline-mode-menu-heading)
  3142. (defvar outline-mode-menu-show)
  3143. (defvar outline-mode-menu-hide)
  3144. (defvar zmacs-regions) ; XEmacs regions
  3145. ;; Emacs only
  3146. (defvar mark-active)
  3147. ;; Various packages
  3148. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3149. (declare-function calendar-forward-day "cal-move" (arg))
  3150. (declare-function calendar-goto-date "cal-move" (date))
  3151. (declare-function calendar-goto-today "cal-move" ())
  3152. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3153. (defvar calc-embedded-close-formula)
  3154. (defvar calc-embedded-open-formula)
  3155. (declare-function cdlatex-tab "ext:cdlatex" ())
  3156. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3157. (defvar font-lock-unfontify-region-function)
  3158. (declare-function iswitchb-read-buffer "iswitchb"
  3159. (prompt &optional default require-match start matches-set))
  3160. (defvar iswitchb-temp-buflist)
  3161. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3162. (defvar org-agenda-tags-todo-honor-ignore-options)
  3163. (declare-function org-agenda-skip "org-agenda" ())
  3164. (declare-function
  3165. org-format-agenda-item "org-agenda"
  3166. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3167. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3168. (declare-function org-agenda-change-all-lines "org-agenda"
  3169. (newhead hdmarker &optional fixface just-this))
  3170. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3171. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3172. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3173. (beg end))
  3174. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3175. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3176. "org-agenda" (&optional end))
  3177. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3178. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3179. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3180. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3181. (declare-function org-indent-mode "org-indent" (&optional arg))
  3182. (declare-function parse-time-string "parse-time" (string))
  3183. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3184. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3185. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3186. (defvar remember-data-file)
  3187. (defvar texmathp-why)
  3188. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3189. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3190. (defvar w3m-current-url)
  3191. (defvar w3m-current-title)
  3192. (defvar org-latex-regexps)
  3193. ;;; Autoload and prepare some org modules
  3194. ;; Some table stuff that needs to be defined here, because it is used
  3195. ;; by the functions setting up org-mode or checking for table context.
  3196. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3197. "Detect an org-type or table-type table.")
  3198. (defconst org-table-line-regexp "^[ \t]*|"
  3199. "Detect an org-type table line.")
  3200. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3201. "Detect an org-type table line.")
  3202. (defconst org-table-hline-regexp "^[ \t]*|-"
  3203. "Detect an org-type table hline.")
  3204. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3205. "Detect a table-type table hline.")
  3206. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3207. "Detect the first line outside a table when searching from within it.
  3208. This works for both table types.")
  3209. ;; Autoload the functions in org-table.el that are needed by functions here.
  3210. (eval-and-compile
  3211. (org-autoload "org-table"
  3212. '(org-table-align org-table-begin org-table-blank-field
  3213. org-table-convert org-table-convert-region org-table-copy-down
  3214. org-table-copy-region org-table-create
  3215. org-table-create-or-convert-from-region
  3216. org-table-create-with-table.el org-table-current-dline
  3217. org-table-cut-region org-table-delete-column org-table-edit-field
  3218. org-table-edit-formulas org-table-end org-table-eval-formula
  3219. org-table-export org-table-field-info
  3220. org-table-get-stored-formulas org-table-goto-column
  3221. org-table-hline-and-move org-table-import org-table-insert-column
  3222. org-table-insert-hline org-table-insert-row org-table-iterate
  3223. org-table-justify-field-maybe org-table-kill-row
  3224. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3225. org-table-move-column org-table-move-column-left
  3226. org-table-move-column-right org-table-move-row
  3227. org-table-move-row-down org-table-move-row-up
  3228. org-table-next-field org-table-next-row org-table-paste-rectangle
  3229. org-table-previous-field org-table-recalculate
  3230. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3231. org-table-toggle-coordinate-overlays
  3232. org-table-toggle-formula-debugger org-table-wrap-region
  3233. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3234. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3235. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3236. (defun org-at-table-p (&optional table-type)
  3237. "Return t if the cursor is inside an org-type table.
  3238. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3239. (if org-enable-table-editor
  3240. (save-excursion
  3241. (beginning-of-line 1)
  3242. (looking-at (if table-type org-table-any-line-regexp
  3243. org-table-line-regexp)))
  3244. nil))
  3245. (defsubst org-table-p () (org-at-table-p))
  3246. (defun org-at-table.el-p ()
  3247. "Return t if and only if we are at a table.el table."
  3248. (and (org-at-table-p 'any)
  3249. (save-excursion
  3250. (goto-char (org-table-begin 'any))
  3251. (looking-at org-table1-hline-regexp))))
  3252. (defun org-table-recognize-table.el ()
  3253. "If there is a table.el table nearby, recognize it and move into it."
  3254. (if org-table-tab-recognizes-table.el
  3255. (if (org-at-table.el-p)
  3256. (progn
  3257. (beginning-of-line 1)
  3258. (if (looking-at org-table-dataline-regexp)
  3259. nil
  3260. (if (looking-at org-table1-hline-regexp)
  3261. (progn
  3262. (beginning-of-line 2)
  3263. (if (looking-at org-table-any-border-regexp)
  3264. (beginning-of-line -1)))))
  3265. (if (re-search-forward "|" (org-table-end t) t)
  3266. (progn
  3267. (require 'table)
  3268. (if (table--at-cell-p (point))
  3269. t
  3270. (message "recognizing table.el table...")
  3271. (table-recognize-table)
  3272. (message "recognizing table.el table...done")))
  3273. (error "This should not happen"))
  3274. t)
  3275. nil)
  3276. nil))
  3277. (defun org-at-table-hline-p ()
  3278. "Return t if the cursor is inside a hline in a table."
  3279. (if org-enable-table-editor
  3280. (save-excursion
  3281. (beginning-of-line 1)
  3282. (looking-at org-table-hline-regexp))
  3283. nil))
  3284. (defvar org-table-clean-did-remove-column nil)
  3285. (defun org-table-map-tables (function &optional quietly)
  3286. "Apply FUNCTION to the start of all tables in the buffer."
  3287. (save-excursion
  3288. (save-restriction
  3289. (widen)
  3290. (goto-char (point-min))
  3291. (while (re-search-forward org-table-any-line-regexp nil t)
  3292. (unless quietly
  3293. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3294. (beginning-of-line 1)
  3295. (when (looking-at org-table-line-regexp)
  3296. (save-excursion (funcall function))
  3297. (or (looking-at org-table-line-regexp)
  3298. (forward-char 1)))
  3299. (re-search-forward org-table-any-border-regexp nil 1))))
  3300. (unless quietly (message "Mapping tables: done")))
  3301. ;; Declare and autoload functions from org-exp.el & Co
  3302. (declare-function org-default-export-plist "org-exp")
  3303. (declare-function org-infile-export-plist "org-exp")
  3304. (declare-function org-get-current-options "org-exp")
  3305. (eval-and-compile
  3306. (org-autoload "org-exp"
  3307. '(org-export org-export-visible
  3308. org-insert-export-options-template
  3309. org-table-clean-before-export))
  3310. (org-autoload "org-ascii"
  3311. '(org-export-as-ascii org-export-ascii-preprocess
  3312. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3313. org-export-region-as-ascii))
  3314. (org-autoload "org-latex"
  3315. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3316. org-replace-region-by-latex org-export-region-as-latex
  3317. org-export-as-latex org-export-as-pdf
  3318. org-export-as-pdf-and-open))
  3319. (org-autoload "org-html"
  3320. '(org-export-as-html-and-open
  3321. org-export-as-html-batch org-export-as-html-to-buffer
  3322. org-replace-region-by-html org-export-region-as-html
  3323. org-export-as-html))
  3324. (org-autoload "org-docbook"
  3325. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3326. org-replace-region-by-docbook org-export-region-as-docbook
  3327. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3328. org-export-as-docbook))
  3329. (org-autoload "org-icalendar"
  3330. '(org-export-icalendar-this-file
  3331. org-export-icalendar-all-agenda-files
  3332. org-export-icalendar-combine-agenda-files))
  3333. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3334. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3335. ;; Declare and autoload functions from org-agenda.el
  3336. (eval-and-compile
  3337. (org-autoload "org-agenda"
  3338. '(org-agenda org-agenda-list org-search-view
  3339. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3340. org-diary org-agenda-to-appt
  3341. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3342. ;; Autoload org-remember
  3343. (eval-and-compile
  3344. (org-autoload "org-remember"
  3345. '(org-remember-insinuate org-remember-annotation
  3346. org-remember-apply-template org-remember org-remember-handler)))
  3347. (eval-and-compile
  3348. (org-autoload "org-capture"
  3349. '(org-capture org-capture-insert-template-here
  3350. org-capture-import-remember-templates)))
  3351. ;; Autoload org-clock.el
  3352. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3353. (beg end))
  3354. (declare-function org-clock-update-mode-line "org-clock" ())
  3355. (declare-function org-resolve-clocks "org-clock"
  3356. (&optional also-non-dangling-p prompt last-valid))
  3357. (defvar org-clock-start-time)
  3358. (defvar org-clock-marker (make-marker)
  3359. "Marker recording the last clock-in.")
  3360. (defvar org-clock-hd-marker (make-marker)
  3361. "Marker recording the last clock-in, but the headline position.")
  3362. (defvar org-clock-heading ""
  3363. "The heading of the current clock entry.")
  3364. (defun org-clock-is-active ()
  3365. "Return non-nil if clock is currently running.
  3366. The return value is actually the clock marker."
  3367. (marker-buffer org-clock-marker))
  3368. (eval-and-compile
  3369. (org-autoload
  3370. "org-clock"
  3371. '(org-clock-in org-clock-out org-clock-cancel
  3372. org-clock-goto org-clock-sum org-clock-display
  3373. org-clock-remove-overlays org-clock-report
  3374. org-clocktable-shift org-dblock-write:clocktable
  3375. org-get-clocktable org-resolve-clocks)))
  3376. (defun org-clock-update-time-maybe ()
  3377. "If this is a CLOCK line, update it and return t.
  3378. Otherwise, return nil."
  3379. (interactive)
  3380. (save-excursion
  3381. (beginning-of-line 1)
  3382. (skip-chars-forward " \t")
  3383. (when (looking-at org-clock-string)
  3384. (let ((re (concat "[ \t]*" org-clock-string
  3385. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3386. "\\([ \t]*=>.*\\)?\\)?"))
  3387. ts te h m s neg)
  3388. (cond
  3389. ((not (looking-at re))
  3390. nil)
  3391. ((not (match-end 2))
  3392. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3393. (> org-clock-marker (point))
  3394. (<= org-clock-marker (point-at-eol)))
  3395. ;; The clock is running here
  3396. (setq org-clock-start-time
  3397. (apply 'encode-time
  3398. (org-parse-time-string (match-string 1))))
  3399. (org-clock-update-mode-line)))
  3400. (t
  3401. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3402. (end-of-line 1)
  3403. (setq ts (match-string 1)
  3404. te (match-string 3))
  3405. (setq s (- (org-float-time
  3406. (apply 'encode-time (org-parse-time-string te)))
  3407. (org-float-time
  3408. (apply 'encode-time (org-parse-time-string ts))))
  3409. neg (< s 0)
  3410. s (abs s)
  3411. h (floor (/ s 3600))
  3412. s (- s (* 3600 h))
  3413. m (floor (/ s 60))
  3414. s (- s (* 60 s)))
  3415. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3416. t))))))
  3417. (defun org-check-running-clock ()
  3418. "Check if the current buffer contains the running clock.
  3419. If yes, offer to stop it and to save the buffer with the changes."
  3420. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3421. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3422. (buffer-name))))
  3423. (org-clock-out)
  3424. (when (y-or-n-p "Save changed buffer?")
  3425. (save-buffer))))
  3426. (defun org-clocktable-try-shift (dir n)
  3427. "Check if this line starts a clock table, if yes, shift the time block."
  3428. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3429. (org-clocktable-shift dir n)))
  3430. ;; Autoload org-timer.el
  3431. (eval-and-compile
  3432. (org-autoload
  3433. "org-timer"
  3434. '(org-timer-start org-timer org-timer-item
  3435. org-timer-change-times-in-region
  3436. org-timer-set-timer
  3437. org-timer-reset-timers
  3438. org-timer-show-remaining-time)))
  3439. ;; Autoload org-feed.el
  3440. (eval-and-compile
  3441. (org-autoload
  3442. "org-feed"
  3443. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3444. ;; Autoload org-indent.el
  3445. ;; Define the variable already here, to make sure we have it.
  3446. (defvar org-indent-mode nil
  3447. "Non-nil if Org-Indent mode is enabled.
  3448. Use the command `org-indent-mode' to change this variable.")
  3449. (eval-and-compile
  3450. (org-autoload
  3451. "org-indent"
  3452. '(org-indent-mode)))
  3453. ;; Autoload org-mobile.el
  3454. (eval-and-compile
  3455. (org-autoload
  3456. "org-mobile"
  3457. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3458. ;; Autoload archiving code
  3459. ;; The stuff that is needed for cycling and tags has to be defined here.
  3460. (defgroup org-archive nil
  3461. "Options concerning archiving in Org-mode."
  3462. :tag "Org Archive"
  3463. :group 'org-structure)
  3464. (defcustom org-archive-location "%s_archive::"
  3465. "The location where subtrees should be archived.
  3466. The value of this variable is a string, consisting of two parts,
  3467. separated by a double-colon. The first part is a filename and
  3468. the second part is a headline.
  3469. When the filename is omitted, archiving happens in the same file.
  3470. %s in the filename will be replaced by the current file
  3471. name (without the directory part). Archiving to a different file
  3472. is useful to keep archived entries from contributing to the
  3473. Org-mode Agenda.
  3474. The archived entries will be filed as subtrees of the specified
  3475. headline. When the headline is omitted, the subtrees are simply
  3476. filed away at the end of the file, as top-level entries. Also in
  3477. the heading you can use %s to represent the file name, this can be
  3478. useful when using the same archive for a number of different files.
  3479. Here are a few examples:
  3480. \"%s_archive::\"
  3481. If the current file is Projects.org, archive in file
  3482. Projects.org_archive, as top-level trees. This is the default.
  3483. \"::* Archived Tasks\"
  3484. Archive in the current file, under the top-level headline
  3485. \"* Archived Tasks\".
  3486. \"~/org/archive.org::\"
  3487. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3488. \"~/org/archive.org::From %s\"
  3489. Archive in file ~/org/archive.org (absolute path), under headlines
  3490. \"From FILENAME\" where file name is the current file name.
  3491. \"basement::** Finished Tasks\"
  3492. Archive in file ./basement (relative path), as level 3 trees
  3493. below the level 2 heading \"** Finished Tasks\".
  3494. You may set this option on a per-file basis by adding to the buffer a
  3495. line like
  3496. #+ARCHIVE: basement::** Finished Tasks
  3497. You may also define it locally for a subtree by setting an ARCHIVE property
  3498. in the entry. If such a property is found in an entry, or anywhere up
  3499. the hierarchy, it will be used."
  3500. :group 'org-archive
  3501. :type 'string)
  3502. (defcustom org-archive-tag "ARCHIVE"
  3503. "The tag that marks a subtree as archived.
  3504. An archived subtree does not open during visibility cycling, and does
  3505. not contribute to the agenda listings.
  3506. After changing this, font-lock must be restarted in the relevant buffers to
  3507. get the proper fontification."
  3508. :group 'org-archive
  3509. :group 'org-keywords
  3510. :type 'string)
  3511. (defcustom org-agenda-skip-archived-trees t
  3512. "Non-nil means the agenda will skip any items located in archived trees.
  3513. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3514. variable is no longer recommended, you should leave it at the value t.
  3515. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3516. :group 'org-archive
  3517. :group 'org-agenda-skip
  3518. :type 'boolean)
  3519. (defcustom org-columns-skip-archived-trees t
  3520. "Non-nil means ignore archived trees when creating column view."
  3521. :group 'org-archive
  3522. :group 'org-properties
  3523. :type 'boolean)
  3524. (defcustom org-cycle-open-archived-trees nil
  3525. "Non-nil means `org-cycle' will open archived trees.
  3526. An archived tree is a tree marked with the tag ARCHIVE.
  3527. When nil, archived trees will stay folded. You can still open them with
  3528. normal outline commands like `show-all', but not with the cycling commands."
  3529. :group 'org-archive
  3530. :group 'org-cycle
  3531. :type 'boolean)
  3532. (defcustom org-sparse-tree-open-archived-trees nil
  3533. "Non-nil means sparse tree construction shows matches in archived trees.
  3534. When nil, matches in these trees are highlighted, but the trees are kept in
  3535. collapsed state."
  3536. :group 'org-archive
  3537. :group 'org-sparse-trees
  3538. :type 'boolean)
  3539. (defun org-cycle-hide-archived-subtrees (state)
  3540. "Re-hide all archived subtrees after a visibility state change."
  3541. (when (and (not org-cycle-open-archived-trees)
  3542. (not (memq state '(overview folded))))
  3543. (save-excursion
  3544. (let* ((globalp (memq state '(contents all)))
  3545. (beg (if globalp (point-min) (point)))
  3546. (end (if globalp (point-max) (org-end-of-subtree t))))
  3547. (org-hide-archived-subtrees beg end)
  3548. (goto-char beg)
  3549. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3550. (message "%s" (substitute-command-keys
  3551. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3552. (defun org-force-cycle-archived ()
  3553. "Cycle subtree even if it is archived."
  3554. (interactive)
  3555. (setq this-command 'org-cycle)
  3556. (let ((org-cycle-open-archived-trees t))
  3557. (call-interactively 'org-cycle)))
  3558. (defun org-hide-archived-subtrees (beg end)
  3559. "Re-hide all archived subtrees after a visibility state change."
  3560. (save-excursion
  3561. (let* ((re (concat ":" org-archive-tag ":")))
  3562. (goto-char beg)
  3563. (while (re-search-forward re end t)
  3564. (when (org-on-heading-p)
  3565. (org-flag-subtree t)
  3566. (org-end-of-subtree t))))))
  3567. (defun org-flag-subtree (flag)
  3568. (save-excursion
  3569. (org-back-to-heading t)
  3570. (outline-end-of-heading)
  3571. (outline-flag-region (point)
  3572. (progn (org-end-of-subtree t) (point))
  3573. flag)))
  3574. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3575. (eval-and-compile
  3576. (org-autoload "org-archive"
  3577. '(org-add-archive-files org-archive-subtree
  3578. org-archive-to-archive-sibling org-toggle-archive-tag
  3579. org-archive-subtree-default
  3580. org-archive-subtree-default-with-confirmation)))
  3581. ;; Autoload Column View Code
  3582. (declare-function org-columns-number-to-string "org-colview")
  3583. (declare-function org-columns-get-format-and-top-level "org-colview")
  3584. (declare-function org-columns-compute "org-colview")
  3585. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3586. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3587. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3588. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3589. ;; Autoload ID code
  3590. (declare-function org-id-store-link "org-id")
  3591. (declare-function org-id-locations-load "org-id")
  3592. (declare-function org-id-locations-save "org-id")
  3593. (defvar org-id-track-globally)
  3594. (org-autoload "org-id"
  3595. '(org-id-get-create org-id-new org-id-copy org-id-get
  3596. org-id-get-with-outline-path-completion
  3597. org-id-get-with-outline-drilling org-id-store-link
  3598. org-id-goto org-id-find org-id-store-link))
  3599. ;; Autoload Plotting Code
  3600. (org-autoload "org-plot"
  3601. '(org-plot/gnuplot))
  3602. ;;; Variables for pre-computed regular expressions, all buffer local
  3603. (defvar org-drawer-regexp nil
  3604. "Matches first line of a hidden block.")
  3605. (make-variable-buffer-local 'org-drawer-regexp)
  3606. (defvar org-todo-regexp nil
  3607. "Matches any of the TODO state keywords.")
  3608. (make-variable-buffer-local 'org-todo-regexp)
  3609. (defvar org-not-done-regexp nil
  3610. "Matches any of the TODO state keywords except the last one.")
  3611. (make-variable-buffer-local 'org-not-done-regexp)
  3612. (defvar org-not-done-heading-regexp nil
  3613. "Matches a TODO headline that is not done.")
  3614. (make-variable-buffer-local 'org-not-done-regexp)
  3615. (defvar org-todo-line-regexp nil
  3616. "Matches a headline and puts TODO state into group 2 if present.")
  3617. (make-variable-buffer-local 'org-todo-line-regexp)
  3618. (defvar org-complex-heading-regexp nil
  3619. "Matches a headline and puts everything into groups:
  3620. group 1: the stars
  3621. group 2: The todo keyword, maybe
  3622. group 3: Priority cookie
  3623. group 4: True headline
  3624. group 5: Tags")
  3625. (make-variable-buffer-local 'org-complex-heading-regexp)
  3626. (defvar org-complex-heading-regexp-format nil
  3627. "Printf format to make regexp to match an exact headline.
  3628. This regexp will match the headline of any node which hase the exact
  3629. headline text that is put into the format, but may have any TODO state,
  3630. priority and tags.")
  3631. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3632. (defvar org-todo-line-tags-regexp nil
  3633. "Matches a headline and puts TODO state into group 2 if present.
  3634. Also put tags into group 4 if tags are present.")
  3635. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3636. (defvar org-nl-done-regexp nil
  3637. "Matches newline followed by a headline with the DONE keyword.")
  3638. (make-variable-buffer-local 'org-nl-done-regexp)
  3639. (defvar org-looking-at-done-regexp nil
  3640. "Matches the DONE keyword a point.")
  3641. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3642. (defvar org-ds-keyword-length 12
  3643. "Maximum length of the Deadline and SCHEDULED keywords.")
  3644. (make-variable-buffer-local 'org-ds-keyword-length)
  3645. (defvar org-deadline-regexp nil
  3646. "Matches the DEADLINE keyword.")
  3647. (make-variable-buffer-local 'org-deadline-regexp)
  3648. (defvar org-deadline-time-regexp nil
  3649. "Matches the DEADLINE keyword together with a time stamp.")
  3650. (make-variable-buffer-local 'org-deadline-time-regexp)
  3651. (defvar org-deadline-line-regexp nil
  3652. "Matches the DEADLINE keyword and the rest of the line.")
  3653. (make-variable-buffer-local 'org-deadline-line-regexp)
  3654. (defvar org-scheduled-regexp nil
  3655. "Matches the SCHEDULED keyword.")
  3656. (make-variable-buffer-local 'org-scheduled-regexp)
  3657. (defvar org-scheduled-time-regexp nil
  3658. "Matches the SCHEDULED keyword together with a time stamp.")
  3659. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3660. (defvar org-closed-time-regexp nil
  3661. "Matches the CLOSED keyword together with a time stamp.")
  3662. (make-variable-buffer-local 'org-closed-time-regexp)
  3663. (defvar org-keyword-time-regexp nil
  3664. "Matches any of the 4 keywords, together with the time stamp.")
  3665. (make-variable-buffer-local 'org-keyword-time-regexp)
  3666. (defvar org-keyword-time-not-clock-regexp nil
  3667. "Matches any of the 3 keywords, together with the time stamp.")
  3668. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3669. (defvar org-maybe-keyword-time-regexp nil
  3670. "Matches a timestamp, possibly preceded by a keyword.")
  3671. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3672. (defvar org-planning-or-clock-line-re nil
  3673. "Matches a line with planning or clock info.")
  3674. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3675. (defvar org-all-time-keywords nil
  3676. "List of time keywords.")
  3677. (make-variable-buffer-local 'org-all-time-keywords)
  3678. (defconst org-plain-time-of-day-regexp
  3679. (concat
  3680. "\\(\\<[012]?[0-9]"
  3681. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3682. "\\(--?"
  3683. "\\(\\<[012]?[0-9]"
  3684. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3685. "\\)?")
  3686. "Regular expression to match a plain time or time range.
  3687. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3688. groups carry important information:
  3689. 0 the full match
  3690. 1 the first time, range or not
  3691. 8 the second time, if it is a range.")
  3692. (defconst org-plain-time-extension-regexp
  3693. (concat
  3694. "\\(\\<[012]?[0-9]"
  3695. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3696. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3697. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3698. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3699. groups carry important information:
  3700. 0 the full match
  3701. 7 hours of duration
  3702. 9 minutes of duration")
  3703. (defconst org-stamp-time-of-day-regexp
  3704. (concat
  3705. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3706. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3707. "\\(--?"
  3708. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3709. "Regular expression to match a timestamp time or time range.
  3710. After a match, the following groups carry important information:
  3711. 0 the full match
  3712. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3713. 2 the first time, range or not
  3714. 4 the second time, if it is a range.")
  3715. (defconst org-startup-options
  3716. '(("fold" org-startup-folded t)
  3717. ("overview" org-startup-folded t)
  3718. ("nofold" org-startup-folded nil)
  3719. ("showall" org-startup-folded nil)
  3720. ("showeverything" org-startup-folded showeverything)
  3721. ("content" org-startup-folded content)
  3722. ("indent" org-startup-indented t)
  3723. ("noindent" org-startup-indented nil)
  3724. ("hidestars" org-hide-leading-stars t)
  3725. ("showstars" org-hide-leading-stars nil)
  3726. ("odd" org-odd-levels-only t)
  3727. ("oddeven" org-odd-levels-only nil)
  3728. ("align" org-startup-align-all-tables t)
  3729. ("noalign" org-startup-align-all-tables nil)
  3730. ("inlineimages" org-startup-with-inline-images t)
  3731. ("noinlineimages" org-startup-with-inline-images nil)
  3732. ("customtime" org-display-custom-times t)
  3733. ("logdone" org-log-done time)
  3734. ("lognotedone" org-log-done note)
  3735. ("nologdone" org-log-done nil)
  3736. ("lognoteclock-out" org-log-note-clock-out t)
  3737. ("nolognoteclock-out" org-log-note-clock-out nil)
  3738. ("logrepeat" org-log-repeat state)
  3739. ("lognoterepeat" org-log-repeat note)
  3740. ("nologrepeat" org-log-repeat nil)
  3741. ("logreschedule" org-log-reschedule time)
  3742. ("lognotereschedule" org-log-reschedule note)
  3743. ("nologreschedule" org-log-reschedule nil)
  3744. ("logredeadline" org-log-redeadline time)
  3745. ("lognoteredeadline" org-log-redeadline note)
  3746. ("nologredeadline" org-log-redeadline nil)
  3747. ("logrefile" org-log-refile time)
  3748. ("lognoterefile" org-log-refile note)
  3749. ("nologrefile" org-log-refile nil)
  3750. ("fninline" org-footnote-define-inline t)
  3751. ("nofninline" org-footnote-define-inline nil)
  3752. ("fnlocal" org-footnote-section nil)
  3753. ("fnauto" org-footnote-auto-label t)
  3754. ("fnprompt" org-footnote-auto-label nil)
  3755. ("fnconfirm" org-footnote-auto-label confirm)
  3756. ("fnplain" org-footnote-auto-label plain)
  3757. ("fnadjust" org-footnote-auto-adjust t)
  3758. ("nofnadjust" org-footnote-auto-adjust nil)
  3759. ("constcgs" constants-unit-system cgs)
  3760. ("constSI" constants-unit-system SI)
  3761. ("noptag" org-tag-persistent-alist nil)
  3762. ("hideblocks" org-hide-block-startup t)
  3763. ("nohideblocks" org-hide-block-startup nil)
  3764. ("beamer" org-startup-with-beamer-mode t)
  3765. ("entitiespretty" org-pretty-entities t)
  3766. ("entitiesplain" org-pretty-entities nil))
  3767. "Variable associated with STARTUP options for org-mode.
  3768. Each element is a list of three items: The startup options as written
  3769. in the #+STARTUP line, the corresponding variable, and the value to
  3770. set this variable to if the option is found. An optional forth element PUSH
  3771. means to push this value onto the list in the variable.")
  3772. (defun org-set-regexps-and-options ()
  3773. "Precompute regular expressions for current buffer."
  3774. (when (org-mode-p)
  3775. (org-set-local 'org-todo-kwd-alist nil)
  3776. (org-set-local 'org-todo-key-alist nil)
  3777. (org-set-local 'org-todo-key-trigger nil)
  3778. (org-set-local 'org-todo-keywords-1 nil)
  3779. (org-set-local 'org-done-keywords nil)
  3780. (org-set-local 'org-todo-heads nil)
  3781. (org-set-local 'org-todo-sets nil)
  3782. (org-set-local 'org-todo-log-states nil)
  3783. (org-set-local 'org-file-properties nil)
  3784. (org-set-local 'org-file-tags nil)
  3785. (let ((re (org-make-options-regexp
  3786. '("CATEGORY" "TODO" "COLUMNS"
  3787. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3788. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3789. "OPTIONS")
  3790. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3791. (splitre "[ \t]+")
  3792. (scripts org-use-sub-superscripts)
  3793. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3794. tail sep kws1 prio props ftags drawers beamer-p
  3795. ext-setup-or-nil setup-contents (start 0))
  3796. (save-excursion
  3797. (save-restriction
  3798. (widen)
  3799. (goto-char (point-min))
  3800. (while (or (and ext-setup-or-nil
  3801. (string-match re ext-setup-or-nil start)
  3802. (setq start (match-end 0)))
  3803. (and (setq ext-setup-or-nil nil start 0)
  3804. (re-search-forward re nil t)))
  3805. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3806. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3807. (if (stringp value) (setq value (org-trim value)))
  3808. (cond
  3809. ((equal key "CATEGORY")
  3810. (setq cat value))
  3811. ((member key '("SEQ_TODO" "TODO"))
  3812. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3813. ((equal key "TYP_TODO")
  3814. (push (cons 'type (org-split-string value splitre)) kwds))
  3815. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3816. ;; general TODO-like setup
  3817. (push (cons (intern (downcase (match-string 1 key)))
  3818. (org-split-string value splitre)) kwds))
  3819. ((equal key "TAGS")
  3820. (setq tags (append tags (if tags '("\\n") nil)
  3821. (org-split-string value splitre))))
  3822. ((equal key "COLUMNS")
  3823. (org-set-local 'org-columns-default-format value))
  3824. ((equal key "LINK")
  3825. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3826. (push (cons (match-string 1 value)
  3827. (org-trim (match-string 2 value)))
  3828. links)))
  3829. ((equal key "PRIORITIES")
  3830. (setq prio (org-split-string value " +")))
  3831. ((equal key "PROPERTY")
  3832. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3833. (push (cons (match-string 1 value) (match-string 2 value))
  3834. props)))
  3835. ((equal key "FILETAGS")
  3836. (when (string-match "\\S-" value)
  3837. (setq ftags
  3838. (append
  3839. ftags
  3840. (apply 'append
  3841. (mapcar (lambda (x) (org-split-string x ":"))
  3842. (org-split-string value)))))))
  3843. ((equal key "DRAWERS")
  3844. (setq drawers (org-split-string value splitre)))
  3845. ((equal key "CONSTANTS")
  3846. (setq const (append const (org-split-string value splitre))))
  3847. ((equal key "STARTUP")
  3848. (let ((opts (org-split-string value splitre))
  3849. l var val)
  3850. (while (setq l (pop opts))
  3851. (when (setq l (assoc l org-startup-options))
  3852. (setq var (nth 1 l) val (nth 2 l))
  3853. (if (not (nth 3 l))
  3854. (set (make-local-variable var) val)
  3855. (if (not (listp (symbol-value var)))
  3856. (set (make-local-variable var) nil))
  3857. (set (make-local-variable var) (symbol-value var))
  3858. (add-to-list var val))))))
  3859. ((equal key "ARCHIVE")
  3860. (setq arch value)
  3861. (remove-text-properties 0 (length arch)
  3862. '(face t fontified t) arch))
  3863. ((equal key "LATEX_CLASS")
  3864. (setq beamer-p (equal value "beamer")))
  3865. ((equal key "OPTIONS")
  3866. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3867. (setq scripts (read (match-string 2 value)))))
  3868. ((equal key "SETUPFILE")
  3869. (setq setup-contents (org-file-contents
  3870. (expand-file-name
  3871. (org-remove-double-quotes value))
  3872. 'noerror))
  3873. (if (not ext-setup-or-nil)
  3874. (setq ext-setup-or-nil setup-contents start 0)
  3875. (setq ext-setup-or-nil
  3876. (concat (substring ext-setup-or-nil 0 start)
  3877. "\n" setup-contents "\n"
  3878. (substring ext-setup-or-nil start)))))
  3879. ))))
  3880. (org-set-local 'org-use-sub-superscripts scripts)
  3881. (when cat
  3882. (org-set-local 'org-category (intern cat))
  3883. (push (cons "CATEGORY" cat) props))
  3884. (when prio
  3885. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3886. (setq prio (mapcar 'string-to-char prio))
  3887. (org-set-local 'org-highest-priority (nth 0 prio))
  3888. (org-set-local 'org-lowest-priority (nth 1 prio))
  3889. (org-set-local 'org-default-priority (nth 2 prio)))
  3890. (and props (org-set-local 'org-file-properties (nreverse props)))
  3891. (and ftags (org-set-local 'org-file-tags
  3892. (mapcar 'org-add-prop-inherited ftags)))
  3893. (and drawers (org-set-local 'org-drawers drawers))
  3894. (and arch (org-set-local 'org-archive-location arch))
  3895. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3896. ;; Process the TODO keywords
  3897. (unless kwds
  3898. ;; Use the global values as if they had been given locally.
  3899. (setq kwds (default-value 'org-todo-keywords))
  3900. (if (stringp (car kwds))
  3901. (setq kwds (list (cons org-todo-interpretation
  3902. (default-value 'org-todo-keywords)))))
  3903. (setq kwds (reverse kwds)))
  3904. (setq kwds (nreverse kwds))
  3905. (let (inter kws kw)
  3906. (while (setq kws (pop kwds))
  3907. (let ((kws (or
  3908. (run-hook-with-args-until-success
  3909. 'org-todo-setup-filter-hook kws)
  3910. kws)))
  3911. (setq inter (pop kws) sep (member "|" kws)
  3912. kws0 (delete "|" (copy-sequence kws))
  3913. kwsa nil
  3914. kws1 (mapcar
  3915. (lambda (x)
  3916. ;; 1 2
  3917. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3918. (progn
  3919. (setq kw (match-string 1 x)
  3920. key (and (match-end 2) (match-string 2 x))
  3921. log (org-extract-log-state-settings x))
  3922. (push (cons kw (and key (string-to-char key))) kwsa)
  3923. (and log (push log org-todo-log-states))
  3924. kw)
  3925. (error "Invalid TODO keyword %s" x)))
  3926. kws0)
  3927. kwsa (if kwsa (append '((:startgroup))
  3928. (nreverse kwsa)
  3929. '((:endgroup))))
  3930. hw (car kws1)
  3931. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3932. tail (list inter hw (car dws) (org-last dws))))
  3933. (add-to-list 'org-todo-heads hw 'append)
  3934. (push kws1 org-todo-sets)
  3935. (setq org-done-keywords (append org-done-keywords dws nil))
  3936. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3937. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3938. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3939. (setq org-todo-sets (nreverse org-todo-sets)
  3940. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3941. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3942. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3943. ;; Process the constants
  3944. (when const
  3945. (let (e cst)
  3946. (while (setq e (pop const))
  3947. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3948. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3949. (setq org-table-formula-constants-local cst)))
  3950. ;; Process the tags.
  3951. (when tags
  3952. (let (e tgs)
  3953. (while (setq e (pop tags))
  3954. (cond
  3955. ((equal e "{") (push '(:startgroup) tgs))
  3956. ((equal e "}") (push '(:endgroup) tgs))
  3957. ((equal e "\\n") (push '(:newline) tgs))
  3958. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  3959. (push (cons (match-string 1 e)
  3960. (string-to-char (match-string 2 e)))
  3961. tgs))
  3962. (t (push (list e) tgs))))
  3963. (org-set-local 'org-tag-alist nil)
  3964. (while (setq e (pop tgs))
  3965. (or (and (stringp (car e))
  3966. (assoc (car e) org-tag-alist))
  3967. (push e org-tag-alist)))))
  3968. ;; Compute the regular expressions and other local variables
  3969. (if (not org-done-keywords)
  3970. (setq org-done-keywords (and org-todo-keywords-1
  3971. (list (org-last org-todo-keywords-1)))))
  3972. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3973. (length org-scheduled-string)
  3974. (length org-clock-string)
  3975. (length org-closed-string)))
  3976. org-drawer-regexp
  3977. (concat "^[ \t]*:\\("
  3978. (mapconcat 'regexp-quote org-drawers "\\|")
  3979. "\\):[ \t]*$")
  3980. org-not-done-keywords
  3981. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3982. org-todo-regexp
  3983. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3984. "\\|") "\\)\\>")
  3985. org-not-done-regexp
  3986. (concat "\\<\\("
  3987. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3988. "\\)\\>")
  3989. org-not-done-heading-regexp
  3990. (concat "^\\(\\*+\\)[ \t]+\\("
  3991. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3992. "\\)\\>")
  3993. org-todo-line-regexp
  3994. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3995. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3996. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3997. org-complex-heading-regexp
  3998. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3999. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4000. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4001. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  4002. org-complex-heading-regexp-format
  4003. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4004. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4005. "\\)\\>\\)?"
  4006. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  4007. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  4008. "[ \t]*\\(%s\\)"
  4009. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  4010. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
  4011. org-nl-done-regexp
  4012. (concat "\n\\*+[ \t]+"
  4013. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4014. "\\)" "\\>")
  4015. org-todo-line-tags-regexp
  4016. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4017. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4018. (org-re
  4019. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
  4020. org-looking-at-done-regexp
  4021. (concat "^" "\\(?:"
  4022. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4023. "\\>")
  4024. org-deadline-regexp (concat "\\<" org-deadline-string)
  4025. org-deadline-time-regexp
  4026. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4027. org-deadline-line-regexp
  4028. (concat "\\<\\(" org-deadline-string "\\).*")
  4029. org-scheduled-regexp
  4030. (concat "\\<" org-scheduled-string)
  4031. org-scheduled-time-regexp
  4032. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4033. org-closed-time-regexp
  4034. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4035. org-keyword-time-regexp
  4036. (concat "\\<\\(" org-scheduled-string
  4037. "\\|" org-deadline-string
  4038. "\\|" org-closed-string
  4039. "\\|" org-clock-string "\\)"
  4040. " *[[<]\\([^]>]+\\)[]>]")
  4041. org-keyword-time-not-clock-regexp
  4042. (concat "\\<\\(" org-scheduled-string
  4043. "\\|" org-deadline-string
  4044. "\\|" org-closed-string
  4045. "\\)"
  4046. " *[[<]\\([^]>]+\\)[]>]")
  4047. org-maybe-keyword-time-regexp
  4048. (concat "\\(\\<\\(" org-scheduled-string
  4049. "\\|" org-deadline-string
  4050. "\\|" org-closed-string
  4051. "\\|" org-clock-string "\\)\\)?"
  4052. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4053. org-planning-or-clock-line-re
  4054. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4055. "\\|" org-deadline-string
  4056. "\\|" org-closed-string "\\|" org-clock-string
  4057. "\\)\\>\\)")
  4058. org-all-time-keywords
  4059. (mapcar (lambda (w) (substring w 0 -1))
  4060. (list org-scheduled-string org-deadline-string
  4061. org-clock-string org-closed-string))
  4062. )
  4063. (org-compute-latex-and-specials-regexp)
  4064. (org-set-font-lock-defaults))))
  4065. (defun org-file-contents (file &optional noerror)
  4066. "Return the contents of FILE, as a string."
  4067. (if (or (not file)
  4068. (not (file-readable-p file)))
  4069. (if noerror
  4070. (progn
  4071. (message "Cannot read file \"%s\"" file)
  4072. (ding) (sit-for 2)
  4073. "")
  4074. (error "Cannot read file \"%s\"" file))
  4075. (with-temp-buffer
  4076. (insert-file-contents file)
  4077. (buffer-string))))
  4078. (defun org-extract-log-state-settings (x)
  4079. "Extract the log state setting from a TODO keyword string.
  4080. This will extract info from a string like \"WAIT(w@/!)\"."
  4081. (let (kw key log1 log2)
  4082. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4083. (setq kw (match-string 1 x)
  4084. key (and (match-end 2) (match-string 2 x))
  4085. log1 (and (match-end 3) (match-string 3 x))
  4086. log2 (and (match-end 4) (match-string 4 x)))
  4087. (and (or log1 log2)
  4088. (list kw
  4089. (and log1 (if (equal log1 "!") 'time 'note))
  4090. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4091. (defun org-remove-keyword-keys (list)
  4092. "Remove a pair of parenthesis at the end of each string in LIST."
  4093. (mapcar (lambda (x)
  4094. (if (string-match "(.*)$" x)
  4095. (substring x 0 (match-beginning 0))
  4096. x))
  4097. list))
  4098. (defun org-assign-fast-keys (alist)
  4099. "Assign fast keys to a keyword-key alist.
  4100. Respect keys that are already there."
  4101. (let (new e (alt ?0))
  4102. (while (setq e (pop alist))
  4103. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4104. (cdr e)) ;; Key already assigned.
  4105. (push e new)
  4106. (let ((clist (string-to-list (downcase (car e))))
  4107. (used (append new alist)))
  4108. (when (= (car clist) ?@)
  4109. (pop clist))
  4110. (while (and clist (rassoc (car clist) used))
  4111. (pop clist))
  4112. (unless clist
  4113. (while (rassoc alt used)
  4114. (incf alt)))
  4115. (push (cons (car e) (or (car clist) alt)) new))))
  4116. (nreverse new)))
  4117. ;;; Some variables used in various places
  4118. (defvar org-window-configuration nil
  4119. "Used in various places to store a window configuration.")
  4120. (defvar org-selected-window nil
  4121. "Used in various places to store a window configuration.")
  4122. (defvar org-finish-function nil
  4123. "Function to be called when `C-c C-c' is used.
  4124. This is for getting out of special buffers like remember.")
  4125. ;; FIXME: Occasionally check by commenting these, to make sure
  4126. ;; no other functions uses these, forgetting to let-bind them.
  4127. (defvar entry)
  4128. (defvar last-state)
  4129. (defvar date)
  4130. ;; Defined somewhere in this file, but used before definition.
  4131. (defvar org-entities) ;; defined in org-entities.el
  4132. (defvar org-struct-menu)
  4133. (defvar org-org-menu)
  4134. (defvar org-tbl-menu)
  4135. ;;;; Define the Org-mode
  4136. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4137. (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"))
  4138. ;; We use a before-change function to check if a table might need
  4139. ;; an update.
  4140. (defvar org-table-may-need-update t
  4141. "Indicates that a table might need an update.
  4142. This variable is set by `org-before-change-function'.
  4143. `org-table-align' sets it back to nil.")
  4144. (defun org-before-change-function (beg end)
  4145. "Every change indicates that a table might need an update."
  4146. (setq org-table-may-need-update t))
  4147. (defvar org-mode-map)
  4148. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4149. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4150. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4151. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4152. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4153. (defvar org-table-buffer-is-an nil)
  4154. (defconst org-outline-regexp "\\*+ ")
  4155. (defconst org-outline-regexp-bol "^\\*+ ")
  4156. ;;;###autoload
  4157. (define-derived-mode org-mode outline-mode "Org"
  4158. "Outline-based notes management and organizer, alias
  4159. \"Carsten's outline-mode for keeping track of everything.\"
  4160. Org-mode develops organizational tasks around a NOTES file which
  4161. contains information about projects as plain text. Org-mode is
  4162. implemented on top of outline-mode, which is ideal to keep the content
  4163. of large files well structured. It supports ToDo items, deadlines and
  4164. time stamps, which magically appear in the diary listing of the Emacs
  4165. calendar. Tables are easily created with a built-in table editor.
  4166. Plain text URL-like links connect to websites, emails (VM), Usenet
  4167. messages (Gnus), BBDB entries, and any files related to the project.
  4168. For printing and sharing of notes, an Org-mode file (or a part of it)
  4169. can be exported as a structured ASCII or HTML file.
  4170. The following commands are available:
  4171. \\{org-mode-map}"
  4172. ;; Get rid of Outline menus, they are not needed
  4173. ;; Need to do this here because define-derived-mode sets up
  4174. ;; the keymap so late. Still, it is a waste to call this each time
  4175. ;; we switch another buffer into org-mode.
  4176. (if (featurep 'xemacs)
  4177. (when (boundp 'outline-mode-menu-heading)
  4178. ;; Assume this is Greg's port, it uses easymenu
  4179. (easy-menu-remove outline-mode-menu-heading)
  4180. (easy-menu-remove outline-mode-menu-show)
  4181. (easy-menu-remove outline-mode-menu-hide))
  4182. (define-key org-mode-map [menu-bar headings] 'undefined)
  4183. (define-key org-mode-map [menu-bar hide] 'undefined)
  4184. (define-key org-mode-map [menu-bar show] 'undefined))
  4185. (org-load-modules-maybe)
  4186. (easy-menu-add org-org-menu)
  4187. (easy-menu-add org-tbl-menu)
  4188. (org-install-agenda-files-menu)
  4189. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4190. (add-to-invisibility-spec '(org-cwidth))
  4191. (add-to-invisibility-spec '(org-hide-block . t))
  4192. (when (featurep 'xemacs)
  4193. (org-set-local 'line-move-ignore-invisible t))
  4194. (org-set-local 'outline-regexp org-outline-regexp)
  4195. (org-set-local 'outline-level 'org-outline-level)
  4196. (when (and org-ellipsis
  4197. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4198. (fboundp 'make-glyph-code))
  4199. (unless org-display-table
  4200. (setq org-display-table (make-display-table)))
  4201. (set-display-table-slot
  4202. org-display-table 4
  4203. (vconcat (mapcar
  4204. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4205. org-ellipsis)))
  4206. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4207. (setq buffer-display-table org-display-table))
  4208. (org-set-regexps-and-options)
  4209. (when (and org-tag-faces (not org-tags-special-faces-re))
  4210. ;; tag faces set outside customize.... force initialization.
  4211. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4212. ;; Calc embedded
  4213. (org-set-local 'calc-embedded-open-mode "# ")
  4214. (modify-syntax-entry ?@ "w")
  4215. (if org-startup-truncated (setq truncate-lines t))
  4216. (org-set-local 'font-lock-unfontify-region-function
  4217. 'org-unfontify-region)
  4218. ;; Activate before-change-function
  4219. (org-set-local 'org-table-may-need-update t)
  4220. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4221. 'local)
  4222. ;; Check for running clock before killing a buffer
  4223. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4224. ;; Paragraphs and auto-filling
  4225. (org-set-autofill-regexps)
  4226. (setq indent-line-function 'org-indent-line-function)
  4227. (org-update-radio-target-regexp)
  4228. ;; Beginning/end of defun
  4229. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4230. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4231. ;; Next error for sparse trees
  4232. (org-set-local 'next-error-function 'org-occur-next-match)
  4233. ;; Make sure dependence stuff works reliably, even for users who set it
  4234. ;; too late :-(
  4235. (if org-enforce-todo-dependencies
  4236. (add-hook 'org-blocker-hook
  4237. 'org-block-todo-from-children-or-siblings-or-parent)
  4238. (remove-hook 'org-blocker-hook
  4239. 'org-block-todo-from-children-or-siblings-or-parent))
  4240. (if org-enforce-todo-checkbox-dependencies
  4241. (add-hook 'org-blocker-hook
  4242. 'org-block-todo-from-checkboxes)
  4243. (remove-hook 'org-blocker-hook
  4244. 'org-block-todo-from-checkboxes))
  4245. ;; Comment characters
  4246. (org-set-local 'comment-start "#")
  4247. (org-set-local 'comment-padding " ")
  4248. ;; Align options lines
  4249. (org-set-local
  4250. 'align-mode-rules-list
  4251. '((org-in-buffer-settings
  4252. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4253. (modes . '(org-mode)))))
  4254. ;; Imenu
  4255. (org-set-local 'imenu-create-index-function
  4256. 'org-imenu-get-tree)
  4257. ;; Make isearch reveal context
  4258. (if (or (featurep 'xemacs)
  4259. (not (boundp 'outline-isearch-open-invisible-function)))
  4260. ;; Emacs 21 and XEmacs make use of the hook
  4261. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4262. ;; Emacs 22 deals with this through a special variable
  4263. (org-set-local 'outline-isearch-open-invisible-function
  4264. (lambda (&rest ignore) (org-show-context 'isearch))))
  4265. ;; Turn on org-beamer-mode?
  4266. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4267. ;; Setup the pcomplete hooks
  4268. (set (make-local-variable 'pcomplete-command-completion-function)
  4269. 'org-pcomplete-initial)
  4270. (set (make-local-variable 'pcomplete-command-name-function)
  4271. 'org-command-at-point)
  4272. (set (make-local-variable 'pcomplete-default-completion-function)
  4273. 'ignore)
  4274. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4275. 'org-parse-arguments)
  4276. (set (make-local-variable 'pcomplete-termination-string) "")
  4277. (set (make-local-variable 'face-remapping-alist)
  4278. '((default org-default)))
  4279. ;; If empty file that did not turn on org-mode automatically, make it to.
  4280. (if (and org-insert-mode-line-in-empty-file
  4281. (org-called-interactively-p 'any)
  4282. (= (point-min) (point-max)))
  4283. (insert "# -*- mode: org -*-\n\n"))
  4284. (unless org-inhibit-startup
  4285. (when org-startup-align-all-tables
  4286. (let ((bmp (buffer-modified-p)))
  4287. (org-table-map-tables 'org-table-align 'quietly)
  4288. (set-buffer-modified-p bmp)))
  4289. (when org-startup-with-inline-images
  4290. (org-display-inline-images))
  4291. (when org-startup-indented
  4292. (require 'org-indent)
  4293. (org-indent-mode 1))
  4294. (unless org-inhibit-startup-visibility-stuff
  4295. (org-set-startup-visibility))))
  4296. (when (fboundp 'abbrev-table-put)
  4297. (abbrev-table-put org-mode-abbrev-table
  4298. :parents (list text-mode-abbrev-table)))
  4299. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4300. (defun org-current-time ()
  4301. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4302. (if (> (car org-time-stamp-rounding-minutes) 1)
  4303. (let ((r (car org-time-stamp-rounding-minutes))
  4304. (time (decode-time)))
  4305. (apply 'encode-time
  4306. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4307. (nthcdr 2 time))))
  4308. (current-time)))
  4309. (defun org-today ()
  4310. "Return today date, considering `org-extend-today-until'."
  4311. (time-to-days
  4312. (time-subtract (current-time)
  4313. (list 0 (* 3600 org-extend-today-until) 0))))
  4314. ;;;; Font-Lock stuff, including the activators
  4315. (defvar org-mouse-map (make-sparse-keymap))
  4316. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4317. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4318. (when org-mouse-1-follows-link
  4319. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4320. (when org-tab-follows-link
  4321. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4322. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4323. (require 'font-lock)
  4324. (defconst org-non-link-chars "]\t\n\r<>")
  4325. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4326. "shell" "elisp" "doi" "message"))
  4327. (defvar org-link-types-re nil
  4328. "Matches a link that has a url-like prefix like \"http:\"")
  4329. (defvar org-link-re-with-space nil
  4330. "Matches a link with spaces, optional angular brackets around it.")
  4331. (defvar org-link-re-with-space2 nil
  4332. "Matches a link with spaces, optional angular brackets around it.")
  4333. (defvar org-link-re-with-space3 nil
  4334. "Matches a link with spaces, only for internal part in bracket links.")
  4335. (defvar org-angle-link-re nil
  4336. "Matches link with angular brackets, spaces are allowed.")
  4337. (defvar org-plain-link-re nil
  4338. "Matches plain link, without spaces.")
  4339. (defvar org-bracket-link-regexp nil
  4340. "Matches a link in double brackets.")
  4341. (defvar org-bracket-link-analytic-regexp nil
  4342. "Regular expression used to analyze links.
  4343. Here is what the match groups contain after a match:
  4344. 1: http:
  4345. 2: http
  4346. 3: path
  4347. 4: [desc]
  4348. 5: desc")
  4349. (defvar org-bracket-link-analytic-regexp++ nil
  4350. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4351. (defvar org-any-link-re nil
  4352. "Regular expression matching any link.")
  4353. (defcustom org-match-sexp-depth 3
  4354. "Number of stacked braces for sub/superscript matching.
  4355. This has to be set before loading org.el to be effective."
  4356. :group 'org-export-translation ; ??????????????????????????/
  4357. :type 'integer)
  4358. (defun org-create-multibrace-regexp (left right n)
  4359. "Create a regular expression which will match a balanced sexp.
  4360. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4361. as single character strings.
  4362. The regexp returned will match the entire expression including the
  4363. delimiters. It will also define a single group which contains the
  4364. match except for the outermost delimiters. The maximum depth of
  4365. stacked delimiters is N. Escaping delimiters is not possible."
  4366. (let* ((nothing (concat "[^" left right "]*?"))
  4367. (or "\\|")
  4368. (re nothing)
  4369. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4370. (while (> n 1)
  4371. (setq n (1- n)
  4372. re (concat re or next)
  4373. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4374. (concat left "\\(" re "\\)" right)))
  4375. (defvar org-match-substring-regexp
  4376. (concat
  4377. "\\([^\\]\\)\\([_^]\\)\\("
  4378. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4379. "\\|"
  4380. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4381. "\\|"
  4382. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4383. "The regular expression matching a sub- or superscript.")
  4384. (defvar org-match-substring-with-braces-regexp
  4385. (concat
  4386. "\\([^\\]\\)\\([_^]\\)\\("
  4387. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4388. "\\)")
  4389. "The regular expression matching a sub- or superscript, forcing braces.")
  4390. (defun org-make-link-regexps ()
  4391. "Update the link regular expressions.
  4392. This should be called after the variable `org-link-types' has changed."
  4393. (setq org-link-types-re
  4394. (concat
  4395. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4396. org-link-re-with-space
  4397. (concat
  4398. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4399. "\\([^" org-non-link-chars " ]"
  4400. "[^" org-non-link-chars "]*"
  4401. "[^" org-non-link-chars " ]\\)>?")
  4402. org-link-re-with-space2
  4403. (concat
  4404. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4405. "\\([^" org-non-link-chars " ]"
  4406. "[^\t\n\r]*"
  4407. "[^" org-non-link-chars " ]\\)>?")
  4408. org-link-re-with-space3
  4409. (concat
  4410. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4411. "\\([^" org-non-link-chars " ]"
  4412. "[^\t\n\r]*\\)")
  4413. org-angle-link-re
  4414. (concat
  4415. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4416. "\\([^" org-non-link-chars " ]"
  4417. "[^" org-non-link-chars "]*"
  4418. "\\)>")
  4419. org-plain-link-re
  4420. (concat
  4421. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4422. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4423. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4424. org-bracket-link-regexp
  4425. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4426. org-bracket-link-analytic-regexp
  4427. (concat
  4428. "\\[\\["
  4429. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4430. "\\([^]]+\\)"
  4431. "\\]"
  4432. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4433. "\\]")
  4434. org-bracket-link-analytic-regexp++
  4435. (concat
  4436. "\\[\\["
  4437. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4438. "\\([^]]+\\)"
  4439. "\\]"
  4440. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4441. "\\]")
  4442. org-any-link-re
  4443. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4444. org-angle-link-re "\\)\\|\\("
  4445. org-plain-link-re "\\)")))
  4446. (org-make-link-regexps)
  4447. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4448. "Regular expression for fast time stamp matching.")
  4449. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?\\)[]>]"
  4450. "Regular expression for fast time stamp matching.")
  4451. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4452. "Regular expression matching time strings for analysis.
  4453. This one does not require the space after the date, so it can be used
  4454. on a string that terminates immediately after the date.")
  4455. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4456. "Regular expression matching time strings for analysis.")
  4457. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4458. "Regular expression matching time stamps, with groups.")
  4459. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4460. "Regular expression matching time stamps (also [..]), with groups.")
  4461. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4462. "Regular expression matching a time stamp range.")
  4463. (defconst org-tr-regexp-both
  4464. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4465. "Regular expression matching a time stamp range.")
  4466. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4467. org-ts-regexp "\\)?")
  4468. "Regular expression matching a time stamp or time stamp range.")
  4469. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4470. org-ts-regexp-both "\\)?")
  4471. "Regular expression matching a time stamp or time stamp range.
  4472. The time stamps may be either active or inactive.")
  4473. (defvar org-emph-face nil)
  4474. (defun org-do-emphasis-faces (limit)
  4475. "Run through the buffer and add overlays to links."
  4476. (let (rtn a)
  4477. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4478. (if (not (= (char-after (match-beginning 3))
  4479. (char-after (match-beginning 4))))
  4480. (progn
  4481. (setq rtn t)
  4482. (setq a (assoc (match-string 3) org-emphasis-alist))
  4483. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4484. 'face
  4485. (nth 1 a))
  4486. (and (nth 4 a)
  4487. (org-remove-flyspell-overlays-in
  4488. (match-beginning 0) (match-end 0)))
  4489. (add-text-properties (match-beginning 2) (match-end 2)
  4490. '(font-lock-multiline t org-emphasis t))
  4491. (when org-hide-emphasis-markers
  4492. (add-text-properties (match-end 4) (match-beginning 5)
  4493. '(invisible org-link))
  4494. (add-text-properties (match-beginning 3) (match-end 3)
  4495. '(invisible org-link)))))
  4496. (backward-char 1))
  4497. rtn))
  4498. (defun org-emphasize (&optional char)
  4499. "Insert or change an emphasis, i.e. a font like bold or italic.
  4500. If there is an active region, change that region to a new emphasis.
  4501. If there is no region, just insert the marker characters and position
  4502. the cursor between them.
  4503. CHAR should be either the marker character, or the first character of the
  4504. HTML tag associated with that emphasis. If CHAR is a space, the means
  4505. to remove the emphasis of the selected region.
  4506. If char is not given (for example in an interactive call) it
  4507. will be prompted for."
  4508. (interactive)
  4509. (let ((eal org-emphasis-alist) e det
  4510. (erc org-emphasis-regexp-components)
  4511. (prompt "")
  4512. (string "") beg end move tag c s)
  4513. (if (org-region-active-p)
  4514. (setq beg (region-beginning) end (region-end)
  4515. string (buffer-substring beg end))
  4516. (setq move t))
  4517. (while (setq e (pop eal))
  4518. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4519. c (aref tag 0))
  4520. (push (cons c (string-to-char (car e))) det)
  4521. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4522. (substring tag 1)))))
  4523. (setq det (nreverse det))
  4524. (unless char
  4525. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4526. (setq char (read-char-exclusive)))
  4527. (setq char (or (cdr (assoc char det)) char))
  4528. (if (equal char ?\ )
  4529. (setq s "" move nil)
  4530. (unless (assoc (char-to-string char) org-emphasis-alist)
  4531. (error "No such emphasis marker: \"%c\"" char))
  4532. (setq s (char-to-string char)))
  4533. (while (and (> (length string) 1)
  4534. (equal (substring string 0 1) (substring string -1))
  4535. (assoc (substring string 0 1) org-emphasis-alist))
  4536. (setq string (substring string 1 -1)))
  4537. (setq string (concat s string s))
  4538. (if beg (delete-region beg end))
  4539. (unless (or (bolp)
  4540. (string-match (concat "[" (nth 0 erc) "\n]")
  4541. (char-to-string (char-before (point)))))
  4542. (insert " "))
  4543. (unless (or (eobp)
  4544. (string-match (concat "[" (nth 1 erc) "\n]")
  4545. (char-to-string (char-after (point)))))
  4546. (insert " ") (backward-char 1))
  4547. (insert string)
  4548. (and move (backward-char 1))))
  4549. (defconst org-nonsticky-props
  4550. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4551. (defsubst org-rear-nonsticky-at (pos)
  4552. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4553. (defun org-activate-plain-links (limit)
  4554. "Run through the buffer and add overlays to links."
  4555. (catch 'exit
  4556. (let (f)
  4557. (if (re-search-forward org-plain-link-re limit t)
  4558. (progn
  4559. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4560. (setq f (get-text-property (match-beginning 0) 'face))
  4561. (if (or (eq f 'org-tag)
  4562. (and (listp f) (memq 'org-tag f)))
  4563. nil
  4564. (add-text-properties (match-beginning 0) (match-end 0)
  4565. (list 'mouse-face 'highlight
  4566. 'face 'org-link
  4567. 'keymap org-mouse-map))
  4568. (org-rear-nonsticky-at (match-end 0)))
  4569. t)))))
  4570. (defun org-activate-code (limit)
  4571. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4572. (progn
  4573. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4574. (remove-text-properties (match-beginning 0) (match-end 0)
  4575. '(display t invisible t intangible t))
  4576. t)))
  4577. (defcustom org-src-fontify-natively nil
  4578. "When non-nil, fontify code in code blocks."
  4579. :type 'boolean
  4580. :group 'org-appearance
  4581. :group 'org-babel)
  4582. (defun org-fontify-meta-lines-and-blocks (limit)
  4583. (condition-case nil
  4584. (org-fontify-meta-lines-and-blocks-1 limit)
  4585. (error (message "org-mode fontification error"))))
  4586. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4587. "Fontify #+ lines and blocks, in the correct ways."
  4588. (let ((case-fold-search t))
  4589. (if (re-search-forward
  4590. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4591. limit t)
  4592. (let ((beg (match-beginning 0))
  4593. (block-start (match-end 0))
  4594. (block-end nil)
  4595. (lang (match-string 7))
  4596. (beg1 (line-beginning-position 2))
  4597. (dc1 (downcase (match-string 2)))
  4598. (dc3 (downcase (match-string 3)))
  4599. end end1 quoting block-type ovl)
  4600. (cond
  4601. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4602. ;; a single line of backend-specific content
  4603. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4604. (remove-text-properties (match-beginning 0) (match-end 0)
  4605. '(display t invisible t intangible t))
  4606. (add-text-properties (match-beginning 1) (match-end 3)
  4607. '(font-lock-fontified t face org-meta-line))
  4608. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4609. '(font-lock-fontified t face org-block))
  4610. ; for backend-specific code
  4611. t)
  4612. ((and (match-end 4) (equal dc3 "begin"))
  4613. ;; Truly a block
  4614. (setq block-type (downcase (match-string 5))
  4615. quoting (member block-type org-protecting-blocks))
  4616. (when (re-search-forward
  4617. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4618. nil t) ;; on purpose, we look further than LIMIT
  4619. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4620. (setq block-end (match-beginning 0))
  4621. (when quoting
  4622. (remove-text-properties beg end
  4623. '(display t invisible t intangible t)))
  4624. (add-text-properties
  4625. beg end
  4626. '(font-lock-fontified t font-lock-multiline t))
  4627. (add-text-properties beg beg1 '(face org-meta-line))
  4628. (add-text-properties end1 (min (point-max) (1+ end))
  4629. '(face org-meta-line)) ; for end_src
  4630. (cond
  4631. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4632. (org-src-font-lock-fontify-block lang block-start block-end)
  4633. ;; remove old background overlays
  4634. (mapc (lambda (ov)
  4635. (if (eq (overlay-get ov 'face) 'org-block-background)
  4636. (delete-overlay ov)))
  4637. (overlays-at (/ (+ beg1 block-end) 2)))
  4638. ;; add a background overlay
  4639. (setq ovl (make-overlay beg1 block-end))
  4640. (overlay-put ovl 'face 'org-block-background)
  4641. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4642. (quoting
  4643. (add-text-properties beg1 (min (point-max) (1+ end1))
  4644. '(face org-block))) ; end of source block
  4645. ((not org-fontify-quote-and-verse-blocks))
  4646. ((string= block-type "quote")
  4647. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4648. ((string= block-type "verse")
  4649. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4650. (add-text-properties beg beg1 '(face org-block-begin-line))
  4651. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4652. t))
  4653. ((member dc1 '("title:" "author:" "email:" "date:"))
  4654. (add-text-properties
  4655. beg (match-end 3)
  4656. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4657. '(font-lock-fontified t invisible t)
  4658. '(font-lock-fontified t face org-document-info-keyword)))
  4659. (add-text-properties
  4660. (match-beginning 6) (match-end 6)
  4661. (if (string-equal dc1 "title:")
  4662. '(font-lock-fontified t face org-document-title)
  4663. '(font-lock-fontified t face org-document-info))))
  4664. ((not (member (char-after beg) '(?\ ?\t)))
  4665. ;; just any other in-buffer setting, but not indented
  4666. (add-text-properties
  4667. beg (1+ (match-end 0))
  4668. '(font-lock-fontified t face org-meta-line))
  4669. t)
  4670. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4671. "orgtbl:" "tblfm:" "tblname:" "result:"
  4672. "results:" "source:" "srcname:" "call:"
  4673. "data:" "header:" "headers:"))
  4674. (and (match-end 4) (equal dc3 "attr")))
  4675. (add-text-properties
  4676. beg (match-end 0)
  4677. '(font-lock-fontified t face org-meta-line))
  4678. t)
  4679. ((member dc3 '(" " ""))
  4680. (add-text-properties
  4681. beg (match-end 0)
  4682. '(font-lock-fontified t face font-lock-comment-face)))
  4683. (t nil))))))
  4684. (defun org-activate-angle-links (limit)
  4685. "Run through the buffer and add overlays to links."
  4686. (if (re-search-forward org-angle-link-re limit t)
  4687. (progn
  4688. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4689. (add-text-properties (match-beginning 0) (match-end 0)
  4690. (list 'mouse-face 'highlight
  4691. 'keymap org-mouse-map))
  4692. (org-rear-nonsticky-at (match-end 0))
  4693. t)))
  4694. (defun org-activate-footnote-links (limit)
  4695. "Run through the buffer and add overlays to footnotes."
  4696. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4697. (when fn
  4698. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4699. (org-remove-flyspell-overlays-in beg end)
  4700. (add-text-properties beg end
  4701. (list 'mouse-face 'highlight
  4702. 'keymap org-mouse-map
  4703. 'help-echo
  4704. (if (= (point-at-bol) beg)
  4705. "Footnote definition"
  4706. "Footnote reference")
  4707. 'font-lock-fontified t
  4708. 'font-lock-multiline t
  4709. 'face 'org-footnote))))))
  4710. (defun org-activate-bracket-links (limit)
  4711. "Run through the buffer and add overlays to bracketed links."
  4712. (if (re-search-forward org-bracket-link-regexp limit t)
  4713. (let* ((help (concat "LINK: "
  4714. (org-match-string-no-properties 1)))
  4715. ;; FIXME: above we should remove the escapes.
  4716. ;; but that requires another match, protecting match data,
  4717. ;; a lot of overhead for font-lock.
  4718. (ip (org-maybe-intangible
  4719. (list 'invisible 'org-link
  4720. 'keymap org-mouse-map 'mouse-face 'highlight
  4721. 'font-lock-multiline t 'help-echo help)))
  4722. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4723. 'font-lock-multiline t 'help-echo help)))
  4724. ;; We need to remove the invisible property here. Table narrowing
  4725. ;; may have made some of this invisible.
  4726. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4727. (remove-text-properties (match-beginning 0) (match-end 0)
  4728. '(invisible nil))
  4729. (if (match-end 3)
  4730. (progn
  4731. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4732. (org-rear-nonsticky-at (match-beginning 3))
  4733. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4734. (org-rear-nonsticky-at (match-end 3))
  4735. (add-text-properties (match-end 3) (match-end 0) ip)
  4736. (org-rear-nonsticky-at (match-end 0)))
  4737. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4738. (org-rear-nonsticky-at (match-beginning 1))
  4739. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4740. (org-rear-nonsticky-at (match-end 1))
  4741. (add-text-properties (match-end 1) (match-end 0) ip)
  4742. (org-rear-nonsticky-at (match-end 0)))
  4743. t)))
  4744. (defun org-activate-dates (limit)
  4745. "Run through the buffer and add overlays to dates."
  4746. (if (re-search-forward org-tsr-regexp-both limit t)
  4747. (progn
  4748. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4749. (add-text-properties (match-beginning 0) (match-end 0)
  4750. (list 'mouse-face 'highlight
  4751. 'keymap org-mouse-map))
  4752. (org-rear-nonsticky-at (match-end 0))
  4753. (when org-display-custom-times
  4754. (if (match-end 3)
  4755. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4756. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4757. t)))
  4758. (defvar org-target-link-regexp nil
  4759. "Regular expression matching radio targets in plain text.")
  4760. (make-variable-buffer-local 'org-target-link-regexp)
  4761. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4762. "Regular expression matching a link target.")
  4763. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4764. "Regular expression matching a radio target.")
  4765. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4766. "Regular expression matching any target.")
  4767. (defun org-activate-target-links (limit)
  4768. "Run through the buffer and add overlays to target matches."
  4769. (when org-target-link-regexp
  4770. (let ((case-fold-search t))
  4771. (if (re-search-forward org-target-link-regexp limit t)
  4772. (progn
  4773. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4774. (add-text-properties (match-beginning 0) (match-end 0)
  4775. (list 'mouse-face 'highlight
  4776. 'keymap org-mouse-map
  4777. 'help-echo "Radio target link"
  4778. 'org-linked-text t))
  4779. (org-rear-nonsticky-at (match-end 0))
  4780. t)))))
  4781. (defun org-update-radio-target-regexp ()
  4782. "Find all radio targets in this file and update the regular expression."
  4783. (interactive)
  4784. (when (memq 'radio org-activate-links)
  4785. (setq org-target-link-regexp
  4786. (org-make-target-link-regexp (org-all-targets 'radio)))
  4787. (org-restart-font-lock)))
  4788. (defun org-hide-wide-columns (limit)
  4789. (let (s e)
  4790. (setq s (text-property-any (point) (or limit (point-max))
  4791. 'org-cwidth t))
  4792. (when s
  4793. (setq e (next-single-property-change s 'org-cwidth))
  4794. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4795. (goto-char e)
  4796. t)))
  4797. (defvar org-latex-and-specials-regexp nil
  4798. "Regular expression for highlighting export special stuff.")
  4799. (defvar org-match-substring-regexp)
  4800. (defvar org-match-substring-with-braces-regexp)
  4801. ;; This should be with the exporter code, but we also use if for font-locking
  4802. (defconst org-export-html-special-string-regexps
  4803. '(("\\\\-" . "&shy;")
  4804. ("---\\([^-]\\)" . "&mdash;\\1")
  4805. ("--\\([^-]\\)" . "&ndash;\\1")
  4806. ("\\.\\.\\." . "&hellip;"))
  4807. "Regular expressions for special string conversion.")
  4808. (defun org-compute-latex-and-specials-regexp ()
  4809. "Compute regular expression for stuff treated specially by exporters."
  4810. (if (not org-highlight-latex-fragments-and-specials)
  4811. (org-set-local 'org-latex-and-specials-regexp nil)
  4812. (require 'org-exp)
  4813. (let*
  4814. ((matchers (plist-get org-format-latex-options :matchers))
  4815. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4816. org-latex-regexps)))
  4817. (org-export-allow-BIND nil)
  4818. (options (org-combine-plists (org-default-export-plist)
  4819. (org-infile-export-plist)))
  4820. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4821. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4822. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4823. (org-export-html-expand (plist-get options :expand-quoted-html))
  4824. (org-export-with-special-strings (plist-get options :special-strings))
  4825. (re-sub
  4826. (cond
  4827. ((equal org-export-with-sub-superscripts '{})
  4828. (list org-match-substring-with-braces-regexp))
  4829. (org-export-with-sub-superscripts
  4830. (list org-match-substring-regexp))
  4831. (t nil)))
  4832. (re-latex
  4833. (if org-export-with-LaTeX-fragments
  4834. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4835. (re-macros
  4836. (if org-export-with-TeX-macros
  4837. (list (concat "\\\\"
  4838. (regexp-opt
  4839. (append
  4840. (delq nil
  4841. (mapcar 'car-safe
  4842. (append org-entities-user
  4843. org-entities)))
  4844. (if (boundp 'org-latex-entities)
  4845. (mapcar (lambda (x)
  4846. (or (car-safe x) x))
  4847. org-latex-entities)
  4848. nil))
  4849. 'words))) ; FIXME
  4850. ))
  4851. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4852. (re-special (if org-export-with-special-strings
  4853. (mapcar (lambda (x) (car x))
  4854. org-export-html-special-string-regexps)))
  4855. (re-rest
  4856. (delq nil
  4857. (list
  4858. (if org-export-html-expand "@<[^>\n]+>")
  4859. ))))
  4860. (org-set-local
  4861. 'org-latex-and-specials-regexp
  4862. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4863. re-rest) "\\|")))))
  4864. (defun org-do-latex-and-special-faces (limit)
  4865. "Run through the buffer and add overlays to links."
  4866. (when org-latex-and-specials-regexp
  4867. (let (rtn d)
  4868. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4869. limit t))
  4870. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4871. 'face))
  4872. '(org-code org-verbatim underline)))
  4873. (progn
  4874. (setq rtn t
  4875. d (cond ((member (char-after (1+ (match-beginning 0)))
  4876. '(?_ ?^)) 1)
  4877. (t 0)))
  4878. (font-lock-prepend-text-property
  4879. (+ d (match-beginning 0)) (match-end 0)
  4880. 'face 'org-latex-and-export-specials)
  4881. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4882. '(font-lock-multiline t)))))
  4883. rtn)))
  4884. (defun org-restart-font-lock ()
  4885. "Restart `font-lock-mode', to force refontification."
  4886. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4887. (font-lock-mode -1)
  4888. (font-lock-mode 1)))
  4889. (defun org-all-targets (&optional radio)
  4890. "Return a list of all targets in this file.
  4891. With optional argument RADIO, only find radio targets."
  4892. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4893. rtn)
  4894. (save-excursion
  4895. (goto-char (point-min))
  4896. (while (re-search-forward re nil t)
  4897. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4898. rtn)))
  4899. (defun org-make-target-link-regexp (targets)
  4900. "Make regular expression matching all strings in TARGETS.
  4901. The regular expression finds the targets also if there is a line break
  4902. between words."
  4903. (and targets
  4904. (concat
  4905. "\\<\\("
  4906. (mapconcat
  4907. (lambda (x)
  4908. (setq x (regexp-quote x))
  4909. (while (string-match " +" x)
  4910. (setq x (replace-match "\\s-+" t t x)))
  4911. x)
  4912. targets
  4913. "\\|")
  4914. "\\)\\>")))
  4915. (defun org-activate-tags (limit)
  4916. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4917. (progn
  4918. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4919. (add-text-properties (match-beginning 1) (match-end 1)
  4920. (list 'mouse-face 'highlight
  4921. 'keymap org-mouse-map))
  4922. (org-rear-nonsticky-at (match-end 1))
  4923. t)))
  4924. (defun org-outline-level ()
  4925. "Compute the outline level of the heading at point.
  4926. This function assumes that the cursor is at the beginning of a line matched
  4927. by `outline-regexp'. Otherwise it returns garbage.
  4928. If this is called at a normal headline, the level is the number of stars.
  4929. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  4930. (save-excursion
  4931. (looking-at org-outline-regexp)
  4932. (1- (- (match-end 0) (match-beginning 0)))))
  4933. (defvar org-font-lock-keywords nil)
  4934. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4935. "Regular expression matching a property line.")
  4936. (defvar org-font-lock-hook nil
  4937. "Functions to be called for special font lock stuff.")
  4938. (defvar org-font-lock-set-keywords-hook nil
  4939. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4940. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4941. it is installed to be used by font lock. This can be useful if something
  4942. needs to be inserted at a specific position in the font-lock sequence.")
  4943. (defun org-font-lock-hook (limit)
  4944. (run-hook-with-args 'org-font-lock-hook limit))
  4945. (defun org-set-font-lock-defaults ()
  4946. (let* ((em org-fontify-emphasized-text)
  4947. (lk org-activate-links)
  4948. (org-font-lock-extra-keywords
  4949. (list
  4950. ;; Call the hook
  4951. '(org-font-lock-hook)
  4952. ;; Headlines
  4953. `(,(if org-fontify-whole-heading-line
  4954. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4955. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4956. (1 (org-get-level-face 1))
  4957. (2 (org-get-level-face 2))
  4958. (3 (org-get-level-face 3)))
  4959. ;; Table lines
  4960. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4961. (1 'org-table t))
  4962. ;; Table internals
  4963. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4964. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4965. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4966. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  4967. ;; Drawers
  4968. (list org-drawer-regexp '(0 'org-special-keyword t))
  4969. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4970. ;; Properties
  4971. (list org-property-re
  4972. '(1 'org-special-keyword t)
  4973. '(3 'org-property-value t))
  4974. ;; Links
  4975. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4976. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4977. (if (memq 'plain lk) '(org-activate-plain-links))
  4978. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4979. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4980. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4981. (if (memq 'footnote lk) '(org-activate-footnote-links))
  4982. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4983. '(org-hide-wide-columns (0 nil append))
  4984. ;; TODO lines
  4985. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4986. '(1 (org-get-todo-face 1) t))
  4987. ;; DONE
  4988. (if org-fontify-done-headline
  4989. (list (concat "^[*]+ +\\<\\("
  4990. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4991. "\\)\\(.*\\)")
  4992. '(2 'org-headline-done t))
  4993. nil)
  4994. ;; Priorities
  4995. '(org-font-lock-add-priority-faces)
  4996. ;; Tags
  4997. '(org-font-lock-add-tag-faces)
  4998. ;; Special keywords
  4999. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5000. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5001. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5002. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5003. ;; Emphasis
  5004. (if em
  5005. (if (featurep 'xemacs)
  5006. '(org-do-emphasis-faces (0 nil append))
  5007. '(org-do-emphasis-faces)))
  5008. ;; Checkboxes
  5009. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5010. 1 'org-checkbox prepend)
  5011. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5012. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5013. (0 (org-get-checkbox-statistics-face) t)))
  5014. ;; Description list items
  5015. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5016. 1 'bold prepend)
  5017. ;; ARCHIVEd headings
  5018. (list (concat
  5019. org-outline-regexp-bol
  5020. "\\(.*:" org-archive-tag ":.*\\)")
  5021. '(1 'org-archived prepend))
  5022. ;; Specials
  5023. '(org-do-latex-and-special-faces)
  5024. '(org-fontify-entities)
  5025. '(org-raise-scripts)
  5026. ;; Code
  5027. '(org-activate-code (1 'org-code t))
  5028. ;; COMMENT
  5029. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  5030. "\\|" org-quote-string "\\)\\>")
  5031. '(1 'org-special-keyword t))
  5032. '("^#.*" (0 'font-lock-comment-face t))
  5033. ;; Blocks and meta lines
  5034. '(org-fontify-meta-lines-and-blocks)
  5035. )))
  5036. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5037. (run-hooks 'org-font-lock-set-keywords-hook)
  5038. ;; Now set the full font-lock-keywords
  5039. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5040. (org-set-local 'font-lock-defaults
  5041. '(org-font-lock-keywords t nil nil backward-paragraph))
  5042. (kill-local-variable 'font-lock-keywords) nil))
  5043. (defun org-toggle-pretty-entities ()
  5044. "Toggle the composition display of entities as UTF8 characters."
  5045. (interactive)
  5046. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5047. (org-restart-font-lock)
  5048. (if org-pretty-entities
  5049. (message "Entities are displayed as UTF8 characers")
  5050. (save-restriction
  5051. (widen)
  5052. (org-decompose-region (point-min) (point-max))
  5053. (message "Entities are displayed plain"))))
  5054. (defun org-fontify-entities (limit)
  5055. "Find an entity to fontify."
  5056. (let (ee)
  5057. (when org-pretty-entities
  5058. (catch 'match
  5059. (while (re-search-forward
  5060. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  5061. limit t)
  5062. (if (and (not (org-in-indented-comment-line))
  5063. (setq ee (org-entity-get (match-string 1)))
  5064. (= (length (nth 6 ee)) 1))
  5065. (progn
  5066. (add-text-properties
  5067. (match-beginning 0) (match-end 1)
  5068. (list 'font-lock-fontified t))
  5069. (compose-region (match-beginning 0) (match-end 1)
  5070. (nth 6 ee) nil)
  5071. (backward-char 1)
  5072. (throw 'match t))))
  5073. nil))))
  5074. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5075. "Fontify string S like in Org-mode."
  5076. (with-temp-buffer
  5077. (insert s)
  5078. (let ((org-odd-levels-only odd-levels))
  5079. (org-mode)
  5080. (font-lock-fontify-buffer)
  5081. (buffer-string))))
  5082. (defvar org-m nil)
  5083. (defvar org-l nil)
  5084. (defvar org-f nil)
  5085. (defun org-get-level-face (n)
  5086. "Get the right face for match N in font-lock matching of headlines."
  5087. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5088. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5089. (if org-cycle-level-faces
  5090. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5091. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5092. (cond
  5093. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5094. ((eq n 2) org-f)
  5095. (t (if org-level-color-stars-only nil org-f))))
  5096. (defun org-get-todo-face (kwd)
  5097. "Get the right face for a TODO keyword KWD.
  5098. If KWD is a number, get the corresponding match group."
  5099. (if (numberp kwd) (setq kwd (match-string kwd)))
  5100. (or (org-face-from-face-or-color
  5101. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5102. (and (member kwd org-done-keywords) 'org-done)
  5103. 'org-todo))
  5104. (defun org-face-from-face-or-color (context inherit face-or-color)
  5105. "Create a face list that inherits INHERIT, but sets the foreground color.
  5106. When FACE-OR-COLOR is not a string, just return it."
  5107. (if (stringp face-or-color)
  5108. (list :inherit inherit
  5109. (cdr (assoc context org-faces-easy-properties))
  5110. face-or-color)
  5111. face-or-color))
  5112. (defun org-font-lock-add-tag-faces (limit)
  5113. "Add the special tag faces."
  5114. (when (and org-tag-faces org-tags-special-faces-re)
  5115. (while (re-search-forward org-tags-special-faces-re limit t)
  5116. (add-text-properties (match-beginning 1) (match-end 1)
  5117. (list 'face (org-get-tag-face 1)
  5118. 'font-lock-fontified t))
  5119. (backward-char 1))))
  5120. (defun org-font-lock-add-priority-faces (limit)
  5121. "Add the special priority faces."
  5122. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5123. (add-text-properties
  5124. (match-beginning 0) (match-end 0)
  5125. (list 'face (or (org-face-from-face-or-color
  5126. 'priority 'org-special-keyword
  5127. (cdr (assoc (char-after (match-beginning 1))
  5128. org-priority-faces)))
  5129. 'org-special-keyword)
  5130. 'font-lock-fontified t))))
  5131. (defun org-get-tag-face (kwd)
  5132. "Get the right face for a TODO keyword KWD.
  5133. If KWD is a number, get the corresponding match group."
  5134. (if (numberp kwd) (setq kwd (match-string kwd)))
  5135. (or (org-face-from-face-or-color
  5136. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5137. 'org-tag))
  5138. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5139. "Remove fontification and activation overlays from links."
  5140. (font-lock-default-unfontify-region beg end)
  5141. (let* ((buffer-undo-list t)
  5142. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5143. (inhibit-modification-hooks t)
  5144. deactivate-mark buffer-file-name buffer-file-truename)
  5145. (org-decompose-region beg end)
  5146. (remove-text-properties
  5147. beg end
  5148. (if org-indent-mode
  5149. ;; also remove line-prefix and wrap-prefix properties
  5150. '(mouse-face t keymap t org-linked-text t
  5151. invisible t intangible t
  5152. line-prefix t wrap-prefix t
  5153. org-no-flyspell t org-emphasis t)
  5154. '(mouse-face t keymap t org-linked-text t
  5155. invisible t intangible t
  5156. org-no-flyspell t org-emphasis t)))
  5157. (org-remove-font-lock-display-properties beg end)))
  5158. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5159. ((raise 0.3) (height 0.7))
  5160. ((raise -0.5))
  5161. ((raise 0.5)))
  5162. "Display properties for showing superscripts and subscripts.")
  5163. (defun org-remove-font-lock-display-properties (beg end)
  5164. "Remove specific display properties that have been added by font lock.
  5165. The will remove the raise properties that are used to show superscripts
  5166. and subscripts."
  5167. (let (next prop)
  5168. (while (< beg end)
  5169. (setq next (next-single-property-change beg 'display nil end)
  5170. prop (get-text-property beg 'display))
  5171. (if (member prop org-script-display)
  5172. (put-text-property beg next 'display nil))
  5173. (setq beg next))))
  5174. (defun org-raise-scripts (limit)
  5175. "Add raise properties to sub/superscripts."
  5176. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5177. (if (re-search-forward
  5178. (if (eq org-use-sub-superscripts t)
  5179. org-match-substring-regexp
  5180. org-match-substring-with-braces-regexp)
  5181. limit t)
  5182. (let* ((pos (point)) table-p comment-p
  5183. (mpos (match-beginning 3))
  5184. (emph-p (get-text-property mpos 'org-emphasis))
  5185. (link-p (get-text-property mpos 'mouse-face))
  5186. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5187. (goto-char (point-at-bol))
  5188. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5189. comment-p (org-looking-at-p "[ \t]*#"))
  5190. (goto-char pos)
  5191. ;; FIXME: Should we go back one character here, for a_b^c
  5192. ;; (goto-char (1- pos)) ;????????????????????
  5193. (if (or comment-p emph-p link-p keyw-p)
  5194. t
  5195. (put-text-property (match-beginning 3) (match-end 0)
  5196. 'display
  5197. (if (equal (char-after (match-beginning 2)) ?^)
  5198. (nth (if table-p 3 1) org-script-display)
  5199. (nth (if table-p 2 0) org-script-display)))
  5200. (add-text-properties (match-beginning 2) (match-end 2)
  5201. (list 'invisible t
  5202. 'org-dwidth t 'org-dwidth-n 1))
  5203. (if (and (eq (char-after (match-beginning 3)) ?{)
  5204. (eq (char-before (match-end 3)) ?}))
  5205. (progn
  5206. (add-text-properties
  5207. (match-beginning 3) (1+ (match-beginning 3))
  5208. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5209. (add-text-properties
  5210. (1- (match-end 3)) (match-end 3)
  5211. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5212. t)))))
  5213. ;;;; Visibility cycling, including org-goto and indirect buffer
  5214. ;;; Cycling
  5215. (defvar org-cycle-global-status nil)
  5216. (make-variable-buffer-local 'org-cycle-global-status)
  5217. (defvar org-cycle-subtree-status nil)
  5218. (make-variable-buffer-local 'org-cycle-subtree-status)
  5219. ;;;###autoload
  5220. (defvar org-inlinetask-min-level)
  5221. (defun org-cycle (&optional arg)
  5222. "TAB-action and visibility cycling for Org-mode.
  5223. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5224. is outline visibility cycling, but it also invokes other actions
  5225. in special contexts.
  5226. - When this function is called with a prefix argument, rotate the entire
  5227. buffer through 3 states (global cycling)
  5228. 1. OVERVIEW: Show only top-level headlines.
  5229. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5230. 3. SHOW ALL: Show everything.
  5231. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5232. determined by the variable `org-startup-folded', and by any VISIBILITY
  5233. properties in the buffer.
  5234. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5235. including any drawers.
  5236. - When inside a table, re-align the table and move to the next field.
  5237. - When point is at the beginning of a headline, rotate the subtree started
  5238. by this line through 3 different states (local cycling)
  5239. 1. FOLDED: Only the main headline is shown.
  5240. 2. CHILDREN: The main headline and the direct children are shown.
  5241. From this state, you can move to one of the children
  5242. and zoom in further.
  5243. 3. SUBTREE: Show the entire subtree, including body text.
  5244. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5245. - When point is at the beginning of an empty headline and the variable
  5246. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5247. of the headline by demoting and promoting it to likely levels. This
  5248. speeds up creation document structure by pressing TAB once or several
  5249. times right after creating a new headline.
  5250. - When there is a numeric prefix, go up to a heading with level ARG, do
  5251. a `show-subtree' and return to the previous cursor position. If ARG
  5252. is negative, go up that many levels.
  5253. - When point is not at the beginning of a headline, execute the global
  5254. binding for TAB, which is re-indenting the line. See the option
  5255. `org-cycle-emulate-tab' for details.
  5256. - Special case: if point is at the beginning of the buffer and there is
  5257. no headline in line 1, this function will act as if called with prefix arg
  5258. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5259. But only if also the variable `org-cycle-global-at-bob' is t."
  5260. (interactive "P")
  5261. (org-load-modules-maybe)
  5262. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5263. (and org-cycle-level-after-item/entry-creation
  5264. (or (org-cycle-level)
  5265. (org-cycle-item-indentation))))
  5266. (let* ((limit-level
  5267. (or org-cycle-max-level
  5268. (and (boundp 'org-inlinetask-min-level)
  5269. org-inlinetask-min-level
  5270. (1- org-inlinetask-min-level))))
  5271. (nstars (and limit-level
  5272. (if org-odd-levels-only
  5273. (and limit-level (1- (* limit-level 2)))
  5274. limit-level)))
  5275. (org-outline-regexp
  5276. (if (not (org-mode-p))
  5277. outline-regexp
  5278. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5279. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5280. (not (looking-at org-outline-regexp))))
  5281. (org-cycle-hook
  5282. (if bob-special
  5283. (delq 'org-optimize-window-after-visibility-change
  5284. (copy-sequence org-cycle-hook))
  5285. org-cycle-hook))
  5286. (pos (point)))
  5287. (if (or bob-special (equal arg '(4)))
  5288. ;; special case: use global cycling
  5289. (setq arg t))
  5290. (cond
  5291. ((equal arg '(16))
  5292. (setq last-command 'dummy)
  5293. (org-set-startup-visibility)
  5294. (message "Startup visibility, plus VISIBILITY properties"))
  5295. ((equal arg '(64))
  5296. (show-all)
  5297. (message "Entire buffer visible, including drawers"))
  5298. ;; Table: enter it or move to the next field.
  5299. ((org-at-table-p 'any)
  5300. (if (org-at-table.el-p)
  5301. (message "Use C-c ' to edit table.el tables")
  5302. (if arg (org-table-edit-field t)
  5303. (org-table-justify-field-maybe)
  5304. (call-interactively 'org-table-next-field))))
  5305. ((run-hook-with-args-until-success
  5306. 'org-tab-after-check-for-table-hook))
  5307. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5308. ((eq arg t) (org-cycle-internal-global))
  5309. ;; Drawers: delegate to `org-flag-drawer'.
  5310. ((and org-drawers org-drawer-regexp
  5311. (save-excursion
  5312. (beginning-of-line 1)
  5313. (looking-at org-drawer-regexp)))
  5314. (org-flag-drawer ; toggle block visibility
  5315. (not (get-char-property (match-end 0) 'invisible))))
  5316. ;; Show-subtree, ARG levels up from here.
  5317. ((integerp arg)
  5318. (save-excursion
  5319. (org-back-to-heading)
  5320. (outline-up-heading (if (< arg 0) (- arg)
  5321. (- (funcall outline-level) arg)))
  5322. (org-show-subtree)))
  5323. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5324. ((and (featurep 'org-inlinetask)
  5325. (org-inlinetask-at-task-p)
  5326. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5327. (org-inlinetask-toggle-visibility))
  5328. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5329. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5330. (save-excursion (beginning-of-line 1)
  5331. (looking-at org-outline-regexp)))
  5332. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5333. (org-cycle-internal-local))
  5334. ;; From there: TAB emulation and template completion.
  5335. (buffer-read-only (org-back-to-heading))
  5336. ((run-hook-with-args-until-success
  5337. 'org-tab-after-check-for-cycling-hook))
  5338. ((org-try-structure-completion))
  5339. ((org-try-cdlatex-tab))
  5340. ((run-hook-with-args-until-success
  5341. 'org-tab-before-tab-emulation-hook))
  5342. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5343. (or (not (bolp))
  5344. (not (looking-at org-outline-regexp))))
  5345. (call-interactively (global-key-binding "\t")))
  5346. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5347. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5348. (or (and (eq org-cycle-emulate-tab 'white)
  5349. (= (match-end 0) (point-at-eol)))
  5350. (and (eq org-cycle-emulate-tab 'whitestart)
  5351. (>= (match-end 0) pos))))
  5352. t
  5353. (eq org-cycle-emulate-tab t))
  5354. (call-interactively (global-key-binding "\t")))
  5355. (t (save-excursion
  5356. (org-back-to-heading)
  5357. (org-cycle)))))))
  5358. (defun org-cycle-internal-global ()
  5359. "Do the global cycling action."
  5360. (cond
  5361. ((and (eq last-command this-command)
  5362. (eq org-cycle-global-status 'overview))
  5363. ;; We just created the overview - now do table of contents
  5364. ;; This can be slow in very large buffers, so indicate action
  5365. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5366. (message "CONTENTS...")
  5367. (org-content)
  5368. (message "CONTENTS...done")
  5369. (setq org-cycle-global-status 'contents)
  5370. (run-hook-with-args 'org-cycle-hook 'contents))
  5371. ((and (eq last-command this-command)
  5372. (eq org-cycle-global-status 'contents))
  5373. ;; We just showed the table of contents - now show everything
  5374. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5375. (show-all)
  5376. (message "SHOW ALL")
  5377. (setq org-cycle-global-status 'all)
  5378. (run-hook-with-args 'org-cycle-hook 'all))
  5379. (t
  5380. ;; Default action: go to overview
  5381. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5382. (org-overview)
  5383. (message "OVERVIEW")
  5384. (setq org-cycle-global-status 'overview)
  5385. (run-hook-with-args 'org-cycle-hook 'overview))))
  5386. (defun org-cycle-internal-local ()
  5387. "Do the local cycling action."
  5388. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5389. ;; First, determine end of headline (EOH), end of subtree or item
  5390. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5391. (save-excursion
  5392. (if (org-at-item-p)
  5393. (progn
  5394. (beginning-of-line)
  5395. (setq struct (org-list-struct))
  5396. (setq eoh (point-at-eol))
  5397. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5398. (setq has-children (org-list-has-child-p (point) struct)))
  5399. (org-back-to-heading)
  5400. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5401. (setq eos (save-excursion
  5402. (org-end-of-subtree t)
  5403. (unless (eobp)
  5404. (skip-chars-forward " \t\n"))
  5405. (if (eobp) (point) (1- (point)))))
  5406. (setq has-children
  5407. (or (save-excursion
  5408. (let ((level (funcall outline-level)))
  5409. (outline-next-heading)
  5410. (and (org-at-heading-p t)
  5411. (> (funcall outline-level) level))))
  5412. (save-excursion
  5413. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5414. ;; Determine end invisible part of buffer (EOL)
  5415. (beginning-of-line 2)
  5416. ;; XEmacs doesn't have `next-single-char-property-change'
  5417. (if (featurep 'xemacs)
  5418. (while (and (not (eobp)) ;; this is like `next-line'
  5419. (get-char-property (1- (point)) 'invisible))
  5420. (beginning-of-line 2))
  5421. (while (and (not (eobp)) ;; this is like `next-line'
  5422. (get-char-property (1- (point)) 'invisible))
  5423. (goto-char (next-single-char-property-change (point) 'invisible))
  5424. (and (eolp) (beginning-of-line 2))))
  5425. (setq eol (point)))
  5426. ;; Find out what to do next and set `this-command'
  5427. (cond
  5428. ((= eos eoh)
  5429. ;; Nothing is hidden behind this heading
  5430. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5431. (message "EMPTY ENTRY")
  5432. (setq org-cycle-subtree-status nil)
  5433. (save-excursion
  5434. (goto-char eos)
  5435. (outline-next-heading)
  5436. (if (outline-invisible-p) (org-flag-heading nil))))
  5437. ((and (or (>= eol eos)
  5438. (not (string-match "\\S-" (buffer-substring eol eos))))
  5439. (or has-children
  5440. (not (setq children-skipped
  5441. org-cycle-skip-children-state-if-no-children))))
  5442. ;; Entire subtree is hidden in one line: children view
  5443. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5444. (if (org-at-item-p)
  5445. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5446. (org-show-entry)
  5447. (show-children)
  5448. ;; Fold every list in subtree to top-level items.
  5449. (when (eq org-cycle-include-plain-lists 'integrate)
  5450. (save-excursion
  5451. (org-back-to-heading)
  5452. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5453. (beginning-of-line 1)
  5454. (let* ((struct (org-list-struct))
  5455. (prevs (org-list-prevs-alist struct))
  5456. (end (org-list-get-bottom-point struct)))
  5457. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5458. (org-list-get-all-items (point) struct prevs))
  5459. (goto-char end))))))
  5460. (message "CHILDREN")
  5461. (save-excursion
  5462. (goto-char eos)
  5463. (outline-next-heading)
  5464. (if (outline-invisible-p) (org-flag-heading nil)))
  5465. (setq org-cycle-subtree-status 'children)
  5466. (run-hook-with-args 'org-cycle-hook 'children))
  5467. ((or children-skipped
  5468. (and (eq last-command this-command)
  5469. (eq org-cycle-subtree-status 'children)))
  5470. ;; We just showed the children, or no children are there,
  5471. ;; now show everything.
  5472. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5473. (outline-flag-region eoh eos nil)
  5474. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5475. (setq org-cycle-subtree-status 'subtree)
  5476. (run-hook-with-args 'org-cycle-hook 'subtree))
  5477. (t
  5478. ;; Default action: hide the subtree.
  5479. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5480. (outline-flag-region eoh eos t)
  5481. (message "FOLDED")
  5482. (setq org-cycle-subtree-status 'folded)
  5483. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5484. ;;;###autoload
  5485. (defun org-global-cycle (&optional arg)
  5486. "Cycle the global visibility. For details see `org-cycle'.
  5487. With \\[universal-argument] prefix arg, switch to startup visibility.
  5488. With a numeric prefix, show all headlines up to that level."
  5489. (interactive "P")
  5490. (let ((org-cycle-include-plain-lists
  5491. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5492. (cond
  5493. ((integerp arg)
  5494. (show-all)
  5495. (hide-sublevels arg)
  5496. (setq org-cycle-global-status 'contents))
  5497. ((equal arg '(4))
  5498. (org-set-startup-visibility)
  5499. (message "Startup visibility, plus VISIBILITY properties."))
  5500. (t
  5501. (org-cycle '(4))))))
  5502. (defun org-set-startup-visibility ()
  5503. "Set the visibility required by startup options and properties."
  5504. (cond
  5505. ((eq org-startup-folded t)
  5506. (org-cycle '(4)))
  5507. ((eq org-startup-folded 'content)
  5508. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5509. (org-cycle '(4)) (org-cycle '(4)))))
  5510. (unless (eq org-startup-folded 'showeverything)
  5511. (if org-hide-block-startup (org-hide-block-all))
  5512. (org-set-visibility-according-to-property 'no-cleanup)
  5513. (org-cycle-hide-archived-subtrees 'all)
  5514. (org-cycle-hide-drawers 'all)
  5515. (org-cycle-show-empty-lines t)))
  5516. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5517. "Switch subtree visibilities according to :VISIBILITY: property."
  5518. (interactive)
  5519. (let (org-show-entry-below state)
  5520. (save-excursion
  5521. (goto-char (point-min))
  5522. (while (re-search-forward
  5523. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5524. nil t)
  5525. (setq state (match-string 1))
  5526. (save-excursion
  5527. (org-back-to-heading t)
  5528. (hide-subtree)
  5529. (org-reveal)
  5530. (cond
  5531. ((equal state '("fold" "folded"))
  5532. (hide-subtree))
  5533. ((equal state "children")
  5534. (org-show-hidden-entry)
  5535. (show-children))
  5536. ((equal state "content")
  5537. (save-excursion
  5538. (save-restriction
  5539. (org-narrow-to-subtree)
  5540. (org-content))))
  5541. ((member state '("all" "showall"))
  5542. (show-subtree)))))
  5543. (unless no-cleanup
  5544. (org-cycle-hide-archived-subtrees 'all)
  5545. (org-cycle-hide-drawers 'all)
  5546. (org-cycle-show-empty-lines 'all)))))
  5547. (defun org-overview ()
  5548. "Switch to overview mode, showing only top-level headlines.
  5549. Really, this shows all headlines with level equal or greater than the level
  5550. of the first headline in the buffer. This is important, because if the
  5551. first headline is not level one, then (hide-sublevels 1) gives confusing
  5552. results."
  5553. (interactive)
  5554. (let ((level (save-excursion
  5555. (goto-char (point-min))
  5556. (if (re-search-forward org-outline-regexp-bol nil t)
  5557. (progn
  5558. (goto-char (match-beginning 0))
  5559. (funcall outline-level))))))
  5560. (and level (hide-sublevels level))))
  5561. (defun org-content (&optional arg)
  5562. "Show all headlines in the buffer, like a table of contents.
  5563. With numerical argument N, show content up to level N."
  5564. (interactive "P")
  5565. (save-excursion
  5566. ;; Visit all headings and show their offspring
  5567. (and (integerp arg) (org-overview))
  5568. (goto-char (point-max))
  5569. (catch 'exit
  5570. (while (and (progn (condition-case nil
  5571. (outline-previous-visible-heading 1)
  5572. (error (goto-char (point-min))))
  5573. t)
  5574. (looking-at org-outline-regexp))
  5575. (if (integerp arg)
  5576. (show-children (1- arg))
  5577. (show-branches))
  5578. (if (bobp) (throw 'exit nil))))))
  5579. (defun org-optimize-window-after-visibility-change (state)
  5580. "Adjust the window after a change in outline visibility.
  5581. This function is the default value of the hook `org-cycle-hook'."
  5582. (when (get-buffer-window (current-buffer))
  5583. (cond
  5584. ((eq state 'content) nil)
  5585. ((eq state 'all) nil)
  5586. ((eq state 'folded) nil)
  5587. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5588. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5589. (defun org-remove-empty-overlays-at (pos)
  5590. "Remove outline overlays that do not contain non-white stuff."
  5591. (mapc
  5592. (lambda (o)
  5593. (and (eq 'outline (overlay-get o 'invisible))
  5594. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5595. (overlay-end o))))
  5596. (delete-overlay o)))
  5597. (overlays-at pos)))
  5598. (defun org-clean-visibility-after-subtree-move ()
  5599. "Fix visibility issues after moving a subtree."
  5600. ;; First, find a reasonable region to look at:
  5601. ;; Start two siblings above, end three below
  5602. (let* ((beg (save-excursion
  5603. (and (org-get-last-sibling)
  5604. (org-get-last-sibling))
  5605. (point)))
  5606. (end (save-excursion
  5607. (and (org-get-next-sibling)
  5608. (org-get-next-sibling)
  5609. (org-get-next-sibling))
  5610. (if (org-at-heading-p)
  5611. (point-at-eol)
  5612. (point))))
  5613. (level (looking-at "\\*+"))
  5614. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5615. (save-excursion
  5616. (save-restriction
  5617. (narrow-to-region beg end)
  5618. (when re
  5619. ;; Properly fold already folded siblings
  5620. (goto-char (point-min))
  5621. (while (re-search-forward re nil t)
  5622. (if (and (not (outline-invisible-p))
  5623. (save-excursion
  5624. (goto-char (point-at-eol)) (outline-invisible-p)))
  5625. (hide-entry))))
  5626. (org-cycle-show-empty-lines 'overview)
  5627. (org-cycle-hide-drawers 'overview)))))
  5628. (defun org-cycle-show-empty-lines (state)
  5629. "Show empty lines above all visible headlines.
  5630. The region to be covered depends on STATE when called through
  5631. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5632. entire buffer covered. Note that an empty line is only shown if there
  5633. are at least `org-cycle-separator-lines' empty lines before the headline."
  5634. (when (not (= org-cycle-separator-lines 0))
  5635. (save-excursion
  5636. (let* ((n (abs org-cycle-separator-lines))
  5637. (re (cond
  5638. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5639. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5640. (t (let ((ns (number-to-string (- n 2))))
  5641. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5642. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5643. beg end b e)
  5644. (cond
  5645. ((memq state '(overview contents t))
  5646. (setq beg (point-min) end (point-max)))
  5647. ((memq state '(children folded))
  5648. (setq beg (point) end (progn (org-end-of-subtree t t)
  5649. (beginning-of-line 2)
  5650. (point)))))
  5651. (when beg
  5652. (goto-char beg)
  5653. (while (re-search-forward re end t)
  5654. (unless (get-char-property (match-end 1) 'invisible)
  5655. (setq e (match-end 1))
  5656. (if (< org-cycle-separator-lines 0)
  5657. (setq b (save-excursion
  5658. (goto-char (match-beginning 0))
  5659. (org-back-over-empty-lines)
  5660. (if (save-excursion
  5661. (goto-char (max (point-min) (1- (point))))
  5662. (org-on-heading-p))
  5663. (1- (point))
  5664. (point))))
  5665. (setq b (match-beginning 1)))
  5666. (outline-flag-region b e nil)))))))
  5667. ;; Never hide empty lines at the end of the file.
  5668. (save-excursion
  5669. (goto-char (point-max))
  5670. (outline-previous-heading)
  5671. (outline-end-of-heading)
  5672. (if (and (looking-at "[ \t\n]+")
  5673. (= (match-end 0) (point-max)))
  5674. (outline-flag-region (point) (match-end 0) nil))))
  5675. (defun org-show-empty-lines-in-parent ()
  5676. "Move to the parent and re-show empty lines before visible headlines."
  5677. (save-excursion
  5678. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5679. (org-cycle-show-empty-lines context))))
  5680. (defun org-files-list ()
  5681. "Return `org-agenda-files' list, plus all open org-mode files.
  5682. This is useful for operations that need to scan all of a user's
  5683. open and agenda-wise Org files."
  5684. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5685. (dolist (buf (buffer-list))
  5686. (with-current-buffer buf
  5687. (if (and (org-mode-p) (buffer-file-name))
  5688. (let ((file (expand-file-name (buffer-file-name))))
  5689. (unless (member file files)
  5690. (push file files))))))
  5691. files))
  5692. (defsubst org-entry-beginning-position ()
  5693. "Return the beginning position of the current entry."
  5694. (save-excursion (outline-back-to-heading t) (point)))
  5695. (defsubst org-entry-end-position ()
  5696. "Return the end position of the current entry."
  5697. (save-excursion (outline-next-heading) (point)))
  5698. (defun org-cycle-hide-drawers (state)
  5699. "Re-hide all drawers after a visibility state change."
  5700. (when (and (org-mode-p)
  5701. (not (memq state '(overview folded contents))))
  5702. (save-excursion
  5703. (let* ((globalp (memq state '(contents all)))
  5704. (beg (if globalp (point-min) (point)))
  5705. (end (if globalp (point-max)
  5706. (if (eq state 'children)
  5707. (save-excursion (outline-next-heading) (point))
  5708. (org-end-of-subtree t)))))
  5709. (goto-char beg)
  5710. (while (re-search-forward org-drawer-regexp end t)
  5711. (org-flag-drawer t))))))
  5712. (defun org-flag-drawer (flag)
  5713. (save-excursion
  5714. (beginning-of-line 1)
  5715. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5716. (let ((b (match-end 0)))
  5717. (if (re-search-forward
  5718. "^[ \t]*:END:"
  5719. (save-excursion (outline-next-heading) (point)) t)
  5720. (outline-flag-region b (point-at-eol) flag)
  5721. (error ":END: line missing at position %s" b))))))
  5722. (defun org-subtree-end-visible-p ()
  5723. "Is the end of the current subtree visible?"
  5724. (pos-visible-in-window-p
  5725. (save-excursion (org-end-of-subtree t) (point))))
  5726. (defun org-first-headline-recenter (&optional N)
  5727. "Move cursor to the first headline and recenter the headline.
  5728. Optional argument N means put the headline into the Nth line of the window."
  5729. (goto-char (point-min))
  5730. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5731. (beginning-of-line)
  5732. (recenter (prefix-numeric-value N))))
  5733. ;;; Saving and restoring visibility
  5734. (defun org-outline-overlay-data (&optional use-markers)
  5735. "Return a list of the locations of all outline overlays.
  5736. These are overlays with the `invisible' property value `outline'.
  5737. The return value is a list of cons cells, with start and stop
  5738. positions for each overlay.
  5739. If USE-MARKERS is set, return the positions as markers."
  5740. (let (beg end)
  5741. (save-excursion
  5742. (save-restriction
  5743. (widen)
  5744. (delq nil
  5745. (mapcar (lambda (o)
  5746. (when (eq (overlay-get o 'invisible) 'outline)
  5747. (setq beg (overlay-start o)
  5748. end (overlay-end o))
  5749. (and beg end (> end beg)
  5750. (if use-markers
  5751. (cons (move-marker (make-marker) beg)
  5752. (move-marker (make-marker) end))
  5753. (cons beg end)))))
  5754. (overlays-in (point-min) (point-max))))))))
  5755. (defun org-set-outline-overlay-data (data)
  5756. "Create visibility overlays for all positions in DATA.
  5757. DATA should have been made by `org-outline-overlay-data'."
  5758. (let (o)
  5759. (save-excursion
  5760. (save-restriction
  5761. (widen)
  5762. (show-all)
  5763. (mapc (lambda (c)
  5764. (setq o (make-overlay (car c) (cdr c)))
  5765. (overlay-put o 'invisible 'outline))
  5766. data)))))
  5767. ;;; Folding of blocks
  5768. (defconst org-block-regexp
  5769. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  5770. "Regular expression for hiding blocks.")
  5771. (defvar org-hide-block-overlays nil
  5772. "Overlays hiding blocks.")
  5773. (make-variable-buffer-local 'org-hide-block-overlays)
  5774. (defun org-block-map (function &optional start end)
  5775. "Call FUNCTION at the head of all source blocks in the current buffer.
  5776. Optional arguments START and END can be used to limit the range."
  5777. (let ((start (or start (point-min)))
  5778. (end (or end (point-max))))
  5779. (save-excursion
  5780. (goto-char start)
  5781. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5782. (save-excursion
  5783. (save-match-data
  5784. (goto-char (match-beginning 0))
  5785. (funcall function)))))))
  5786. (defun org-hide-block-toggle-all ()
  5787. "Toggle the visibility of all blocks in the current buffer."
  5788. (org-block-map #'org-hide-block-toggle))
  5789. (defun org-hide-block-all ()
  5790. "Fold all blocks in the current buffer."
  5791. (interactive)
  5792. (org-show-block-all)
  5793. (org-block-map #'org-hide-block-toggle-maybe))
  5794. (defun org-show-block-all ()
  5795. "Unfold all blocks in the current buffer."
  5796. (interactive)
  5797. (mapc 'delete-overlay org-hide-block-overlays)
  5798. (setq org-hide-block-overlays nil))
  5799. (defun org-hide-block-toggle-maybe ()
  5800. "Toggle visibility of block at point."
  5801. (interactive)
  5802. (let ((case-fold-search t))
  5803. (if (save-excursion
  5804. (beginning-of-line 1)
  5805. (looking-at org-block-regexp))
  5806. (progn (org-hide-block-toggle)
  5807. t) ;; to signal that we took action
  5808. nil))) ;; to signal that we did not
  5809. (defun org-hide-block-toggle (&optional force)
  5810. "Toggle the visibility of the current block."
  5811. (interactive)
  5812. (save-excursion
  5813. (beginning-of-line)
  5814. (if (re-search-forward org-block-regexp nil t)
  5815. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5816. (end (match-end 0)) ;; end of entire body
  5817. ov)
  5818. (if (memq t (mapcar (lambda (overlay)
  5819. (eq (overlay-get overlay 'invisible)
  5820. 'org-hide-block))
  5821. (overlays-at start)))
  5822. (if (or (not force) (eq force 'off))
  5823. (mapc (lambda (ov)
  5824. (when (member ov org-hide-block-overlays)
  5825. (setq org-hide-block-overlays
  5826. (delq ov org-hide-block-overlays)))
  5827. (when (eq (overlay-get ov 'invisible)
  5828. 'org-hide-block)
  5829. (delete-overlay ov)))
  5830. (overlays-at start)))
  5831. (setq ov (make-overlay start end))
  5832. (overlay-put ov 'invisible 'org-hide-block)
  5833. ;; make the block accessible to isearch
  5834. (overlay-put
  5835. ov 'isearch-open-invisible
  5836. (lambda (ov)
  5837. (when (member ov org-hide-block-overlays)
  5838. (setq org-hide-block-overlays
  5839. (delq ov org-hide-block-overlays)))
  5840. (when (eq (overlay-get ov 'invisible)
  5841. 'org-hide-block)
  5842. (delete-overlay ov))))
  5843. (push ov org-hide-block-overlays)))
  5844. (error "Not looking at a source block"))))
  5845. ;; org-tab-after-check-for-cycling-hook
  5846. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5847. ;; Remove overlays when changing major mode
  5848. (add-hook 'org-mode-hook
  5849. (lambda () (org-add-hook 'change-major-mode-hook
  5850. 'org-show-block-all 'append 'local)))
  5851. ;;; Org-goto
  5852. (defvar org-goto-window-configuration nil)
  5853. (defvar org-goto-marker nil)
  5854. (defvar org-goto-map
  5855. (let ((map (make-sparse-keymap)))
  5856. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5857. (while (setq cmd (pop cmds))
  5858. (substitute-key-definition cmd cmd map global-map)))
  5859. (suppress-keymap map)
  5860. (org-defkey map "\C-m" 'org-goto-ret)
  5861. (org-defkey map [(return)] 'org-goto-ret)
  5862. (org-defkey map [(left)] 'org-goto-left)
  5863. (org-defkey map [(right)] 'org-goto-right)
  5864. (org-defkey map [(control ?g)] 'org-goto-quit)
  5865. (org-defkey map "\C-i" 'org-cycle)
  5866. (org-defkey map [(tab)] 'org-cycle)
  5867. (org-defkey map [(down)] 'outline-next-visible-heading)
  5868. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5869. (if org-goto-auto-isearch
  5870. (if (fboundp 'define-key-after)
  5871. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5872. nil)
  5873. (org-defkey map "q" 'org-goto-quit)
  5874. (org-defkey map "n" 'outline-next-visible-heading)
  5875. (org-defkey map "p" 'outline-previous-visible-heading)
  5876. (org-defkey map "f" 'outline-forward-same-level)
  5877. (org-defkey map "b" 'outline-backward-same-level)
  5878. (org-defkey map "u" 'outline-up-heading))
  5879. (org-defkey map "/" 'org-occur)
  5880. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5881. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5882. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5883. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5884. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5885. map))
  5886. (defconst org-goto-help
  5887. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5888. RET=jump to location [Q]uit and return to previous location
  5889. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5890. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5891. ;; FIXME: Docstring does not mention both interfaces
  5892. (defun org-goto (&optional alternative-interface)
  5893. "Look up a different location in the current file, keeping current visibility.
  5894. When you want look-up or go to a different location in a document, the
  5895. fastest way is often to fold the entire buffer and then dive into the tree.
  5896. This method has the disadvantage, that the previous location will be folded,
  5897. which may not be what you want.
  5898. This command works around this by showing a copy of the current buffer
  5899. in an indirect buffer, in overview mode. You can dive into the tree in
  5900. that copy, use org-occur and incremental search to find a location.
  5901. When pressing RET or `Q', the command returns to the original buffer in
  5902. which the visibility is still unchanged. After RET is will also jump to
  5903. the location selected in the indirect buffer and expose the headline
  5904. hierarchy above."
  5905. (interactive "P")
  5906. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5907. (org-refile-use-outline-path t)
  5908. (org-refile-target-verify-function nil)
  5909. (interface
  5910. (if (not alternative-interface)
  5911. org-goto-interface
  5912. (if (eq org-goto-interface 'outline)
  5913. 'outline-path-completion
  5914. 'outline)))
  5915. (org-goto-start-pos (point))
  5916. (selected-point
  5917. (if (eq interface 'outline)
  5918. (car (org-get-location (current-buffer) org-goto-help))
  5919. (let ((pa (org-refile-get-location "Goto")))
  5920. (org-refile-check-position pa)
  5921. (nth 3 pa)))))
  5922. (if selected-point
  5923. (progn
  5924. (org-mark-ring-push org-goto-start-pos)
  5925. (goto-char selected-point)
  5926. (if (or (outline-invisible-p) (org-invisible-p2))
  5927. (org-show-context 'org-goto)))
  5928. (message "Quit"))))
  5929. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5930. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5931. (defvar org-goto-local-auto-isearch-map) ; defined below
  5932. (defun org-get-location (buf help)
  5933. "Let the user select a location in the Org-mode buffer BUF.
  5934. This function uses a recursive edit. It returns the selected position
  5935. or nil."
  5936. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5937. (isearch-hide-immediately nil)
  5938. (isearch-search-fun-function
  5939. (lambda () 'org-goto-local-search-headings))
  5940. (org-goto-selected-point org-goto-exit-command)
  5941. (pop-up-frames nil)
  5942. (special-display-buffer-names nil)
  5943. (special-display-regexps nil)
  5944. (special-display-function nil))
  5945. (save-excursion
  5946. (save-window-excursion
  5947. (delete-other-windows)
  5948. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5949. (switch-to-buffer
  5950. (condition-case nil
  5951. (make-indirect-buffer (current-buffer) "*org-goto*")
  5952. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5953. (with-output-to-temp-buffer "*Help*"
  5954. (princ help))
  5955. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5956. (setq buffer-read-only nil)
  5957. (let ((org-startup-truncated t)
  5958. (org-startup-folded nil)
  5959. (org-startup-align-all-tables nil))
  5960. (org-mode)
  5961. (org-overview))
  5962. (setq buffer-read-only t)
  5963. (if (and (boundp 'org-goto-start-pos)
  5964. (integer-or-marker-p org-goto-start-pos))
  5965. (let ((org-show-hierarchy-above t)
  5966. (org-show-siblings t)
  5967. (org-show-following-heading t))
  5968. (goto-char org-goto-start-pos)
  5969. (and (outline-invisible-p) (org-show-context)))
  5970. (goto-char (point-min)))
  5971. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5972. (message "Select location and press RET")
  5973. (use-local-map org-goto-map)
  5974. (recursive-edit)
  5975. ))
  5976. (kill-buffer "*org-goto*")
  5977. (cons org-goto-selected-point org-goto-exit-command)))
  5978. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5979. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5980. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5981. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5982. (defun org-goto-local-search-headings (string bound noerror)
  5983. "Search and make sure that any matches are in headlines."
  5984. (catch 'return
  5985. (while (if isearch-forward
  5986. (search-forward string bound noerror)
  5987. (search-backward string bound noerror))
  5988. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5989. (and (member :headline context)
  5990. (not (member :tags context))))
  5991. (throw 'return (point))))))
  5992. (defun org-goto-local-auto-isearch ()
  5993. "Start isearch."
  5994. (interactive)
  5995. (goto-char (point-min))
  5996. (let ((keys (this-command-keys)))
  5997. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5998. (isearch-mode t)
  5999. (isearch-process-search-char (string-to-char keys)))))
  6000. (defun org-goto-ret (&optional arg)
  6001. "Finish `org-goto' by going to the new location."
  6002. (interactive "P")
  6003. (setq org-goto-selected-point (point)
  6004. org-goto-exit-command 'return)
  6005. (throw 'exit nil))
  6006. (defun org-goto-left ()
  6007. "Finish `org-goto' by going to the new location."
  6008. (interactive)
  6009. (if (org-on-heading-p)
  6010. (progn
  6011. (beginning-of-line 1)
  6012. (setq org-goto-selected-point (point)
  6013. org-goto-exit-command 'left)
  6014. (throw 'exit nil))
  6015. (error "Not on a heading")))
  6016. (defun org-goto-right ()
  6017. "Finish `org-goto' by going to the new location."
  6018. (interactive)
  6019. (if (org-on-heading-p)
  6020. (progn
  6021. (setq org-goto-selected-point (point)
  6022. org-goto-exit-command 'right)
  6023. (throw 'exit nil))
  6024. (error "Not on a heading")))
  6025. (defun org-goto-quit ()
  6026. "Finish `org-goto' without cursor motion."
  6027. (interactive)
  6028. (setq org-goto-selected-point nil)
  6029. (setq org-goto-exit-command 'quit)
  6030. (throw 'exit nil))
  6031. ;;; Indirect buffer display of subtrees
  6032. (defvar org-indirect-dedicated-frame nil
  6033. "This is the frame being used for indirect tree display.")
  6034. (defvar org-last-indirect-buffer nil)
  6035. (defun org-tree-to-indirect-buffer (&optional arg)
  6036. "Create indirect buffer and narrow it to current subtree.
  6037. With numerical prefix ARG, go up to this level and then take that tree.
  6038. If ARG is negative, go up that many levels.
  6039. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6040. indirect buffer previously made with this command, to avoid proliferation of
  6041. indirect buffers. However, when you call the command with a \
  6042. \\[universal-argument] prefix, or
  6043. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6044. is kept so that you can work with several indirect buffers at the same time.
  6045. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6046. \\[universal-argument] prefix also
  6047. requests that a new frame be made for the new buffer, so that the dedicated
  6048. frame is not changed."
  6049. (interactive "P")
  6050. (let ((cbuf (current-buffer))
  6051. (cwin (selected-window))
  6052. (pos (point))
  6053. beg end level heading ibuf)
  6054. (save-excursion
  6055. (org-back-to-heading t)
  6056. (when (numberp arg)
  6057. (setq level (org-outline-level))
  6058. (if (< arg 0) (setq arg (+ level arg)))
  6059. (while (> (setq level (org-outline-level)) arg)
  6060. (outline-up-heading 1 t)))
  6061. (setq beg (point)
  6062. heading (org-get-heading))
  6063. (org-end-of-subtree t t)
  6064. (if (org-on-heading-p) (backward-char 1))
  6065. (setq end (point)))
  6066. (if (and (buffer-live-p org-last-indirect-buffer)
  6067. (not (eq org-indirect-buffer-display 'new-frame))
  6068. (not arg))
  6069. (kill-buffer org-last-indirect-buffer))
  6070. (setq ibuf (org-get-indirect-buffer cbuf)
  6071. org-last-indirect-buffer ibuf)
  6072. (cond
  6073. ((or (eq org-indirect-buffer-display 'new-frame)
  6074. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6075. (select-frame (make-frame))
  6076. (delete-other-windows)
  6077. (switch-to-buffer ibuf)
  6078. (org-set-frame-title heading))
  6079. ((eq org-indirect-buffer-display 'dedicated-frame)
  6080. (raise-frame
  6081. (select-frame (or (and org-indirect-dedicated-frame
  6082. (frame-live-p org-indirect-dedicated-frame)
  6083. org-indirect-dedicated-frame)
  6084. (setq org-indirect-dedicated-frame (make-frame)))))
  6085. (delete-other-windows)
  6086. (switch-to-buffer ibuf)
  6087. (org-set-frame-title (concat "Indirect: " heading)))
  6088. ((eq org-indirect-buffer-display 'current-window)
  6089. (switch-to-buffer ibuf))
  6090. ((eq org-indirect-buffer-display 'other-window)
  6091. (pop-to-buffer ibuf))
  6092. (t (error "Invalid value")))
  6093. (if (featurep 'xemacs)
  6094. (save-excursion (org-mode) (turn-on-font-lock)))
  6095. (narrow-to-region beg end)
  6096. (show-all)
  6097. (goto-char pos)
  6098. (and (window-live-p cwin) (select-window cwin))))
  6099. (defun org-get-indirect-buffer (&optional buffer)
  6100. (setq buffer (or buffer (current-buffer)))
  6101. (let ((n 1) (base (buffer-name buffer)) bname)
  6102. (while (buffer-live-p
  6103. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6104. (setq n (1+ n)))
  6105. (condition-case nil
  6106. (make-indirect-buffer buffer bname 'clone)
  6107. (error (make-indirect-buffer buffer bname)))))
  6108. (defun org-set-frame-title (title)
  6109. "Set the title of the current frame to the string TITLE."
  6110. ;; FIXME: how to name a single frame in XEmacs???
  6111. (unless (featurep 'xemacs)
  6112. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6113. ;;;; Structure editing
  6114. ;;; Inserting headlines
  6115. (defun org-previous-line-empty-p ()
  6116. (save-excursion
  6117. (and (not (bobp))
  6118. (or (beginning-of-line 0) t)
  6119. (save-match-data
  6120. (looking-at "[ \t]*$")))))
  6121. (defun org-insert-heading (&optional force-heading invisible-ok)
  6122. "Insert a new heading or item with same depth at point.
  6123. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6124. If point is at the beginning of a headline, insert a sibling before the
  6125. current headline. If point is not at the beginning, split the line,
  6126. create the new headline with the text in the current line after point
  6127. \(but see also the variable `org-M-RET-may-split-line').
  6128. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6129. This is important for non-interactive uses of the command."
  6130. (interactive "P")
  6131. (if (or (= (buffer-size) 0)
  6132. (and (not (save-excursion
  6133. (and (ignore-errors (org-back-to-heading invisible-ok))
  6134. (org-on-heading-p))))
  6135. (not (org-in-item-p))))
  6136. (progn
  6137. (insert "\n* ")
  6138. (run-hooks 'org-insert-heading-hook))
  6139. (when (or force-heading (not (org-insert-item)))
  6140. (let* ((empty-line-p nil)
  6141. (level nil)
  6142. (on-heading (org-on-heading-p))
  6143. (head (save-excursion
  6144. (condition-case nil
  6145. (progn
  6146. (org-back-to-heading invisible-ok)
  6147. (when (and (not on-heading)
  6148. (featurep 'org-inlinetask)
  6149. (integerp org-inlinetask-min-level)
  6150. (>= (length (match-string 0))
  6151. org-inlinetask-min-level))
  6152. ;; Find a heading level before the inline task
  6153. (while (and (setq level (org-up-heading-safe))
  6154. (>= level org-inlinetask-min-level)))
  6155. (if (org-on-heading-p)
  6156. (org-back-to-heading invisible-ok)
  6157. (error "This should not happen")))
  6158. (setq empty-line-p (org-previous-line-empty-p))
  6159. (match-string 0))
  6160. (error "*"))))
  6161. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6162. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6163. pos hide-previous previous-pos)
  6164. (cond
  6165. ((and (org-on-heading-p) (bolp)
  6166. (or (bobp)
  6167. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6168. ;; insert before the current line
  6169. (open-line (if blank 2 1)))
  6170. ((and (bolp)
  6171. (not org-insert-heading-respect-content)
  6172. (or (bobp)
  6173. (save-excursion
  6174. (backward-char 1) (not (outline-invisible-p)))))
  6175. ;; insert right here
  6176. nil)
  6177. (t
  6178. ;; somewhere in the line
  6179. (save-excursion
  6180. (setq previous-pos (point-at-bol))
  6181. (end-of-line)
  6182. (setq hide-previous (outline-invisible-p)))
  6183. (and org-insert-heading-respect-content (org-show-subtree))
  6184. (let ((split
  6185. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6186. (save-excursion
  6187. (let ((p (point)))
  6188. (goto-char (point-at-bol))
  6189. (and (looking-at org-complex-heading-regexp)
  6190. (> p (match-beginning 4)))))))
  6191. tags pos)
  6192. (cond
  6193. (org-insert-heading-respect-content
  6194. (org-end-of-subtree nil t)
  6195. (when (featurep 'org-inlinetask)
  6196. (while (and (not (eobp))
  6197. (looking-at "\\(\\*+\\)[ \t]+")
  6198. (>= (length (match-string 1))
  6199. org-inlinetask-min-level))
  6200. (org-end-of-subtree nil t)))
  6201. (or (bolp) (newline))
  6202. (or (org-previous-line-empty-p)
  6203. (and blank (newline)))
  6204. (open-line 1))
  6205. ((org-on-heading-p)
  6206. (when hide-previous
  6207. (show-children)
  6208. (org-show-entry))
  6209. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6210. (setq tags (and (match-end 2) (match-string 2)))
  6211. (and (match-end 1)
  6212. (delete-region (match-beginning 1) (match-end 1)))
  6213. (setq pos (point-at-bol))
  6214. (or split (end-of-line 1))
  6215. (delete-horizontal-space)
  6216. (if (string-match "\\`\\*+\\'"
  6217. (buffer-substring (point-at-bol) (point)))
  6218. (insert " "))
  6219. (newline (if blank 2 1))
  6220. (when tags
  6221. (save-excursion
  6222. (goto-char pos)
  6223. (end-of-line 1)
  6224. (insert " " tags)
  6225. (org-set-tags nil 'align))))
  6226. (t
  6227. (or split (end-of-line 1))
  6228. (newline (if blank 2 1)))))))
  6229. (insert head) (just-one-space)
  6230. (setq pos (point))
  6231. (end-of-line 1)
  6232. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6233. (when (and org-insert-heading-respect-content hide-previous)
  6234. (save-excursion
  6235. (goto-char previous-pos)
  6236. (hide-subtree)))
  6237. (run-hooks 'org-insert-heading-hook)))))
  6238. (defun org-get-heading (&optional no-tags)
  6239. "Return the heading of the current entry, without the stars."
  6240. (save-excursion
  6241. (org-back-to-heading t)
  6242. (if (looking-at
  6243. (if no-tags
  6244. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6245. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6246. (match-string 1) "")))
  6247. (defun org-heading-components ()
  6248. "Return the components of the current heading.
  6249. This is a list with the following elements:
  6250. - the level as an integer
  6251. - the reduced level, different if `org-odd-levels-only' is set.
  6252. - the TODO keyword, or nil
  6253. - the priority character, like ?A, or nil if no priority is given
  6254. - the headline text itself, or the tags string if no headline text
  6255. - the tags string, or nil."
  6256. (save-excursion
  6257. (org-back-to-heading t)
  6258. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6259. (list (length (match-string 1))
  6260. (org-reduced-level (length (match-string 1)))
  6261. (org-match-string-no-properties 2)
  6262. (and (match-end 3) (aref (match-string 3) 2))
  6263. (org-match-string-no-properties 4)
  6264. (org-match-string-no-properties 5)))))
  6265. (defun org-get-entry ()
  6266. "Get the entry text, after heading, entire subtree."
  6267. (save-excursion
  6268. (org-back-to-heading t)
  6269. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6270. (defun org-insert-heading-after-current ()
  6271. "Insert a new heading with same level as current, after current subtree."
  6272. (interactive)
  6273. (org-back-to-heading)
  6274. (org-insert-heading)
  6275. (org-move-subtree-down)
  6276. (end-of-line 1))
  6277. (defun org-insert-heading-respect-content ()
  6278. (interactive)
  6279. (let ((org-insert-heading-respect-content t))
  6280. (org-insert-heading t)))
  6281. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6282. (interactive "P")
  6283. (let ((org-insert-heading-respect-content t))
  6284. (org-insert-todo-heading force-state t)))
  6285. (defun org-insert-todo-heading (arg &optional force-heading)
  6286. "Insert a new heading with the same level and TODO state as current heading.
  6287. If the heading has no TODO state, or if the state is DONE, use the first
  6288. state (TODO by default). Also with prefix arg, force first state."
  6289. (interactive "P")
  6290. (when (or force-heading (not (org-insert-item 'checkbox)))
  6291. (org-insert-heading force-heading)
  6292. (save-excursion
  6293. (org-back-to-heading)
  6294. (outline-previous-heading)
  6295. (looking-at org-todo-line-regexp))
  6296. (let*
  6297. ((new-mark-x
  6298. (if (or arg
  6299. (not (match-beginning 2))
  6300. (member (match-string 2) org-done-keywords))
  6301. (car org-todo-keywords-1)
  6302. (match-string 2)))
  6303. (new-mark
  6304. (or
  6305. (run-hook-with-args-until-success
  6306. 'org-todo-get-default-hook new-mark-x nil)
  6307. new-mark-x)))
  6308. (beginning-of-line 1)
  6309. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6310. (if org-treat-insert-todo-heading-as-state-change
  6311. (org-todo new-mark)
  6312. (insert new-mark " "))))
  6313. (when org-provide-todo-statistics
  6314. (org-update-parent-todo-statistics))))
  6315. (defun org-insert-subheading (arg)
  6316. "Insert a new subheading and demote it.
  6317. Works for outline headings and for plain lists alike."
  6318. (interactive "P")
  6319. (org-insert-heading arg)
  6320. (cond
  6321. ((org-on-heading-p) (org-do-demote))
  6322. ((org-at-item-p) (org-indent-item))))
  6323. (defun org-insert-todo-subheading (arg)
  6324. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6325. Works for outline headings and for plain lists alike."
  6326. (interactive "P")
  6327. (org-insert-todo-heading arg)
  6328. (cond
  6329. ((org-on-heading-p) (org-do-demote))
  6330. ((org-at-item-p) (org-indent-item))))
  6331. ;;; Promotion and Demotion
  6332. (defvar org-after-demote-entry-hook nil
  6333. "Hook run after an entry has been demoted.
  6334. The cursor will be at the beginning of the entry.
  6335. When a subtree is being demoted, the hook will be called for each node.")
  6336. (defvar org-after-promote-entry-hook nil
  6337. "Hook run after an entry has been promoted.
  6338. The cursor will be at the beginning of the entry.
  6339. When a subtree is being promoted, the hook will be called for each node.")
  6340. (defun org-promote-subtree ()
  6341. "Promote the entire subtree.
  6342. See also `org-promote'."
  6343. (interactive)
  6344. (save-excursion
  6345. (org-with-limited-levels (org-map-tree 'org-promote)))
  6346. (org-fix-position-after-promote))
  6347. (defun org-demote-subtree ()
  6348. "Demote the entire subtree. See `org-demote'.
  6349. See also `org-promote'."
  6350. (interactive)
  6351. (save-excursion
  6352. (org-with-limited-levels (org-map-tree 'org-demote)))
  6353. (org-fix-position-after-promote))
  6354. (defun org-do-promote ()
  6355. "Promote the current heading higher up the tree.
  6356. If the region is active in `transient-mark-mode', promote all headings
  6357. in the region."
  6358. (interactive)
  6359. (save-excursion
  6360. (if (org-region-active-p)
  6361. (org-map-region 'org-promote (region-beginning) (region-end))
  6362. (org-promote)))
  6363. (org-fix-position-after-promote))
  6364. (defun org-do-demote ()
  6365. "Demote the current heading lower down the tree.
  6366. If the region is active in `transient-mark-mode', demote all headings
  6367. in the region."
  6368. (interactive)
  6369. (save-excursion
  6370. (if (org-region-active-p)
  6371. (org-map-region 'org-demote (region-beginning) (region-end))
  6372. (org-demote)))
  6373. (org-fix-position-after-promote))
  6374. (defun org-fix-position-after-promote ()
  6375. "Make sure that after pro/demotion cursor position is right."
  6376. (let ((pos (point)))
  6377. (when (save-excursion
  6378. (beginning-of-line 1)
  6379. (looking-at org-todo-line-regexp)
  6380. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6381. (cond ((eobp) (insert " "))
  6382. ((eolp) (insert " "))
  6383. ((equal (char-after) ?\ ) (forward-char 1))))))
  6384. (defun org-current-level ()
  6385. "Return the level of the current entry, or nil if before the first headline.
  6386. The level is the number of stars at the beginning of the headline."
  6387. (save-excursion
  6388. (org-with-limited-levels
  6389. (ignore-errors
  6390. (org-back-to-heading t)
  6391. (funcall outline-level)))))
  6392. (defun org-get-previous-line-level ()
  6393. "Return the outline depth of the last headline before the current line.
  6394. Returns 0 for the first headline in the buffer, and nil if before the
  6395. first headline."
  6396. (let ((current-level (org-current-level))
  6397. (prev-level (when (> (line-number-at-pos) 1)
  6398. (save-excursion
  6399. (beginning-of-line 0)
  6400. (org-current-level)))))
  6401. (cond ((null current-level) nil) ; Before first headline
  6402. ((null prev-level) 0) ; At first headline
  6403. (prev-level))))
  6404. (defun org-reduced-level (l)
  6405. "Compute the effective level of a heading.
  6406. This takes into account the setting of `org-odd-levels-only'."
  6407. (cond
  6408. ((zerop l) 0)
  6409. (org-odd-levels-only (1+ (floor (/ l 2))))
  6410. (t l)))
  6411. (defun org-level-increment ()
  6412. "Return the number of stars that will be added or removed at a
  6413. time to headlines when structure editing, based on the value of
  6414. `org-odd-levels-only'."
  6415. (if org-odd-levels-only 2 1))
  6416. (defun org-get-valid-level (level &optional change)
  6417. "Rectify a level change under the influence of `org-odd-levels-only'
  6418. LEVEL is a current level, CHANGE is by how much the level should be
  6419. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6420. even level numbers will become the next higher odd number."
  6421. (if org-odd-levels-only
  6422. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6423. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6424. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6425. (max 1 (+ level (or change 0)))))
  6426. (if (boundp 'define-obsolete-function-alias)
  6427. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6428. (define-obsolete-function-alias 'org-get-legal-level
  6429. 'org-get-valid-level)
  6430. (define-obsolete-function-alias 'org-get-legal-level
  6431. 'org-get-valid-level "23.1")))
  6432. (defun org-promote ()
  6433. "Promote the current heading higher up the tree.
  6434. If the region is active in `transient-mark-mode', promote all headings
  6435. in the region."
  6436. (org-back-to-heading t)
  6437. (let* ((level (save-match-data (funcall outline-level)))
  6438. (after-change-functions (remove 'flyspell-after-change-function
  6439. after-change-functions))
  6440. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6441. (diff (abs (- level (length up-head) -1))))
  6442. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6443. (replace-match up-head nil t)
  6444. ;; Fixup tag positioning
  6445. (and org-auto-align-tags (org-set-tags nil t))
  6446. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6447. (run-hooks 'org-after-promote-entry-hook)))
  6448. (defun org-demote ()
  6449. "Demote the current heading lower down the tree.
  6450. If the region is active in `transient-mark-mode', demote all headings
  6451. in the region."
  6452. (org-back-to-heading t)
  6453. (let* ((level (save-match-data (funcall outline-level)))
  6454. (after-change-functions (remove 'flyspell-after-change-function
  6455. after-change-functions))
  6456. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6457. (diff (abs (- level (length down-head) -1))))
  6458. (replace-match down-head nil t)
  6459. ;; Fixup tag positioning
  6460. (and org-auto-align-tags (org-set-tags nil t))
  6461. (if org-adapt-indentation (org-fixup-indentation diff))
  6462. (run-hooks 'org-after-demote-entry-hook)))
  6463. (defun org-cycle-level ()
  6464. "Cycle the level of an empty headline through possible states.
  6465. This goes first to child, then to parent, level, then up the hierarchy.
  6466. After top level, it switches back to sibling level."
  6467. (interactive)
  6468. (let ((org-adapt-indentation nil))
  6469. (when (org-point-at-end-of-empty-headline)
  6470. (setq this-command 'org-cycle-level) ; Only needed for caching
  6471. (let ((cur-level (org-current-level))
  6472. (prev-level (org-get-previous-line-level)))
  6473. (cond
  6474. ;; If first headline in file, promote to top-level.
  6475. ((= prev-level 0)
  6476. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6477. do (org-do-promote)))
  6478. ;; If same level as prev, demote one.
  6479. ((= prev-level cur-level)
  6480. (org-do-demote))
  6481. ;; If parent is top-level, promote to top level if not already.
  6482. ((= prev-level 1)
  6483. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6484. do (org-do-promote)))
  6485. ;; If top-level, return to prev-level.
  6486. ((= cur-level 1)
  6487. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6488. do (org-do-demote)))
  6489. ;; If less than prev-level, promote one.
  6490. ((< cur-level prev-level)
  6491. (org-do-promote))
  6492. ;; If deeper than prev-level, promote until higher than
  6493. ;; prev-level.
  6494. ((> cur-level prev-level)
  6495. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6496. do (org-do-promote))))
  6497. t))))
  6498. (defun org-map-tree (fun)
  6499. "Call FUN for every heading underneath the current one."
  6500. (org-back-to-heading)
  6501. (let ((level (funcall outline-level)))
  6502. (save-excursion
  6503. (funcall fun)
  6504. (while (and (progn
  6505. (outline-next-heading)
  6506. (> (funcall outline-level) level))
  6507. (not (eobp)))
  6508. (funcall fun)))))
  6509. (defun org-map-region (fun beg end)
  6510. "Call FUN for every heading between BEG and END."
  6511. (let ((org-ignore-region t))
  6512. (save-excursion
  6513. (setq end (copy-marker end))
  6514. (goto-char beg)
  6515. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6516. (< (point) end))
  6517. (funcall fun))
  6518. (while (and (progn
  6519. (outline-next-heading)
  6520. (< (point) end))
  6521. (not (eobp)))
  6522. (funcall fun)))))
  6523. (defun org-fixup-indentation (diff)
  6524. "Change the indentation in the current entry by DIFF.
  6525. However, if any line in the current entry has no indentation, or if it
  6526. would end up with no indentation after the change, nothing at all is done."
  6527. (save-excursion
  6528. (let ((end (save-excursion (outline-next-heading)
  6529. (point-marker)))
  6530. (prohibit (if (> diff 0)
  6531. "^\\S-"
  6532. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6533. col)
  6534. (unless (save-excursion (end-of-line 1)
  6535. (re-search-forward prohibit end t))
  6536. (while (and (< (point) end)
  6537. (re-search-forward "^[ \t]+" end t))
  6538. (goto-char (match-end 0))
  6539. (setq col (current-column))
  6540. (if (< diff 0) (replace-match ""))
  6541. (org-indent-to-column (+ diff col))))
  6542. (move-marker end nil))))
  6543. (defun org-convert-to-odd-levels ()
  6544. "Convert an org-mode file with all levels allowed to one with odd levels.
  6545. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6546. level 5 etc."
  6547. (interactive)
  6548. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6549. (let ((outline-level 'org-outline-level)
  6550. (org-odd-levels-only nil) n)
  6551. (save-excursion
  6552. (goto-char (point-min))
  6553. (while (re-search-forward "^\\*\\*+ " nil t)
  6554. (setq n (- (length (match-string 0)) 2))
  6555. (while (>= (setq n (1- n)) 0)
  6556. (org-demote))
  6557. (end-of-line 1))))))
  6558. (defun org-convert-to-oddeven-levels ()
  6559. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6560. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6561. file contains a section with an even level, conversion would
  6562. destroy the structure of the file. An error is signaled in this
  6563. case."
  6564. (interactive)
  6565. (goto-char (point-min))
  6566. ;; First check if there are no even levels
  6567. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6568. (org-show-context t)
  6569. (error "Not all levels are odd in this file. Conversion not possible"))
  6570. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6571. (let ((outline-regexp org-outline-regexp)
  6572. (outline-level 'org-outline-level)
  6573. (org-odd-levels-only nil) n)
  6574. (save-excursion
  6575. (goto-char (point-min))
  6576. (while (re-search-forward "^\\*\\*+ " nil t)
  6577. (setq n (/ (1- (length (match-string 0))) 2))
  6578. (while (>= (setq n (1- n)) 0)
  6579. (org-promote))
  6580. (end-of-line 1))))))
  6581. (defun org-tr-level (n)
  6582. "Make N odd if required."
  6583. (if org-odd-levels-only (1+ (/ n 2)) n))
  6584. ;;; Vertical tree motion, cutting and pasting of subtrees
  6585. (defun org-move-subtree-up (&optional arg)
  6586. "Move the current subtree up past ARG headlines of the same level."
  6587. (interactive "p")
  6588. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6589. (defun org-move-subtree-down (&optional arg)
  6590. "Move the current subtree down past ARG headlines of the same level."
  6591. (interactive "p")
  6592. (setq arg (prefix-numeric-value arg))
  6593. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6594. 'org-get-last-sibling))
  6595. (ins-point (make-marker))
  6596. (cnt (abs arg))
  6597. (col (current-column))
  6598. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6599. ;; Select the tree
  6600. (org-back-to-heading)
  6601. (setq beg0 (point))
  6602. (save-excursion
  6603. (setq ne-beg (org-back-over-empty-lines))
  6604. (setq beg (point)))
  6605. (save-match-data
  6606. (save-excursion (outline-end-of-heading)
  6607. (setq folded (outline-invisible-p)))
  6608. (outline-end-of-subtree))
  6609. (outline-next-heading)
  6610. (setq ne-end (org-back-over-empty-lines))
  6611. (setq end (point))
  6612. (goto-char beg0)
  6613. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6614. ;; include less whitespace
  6615. (save-excursion
  6616. (goto-char beg)
  6617. (forward-line (- ne-beg ne-end))
  6618. (setq beg (point))))
  6619. ;; Find insertion point, with error handling
  6620. (while (> cnt 0)
  6621. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6622. (progn (goto-char beg0)
  6623. (error "Cannot move past superior level or buffer limit")))
  6624. (setq cnt (1- cnt)))
  6625. (if (> arg 0)
  6626. ;; Moving forward - still need to move over subtree
  6627. (progn (org-end-of-subtree t t)
  6628. (save-excursion
  6629. (org-back-over-empty-lines)
  6630. (or (bolp) (newline)))))
  6631. (setq ne-ins (org-back-over-empty-lines))
  6632. (move-marker ins-point (point))
  6633. (setq txt (buffer-substring beg end))
  6634. (org-save-markers-in-region beg end)
  6635. (delete-region beg end)
  6636. (org-remove-empty-overlays-at beg)
  6637. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6638. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6639. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6640. (let ((bbb (point)))
  6641. (insert-before-markers txt)
  6642. (org-reinstall-markers-in-region bbb)
  6643. (move-marker ins-point bbb))
  6644. (or (bolp) (insert "\n"))
  6645. (setq ins-end (point))
  6646. (goto-char ins-point)
  6647. (org-skip-whitespace)
  6648. (when (and (< arg 0)
  6649. (org-first-sibling-p)
  6650. (> ne-ins ne-beg))
  6651. ;; Move whitespace back to beginning
  6652. (save-excursion
  6653. (goto-char ins-end)
  6654. (let ((kill-whole-line t))
  6655. (kill-line (- ne-ins ne-beg)) (point)))
  6656. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6657. (move-marker ins-point nil)
  6658. (if folded
  6659. (hide-subtree)
  6660. (org-show-entry)
  6661. (show-children)
  6662. (org-cycle-hide-drawers 'children))
  6663. (org-clean-visibility-after-subtree-move)
  6664. ;; move back to the initial column we were at
  6665. (move-to-column col)))
  6666. (defvar org-subtree-clip ""
  6667. "Clipboard for cut and paste of subtrees.
  6668. This is actually only a copy of the kill, because we use the normal kill
  6669. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6670. (defvar org-subtree-clip-folded nil
  6671. "Was the last copied subtree folded?
  6672. This is used to fold the tree back after pasting.")
  6673. (defun org-cut-subtree (&optional n)
  6674. "Cut the current subtree into the clipboard.
  6675. With prefix arg N, cut this many sequential subtrees.
  6676. This is a short-hand for marking the subtree and then cutting it."
  6677. (interactive "p")
  6678. (org-copy-subtree n 'cut))
  6679. (defun org-copy-subtree (&optional n cut force-store-markers)
  6680. "Cut the current subtree into the clipboard.
  6681. With prefix arg N, cut this many sequential subtrees.
  6682. This is a short-hand for marking the subtree and then copying it.
  6683. If CUT is non-nil, actually cut the subtree.
  6684. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6685. of some markers in the region, even if CUT is non-nil. This is
  6686. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6687. (interactive "p")
  6688. (let (beg end folded (beg0 (point)))
  6689. (if (org-called-interactively-p 'any)
  6690. (org-back-to-heading nil) ; take what looks like a subtree
  6691. (org-back-to-heading t)) ; take what is really there
  6692. (org-back-over-empty-lines)
  6693. (setq beg (point))
  6694. (skip-chars-forward " \t\r\n")
  6695. (save-match-data
  6696. (save-excursion (outline-end-of-heading)
  6697. (setq folded (outline-invisible-p)))
  6698. (condition-case nil
  6699. (org-forward-same-level (1- n) t)
  6700. (error nil))
  6701. (org-end-of-subtree t t))
  6702. (org-back-over-empty-lines)
  6703. (setq end (point))
  6704. (goto-char beg0)
  6705. (when (> end beg)
  6706. (setq org-subtree-clip-folded folded)
  6707. (when (or cut force-store-markers)
  6708. (org-save-markers-in-region beg end))
  6709. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6710. (setq org-subtree-clip (current-kill 0))
  6711. (message "%s: Subtree(s) with %d characters"
  6712. (if cut "Cut" "Copied")
  6713. (length org-subtree-clip)))))
  6714. (defun org-paste-subtree (&optional level tree for-yank)
  6715. "Paste the clipboard as a subtree, with modification of headline level.
  6716. The entire subtree is promoted or demoted in order to match a new headline
  6717. level.
  6718. If the cursor is at the beginning of a headline, the same level as
  6719. that headline is used to paste the tree
  6720. If not, the new level is derived from the *visible* headings
  6721. before and after the insertion point, and taken to be the inferior headline
  6722. level of the two. So if the previous visible heading is level 3 and the
  6723. next is level 4 (or vice versa), level 4 will be used for insertion.
  6724. This makes sure that the subtree remains an independent subtree and does
  6725. not swallow low level entries.
  6726. You can also force a different level, either by using a numeric prefix
  6727. argument, or by inserting the heading marker by hand. For example, if the
  6728. cursor is after \"*****\", then the tree will be shifted to level 5.
  6729. If optional TREE is given, use this text instead of the kill ring.
  6730. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6731. move back over whitespace before inserting, and move point to the end of
  6732. the inserted text when done."
  6733. (interactive "P")
  6734. (setq tree (or tree (and kill-ring (current-kill 0))))
  6735. (unless (org-kill-is-subtree-p tree)
  6736. (error "%s"
  6737. (substitute-command-keys
  6738. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6739. (let* ((visp (not (outline-invisible-p)))
  6740. (txt tree)
  6741. (^re (concat "^\\(" org-outline-regexp "\\)"))
  6742. (re (concat "\\(" org-outline-regexp "\\)"))
  6743. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6744. (old-level (if (string-match ^re txt)
  6745. (- (match-end 0) (match-beginning 0) 1)
  6746. -1))
  6747. (force-level (cond (level (prefix-numeric-value level))
  6748. ((and (looking-at "[ \t]*$")
  6749. (string-match
  6750. ^re_ (buffer-substring
  6751. (point-at-bol) (point))))
  6752. (- (match-end 1) (match-beginning 1)))
  6753. ((and (bolp)
  6754. (looking-at org-outline-regexp))
  6755. (- (match-end 0) (point) 1))
  6756. (t nil)))
  6757. (previous-level (save-excursion
  6758. (condition-case nil
  6759. (progn
  6760. (outline-previous-visible-heading 1)
  6761. (if (looking-at re)
  6762. (- (match-end 0) (match-beginning 0) 1)
  6763. 1))
  6764. (error 1))))
  6765. (next-level (save-excursion
  6766. (condition-case nil
  6767. (progn
  6768. (or (looking-at org-outline-regexp)
  6769. (outline-next-visible-heading 1))
  6770. (if (looking-at re)
  6771. (- (match-end 0) (match-beginning 0) 1)
  6772. 1))
  6773. (error 1))))
  6774. (new-level (or force-level (max previous-level next-level)))
  6775. (shift (if (or (= old-level -1)
  6776. (= new-level -1)
  6777. (= old-level new-level))
  6778. 0
  6779. (- new-level old-level)))
  6780. (delta (if (> shift 0) -1 1))
  6781. (func (if (> shift 0) 'org-demote 'org-promote))
  6782. (org-odd-levels-only nil)
  6783. beg end newend)
  6784. ;; Remove the forced level indicator
  6785. (if force-level
  6786. (delete-region (point-at-bol) (point)))
  6787. ;; Paste
  6788. (beginning-of-line 1)
  6789. (unless for-yank (org-back-over-empty-lines))
  6790. (setq beg (point))
  6791. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6792. (insert-before-markers txt)
  6793. (unless (string-match "\n\\'" txt) (insert "\n"))
  6794. (setq newend (point))
  6795. (org-reinstall-markers-in-region beg)
  6796. (setq end (point))
  6797. (goto-char beg)
  6798. (skip-chars-forward " \t\n\r")
  6799. (setq beg (point))
  6800. (if (and (outline-invisible-p) visp)
  6801. (save-excursion (outline-show-heading)))
  6802. ;; Shift if necessary
  6803. (unless (= shift 0)
  6804. (save-restriction
  6805. (narrow-to-region beg end)
  6806. (while (not (= shift 0))
  6807. (org-map-region func (point-min) (point-max))
  6808. (setq shift (+ delta shift)))
  6809. (goto-char (point-min))
  6810. (setq newend (point-max))))
  6811. (when (or (org-called-interactively-p 'interactive) for-yank)
  6812. (message "Clipboard pasted as level %d subtree" new-level))
  6813. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6814. kill-ring
  6815. (eq org-subtree-clip (current-kill 0))
  6816. org-subtree-clip-folded)
  6817. ;; The tree was folded before it was killed/copied
  6818. (hide-subtree))
  6819. (and for-yank (goto-char newend))))
  6820. (defun org-kill-is-subtree-p (&optional txt)
  6821. "Check if the current kill is an outline subtree, or a set of trees.
  6822. Returns nil if kill does not start with a headline, or if the first
  6823. headline level is not the largest headline level in the tree.
  6824. So this will actually accept several entries of equal levels as well,
  6825. which is OK for `org-paste-subtree'.
  6826. If optional TXT is given, check this string instead of the current kill."
  6827. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6828. (start-level (and kill
  6829. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6830. org-outline-regexp "\\)")
  6831. kill)
  6832. (- (match-end 2) (match-beginning 2) 1)))
  6833. (re org-outline-regexp-bol)
  6834. (start (1+ (or (match-beginning 2) -1))))
  6835. (if (not start-level)
  6836. (progn
  6837. nil) ;; does not even start with a heading
  6838. (catch 'exit
  6839. (while (setq start (string-match re kill (1+ start)))
  6840. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6841. (throw 'exit nil)))
  6842. t))))
  6843. (defvar org-markers-to-move nil
  6844. "Markers that should be moved with a cut-and-paste operation.
  6845. Those markers are stored together with their positions relative to
  6846. the start of the region.")
  6847. (defun org-save-markers-in-region (beg end)
  6848. "Check markers in region.
  6849. If these markers are between BEG and END, record their position relative
  6850. to BEG, so that after moving the block of text, we can put the markers back
  6851. into place.
  6852. This function gets called just before an entry or tree gets cut from the
  6853. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6854. called immediately, to move the markers with the entries."
  6855. (setq org-markers-to-move nil)
  6856. (when (featurep 'org-clock)
  6857. (org-clock-save-markers-for-cut-and-paste beg end))
  6858. (when (featurep 'org-agenda)
  6859. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6860. (defun org-check-and-save-marker (marker beg end)
  6861. "Check if MARKER is between BEG and END.
  6862. If yes, remember the marker and the distance to BEG."
  6863. (when (and (marker-buffer marker)
  6864. (equal (marker-buffer marker) (current-buffer)))
  6865. (if (and (>= marker beg) (< marker end))
  6866. (push (cons marker (- marker beg)) org-markers-to-move))))
  6867. (defun org-reinstall-markers-in-region (beg)
  6868. "Move all remembered markers to their position relative to BEG."
  6869. (mapc (lambda (x)
  6870. (move-marker (car x) (+ beg (cdr x))))
  6871. org-markers-to-move)
  6872. (setq org-markers-to-move nil))
  6873. (defun org-narrow-to-subtree ()
  6874. "Narrow buffer to the current subtree."
  6875. (interactive)
  6876. (save-excursion
  6877. (save-match-data
  6878. (org-with-limited-levels
  6879. (narrow-to-region
  6880. (progn (org-back-to-heading t) (point))
  6881. (progn (org-end-of-subtree t t)
  6882. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  6883. (point)))))))
  6884. (defun org-narrow-to-block ()
  6885. "Narrow buffer to the current block."
  6886. (interactive)
  6887. (let ((bstart "^[ \t]*#\\+begin")
  6888. (bend "[ \t]*#\\+end")
  6889. (case-fold-search t) ;; allow #+BEGIN
  6890. b_start b_end)
  6891. (if (org-in-regexps-block-p bstart bend)
  6892. (progn
  6893. (save-excursion (re-search-backward bstart nil t)
  6894. (setq b_start (match-beginning 0)))
  6895. (save-excursion (re-search-forward bend nil t)
  6896. (setq b_end (match-end 0)))
  6897. (narrow-to-region b_start b_end))
  6898. (error "Not in a block"))))
  6899. (eval-when-compile
  6900. (defvar org-property-drawer-re))
  6901. (defvar org-property-start-re) ;; defined below
  6902. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6903. "Clone the task (subtree) at point N times.
  6904. The clones will be inserted as siblings.
  6905. In interactive use, the user will be prompted for the number of
  6906. clones to be produced, and for a time SHIFT, which may be a
  6907. repeater as used in time stamps, for example `+3d'.
  6908. When a valid repeater is given and the entry contains any time
  6909. stamps, the clones will become a sequence in time, with time
  6910. stamps in the subtree shifted for each clone produced. If SHIFT
  6911. is nil or the empty string, time stamps will be left alone. The
  6912. ID property of the original subtree is removed.
  6913. If the original subtree did contain time stamps with a repeater,
  6914. the following will happen:
  6915. - the repeater will be removed in each clone
  6916. - an additional clone will be produced, with the current, unshifted
  6917. date(s) in the entry.
  6918. - the original entry will be placed *after* all the clones, with
  6919. repeater intact.
  6920. - the start days in the repeater in the original entry will be shifted
  6921. to past the last clone.
  6922. I this way you can spell out a number of instances of a repeating task,
  6923. and still retain the repeater to cover future instances of the task."
  6924. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6925. (let (beg end template task idprop
  6926. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6927. (if (not (and (integerp n) (> n 0)))
  6928. (error "Invalid number of replications %s" n))
  6929. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6930. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6931. shift)))
  6932. (error "Invalid shift specification %s" shift))
  6933. (when doshift
  6934. (setq shift-n (string-to-number (match-string 1 shift))
  6935. shift-what (cdr (assoc (match-string 2 shift)
  6936. '(("d" . day) ("w" . week)
  6937. ("m" . month) ("y" . year))))))
  6938. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6939. (setq nmin 1 nmax n)
  6940. (org-back-to-heading t)
  6941. (setq beg (point))
  6942. (setq idprop (org-entry-get nil "ID"))
  6943. (org-end-of-subtree t t)
  6944. (or (bolp) (insert "\n"))
  6945. (setq end (point))
  6946. (setq template (buffer-substring beg end))
  6947. ;; Remove clocks and empty drawers
  6948. (with-temp-buffer
  6949. (insert template)
  6950. (goto-char (point-min))
  6951. (while (re-search-forward
  6952. "^[ \t]*CLOCK:.*$" (save-excursion (org-end-of-subtree t t)) t)
  6953. (replace-match "")
  6954. (kill-whole-line))
  6955. (goto-char (point-min))
  6956. (while (re-search-forward
  6957. (concat "^[ \t]*:" (regexp-opt org-drawers) ":[ \t]*$") nil t)
  6958. (mapc (lambda(d) (org-remove-empty-drawer-at d (point))) org-drawers))
  6959. (setq template (buffer-substring (point-min) (point-max))))
  6960. (when (and doshift
  6961. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6962. (delete-region beg end)
  6963. (setq end beg)
  6964. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6965. (goto-char end)
  6966. (loop for n from nmin to nmax do
  6967. ;; prepare clone
  6968. (with-temp-buffer
  6969. (insert template)
  6970. (org-mode)
  6971. (goto-char (point-min))
  6972. (and idprop (if org-clone-delete-id
  6973. (org-entry-delete nil "ID")
  6974. (org-id-get-create t)))
  6975. (while (re-search-forward org-property-start-re nil t)
  6976. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6977. (goto-char (point-min))
  6978. (when doshift
  6979. (while (re-search-forward org-ts-regexp-both nil t)
  6980. (org-timestamp-change (* n shift-n) shift-what))
  6981. (unless (= n n-no-remove)
  6982. (goto-char (point-min))
  6983. (while (re-search-forward org-ts-regexp nil t)
  6984. (save-excursion
  6985. (goto-char (match-beginning 0))
  6986. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6987. (delete-region (match-beginning 1) (match-end 1)))))))
  6988. (setq task (buffer-string)))
  6989. (insert task))
  6990. (goto-char beg)))
  6991. ;;; Outline Sorting
  6992. (defun org-sort (with-case)
  6993. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  6994. Optional argument WITH-CASE means sort case-sensitively.
  6995. With a double prefix argument, also remove duplicate entries."
  6996. (interactive "P")
  6997. (cond
  6998. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  6999. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7000. (t
  7001. (org-call-with-arg 'org-sort-entries with-case))))
  7002. (defun org-sort-remove-invisible (s)
  7003. (remove-text-properties 0 (length s) org-rm-props s)
  7004. (while (string-match org-bracket-link-regexp s)
  7005. (setq s (replace-match (if (match-end 2)
  7006. (match-string 3 s)
  7007. (match-string 1 s)) t t s)))
  7008. s)
  7009. (defvar org-priority-regexp) ; defined later in the file
  7010. (defvar org-after-sorting-entries-or-items-hook nil
  7011. "Hook that is run after a bunch of entries or items have been sorted.
  7012. When children are sorted, the cursor is in the parent line when this
  7013. hook gets called. When a region or a plain list is sorted, the cursor
  7014. will be in the first entry of the sorted region/list.")
  7015. (defun org-sort-entries
  7016. (&optional with-case sorting-type getkey-func compare-func property)
  7017. "Sort entries on a certain level of an outline tree.
  7018. If there is an active region, the entries in the region are sorted.
  7019. Else, if the cursor is before the first entry, sort the top-level items.
  7020. Else, the children of the entry at point are sorted.
  7021. Sorting can be alphabetically, numerically, by date/time as given by
  7022. a time stamp, by a property or by priority.
  7023. The command prompts for the sorting type unless it has been given to the
  7024. function through the SORTING-TYPE argument, which needs to be a character,
  7025. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7026. precise meaning of each character:
  7027. n Numerically, by converting the beginning of the entry/item to a number.
  7028. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7029. t By date/time, either the first active time stamp in the entry, or, if
  7030. none exist, by the first inactive one.
  7031. s By the scheduled date/time.
  7032. d By deadline date/time.
  7033. c By creation time, which is assumed to be the first inactive time stamp
  7034. at the beginning of a line.
  7035. p By priority according to the cookie.
  7036. r By the value of a property.
  7037. Capital letters will reverse the sort order.
  7038. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7039. called with point at the beginning of the record. It must return either
  7040. a string or a number that should serve as the sorting key for that record.
  7041. Comparing entries ignores case by default. However, with an optional argument
  7042. WITH-CASE, the sorting considers case as well."
  7043. (interactive "P")
  7044. (let ((case-func (if with-case 'identity 'downcase))
  7045. start beg end stars re re2
  7046. txt what tmp)
  7047. ;; Find beginning and end of region to sort
  7048. (cond
  7049. ((org-region-active-p)
  7050. ;; we will sort the region
  7051. (setq end (region-end)
  7052. what "region")
  7053. (goto-char (region-beginning))
  7054. (if (not (org-on-heading-p)) (outline-next-heading))
  7055. (setq start (point)))
  7056. ((or (org-on-heading-p)
  7057. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7058. ;; we will sort the children of the current headline
  7059. (org-back-to-heading)
  7060. (setq start (point)
  7061. end (progn (org-end-of-subtree t t)
  7062. (or (bolp) (insert "\n"))
  7063. (org-back-over-empty-lines)
  7064. (point))
  7065. what "children")
  7066. (goto-char start)
  7067. (show-subtree)
  7068. (outline-next-heading))
  7069. (t
  7070. ;; we will sort the top-level entries in this file
  7071. (goto-char (point-min))
  7072. (or (org-on-heading-p) (outline-next-heading))
  7073. (setq start (point))
  7074. (goto-char (point-max))
  7075. (beginning-of-line 1)
  7076. (when (looking-at ".*?\\S-")
  7077. ;; File ends in a non-white line
  7078. (end-of-line 1)
  7079. (insert "\n"))
  7080. (setq end (point-max))
  7081. (setq what "top-level")
  7082. (goto-char start)
  7083. (show-all)))
  7084. (setq beg (point))
  7085. (if (>= beg end) (error "Nothing to sort"))
  7086. (looking-at "\\(\\*+\\)")
  7087. (setq stars (match-string 1)
  7088. re (concat "^" (regexp-quote stars) " +")
  7089. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7090. txt (buffer-substring beg end))
  7091. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7092. (if (and (not (equal stars "*")) (string-match re2 txt))
  7093. (error "Region to sort contains a level above the first entry"))
  7094. (unless sorting-type
  7095. (message
  7096. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7097. [t]ime [s]cheduled [d]eadline [c]reated
  7098. A/N/T/S/D/C/P/O/F means reversed:"
  7099. what)
  7100. (setq sorting-type (read-char-exclusive))
  7101. (and (= (downcase sorting-type) ?f)
  7102. (setq getkey-func
  7103. (org-icompleting-read "Sort using function: "
  7104. obarray 'fboundp t nil nil))
  7105. (setq getkey-func (intern getkey-func)))
  7106. (and (= (downcase sorting-type) ?r)
  7107. (setq property
  7108. (org-icompleting-read "Property: "
  7109. (mapcar 'list (org-buffer-property-keys t))
  7110. nil t))))
  7111. (message "Sorting entries...")
  7112. (save-restriction
  7113. (narrow-to-region start end)
  7114. (let ((dcst (downcase sorting-type))
  7115. (case-fold-search nil)
  7116. (now (current-time)))
  7117. (sort-subr
  7118. (/= dcst sorting-type)
  7119. ;; This function moves to the beginning character of the "record" to
  7120. ;; be sorted.
  7121. (lambda nil
  7122. (if (re-search-forward re nil t)
  7123. (goto-char (match-beginning 0))
  7124. (goto-char (point-max))))
  7125. ;; This function moves to the last character of the "record" being
  7126. ;; sorted.
  7127. (lambda nil
  7128. (save-match-data
  7129. (condition-case nil
  7130. (outline-forward-same-level 1)
  7131. (error
  7132. (goto-char (point-max))))))
  7133. ;; This function returns the value that gets sorted against.
  7134. (lambda nil
  7135. (cond
  7136. ((= dcst ?n)
  7137. (if (looking-at org-complex-heading-regexp)
  7138. (string-to-number (match-string 4))
  7139. nil))
  7140. ((= dcst ?a)
  7141. (if (looking-at org-complex-heading-regexp)
  7142. (funcall case-func (match-string 4))
  7143. nil))
  7144. ((= dcst ?t)
  7145. (let ((end (save-excursion (outline-next-heading) (point))))
  7146. (if (or (re-search-forward org-ts-regexp end t)
  7147. (re-search-forward org-ts-regexp-both end t))
  7148. (org-time-string-to-seconds (match-string 0))
  7149. (org-float-time now))))
  7150. ((= dcst ?c)
  7151. (let ((end (save-excursion (outline-next-heading) (point))))
  7152. (if (re-search-forward
  7153. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7154. end t)
  7155. (org-time-string-to-seconds (match-string 0))
  7156. (org-float-time now))))
  7157. ((= dcst ?s)
  7158. (let ((end (save-excursion (outline-next-heading) (point))))
  7159. (if (re-search-forward org-scheduled-time-regexp end t)
  7160. (org-time-string-to-seconds (match-string 1))
  7161. (org-float-time now))))
  7162. ((= dcst ?d)
  7163. (let ((end (save-excursion (outline-next-heading) (point))))
  7164. (if (re-search-forward org-deadline-time-regexp end t)
  7165. (org-time-string-to-seconds (match-string 1))
  7166. (org-float-time now))))
  7167. ((= dcst ?p)
  7168. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7169. (string-to-char (match-string 2))
  7170. org-default-priority))
  7171. ((= dcst ?r)
  7172. (or (org-entry-get nil property) ""))
  7173. ((= dcst ?o)
  7174. (if (looking-at org-complex-heading-regexp)
  7175. (- 9999 (length (member (match-string 2)
  7176. org-todo-keywords-1)))))
  7177. ((= dcst ?f)
  7178. (if getkey-func
  7179. (progn
  7180. (setq tmp (funcall getkey-func))
  7181. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7182. tmp)
  7183. (error "Invalid key function `%s'" getkey-func)))
  7184. (t (error "Invalid sorting type `%c'" sorting-type))))
  7185. nil
  7186. (cond
  7187. ((= dcst ?a) 'string<)
  7188. ((= dcst ?f) compare-func)
  7189. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7190. (t nil)))))
  7191. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7192. (message "Sorting entries...done")))
  7193. (defun org-do-sort (table what &optional with-case sorting-type)
  7194. "Sort TABLE of WHAT according to SORTING-TYPE.
  7195. The user will be prompted for the SORTING-TYPE if the call to this
  7196. function does not specify it. WHAT is only for the prompt, to indicate
  7197. what is being sorted. The sorting key will be extracted from
  7198. the car of the elements of the table.
  7199. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7200. (unless sorting-type
  7201. (message
  7202. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7203. what)
  7204. (setq sorting-type (read-char-exclusive)))
  7205. (let ((dcst (downcase sorting-type))
  7206. extractfun comparefun)
  7207. ;; Define the appropriate functions
  7208. (cond
  7209. ((= dcst ?n)
  7210. (setq extractfun 'string-to-number
  7211. comparefun (if (= dcst sorting-type) '< '>)))
  7212. ((= dcst ?a)
  7213. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7214. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7215. comparefun (if (= dcst sorting-type)
  7216. 'string<
  7217. (lambda (a b) (and (not (string< a b))
  7218. (not (string= a b)))))))
  7219. ((= dcst ?t)
  7220. (setq extractfun
  7221. (lambda (x)
  7222. (if (or (string-match org-ts-regexp x)
  7223. (string-match org-ts-regexp-both x))
  7224. (org-float-time
  7225. (org-time-string-to-time (match-string 0 x)))
  7226. 0))
  7227. comparefun (if (= dcst sorting-type) '< '>)))
  7228. (t (error "Invalid sorting type `%c'" sorting-type)))
  7229. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7230. table)
  7231. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7232. ;;; The orgstruct minor mode
  7233. ;; Define a minor mode which can be used in other modes in order to
  7234. ;; integrate the org-mode structure editing commands.
  7235. ;; This is really a hack, because the org-mode structure commands use
  7236. ;; keys which normally belong to the major mode. Here is how it
  7237. ;; works: The minor mode defines all the keys necessary to operate the
  7238. ;; structure commands, but wraps the commands into a function which
  7239. ;; tests if the cursor is currently at a headline or a plain list
  7240. ;; item. If that is the case, the structure command is used,
  7241. ;; temporarily setting many Org-mode variables like regular
  7242. ;; expressions for filling etc. However, when any of those keys is
  7243. ;; used at a different location, function uses `key-binding' to look
  7244. ;; up if the key has an associated command in another currently active
  7245. ;; keymap (minor modes, major mode, global), and executes that
  7246. ;; command. There might be problems if any of the keys is otherwise
  7247. ;; used as a prefix key.
  7248. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7249. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7250. ;; addresses this by checking explicitly for both bindings.
  7251. (defvar orgstruct-mode-map (make-sparse-keymap)
  7252. "Keymap for the minor `orgstruct-mode'.")
  7253. (defvar org-local-vars nil
  7254. "List of local variables, for use by `orgstruct-mode'.")
  7255. ;;;###autoload
  7256. (define-minor-mode orgstruct-mode
  7257. "Toggle the minor mode `orgstruct-mode'.
  7258. This mode is for using Org-mode structure commands in other
  7259. modes. The following keys behave as if Org-mode were active, if
  7260. the cursor is on a headline, or on a plain list item (both as
  7261. defined by Org-mode).
  7262. M-up Move entry/item up
  7263. M-down Move entry/item down
  7264. M-left Promote
  7265. M-right Demote
  7266. M-S-up Move entry/item up
  7267. M-S-down Move entry/item down
  7268. M-S-left Promote subtree
  7269. M-S-right Demote subtree
  7270. M-q Fill paragraph and items like in Org-mode
  7271. C-c ^ Sort entries
  7272. C-c - Cycle list bullet
  7273. TAB Cycle item visibility
  7274. M-RET Insert new heading/item
  7275. S-M-RET Insert new TODO heading / Checkbox item
  7276. C-c C-c Set tags / toggle checkbox"
  7277. nil " OrgStruct" nil
  7278. (org-load-modules-maybe)
  7279. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7280. ;;;###autoload
  7281. (defun turn-on-orgstruct ()
  7282. "Unconditionally turn on `orgstruct-mode'."
  7283. (orgstruct-mode 1))
  7284. (defun orgstruct++-mode (&optional arg)
  7285. "Toggle `orgstruct-mode', the enhanced version of it.
  7286. In addition to setting orgstruct-mode, this also exports all indentation
  7287. and autofilling variables from org-mode into the buffer. It will also
  7288. recognize item context in multiline items.
  7289. Note that turning off orgstruct-mode will *not* remove the
  7290. indentation/paragraph settings. This can only be done by refreshing the
  7291. major mode, for example with \\[normal-mode]."
  7292. (interactive "P")
  7293. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7294. (if (< arg 1)
  7295. (orgstruct-mode -1)
  7296. (orgstruct-mode 1)
  7297. (let (var val)
  7298. (mapc
  7299. (lambda (x)
  7300. (when (string-match
  7301. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7302. (symbol-name (car x)))
  7303. (setq var (car x) val (nth 1 x))
  7304. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7305. org-local-vars)
  7306. (org-set-local 'orgstruct-is-++ t))))
  7307. (defvar orgstruct-is-++ nil
  7308. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7309. (make-variable-buffer-local 'orgstruct-is-++)
  7310. ;;;###autoload
  7311. (defun turn-on-orgstruct++ ()
  7312. "Unconditionally turn on `orgstruct++-mode'."
  7313. (orgstruct++-mode 1))
  7314. (defun orgstruct-error ()
  7315. "Error when there is no default binding for a structure key."
  7316. (interactive)
  7317. (error "This key has no function outside structure elements"))
  7318. (defun orgstruct-setup ()
  7319. "Setup orgstruct keymaps."
  7320. (let ((nfunc 0)
  7321. (bindings
  7322. (list
  7323. '([(meta up)] org-metaup)
  7324. '([(meta down)] org-metadown)
  7325. '([(meta left)] org-metaleft)
  7326. '([(meta right)] org-metaright)
  7327. '([(meta shift up)] org-shiftmetaup)
  7328. '([(meta shift down)] org-shiftmetadown)
  7329. '([(meta shift left)] org-shiftmetaleft)
  7330. '([(meta shift right)] org-shiftmetaright)
  7331. '([?\e (up)] org-metaup)
  7332. '([?\e (down)] org-metadown)
  7333. '([?\e (left)] org-metaleft)
  7334. '([?\e (right)] org-metaright)
  7335. '([?\e (shift up)] org-shiftmetaup)
  7336. '([?\e (shift down)] org-shiftmetadown)
  7337. '([?\e (shift left)] org-shiftmetaleft)
  7338. '([?\e (shift right)] org-shiftmetaright)
  7339. '([(shift up)] org-shiftup)
  7340. '([(shift down)] org-shiftdown)
  7341. '([(shift left)] org-shiftleft)
  7342. '([(shift right)] org-shiftright)
  7343. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7344. '("\M-q" fill-paragraph)
  7345. '("\C-c^" org-sort)
  7346. '("\C-c-" org-cycle-list-bullet)))
  7347. elt key fun cmd)
  7348. (while (setq elt (pop bindings))
  7349. (setq nfunc (1+ nfunc))
  7350. (setq key (org-key (car elt))
  7351. fun (nth 1 elt)
  7352. cmd (orgstruct-make-binding fun nfunc key))
  7353. (org-defkey orgstruct-mode-map key cmd))
  7354. ;; Special treatment needed for TAB and RET
  7355. (org-defkey orgstruct-mode-map [(tab)]
  7356. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7357. (org-defkey orgstruct-mode-map "\C-i"
  7358. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7359. (org-defkey orgstruct-mode-map "\M-\C-m"
  7360. (orgstruct-make-binding 'org-insert-heading 105
  7361. "\M-\C-m" [(meta return)]))
  7362. (org-defkey orgstruct-mode-map [(meta return)]
  7363. (orgstruct-make-binding 'org-insert-heading 106
  7364. [(meta return)] "\M-\C-m"))
  7365. (org-defkey orgstruct-mode-map [(shift meta return)]
  7366. (orgstruct-make-binding 'org-insert-todo-heading 107
  7367. [(meta return)] "\M-\C-m"))
  7368. (org-defkey orgstruct-mode-map "\e\C-m"
  7369. (orgstruct-make-binding 'org-insert-heading 108
  7370. "\e\C-m" [?\e (return)]))
  7371. (org-defkey orgstruct-mode-map [?\e (return)]
  7372. (orgstruct-make-binding 'org-insert-heading 109
  7373. [?\e (return)] "\e\C-m"))
  7374. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7375. (orgstruct-make-binding 'org-insert-todo-heading 110
  7376. [?\e (return)] "\e\C-m"))
  7377. (unless org-local-vars
  7378. (setq org-local-vars (org-get-local-variables)))
  7379. t))
  7380. (defun orgstruct-make-binding (fun n &rest keys)
  7381. "Create a function for binding in the structure minor mode.
  7382. FUN is the command to call inside a table. N is used to create a unique
  7383. command name. KEYS are keys that should be checked in for a command
  7384. to execute outside of tables."
  7385. (eval
  7386. (list 'defun
  7387. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7388. '(arg)
  7389. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7390. "Outside of structure, run the binding of `"
  7391. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7392. "'.")
  7393. '(interactive "p")
  7394. (list 'if
  7395. `(org-context-p 'headline 'item
  7396. (and orgstruct-is-++
  7397. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7398. 'item-body))
  7399. (list 'org-run-like-in-org-mode (list 'quote fun))
  7400. (list 'let '(orgstruct-mode)
  7401. (list 'call-interactively
  7402. (append '(or)
  7403. (mapcar (lambda (k)
  7404. (list 'key-binding k))
  7405. keys)
  7406. '('orgstruct-error))))))))
  7407. (defun org-context-p (&rest contexts)
  7408. "Check if local context is any of CONTEXTS.
  7409. Possible values in the list of contexts are `table', `headline', and `item'."
  7410. (let ((pos (point)))
  7411. (goto-char (point-at-bol))
  7412. (prog1 (or (and (memq 'table contexts)
  7413. (looking-at "[ \t]*|"))
  7414. (and (memq 'headline contexts)
  7415. (looking-at org-outline-regexp))
  7416. (and (memq 'item contexts)
  7417. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7418. (and (memq 'item-body contexts)
  7419. (org-in-item-p)))
  7420. (goto-char pos))))
  7421. (defun org-get-local-variables ()
  7422. "Return a list of all local variables in an org-mode buffer."
  7423. (let (varlist)
  7424. (with-current-buffer (get-buffer-create "*Org tmp*")
  7425. (erase-buffer)
  7426. (org-mode)
  7427. (setq varlist (buffer-local-variables)))
  7428. (kill-buffer "*Org tmp*")
  7429. (delq nil
  7430. (mapcar
  7431. (lambda (x)
  7432. (setq x
  7433. (if (symbolp x)
  7434. (list x)
  7435. (list (car x) (list 'quote (cdr x)))))
  7436. (if (string-match
  7437. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7438. (symbol-name (car x)))
  7439. x nil))
  7440. varlist))))
  7441. (defun org-clone-local-variables (from-buffer &optional regexp)
  7442. "Clone local variables from FROM-BUFFER.
  7443. Optional argument REGEXP selects variables to clone."
  7444. (mapc
  7445. (lambda (pair)
  7446. (and (symbolp (car pair))
  7447. (or (null regexp)
  7448. (string-match regexp (symbol-name (car pair))))
  7449. (set (make-local-variable (car pair))
  7450. (cdr pair))))
  7451. (buffer-local-variables from-buffer)))
  7452. ;;;###autoload
  7453. (defun org-run-like-in-org-mode (cmd)
  7454. "Run a command, pretending that the current buffer is in Org-mode.
  7455. This will temporarily bind local variables that are typically bound in
  7456. Org-mode to the values they have in Org-mode, and then interactively
  7457. call CMD."
  7458. (org-load-modules-maybe)
  7459. (unless org-local-vars
  7460. (setq org-local-vars (org-get-local-variables)))
  7461. (eval (list 'let org-local-vars
  7462. (list 'call-interactively (list 'quote cmd)))))
  7463. ;;;; Archiving
  7464. (defun org-get-category (&optional pos force-refresh)
  7465. "Get the category applying to position POS."
  7466. (if force-refresh (org-refresh-category-properties))
  7467. (let ((pos (or pos (point))))
  7468. (or (get-text-property pos 'org-category)
  7469. (progn (org-refresh-category-properties)
  7470. (get-text-property pos 'org-category)))))
  7471. (defun org-refresh-category-properties ()
  7472. "Refresh category text properties in the buffer."
  7473. (let ((def-cat (cond
  7474. ((null org-category)
  7475. (if buffer-file-name
  7476. (file-name-sans-extension
  7477. (file-name-nondirectory buffer-file-name))
  7478. "???"))
  7479. ((symbolp org-category) (symbol-name org-category))
  7480. (t org-category)))
  7481. beg end cat pos optionp)
  7482. (org-unmodified
  7483. (save-excursion
  7484. (save-restriction
  7485. (widen)
  7486. (goto-char (point-min))
  7487. (put-text-property (point) (point-max) 'org-category def-cat)
  7488. (while (re-search-forward
  7489. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7490. (setq pos (match-end 0)
  7491. optionp (equal (char-after (match-beginning 0)) ?#)
  7492. cat (org-trim (match-string 2)))
  7493. (if optionp
  7494. (setq beg (point-at-bol) end (point-max))
  7495. (org-back-to-heading t)
  7496. (setq beg (point) end (org-end-of-subtree t t)))
  7497. (put-text-property beg end 'org-category cat)
  7498. (goto-char pos)))))))
  7499. ;;;; Link Stuff
  7500. ;;; Link abbreviations
  7501. (defun org-link-expand-abbrev (link)
  7502. "Apply replacements as defined in `org-link-abbrev-alist."
  7503. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7504. (let* ((key (match-string 1 link))
  7505. (as (or (assoc key org-link-abbrev-alist-local)
  7506. (assoc key org-link-abbrev-alist)))
  7507. (tag (and (match-end 2) (match-string 3 link)))
  7508. rpl)
  7509. (if (not as)
  7510. link
  7511. (setq rpl (cdr as))
  7512. (cond
  7513. ((symbolp rpl) (funcall rpl tag))
  7514. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7515. ((string-match "%h" rpl)
  7516. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7517. (t (concat rpl tag)))))
  7518. link))
  7519. ;;; Storing and inserting links
  7520. (defvar org-insert-link-history nil
  7521. "Minibuffer history for links inserted with `org-insert-link'.")
  7522. (defvar org-stored-links nil
  7523. "Contains the links stored with `org-store-link'.")
  7524. (defvar org-store-link-plist nil
  7525. "Plist with info about the most recently link created with `org-store-link'.")
  7526. (defvar org-link-protocols nil
  7527. "Link protocols added to Org-mode using `org-add-link-type'.")
  7528. (defvar org-store-link-functions nil
  7529. "List of functions that are called to create and store a link.
  7530. Each function will be called in turn until one returns a non-nil
  7531. value. Each function should check if it is responsible for creating
  7532. this link (for example by looking at the major mode).
  7533. If not, it must exit and return nil.
  7534. If yes, it should return a non-nil value after a calling
  7535. `org-store-link-props' with a list of properties and values.
  7536. Special properties are:
  7537. :type The link prefix, like \"http\". This must be given.
  7538. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7539. This is obligatory as well.
  7540. :description Optional default description for the second pair
  7541. of brackets in an Org-mode link. The user can still change
  7542. this when inserting this link into an Org-mode buffer.
  7543. In addition to these, any additional properties can be specified
  7544. and then used in remember templates.")
  7545. (defun org-add-link-type (type &optional follow export)
  7546. "Add TYPE to the list of `org-link-types'.
  7547. Re-compute all regular expressions depending on `org-link-types'
  7548. FOLLOW and EXPORT are two functions.
  7549. FOLLOW should take the link path as the single argument and do whatever
  7550. is necessary to follow the link, for example find a file or display
  7551. a mail message.
  7552. EXPORT should format the link path for export to one of the export formats.
  7553. It should be a function accepting three arguments:
  7554. path the path of the link, the text after the prefix (like \"http:\")
  7555. desc the description of the link, if any, or a description added by
  7556. org-export-normalize-links if there is none
  7557. format the export format, a symbol like `html' or `latex' or `ascii'..
  7558. The function may use the FORMAT information to return different values
  7559. depending on the format. The return value will be put literally into
  7560. the exported file. If the return value is nil, this means Org should
  7561. do what it normally does with links which do not have EXPORT defined.
  7562. Org-mode has a built-in default for exporting links. If you are happy with
  7563. this default, there is no need to define an export function for the link
  7564. type. For a simple example of an export function, see `org-bbdb.el'."
  7565. (add-to-list 'org-link-types type t)
  7566. (org-make-link-regexps)
  7567. (if (assoc type org-link-protocols)
  7568. (setcdr (assoc type org-link-protocols) (list follow export))
  7569. (push (list type follow export) org-link-protocols)))
  7570. (defvar org-agenda-buffer-name)
  7571. ;;;###autoload
  7572. (defun org-store-link (arg)
  7573. "\\<org-mode-map>Store an org-link to the current location.
  7574. This link is added to `org-stored-links' and can later be inserted
  7575. into an org-buffer with \\[org-insert-link].
  7576. For some link types, a prefix arg is interpreted:
  7577. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7578. For file links, arg negates `org-context-in-file-links'."
  7579. (interactive "P")
  7580. (org-load-modules-maybe)
  7581. (setq org-store-link-plist nil) ; reset
  7582. (org-with-limited-levels
  7583. (let (link cpltxt desc description search txt custom-id agenda-link)
  7584. (cond
  7585. ((run-hook-with-args-until-success 'org-store-link-functions)
  7586. (setq link (plist-get org-store-link-plist :link)
  7587. desc (or (plist-get org-store-link-plist :description) link)))
  7588. ((equal (buffer-name) "*Org Edit Src Example*")
  7589. (let (label gc)
  7590. (while (or (not label)
  7591. (save-excursion
  7592. (save-restriction
  7593. (widen)
  7594. (goto-char (point-min))
  7595. (re-search-forward
  7596. (regexp-quote (format org-coderef-label-format label))
  7597. nil t))))
  7598. (when label (message "Label exists already") (sit-for 2))
  7599. (setq label (read-string "Code line label: " label)))
  7600. (end-of-line 1)
  7601. (setq link (format org-coderef-label-format label))
  7602. (setq gc (- 79 (length link)))
  7603. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7604. (insert link)
  7605. (setq link (concat "(" label ")") desc nil)))
  7606. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7607. ;; We are in the agenda, link to referenced location
  7608. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7609. (get-text-property (point) 'org-marker))))
  7610. (when m
  7611. (org-with-point-at m
  7612. (setq agenda-link
  7613. (if (org-called-interactively-p 'any)
  7614. (call-interactively 'org-store-link)
  7615. (org-store-link nil)))))))
  7616. ((eq major-mode 'calendar-mode)
  7617. (let ((cd (calendar-cursor-to-date)))
  7618. (setq link
  7619. (format-time-string
  7620. (car org-time-stamp-formats)
  7621. (apply 'encode-time
  7622. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7623. nil nil nil))))
  7624. (org-store-link-props :type "calendar" :date cd)))
  7625. ((eq major-mode 'w3-mode)
  7626. (setq cpltxt (if (and (buffer-name)
  7627. (not (string-match "Untitled" (buffer-name))))
  7628. (buffer-name)
  7629. (url-view-url t))
  7630. link (org-make-link (url-view-url t)))
  7631. (org-store-link-props :type "w3" :url (url-view-url t)))
  7632. ((eq major-mode 'w3m-mode)
  7633. (setq cpltxt (or w3m-current-title w3m-current-url)
  7634. link (org-make-link w3m-current-url))
  7635. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7636. ((setq search (run-hook-with-args-until-success
  7637. 'org-create-file-search-functions))
  7638. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7639. "::" search))
  7640. (setq cpltxt (or description link)))
  7641. ((eq major-mode 'image-mode)
  7642. (setq cpltxt (concat "file:"
  7643. (abbreviate-file-name buffer-file-name))
  7644. link (org-make-link cpltxt))
  7645. (org-store-link-props :type "image" :file buffer-file-name))
  7646. ((eq major-mode 'dired-mode)
  7647. ;; link to the file in the current line
  7648. (let ((file (dired-get-filename nil t)))
  7649. (setq file (if file
  7650. (abbreviate-file-name
  7651. (expand-file-name (dired-get-filename nil t)))
  7652. ;; otherwise, no file so use current directory.
  7653. default-directory))
  7654. (setq cpltxt (concat "file:" file)
  7655. link (org-make-link cpltxt))))
  7656. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7657. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7658. (cond
  7659. ((org-in-regexp "<<\\(.*?\\)>>")
  7660. (setq cpltxt
  7661. (concat "file:"
  7662. (abbreviate-file-name
  7663. (buffer-file-name (buffer-base-buffer)))
  7664. "::" (match-string 1))
  7665. link (org-make-link cpltxt)))
  7666. ((and (featurep 'org-id)
  7667. (or (eq org-link-to-org-use-id t)
  7668. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7669. (org-called-interactively-p 'any))
  7670. (and (eq org-link-to-org-use-id
  7671. 'create-if-interactive-and-no-custom-id)
  7672. (org-called-interactively-p 'any)
  7673. (not custom-id))
  7674. (and org-link-to-org-use-id
  7675. (org-entry-get nil "ID"))))
  7676. ;; We can make a link using the ID.
  7677. (setq link (condition-case nil
  7678. (prog1 (org-id-store-link)
  7679. (setq desc (plist-get org-store-link-plist
  7680. :description)))
  7681. (error
  7682. ;; probably before first headline, link to file only
  7683. (concat "file:"
  7684. (abbreviate-file-name
  7685. (buffer-file-name (buffer-base-buffer))))))))
  7686. (t
  7687. ;; Just link to current headline
  7688. (setq cpltxt (concat "file:"
  7689. (abbreviate-file-name
  7690. (buffer-file-name (buffer-base-buffer)))))
  7691. ;; Add a context search string
  7692. (when (org-xor org-context-in-file-links arg)
  7693. (setq txt (cond
  7694. ((org-on-heading-p) nil)
  7695. ((org-region-active-p)
  7696. (buffer-substring (region-beginning) (region-end)))
  7697. (t nil)))
  7698. (when (or (null txt) (string-match "\\S-" txt))
  7699. (setq cpltxt
  7700. (concat cpltxt "::"
  7701. (condition-case nil
  7702. (org-make-org-heading-search-string txt)
  7703. (error "")))
  7704. desc (or (nth 4 (ignore-errors
  7705. (org-heading-components))) "NONE"))))
  7706. (if (string-match "::\\'" cpltxt)
  7707. (setq cpltxt (substring cpltxt 0 -2)))
  7708. (setq link (org-make-link cpltxt)))))
  7709. ((buffer-file-name (buffer-base-buffer))
  7710. ;; Just link to this file here.
  7711. (setq cpltxt (concat "file:"
  7712. (abbreviate-file-name
  7713. (buffer-file-name (buffer-base-buffer)))))
  7714. ;; Add a context string
  7715. (when (org-xor org-context-in-file-links arg)
  7716. (setq txt (if (org-region-active-p)
  7717. (buffer-substring (region-beginning) (region-end))
  7718. (buffer-substring (point-at-bol) (point-at-eol))))
  7719. ;; Only use search option if there is some text.
  7720. (when (string-match "\\S-" txt)
  7721. (setq cpltxt
  7722. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7723. desc "NONE")))
  7724. (setq link (org-make-link cpltxt)))
  7725. ((org-called-interactively-p 'interactive)
  7726. (error "Cannot link to a buffer which is not visiting a file"))
  7727. (t (setq link nil)))
  7728. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7729. (setq link (or link cpltxt)
  7730. desc (or desc cpltxt))
  7731. (if (equal desc "NONE") (setq desc nil))
  7732. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7733. (progn
  7734. (setq org-stored-links
  7735. (cons (list link desc) org-stored-links))
  7736. (message "Stored: %s" (or desc link))
  7737. (when custom-id
  7738. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7739. "::#" custom-id))
  7740. (setq org-stored-links
  7741. (cons (list link desc) org-stored-links))))
  7742. (or agenda-link (and link (org-make-link-string link desc)))))))
  7743. (defun org-store-link-props (&rest plist)
  7744. "Store link properties, extract names and addresses."
  7745. (let (x adr)
  7746. (when (setq x (plist-get plist :from))
  7747. (setq adr (mail-extract-address-components x))
  7748. (setq plist (plist-put plist :fromname (car adr)))
  7749. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7750. (when (setq x (plist-get plist :to))
  7751. (setq adr (mail-extract-address-components x))
  7752. (setq plist (plist-put plist :toname (car adr)))
  7753. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7754. (let ((from (plist-get plist :from))
  7755. (to (plist-get plist :to)))
  7756. (when (and from to org-from-is-user-regexp)
  7757. (setq plist
  7758. (plist-put plist :fromto
  7759. (if (string-match org-from-is-user-regexp from)
  7760. (concat "to %t")
  7761. (concat "from %f"))))))
  7762. (setq org-store-link-plist plist))
  7763. (defun org-add-link-props (&rest plist)
  7764. "Add these properties to the link property list."
  7765. (let (key value)
  7766. (while plist
  7767. (setq key (pop plist) value (pop plist))
  7768. (setq org-store-link-plist
  7769. (plist-put org-store-link-plist key value)))))
  7770. (defun org-email-link-description (&optional fmt)
  7771. "Return the description part of an email link.
  7772. This takes information from `org-store-link-plist' and formats it
  7773. according to FMT (default from `org-email-link-description-format')."
  7774. (setq fmt (or fmt org-email-link-description-format))
  7775. (let* ((p org-store-link-plist)
  7776. (to (plist-get p :toaddress))
  7777. (from (plist-get p :fromaddress))
  7778. (table
  7779. (list
  7780. (cons "%c" (plist-get p :fromto))
  7781. (cons "%F" (plist-get p :from))
  7782. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7783. (cons "%T" (plist-get p :to))
  7784. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7785. (cons "%s" (plist-get p :subject))
  7786. (cons "%d" (plist-get p :date))
  7787. (cons "%m" (plist-get p :message-id)))))
  7788. (when (string-match "%c" fmt)
  7789. ;; Check if the user wrote this message
  7790. (if (and org-from-is-user-regexp from to
  7791. (save-match-data (string-match org-from-is-user-regexp from)))
  7792. (setq fmt (replace-match "to %t" t t fmt))
  7793. (setq fmt (replace-match "from %f" t t fmt))))
  7794. (org-replace-escapes fmt table)))
  7795. (defun org-make-org-heading-search-string (&optional string heading)
  7796. "Make search string for STRING or current headline."
  7797. (interactive)
  7798. (let ((s (or string (org-get-heading)))
  7799. (lines org-context-in-file-links))
  7800. (unless (and string (not heading))
  7801. ;; We are using a headline, clean up garbage in there.
  7802. (if (string-match org-todo-regexp s)
  7803. (setq s (replace-match "" t t s)))
  7804. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7805. (setq s (replace-match "" t t s)))
  7806. (setq s (org-trim s))
  7807. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7808. org-comment-string "\\)") s)
  7809. (setq s (replace-match "" t t s)))
  7810. (while (string-match org-ts-regexp s)
  7811. (setq s (replace-match "" t t s))))
  7812. (or string (setq s (concat "*" s))) ; Add * for headlines
  7813. (when (and string (integerp lines) (> lines 0))
  7814. (let ((slines (org-split-string s "\n")))
  7815. (when (< lines (length slines))
  7816. (setq s (mapconcat
  7817. 'identity
  7818. (reverse (nthcdr (- (length slines) lines)
  7819. (reverse slines))) "\n")))))
  7820. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7821. (defun org-make-link (&rest strings)
  7822. "Concatenate STRINGS."
  7823. (apply 'concat strings))
  7824. (defun org-make-link-string (link &optional description)
  7825. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7826. (unless (string-match "\\S-" link)
  7827. (error "Empty link"))
  7828. (when (and description
  7829. (stringp description)
  7830. (not (string-match "\\S-" description)))
  7831. (setq description nil))
  7832. (when (stringp description)
  7833. ;; Remove brackets from the description, they are fatal.
  7834. (while (string-match "\\[" description)
  7835. (setq description (replace-match "{" t t description)))
  7836. (while (string-match "\\]" description)
  7837. (setq description (replace-match "}" t t description))))
  7838. (when (equal link description)
  7839. ;; No description needed, it is identical
  7840. (setq description nil))
  7841. (when (and (not description)
  7842. (not (string-match (org-image-file-name-regexp) link))
  7843. (not (equal link (org-link-escape link))))
  7844. (setq description (org-extract-attributes link)))
  7845. (setq link
  7846. (cond ((string-match (org-image-file-name-regexp) link) link)
  7847. ((string-match org-link-types-re link)
  7848. (concat (match-string 1 link)
  7849. (org-link-escape (substring link (match-end 1)))))
  7850. (t (org-link-escape link))))
  7851. (concat "[[" link "]"
  7852. (if description (concat "[" description "]") "")
  7853. "]"))
  7854. (defconst org-link-escape-chars
  7855. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  7856. "List of characters that should be escaped in link.
  7857. This is the list that is used for internal purposes.")
  7858. (defvar org-url-encoding-use-url-hexify nil)
  7859. (defconst org-link-escape-chars-browser
  7860. '(?\ )
  7861. "List of escapes for characters that are problematic in links.
  7862. This is the list that is used before handing over to the browser.")
  7863. (defun org-link-escape (text &optional table merge)
  7864. "Return percent escaped representation of TEXT.
  7865. TEXT is a string with the text to escape.
  7866. Optional argument TABLE is a list with characters that should be
  7867. escaped. When nil, `org-link-escape-chars' is used.
  7868. If optional argument MERGE is set, merge TABLE into
  7869. `org-link-escape-chars'."
  7870. (if (and org-url-encoding-use-url-hexify (not table))
  7871. (url-hexify-string text)
  7872. (cond
  7873. ((and table merge)
  7874. (mapc (lambda (defchr)
  7875. (unless (member defchr table)
  7876. (setq table (cons defchr table)))) org-link-escape-chars))
  7877. ((null table)
  7878. (setq table org-link-escape-chars)))
  7879. (mapconcat
  7880. (lambda (char)
  7881. (if (or (member char table)
  7882. (< char 32) (= char 37) (> char 126))
  7883. (mapconcat (lambda (sequence-element)
  7884. (format "%%%.2X" sequence-element))
  7885. (or (encode-coding-char char 'utf-8)
  7886. (error "Unable to percent escape character: %s"
  7887. (char-to-string char))) "")
  7888. (char-to-string char))) text "")))
  7889. (defun org-link-unescape (str)
  7890. "Unhex hexified unicode strings as returned from the JavaScript function
  7891. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  7892. (unless (and (null str) (string= "" str))
  7893. (let ((pos 0) (case-fold-search t) unhexed)
  7894. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  7895. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  7896. (setq str (replace-match unhexed t t str))
  7897. (setq pos (+ pos (length unhexed))))))
  7898. str)
  7899. (defun org-link-unescape-compound (hex)
  7900. "Unhexify unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  7901. Note: this function also decodes single byte encodings like
  7902. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  7903. (save-match-data
  7904. (let* ((bytes (cdr (split-string hex "%")))
  7905. (ret "")
  7906. (eat 0)
  7907. (sum 0))
  7908. (while bytes
  7909. (let* ((val (string-to-number (pop bytes) 16))
  7910. (shift-xor
  7911. (if (= 0 eat)
  7912. (cond
  7913. ((>= val 252) (cons 6 252))
  7914. ((>= val 248) (cons 5 248))
  7915. ((>= val 240) (cons 4 240))
  7916. ((>= val 224) (cons 3 224))
  7917. ((>= val 192) (cons 2 192))
  7918. (t (cons 0 0)))
  7919. (cons 6 128))))
  7920. (if (>= val 192) (setq eat (car shift-xor)))
  7921. (setq val (logxor val (cdr shift-xor)))
  7922. (setq sum (+ (lsh sum (car shift-xor)) val))
  7923. (if (> eat 0) (setq eat (- eat 1)))
  7924. (cond
  7925. ((= 0 eat) ;multi byte
  7926. (setq ret (concat ret (org-char-to-string sum)))
  7927. (setq sum 0))
  7928. ((not bytes) ; single byte(s)
  7929. (setq ret (org-link-unescape-single-byte-sequence hex))))
  7930. )) ;; end (while bytes
  7931. ret )))
  7932. (defun org-link-unescape-single-byte-sequence (hex)
  7933. "Unhexify hex-encoded single byte character sequences."
  7934. (mapconcat (lambda (byte)
  7935. (char-to-string (string-to-number byte 16)))
  7936. (cdr (split-string hex "%")) ""))
  7937. (defun org-xor (a b)
  7938. "Exclusive or."
  7939. (if a (not b) b))
  7940. (defun org-fixup-message-id-for-http (s)
  7941. "Replace special characters in a message id, so it can be used in an http query."
  7942. (when (string-match "%" s)
  7943. (setq s (mapconcat (lambda (c)
  7944. (if (eq c ?%)
  7945. "%25"
  7946. (char-to-string c)))
  7947. s "")))
  7948. (while (string-match "<" s)
  7949. (setq s (replace-match "%3C" t t s)))
  7950. (while (string-match ">" s)
  7951. (setq s (replace-match "%3E" t t s)))
  7952. (while (string-match "@" s)
  7953. (setq s (replace-match "%40" t t s)))
  7954. s)
  7955. ;;;###autoload
  7956. (defun org-insert-link-global ()
  7957. "Insert a link like Org-mode does.
  7958. This command can be called in any mode to insert a link in Org-mode syntax."
  7959. (interactive)
  7960. (org-load-modules-maybe)
  7961. (org-run-like-in-org-mode 'org-insert-link))
  7962. (defun org-insert-link (&optional complete-file link-location)
  7963. "Insert a link. At the prompt, enter the link.
  7964. Completion can be used to insert any of the link protocol prefixes like
  7965. http or ftp in use.
  7966. The history can be used to select a link previously stored with
  7967. `org-store-link'. When the empty string is entered (i.e. if you just
  7968. press RET at the prompt), the link defaults to the most recently
  7969. stored link. As SPC triggers completion in the minibuffer, you need to
  7970. use M-SPC or C-q SPC to force the insertion of a space character.
  7971. You will also be prompted for a description, and if one is given, it will
  7972. be displayed in the buffer instead of the link.
  7973. If there is already a link at point, this command will allow you to edit link
  7974. and description parts.
  7975. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7976. be selected using completion. The path to the file will be relative to the
  7977. current directory if the file is in the current directory or a subdirectory.
  7978. Otherwise, the link will be the absolute path as completed in the minibuffer
  7979. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7980. option `org-link-file-path-type'.
  7981. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7982. the current directory or below.
  7983. With three \\[universal-argument] prefixes, negate the meaning of
  7984. `org-keep-stored-link-after-insertion'.
  7985. If `org-make-link-description-function' is non-nil, this function will be
  7986. called with the link target, and the result will be the default
  7987. link description.
  7988. If the LINK-LOCATION parameter is non-nil, this value will be
  7989. used as the link location instead of reading one interactively."
  7990. (interactive "P")
  7991. (let* ((wcf (current-window-configuration))
  7992. (region (if (org-region-active-p)
  7993. (buffer-substring (region-beginning) (region-end))))
  7994. (remove (and region (list (region-beginning) (region-end))))
  7995. (desc region)
  7996. tmphist ; byte-compile incorrectly complains about this
  7997. (link link-location)
  7998. entry file all-prefixes)
  7999. (cond
  8000. (link-location) ; specified by arg, just use it.
  8001. ((org-in-regexp org-bracket-link-regexp 1)
  8002. ;; We do have a link at point, and we are going to edit it.
  8003. (setq remove (list (match-beginning 0) (match-end 0)))
  8004. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8005. (setq link (read-string "Link: "
  8006. (org-link-unescape
  8007. (org-match-string-no-properties 1)))))
  8008. ((or (org-in-regexp org-angle-link-re)
  8009. (org-in-regexp org-plain-link-re))
  8010. ;; Convert to bracket link
  8011. (setq remove (list (match-beginning 0) (match-end 0))
  8012. link (read-string "Link: "
  8013. (org-remove-angle-brackets (match-string 0)))))
  8014. ((member complete-file '((4) (16)))
  8015. ;; Completing read for file names.
  8016. (setq link (org-file-complete-link complete-file)))
  8017. (t
  8018. ;; Read link, with completion for stored links.
  8019. (with-output-to-temp-buffer "*Org Links*"
  8020. (princ "Insert a link.
  8021. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8022. (when org-stored-links
  8023. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8024. (princ (mapconcat
  8025. (lambda (x)
  8026. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8027. (reverse org-stored-links) "\n"))))
  8028. (let ((cw (selected-window)))
  8029. (select-window (get-buffer-window "*Org Links*" 'visible))
  8030. (with-current-buffer "*Org Links*" (setq truncate-lines) t)
  8031. (unless (pos-visible-in-window-p (point-max))
  8032. (org-fit-window-to-buffer))
  8033. (and (window-live-p cw) (select-window cw)))
  8034. ;; Fake a link history, containing the stored links.
  8035. (setq tmphist (append (mapcar 'car org-stored-links)
  8036. org-insert-link-history))
  8037. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8038. (mapcar 'car org-link-abbrev-alist)
  8039. org-link-types))
  8040. (unwind-protect
  8041. (progn
  8042. (setq link
  8043. (let ((org-completion-use-ido nil)
  8044. (org-completion-use-iswitchb nil))
  8045. (org-completing-read
  8046. "Link: "
  8047. (append
  8048. (mapcar (lambda (x) (list (concat x ":")))
  8049. all-prefixes)
  8050. (mapcar 'car org-stored-links))
  8051. nil nil nil
  8052. 'tmphist
  8053. (car (car org-stored-links)))))
  8054. (if (not (string-match "\\S-" link))
  8055. (error "No link selected"))
  8056. (if (or (member link all-prefixes)
  8057. (and (equal ":" (substring link -1))
  8058. (member (substring link 0 -1) all-prefixes)
  8059. (setq link (substring link 0 -1))))
  8060. (setq link (org-link-try-special-completion link))))
  8061. (set-window-configuration wcf)
  8062. (kill-buffer "*Org Links*"))
  8063. (setq entry (assoc link org-stored-links))
  8064. (or entry (push link org-insert-link-history))
  8065. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8066. (not org-keep-stored-link-after-insertion))
  8067. (setq org-stored-links (delq (assoc link org-stored-links)
  8068. org-stored-links)))
  8069. (setq desc (or desc (nth 1 entry)))))
  8070. (if (string-match org-plain-link-re link)
  8071. ;; URL-like link, normalize the use of angular brackets.
  8072. (setq link (org-make-link (org-remove-angle-brackets link))))
  8073. ;; Check if we are linking to the current file with a search option
  8074. ;; If yes, simplify the link by using only the search option.
  8075. (when (and buffer-file-name
  8076. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8077. (let* ((path (match-string 1 link))
  8078. (case-fold-search nil)
  8079. (search (match-string 2 link)))
  8080. (save-match-data
  8081. (if (equal (file-truename buffer-file-name) (file-truename path))
  8082. ;; We are linking to this same file, with a search option
  8083. (setq link search)))))
  8084. ;; Check if we can/should use a relative path. If yes, simplify the link
  8085. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8086. (let* ((type (match-string 1 link))
  8087. (path (match-string 2 link))
  8088. (origpath path)
  8089. (case-fold-search nil))
  8090. (cond
  8091. ((or (eq org-link-file-path-type 'absolute)
  8092. (equal complete-file '(16)))
  8093. (setq path (abbreviate-file-name (expand-file-name path))))
  8094. ((eq org-link-file-path-type 'noabbrev)
  8095. (setq path (expand-file-name path)))
  8096. ((eq org-link-file-path-type 'relative)
  8097. (setq path (file-relative-name path)))
  8098. (t
  8099. (save-match-data
  8100. (if (string-match (concat "^" (regexp-quote
  8101. (expand-file-name
  8102. (file-name-as-directory
  8103. default-directory))))
  8104. (expand-file-name path))
  8105. ;; We are linking a file with relative path name.
  8106. (setq path (substring (expand-file-name path)
  8107. (match-end 0)))
  8108. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8109. (setq link (concat type path))
  8110. (if (equal desc origpath)
  8111. (setq desc path))))
  8112. (if org-make-link-description-function
  8113. (setq desc (funcall org-make-link-description-function link desc)))
  8114. (setq desc (read-string "Description: " desc))
  8115. (unless (string-match "\\S-" desc) (setq desc nil))
  8116. (if remove (apply 'delete-region remove))
  8117. (insert (org-make-link-string link desc))))
  8118. (defun org-link-try-special-completion (type)
  8119. "If there is completion support for link type TYPE, offer it."
  8120. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8121. (if (functionp fun)
  8122. (funcall fun)
  8123. (read-string "Link (no completion support): " (concat type ":")))))
  8124. (defun org-file-complete-link (&optional arg)
  8125. "Create a file link using completion."
  8126. (let (file link)
  8127. (setq file (read-file-name "File: "))
  8128. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8129. (pwd1 (file-name-as-directory (abbreviate-file-name
  8130. (expand-file-name ".")))))
  8131. (cond
  8132. ((equal arg '(16))
  8133. (setq link (org-make-link
  8134. "file:"
  8135. (abbreviate-file-name (expand-file-name file)))))
  8136. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8137. (setq link (org-make-link "file:" (match-string 1 file))))
  8138. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8139. (expand-file-name file))
  8140. (setq link (org-make-link
  8141. "file:" (match-string 1 (expand-file-name file)))))
  8142. (t (setq link (org-make-link "file:" file)))))
  8143. link))
  8144. (defun org-completing-read (&rest args)
  8145. "Completing-read with SPACE being a normal character."
  8146. (let ((minibuffer-local-completion-map
  8147. (copy-keymap minibuffer-local-completion-map)))
  8148. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8149. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8150. (apply 'org-icompleting-read args)))
  8151. (defun org-completing-read-no-i (&rest args)
  8152. (let (org-completion-use-ido org-completion-use-iswitchb)
  8153. (apply 'org-completing-read args)))
  8154. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8155. "Use iswitch as a completing-read replacement to choose from choices.
  8156. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8157. from."
  8158. (let* ((iswitchb-use-virtual-buffers nil)
  8159. (iswitchb-make-buflist-hook
  8160. (lambda ()
  8161. (setq iswitchb-temp-buflist choices))))
  8162. (iswitchb-read-buffer prompt)))
  8163. (defun org-icompleting-read (&rest args)
  8164. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8165. (org-without-partial-completion
  8166. (if (and org-completion-use-ido
  8167. (fboundp 'ido-completing-read)
  8168. (boundp 'ido-mode) ido-mode
  8169. (listp (second args)))
  8170. (let ((ido-enter-matching-directory nil))
  8171. (apply 'ido-completing-read (concat (car args))
  8172. (if (consp (car (nth 1 args)))
  8173. (mapcar 'car (nth 1 args))
  8174. (nth 1 args))
  8175. (cddr args)))
  8176. (if (and org-completion-use-iswitchb
  8177. (boundp 'iswitchb-mode) iswitchb-mode
  8178. (listp (second args)))
  8179. (apply 'org-iswitchb-completing-read (concat (car args))
  8180. (if (consp (car (nth 1 args)))
  8181. (mapcar 'car (nth 1 args))
  8182. (nth 1 args))
  8183. (cddr args))
  8184. (apply 'completing-read args)))))
  8185. (defun org-extract-attributes (s)
  8186. "Extract the attributes cookie from a string and set as text property."
  8187. (let (a attr (start 0) key value)
  8188. (save-match-data
  8189. (when (string-match "{{\\([^}]+\\)}}$" s)
  8190. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8191. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8192. (setq key (match-string 1 a) value (match-string 2 a)
  8193. start (match-end 0)
  8194. attr (plist-put attr (intern key) value))))
  8195. (org-add-props s nil 'org-attr attr))
  8196. s))
  8197. (defun org-extract-attributes-from-string (tag)
  8198. (let (key value attr)
  8199. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8200. (setq key (match-string 1 tag) value (match-string 2 tag)
  8201. tag (replace-match "" t t tag)
  8202. attr (plist-put attr (intern key) value)))
  8203. (cons tag attr)))
  8204. (defun org-attributes-to-string (plist)
  8205. "Format a property list into an HTML attribute list."
  8206. (let ((s "") key value)
  8207. (while plist
  8208. (setq key (pop plist) value (pop plist))
  8209. (and value
  8210. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8211. s))
  8212. ;;; Opening/following a link
  8213. (defvar org-link-search-failed nil)
  8214. (defvar org-open-link-functions nil
  8215. "Hook for functions finding a plain text link.
  8216. These functions must take a single argument, the link content.
  8217. They will be called for links that look like [[link text][description]]
  8218. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8219. like a filename (e.g. \"./blue.png\").
  8220. These functions will be called *before* Org attempts to resolve the
  8221. link by doing text searches in the current buffer - so if you want a
  8222. link \"[[target]]\" to still find \"<<target>>\", your function should
  8223. handle this as a special case.
  8224. When the function does handle the link, it must return a non-nil value.
  8225. If it decides that it is not responsible for this link, it must return
  8226. nil to indicate that that Org-mode can continue with other options
  8227. like exact and fuzzy text search.")
  8228. (defun org-next-link ()
  8229. "Move forward to the next link.
  8230. If the link is in hidden text, expose it."
  8231. (interactive)
  8232. (when (and org-link-search-failed (eq this-command last-command))
  8233. (goto-char (point-min))
  8234. (message "Link search wrapped back to beginning of buffer"))
  8235. (setq org-link-search-failed nil)
  8236. (let* ((pos (point))
  8237. (ct (org-context))
  8238. (a (assoc :link ct)))
  8239. (if a (goto-char (nth 2 a)))
  8240. (if (re-search-forward org-any-link-re nil t)
  8241. (progn
  8242. (goto-char (match-beginning 0))
  8243. (if (outline-invisible-p) (org-show-context)))
  8244. (goto-char pos)
  8245. (setq org-link-search-failed t)
  8246. (error "No further link found"))))
  8247. (defun org-previous-link ()
  8248. "Move backward to the previous link.
  8249. If the link is in hidden text, expose it."
  8250. (interactive)
  8251. (when (and org-link-search-failed (eq this-command last-command))
  8252. (goto-char (point-max))
  8253. (message "Link search wrapped back to end of buffer"))
  8254. (setq org-link-search-failed nil)
  8255. (let* ((pos (point))
  8256. (ct (org-context))
  8257. (a (assoc :link ct)))
  8258. (if a (goto-char (nth 1 a)))
  8259. (if (re-search-backward org-any-link-re nil t)
  8260. (progn
  8261. (goto-char (match-beginning 0))
  8262. (if (outline-invisible-p) (org-show-context)))
  8263. (goto-char pos)
  8264. (setq org-link-search-failed t)
  8265. (error "No further link found"))))
  8266. (defun org-translate-link (s)
  8267. "Translate a link string if a translation function has been defined."
  8268. (if (and org-link-translation-function
  8269. (fboundp org-link-translation-function)
  8270. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8271. (progn
  8272. (setq s (funcall org-link-translation-function
  8273. (match-string 1) (match-string 2)))
  8274. (concat (car s) ":" (cdr s)))
  8275. s))
  8276. (defun org-translate-link-from-planner (type path)
  8277. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8278. This is still an experimental function, your mileage may vary."
  8279. (cond
  8280. ((member type '("http" "https" "news" "ftp"))
  8281. ;; standard Internet links are the same.
  8282. nil)
  8283. ((and (equal type "irc") (string-match "^//" path))
  8284. ;; Planner has two / at the beginning of an irc link, we have 1.
  8285. ;; We should have zero, actually....
  8286. (setq path (substring path 1)))
  8287. ((and (equal type "lisp") (string-match "^/" path))
  8288. ;; Planner has a slash, we do not.
  8289. (setq type "elisp" path (substring path 1)))
  8290. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8291. ;; A typical message link. Planner has the id after the final slash,
  8292. ;; we separate it with a hash mark
  8293. (setq path (concat (match-string 1 path) "#"
  8294. (org-remove-angle-brackets (match-string 2 path)))))
  8295. )
  8296. (cons type path))
  8297. (defun org-find-file-at-mouse (ev)
  8298. "Open file link or URL at mouse."
  8299. (interactive "e")
  8300. (mouse-set-point ev)
  8301. (org-open-at-point 'in-emacs))
  8302. (defun org-open-at-mouse (ev)
  8303. "Open file link or URL at mouse."
  8304. (interactive "e")
  8305. (mouse-set-point ev)
  8306. (if (eq major-mode 'org-agenda-mode)
  8307. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8308. (org-open-at-point))
  8309. (defvar org-window-config-before-follow-link nil
  8310. "The window configuration before following a link.
  8311. This is saved in case the need arises to restore it.")
  8312. (defvar org-open-link-marker (make-marker)
  8313. "Marker pointing to the location where `org-open-at-point; was called.")
  8314. ;;;###autoload
  8315. (defun org-open-at-point-global ()
  8316. "Follow a link like Org-mode does.
  8317. This command can be called in any mode to follow a link that has
  8318. Org-mode syntax."
  8319. (interactive)
  8320. (org-run-like-in-org-mode 'org-open-at-point))
  8321. ;;;###autoload
  8322. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8323. "Open a link in the string S, as if it was in Org-mode."
  8324. (interactive "sLink: \nP")
  8325. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8326. (with-temp-buffer
  8327. (let ((org-inhibit-startup t))
  8328. (org-mode)
  8329. (insert s)
  8330. (goto-char (point-min))
  8331. (when reference-buffer
  8332. (setq org-link-abbrev-alist-local
  8333. (with-current-buffer reference-buffer
  8334. org-link-abbrev-alist-local)))
  8335. (org-open-at-point arg reference-buffer)))))
  8336. (defvar org-open-at-point-functions nil
  8337. "Hook that is run when following a link at point.
  8338. Functions in this hook must return t if they identify and follow
  8339. a link at point. If they don't find anything interesting at point,
  8340. they must return nil.")
  8341. (defun org-open-at-point (&optional arg reference-buffer)
  8342. "Open link at or after point.
  8343. If there is no link at point, this function will search forward up to
  8344. the end of the current line.
  8345. Normally, files will be opened by an appropriate application. If the
  8346. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8347. With a double prefix argument, try to open outside of Emacs, in the
  8348. application the system uses for this file type."
  8349. (interactive "P")
  8350. ;; if in a code block, then open the block's results
  8351. (unless (call-interactively #'org-babel-open-src-block-result)
  8352. (org-load-modules-maybe)
  8353. (move-marker org-open-link-marker (point))
  8354. (setq org-window-config-before-follow-link (current-window-configuration))
  8355. (org-remove-occur-highlights nil nil t)
  8356. (cond
  8357. ((and (org-on-heading-p)
  8358. (not (org-in-regexp
  8359. (concat org-plain-link-re "\\|"
  8360. org-bracket-link-regexp "\\|"
  8361. org-angle-link-re "\\|"
  8362. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8363. (not (get-text-property (point) 'org-linked-text)))
  8364. (or (org-offer-links-in-entry arg)
  8365. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8366. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8367. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8368. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8369. (not (org-in-regexp org-bracket-link-regexp)))
  8370. (org-footnote-action))
  8371. (t
  8372. (let (type path link line search (pos (point)))
  8373. (catch 'match
  8374. (save-excursion
  8375. (skip-chars-forward "^]\n\r")
  8376. (when (org-in-regexp org-bracket-link-regexp 1)
  8377. (setq link (org-extract-attributes
  8378. (org-link-unescape (org-match-string-no-properties 1))))
  8379. (while (string-match " *\n *" link)
  8380. (setq link (replace-match " " t t link)))
  8381. (setq link (org-link-expand-abbrev link))
  8382. (cond
  8383. ((or (file-name-absolute-p link)
  8384. (string-match "^\\.\\.?/" link))
  8385. (setq type "file" path link))
  8386. ((string-match org-link-re-with-space3 link)
  8387. (setq type (match-string 1 link) path (match-string 2 link)))
  8388. (t (setq type "thisfile" path link)))
  8389. (throw 'match t)))
  8390. (when (get-text-property (point) 'org-linked-text)
  8391. (setq type "thisfile"
  8392. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8393. (1+ (point)) (point))
  8394. path (buffer-substring
  8395. (or (previous-single-property-change pos 'org-linked-text)
  8396. (point-min))
  8397. (or (next-single-property-change pos 'org-linked-text)
  8398. (point-max))))
  8399. (throw 'match t))
  8400. (save-excursion
  8401. (when (or (org-in-regexp org-angle-link-re)
  8402. (org-in-regexp org-plain-link-re))
  8403. (setq type (match-string 1) path (match-string 2))
  8404. (throw 'match t)))
  8405. (save-excursion
  8406. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8407. (setq type "tags"
  8408. path (match-string 1))
  8409. (while (string-match ":" path)
  8410. (setq path (replace-match "+" t t path)))
  8411. (throw 'match t)))
  8412. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8413. (setq type "tree-match"
  8414. path (match-string 1))
  8415. (throw 'match t)))
  8416. (unless path
  8417. (error "No link found"))
  8418. ;; switch back to reference buffer
  8419. ;; needed when if called in a temporary buffer through
  8420. ;; org-open-link-from-string
  8421. (with-current-buffer (or reference-buffer (current-buffer))
  8422. ;; Remove any trailing spaces in path
  8423. (if (string-match " +\\'" path)
  8424. (setq path (replace-match "" t t path)))
  8425. (if (and org-link-translation-function
  8426. (fboundp org-link-translation-function))
  8427. ;; Check if we need to translate the link
  8428. (let ((tmp (funcall org-link-translation-function type path)))
  8429. (setq type (car tmp) path (cdr tmp))))
  8430. (cond
  8431. ((assoc type org-link-protocols)
  8432. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8433. ((equal type "mailto")
  8434. (let ((cmd (car org-link-mailto-program))
  8435. (args (cdr org-link-mailto-program)) args1
  8436. (address path) (subject "") a)
  8437. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8438. (setq address (match-string 1 path)
  8439. subject (org-link-escape (match-string 2 path))))
  8440. (while args
  8441. (cond
  8442. ((not (stringp (car args))) (push (pop args) args1))
  8443. (t (setq a (pop args))
  8444. (if (string-match "%a" a)
  8445. (setq a (replace-match address t t a)))
  8446. (if (string-match "%s" a)
  8447. (setq a (replace-match subject t t a)))
  8448. (push a args1))))
  8449. (apply cmd (nreverse args1))))
  8450. ((member type '("http" "https" "ftp" "news"))
  8451. (browse-url (concat type ":" (org-link-escape
  8452. path org-link-escape-chars-browser))))
  8453. ((string= type "doi")
  8454. (browse-url (concat "http://dx.doi.org/"
  8455. (org-link-escape
  8456. path org-link-escape-chars-browser))))
  8457. ((member type '("message"))
  8458. (browse-url (concat type ":" path)))
  8459. ((string= type "tags")
  8460. (org-tags-view arg path))
  8461. ((string= type "tree-match")
  8462. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8463. ((string= type "file")
  8464. (if (string-match "::\\([0-9]+\\)\\'" path)
  8465. (setq line (string-to-number (match-string 1 path))
  8466. path (substring path 0 (match-beginning 0)))
  8467. (if (string-match "::\\(.+\\)\\'" path)
  8468. (setq search (match-string 1 path)
  8469. path (substring path 0 (match-beginning 0)))))
  8470. (if (string-match "[*?{]" (file-name-nondirectory path))
  8471. (dired path)
  8472. (org-open-file path arg line search)))
  8473. ((string= type "shell")
  8474. (let ((cmd path))
  8475. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8476. (string-match org-confirm-shell-link-not-regexp cmd))
  8477. (not org-confirm-shell-link-function)
  8478. (funcall org-confirm-shell-link-function
  8479. (format "Execute \"%s\" in shell? "
  8480. (org-add-props cmd nil
  8481. 'face 'org-warning))))
  8482. (progn
  8483. (message "Executing %s" cmd)
  8484. (shell-command cmd))
  8485. (error "Abort"))))
  8486. ((string= type "elisp")
  8487. (let ((cmd path))
  8488. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8489. (string-match org-confirm-elisp-link-not-regexp cmd))
  8490. (not org-confirm-elisp-link-function)
  8491. (funcall org-confirm-elisp-link-function
  8492. (format "Execute \"%s\" as elisp? "
  8493. (org-add-props cmd nil
  8494. 'face 'org-warning))))
  8495. (message "%s => %s" cmd
  8496. (if (equal (string-to-char cmd) ?\()
  8497. (eval (read cmd))
  8498. (call-interactively (read cmd))))
  8499. (error "Abort"))))
  8500. ((and (string= type "thisfile")
  8501. (run-hook-with-args-until-success
  8502. 'org-open-link-functions path)))
  8503. ((string= type "thisfile")
  8504. (if arg
  8505. (switch-to-buffer-other-window
  8506. (org-get-buffer-for-internal-link (current-buffer)))
  8507. (org-mark-ring-push))
  8508. (let ((cmd `(org-link-search
  8509. ,path
  8510. ,(cond ((equal arg '(4)) ''occur)
  8511. ((equal arg '(16)) ''org-occur)
  8512. (t nil))
  8513. ,pos)))
  8514. (condition-case nil (eval cmd)
  8515. (error (progn (widen) (eval cmd))))))
  8516. (t
  8517. (browse-url-at-point)))))))
  8518. (move-marker org-open-link-marker nil)
  8519. (run-hook-with-args 'org-follow-link-hook)))
  8520. (defun org-offer-links-in-entry (&optional nth zero)
  8521. "Offer links in the current entry and follow the selected link.
  8522. If there is only one link, follow it immediately as well.
  8523. If NTH is an integer, immediately pick the NTH link found.
  8524. If ZERO is a string, check also this string for a link, and if
  8525. there is one, offer it as link number zero."
  8526. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8527. "\\(" org-angle-link-re "\\)\\|"
  8528. "\\(" org-plain-link-re "\\)"))
  8529. (cnt ?0)
  8530. (in-emacs (if (integerp nth) nil nth))
  8531. have-zero end links link c)
  8532. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8533. (push (match-string 0 zero) links)
  8534. (setq cnt (1- cnt) have-zero t))
  8535. (save-excursion
  8536. (org-back-to-heading t)
  8537. (setq end (save-excursion (outline-next-heading) (point)))
  8538. (while (re-search-forward re end t)
  8539. (push (match-string 0) links))
  8540. (setq links (org-uniquify (reverse links))))
  8541. (cond
  8542. ((null links)
  8543. (message "No links"))
  8544. ((equal (length links) 1)
  8545. (setq link (list (car links))))
  8546. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8547. (setq link (nth (if have-zero nth (1- nth)) links)))
  8548. (t ; we have to select a link
  8549. (save-excursion
  8550. (save-window-excursion
  8551. (delete-other-windows)
  8552. (with-output-to-temp-buffer "*Select Link*"
  8553. (mapc (lambda (l)
  8554. (if (not (string-match org-bracket-link-regexp l))
  8555. (princ (format "[%c] %s\n" (incf cnt)
  8556. (org-remove-angle-brackets l)))
  8557. (if (match-end 3)
  8558. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8559. (match-string 3 l) (match-string 1 l)))
  8560. (princ (format "[%c] %s\n" (incf cnt)
  8561. (match-string 1 l))))))
  8562. links))
  8563. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8564. (message "Select link to open, RET to open all:")
  8565. (setq c (read-char-exclusive))
  8566. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8567. (when (equal c ?q) (error "Abort"))
  8568. (if (equal c ?\C-m)
  8569. (setq link links)
  8570. (setq nth (- c ?0))
  8571. (if have-zero (setq nth (1+ nth)))
  8572. (unless (and (integerp nth) (>= (length links) nth))
  8573. (error "Invalid link selection"))
  8574. (setq link (list (nth (1- nth) links))))))
  8575. (if link
  8576. (let ((buf (current-buffer)))
  8577. (dolist (l link)
  8578. (org-open-link-from-string l in-emacs buf))
  8579. t)
  8580. nil)))
  8581. ;; Add special file links that specify the way of opening
  8582. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8583. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8584. (defun org-open-file-with-system (path)
  8585. "Open file at PATH using the system way of opening it."
  8586. (org-open-file path 'system))
  8587. (defun org-open-file-with-emacs (path)
  8588. "Open file at PATH in Emacs."
  8589. (org-open-file path 'emacs))
  8590. (defun org-remove-file-link-modifiers ()
  8591. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8592. (goto-char (point-min))
  8593. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8594. (org-if-unprotected
  8595. (replace-match "file:" t t))))
  8596. (eval-after-load "org-exp"
  8597. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8598. 'org-remove-file-link-modifiers))
  8599. ;;;; Time estimates
  8600. (defun org-get-effort (&optional pom)
  8601. "Get the effort estimate for the current entry."
  8602. (org-entry-get pom org-effort-property))
  8603. ;;; File search
  8604. (defvar org-create-file-search-functions nil
  8605. "List of functions to construct the right search string for a file link.
  8606. These functions are called in turn with point at the location to
  8607. which the link should point.
  8608. A function in the hook should first test if it would like to
  8609. handle this file type, for example by checking the `major-mode'
  8610. or the file extension. If it decides not to handle this file, it
  8611. should just return nil to give other functions a chance. If it
  8612. does handle the file, it must return the search string to be used
  8613. when following the link. The search string will be part of the
  8614. file link, given after a double colon, and `org-open-at-point'
  8615. will automatically search for it. If special measures must be
  8616. taken to make the search successful, another function should be
  8617. added to the companion hook `org-execute-file-search-functions',
  8618. which see.
  8619. A function in this hook may also use `setq' to set the variable
  8620. `description' to provide a suggestion for the descriptive text to
  8621. be used for this link when it gets inserted into an Org-mode
  8622. buffer with \\[org-insert-link].")
  8623. (defvar org-execute-file-search-functions nil
  8624. "List of functions to execute a file search triggered by a link.
  8625. Functions added to this hook must accept a single argument, the
  8626. search string that was part of the file link, the part after the
  8627. double colon. The function must first check if it would like to
  8628. handle this search, for example by checking the `major-mode' or
  8629. the file extension. If it decides not to handle this search, it
  8630. should just return nil to give other functions a chance. If it
  8631. does handle the search, it must return a non-nil value to keep
  8632. other functions from trying.
  8633. Each function can access the current prefix argument through the
  8634. variable `current-prefix-argument'. Note that a single prefix is
  8635. used to force opening a link in Emacs, so it may be good to only
  8636. use a numeric or double prefix to guide the search function.
  8637. In case this is needed, a function in this hook can also restore
  8638. the window configuration before `org-open-at-point' was called using:
  8639. (set-window-configuration org-window-config-before-follow-link)")
  8640. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8641. (defun org-link-search (s &optional type avoid-pos)
  8642. "Search for a link search option.
  8643. If S is surrounded by forward slashes, it is interpreted as a
  8644. regular expression. In org-mode files, this will create an `org-occur'
  8645. sparse tree. In ordinary files, `occur' will be used to list matches.
  8646. If the current buffer is in `dired-mode', grep will be used to search
  8647. in all files. If AVOID-POS is given, ignore matches near that position."
  8648. (let ((case-fold-search t)
  8649. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8650. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8651. (append '(("") (" ") ("\t") ("\n"))
  8652. org-emphasis-alist)
  8653. "\\|") "\\)"))
  8654. (pos (point))
  8655. (pre nil) (post nil)
  8656. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8657. (cond
  8658. ;; First check if there are any special search functions
  8659. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8660. ;; Now try the builtin stuff
  8661. ((and (equal (string-to-char s0) ?#)
  8662. (> (length s0) 1)
  8663. (save-excursion
  8664. (goto-char (point-min))
  8665. (and
  8666. (re-search-forward
  8667. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8668. (setq type 'dedicated
  8669. pos (match-beginning 0))))
  8670. ;; There is an exact target for this
  8671. (goto-char pos)
  8672. (org-back-to-heading t)))
  8673. ((save-excursion
  8674. (goto-char (point-min))
  8675. (and
  8676. (re-search-forward
  8677. (concat "<<" (regexp-quote s0) ">>") nil t)
  8678. (setq type 'dedicated
  8679. pos (match-beginning 0))))
  8680. ;; There is an exact target for this
  8681. (goto-char pos))
  8682. ((and (string-match "^(\\(.*\\))$" s0)
  8683. (save-excursion
  8684. (goto-char (point-min))
  8685. (and
  8686. (re-search-forward
  8687. (concat "[^[]" (regexp-quote
  8688. (format org-coderef-label-format
  8689. (match-string 1 s0))))
  8690. nil t)
  8691. (setq type 'dedicated
  8692. pos (1+ (match-beginning 0))))))
  8693. ;; There is a coderef target for this
  8694. (goto-char pos))
  8695. ((string-match "^/\\(.*\\)/$" s)
  8696. ;; A regular expression
  8697. (cond
  8698. ((org-mode-p)
  8699. (org-occur (match-string 1 s)))
  8700. ;;((eq major-mode 'dired-mode)
  8701. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8702. (t (org-do-occur (match-string 1 s)))))
  8703. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8704. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8705. (goto-char (point-min))
  8706. (cond
  8707. ((let (case-fold-search)
  8708. (re-search-forward (format org-complex-heading-regexp-format
  8709. (regexp-quote s))
  8710. nil t))
  8711. ;; OK, found a match
  8712. (setq type 'dedicated)
  8713. (goto-char (match-beginning 0)))
  8714. ((and (not org-link-search-inhibit-query)
  8715. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8716. (y-or-n-p "No match - create this as a new heading? "))
  8717. (goto-char (point-max))
  8718. (or (bolp) (newline))
  8719. (insert "* " s "\n")
  8720. (beginning-of-line 0))
  8721. (t
  8722. (goto-char pos)
  8723. (error "No match"))))
  8724. (t
  8725. ;; A normal search string
  8726. (when (equal (string-to-char s) ?*)
  8727. ;; Anchor on headlines, post may include tags.
  8728. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8729. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8730. s (substring s 1)))
  8731. (remove-text-properties
  8732. 0 (length s)
  8733. '(face nil mouse-face nil keymap nil fontified nil) s)
  8734. ;; Make a series of regular expressions to find a match
  8735. (setq words (org-split-string s "[ \n\r\t]+")
  8736. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8737. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8738. "\\)" markers)
  8739. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8740. re2a (concat "[ \t\r\n]" re2a_)
  8741. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8742. re4 (concat "[^a-zA-Z_]" re4_)
  8743. re1 (concat pre re2 post)
  8744. re3 (concat pre (if pre re4_ re4) post)
  8745. re5 (concat pre ".*" re4)
  8746. re2 (concat pre re2)
  8747. re2a (concat pre (if pre re2a_ re2a))
  8748. re4 (concat pre (if pre re4_ re4))
  8749. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8750. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8751. re5 "\\)"
  8752. ))
  8753. (cond
  8754. ((eq type 'org-occur) (org-occur reall))
  8755. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8756. (t (goto-char (point-min))
  8757. (setq type 'fuzzy)
  8758. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8759. (org-search-not-self 1 re1 nil t)
  8760. (org-search-not-self 1 re2 nil t)
  8761. (org-search-not-self 1 re2a nil t)
  8762. (org-search-not-self 1 re3 nil t)
  8763. (org-search-not-self 1 re4 nil t)
  8764. (org-search-not-self 1 re5 nil t)
  8765. )
  8766. (goto-char (match-beginning 1))
  8767. (goto-char pos)
  8768. (error "No match"))))))
  8769. (and (org-mode-p) (org-show-context 'link-search))
  8770. type))
  8771. (defun org-search-not-self (group &rest args)
  8772. "Execute `re-search-forward', but only accept matches that do not
  8773. enclose the position of `org-open-link-marker'."
  8774. (let ((m org-open-link-marker))
  8775. (catch 'exit
  8776. (while (apply 're-search-forward args)
  8777. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8778. (goto-char (match-end group))
  8779. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8780. (> (match-beginning 0) (marker-position m))
  8781. (< (match-end 0) (marker-position m)))
  8782. (save-match-data
  8783. (or (not (org-in-regexp
  8784. org-bracket-link-analytic-regexp 1))
  8785. (not (match-end 4)) ; no description
  8786. (and (<= (match-beginning 4) (point))
  8787. (>= (match-end 4) (point))))))
  8788. (throw 'exit (point))))))))
  8789. (defun org-get-buffer-for-internal-link (buffer)
  8790. "Return a buffer to be used for displaying the link target of internal links."
  8791. (cond
  8792. ((not org-display-internal-link-with-indirect-buffer)
  8793. buffer)
  8794. ((string-match "(Clone)$" (buffer-name buffer))
  8795. (message "Buffer is already a clone, not making another one")
  8796. ;; we also do not modify visibility in this case
  8797. buffer)
  8798. (t ; make a new indirect buffer for displaying the link
  8799. (let* ((bn (buffer-name buffer))
  8800. (ibn (concat bn "(Clone)"))
  8801. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8802. (with-current-buffer ib (org-overview))
  8803. ib))))
  8804. (defun org-do-occur (regexp &optional cleanup)
  8805. "Call the Emacs command `occur'.
  8806. If CLEANUP is non-nil, remove the printout of the regular expression
  8807. in the *Occur* buffer. This is useful if the regex is long and not useful
  8808. to read."
  8809. (occur regexp)
  8810. (when cleanup
  8811. (let ((cwin (selected-window)) win beg end)
  8812. (when (setq win (get-buffer-window "*Occur*"))
  8813. (select-window win))
  8814. (goto-char (point-min))
  8815. (when (re-search-forward "match[a-z]+" nil t)
  8816. (setq beg (match-end 0))
  8817. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8818. (setq end (1- (match-beginning 0)))))
  8819. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8820. (goto-char (point-min))
  8821. (select-window cwin))))
  8822. ;;; The mark ring for links jumps
  8823. (defvar org-mark-ring nil
  8824. "Mark ring for positions before jumps in Org-mode.")
  8825. (defvar org-mark-ring-last-goto nil
  8826. "Last position in the mark ring used to go back.")
  8827. ;; Fill and close the ring
  8828. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8829. (loop for i from 1 to org-mark-ring-length do
  8830. (push (make-marker) org-mark-ring))
  8831. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8832. org-mark-ring)
  8833. (defun org-mark-ring-push (&optional pos buffer)
  8834. "Put the current position or POS into the mark ring and rotate it."
  8835. (interactive)
  8836. (setq pos (or pos (point)))
  8837. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8838. (move-marker (car org-mark-ring)
  8839. (or pos (point))
  8840. (or buffer (current-buffer)))
  8841. (message "%s"
  8842. (substitute-command-keys
  8843. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8844. (defun org-mark-ring-goto (&optional n)
  8845. "Jump to the previous position in the mark ring.
  8846. With prefix arg N, jump back that many stored positions. When
  8847. called several times in succession, walk through the entire ring.
  8848. Org-mode commands jumping to a different position in the current file,
  8849. or to another Org-mode file, automatically push the old position
  8850. onto the ring."
  8851. (interactive "p")
  8852. (let (p m)
  8853. (if (eq last-command this-command)
  8854. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8855. (setq p org-mark-ring))
  8856. (setq org-mark-ring-last-goto p)
  8857. (setq m (car p))
  8858. (switch-to-buffer (marker-buffer m))
  8859. (goto-char m)
  8860. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8861. (defun org-remove-angle-brackets (s)
  8862. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8863. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8864. s)
  8865. (defun org-add-angle-brackets (s)
  8866. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8867. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8868. s)
  8869. (defun org-remove-double-quotes (s)
  8870. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8871. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8872. s)
  8873. ;;; Following specific links
  8874. (defun org-follow-timestamp-link ()
  8875. (cond
  8876. ((org-at-date-range-p t)
  8877. (let ((org-agenda-start-on-weekday)
  8878. (t1 (match-string 1))
  8879. (t2 (match-string 2)))
  8880. (setq t1 (time-to-days (org-time-string-to-time t1))
  8881. t2 (time-to-days (org-time-string-to-time t2)))
  8882. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8883. ((org-at-timestamp-p t)
  8884. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8885. (substring (match-string 1) 0 10)))
  8886. 1))
  8887. (t (error "This should not happen"))))
  8888. ;;; Following file links
  8889. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  8890. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  8891. (declare-function mailcap-mime-info
  8892. "mailcap" (string &optional request no-decode))
  8893. (defvar org-wait nil)
  8894. (defun org-open-file (path &optional in-emacs line search)
  8895. "Open the file at PATH.
  8896. First, this expands any special file name abbreviations. Then the
  8897. configuration variable `org-file-apps' is checked if it contains an
  8898. entry for this file type, and if yes, the corresponding command is launched.
  8899. If no application is found, Emacs simply visits the file.
  8900. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8901. With a double \\[universal-argument] \\[universal-argument] \
  8902. prefix arg, Org tries to avoid opening in Emacs
  8903. and to use an external application to visit the file.
  8904. Optional LINE specifies a line to go to, optional SEARCH a string
  8905. to search for. If LINE or SEARCH is given, the file will be
  8906. opened in Emacs, unless an entry from org-file-apps that makes
  8907. use of groups in a regexp matches.
  8908. If the file does not exist, an error is thrown."
  8909. (let* ((file (if (equal path "")
  8910. buffer-file-name
  8911. (substitute-in-file-name (expand-file-name path))))
  8912. (file-apps (append org-file-apps (org-default-apps)))
  8913. (apps (org-remove-if
  8914. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8915. (apps-dlink (org-remove-if-not
  8916. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8917. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8918. (dirp (if remp nil (file-directory-p file)))
  8919. (file (if (and dirp org-open-directory-means-index-dot-org)
  8920. (concat (file-name-as-directory file) "index.org")
  8921. file))
  8922. (a-m-a-p (assq 'auto-mode apps))
  8923. (dfile (downcase file))
  8924. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8925. (link (cond ((and (eq line nil)
  8926. (eq search nil))
  8927. file)
  8928. (line
  8929. (concat file "::" (number-to-string line)))
  8930. (search
  8931. (concat file "::" search))))
  8932. (dlink (downcase link))
  8933. (old-buffer (current-buffer))
  8934. (old-pos (point))
  8935. (old-mode major-mode)
  8936. ext cmd link-match-data)
  8937. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8938. (setq ext (match-string 1 dfile))
  8939. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8940. (setq ext (match-string 1 dfile))))
  8941. (cond
  8942. ((member in-emacs '((16) system))
  8943. (setq cmd (cdr (assoc 'system apps))))
  8944. (in-emacs (setq cmd 'emacs))
  8945. (t
  8946. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8947. (and dirp (cdr (assoc 'directory apps)))
  8948. ; first, try matching against apps-dlink
  8949. ; if we get a match here, store the match data for later
  8950. (let ((match (assoc-default dlink apps-dlink
  8951. 'string-match)))
  8952. (if match
  8953. (progn (setq link-match-data (match-data))
  8954. match)
  8955. (progn (setq in-emacs (or in-emacs line search))
  8956. nil))) ; if we have no match in apps-dlink,
  8957. ; always open the file in emacs if line or search
  8958. ; is given (for backwards compatibility)
  8959. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8960. 'string-match)
  8961. (cdr (assoc ext apps))
  8962. (cdr (assoc t apps))))))
  8963. (when (eq cmd 'system)
  8964. (setq cmd (cdr (assoc 'system apps))))
  8965. (when (eq cmd 'default)
  8966. (setq cmd (cdr (assoc t apps))))
  8967. (when (eq cmd 'mailcap)
  8968. (require 'mailcap)
  8969. (mailcap-parse-mailcaps)
  8970. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8971. (command (mailcap-mime-info mime-type)))
  8972. (if (stringp command)
  8973. (setq cmd command)
  8974. (setq cmd 'emacs))))
  8975. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8976. (not (file-exists-p file))
  8977. (not org-open-non-existing-files))
  8978. (error "No such file: %s" file))
  8979. (cond
  8980. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8981. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8982. (while (string-match "['\"]%s['\"]" cmd)
  8983. (setq cmd (replace-match "%s" t t cmd)))
  8984. (while (string-match "%s" cmd)
  8985. (setq cmd (replace-match
  8986. (save-match-data
  8987. (shell-quote-argument
  8988. (convert-standard-filename file)))
  8989. t t cmd)))
  8990. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8991. (save-match-data
  8992. (let ((match-index 1)
  8993. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8994. (set-match-data link-match-data)
  8995. (while (<= match-index number-of-groups)
  8996. (let ((regex (concat "%" (number-to-string match-index)))
  8997. (replace-with (match-string match-index dlink)))
  8998. (while (string-match regex cmd)
  8999. (setq cmd (replace-match replace-with t t cmd))))
  9000. (setq match-index (+ match-index 1)))))
  9001. (save-window-excursion
  9002. (start-process-shell-command cmd nil cmd)
  9003. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9004. ))
  9005. ((or (stringp cmd)
  9006. (eq cmd 'emacs))
  9007. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9008. (widen)
  9009. (if line (org-goto-line line)
  9010. (if search (org-link-search search))))
  9011. ((consp cmd)
  9012. (let ((file (convert-standard-filename file)))
  9013. (save-match-data
  9014. (set-match-data link-match-data)
  9015. (eval cmd))))
  9016. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9017. (and (org-mode-p) (eq old-mode 'org-mode)
  9018. (or (not (equal old-buffer (current-buffer)))
  9019. (not (equal old-pos (point))))
  9020. (org-mark-ring-push old-pos old-buffer))))
  9021. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9022. "This function returns non-nil if `entry' uses a regular
  9023. expression which should be matched against the whole link by
  9024. org-open-file.
  9025. It assumes that is the case when the entry uses a regular
  9026. expression which has at least one grouping construct and the
  9027. action is either a lisp form or a command string containing
  9028. '%1', i.e. using at least one subexpression match as a
  9029. parameter."
  9030. (let ((selector (car entry))
  9031. (action (cdr entry)))
  9032. (if (stringp selector)
  9033. (and (> (regexp-opt-depth selector) 0)
  9034. (or (and (stringp action)
  9035. (string-match "%[0-9]" action))
  9036. (consp action)))
  9037. nil)))
  9038. (defun org-default-apps ()
  9039. "Return the default applications for this operating system."
  9040. (cond
  9041. ((eq system-type 'darwin)
  9042. org-file-apps-defaults-macosx)
  9043. ((eq system-type 'windows-nt)
  9044. org-file-apps-defaults-windowsnt)
  9045. (t org-file-apps-defaults-gnu)))
  9046. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9047. "Convert extensions to regular expressions in the cars of LIST.
  9048. Also, weed out any non-string entries, because the return value is used
  9049. only for regexp matching.
  9050. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9051. point to the symbol `emacs', indicating that the file should
  9052. be opened in Emacs."
  9053. (append
  9054. (delq nil
  9055. (mapcar (lambda (x)
  9056. (if (not (stringp (car x)))
  9057. nil
  9058. (if (string-match "\\W" (car x))
  9059. x
  9060. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9061. list))
  9062. (if add-auto-mode
  9063. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9064. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9065. (defun org-file-remote-p (file)
  9066. "Test whether FILE specifies a location on a remote system.
  9067. Return non-nil if the location is indeed remote.
  9068. For example, the filename \"/user@host:/foo\" specifies a location
  9069. on the system \"/user@host:\"."
  9070. (cond ((fboundp 'file-remote-p)
  9071. (file-remote-p file))
  9072. ((fboundp 'tramp-handle-file-remote-p)
  9073. (tramp-handle-file-remote-p file))
  9074. ((and (boundp 'ange-ftp-name-format)
  9075. (string-match (car ange-ftp-name-format) file))
  9076. t)
  9077. (t nil)))
  9078. ;;;; Refiling
  9079. (defun org-get-org-file ()
  9080. "Read a filename, with default directory `org-directory'."
  9081. (let ((default (or org-default-notes-file remember-data-file)))
  9082. (read-file-name (format "File name [%s]: " default)
  9083. (file-name-as-directory org-directory)
  9084. default)))
  9085. (defun org-notes-order-reversed-p ()
  9086. "Check if the current file should receive notes in reversed order."
  9087. (cond
  9088. ((not org-reverse-note-order) nil)
  9089. ((eq t org-reverse-note-order) t)
  9090. ((not (listp org-reverse-note-order)) nil)
  9091. (t (catch 'exit
  9092. (let ((all org-reverse-note-order)
  9093. entry)
  9094. (while (setq entry (pop all))
  9095. (if (string-match (car entry) buffer-file-name)
  9096. (throw 'exit (cdr entry))))
  9097. nil)))))
  9098. (defvar org-refile-target-table nil
  9099. "The list of refile targets, created by `org-refile'.")
  9100. (defvar org-agenda-new-buffers nil
  9101. "Buffers created to visit agenda files.")
  9102. (defvar org-refile-cache nil
  9103. "Cache for refile targets.")
  9104. (defvar org-refile-markers nil
  9105. "All the markers used for caching refile locations.")
  9106. (defun org-refile-marker (pos)
  9107. "Get a new refile marker, but only if caching is in use."
  9108. (if (not org-refile-use-cache)
  9109. pos
  9110. (let ((m (make-marker)))
  9111. (move-marker m pos)
  9112. (push m org-refile-markers)
  9113. m)))
  9114. (defun org-refile-cache-clear ()
  9115. "Clear the refile cache and disable all the markers."
  9116. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9117. (setq org-refile-markers nil)
  9118. (setq org-refile-cache nil)
  9119. (message "Refile cache has been cleared"))
  9120. (defun org-refile-cache-check-set (set)
  9121. "Check if all the markers in the cache still have live buffers."
  9122. (let (marker)
  9123. (catch 'exit
  9124. (while (and set (setq marker (nth 3 (pop set))))
  9125. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9126. (when (and marker (null (marker-buffer marker)))
  9127. (message "not found") (sit-for 3)
  9128. (throw 'exit nil)))
  9129. t)))
  9130. (defun org-refile-cache-put (set &rest identifiers)
  9131. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9132. (let* ((key (sha1 (prin1-to-string identifiers)))
  9133. (entry (assoc key org-refile-cache)))
  9134. (if entry
  9135. (setcdr entry set)
  9136. (push (cons key set) org-refile-cache))))
  9137. (defun org-refile-cache-get (&rest identifiers)
  9138. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9139. (cond
  9140. ((not org-refile-cache) nil)
  9141. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9142. (t
  9143. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9144. org-refile-cache))))
  9145. (and set (org-refile-cache-check-set set) set)))))
  9146. (defun org-refile-get-targets (&optional default-buffer)
  9147. "Produce a table with refile targets."
  9148. (let ((case-fold-search nil)
  9149. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9150. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9151. targets tgs txt re files f desc descre fast-path-p level pos0)
  9152. (message "Getting targets...")
  9153. (with-current-buffer (or default-buffer (current-buffer))
  9154. (while (setq entry (pop entries))
  9155. (setq files (car entry) desc (cdr entry))
  9156. (setq fast-path-p nil)
  9157. (cond
  9158. ((null files) (setq files (list (current-buffer))))
  9159. ((eq files 'org-agenda-files)
  9160. (setq files (org-agenda-files 'unrestricted)))
  9161. ((and (symbolp files) (fboundp files))
  9162. (setq files (funcall files)))
  9163. ((and (symbolp files) (boundp files))
  9164. (setq files (symbol-value files))))
  9165. (if (stringp files) (setq files (list files)))
  9166. (cond
  9167. ((eq (car desc) :tag)
  9168. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9169. ((eq (car desc) :todo)
  9170. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9171. ((eq (car desc) :regexp)
  9172. (setq descre (cdr desc)))
  9173. ((eq (car desc) :level)
  9174. (setq descre (concat "^\\*\\{" (number-to-string
  9175. (if org-odd-levels-only
  9176. (1- (* 2 (cdr desc)))
  9177. (cdr desc)))
  9178. "\\}[ \t]")))
  9179. ((eq (car desc) :maxlevel)
  9180. (setq fast-path-p t)
  9181. (setq descre (concat "^\\*\\{1," (number-to-string
  9182. (if org-odd-levels-only
  9183. (1- (* 2 (cdr desc)))
  9184. (cdr desc)))
  9185. "\\}[ \t]")))
  9186. (t (error "Bad refiling target description %s" desc)))
  9187. (while (setq f (pop files))
  9188. (with-current-buffer
  9189. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9190. (or
  9191. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9192. (progn
  9193. (if (bufferp f) (setq f (buffer-file-name
  9194. (buffer-base-buffer f))))
  9195. (setq f (and f (expand-file-name f)))
  9196. (if (eq org-refile-use-outline-path 'file)
  9197. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9198. (save-excursion
  9199. (save-restriction
  9200. (widen)
  9201. (goto-char (point-min))
  9202. (while (re-search-forward descre nil t)
  9203. (goto-char (setq pos0 (point-at-bol)))
  9204. (catch 'next
  9205. (when org-refile-target-verify-function
  9206. (save-match-data
  9207. (or (funcall org-refile-target-verify-function)
  9208. (throw 'next t))))
  9209. (when (looking-at org-complex-heading-regexp)
  9210. (setq level (org-reduced-level
  9211. (- (match-end 1) (match-beginning 1)))
  9212. txt (org-link-display-format (match-string 4))
  9213. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9214. re (format org-complex-heading-regexp-format
  9215. (regexp-quote (match-string 4))))
  9216. (when org-refile-use-outline-path
  9217. (setq txt (mapconcat
  9218. 'org-protect-slash
  9219. (append
  9220. (if (eq org-refile-use-outline-path
  9221. 'file)
  9222. (list (file-name-nondirectory
  9223. (buffer-file-name
  9224. (buffer-base-buffer))))
  9225. (if (eq org-refile-use-outline-path
  9226. 'full-file-path)
  9227. (list (buffer-file-name
  9228. (buffer-base-buffer)))))
  9229. (org-get-outline-path fast-path-p
  9230. level txt)
  9231. (list txt))
  9232. "/")))
  9233. (push (list txt f re (org-refile-marker (point)))
  9234. tgs)))
  9235. (when (= (point) pos0)
  9236. ;; verification function has not moved point
  9237. (goto-char (point-at-eol))))))))
  9238. (when org-refile-use-cache
  9239. (org-refile-cache-put tgs (buffer-file-name) descre))
  9240. (setq targets (append tgs targets))
  9241. ))))
  9242. (message "Getting targets...done")
  9243. (nreverse targets)))
  9244. (defun org-protect-slash (s)
  9245. (while (string-match "/" s)
  9246. (setq s (replace-match "\\" t t s)))
  9247. s)
  9248. (defvar org-olpa (make-vector 20 nil))
  9249. (defun org-get-outline-path (&optional fastp level heading)
  9250. "Return the outline path to the current entry, as a list.
  9251. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9252. routine which makes outline path derivations for an entire file,
  9253. avoiding backtracing. Refile target collection makes use of that."
  9254. (if fastp
  9255. (progn
  9256. (if (> level 19)
  9257. (error "Outline path failure, more than 19 levels"))
  9258. (loop for i from level upto 19 do
  9259. (aset org-olpa i nil))
  9260. (prog1
  9261. (delq nil (append org-olpa nil))
  9262. (aset org-olpa level heading)))
  9263. (let (rtn case-fold-search)
  9264. (save-excursion
  9265. (save-restriction
  9266. (widen)
  9267. (while (org-up-heading-safe)
  9268. (when (looking-at org-complex-heading-regexp)
  9269. (push (org-match-string-no-properties 4) rtn)))
  9270. rtn)))))
  9271. (defun org-format-outline-path (path &optional width prefix)
  9272. "Format the outline path PATH for display.
  9273. Width is the maximum number of characters that is available.
  9274. Prefix is a prefix to be included in the returned string,
  9275. such as the file name."
  9276. (setq width (or width 79))
  9277. (if prefix (setq width (- width (length prefix))))
  9278. (if (not path)
  9279. (or prefix "")
  9280. (let* ((nsteps (length path))
  9281. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9282. (maxwidth (if (<= total-width width)
  9283. 10000 ;; everything fits
  9284. ;; we need to shorten the level headings
  9285. (/ (- width nsteps) nsteps)))
  9286. (org-odd-levels-only nil)
  9287. (n 0)
  9288. (total (1+ (length prefix))))
  9289. (setq maxwidth (max maxwidth 10))
  9290. (concat prefix
  9291. (mapconcat
  9292. (lambda (h)
  9293. (setq n (1+ n))
  9294. (if (and (= n nsteps) (< maxwidth 10000))
  9295. (setq maxwidth (- total-width total)))
  9296. (if (< (length h) maxwidth)
  9297. (progn (setq total (+ total (length h) 1)) h)
  9298. (setq h (substring h 0 (- maxwidth 2))
  9299. total (+ total maxwidth 1))
  9300. (if (string-match "[ \t]+\\'" h)
  9301. (setq h (substring h 0 (match-beginning 0))))
  9302. (setq h (concat h "..")))
  9303. (org-add-props h nil 'face
  9304. (nth (% (1- n) org-n-level-faces)
  9305. org-level-faces))
  9306. h)
  9307. path "/")))))
  9308. (defun org-display-outline-path (&optional file current)
  9309. "Display the current outline path in the echo area."
  9310. (interactive "P")
  9311. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9312. (case-fold-search nil)
  9313. (path (and (org-mode-p) (org-get-outline-path))))
  9314. (if current (setq path (append path
  9315. (save-excursion
  9316. (org-back-to-heading t)
  9317. (if (looking-at org-complex-heading-regexp)
  9318. (list (match-string 4)))))))
  9319. (message "%s"
  9320. (org-format-outline-path
  9321. path
  9322. (1- (frame-width))
  9323. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9324. (defvar org-refile-history nil
  9325. "History for refiling operations.")
  9326. (defvar org-after-refile-insert-hook nil
  9327. "Hook run after `org-refile' has inserted its stuff at the new location.
  9328. Note that this is still *before* the stuff will be removed from
  9329. the *old* location.")
  9330. (defvar org-capture-last-stored-marker)
  9331. (defun org-refile (&optional goto default-buffer rfloc)
  9332. "Move the entry or entries at point to another heading.
  9333. The list of target headings is compiled using the information in
  9334. `org-refile-targets', which see.
  9335. At the target location, the entry is filed as a subitem of the target
  9336. heading. Depending on `org-reverse-note-order', the new subitem will
  9337. either be the first or the last subitem.
  9338. If there is an active region, all entries in that region will be moved.
  9339. However, the region must fulfill the requirement that the first heading
  9340. is the first one sets the top-level of the moved text - at most siblings
  9341. below it are allowed.
  9342. With prefix arg GOTO, the command will only visit the target location
  9343. and not actually move anything.
  9344. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9345. go to the location where the last refiling operation has put the subtree.
  9346. With a prefix argument of `2', refile to the running clock.
  9347. RFLOC can be a refile location obtained in a different way.
  9348. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9349. If you are using target caching (see `org-refile-use-cache'),
  9350. You have to clear the target cache in order to find new targets.
  9351. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9352. prefix argument (`C-u C-u C-u C-c C-w')."
  9353. (interactive "P")
  9354. (if (member goto '(0 (64)))
  9355. (org-refile-cache-clear)
  9356. (let* ((cbuf (current-buffer))
  9357. (regionp (org-region-active-p))
  9358. (region-start (and regionp (region-beginning)))
  9359. (region-end (and regionp (region-end)))
  9360. (region-length (and regionp (- region-end region-start)))
  9361. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9362. pos it nbuf file re level reversed)
  9363. (setq last-command nil)
  9364. (when regionp
  9365. (goto-char region-start)
  9366. (or (bolp) (goto-char (point-at-bol)))
  9367. (setq region-start (point))
  9368. (unless (org-kill-is-subtree-p
  9369. (buffer-substring region-start region-end))
  9370. (error "The region is not a (sequence of) subtree(s)")))
  9371. (if (equal goto '(16))
  9372. (org-refile-goto-last-stored)
  9373. (when (or
  9374. (and (equal goto 2)
  9375. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9376. (prog1
  9377. (setq it (list (or org-clock-heading "running clock")
  9378. (buffer-file-name
  9379. (marker-buffer org-clock-hd-marker))
  9380. ""
  9381. (marker-position org-clock-hd-marker)))
  9382. (setq goto nil)))
  9383. (setq it (or rfloc
  9384. (save-excursion
  9385. (org-refile-get-location
  9386. (if goto "Goto" "Refile to") default-buffer
  9387. org-refile-allow-creating-parent-nodes)))))
  9388. (setq file (nth 1 it)
  9389. re (nth 2 it)
  9390. pos (nth 3 it))
  9391. (if (and (not goto)
  9392. pos
  9393. (equal (buffer-file-name) file)
  9394. (if regionp
  9395. (and (>= pos region-start)
  9396. (<= pos region-end))
  9397. (and (>= pos (point))
  9398. (< pos (save-excursion
  9399. (org-end-of-subtree t t))))))
  9400. (error "Cannot refile to position inside the tree or region"))
  9401. (setq nbuf (or (find-buffer-visiting file)
  9402. (find-file-noselect file)))
  9403. (if goto
  9404. (progn
  9405. (switch-to-buffer nbuf)
  9406. (goto-char pos)
  9407. (org-show-context 'org-goto))
  9408. (if regionp
  9409. (progn
  9410. (org-kill-new (buffer-substring region-start region-end))
  9411. (org-save-markers-in-region region-start region-end))
  9412. (org-copy-subtree 1 nil t))
  9413. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9414. (find-file-noselect file)))
  9415. (setq reversed (org-notes-order-reversed-p))
  9416. (save-excursion
  9417. (save-restriction
  9418. (widen)
  9419. (if pos
  9420. (progn
  9421. (goto-char pos)
  9422. (looking-at org-outline-regexp)
  9423. (setq level (org-get-valid-level (funcall outline-level) 1))
  9424. (goto-char
  9425. (if reversed
  9426. (or (outline-next-heading) (point-max))
  9427. (or (save-excursion (org-get-next-sibling))
  9428. (org-end-of-subtree t t)
  9429. (point-max)))))
  9430. (setq level 1)
  9431. (if (not reversed)
  9432. (goto-char (point-max))
  9433. (goto-char (point-min))
  9434. (or (outline-next-heading) (goto-char (point-max)))))
  9435. (if (not (bolp)) (newline))
  9436. (org-paste-subtree level)
  9437. (when org-log-refile
  9438. (org-add-log-setup 'refile nil nil 'findpos
  9439. org-log-refile)
  9440. (unless (eq org-log-refile 'note)
  9441. (save-excursion (org-add-log-note))))
  9442. (and org-auto-align-tags (org-set-tags nil t))
  9443. (bookmark-set "org-refile-last-stored")
  9444. ;; If we are refiling for capture, make sure that the
  9445. ;; last-capture pointers point here
  9446. (when (org-bound-and-true-p org-refile-for-capture)
  9447. (bookmark-set "org-capture-last-stored-marker")
  9448. (move-marker org-capture-last-stored-marker (point)))
  9449. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9450. (run-hooks 'org-after-refile-insert-hook))))
  9451. (if regionp
  9452. (delete-region (point) (+ (point) region-length))
  9453. (org-cut-subtree))
  9454. (when (featurep 'org-inlinetask)
  9455. (org-inlinetask-remove-END-maybe))
  9456. (setq org-markers-to-move nil)
  9457. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9458. (defun org-refile-goto-last-stored ()
  9459. "Go to the location where the last refile was stored."
  9460. (interactive)
  9461. (bookmark-jump "org-refile-last-stored")
  9462. (message "This is the location of the last refile"))
  9463. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9464. "Prompt the user for a refile location, using PROMPT.
  9465. PROMPT should not be suffixed with a colon and a space, because
  9466. this function appends the default value from
  9467. `org-refile-history' automatically, if that is not empty."
  9468. (let ((org-refile-targets org-refile-targets)
  9469. (org-refile-use-outline-path org-refile-use-outline-path))
  9470. (setq org-refile-target-table (org-refile-get-targets default-buffer)))
  9471. (unless org-refile-target-table
  9472. (error "No refile targets"))
  9473. (let* ((prompt (concat prompt
  9474. (and (car org-refile-history)
  9475. (concat " (default " (car org-refile-history) ")"))
  9476. ": "))
  9477. (cbuf (current-buffer))
  9478. (partial-completion-mode nil)
  9479. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9480. (cfunc (if (and org-refile-use-outline-path
  9481. org-outline-path-complete-in-steps)
  9482. 'org-olpath-completing-read
  9483. 'org-icompleting-read))
  9484. (extra (if org-refile-use-outline-path "/" ""))
  9485. (filename (and cfn (expand-file-name cfn)))
  9486. (tbl (mapcar
  9487. (lambda (x)
  9488. (if (and (not (member org-refile-use-outline-path
  9489. '(file full-file-path)))
  9490. (not (equal filename (nth 1 x))))
  9491. (cons (concat (car x) extra " ("
  9492. (file-name-nondirectory (nth 1 x)) ")")
  9493. (cdr x))
  9494. (cons (concat (car x) extra) (cdr x))))
  9495. org-refile-target-table))
  9496. (completion-ignore-case t)
  9497. pa answ parent-target child parent old-hist)
  9498. (setq old-hist org-refile-history)
  9499. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9500. nil 'org-refile-history (car org-refile-history)))
  9501. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9502. (org-refile-check-position pa)
  9503. (if pa
  9504. (progn
  9505. (when (or (not org-refile-history)
  9506. (not (eq old-hist org-refile-history))
  9507. (not (equal (car pa) (car org-refile-history))))
  9508. (setq org-refile-history
  9509. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9510. org-refile-history
  9511. (cdr org-refile-history))))
  9512. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9513. (pop org-refile-history)))
  9514. pa)
  9515. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9516. (progn
  9517. (setq parent (match-string 1 answ)
  9518. child (match-string 2 answ))
  9519. (setq parent-target (or (assoc parent tbl)
  9520. (assoc (concat parent "/") tbl)))
  9521. (when (and parent-target
  9522. (or (eq new-nodes t)
  9523. (and (eq new-nodes 'confirm)
  9524. (y-or-n-p (format "Create new node \"%s\"? "
  9525. child)))))
  9526. (org-refile-new-child parent-target child)))
  9527. (error "Invalid target location")))))
  9528. (defun org-refile-check-position (refile-pointer)
  9529. "Check if the refile pointer matches the readline to which it points."
  9530. (let* ((file (nth 1 refile-pointer))
  9531. (re (nth 2 refile-pointer))
  9532. (pos (nth 3 refile-pointer))
  9533. buffer)
  9534. (when (org-string-nw-p re)
  9535. (setq buffer (if (markerp pos)
  9536. (marker-buffer pos)
  9537. (or (find-buffer-visiting file)
  9538. (find-file-noselect file))))
  9539. (with-current-buffer buffer
  9540. (save-excursion
  9541. (save-restriction
  9542. (widen)
  9543. (goto-char pos)
  9544. (beginning-of-line 1)
  9545. (unless (org-looking-at-p re)
  9546. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9547. (defun org-refile-new-child (parent-target child)
  9548. "Use refile target PARENT-TARGET to add new CHILD below it."
  9549. (unless parent-target
  9550. (error "Cannot find parent for new node"))
  9551. (let ((file (nth 1 parent-target))
  9552. (pos (nth 3 parent-target))
  9553. level)
  9554. (with-current-buffer (or (find-buffer-visiting file)
  9555. (find-file-noselect file))
  9556. (save-excursion
  9557. (save-restriction
  9558. (widen)
  9559. (if pos
  9560. (goto-char pos)
  9561. (goto-char (point-max))
  9562. (if (not (bolp)) (newline)))
  9563. (when (looking-at org-outline-regexp)
  9564. (setq level (funcall outline-level))
  9565. (org-end-of-subtree t t))
  9566. (org-back-over-empty-lines)
  9567. (insert "\n" (make-string
  9568. (if pos (org-get-valid-level level 1) 1) ?*)
  9569. " " child "\n")
  9570. (beginning-of-line 0)
  9571. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9572. (defun org-olpath-completing-read (prompt collection &rest args)
  9573. "Read an outline path like a file name."
  9574. (let ((thetable collection)
  9575. (org-completion-use-ido nil) ; does not work with ido.
  9576. (org-completion-use-iswitchb nil)) ; or iswitchb
  9577. (apply
  9578. 'org-icompleting-read prompt
  9579. (lambda (string predicate &optional flag)
  9580. (let (rtn r f (l (length string)))
  9581. (cond
  9582. ((eq flag nil)
  9583. ;; try completion
  9584. (try-completion string thetable))
  9585. ((eq flag t)
  9586. ;; all-completions
  9587. (setq rtn (all-completions string thetable predicate))
  9588. (mapcar
  9589. (lambda (x)
  9590. (setq r (substring x l))
  9591. (if (string-match " ([^)]*)$" x)
  9592. (setq f (match-string 0 x))
  9593. (setq f ""))
  9594. (if (string-match "/" r)
  9595. (concat string (substring r 0 (match-end 0)) f)
  9596. x))
  9597. rtn))
  9598. ((eq flag 'lambda)
  9599. ;; exact match?
  9600. (assoc string thetable)))
  9601. ))
  9602. args)))
  9603. ;;;; Dynamic blocks
  9604. (defun org-find-dblock (name)
  9605. "Find the first dynamic block with name NAME in the buffer.
  9606. If not found, stay at current position and return nil."
  9607. (let (pos)
  9608. (save-excursion
  9609. (goto-char (point-min))
  9610. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9611. nil t)
  9612. (match-beginning 0))))
  9613. (if pos (goto-char pos))
  9614. pos))
  9615. (defconst org-dblock-start-re
  9616. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9617. "Matches the start line of a dynamic block, with parameters.")
  9618. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9619. "Matches the end of a dynamic block.")
  9620. (defun org-create-dblock (plist)
  9621. "Create a dynamic block section, with parameters taken from PLIST.
  9622. PLIST must contain a :name entry which is used as name of the block."
  9623. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9624. (end-of-line 1)
  9625. (newline))
  9626. (let ((col (current-column))
  9627. (name (plist-get plist :name)))
  9628. (insert "#+BEGIN: " name)
  9629. (while plist
  9630. (if (eq (car plist) :name)
  9631. (setq plist (cddr plist))
  9632. (insert " " (prin1-to-string (pop plist)))))
  9633. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9634. (beginning-of-line -2)))
  9635. (defun org-prepare-dblock ()
  9636. "Prepare dynamic block for refresh.
  9637. This empties the block, puts the cursor at the insert position and returns
  9638. the property list including an extra property :name with the block name."
  9639. (unless (looking-at org-dblock-start-re)
  9640. (error "Not at a dynamic block"))
  9641. (let* ((begdel (1+ (match-end 0)))
  9642. (name (org-no-properties (match-string 1)))
  9643. (params (append (list :name name)
  9644. (read (concat "(" (match-string 3) ")")))))
  9645. (save-excursion
  9646. (beginning-of-line 1)
  9647. (skip-chars-forward " \t")
  9648. (setq params (plist-put params :indentation-column (current-column))))
  9649. (unless (re-search-forward org-dblock-end-re nil t)
  9650. (error "Dynamic block not terminated"))
  9651. (setq params
  9652. (append params
  9653. (list :content (buffer-substring
  9654. begdel (match-beginning 0)))))
  9655. (delete-region begdel (match-beginning 0))
  9656. (goto-char begdel)
  9657. (open-line 1)
  9658. params))
  9659. (defun org-map-dblocks (&optional command)
  9660. "Apply COMMAND to all dynamic blocks in the current buffer.
  9661. If COMMAND is not given, use `org-update-dblock'."
  9662. (let ((cmd (or command 'org-update-dblock)))
  9663. (save-excursion
  9664. (goto-char (point-min))
  9665. (while (re-search-forward org-dblock-start-re nil t)
  9666. (goto-char (match-beginning 0))
  9667. (save-excursion
  9668. (condition-case nil
  9669. (funcall cmd)
  9670. (error (message "Error during update of dynamic block"))))
  9671. (unless (re-search-forward org-dblock-end-re nil t)
  9672. (error "Dynamic block not terminated"))))))
  9673. (defun org-dblock-update (&optional arg)
  9674. "User command for updating dynamic blocks.
  9675. Update the dynamic block at point. With prefix ARG, update all dynamic
  9676. blocks in the buffer."
  9677. (interactive "P")
  9678. (if arg
  9679. (org-update-all-dblocks)
  9680. (or (looking-at org-dblock-start-re)
  9681. (org-beginning-of-dblock))
  9682. (org-update-dblock)))
  9683. (defun org-update-dblock ()
  9684. "Update the dynamic block at point.
  9685. This means to empty the block, parse for parameters and then call
  9686. the correct writing function."
  9687. (interactive)
  9688. (save-window-excursion
  9689. (let* ((pos (point))
  9690. (line (org-current-line))
  9691. (params (org-prepare-dblock))
  9692. (name (plist-get params :name))
  9693. (indent (plist-get params :indentation-column))
  9694. (cmd (intern (concat "org-dblock-write:" name))))
  9695. (message "Updating dynamic block `%s' at line %d..." name line)
  9696. (funcall cmd params)
  9697. (message "Updating dynamic block `%s' at line %d...done" name line)
  9698. (goto-char pos)
  9699. (when (and indent (> indent 0))
  9700. (setq indent (make-string indent ?\ ))
  9701. (save-excursion
  9702. (org-beginning-of-dblock)
  9703. (forward-line 1)
  9704. (while (not (looking-at org-dblock-end-re))
  9705. (insert indent)
  9706. (beginning-of-line 2))
  9707. (when (looking-at org-dblock-end-re)
  9708. (and (looking-at "[ \t]+")
  9709. (replace-match ""))
  9710. (insert indent)))))))
  9711. (defun org-beginning-of-dblock ()
  9712. "Find the beginning of the dynamic block at point.
  9713. Error if there is no such block at point."
  9714. (let ((pos (point))
  9715. beg)
  9716. (end-of-line 1)
  9717. (if (and (re-search-backward org-dblock-start-re nil t)
  9718. (setq beg (match-beginning 0))
  9719. (re-search-forward org-dblock-end-re nil t)
  9720. (> (match-end 0) pos))
  9721. (goto-char beg)
  9722. (goto-char pos)
  9723. (error "Not in a dynamic block"))))
  9724. (defun org-update-all-dblocks ()
  9725. "Update all dynamic blocks in the buffer.
  9726. This function can be used in a hook."
  9727. (interactive)
  9728. (when (org-mode-p)
  9729. (org-map-dblocks 'org-update-dblock)))
  9730. ;;;; Completion
  9731. (defconst org-additional-option-like-keywords
  9732. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9733. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9734. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9735. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9736. "BEGIN:" "END:"
  9737. "ORGTBL" "TBLFM:" "TBLNAME:"
  9738. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9739. "BEGIN_QUOTE" "END_QUOTE"
  9740. "BEGIN_VERSE" "END_VERSE"
  9741. "BEGIN_CENTER" "END_CENTER"
  9742. "BEGIN_SRC" "END_SRC"
  9743. "BEGIN_RESULT" "END_RESULT"
  9744. "SOURCE:" "SRCNAME:" "FUNCTION:"
  9745. "RESULTS:" "DATA:"
  9746. "HEADER:" "HEADERS:"
  9747. "BABEL:"
  9748. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9749. "CAPTION:" "LABEL:"
  9750. "SETUPFILE:"
  9751. "INCLUDE:"
  9752. "BIND:"
  9753. "MACRO:"))
  9754. (defcustom org-structure-template-alist
  9755. '(
  9756. ("s" "#+begin_src ?\n\n#+end_src"
  9757. "<src lang=\"?\">\n\n</src>")
  9758. ("e" "#+begin_example\n?\n#+end_example"
  9759. "<example>\n?\n</example>")
  9760. ("q" "#+begin_quote\n?\n#+end_quote"
  9761. "<quote>\n?\n</quote>")
  9762. ("v" "#+begin_verse\n?\n#+end_verse"
  9763. "<verse>\n?\n/verse>")
  9764. ("c" "#+begin_center\n?\n#+end_center"
  9765. "<center>\n?\n/center>")
  9766. ("l" "#+begin_latex\n?\n#+end_latex"
  9767. "<literal style=\"latex\">\n?\n</literal>")
  9768. ("L" "#+latex: "
  9769. "<literal style=\"latex\">?</literal>")
  9770. ("h" "#+begin_html\n?\n#+end_html"
  9771. "<literal style=\"html\">\n?\n</literal>")
  9772. ("H" "#+html: "
  9773. "<literal style=\"html\">?</literal>")
  9774. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9775. ("A" "#+ascii: ")
  9776. ("i" "#+index: ?"
  9777. "#+index: ?")
  9778. ("I" "#+include %file ?"
  9779. "<include file=%file markup=\"?\">")
  9780. )
  9781. "Structure completion elements.
  9782. This is a list of abbreviation keys and values. The value gets inserted
  9783. if you type `<' followed by the key and then press the completion key,
  9784. usually `M-TAB'. %file will be replaced by a file name after prompting
  9785. for the file using completion. The cursor will be placed at the position
  9786. of the `?` in the template.
  9787. There are two templates for each key, the first uses the original Org syntax,
  9788. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9789. the default when the /org-mtags.el/ module has been loaded. See also the
  9790. variable `org-mtags-prefer-muse-templates'.
  9791. This is an experimental feature, it is undecided if it is going to stay in."
  9792. :group 'org-completion
  9793. :type '(repeat
  9794. (string :tag "Key")
  9795. (string :tag "Template")
  9796. (string :tag "Muse Template")))
  9797. (defun org-try-structure-completion ()
  9798. "Try to complete a structure template before point.
  9799. This looks for strings like \"<e\" on an otherwise empty line and
  9800. expands them."
  9801. (let ((l (buffer-substring (point-at-bol) (point)))
  9802. a)
  9803. (when (and (looking-at "[ \t]*$")
  9804. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  9805. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9806. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9807. (match-beginning 1)) a)
  9808. t)))
  9809. (defun org-complete-expand-structure-template (start cell)
  9810. "Expand a structure template."
  9811. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9812. (rpl (nth (if musep 2 1) cell))
  9813. (ind ""))
  9814. (delete-region start (point))
  9815. (when (string-match "\\`#\\+" rpl)
  9816. (cond
  9817. ((bolp))
  9818. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9819. (setq ind (buffer-substring (point-at-bol) (point))))
  9820. (t (newline))))
  9821. (setq start (point))
  9822. (if (string-match "%file" rpl)
  9823. (setq rpl (replace-match
  9824. (concat
  9825. "\""
  9826. (save-match-data
  9827. (abbreviate-file-name (read-file-name "Include file: ")))
  9828. "\"")
  9829. t t rpl)))
  9830. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9831. (concat "\n" ind)))
  9832. (insert rpl)
  9833. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9834. ;;;; TODO, DEADLINE, Comments
  9835. (defun org-toggle-comment ()
  9836. "Change the COMMENT state of an entry."
  9837. (interactive)
  9838. (save-excursion
  9839. (org-back-to-heading)
  9840. (let (case-fold-search)
  9841. (if (looking-at (concat org-outline-regexp
  9842. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9843. (replace-match "" t t nil 1)
  9844. (if (looking-at org-outline-regexp)
  9845. (progn
  9846. (goto-char (match-end 0))
  9847. (insert org-comment-string " ")))))))
  9848. (defvar org-last-todo-state-is-todo nil
  9849. "This is non-nil when the last TODO state change led to a TODO state.
  9850. If the last change removed the TODO tag or switched to DONE, then
  9851. this is nil.")
  9852. (defvar org-setting-tags nil) ; dynamically skipped
  9853. (defvar org-todo-setup-filter-hook nil
  9854. "Hook for functions that pre-filter todo specs.
  9855. Each function takes a todo spec and returns either nil or the spec
  9856. transformed into canonical form." )
  9857. (defvar org-todo-get-default-hook nil
  9858. "Hook for functions that get a default item for todo.
  9859. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9860. nil or a string to be used for the todo mark." )
  9861. (defvar org-agenda-headline-snapshot-before-repeat)
  9862. (defun org-todo (&optional arg)
  9863. "Change the TODO state of an item.
  9864. The state of an item is given by a keyword at the start of the heading,
  9865. like
  9866. *** TODO Write paper
  9867. *** DONE Call mom
  9868. The different keywords are specified in the variable `org-todo-keywords'.
  9869. By default the available states are \"TODO\" and \"DONE\".
  9870. So for this example: when the item starts with TODO, it is changed to DONE.
  9871. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9872. DONE are present, add TODO at the beginning of the heading.
  9873. With \\[universal-argument] prefix arg, use completion to determine the new \
  9874. state.
  9875. With numeric prefix arg, switch to that state.
  9876. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9877. keywords (nextset).
  9878. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9879. For calling through lisp, arg is also interpreted in the following way:
  9880. 'none -> empty state
  9881. \"\"(empty string) -> switch to empty state
  9882. 'done -> switch to DONE
  9883. 'nextset -> switch to the next set of keywords
  9884. 'previousset -> switch to the previous set of keywords
  9885. \"WAITING\" -> switch to the specified keyword, but only if it
  9886. really is a member of `org-todo-keywords'."
  9887. (interactive "P")
  9888. (if (equal arg '(16)) (setq arg 'nextset))
  9889. (let ((org-blocker-hook org-blocker-hook)
  9890. (case-fold-search nil))
  9891. (when (equal arg '(64))
  9892. (setq arg nil org-blocker-hook nil))
  9893. (when (and org-blocker-hook
  9894. (or org-inhibit-blocking
  9895. (org-entry-get nil "NOBLOCKING")))
  9896. (setq org-blocker-hook nil))
  9897. (save-excursion
  9898. (catch 'exit
  9899. (org-back-to-heading t)
  9900. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  9901. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9902. (looking-at " *"))
  9903. (let* ((match-data (match-data))
  9904. (startpos (point-at-bol))
  9905. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9906. (org-log-done org-log-done)
  9907. (org-log-repeat org-log-repeat)
  9908. (org-todo-log-states org-todo-log-states)
  9909. (this (match-string 1))
  9910. (hl-pos (match-beginning 0))
  9911. (head (org-get-todo-sequence-head this))
  9912. (ass (assoc head org-todo-kwd-alist))
  9913. (interpret (nth 1 ass))
  9914. (done-word (nth 3 ass))
  9915. (final-done-word (nth 4 ass))
  9916. (last-state (or this ""))
  9917. (completion-ignore-case t)
  9918. (member (member this org-todo-keywords-1))
  9919. (tail (cdr member))
  9920. (state (cond
  9921. ((and org-todo-key-trigger
  9922. (or (and (equal arg '(4))
  9923. (eq org-use-fast-todo-selection 'prefix))
  9924. (and (not arg) org-use-fast-todo-selection
  9925. (not (eq org-use-fast-todo-selection
  9926. 'prefix)))))
  9927. ;; Use fast selection
  9928. (org-fast-todo-selection))
  9929. ((and (equal arg '(4))
  9930. (or (not org-use-fast-todo-selection)
  9931. (not org-todo-key-trigger)))
  9932. ;; Read a state with completion
  9933. (org-icompleting-read
  9934. "State: " (mapcar (lambda(x) (list x))
  9935. org-todo-keywords-1)
  9936. nil t))
  9937. ((eq arg 'right)
  9938. (if this
  9939. (if tail (car tail) nil)
  9940. (car org-todo-keywords-1)))
  9941. ((eq arg 'left)
  9942. (if (equal member org-todo-keywords-1)
  9943. nil
  9944. (if this
  9945. (nth (- (length org-todo-keywords-1)
  9946. (length tail) 2)
  9947. org-todo-keywords-1)
  9948. (org-last org-todo-keywords-1))))
  9949. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9950. (setq arg nil))) ; hack to fall back to cycling
  9951. (arg
  9952. ;; user or caller requests a specific state
  9953. (cond
  9954. ((equal arg "") nil)
  9955. ((eq arg 'none) nil)
  9956. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9957. ((eq arg 'nextset)
  9958. (or (car (cdr (member head org-todo-heads)))
  9959. (car org-todo-heads)))
  9960. ((eq arg 'previousset)
  9961. (let ((org-todo-heads (reverse org-todo-heads)))
  9962. (or (car (cdr (member head org-todo-heads)))
  9963. (car org-todo-heads))))
  9964. ((car (member arg org-todo-keywords-1)))
  9965. ((stringp arg)
  9966. (error "State `%s' not valid in this file" arg))
  9967. ((nth (1- (prefix-numeric-value arg))
  9968. org-todo-keywords-1))))
  9969. ((null member) (or head (car org-todo-keywords-1)))
  9970. ((equal this final-done-word) nil) ;; -> make empty
  9971. ((null tail) nil) ;; -> first entry
  9972. ((memq interpret '(type priority))
  9973. (if (eq this-command last-command)
  9974. (car tail)
  9975. (if (> (length tail) 0)
  9976. (or done-word (car org-done-keywords))
  9977. nil)))
  9978. (t
  9979. (car tail))))
  9980. (state (or
  9981. (run-hook-with-args-until-success
  9982. 'org-todo-get-default-hook state last-state)
  9983. state))
  9984. (next (if state (concat " " state " ") " "))
  9985. (change-plist (list :type 'todo-state-change :from this :to state
  9986. :position startpos))
  9987. dolog now-done-p)
  9988. (when org-blocker-hook
  9989. (setq org-last-todo-state-is-todo
  9990. (not (member this org-done-keywords)))
  9991. (unless (save-excursion
  9992. (save-match-data
  9993. (org-with-wide-buffer
  9994. (run-hook-with-args-until-failure
  9995. 'org-blocker-hook change-plist))))
  9996. (if (org-called-interactively-p 'interactive)
  9997. (error "TODO state change from %s to %s blocked" this state)
  9998. ;; fail silently
  9999. (message "TODO state change from %s to %s blocked" this state)
  10000. (throw 'exit nil))))
  10001. (store-match-data match-data)
  10002. (replace-match next t t)
  10003. (unless (pos-visible-in-window-p hl-pos)
  10004. (message "TODO state changed to %s" (org-trim next)))
  10005. (unless head
  10006. (setq head (org-get-todo-sequence-head state)
  10007. ass (assoc head org-todo-kwd-alist)
  10008. interpret (nth 1 ass)
  10009. done-word (nth 3 ass)
  10010. final-done-word (nth 4 ass)))
  10011. (when (memq arg '(nextset previousset))
  10012. (message "Keyword-Set %d/%d: %s"
  10013. (- (length org-todo-sets) -1
  10014. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10015. (length org-todo-sets)
  10016. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10017. (setq org-last-todo-state-is-todo
  10018. (not (member state org-done-keywords)))
  10019. (setq now-done-p (and (member state org-done-keywords)
  10020. (not (member this org-done-keywords))))
  10021. (and logging (org-local-logging logging))
  10022. (when (and (or org-todo-log-states org-log-done)
  10023. (not (eq org-inhibit-logging t))
  10024. (not (memq arg '(nextset previousset))))
  10025. ;; we need to look at recording a time and note
  10026. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10027. (nth 2 (assoc this org-todo-log-states))))
  10028. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10029. (setq dolog 'time))
  10030. (when (and state
  10031. (member state org-not-done-keywords)
  10032. (not (member this org-not-done-keywords)))
  10033. ;; This is now a todo state and was not one before
  10034. ;; If there was a CLOSED time stamp, get rid of it.
  10035. (org-add-planning-info nil nil 'closed))
  10036. (when (and now-done-p org-log-done)
  10037. ;; It is now done, and it was not done before
  10038. (org-add-planning-info 'closed (org-current-time))
  10039. (if (and (not dolog) (eq 'note org-log-done))
  10040. (org-add-log-setup 'done state this 'findpos 'note)))
  10041. (when (and state dolog)
  10042. ;; This is a non-nil state, and we need to log it
  10043. (org-add-log-setup 'state state this 'findpos dolog)))
  10044. ;; Fixup tag positioning
  10045. (org-todo-trigger-tag-changes state)
  10046. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10047. (when org-provide-todo-statistics
  10048. (org-update-parent-todo-statistics))
  10049. (run-hooks 'org-after-todo-state-change-hook)
  10050. (if (and arg (not (member state org-done-keywords)))
  10051. (setq head (org-get-todo-sequence-head state)))
  10052. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10053. ;; Do we need to trigger a repeat?
  10054. (when now-done-p
  10055. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10056. ;; This is for the agenda, take a snapshot of the headline.
  10057. (save-match-data
  10058. (setq org-agenda-headline-snapshot-before-repeat
  10059. (org-get-heading))))
  10060. (org-auto-repeat-maybe state))
  10061. ;; Fixup cursor location if close to the keyword
  10062. (if (and (outline-on-heading-p)
  10063. (not (bolp))
  10064. (save-excursion (beginning-of-line 1)
  10065. (looking-at org-todo-line-regexp))
  10066. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10067. (progn
  10068. (goto-char (or (match-end 2) (match-end 1)))
  10069. (and (looking-at " ") (just-one-space))))
  10070. (when org-trigger-hook
  10071. (save-excursion
  10072. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  10073. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10074. "Block turning an entry into a TODO, using the hierarchy.
  10075. This checks whether the current task should be blocked from state
  10076. changes. Such blocking occurs when:
  10077. 1. The task has children which are not all in a completed state.
  10078. 2. A task has a parent with the property :ORDERED:, and there
  10079. are siblings prior to the current task with incomplete
  10080. status.
  10081. 3. The parent of the task is blocked because it has siblings that should
  10082. be done first, or is child of a block grandparent TODO entry."
  10083. (if (not org-enforce-todo-dependencies)
  10084. t ; if locally turned off don't block
  10085. (catch 'dont-block
  10086. ;; If this is not a todo state change, or if this entry is already DONE,
  10087. ;; do not block
  10088. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10089. (member (plist-get change-plist :from)
  10090. (cons 'done org-done-keywords))
  10091. (member (plist-get change-plist :to)
  10092. (cons 'todo org-not-done-keywords))
  10093. (not (plist-get change-plist :to)))
  10094. (throw 'dont-block t))
  10095. ;; If this task has children, and any are undone, it's blocked
  10096. (save-excursion
  10097. (org-back-to-heading t)
  10098. (let ((this-level (funcall outline-level)))
  10099. (outline-next-heading)
  10100. (let ((child-level (funcall outline-level)))
  10101. (while (and (not (eobp))
  10102. (> child-level this-level))
  10103. ;; this todo has children, check whether they are all
  10104. ;; completed
  10105. (if (and (not (org-entry-is-done-p))
  10106. (org-entry-is-todo-p))
  10107. (throw 'dont-block nil))
  10108. (outline-next-heading)
  10109. (setq child-level (funcall outline-level))))))
  10110. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10111. ;; any previous siblings are undone, it's blocked
  10112. (save-excursion
  10113. (org-back-to-heading t)
  10114. (let* ((pos (point))
  10115. (parent-pos (and (org-up-heading-safe) (point))))
  10116. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10117. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10118. (forward-line 1)
  10119. (re-search-forward org-not-done-heading-regexp pos t))
  10120. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10121. ;; Search further up the hierarchy, to see if an anchestor is blocked
  10122. (while t
  10123. (goto-char parent-pos)
  10124. (if (not (looking-at org-not-done-heading-regexp))
  10125. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10126. (setq pos (point))
  10127. (setq parent-pos (and (org-up-heading-safe) (point)))
  10128. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10129. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10130. (forward-line 1)
  10131. (re-search-forward org-not-done-heading-regexp pos t))
  10132. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10133. (defcustom org-track-ordered-property-with-tag nil
  10134. "Should the ORDERED property also be shown as a tag?
  10135. The ORDERED property decides if an entry should require subtasks to be
  10136. completed in sequence. Since a property is not very visible, setting
  10137. this option means that toggling the ORDERED property with the command
  10138. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10139. not relevant for the behavior, but it makes things more visible.
  10140. Note that toggling the tag with tags commands will not change the property
  10141. and therefore not influence behavior!
  10142. This can be t, meaning the tag ORDERED should be used, It can also be a
  10143. string to select a different tag for this task."
  10144. :group 'org-todo
  10145. :type '(choice
  10146. (const :tag "No tracking" nil)
  10147. (const :tag "Track with ORDERED tag" t)
  10148. (string :tag "Use other tag")))
  10149. (defun org-toggle-ordered-property ()
  10150. "Toggle the ORDERED property of the current entry.
  10151. For better visibility, you can track the value of this property with a tag.
  10152. See variable `org-track-ordered-property-with-tag'."
  10153. (interactive)
  10154. (let* ((t1 org-track-ordered-property-with-tag)
  10155. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10156. (save-excursion
  10157. (org-back-to-heading)
  10158. (if (org-entry-get nil "ORDERED")
  10159. (progn
  10160. (org-delete-property "ORDERED")
  10161. (and tag (org-toggle-tag tag 'off))
  10162. (message "Subtasks can be completed in arbitrary order"))
  10163. (org-entry-put nil "ORDERED" "t")
  10164. (and tag (org-toggle-tag tag 'on))
  10165. (message "Subtasks must be completed in sequence")))))
  10166. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10167. (defun org-block-todo-from-checkboxes (change-plist)
  10168. "Block turning an entry into a TODO, using checkboxes.
  10169. This checks whether the current task should be blocked from state
  10170. changes because there are unchecked boxes in this entry."
  10171. (if (not org-enforce-todo-checkbox-dependencies)
  10172. t ; if locally turned off don't block
  10173. (catch 'dont-block
  10174. ;; If this is not a todo state change, or if this entry is already DONE,
  10175. ;; do not block
  10176. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10177. (member (plist-get change-plist :from)
  10178. (cons 'done org-done-keywords))
  10179. (member (plist-get change-plist :to)
  10180. (cons 'todo org-not-done-keywords))
  10181. (not (plist-get change-plist :to)))
  10182. (throw 'dont-block t))
  10183. ;; If this task has checkboxes that are not checked, it's blocked
  10184. (save-excursion
  10185. (org-back-to-heading t)
  10186. (let ((beg (point)) end)
  10187. (outline-next-heading)
  10188. (setq end (point))
  10189. (goto-char beg)
  10190. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  10191. end t)
  10192. (progn
  10193. (if (boundp 'org-blocked-by-checkboxes)
  10194. (setq org-blocked-by-checkboxes t))
  10195. (throw 'dont-block nil)))))
  10196. t))) ; do not block
  10197. (defun org-entry-blocked-p ()
  10198. "Is the current entry blocked?"
  10199. (if (org-entry-get nil "NOBLOCKING")
  10200. nil ;; Never block this entry
  10201. (not
  10202. (run-hook-with-args-until-failure
  10203. 'org-blocker-hook
  10204. (list :type 'todo-state-change
  10205. :position (point)
  10206. :from 'todo
  10207. :to 'done)))))
  10208. (defun org-update-statistics-cookies (all)
  10209. "Update the statistics cookie, either from TODO or from checkboxes.
  10210. This should be called with the cursor in a line with a statistics cookie."
  10211. (interactive "P")
  10212. (if all
  10213. (progn
  10214. (org-update-checkbox-count 'all)
  10215. (org-map-entries 'org-update-parent-todo-statistics))
  10216. (if (not (org-on-heading-p))
  10217. (org-update-checkbox-count)
  10218. (let ((pos (move-marker (make-marker) (point)))
  10219. end l1 l2)
  10220. (ignore-errors (org-back-to-heading t))
  10221. (if (not (org-on-heading-p))
  10222. (org-update-checkbox-count)
  10223. (setq l1 (org-outline-level))
  10224. (setq end (save-excursion
  10225. (outline-next-heading)
  10226. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10227. (point)))
  10228. (if (and (save-excursion
  10229. (re-search-forward
  10230. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10231. (not (save-excursion (re-search-forward
  10232. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10233. (org-update-checkbox-count)
  10234. (if (and l2 (> l2 l1))
  10235. (progn
  10236. (goto-char end)
  10237. (org-update-parent-todo-statistics))
  10238. (goto-char pos)
  10239. (beginning-of-line 1)
  10240. (while (re-search-forward
  10241. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10242. (point-at-eol) t)
  10243. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10244. (goto-char pos)
  10245. (move-marker pos nil)))))
  10246. (defvar org-entry-property-inherited-from) ;; defined below
  10247. (defun org-update-parent-todo-statistics ()
  10248. "Update any statistics cookie in the parent of the current headline.
  10249. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10250. the entire subtree and this will travel up the hierarchy and update
  10251. statistics everywhere."
  10252. (let* ((prop (save-excursion (org-up-heading-safe)
  10253. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10254. (recursive (or (not org-hierarchical-todo-statistics)
  10255. (and prop (string-match "\\<recursive\\>" prop))))
  10256. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10257. 0))
  10258. (first t)
  10259. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10260. level ltoggle l1 new ndel
  10261. (cnt-all 0) (cnt-done 0) is-percent kwd
  10262. checkbox-beg ov ovs ove cookie-present)
  10263. (catch 'exit
  10264. (save-excursion
  10265. (beginning-of-line 1)
  10266. (setq ltoggle (funcall outline-level))
  10267. ;; Three situations are to consider:
  10268. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10269. ;; to the top-level ancestor on the headline;
  10270. ;; 2. If parent has "recursive" property, repeat up to the
  10271. ;; headline setting that property, taking inheritance into
  10272. ;; account;
  10273. ;; 3. Else, move up to direct parent and proceed only once.
  10274. (while (and (setq level (org-up-heading-safe))
  10275. (or recursive first)
  10276. (>= (point) lim))
  10277. (setq first nil cookie-present nil)
  10278. (unless (and level
  10279. (not (string-match
  10280. "\\<checkbox\\>"
  10281. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10282. "")))))
  10283. (throw 'exit nil))
  10284. (while (re-search-forward box-re (point-at-eol) t)
  10285. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10286. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10287. (save-match-data
  10288. (unless (outline-next-heading) (throw 'exit nil))
  10289. (while (and (looking-at org-complex-heading-regexp)
  10290. (> (setq l1 (length (match-string 1))) level))
  10291. (setq kwd (and (or recursive (= l1 ltoggle))
  10292. (match-string 2)))
  10293. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10294. (and (listp org-provide-todo-statistics)
  10295. (or (member kwd org-provide-todo-statistics)
  10296. (member kwd org-done-keywords))))
  10297. (setq cnt-all (1+ cnt-all))
  10298. (if (eq org-provide-todo-statistics t)
  10299. (and kwd (setq cnt-all (1+ cnt-all)))))
  10300. (and (member kwd org-done-keywords)
  10301. (setq cnt-done (1+ cnt-done)))
  10302. (outline-next-heading)))
  10303. (setq new
  10304. (if is-percent
  10305. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10306. (format "[%d/%d]" cnt-done cnt-all))
  10307. ndel (- (match-end 0) checkbox-beg))
  10308. ;; handle overlays when updating cookie from column view
  10309. (when (setq ov (car (overlays-at checkbox-beg)))
  10310. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10311. (delete-overlay ov))
  10312. (goto-char checkbox-beg)
  10313. (insert new)
  10314. (delete-region (point) (+ (point) ndel))
  10315. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10316. (when ov (move-overlay ov ovs ove)))
  10317. (when cookie-present
  10318. (run-hook-with-args 'org-after-todo-statistics-hook
  10319. cnt-done (- cnt-all cnt-done))))))
  10320. (run-hooks 'org-todo-statistics-hook)))
  10321. (defvar org-after-todo-statistics-hook nil
  10322. "Hook that is called after a TODO statistics cookie has been updated.
  10323. Each function is called with two arguments: the number of not-done entries
  10324. and the number of done entries.
  10325. For example, the following function, when added to this hook, will switch
  10326. an entry to DONE when all children are done, and back to TODO when new
  10327. entries are set to a TODO status. Note that this hook is only called
  10328. when there is a statistics cookie in the headline!
  10329. (defun org-summary-todo (n-done n-not-done)
  10330. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10331. (let (org-log-done org-log-states) ; turn off logging
  10332. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10333. ")
  10334. (defvar org-todo-statistics-hook nil
  10335. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10336. This hook runs even if there is no statistics cookie present, in which case
  10337. `org-after-todo-statistics-hook' would not run.")
  10338. (defun org-todo-trigger-tag-changes (state)
  10339. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10340. (let ((l org-todo-state-tags-triggers)
  10341. changes)
  10342. (when (or (not state) (equal state ""))
  10343. (setq changes (append changes (cdr (assoc "" l)))))
  10344. (when (and (stringp state) (> (length state) 0))
  10345. (setq changes (append changes (cdr (assoc state l)))))
  10346. (when (member state org-not-done-keywords)
  10347. (setq changes (append changes (cdr (assoc 'todo l)))))
  10348. (when (member state org-done-keywords)
  10349. (setq changes (append changes (cdr (assoc 'done l)))))
  10350. (dolist (c changes)
  10351. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10352. (defun org-local-logging (value)
  10353. "Get logging settings from a property VALUE."
  10354. (let* (words w a)
  10355. ;; directly set the variables, they are already local.
  10356. (setq org-log-done nil
  10357. org-log-repeat nil
  10358. org-todo-log-states nil)
  10359. (setq words (org-split-string value))
  10360. (while (setq w (pop words))
  10361. (cond
  10362. ((setq a (assoc w org-startup-options))
  10363. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10364. (set (nth 1 a) (nth 2 a))))
  10365. ((setq a (org-extract-log-state-settings w))
  10366. (and (member (car a) org-todo-keywords-1)
  10367. (push a org-todo-log-states)))))))
  10368. (defun org-get-todo-sequence-head (kwd)
  10369. "Return the head of the TODO sequence to which KWD belongs.
  10370. If KWD is not set, check if there is a text property remembering the
  10371. right sequence."
  10372. (let (p)
  10373. (cond
  10374. ((not kwd)
  10375. (or (get-text-property (point-at-bol) 'org-todo-head)
  10376. (progn
  10377. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10378. nil (point-at-eol)))
  10379. (get-text-property p 'org-todo-head))))
  10380. ((not (member kwd org-todo-keywords-1))
  10381. (car org-todo-keywords-1))
  10382. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10383. (defun org-fast-todo-selection ()
  10384. "Fast TODO keyword selection with single keys.
  10385. Returns the new TODO keyword, or nil if no state change should occur."
  10386. (let* ((fulltable org-todo-key-alist)
  10387. (done-keywords org-done-keywords) ;; needed for the faces.
  10388. (maxlen (apply 'max (mapcar
  10389. (lambda (x)
  10390. (if (stringp (car x)) (string-width (car x)) 0))
  10391. fulltable)))
  10392. (expert nil)
  10393. (fwidth (+ maxlen 3 1 3))
  10394. (ncol (/ (- (window-width) 4) fwidth))
  10395. tg cnt e c tbl
  10396. groups ingroup)
  10397. (save-excursion
  10398. (save-window-excursion
  10399. (if expert
  10400. (set-buffer (get-buffer-create " *Org todo*"))
  10401. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10402. (erase-buffer)
  10403. (org-set-local 'org-done-keywords done-keywords)
  10404. (setq tbl fulltable cnt 0)
  10405. (while (setq e (pop tbl))
  10406. (cond
  10407. ((equal e '(:startgroup))
  10408. (push '() groups) (setq ingroup t)
  10409. (when (not (= cnt 0))
  10410. (setq cnt 0)
  10411. (insert "\n"))
  10412. (insert "{ "))
  10413. ((equal e '(:endgroup))
  10414. (setq ingroup nil cnt 0)
  10415. (insert "}\n"))
  10416. ((equal e '(:newline))
  10417. (when (not (= cnt 0))
  10418. (setq cnt 0)
  10419. (insert "\n")
  10420. (setq e (car tbl))
  10421. (while (equal (car tbl) '(:newline))
  10422. (insert "\n")
  10423. (setq tbl (cdr tbl)))))
  10424. (t
  10425. (setq tg (car e) c (cdr e))
  10426. (if ingroup (push tg (car groups)))
  10427. (setq tg (org-add-props tg nil 'face
  10428. (org-get-todo-face tg)))
  10429. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10430. (insert "[" c "] " tg (make-string
  10431. (- fwidth 4 (length tg)) ?\ ))
  10432. (when (= (setq cnt (1+ cnt)) ncol)
  10433. (insert "\n")
  10434. (if ingroup (insert " "))
  10435. (setq cnt 0)))))
  10436. (insert "\n")
  10437. (goto-char (point-min))
  10438. (if (not expert) (org-fit-window-to-buffer))
  10439. (message "[a-z..]:Set [SPC]:clear")
  10440. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10441. (cond
  10442. ((or (= c ?\C-g)
  10443. (and (= c ?q) (not (rassoc c fulltable))))
  10444. (setq quit-flag t))
  10445. ((= c ?\ ) nil)
  10446. ((setq e (rassoc c fulltable) tg (car e))
  10447. tg)
  10448. (t (setq quit-flag t)))))))
  10449. (defun org-entry-is-todo-p ()
  10450. (member (org-get-todo-state) org-not-done-keywords))
  10451. (defun org-entry-is-done-p ()
  10452. (member (org-get-todo-state) org-done-keywords))
  10453. (defun org-get-todo-state ()
  10454. (save-excursion
  10455. (org-back-to-heading t)
  10456. (and (looking-at org-todo-line-regexp)
  10457. (match-end 2)
  10458. (match-string 2))))
  10459. (defun org-at-date-range-p (&optional inactive-ok)
  10460. "Is the cursor inside a date range?"
  10461. (interactive)
  10462. (save-excursion
  10463. (catch 'exit
  10464. (let ((pos (point)))
  10465. (skip-chars-backward "^[<\r\n")
  10466. (skip-chars-backward "<[")
  10467. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10468. (>= (match-end 0) pos)
  10469. (throw 'exit t))
  10470. (skip-chars-backward "^<[\r\n")
  10471. (skip-chars-backward "<[")
  10472. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10473. (>= (match-end 0) pos)
  10474. (throw 'exit t)))
  10475. nil)))
  10476. (defun org-get-repeat (&optional tagline)
  10477. "Check if there is a deadline/schedule with repeater in this entry."
  10478. (save-match-data
  10479. (save-excursion
  10480. (org-back-to-heading t)
  10481. (and (re-search-forward (if tagline
  10482. (concat tagline "\\s-*" org-repeat-re)
  10483. org-repeat-re)
  10484. (org-entry-end-position) t)
  10485. (match-string-no-properties 1)))))
  10486. (defvar org-last-changed-timestamp)
  10487. (defvar org-last-inserted-timestamp)
  10488. (defvar org-log-post-message)
  10489. (defvar org-log-note-purpose)
  10490. (defvar org-log-note-how)
  10491. (defvar org-log-note-extra)
  10492. (defun org-auto-repeat-maybe (done-word)
  10493. "Check if the current headline contains a repeated deadline/schedule.
  10494. If yes, set TODO state back to what it was and change the base date
  10495. of repeating deadline/scheduled time stamps to new date.
  10496. This function is run automatically after each state change to a DONE state."
  10497. ;; last-state is dynamically scoped into this function
  10498. (let* ((repeat (org-get-repeat))
  10499. (aa (assoc last-state org-todo-kwd-alist))
  10500. (interpret (nth 1 aa))
  10501. (head (nth 2 aa))
  10502. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10503. (msg "Entry repeats: ")
  10504. (org-log-done nil)
  10505. (org-todo-log-states nil)
  10506. re type n what ts time to-state)
  10507. (when repeat
  10508. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10509. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10510. org-todo-repeat-to-state))
  10511. (unless (and to-state (member to-state org-todo-keywords-1))
  10512. (setq to-state (if (eq interpret 'type) last-state head)))
  10513. (org-todo to-state)
  10514. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10515. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10516. (org-time-stamp-format t t))))
  10517. (when org-log-repeat
  10518. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10519. (memq 'org-add-log-note post-command-hook))
  10520. ;; OK, we are already setup for some record
  10521. (if (eq org-log-repeat 'note)
  10522. ;; make sure we take a note, not only a time stamp
  10523. (setq org-log-note-how 'note))
  10524. ;; Set up for taking a record
  10525. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10526. last-state
  10527. 'findpos org-log-repeat)))
  10528. (org-back-to-heading t)
  10529. (org-add-planning-info nil nil 'closed)
  10530. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10531. org-deadline-time-regexp "\\)\\|\\("
  10532. org-ts-regexp "\\)"))
  10533. (while (re-search-forward
  10534. re (save-excursion (outline-next-heading) (point)) t)
  10535. (setq type (if (match-end 1) org-scheduled-string
  10536. (if (match-end 3) org-deadline-string "Plain:"))
  10537. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10538. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10539. (setq n (string-to-number (match-string 2 ts))
  10540. what (match-string 3 ts))
  10541. (if (equal what "w") (setq n (* n 7) what "d"))
  10542. ;; Preparation, see if we need to modify the start date for the change
  10543. (when (match-end 1)
  10544. (setq time (save-match-data (org-time-string-to-time ts)))
  10545. (cond
  10546. ((equal (match-string 1 ts) ".")
  10547. ;; Shift starting date to today
  10548. (org-timestamp-change
  10549. (- (time-to-days (current-time)) (time-to-days time))
  10550. 'day))
  10551. ((equal (match-string 1 ts) "+")
  10552. (let ((nshiftmax 10) (nshift 0))
  10553. (while (or (= nshift 0)
  10554. (<= (time-to-days time)
  10555. (time-to-days (current-time))))
  10556. (when (= (incf nshift) nshiftmax)
  10557. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10558. (error "Abort")))
  10559. (org-timestamp-change n (cdr (assoc what whata)))
  10560. (org-at-timestamp-p t)
  10561. (setq ts (match-string 1))
  10562. (setq time (save-match-data (org-time-string-to-time ts)))))
  10563. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10564. ;; rematch, so that we have everything in place for the real shift
  10565. (org-at-timestamp-p t)
  10566. (setq ts (match-string 1))
  10567. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10568. (org-timestamp-change n (cdr (assoc what whata)))
  10569. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10570. (setq org-log-post-message msg)
  10571. (message "%s" msg))))
  10572. (defun org-show-todo-tree (arg)
  10573. "Make a compact tree which shows all headlines marked with TODO.
  10574. The tree will show the lines where the regexp matches, and all higher
  10575. headlines above the match.
  10576. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10577. With a numeric prefix N, construct a sparse tree for the Nth element
  10578. of `org-todo-keywords-1'."
  10579. (interactive "P")
  10580. (let ((case-fold-search nil)
  10581. (kwd-re
  10582. (cond ((null arg) org-not-done-regexp)
  10583. ((equal arg '(4))
  10584. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10585. (mapcar 'list org-todo-keywords-1))))
  10586. (concat "\\("
  10587. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10588. "\\)\\>")))
  10589. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10590. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10591. org-todo-keywords-1)))
  10592. (t (error "Invalid prefix argument: %s" arg)))))
  10593. (message "%d TODO entries found"
  10594. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10595. (defun org-deadline (&optional remove time)
  10596. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10597. With argument REMOVE, remove any deadline from the item.
  10598. When TIME is set, it should be an internal time specification, and the
  10599. scheduling will use the corresponding date."
  10600. (interactive "P")
  10601. (org-loop-over-siblings-in-active-region
  10602. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10603. (repeater (and old-date
  10604. (string-match
  10605. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10606. old-date)
  10607. (match-string 1 old-date))))
  10608. (if remove
  10609. (progn
  10610. (when (and old-date org-log-redeadline)
  10611. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10612. org-log-redeadline))
  10613. (org-remove-timestamp-with-keyword org-deadline-string)
  10614. (message "Item no longer has a deadline."))
  10615. (org-add-planning-info 'deadline time 'closed)
  10616. (when (and old-date org-log-redeadline
  10617. (not (equal old-date
  10618. (substring org-last-inserted-timestamp 1 -1))))
  10619. (org-add-log-setup 'redeadline nil old-date 'findpos
  10620. org-log-redeadline))
  10621. (when repeater
  10622. (save-excursion
  10623. (org-back-to-heading t)
  10624. (when (re-search-forward (concat org-deadline-string " "
  10625. org-last-inserted-timestamp)
  10626. (save-excursion
  10627. (outline-next-heading) (point)) t)
  10628. (goto-char (1- (match-end 0)))
  10629. (insert " " repeater)
  10630. (setq org-last-inserted-timestamp
  10631. (concat (substring org-last-inserted-timestamp 0 -1)
  10632. " " repeater
  10633. (substring org-last-inserted-timestamp -1))))))
  10634. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10635. (defun org-schedule (&optional remove time)
  10636. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10637. With argument REMOVE, remove any scheduling date from the item.
  10638. When TIME is set, it should be an internal time specification, and the
  10639. scheduling will use the corresponding date."
  10640. (interactive "P")
  10641. (org-loop-over-siblings-in-active-region
  10642. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10643. (repeater (and old-date
  10644. (string-match
  10645. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10646. old-date)
  10647. (match-string 1 old-date))))
  10648. (if remove
  10649. (progn
  10650. (when (and old-date org-log-reschedule)
  10651. (org-add-log-setup 'delschedule nil old-date 'findpos
  10652. org-log-reschedule))
  10653. (org-remove-timestamp-with-keyword org-scheduled-string)
  10654. (message "Item is no longer scheduled."))
  10655. (org-add-planning-info 'scheduled time 'closed)
  10656. (when (and old-date org-log-reschedule
  10657. (not (equal old-date
  10658. (substring org-last-inserted-timestamp 1 -1))))
  10659. (org-add-log-setup 'reschedule nil old-date 'findpos
  10660. org-log-reschedule))
  10661. (when repeater
  10662. (save-excursion
  10663. (org-back-to-heading t)
  10664. (when (re-search-forward (concat org-scheduled-string " "
  10665. org-last-inserted-timestamp)
  10666. (save-excursion
  10667. (outline-next-heading) (point)) t)
  10668. (goto-char (1- (match-end 0)))
  10669. (insert " " repeater)
  10670. (setq org-last-inserted-timestamp
  10671. (concat (substring org-last-inserted-timestamp 0 -1)
  10672. " " repeater
  10673. (substring org-last-inserted-timestamp -1))))))
  10674. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10675. (defun org-get-scheduled-time (pom &optional inherit)
  10676. "Get the scheduled time as a time tuple, of a format suitable
  10677. for calling org-schedule with, or if there is no scheduling,
  10678. returns nil."
  10679. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10680. (when time
  10681. (apply 'encode-time (org-parse-time-string time)))))
  10682. (defun org-get-deadline-time (pom &optional inherit)
  10683. "Get the deadline as a time tuple, of a format suitable for
  10684. calling org-deadline with, or if there is no scheduling, returns
  10685. nil."
  10686. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10687. (when time
  10688. (apply 'encode-time (org-parse-time-string time)))))
  10689. (defun org-remove-timestamp-with-keyword (keyword)
  10690. "Remove all time stamps with KEYWORD in the current entry."
  10691. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10692. beg)
  10693. (save-excursion
  10694. (org-back-to-heading t)
  10695. (setq beg (point))
  10696. (outline-next-heading)
  10697. (while (re-search-backward re beg t)
  10698. (replace-match "")
  10699. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10700. (equal (char-before) ?\ ))
  10701. (backward-delete-char 1)
  10702. (if (string-match "^[ \t]*$" (buffer-substring
  10703. (point-at-bol) (point-at-eol)))
  10704. (delete-region (point-at-bol)
  10705. (min (point-max) (1+ (point-at-eol))))))))))
  10706. (defun org-add-planning-info (what &optional time &rest remove)
  10707. "Insert new timestamp with keyword in the line directly after the headline.
  10708. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10709. If non is given, the user is prompted for a date.
  10710. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10711. be removed."
  10712. (interactive)
  10713. (let (org-time-was-given org-end-time-was-given ts
  10714. end default-time default-input)
  10715. (catch 'exit
  10716. (when (and (not time) (memq what '(scheduled deadline)))
  10717. ;; Try to get a default date/time from existing timestamp
  10718. (save-excursion
  10719. (org-back-to-heading t)
  10720. (setq end (save-excursion (outline-next-heading) (point)))
  10721. (when (re-search-forward (if (eq what 'scheduled)
  10722. org-scheduled-time-regexp
  10723. org-deadline-time-regexp)
  10724. end t)
  10725. (setq ts (match-string 1)
  10726. default-time
  10727. (apply 'encode-time (org-parse-time-string ts))
  10728. default-input (and ts (org-get-compact-tod ts))))))
  10729. (when what
  10730. ;; If necessary, get the time from the user
  10731. (setq time (or time (org-read-date nil 'to-time nil nil
  10732. default-time default-input))))
  10733. (when (and org-insert-labeled-timestamps-at-point
  10734. (member what '(scheduled deadline)))
  10735. (insert
  10736. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10737. (org-insert-time-stamp time org-time-was-given
  10738. nil nil nil (list org-end-time-was-given))
  10739. (setq what nil))
  10740. (save-excursion
  10741. (save-restriction
  10742. (let (col list elt ts buffer-invisibility-spec)
  10743. (org-back-to-heading t)
  10744. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  10745. (goto-char (match-end 1))
  10746. (setq col (current-column))
  10747. (goto-char (match-end 0))
  10748. (if (eobp) (insert "\n") (forward-char 1))
  10749. (when (and (not what)
  10750. (not (looking-at
  10751. (concat "[ \t]*"
  10752. org-keyword-time-not-clock-regexp))))
  10753. ;; Nothing to add, nothing to remove...... :-)
  10754. (throw 'exit nil))
  10755. (if (and (not (looking-at org-outline-regexp))
  10756. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10757. "[^\r\n]*"))
  10758. (not (equal (match-string 1) org-clock-string)))
  10759. (narrow-to-region (match-beginning 0) (match-end 0))
  10760. (insert-before-markers "\n")
  10761. (backward-char 1)
  10762. (narrow-to-region (point) (point))
  10763. (and org-adapt-indentation (org-indent-to-column col)))
  10764. ;; Check if we have to remove something.
  10765. (setq list (cons what remove))
  10766. (while list
  10767. (setq elt (pop list))
  10768. (when (or (and (eq elt 'scheduled)
  10769. (re-search-forward org-scheduled-time-regexp nil t))
  10770. (and (eq elt 'deadline)
  10771. (re-search-forward org-deadline-time-regexp nil t))
  10772. (and (eq elt 'closed)
  10773. (re-search-forward org-closed-time-regexp nil t)))
  10774. (replace-match "")
  10775. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  10776. (and (looking-at "^[ \t]+") (replace-match ""))
  10777. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10778. (when what
  10779. (insert
  10780. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10781. (cond ((eq what 'scheduled) org-scheduled-string)
  10782. ((eq what 'deadline) org-deadline-string)
  10783. ((eq what 'closed) org-closed-string))
  10784. " ")
  10785. (setq ts (org-insert-time-stamp
  10786. time
  10787. (or org-time-was-given
  10788. (and (eq what 'closed) org-log-done-with-time))
  10789. (eq what 'closed)
  10790. nil nil (list org-end-time-was-given)))
  10791. (insert
  10792. (if (not (or (bolp) (eq (char-before) ?\ )
  10793. (memq (char-after) '(32 10))
  10794. (eobp))) " " ""))
  10795. (end-of-line 1))
  10796. (goto-char (point-min))
  10797. (widen)
  10798. (if (and (looking-at "[ \t]*\n")
  10799. (equal (char-before) ?\n))
  10800. (delete-region (1- (point)) (point-at-eol)))
  10801. ts))))))
  10802. (defvar org-log-note-marker (make-marker))
  10803. (defvar org-log-note-purpose nil)
  10804. (defvar org-log-note-state nil)
  10805. (defvar org-log-note-previous-state nil)
  10806. (defvar org-log-note-how nil)
  10807. (defvar org-log-note-extra nil)
  10808. (defvar org-log-note-window-configuration nil)
  10809. (defvar org-log-note-return-to (make-marker))
  10810. (defvar org-log-post-message nil
  10811. "Message to be displayed after a log note has been stored.
  10812. The auto-repeater uses this.")
  10813. (defun org-add-note ()
  10814. "Add a note to the current entry.
  10815. This is done in the same way as adding a state change note."
  10816. (interactive)
  10817. (org-add-log-setup 'note nil nil 'findpos nil))
  10818. (defvar org-property-end-re)
  10819. (defun org-add-log-setup (&optional purpose state prev-state
  10820. findpos how extra)
  10821. "Set up the post command hook to take a note.
  10822. If this is about to TODO state change, the new state is expected in STATE.
  10823. When FINDPOS is non-nil, find the correct position for the note in
  10824. the current entry. If not, assume that it can be inserted at point.
  10825. HOW is an indicator what kind of note should be created.
  10826. EXTRA is additional text that will be inserted into the notes buffer."
  10827. (let* ((org-log-into-drawer (org-log-into-drawer))
  10828. (drawer (cond ((stringp org-log-into-drawer)
  10829. org-log-into-drawer)
  10830. (org-log-into-drawer "LOGBOOK")
  10831. (t nil))))
  10832. (save-restriction
  10833. (save-excursion
  10834. (when findpos
  10835. (org-back-to-heading t)
  10836. (narrow-to-region (point) (save-excursion
  10837. (outline-next-heading) (point)))
  10838. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  10839. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10840. "[^\r\n]*\\)?"))
  10841. (goto-char (match-end 0))
  10842. (cond
  10843. (drawer
  10844. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10845. nil t)
  10846. (progn
  10847. (goto-char (match-end 0))
  10848. (or org-log-states-order-reversed
  10849. (and (re-search-forward org-property-end-re nil t)
  10850. (goto-char (1- (match-beginning 0))))))
  10851. (insert "\n:" drawer ":\n:END:")
  10852. (beginning-of-line 0)
  10853. (org-indent-line-function)
  10854. (beginning-of-line 2)
  10855. (org-indent-line-function)
  10856. (end-of-line 0)))
  10857. ((and org-log-state-notes-insert-after-drawers
  10858. (save-excursion
  10859. (forward-line) (looking-at org-drawer-regexp)))
  10860. (forward-line)
  10861. (while (looking-at org-drawer-regexp)
  10862. (goto-char (match-end 0))
  10863. (re-search-forward org-property-end-re (point-max) t)
  10864. (forward-line))
  10865. (forward-line -1)))
  10866. (unless org-log-states-order-reversed
  10867. (and (= (char-after) ?\n) (forward-char 1))
  10868. (org-skip-over-state-notes)
  10869. (skip-chars-backward " \t\n\r")))
  10870. (move-marker org-log-note-marker (point))
  10871. (setq org-log-note-purpose purpose
  10872. org-log-note-state state
  10873. org-log-note-previous-state prev-state
  10874. org-log-note-how how
  10875. org-log-note-extra extra)
  10876. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10877. (defun org-skip-over-state-notes ()
  10878. "Skip past the list of State notes in an entry."
  10879. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10880. (when (ignore-errors (goto-char (org-in-item-p)))
  10881. (let* ((struct (org-list-struct))
  10882. (prevs (org-list-prevs-alist struct)))
  10883. (while (looking-at "[ \t]*- State")
  10884. (goto-char (or (org-list-get-next-item (point) struct prevs)
  10885. (org-list-get-item-end (point) struct)))))))
  10886. (defun org-add-log-note (&optional purpose)
  10887. "Pop up a window for taking a note, and add this note later at point."
  10888. (remove-hook 'post-command-hook 'org-add-log-note)
  10889. (setq org-log-note-window-configuration (current-window-configuration))
  10890. (delete-other-windows)
  10891. (move-marker org-log-note-return-to (point))
  10892. (switch-to-buffer (marker-buffer org-log-note-marker))
  10893. (goto-char org-log-note-marker)
  10894. (org-switch-to-buffer-other-window "*Org Note*")
  10895. (erase-buffer)
  10896. (if (memq org-log-note-how '(time state))
  10897. (let (current-prefix-arg) (org-store-log-note))
  10898. (let ((org-inhibit-startup t)) (org-mode))
  10899. (insert (format "# Insert note for %s.
  10900. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10901. (cond
  10902. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10903. ((eq org-log-note-purpose 'done) "closed todo item")
  10904. ((eq org-log-note-purpose 'state)
  10905. (format "state change from \"%s\" to \"%s\""
  10906. (or org-log-note-previous-state "")
  10907. (or org-log-note-state "")))
  10908. ((eq org-log-note-purpose 'reschedule)
  10909. "rescheduling")
  10910. ((eq org-log-note-purpose 'delschedule)
  10911. "no longer scheduled")
  10912. ((eq org-log-note-purpose 'redeadline)
  10913. "changing deadline")
  10914. ((eq org-log-note-purpose 'deldeadline)
  10915. "removing deadline")
  10916. ((eq org-log-note-purpose 'refile)
  10917. "refiling")
  10918. ((eq org-log-note-purpose 'note)
  10919. "this entry")
  10920. (t (error "This should not happen")))))
  10921. (if org-log-note-extra (insert org-log-note-extra))
  10922. (org-set-local 'org-finish-function 'org-store-log-note)))
  10923. (defvar org-note-abort nil) ; dynamically scoped
  10924. (defun org-store-log-note ()
  10925. "Finish taking a log note, and insert it to where it belongs."
  10926. (let ((txt (buffer-string))
  10927. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10928. lines ind bul)
  10929. (kill-buffer (current-buffer))
  10930. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10931. (setq txt (replace-match "" t t txt)))
  10932. (if (string-match "\\s-+\\'" txt)
  10933. (setq txt (replace-match "" t t txt)))
  10934. (setq lines (org-split-string txt "\n"))
  10935. (when (and note (string-match "\\S-" note))
  10936. (setq note
  10937. (org-replace-escapes
  10938. note
  10939. (list (cons "%u" (user-login-name))
  10940. (cons "%U" user-full-name)
  10941. (cons "%t" (format-time-string
  10942. (org-time-stamp-format 'long 'inactive)
  10943. (current-time)))
  10944. (cons "%T" (format-time-string
  10945. (org-time-stamp-format 'long nil)
  10946. (current-time)))
  10947. (cons "%s" (if org-log-note-state
  10948. (concat "\"" org-log-note-state "\"")
  10949. ""))
  10950. (cons "%S" (if org-log-note-previous-state
  10951. (concat "\"" org-log-note-previous-state "\"")
  10952. "\"\"")))))
  10953. (if lines (setq note (concat note " \\\\")))
  10954. (push note lines))
  10955. (when (or current-prefix-arg org-note-abort)
  10956. (when org-log-into-drawer
  10957. (org-remove-empty-drawer-at
  10958. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10959. org-log-note-marker))
  10960. (setq lines nil))
  10961. (when lines
  10962. (with-current-buffer (marker-buffer org-log-note-marker)
  10963. (save-excursion
  10964. (goto-char org-log-note-marker)
  10965. (move-marker org-log-note-marker nil)
  10966. (end-of-line 1)
  10967. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10968. (setq ind (save-excursion
  10969. (if (ignore-errors (goto-char (org-in-item-p)))
  10970. (let ((struct (org-list-struct)))
  10971. (org-list-get-ind
  10972. (org-list-get-top-point struct) struct))
  10973. (skip-chars-backward " \r\t\n")
  10974. (cond
  10975. ((and (org-at-heading-p)
  10976. org-adapt-indentation)
  10977. (1+ (org-current-level)))
  10978. ((org-at-heading-p) 0)
  10979. (t (org-get-indentation))))))
  10980. (setq bul (org-list-bullet-string "-"))
  10981. (org-indent-line-to ind)
  10982. (insert bul (pop lines))
  10983. (let ((ind-body (+ (length bul) ind)))
  10984. (while lines
  10985. (insert "\n")
  10986. (org-indent-line-to ind-body)
  10987. (insert (pop lines))))
  10988. (message "Note stored")
  10989. (org-back-to-heading t)
  10990. (org-cycle-hide-drawers 'children)))))
  10991. (set-window-configuration org-log-note-window-configuration)
  10992. (with-current-buffer (marker-buffer org-log-note-return-to)
  10993. (goto-char org-log-note-return-to))
  10994. (move-marker org-log-note-return-to nil)
  10995. (and org-log-post-message (message "%s" org-log-post-message)))
  10996. (defun org-remove-empty-drawer-at (drawer pos)
  10997. "Remove an empty drawer DRAWER at position POS.
  10998. POS may also be a marker."
  10999. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11000. (save-excursion
  11001. (save-restriction
  11002. (widen)
  11003. (goto-char pos)
  11004. (if (org-in-regexp
  11005. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11006. (replace-match ""))))))
  11007. (defun org-sparse-tree (&optional arg)
  11008. "Create a sparse tree, prompt for the details.
  11009. This command can create sparse trees. You first need to select the type
  11010. of match used to create the tree:
  11011. t Show all TODO entries.
  11012. T Show entries with a specific TODO keyword.
  11013. m Show entries selected by a tags/property match.
  11014. p Enter a property name and its value (both with completion on existing
  11015. names/values) and show entries with that property.
  11016. r Show entries matching a regular expression (`/' can be used as well)
  11017. d Show deadlines due within `org-deadline-warning-days'.
  11018. b Show deadlines and scheduled items before a date.
  11019. a Show deadlines and scheduled items after a date."
  11020. (interactive "P")
  11021. (let (ans kwd value)
  11022. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  11023. (setq ans (read-char-exclusive))
  11024. (cond
  11025. ((equal ans ?d)
  11026. (call-interactively 'org-check-deadlines))
  11027. ((equal ans ?b)
  11028. (call-interactively 'org-check-before-date))
  11029. ((equal ans ?a)
  11030. (call-interactively 'org-check-after-date))
  11031. ((equal ans ?t)
  11032. (org-show-todo-tree nil))
  11033. ((equal ans ?T)
  11034. (org-show-todo-tree '(4)))
  11035. ((member ans '(?T ?m))
  11036. (call-interactively 'org-match-sparse-tree))
  11037. ((member ans '(?p ?P))
  11038. (setq kwd (org-icompleting-read "Property: "
  11039. (mapcar 'list (org-buffer-property-keys))))
  11040. (setq value (org-icompleting-read "Value: "
  11041. (mapcar 'list (org-property-values kwd))))
  11042. (unless (string-match "\\`{.*}\\'" value)
  11043. (setq value (concat "\"" value "\"")))
  11044. (org-match-sparse-tree arg (concat kwd "=" value)))
  11045. ((member ans '(?r ?R ?/))
  11046. (call-interactively 'org-occur))
  11047. (t (error "No such sparse tree command \"%c\"" ans)))))
  11048. (defvar org-occur-highlights nil
  11049. "List of overlays used for occur matches.")
  11050. (make-variable-buffer-local 'org-occur-highlights)
  11051. (defvar org-occur-parameters nil
  11052. "Parameters of the active org-occur calls.
  11053. This is a list, each call to org-occur pushes as cons cell,
  11054. containing the regular expression and the callback, onto the list.
  11055. The list can contain several entries if `org-occur' has been called
  11056. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11057. will only contain one set of parameters. When the highlights are
  11058. removed (for example with `C-c C-c', or with the next edit (depending
  11059. on `org-remove-highlights-with-change'), this variable is emptied
  11060. as well.")
  11061. (make-variable-buffer-local 'org-occur-parameters)
  11062. (defun org-occur (regexp &optional keep-previous callback)
  11063. "Make a compact tree which shows all matches of REGEXP.
  11064. The tree will show the lines where the regexp matches, and all higher
  11065. headlines above the match. It will also show the heading after the match,
  11066. to make sure editing the matching entry is easy.
  11067. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11068. call to `org-occur' will be kept, to allow stacking of calls to this
  11069. command.
  11070. If CALLBACK is non-nil, it is a function which is called to confirm
  11071. that the match should indeed be shown."
  11072. (interactive "sRegexp: \nP")
  11073. (when (equal regexp "")
  11074. (error "Regexp cannot be empty"))
  11075. (unless keep-previous
  11076. (org-remove-occur-highlights nil nil t))
  11077. (push (cons regexp callback) org-occur-parameters)
  11078. (let ((cnt 0))
  11079. (save-excursion
  11080. (goto-char (point-min))
  11081. (if (or (not keep-previous) ; do not want to keep
  11082. (not org-occur-highlights)) ; no previous matches
  11083. ;; hide everything
  11084. (org-overview))
  11085. (while (re-search-forward regexp nil t)
  11086. (when (or (not callback)
  11087. (save-match-data (funcall callback)))
  11088. (setq cnt (1+ cnt))
  11089. (when org-highlight-sparse-tree-matches
  11090. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11091. (org-show-context 'occur-tree))))
  11092. (when org-remove-highlights-with-change
  11093. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11094. nil 'local))
  11095. (unless org-sparse-tree-open-archived-trees
  11096. (org-hide-archived-subtrees (point-min) (point-max)))
  11097. (run-hooks 'org-occur-hook)
  11098. (if (org-called-interactively-p 'interactive)
  11099. (message "%d match(es) for regexp %s" cnt regexp))
  11100. cnt))
  11101. (defun org-occur-next-match (&optional n reset)
  11102. "Function for `next-error-function' to find sparse tree matches.
  11103. N is the number of matches to move, when negative move backwards.
  11104. RESET is entirely ignored - this function always goes back to the
  11105. starting point when no match is found."
  11106. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11107. (search-func (if (< n 0)
  11108. 'previous-single-char-property-change
  11109. 'next-single-char-property-change))
  11110. (n (abs n))
  11111. (pos (point))
  11112. p1)
  11113. (catch 'exit
  11114. (while (setq p1 (funcall search-func (point) 'org-type))
  11115. (when (equal p1 limit)
  11116. (goto-char pos)
  11117. (error "No more matches"))
  11118. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11119. (setq n (1- n))
  11120. (when (= n 0)
  11121. (goto-char p1)
  11122. (throw 'exit (point))))
  11123. (goto-char p1))
  11124. (goto-char p1)
  11125. (error "No more matches"))))
  11126. (defun org-show-context (&optional key)
  11127. "Make sure point and context are visible.
  11128. How much context is shown depends upon the variables
  11129. `org-show-hierarchy-above', `org-show-following-heading'. and
  11130. `org-show-siblings'."
  11131. (let ((heading-p (org-on-heading-p t))
  11132. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11133. (following-p (org-get-alist-option org-show-following-heading key))
  11134. (entry-p (org-get-alist-option org-show-entry-below key))
  11135. (siblings-p (org-get-alist-option org-show-siblings key)))
  11136. (catch 'exit
  11137. ;; Show heading or entry text
  11138. (if (and heading-p (not entry-p))
  11139. (org-flag-heading nil) ; only show the heading
  11140. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11141. (org-show-hidden-entry))) ; show entire entry
  11142. (when following-p
  11143. ;; Show next sibling, or heading below text
  11144. (save-excursion
  11145. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11146. (org-flag-heading nil))))
  11147. (when siblings-p (org-show-siblings))
  11148. (when hierarchy-p
  11149. ;; show all higher headings, possibly with siblings
  11150. (save-excursion
  11151. (while (and (condition-case nil
  11152. (progn (org-up-heading-all 1) t)
  11153. (error nil))
  11154. (not (bobp)))
  11155. (org-flag-heading nil)
  11156. (when siblings-p (org-show-siblings))))))))
  11157. (defvar org-reveal-start-hook nil
  11158. "Hook run before revealing a location.")
  11159. (defun org-reveal (&optional siblings)
  11160. "Show current entry, hierarchy above it, and the following headline.
  11161. This can be used to show a consistent set of context around locations
  11162. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11163. not t for the search context.
  11164. With optional argument SIBLINGS, on each level of the hierarchy all
  11165. siblings are shown. This repairs the tree structure to what it would
  11166. look like when opened with hierarchical calls to `org-cycle'.
  11167. With double optional argument \\[universal-argument] \\[universal-argument], \
  11168. go to the parent and show the
  11169. entire tree."
  11170. (interactive "P")
  11171. (run-hooks 'org-reveal-start-hook)
  11172. (let ((org-show-hierarchy-above t)
  11173. (org-show-following-heading t)
  11174. (org-show-siblings (if siblings t org-show-siblings)))
  11175. (org-show-context nil))
  11176. (when (equal siblings '(16))
  11177. (save-excursion
  11178. (when (org-up-heading-safe)
  11179. (org-show-subtree)
  11180. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11181. (defun org-highlight-new-match (beg end)
  11182. "Highlight from BEG to END and mark the highlight is an occur headline."
  11183. (let ((ov (make-overlay beg end)))
  11184. (overlay-put ov 'face 'secondary-selection)
  11185. (overlay-put ov 'org-type 'org-occur)
  11186. (push ov org-occur-highlights)))
  11187. (defun org-remove-occur-highlights (&optional beg end noremove)
  11188. "Remove the occur highlights from the buffer.
  11189. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11190. from the `before-change-functions' in the current buffer."
  11191. (interactive)
  11192. (unless org-inhibit-highlight-removal
  11193. (mapc 'delete-overlay org-occur-highlights)
  11194. (setq org-occur-highlights nil)
  11195. (setq org-occur-parameters nil)
  11196. (unless noremove
  11197. (remove-hook 'before-change-functions
  11198. 'org-remove-occur-highlights 'local))))
  11199. ;;;; Priorities
  11200. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11201. "Regular expression matching the priority indicator.")
  11202. (defvar org-remove-priority-next-time nil)
  11203. (defun org-priority-up ()
  11204. "Increase the priority of the current item."
  11205. (interactive)
  11206. (org-priority 'up))
  11207. (defun org-priority-down ()
  11208. "Decrease the priority of the current item."
  11209. (interactive)
  11210. (org-priority 'down))
  11211. (defun org-priority (&optional action)
  11212. "Change the priority of an item by ARG.
  11213. ACTION can be `set', `up', `down', or a character."
  11214. (interactive)
  11215. (unless org-enable-priority-commands
  11216. (error "Priority commands are disabled"))
  11217. (setq action (or action 'set))
  11218. (let (current new news have remove)
  11219. (save-excursion
  11220. (org-back-to-heading t)
  11221. (if (looking-at org-priority-regexp)
  11222. (setq current (string-to-char (match-string 2))
  11223. have t))
  11224. (cond
  11225. ((eq action 'remove)
  11226. (setq remove t new ?\ ))
  11227. ((or (eq action 'set)
  11228. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11229. (if (not (eq action 'set))
  11230. (setq new action)
  11231. (message "Priority %c-%c, SPC to remove: "
  11232. org-highest-priority org-lowest-priority)
  11233. (save-match-data
  11234. (setq new (read-char-exclusive))))
  11235. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11236. (= (upcase org-lowest-priority) org-lowest-priority))
  11237. (setq new (upcase new)))
  11238. (cond ((equal new ?\ ) (setq remove t))
  11239. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11240. (error "Priority must be between `%c' and `%c'"
  11241. org-highest-priority org-lowest-priority))))
  11242. ((eq action 'up)
  11243. (setq new (if have
  11244. (1- current) ; normal cycling
  11245. ;; last priority was empty
  11246. (if (eq last-command this-command)
  11247. org-lowest-priority ; wrap around empty to lowest
  11248. ;; default
  11249. (if org-priority-start-cycle-with-default
  11250. org-default-priority
  11251. (1- org-default-priority))))))
  11252. ((eq action 'down)
  11253. (setq new (if have
  11254. (1+ current) ; normal cycling
  11255. ;; last priority was empty
  11256. (if (eq last-command this-command)
  11257. org-highest-priority ; wrap around empty to highest
  11258. ;; default
  11259. (if org-priority-start-cycle-with-default
  11260. org-default-priority
  11261. (1+ org-default-priority))))))
  11262. (t (error "Invalid action")))
  11263. (if (or (< (upcase new) org-highest-priority)
  11264. (> (upcase new) org-lowest-priority))
  11265. (if (and (memq action '(up down))
  11266. (not have) (not (eq last-command this-command)))
  11267. ;; `new' is from default priority
  11268. (error
  11269. "The default can not be set, see `org-default-priority' why")
  11270. ;; normal cycling: `new' is beyond highest/lowest priority
  11271. ;; and is wrapped around to the empty priority
  11272. (setq remove t)))
  11273. (setq news (format "%c" new))
  11274. (if have
  11275. (if remove
  11276. (replace-match "" t t nil 1)
  11277. (replace-match news t t nil 2))
  11278. (if remove
  11279. (error "No priority cookie found in line")
  11280. (let ((case-fold-search nil))
  11281. (looking-at org-todo-line-regexp))
  11282. (if (match-end 2)
  11283. (progn
  11284. (goto-char (match-end 2))
  11285. (insert " [#" news "]"))
  11286. (goto-char (match-beginning 3))
  11287. (insert "[#" news "] "))))
  11288. (org-preserve-lc (org-set-tags nil 'align)))
  11289. (if remove
  11290. (message "Priority removed")
  11291. (message "Priority of current item set to %s" news))))
  11292. (defun org-get-priority (s)
  11293. "Find priority cookie and return priority."
  11294. (if (functionp org-get-priority-function)
  11295. (funcall org-get-priority-function)
  11296. (save-match-data
  11297. (if (not (string-match org-priority-regexp s))
  11298. (* 1000 (- org-lowest-priority org-default-priority))
  11299. (* 1000 (- org-lowest-priority
  11300. (string-to-char (match-string 2 s))))))))
  11301. ;;;; Tags
  11302. (defvar org-agenda-archives-mode)
  11303. (defvar org-map-continue-from nil
  11304. "Position from where mapping should continue.
  11305. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11306. (defvar org-scanner-tags nil
  11307. "The current tag list while the tags scanner is running.")
  11308. (defvar org-trust-scanner-tags nil
  11309. "Should `org-get-tags-at' use the tags for the scanner.
  11310. This is for internal dynamical scoping only.
  11311. When this is non-nil, the function `org-get-tags-at' will return the value
  11312. of `org-scanner-tags' instead of building the list by itself. This
  11313. can lead to large speed-ups when the tags scanner is used in a file with
  11314. many entries, and when the list of tags is retrieved, for example to
  11315. obtain a list of properties. Building the tags list for each entry in such
  11316. a file becomes an N^2 operation - but with this variable set, it scales
  11317. as N.")
  11318. (defun org-scan-tags (action matcher &optional todo-only)
  11319. "Scan headline tags with inheritance and produce output ACTION.
  11320. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11321. or `agenda' to produce an entry list for an agenda view. It can also be
  11322. a Lisp form or a function that should be called at each matched headline, in
  11323. this case the return value is a list of all return values from these calls.
  11324. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11325. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11326. only lines with a TODO keyword are included in the output."
  11327. (require 'org-agenda)
  11328. (let* ((re (concat "^" org-outline-regexp " *\\(\\<\\("
  11329. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11330. (org-re
  11331. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11332. (props (list 'face 'default
  11333. 'done-face 'org-agenda-done
  11334. 'undone-face 'default
  11335. 'mouse-face 'highlight
  11336. 'org-not-done-regexp org-not-done-regexp
  11337. 'org-todo-regexp org-todo-regexp
  11338. 'help-echo
  11339. (format "mouse-2 or RET jump to org file %s"
  11340. (abbreviate-file-name
  11341. (or (buffer-file-name (buffer-base-buffer))
  11342. (buffer-name (buffer-base-buffer)))))))
  11343. (case-fold-search nil)
  11344. (org-map-continue-from nil)
  11345. lspos tags tags-list
  11346. (tags-alist (list (cons 0 org-file-tags)))
  11347. (llast 0) rtn rtn1 level category i txt
  11348. todo marker entry priority)
  11349. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11350. (setq action (list 'lambda nil action)))
  11351. (save-excursion
  11352. (goto-char (point-min))
  11353. (when (eq action 'sparse-tree)
  11354. (org-overview)
  11355. (org-remove-occur-highlights))
  11356. (while (re-search-forward re nil t)
  11357. (catch :skip
  11358. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11359. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11360. (goto-char (setq lspos (match-beginning 0)))
  11361. (setq level (org-reduced-level (funcall outline-level))
  11362. category (org-get-category))
  11363. (setq i llast llast level)
  11364. ;; remove tag lists from same and sublevels
  11365. (while (>= i level)
  11366. (when (setq entry (assoc i tags-alist))
  11367. (setq tags-alist (delete entry tags-alist)))
  11368. (setq i (1- i)))
  11369. ;; add the next tags
  11370. (when tags
  11371. (setq tags (org-split-string tags ":")
  11372. tags-alist
  11373. (cons (cons level tags) tags-alist)))
  11374. ;; compile tags for current headline
  11375. (setq tags-list
  11376. (if org-use-tag-inheritance
  11377. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11378. tags)
  11379. org-scanner-tags tags-list)
  11380. (when org-use-tag-inheritance
  11381. (setcdr (car tags-alist)
  11382. (mapcar (lambda (x)
  11383. (setq x (copy-sequence x))
  11384. (org-add-prop-inherited x))
  11385. (cdar tags-alist))))
  11386. (when (and tags org-use-tag-inheritance
  11387. (or (not (eq t org-use-tag-inheritance))
  11388. org-tags-exclude-from-inheritance))
  11389. ;; selective inheritance, remove uninherited ones
  11390. (setcdr (car tags-alist)
  11391. (org-remove-uninherited-tags (cdar tags-alist))))
  11392. (when (and
  11393. ;; eval matcher only when the todo condition is OK
  11394. (and (or (not todo-only) (member todo org-not-done-keywords))
  11395. (let ((case-fold-search t)) (eval matcher)))
  11396. ;; Call the skipper, but return t if it does not skip,
  11397. ;; so that the `and' form continues evaluating
  11398. (progn
  11399. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11400. t)
  11401. ;; Check if timestamps are deselecting this entry
  11402. (or (not todo-only)
  11403. (and (member todo org-not-done-keywords)
  11404. (or (not org-agenda-tags-todo-honor-ignore-options)
  11405. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11406. ;; Extra check for the archive tag
  11407. ;; FIXME: Does the skipper already do this????
  11408. (or
  11409. (not (member org-archive-tag tags-list))
  11410. ;; we have an archive tag, should we use this anyway?
  11411. (or (not org-agenda-skip-archived-trees)
  11412. (and (eq action 'agenda) org-agenda-archives-mode))))
  11413. ;; select this headline
  11414. (cond
  11415. ((eq action 'sparse-tree)
  11416. (and org-highlight-sparse-tree-matches
  11417. (org-get-heading) (match-end 0)
  11418. (org-highlight-new-match
  11419. (match-beginning 0) (match-beginning 1)))
  11420. (org-show-context 'tags-tree))
  11421. ((eq action 'agenda)
  11422. (setq txt (org-format-agenda-item
  11423. ""
  11424. (concat
  11425. (if (eq org-tags-match-list-sublevels 'indented)
  11426. (make-string (1- level) ?.) "")
  11427. (org-get-heading))
  11428. category
  11429. tags-list
  11430. )
  11431. priority (org-get-priority txt))
  11432. (goto-char lspos)
  11433. (setq marker (org-agenda-new-marker))
  11434. (org-add-props txt props
  11435. 'org-marker marker 'org-hd-marker marker 'org-category category
  11436. 'todo-state todo
  11437. 'priority priority 'type "tagsmatch")
  11438. (push txt rtn))
  11439. ((functionp action)
  11440. (setq org-map-continue-from nil)
  11441. (save-excursion
  11442. (setq rtn1 (funcall action))
  11443. (push rtn1 rtn)))
  11444. (t (error "Invalid action")))
  11445. ;; if we are to skip sublevels, jump to end of subtree
  11446. (unless org-tags-match-list-sublevels
  11447. (org-end-of-subtree t)
  11448. (backward-char 1))))
  11449. ;; Get the correct position from where to continue
  11450. (if org-map-continue-from
  11451. (goto-char org-map-continue-from)
  11452. (and (= (point) lspos) (end-of-line 1)))))
  11453. (when (and (eq action 'sparse-tree)
  11454. (not org-sparse-tree-open-archived-trees))
  11455. (org-hide-archived-subtrees (point-min) (point-max)))
  11456. (nreverse rtn)))
  11457. (defun org-remove-uninherited-tags (tags)
  11458. "Remove all tags that are not inherited from the list TAGS."
  11459. (cond
  11460. ((eq org-use-tag-inheritance t)
  11461. (if org-tags-exclude-from-inheritance
  11462. (org-delete-all org-tags-exclude-from-inheritance tags)
  11463. tags))
  11464. ((not org-use-tag-inheritance) nil)
  11465. ((stringp org-use-tag-inheritance)
  11466. (delq nil (mapcar
  11467. (lambda (x)
  11468. (if (and (string-match org-use-tag-inheritance x)
  11469. (not (member x org-tags-exclude-from-inheritance)))
  11470. x nil))
  11471. tags)))
  11472. ((listp org-use-tag-inheritance)
  11473. (delq nil (mapcar
  11474. (lambda (x)
  11475. (if (member x org-use-tag-inheritance) x nil))
  11476. tags)))))
  11477. (defvar todo-only) ;; dynamically scoped
  11478. (defun org-match-sparse-tree (&optional todo-only match)
  11479. "Create a sparse tree according to tags string MATCH.
  11480. MATCH can contain positive and negative selection of tags, like
  11481. \"+WORK+URGENT-WITHBOSS\".
  11482. If optional argument TODO-ONLY is non-nil, only select lines that are
  11483. also TODO lines."
  11484. (interactive "P")
  11485. (org-prepare-agenda-buffers (list (current-buffer)))
  11486. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11487. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11488. (defvar org-cached-props nil)
  11489. (defun org-cached-entry-get (pom property)
  11490. (if (or (eq t org-use-property-inheritance)
  11491. (and (stringp org-use-property-inheritance)
  11492. (string-match org-use-property-inheritance property))
  11493. (and (listp org-use-property-inheritance)
  11494. (member property org-use-property-inheritance)))
  11495. ;; Caching is not possible, check it directly
  11496. (org-entry-get pom property 'inherit)
  11497. ;; Get all properties, so that we can do complicated checks easily
  11498. (cdr (assoc property (or org-cached-props
  11499. (setq org-cached-props
  11500. (org-entry-properties pom)))))))
  11501. (defun org-global-tags-completion-table (&optional files)
  11502. "Return the list of all tags in all agenda buffer/files.
  11503. Optional FILES argument is a list of files to which can be used
  11504. instead of the agenda files."
  11505. (save-excursion
  11506. (org-uniquify
  11507. (delq nil
  11508. (apply 'append
  11509. (mapcar
  11510. (lambda (file)
  11511. (set-buffer (find-file-noselect file))
  11512. (append (org-get-buffer-tags)
  11513. (mapcar (lambda (x) (if (stringp (car-safe x))
  11514. (list (car-safe x)) nil))
  11515. org-tag-alist)))
  11516. (if (and files (car files))
  11517. files
  11518. (org-agenda-files))))))))
  11519. (defun org-make-tags-matcher (match)
  11520. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11521. ;; todo-only is scoped dynamically into this function, and the function
  11522. ;; may change it if the matcher asks for it.
  11523. (unless match
  11524. ;; Get a new match request, with completion
  11525. (let ((org-last-tags-completion-table
  11526. (org-global-tags-completion-table)))
  11527. (setq match (org-completing-read-no-i
  11528. "Match: " 'org-tags-completion-function nil nil nil
  11529. 'org-tags-history))))
  11530. ;; Parse the string and create a lisp form
  11531. (let ((match0 match)
  11532. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11533. minus tag mm
  11534. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11535. orterms term orlist re-p str-p level-p level-op time-p
  11536. prop-p pn pv po gv rest)
  11537. (if (string-match "/+" match)
  11538. ;; match contains also a todo-matching request
  11539. (progn
  11540. (setq tagsmatch (substring match 0 (match-beginning 0))
  11541. todomatch (substring match (match-end 0)))
  11542. (if (string-match "^!" todomatch)
  11543. (setq todo-only t todomatch (substring todomatch 1)))
  11544. (if (string-match "^\\s-*$" todomatch)
  11545. (setq todomatch nil)))
  11546. ;; only matching tags
  11547. (setq tagsmatch match todomatch nil))
  11548. ;; Make the tags matcher
  11549. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11550. (setq tagsmatcher t)
  11551. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11552. (while (setq term (pop orterms))
  11553. (while (and (equal (substring term -1) "\\") orterms)
  11554. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11555. (while (string-match re term)
  11556. (setq rest (substring term (match-end 0))
  11557. minus (and (match-end 1)
  11558. (equal (match-string 1 term) "-"))
  11559. tag (save-match-data (replace-regexp-in-string
  11560. "\\\\-" "-"
  11561. (match-string 2 term)))
  11562. re-p (equal (string-to-char tag) ?{)
  11563. level-p (match-end 4)
  11564. prop-p (match-end 5)
  11565. mm (cond
  11566. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11567. (level-p
  11568. (setq level-op (org-op-to-function (match-string 3 term)))
  11569. `(,level-op level ,(string-to-number
  11570. (match-string 4 term))))
  11571. (prop-p
  11572. (setq pn (match-string 5 term)
  11573. po (match-string 6 term)
  11574. pv (match-string 7 term)
  11575. re-p (equal (string-to-char pv) ?{)
  11576. str-p (equal (string-to-char pv) ?\")
  11577. time-p (save-match-data
  11578. (string-match "^\"[[<].*[]>]\"$" pv))
  11579. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11580. (if time-p (setq pv (org-matcher-time pv)))
  11581. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11582. (cond
  11583. ((equal pn "CATEGORY")
  11584. (setq gv '(get-text-property (point) 'org-category)))
  11585. ((equal pn "TODO")
  11586. (setq gv 'todo))
  11587. (t
  11588. (setq gv `(org-cached-entry-get nil ,pn))))
  11589. (if re-p
  11590. (if (eq po 'org<>)
  11591. `(not (string-match ,pv (or ,gv "")))
  11592. `(string-match ,pv (or ,gv "")))
  11593. (if str-p
  11594. `(,po (or ,gv "") ,pv)
  11595. `(,po (string-to-number (or ,gv ""))
  11596. ,(string-to-number pv) ))))
  11597. (t `(member ,tag tags-list)))
  11598. mm (if minus (list 'not mm) mm)
  11599. term rest)
  11600. (push mm tagsmatcher))
  11601. (push (if (> (length tagsmatcher) 1)
  11602. (cons 'and tagsmatcher)
  11603. (car tagsmatcher))
  11604. orlist)
  11605. (setq tagsmatcher nil))
  11606. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11607. (setq tagsmatcher
  11608. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11609. ;; Make the todo matcher
  11610. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11611. (setq todomatcher t)
  11612. (setq orterms (org-split-string todomatch "|") orlist nil)
  11613. (while (setq term (pop orterms))
  11614. (while (string-match re term)
  11615. (setq minus (and (match-end 1)
  11616. (equal (match-string 1 term) "-"))
  11617. kwd (match-string 2 term)
  11618. re-p (equal (string-to-char kwd) ?{)
  11619. term (substring term (match-end 0))
  11620. mm (if re-p
  11621. `(string-match ,(substring kwd 1 -1) todo)
  11622. (list 'equal 'todo kwd))
  11623. mm (if minus (list 'not mm) mm))
  11624. (push mm todomatcher))
  11625. (push (if (> (length todomatcher) 1)
  11626. (cons 'and todomatcher)
  11627. (car todomatcher))
  11628. orlist)
  11629. (setq todomatcher nil))
  11630. (setq todomatcher (if (> (length orlist) 1)
  11631. (cons 'or orlist) (car orlist))))
  11632. ;; Return the string and lisp forms of the matcher
  11633. (setq matcher (if todomatcher
  11634. (list 'and tagsmatcher todomatcher)
  11635. tagsmatcher))
  11636. (cons match0 matcher)))
  11637. (defun org-op-to-function (op &optional stringp)
  11638. "Turn an operator into the appropriate function."
  11639. (setq op
  11640. (cond
  11641. ((equal op "<" ) '(< string< org-time<))
  11642. ((equal op ">" ) '(> org-string> org-time>))
  11643. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11644. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11645. ((member op '("=" "==")) '(= string= org-time=))
  11646. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11647. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11648. (defun org<> (a b) (not (= a b)))
  11649. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11650. (defun org-string>= (a b) (not (string< a b)))
  11651. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11652. (defun org-string<> (a b) (not (string= a b)))
  11653. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11654. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11655. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11656. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11657. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11658. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11659. (defun org-2ft (s)
  11660. "Convert S to a floating point time.
  11661. If S is already a number, just return it. If it is a string, parse
  11662. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11663. (cond
  11664. ((numberp s) s)
  11665. ((stringp s)
  11666. (condition-case nil
  11667. (float-time (apply 'encode-time (org-parse-time-string s)))
  11668. (error 0.)))
  11669. (t 0.)))
  11670. (defun org-time-today ()
  11671. "Time in seconds today at 0:00.
  11672. Returns the float number of seconds since the beginning of the
  11673. epoch to the beginning of today (00:00)."
  11674. (float-time (apply 'encode-time
  11675. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11676. (defun org-matcher-time (s)
  11677. "Interpret a time comparison value."
  11678. (save-match-data
  11679. (cond
  11680. ((string= s "<now>") (float-time))
  11681. ((string= s "<today>") (org-time-today))
  11682. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11683. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11684. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11685. (+ (org-time-today)
  11686. (* (string-to-number (match-string 1 s))
  11687. (cdr (assoc (match-string 2 s)
  11688. '(("d" . 86400.0) ("w" . 604800.0)
  11689. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11690. (t (org-2ft s)))))
  11691. (defun org-match-any-p (re list)
  11692. "Does re match any element of list?"
  11693. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11694. (delq nil list))
  11695. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11696. (defvar org-tags-overlay (make-overlay 1 1))
  11697. (org-detach-overlay org-tags-overlay)
  11698. (defun org-get-local-tags-at (&optional pos)
  11699. "Get a list of tags defined in the current headline."
  11700. (org-get-tags-at pos 'local))
  11701. (defun org-get-local-tags ()
  11702. "Get a list of tags defined in the current headline."
  11703. (org-get-tags-at nil 'local))
  11704. (defun org-get-tags-at (&optional pos local)
  11705. "Get a list of all headline tags applicable at POS.
  11706. POS defaults to point. If tags are inherited, the list contains
  11707. the targets in the same sequence as the headlines appear, i.e.
  11708. the tags of the current headline come last.
  11709. When LOCAL is non-nil, only return tags from the current headline,
  11710. ignore inherited ones."
  11711. (interactive)
  11712. (if (and org-trust-scanner-tags
  11713. (or (not pos) (equal pos (point)))
  11714. (not local))
  11715. org-scanner-tags
  11716. (let (tags ltags lastpos parent)
  11717. (save-excursion
  11718. (save-restriction
  11719. (widen)
  11720. (goto-char (or pos (point)))
  11721. (save-match-data
  11722. (catch 'done
  11723. (condition-case nil
  11724. (progn
  11725. (org-back-to-heading t)
  11726. (while (not (equal lastpos (point)))
  11727. (setq lastpos (point))
  11728. (when (looking-at
  11729. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11730. (setq ltags (org-split-string
  11731. (org-match-string-no-properties 1) ":"))
  11732. (when parent
  11733. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11734. (setq tags (append
  11735. (if parent
  11736. (org-remove-uninherited-tags ltags)
  11737. ltags)
  11738. tags)))
  11739. (or org-use-tag-inheritance (throw 'done t))
  11740. (if local (throw 'done t))
  11741. (or (org-up-heading-safe) (error nil))
  11742. (setq parent t)))
  11743. (error nil)))))
  11744. (if local
  11745. tags
  11746. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  11747. (defun org-add-prop-inherited (s)
  11748. (add-text-properties 0 (length s) '(inherited t) s)
  11749. s)
  11750. (defun org-toggle-tag (tag &optional onoff)
  11751. "Toggle the tag TAG for the current line.
  11752. If ONOFF is `on' or `off', don't toggle but set to this state."
  11753. (let (res current)
  11754. (save-excursion
  11755. (org-back-to-heading t)
  11756. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11757. (point-at-eol) t)
  11758. (progn
  11759. (setq current (match-string 1))
  11760. (replace-match ""))
  11761. (setq current ""))
  11762. (setq current (nreverse (org-split-string current ":")))
  11763. (cond
  11764. ((eq onoff 'on)
  11765. (setq res t)
  11766. (or (member tag current) (push tag current)))
  11767. ((eq onoff 'off)
  11768. (or (not (member tag current)) (setq current (delete tag current))))
  11769. (t (if (member tag current)
  11770. (setq current (delete tag current))
  11771. (setq res t)
  11772. (push tag current))))
  11773. (end-of-line 1)
  11774. (if current
  11775. (progn
  11776. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11777. (org-set-tags nil t))
  11778. (delete-horizontal-space))
  11779. (run-hooks 'org-after-tags-change-hook))
  11780. res))
  11781. (defun org-align-tags-here (to-col)
  11782. ;; Assumes that this is a headline
  11783. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11784. (beginning-of-line 1)
  11785. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11786. (< pos (match-beginning 2)))
  11787. (progn
  11788. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11789. (goto-char (match-beginning 1))
  11790. (insert " ")
  11791. (delete-region (point) (1+ (match-beginning 2)))
  11792. (setq ncol (max (1+ (current-column))
  11793. (1+ col)
  11794. (if (> to-col 0)
  11795. to-col
  11796. (- (abs to-col) tags-l))))
  11797. (setq p (point))
  11798. (insert (make-string (- ncol (current-column)) ?\ ))
  11799. (setq ncol (current-column))
  11800. (when indent-tabs-mode (tabify p (point-at-eol)))
  11801. (org-move-to-column (min ncol col) t))
  11802. (goto-char pos))))
  11803. (defun org-set-tags-command (&optional arg just-align)
  11804. "Call the set-tags command for the current entry."
  11805. (interactive "P")
  11806. (if (org-on-heading-p)
  11807. (org-set-tags arg just-align)
  11808. (save-excursion
  11809. (org-back-to-heading t)
  11810. (org-set-tags arg just-align))))
  11811. (defun org-set-tags-to (data)
  11812. "Set the tags of the current entry to DATA, replacing the current tags.
  11813. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11814. If DATA is nil or the empty string, any tags will be removed."
  11815. (interactive "sTags: ")
  11816. (setq data
  11817. (cond
  11818. ((eq data nil) "")
  11819. ((equal data "") "")
  11820. ((stringp data)
  11821. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11822. ":"))
  11823. ((listp data)
  11824. (concat ":" (mapconcat 'identity data ":") ":"))
  11825. (t nil)))
  11826. (when data
  11827. (save-excursion
  11828. (org-back-to-heading t)
  11829. (when (looking-at org-complex-heading-regexp)
  11830. (if (match-end 5)
  11831. (progn
  11832. (goto-char (match-beginning 5))
  11833. (insert data)
  11834. (delete-region (point) (point-at-eol))
  11835. (org-set-tags nil 'align))
  11836. (goto-char (point-at-eol))
  11837. (insert " " data)
  11838. (org-set-tags nil 'align)))
  11839. (beginning-of-line 1)
  11840. (if (looking-at ".*?\\([ \t]+\\)$")
  11841. (delete-region (match-beginning 1) (match-end 1))))))
  11842. (defun org-align-all-tags ()
  11843. "Align the tags i all headings."
  11844. (interactive)
  11845. (save-excursion
  11846. (or (ignore-errors (org-back-to-heading t))
  11847. (outline-next-heading))
  11848. (if (org-on-heading-p)
  11849. (org-set-tags t)
  11850. (message "No headings"))))
  11851. (defvar org-indent-indentation-per-level)
  11852. (defun org-set-tags (&optional arg just-align)
  11853. "Set the tags for the current headline.
  11854. With prefix ARG, realign all tags in headings in the current buffer."
  11855. (interactive "P")
  11856. (let* ((re org-outline-regexp-bol)
  11857. (current (org-get-tags-string))
  11858. (col (current-column))
  11859. (org-setting-tags t)
  11860. table current-tags inherited-tags ; computed below when needed
  11861. tags p0 c0 c1 rpl di tc level)
  11862. (if arg
  11863. (save-excursion
  11864. (goto-char (point-min))
  11865. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11866. (while (re-search-forward re nil t)
  11867. (org-set-tags nil t)
  11868. (end-of-line 1)))
  11869. (message "All tags realigned to column %d" org-tags-column))
  11870. (if just-align
  11871. (setq tags current)
  11872. ;; Get a new set of tags from the user
  11873. (save-excursion
  11874. (setq table (append org-tag-persistent-alist
  11875. (or org-tag-alist (org-get-buffer-tags))
  11876. (and
  11877. org-complete-tags-always-offer-all-agenda-tags
  11878. (org-global-tags-completion-table
  11879. (org-agenda-files))))
  11880. org-last-tags-completion-table table
  11881. current-tags (org-split-string current ":")
  11882. inherited-tags (nreverse
  11883. (nthcdr (length current-tags)
  11884. (nreverse (org-get-tags-at))))
  11885. tags
  11886. (if (or (eq t org-use-fast-tag-selection)
  11887. (and org-use-fast-tag-selection
  11888. (delq nil (mapcar 'cdr table))))
  11889. (org-fast-tag-selection
  11890. current-tags inherited-tags table
  11891. (if org-fast-tag-selection-include-todo
  11892. org-todo-key-alist))
  11893. (let ((org-add-colon-after-tag-completion t))
  11894. (org-trim
  11895. (org-icompleting-read "Tags: "
  11896. 'org-tags-completion-function
  11897. nil nil current 'org-tags-history))))))
  11898. (while (string-match "[-+&]+" tags)
  11899. ;; No boolean logic, just a list
  11900. (setq tags (replace-match ":" t t tags))))
  11901. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  11902. (if org-tags-sort-function
  11903. (setq tags (mapconcat 'identity
  11904. (sort (org-split-string
  11905. tags (org-re "[^[:alnum:]_@#%]+"))
  11906. org-tags-sort-function) ":")))
  11907. (if (string-match "\\`[\t ]*\\'" tags)
  11908. (setq tags "")
  11909. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11910. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11911. ;; Insert new tags at the correct column
  11912. (beginning-of-line 1)
  11913. (setq level (or (and (looking-at org-outline-regexp)
  11914. (- (match-end 0) (point) 1))
  11915. 1))
  11916. (cond
  11917. ((and (equal current "") (equal tags "")))
  11918. ((re-search-forward
  11919. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11920. (point-at-eol) t)
  11921. (if (equal tags "")
  11922. (setq rpl "")
  11923. (goto-char (match-beginning 0))
  11924. (setq c0 (current-column)
  11925. ;; compute offset for the case of org-indent-mode active
  11926. di (if org-indent-mode
  11927. (* (1- org-indent-indentation-per-level) (1- level))
  11928. 0)
  11929. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11930. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11931. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11932. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11933. (replace-match rpl t t)
  11934. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11935. tags)
  11936. (t (error "Tags alignment failed")))
  11937. (org-move-to-column col)
  11938. (unless just-align
  11939. (run-hooks 'org-after-tags-change-hook)))))
  11940. (defun org-change-tag-in-region (beg end tag off)
  11941. "Add or remove TAG for each entry in the region.
  11942. This works in the agenda, and also in an org-mode buffer."
  11943. (interactive
  11944. (list (region-beginning) (region-end)
  11945. (let ((org-last-tags-completion-table
  11946. (if (org-mode-p)
  11947. (org-get-buffer-tags)
  11948. (org-global-tags-completion-table))))
  11949. (org-icompleting-read
  11950. "Tag: " 'org-tags-completion-function nil nil nil
  11951. 'org-tags-history))
  11952. (progn
  11953. (message "[s]et or [r]emove? ")
  11954. (equal (read-char-exclusive) ?r))))
  11955. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11956. (let ((agendap (equal major-mode 'org-agenda-mode))
  11957. l1 l2 m buf pos newhead (cnt 0))
  11958. (goto-char end)
  11959. (setq l2 (1- (org-current-line)))
  11960. (goto-char beg)
  11961. (setq l1 (org-current-line))
  11962. (loop for l from l1 to l2 do
  11963. (org-goto-line l)
  11964. (setq m (get-text-property (point) 'org-hd-marker))
  11965. (when (or (and (org-mode-p) (org-on-heading-p))
  11966. (and agendap m))
  11967. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11968. pos (if agendap m (point)))
  11969. (with-current-buffer buf
  11970. (save-excursion
  11971. (save-restriction
  11972. (goto-char pos)
  11973. (setq cnt (1+ cnt))
  11974. (org-toggle-tag tag (if off 'off 'on))
  11975. (setq newhead (org-get-heading)))))
  11976. (and agendap (org-agenda-change-all-lines newhead m))))
  11977. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11978. (defun org-tags-completion-function (string predicate &optional flag)
  11979. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11980. (confirm (lambda (x) (stringp (car x)))))
  11981. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  11982. (setq s1 (match-string 1 string)
  11983. s2 (match-string 2 string))
  11984. (setq s1 "" s2 string))
  11985. (cond
  11986. ((eq flag nil)
  11987. ;; try completion
  11988. (setq rtn (try-completion s2 ctable confirm))
  11989. (if (stringp rtn)
  11990. (setq rtn
  11991. (concat s1 s2 (substring rtn (length s2))
  11992. (if (and org-add-colon-after-tag-completion
  11993. (assoc rtn ctable))
  11994. ":" ""))))
  11995. rtn)
  11996. ((eq flag t)
  11997. ;; all-completions
  11998. (all-completions s2 ctable confirm)
  11999. )
  12000. ((eq flag 'lambda)
  12001. ;; exact match?
  12002. (assoc s2 ctable)))
  12003. ))
  12004. (defun org-fast-tag-insert (kwd tags face &optional end)
  12005. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12006. (insert (format "%-12s" (concat kwd ":"))
  12007. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12008. (or end "")))
  12009. (defun org-fast-tag-show-exit (flag)
  12010. (save-excursion
  12011. (org-goto-line 3)
  12012. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12013. (replace-match ""))
  12014. (when flag
  12015. (end-of-line 1)
  12016. (org-move-to-column (- (window-width) 19) t)
  12017. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12018. (defun org-set-current-tags-overlay (current prefix)
  12019. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12020. (if (featurep 'xemacs)
  12021. (org-overlay-display org-tags-overlay (concat prefix s)
  12022. 'secondary-selection)
  12023. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12024. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12025. (defvar org-last-tag-selection-key nil)
  12026. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12027. "Fast tag selection with single keys.
  12028. CURRENT is the current list of tags in the headline, INHERITED is the
  12029. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12030. possibly with grouping information. TODO-TABLE is a similar table with
  12031. TODO keywords, should these have keys assigned to them.
  12032. If the keys are nil, a-z are automatically assigned.
  12033. Returns the new tags string, or nil to not change the current settings."
  12034. (let* ((fulltable (append table todo-table))
  12035. (maxlen (apply 'max (mapcar
  12036. (lambda (x)
  12037. (if (stringp (car x)) (string-width (car x)) 0))
  12038. fulltable)))
  12039. (buf (current-buffer))
  12040. (expert (eq org-fast-tag-selection-single-key 'expert))
  12041. (buffer-tags nil)
  12042. (fwidth (+ maxlen 3 1 3))
  12043. (ncol (/ (- (window-width) 4) fwidth))
  12044. (i-face 'org-done)
  12045. (c-face 'org-todo)
  12046. tg cnt e c char c1 c2 ntable tbl rtn
  12047. ov-start ov-end ov-prefix
  12048. (exit-after-next org-fast-tag-selection-single-key)
  12049. (done-keywords org-done-keywords)
  12050. groups ingroup)
  12051. (save-excursion
  12052. (beginning-of-line 1)
  12053. (if (looking-at
  12054. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12055. (setq ov-start (match-beginning 1)
  12056. ov-end (match-end 1)
  12057. ov-prefix "")
  12058. (setq ov-start (1- (point-at-eol))
  12059. ov-end (1+ ov-start))
  12060. (skip-chars-forward "^\n\r")
  12061. (setq ov-prefix
  12062. (concat
  12063. (buffer-substring (1- (point)) (point))
  12064. (if (> (current-column) org-tags-column)
  12065. " "
  12066. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12067. (move-overlay org-tags-overlay ov-start ov-end)
  12068. (save-window-excursion
  12069. (if expert
  12070. (set-buffer (get-buffer-create " *Org tags*"))
  12071. (delete-other-windows)
  12072. (split-window-vertically)
  12073. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12074. (erase-buffer)
  12075. (org-set-local 'org-done-keywords done-keywords)
  12076. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12077. (org-fast-tag-insert "Current" current c-face "\n\n")
  12078. (org-fast-tag-show-exit exit-after-next)
  12079. (org-set-current-tags-overlay current ov-prefix)
  12080. (setq tbl fulltable char ?a cnt 0)
  12081. (while (setq e (pop tbl))
  12082. (cond
  12083. ((equal (car e) :startgroup)
  12084. (push '() groups) (setq ingroup t)
  12085. (when (not (= cnt 0))
  12086. (setq cnt 0)
  12087. (insert "\n"))
  12088. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12089. ((equal (car e) :endgroup)
  12090. (setq ingroup nil cnt 0)
  12091. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12092. ((equal e '(:newline))
  12093. (when (not (= cnt 0))
  12094. (setq cnt 0)
  12095. (insert "\n")
  12096. (setq e (car tbl))
  12097. (while (equal (car tbl) '(:newline))
  12098. (insert "\n")
  12099. (setq tbl (cdr tbl)))))
  12100. (t
  12101. (setq tg (copy-sequence (car e)) c2 nil)
  12102. (if (cdr e)
  12103. (setq c (cdr e))
  12104. ;; automatically assign a character.
  12105. (setq c1 (string-to-char
  12106. (downcase (substring
  12107. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12108. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12109. (while (or (rassoc char ntable) (rassoc char table))
  12110. (setq char (1+ char)))
  12111. (setq c2 c1))
  12112. (setq c (or c2 char)))
  12113. (if ingroup (push tg (car groups)))
  12114. (setq tg (org-add-props tg nil 'face
  12115. (cond
  12116. ((not (assoc tg table))
  12117. (org-get-todo-face tg))
  12118. ((member tg current) c-face)
  12119. ((member tg inherited) i-face)
  12120. (t nil))))
  12121. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12122. (insert "[" c "] " tg (make-string
  12123. (- fwidth 4 (length tg)) ?\ ))
  12124. (push (cons tg c) ntable)
  12125. (when (= (setq cnt (1+ cnt)) ncol)
  12126. (insert "\n")
  12127. (if ingroup (insert " "))
  12128. (setq cnt 0)))))
  12129. (setq ntable (nreverse ntable))
  12130. (insert "\n")
  12131. (goto-char (point-min))
  12132. (if (not expert) (org-fit-window-to-buffer))
  12133. (setq rtn
  12134. (catch 'exit
  12135. (while t
  12136. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12137. (if (not groups) "no " "")
  12138. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12139. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12140. (setq org-last-tag-selection-key c)
  12141. (cond
  12142. ((= c ?\r) (throw 'exit t))
  12143. ((= c ?!)
  12144. (setq groups (not groups))
  12145. (goto-char (point-min))
  12146. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12147. ((= c ?\C-c)
  12148. (if (not expert)
  12149. (org-fast-tag-show-exit
  12150. (setq exit-after-next (not exit-after-next)))
  12151. (setq expert nil)
  12152. (delete-other-windows)
  12153. (set-window-buffer (split-window-vertically) " *Org tags*")
  12154. (org-switch-to-buffer-other-window " *Org tags*")
  12155. (org-fit-window-to-buffer)))
  12156. ((or (= c ?\C-g)
  12157. (and (= c ?q) (not (rassoc c ntable))))
  12158. (org-detach-overlay org-tags-overlay)
  12159. (setq quit-flag t))
  12160. ((= c ?\ )
  12161. (setq current nil)
  12162. (if exit-after-next (setq exit-after-next 'now)))
  12163. ((= c ?\t)
  12164. (condition-case nil
  12165. (setq tg (org-icompleting-read
  12166. "Tag: "
  12167. (or buffer-tags
  12168. (with-current-buffer buf
  12169. (org-get-buffer-tags)))))
  12170. (quit (setq tg "")))
  12171. (when (string-match "\\S-" tg)
  12172. (add-to-list 'buffer-tags (list tg))
  12173. (if (member tg current)
  12174. (setq current (delete tg current))
  12175. (push tg current)))
  12176. (if exit-after-next (setq exit-after-next 'now)))
  12177. ((setq e (rassoc c todo-table) tg (car e))
  12178. (with-current-buffer buf
  12179. (save-excursion (org-todo tg)))
  12180. (if exit-after-next (setq exit-after-next 'now)))
  12181. ((setq e (rassoc c ntable) tg (car e))
  12182. (if (member tg current)
  12183. (setq current (delete tg current))
  12184. (loop for g in groups do
  12185. (if (member tg g)
  12186. (mapc (lambda (x)
  12187. (setq current (delete x current)))
  12188. g)))
  12189. (push tg current))
  12190. (if exit-after-next (setq exit-after-next 'now))))
  12191. ;; Create a sorted list
  12192. (setq current
  12193. (sort current
  12194. (lambda (a b)
  12195. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12196. (if (eq exit-after-next 'now) (throw 'exit t))
  12197. (goto-char (point-min))
  12198. (beginning-of-line 2)
  12199. (delete-region (point) (point-at-eol))
  12200. (org-fast-tag-insert "Current" current c-face)
  12201. (org-set-current-tags-overlay current ov-prefix)
  12202. (while (re-search-forward
  12203. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12204. (setq tg (match-string 1))
  12205. (add-text-properties
  12206. (match-beginning 1) (match-end 1)
  12207. (list 'face
  12208. (cond
  12209. ((member tg current) c-face)
  12210. ((member tg inherited) i-face)
  12211. (t (get-text-property (match-beginning 1) 'face))))))
  12212. (goto-char (point-min)))))
  12213. (org-detach-overlay org-tags-overlay)
  12214. (if rtn
  12215. (mapconcat 'identity current ":")
  12216. nil))))
  12217. (defun org-get-tags-string ()
  12218. "Get the TAGS string in the current headline."
  12219. (unless (org-on-heading-p t)
  12220. (error "Not on a heading"))
  12221. (save-excursion
  12222. (beginning-of-line 1)
  12223. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12224. (org-match-string-no-properties 1)
  12225. "")))
  12226. (defun org-get-tags ()
  12227. "Get the list of tags specified in the current headline."
  12228. (org-split-string (org-get-tags-string) ":"))
  12229. (defun org-get-buffer-tags ()
  12230. "Get a table of all tags used in the buffer, for completion."
  12231. (let (tags)
  12232. (save-excursion
  12233. (goto-char (point-min))
  12234. (while (re-search-forward
  12235. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12236. (when (equal (char-after (point-at-bol 0)) ?*)
  12237. (mapc (lambda (x) (add-to-list 'tags x))
  12238. (org-split-string (org-match-string-no-properties 1) ":")))))
  12239. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12240. (mapcar 'list tags)))
  12241. ;;;; The mapping API
  12242. ;;;###autoload
  12243. (defun org-map-entries (func &optional match scope &rest skip)
  12244. "Call FUNC at each headline selected by MATCH in SCOPE.
  12245. FUNC is a function or a lisp form. The function will be called without
  12246. arguments, with the cursor positioned at the beginning of the headline.
  12247. The return values of all calls to the function will be collected and
  12248. returned as a list.
  12249. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12250. does not need to preserve point. After evaluation, the cursor will be
  12251. moved to the end of the line (presumably of the headline of the
  12252. processed entry) and search continues from there. Under some
  12253. circumstances, this may not produce the wanted results. For example,
  12254. if you have removed (e.g. archived) the current (sub)tree it could
  12255. mean that the next entry will be skipped entirely. In such cases, you
  12256. can specify the position from where search should continue by making
  12257. FUNC set the variable `org-map-continue-from' to the desired buffer
  12258. position.
  12259. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12260. Only headlines that are matched by this query will be considered during
  12261. the iteration. When MATCH is nil or t, all headlines will be
  12262. visited by the iteration.
  12263. SCOPE determines the scope of this command. It can be any of:
  12264. nil The current buffer, respecting the restriction if any
  12265. tree The subtree started with the entry at point
  12266. file The current buffer, without restriction
  12267. file-with-archives
  12268. The current buffer, and any archives associated with it
  12269. agenda All agenda files
  12270. agenda-with-archives
  12271. All agenda files with any archive files associated with them
  12272. \(file1 file2 ...)
  12273. If this is a list, all files in the list will be scanned
  12274. The remaining args are treated as settings for the skipping facilities of
  12275. the scanner. The following items can be given here:
  12276. archive skip trees with the archive tag.
  12277. comment skip trees with the COMMENT keyword
  12278. function or Emacs Lisp form:
  12279. will be used as value for `org-agenda-skip-function', so whenever
  12280. the function returns t, FUNC will not be called for that
  12281. entry and search will continue from the point where the
  12282. function leaves it.
  12283. If your function needs to retrieve the tags including inherited tags
  12284. at the *current* entry, you can use the value of the variable
  12285. `org-scanner-tags' which will be much faster than getting the value
  12286. with `org-get-tags-at'. If your function gets properties with
  12287. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12288. to t around the call to `org-entry-properties' to get the same speedup.
  12289. Note that if your function moves around to retrieve tags and properties at
  12290. a *different* entry, you cannot use these techniques."
  12291. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12292. (org-agenda-skip-archived-trees (memq 'archive skip))
  12293. (org-agenda-skip-comment-trees (memq 'comment skip))
  12294. (org-agenda-skip-function
  12295. (car (org-delete-all '(comment archive) skip)))
  12296. (org-tags-match-list-sublevels t)
  12297. matcher file res
  12298. org-todo-keywords-for-agenda
  12299. org-done-keywords-for-agenda
  12300. org-todo-keyword-alist-for-agenda
  12301. org-drawers-for-agenda
  12302. org-tag-alist-for-agenda)
  12303. (cond
  12304. ((eq match t) (setq matcher t))
  12305. ((eq match nil) (setq matcher t))
  12306. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12307. (save-excursion
  12308. (save-restriction
  12309. (when (eq scope 'tree)
  12310. (org-back-to-heading t)
  12311. (org-narrow-to-subtree)
  12312. (setq scope nil))
  12313. (if (not scope)
  12314. (progn
  12315. (org-prepare-agenda-buffers
  12316. (list (buffer-file-name (current-buffer))))
  12317. (setq res (org-scan-tags func matcher)))
  12318. ;; Get the right scope
  12319. (cond
  12320. ((and scope (listp scope) (symbolp (car scope)))
  12321. (setq scope (eval scope)))
  12322. ((eq scope 'agenda)
  12323. (setq scope (org-agenda-files t)))
  12324. ((eq scope 'agenda-with-archives)
  12325. (setq scope (org-agenda-files t))
  12326. (setq scope (org-add-archive-files scope)))
  12327. ((eq scope 'file)
  12328. (setq scope (list (buffer-file-name))))
  12329. ((eq scope 'file-with-archives)
  12330. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12331. (org-prepare-agenda-buffers scope)
  12332. (while (setq file (pop scope))
  12333. (with-current-buffer (org-find-base-buffer-visiting file)
  12334. (save-excursion
  12335. (save-restriction
  12336. (widen)
  12337. (goto-char (point-min))
  12338. (setq res (append res (org-scan-tags func matcher))))))))))
  12339. res))
  12340. ;;;; Properties
  12341. ;;; Setting and retrieving properties
  12342. (defconst org-special-properties
  12343. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12344. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12345. "The special properties valid in Org-mode.
  12346. These are properties that are not defined in the property drawer,
  12347. but in some other way.")
  12348. (defconst org-default-properties
  12349. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12350. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12351. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12352. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12353. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12354. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12355. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12356. "Some properties that are used by Org-mode for various purposes.
  12357. Being in this list makes sure that they are offered for completion.")
  12358. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12359. "Regular expression matching the first line of a property drawer.")
  12360. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12361. "Regular expression matching the last line of a property drawer.")
  12362. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12363. "Regular expression matching the first line of a property drawer.")
  12364. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12365. "Regular expression matching the first line of a property drawer.")
  12366. (defconst org-property-drawer-re
  12367. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12368. org-property-end-re "\\)\n?")
  12369. "Matches an entire property drawer.")
  12370. (defconst org-clock-drawer-re
  12371. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12372. org-property-end-re "\\)\n?")
  12373. "Matches an entire clock drawer.")
  12374. (defsubst org-re-property (property)
  12375. "Return a regexp matching PROPERTY.
  12376. Match group 1 will be set to the value "
  12377. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12378. (defun org-property-action ()
  12379. "Do an action on properties."
  12380. (interactive)
  12381. (let (c)
  12382. (org-at-property-p)
  12383. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12384. (setq c (read-char-exclusive))
  12385. (cond
  12386. ((equal c ?s)
  12387. (call-interactively 'org-set-property))
  12388. ((equal c ?d)
  12389. (call-interactively 'org-delete-property))
  12390. ((equal c ?D)
  12391. (call-interactively 'org-delete-property-globally))
  12392. ((equal c ?c)
  12393. (call-interactively 'org-compute-property-at-point))
  12394. (t (error "No such property action %c" c)))))
  12395. (defun org-set-effort (&optional value)
  12396. "Set the effort property of the current entry.
  12397. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12398. allowed value."
  12399. (interactive "P")
  12400. (if (equal value 0) (setq value 10))
  12401. (let* ((completion-ignore-case t)
  12402. (prop org-effort-property)
  12403. (cur (org-entry-get nil prop))
  12404. (allowed (org-property-get-allowed-values nil prop 'table))
  12405. (existing (mapcar 'list (org-property-values prop)))
  12406. rpl
  12407. (val (cond
  12408. ((stringp value) value)
  12409. ((and allowed (integerp value))
  12410. (or (car (nth (1- value) allowed))
  12411. (car (org-last allowed))))
  12412. (allowed
  12413. (message "Select 1-9,0, [RET%s]: %s"
  12414. (if cur (concat "=" cur) "")
  12415. (mapconcat 'car allowed " "))
  12416. (setq rpl (read-char-exclusive))
  12417. (if (equal rpl ?\r)
  12418. cur
  12419. (setq rpl (- rpl ?0))
  12420. (if (equal rpl 0) (setq rpl 10))
  12421. (if (and (> rpl 0) (<= rpl (length allowed)))
  12422. (car (nth (1- rpl) allowed))
  12423. (org-completing-read "Effort: " allowed nil))))
  12424. (t
  12425. (let (org-completion-use-ido org-completion-use-iswitchb)
  12426. (org-completing-read
  12427. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12428. (concat "[" cur "]") "")
  12429. ": ")
  12430. existing nil nil "" nil cur))))))
  12431. (unless (equal (org-entry-get nil prop) val)
  12432. (org-entry-put nil prop val))
  12433. (message "%s is now %s" prop val)))
  12434. (defun org-at-property-p ()
  12435. "Is cursor inside a property drawer?"
  12436. (save-excursion
  12437. (beginning-of-line 1)
  12438. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12439. (save-match-data ;; Used by calling procedures
  12440. (let ((p (point))
  12441. (range (unless (org-before-first-heading-p)
  12442. (org-get-property-block))))
  12443. (and range (<= (car range) p) (< p (cdr range))))))))
  12444. (defun org-get-property-block (&optional beg end force)
  12445. "Return the (beg . end) range of the body of the property drawer.
  12446. BEG and END can be beginning and end of subtree, if not given
  12447. they will be found.
  12448. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12449. (catch 'exit
  12450. (save-excursion
  12451. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12452. (end (or end (progn (outline-next-heading) (point)))))
  12453. (goto-char beg)
  12454. (if (re-search-forward org-property-start-re end t)
  12455. (setq beg (1+ (match-end 0)))
  12456. (if force
  12457. (save-excursion
  12458. (org-insert-property-drawer)
  12459. (setq end (progn (outline-next-heading) (point))))
  12460. (throw 'exit nil))
  12461. (goto-char beg)
  12462. (if (re-search-forward org-property-start-re end t)
  12463. (setq beg (1+ (match-end 0)))))
  12464. (if (re-search-forward org-property-end-re end t)
  12465. (setq end (match-beginning 0))
  12466. (or force (throw 'exit nil))
  12467. (goto-char beg)
  12468. (setq end beg)
  12469. (org-indent-line-function)
  12470. (insert ":END:\n"))
  12471. (cons beg end)))))
  12472. (defun org-entry-properties (&optional pom which specific)
  12473. "Get all properties of the entry at point-or-marker POM.
  12474. This includes the TODO keyword, the tags, time strings for deadline,
  12475. scheduled, and clocking, and any additional properties defined in the
  12476. entry. The return value is an alist, keys may occur multiple times
  12477. if the property key was used several times.
  12478. POM may also be nil, in which case the current entry is used.
  12479. If WHICH is nil or `all', get all properties. If WHICH is
  12480. `special' or `standard', only get that subclass. If WHICH
  12481. is a string only get exactly this property. SPECIFIC can be a string, the
  12482. specific property we are interested in. Specifying it can speed
  12483. things up because then unnecessary parsing is avoided."
  12484. (setq which (or which 'all))
  12485. (org-with-point-at pom
  12486. (let ((clockstr (substring org-clock-string 0 -1))
  12487. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12488. (case-fold-search nil)
  12489. beg end range props sum-props key key1 value string clocksum)
  12490. (save-excursion
  12491. (when (condition-case nil
  12492. (and (org-mode-p) (org-back-to-heading t))
  12493. (error nil))
  12494. (setq beg (point))
  12495. (setq sum-props (get-text-property (point) 'org-summaries))
  12496. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12497. (outline-next-heading)
  12498. (setq end (point))
  12499. (when (memq which '(all special))
  12500. ;; Get the special properties, like TODO and tags
  12501. (goto-char beg)
  12502. (when (and (or (not specific) (string= specific "TODO"))
  12503. (looking-at org-todo-line-regexp) (match-end 2))
  12504. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12505. (when (and (or (not specific) (string= specific "PRIORITY"))
  12506. (looking-at org-priority-regexp))
  12507. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12508. (when (or (not specific) (string= specific "FILE"))
  12509. (push (cons "FILE" buffer-file-name) props))
  12510. (when (and (or (not specific) (string= specific "TAGS"))
  12511. (setq value (org-get-tags-string))
  12512. (string-match "\\S-" value))
  12513. (push (cons "TAGS" value) props))
  12514. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12515. (setq value (org-get-tags-at)))
  12516. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12517. ":"))
  12518. props))
  12519. (when (or (not specific) (string= specific "BLOCKED"))
  12520. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12521. (when (or (not specific)
  12522. (member specific
  12523. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12524. "TIMESTAMP" "TIMESTAMP_IA")))
  12525. (catch 'match
  12526. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12527. (setq key (if (match-end 1)
  12528. (substring (org-match-string-no-properties 1)
  12529. 0 -1))
  12530. string (if (equal key clockstr)
  12531. (org-no-properties
  12532. (org-trim
  12533. (buffer-substring
  12534. (match-beginning 3) (goto-char
  12535. (point-at-eol)))))
  12536. (substring (org-match-string-no-properties 3)
  12537. 1 -1)))
  12538. ;; Get the correct property name from the key. This is
  12539. ;; necessary if the user has configured time keywords.
  12540. (setq key1 (concat key ":"))
  12541. (cond
  12542. ((not key)
  12543. (setq key
  12544. (if (= (char-after (match-beginning 3)) ?\[)
  12545. "TIMESTAMP_IA" "TIMESTAMP")))
  12546. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12547. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12548. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12549. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12550. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12551. (progn
  12552. (push (cons key string) props)
  12553. ;; no need to search further if match is found
  12554. (throw 'match t))
  12555. (when (or (equal key "CLOCK") (not (assoc key props)))
  12556. (push (cons key string) props))))))
  12557. )
  12558. (when (memq which '(all standard))
  12559. ;; Get the standard properties, like :PROP: ...
  12560. (setq range (org-get-property-block beg end))
  12561. (when range
  12562. (goto-char (car range))
  12563. (while (re-search-forward
  12564. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12565. (cdr range) t)
  12566. (setq key (org-match-string-no-properties 1)
  12567. value (org-trim (or (org-match-string-no-properties 2) "")))
  12568. (unless (member key excluded)
  12569. (push (cons key (or value "")) props)))))
  12570. (if clocksum
  12571. (push (cons "CLOCKSUM"
  12572. (org-columns-number-to-string (/ (float clocksum) 60.)
  12573. 'add_times))
  12574. props))
  12575. (unless (assoc "CATEGORY" props)
  12576. (push (cons "CATEGORY" (org-get-category)) props))
  12577. (append sum-props (nreverse props)))))))
  12578. (defun org-entry-get (pom property &optional inherit literal-nil)
  12579. "Get value of PROPERTY for entry at point-or-marker POM.
  12580. If INHERIT is non-nil and the entry does not have the property,
  12581. then also check higher levels of the hierarchy.
  12582. If INHERIT is the symbol `selective', use inheritance only if the setting
  12583. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12584. If the property is present but empty, the return value is the empty string.
  12585. If the property is not present at all, nil is returned.
  12586. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12587. do not interpret it as the list atom nil. This is used for inheritance
  12588. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12589. (org-with-point-at pom
  12590. (if (and inherit (if (eq inherit 'selective)
  12591. (org-property-inherit-p property)
  12592. t))
  12593. (org-entry-get-with-inheritance property literal-nil)
  12594. (if (member property org-special-properties)
  12595. ;; We need a special property. Use `org-entry-properties' to
  12596. ;; retrieve it, but specify the wanted property
  12597. (cdr (assoc property (org-entry-properties nil 'special property)))
  12598. (let ((range (unless (org-before-first-heading-p)
  12599. (org-get-property-block))))
  12600. (if (and range
  12601. (goto-char (car range))
  12602. (re-search-forward
  12603. (org-re-property property)
  12604. (cdr range) t))
  12605. ;; Found the property, return it.
  12606. (if (match-end 1)
  12607. (if literal-nil
  12608. (org-match-string-no-properties 1)
  12609. (org-not-nil (org-match-string-no-properties 1)))
  12610. "")))))))
  12611. (defun org-property-or-variable-value (var &optional inherit)
  12612. "Check if there is a property fixing the value of VAR.
  12613. If yes, return this value. If not, return the current value of the variable."
  12614. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12615. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12616. (read prop)
  12617. (symbol-value var))))
  12618. (defun org-entry-delete (pom property)
  12619. "Delete the property PROPERTY from entry at point-or-marker POM."
  12620. (org-with-point-at pom
  12621. (if (member property org-special-properties)
  12622. nil ; cannot delete these properties.
  12623. (let ((range (org-get-property-block)))
  12624. (if (and range
  12625. (goto-char (car range))
  12626. (re-search-forward
  12627. (org-re-property property)
  12628. (cdr range) t))
  12629. (progn
  12630. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12631. t)
  12632. nil)))))
  12633. ;; Multi-values properties are properties that contain multiple values
  12634. ;; These values are assumed to be single words, separated by whitespace.
  12635. (defun org-entry-add-to-multivalued-property (pom property value)
  12636. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12637. (let* ((old (org-entry-get pom property))
  12638. (values (and old (org-split-string old "[ \t]"))))
  12639. (setq value (org-entry-protect-space value))
  12640. (unless (member value values)
  12641. (setq values (cons value values))
  12642. (org-entry-put pom property
  12643. (mapconcat 'identity values " ")))))
  12644. (defun org-entry-remove-from-multivalued-property (pom property value)
  12645. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12646. (let* ((old (org-entry-get pom property))
  12647. (values (and old (org-split-string old "[ \t]"))))
  12648. (setq value (org-entry-protect-space value))
  12649. (when (member value values)
  12650. (setq values (delete value values))
  12651. (org-entry-put pom property
  12652. (mapconcat 'identity values " ")))))
  12653. (defun org-entry-member-in-multivalued-property (pom property value)
  12654. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12655. (let* ((old (org-entry-get pom property))
  12656. (values (and old (org-split-string old "[ \t]"))))
  12657. (setq value (org-entry-protect-space value))
  12658. (member value values)))
  12659. (defun org-entry-get-multivalued-property (pom property)
  12660. "Return a list of values in a multivalued property."
  12661. (let* ((value (org-entry-get pom property))
  12662. (values (and value (org-split-string value "[ \t]"))))
  12663. (mapcar 'org-entry-restore-space values)))
  12664. (defun org-entry-put-multivalued-property (pom property &rest values)
  12665. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12666. VALUES should be a list of strings. Spaces will be protected."
  12667. (org-entry-put pom property
  12668. (mapconcat 'org-entry-protect-space values " "))
  12669. (let* ((value (org-entry-get pom property))
  12670. (values (and value (org-split-string value "[ \t]"))))
  12671. (mapcar 'org-entry-restore-space values)))
  12672. (defun org-entry-protect-space (s)
  12673. "Protect spaces and newline in string S."
  12674. (while (string-match " " s)
  12675. (setq s (replace-match "%20" t t s)))
  12676. (while (string-match "\n" s)
  12677. (setq s (replace-match "%0A" t t s)))
  12678. s)
  12679. (defun org-entry-restore-space (s)
  12680. "Restore spaces and newline in string S."
  12681. (while (string-match "%20" s)
  12682. (setq s (replace-match " " t t s)))
  12683. (while (string-match "%0A" s)
  12684. (setq s (replace-match "\n" t t s)))
  12685. s)
  12686. (defvar org-entry-property-inherited-from (make-marker)
  12687. "Marker pointing to the entry from where a property was inherited.
  12688. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12689. location of the entry where the inheritance search matched. If there was
  12690. no match, the marker will point nowhere.
  12691. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12692. is set.")
  12693. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12694. "Get entry property, and search higher levels if not present.
  12695. The search will stop at the first ancestor which has the property defined.
  12696. If the value found is \"nil\", return nil to show that the property
  12697. should be considered as undefined (this is the meaning of nil here).
  12698. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12699. (move-marker org-entry-property-inherited-from nil)
  12700. (let (tmp)
  12701. (unless (org-before-first-heading-p)
  12702. (save-excursion
  12703. (save-restriction
  12704. (widen)
  12705. (catch 'ex
  12706. (while t
  12707. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12708. (org-back-to-heading t)
  12709. (move-marker org-entry-property-inherited-from (point))
  12710. (throw 'ex tmp))
  12711. (or (org-up-heading-safe) (throw 'ex nil)))))))
  12712. (setq tmp (or tmp
  12713. (cdr (assoc property org-file-properties))
  12714. (cdr (assoc property org-global-properties))
  12715. (cdr (assoc property org-global-properties-fixed))))
  12716. (if literal-nil tmp (org-not-nil tmp))))
  12717. (defvar org-property-changed-functions nil
  12718. "Hook called when the value of a property has changed.
  12719. Each hook function should accept two arguments, the name of the property
  12720. and the new value.")
  12721. (defun org-entry-put (pom property value)
  12722. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12723. (org-with-point-at pom
  12724. (org-back-to-heading t)
  12725. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12726. range)
  12727. (cond
  12728. ((equal property "TODO")
  12729. (when (and (stringp value) (string-match "\\S-" value)
  12730. (not (member value org-todo-keywords-1)))
  12731. (error "\"%s\" is not a valid TODO state" value))
  12732. (if (or (not value)
  12733. (not (string-match "\\S-" value)))
  12734. (setq value 'none))
  12735. (org-todo value)
  12736. (org-set-tags nil 'align))
  12737. ((equal property "PRIORITY")
  12738. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12739. (string-to-char value) ?\ ))
  12740. (org-set-tags nil 'align))
  12741. ((equal property "SCHEDULED")
  12742. (if (re-search-forward org-scheduled-time-regexp end t)
  12743. (cond
  12744. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12745. ((eq value 'later) (org-timestamp-change 1 'day))
  12746. (t (call-interactively 'org-schedule)))
  12747. (call-interactively 'org-schedule)))
  12748. ((equal property "DEADLINE")
  12749. (if (re-search-forward org-deadline-time-regexp end t)
  12750. (cond
  12751. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12752. ((eq value 'later) (org-timestamp-change 1 'day))
  12753. (t (call-interactively 'org-deadline)))
  12754. (call-interactively 'org-deadline)))
  12755. ((member property org-special-properties)
  12756. (error "The %s property can not yet be set with `org-entry-put'"
  12757. property))
  12758. (t ; a non-special property
  12759. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12760. (setq range (org-get-property-block beg end 'force))
  12761. (goto-char (car range))
  12762. (if (re-search-forward
  12763. (org-re-property property) (cdr range) t)
  12764. (progn
  12765. (delete-region (match-beginning 0) (match-end 0))
  12766. (goto-char (match-beginning 0)))
  12767. (goto-char (cdr range))
  12768. (insert "\n")
  12769. (backward-char 1)
  12770. (org-indent-line-function))
  12771. (insert ":" property ":")
  12772. (and value (insert " " value))
  12773. (org-indent-line-function)))))
  12774. (run-hook-with-args 'org-property-changed-functions property value)))
  12775. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12776. "Get all property keys in the current buffer.
  12777. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12778. like tags and TODO state.
  12779. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12780. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  12781. and others.
  12782. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12783. formats in the current buffer."
  12784. (let (rtn range cfmt s p)
  12785. (save-excursion
  12786. (save-restriction
  12787. (widen)
  12788. (goto-char (point-min))
  12789. (while (re-search-forward org-property-start-re nil t)
  12790. (setq range (org-get-property-block))
  12791. (goto-char (car range))
  12792. (while (re-search-forward
  12793. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12794. (cdr range) t)
  12795. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12796. (outline-next-heading))))
  12797. (when include-specials
  12798. (setq rtn (append org-special-properties rtn)))
  12799. (when include-defaults
  12800. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12801. (add-to-list 'rtn org-effort-property))
  12802. (when include-columns
  12803. (save-excursion
  12804. (save-restriction
  12805. (widen)
  12806. (goto-char (point-min))
  12807. (while (re-search-forward
  12808. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12809. nil t)
  12810. (setq cfmt (match-string 2) s 0)
  12811. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12812. cfmt s)
  12813. (setq s (match-end 0)
  12814. p (match-string 1 cfmt))
  12815. (unless (or (equal p "ITEM")
  12816. (member p org-special-properties))
  12817. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12818. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12819. (defun org-property-values (key)
  12820. "Return a list of all values of property KEY in the current buffer."
  12821. (save-excursion
  12822. (save-restriction
  12823. (widen)
  12824. (goto-char (point-min))
  12825. (let ((re (org-re-property key))
  12826. values)
  12827. (while (re-search-forward re nil t)
  12828. (add-to-list 'values (org-trim (match-string 1))))
  12829. (delete "" values)))))
  12830. (defun org-insert-property-drawer ()
  12831. "Insert a property drawer into the current entry."
  12832. (interactive)
  12833. (org-back-to-heading t)
  12834. (looking-at org-outline-regexp)
  12835. (let ((indent (if org-adapt-indentation
  12836. (- (match-end 0)(match-beginning 0))
  12837. 0))
  12838. (beg (point))
  12839. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12840. end hiddenp)
  12841. (outline-next-heading)
  12842. (setq end (point))
  12843. (goto-char beg)
  12844. (while (re-search-forward re end t))
  12845. (setq hiddenp (outline-invisible-p))
  12846. (end-of-line 1)
  12847. (and (equal (char-after) ?\n) (forward-char 1))
  12848. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12849. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12850. ;; just skip this line
  12851. (beginning-of-line 2)
  12852. ;; Drawer start, find the end
  12853. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12854. (beginning-of-line 1)))
  12855. (org-skip-over-state-notes)
  12856. (skip-chars-backward " \t\n\r")
  12857. (if (eq (char-before) ?*) (forward-char 1))
  12858. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12859. (beginning-of-line 0)
  12860. (org-indent-to-column indent)
  12861. (beginning-of-line 2)
  12862. (org-indent-to-column indent)
  12863. (beginning-of-line 0)
  12864. (if hiddenp
  12865. (save-excursion
  12866. (org-back-to-heading t)
  12867. (hide-entry))
  12868. (org-flag-drawer t))))
  12869. (defvar org-property-set-functions-alist nil
  12870. "Property set function alist.
  12871. Each entry should have the following format:
  12872. (PROPERTY . READ-FUNCTION)
  12873. The read function will be called with the same argument as
  12874. `org-completing-read'.")
  12875. (defun org-set-property-function (property)
  12876. "Get the function that should be used to set PROPERTY.
  12877. This is computed according to `org-property-set-functions-alist'."
  12878. (or (cdr (assoc property org-property-set-functions-alist))
  12879. 'org-completing-read))
  12880. (defun org-read-property-value (property)
  12881. "Read PROPERTY value from user."
  12882. (let* ((completion-ignore-case t)
  12883. (allowed (org-property-get-allowed-values nil property 'table))
  12884. (cur (org-entry-get nil property))
  12885. (prompt (concat property " value"
  12886. (if (and cur (string-match "\\S-" cur))
  12887. (concat " [" cur "]") "") ": "))
  12888. (set-function (org-set-property-function property))
  12889. (val (if allowed
  12890. (funcall set-function prompt allowed nil
  12891. (not (get-text-property 0 'org-unrestricted
  12892. (caar allowed))))
  12893. (let (org-completion-use-ido org-completion-use-iswitchb)
  12894. (funcall set-function prompt
  12895. (mapcar 'list (org-property-values property))
  12896. nil nil "" nil cur)))))
  12897. (if (equal val "")
  12898. cur
  12899. val)))
  12900. (defun org-read-property-name ()
  12901. "Read a property name."
  12902. (let* ((completion-ignore-case t)
  12903. (keys (org-buffer-property-keys nil t t))
  12904. (default-prop (or (save-excursion
  12905. (save-match-data
  12906. (beginning-of-line)
  12907. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  12908. (null (string= (match-string 1) "END"))
  12909. (match-string 1))))
  12910. org-last-set-property))
  12911. (property (org-icompleting-read
  12912. (concat "Property"
  12913. (if default-prop (concat " [" default-prop "]") "")
  12914. ": ")
  12915. (mapcar 'list keys)
  12916. nil nil nil nil
  12917. default-prop
  12918. )))
  12919. (if (member property keys)
  12920. property
  12921. (or (cdr (assoc (downcase property)
  12922. (mapcar (lambda (x) (cons (downcase x) x))
  12923. keys)))
  12924. property))))
  12925. (defvar org-last-set-property nil)
  12926. (defun org-set-property (property value)
  12927. "In the current entry, set PROPERTY to VALUE.
  12928. When called interactively, this will prompt for a property name, offering
  12929. completion on existing and default properties. And then it will prompt
  12930. for a value, offering completion either on allowed values (via an inherited
  12931. xxx_ALL property) or on existing values in other instances of this property
  12932. in the current file."
  12933. (interactive (list nil nil))
  12934. (let* ((property (or property (org-read-property-name)))
  12935. (value (or value (org-read-property-value property))))
  12936. (setq org-last-set-property property)
  12937. (unless (equal (org-entry-get nil property) value)
  12938. (org-entry-put nil property value))))
  12939. (defun org-delete-property (property)
  12940. "In the current entry, delete PROPERTY."
  12941. (interactive
  12942. (let* ((completion-ignore-case t)
  12943. (prop (org-icompleting-read "Property: "
  12944. (org-entry-properties nil 'standard))))
  12945. (list prop)))
  12946. (message "Property %s %s" property
  12947. (if (org-entry-delete nil property)
  12948. "deleted"
  12949. "was not present in the entry")))
  12950. (defun org-delete-property-globally (property)
  12951. "Remove PROPERTY globally, from all entries."
  12952. (interactive
  12953. (let* ((completion-ignore-case t)
  12954. (prop (org-icompleting-read
  12955. "Globally remove property: "
  12956. (mapcar 'list (org-buffer-property-keys)))))
  12957. (list prop)))
  12958. (save-excursion
  12959. (save-restriction
  12960. (widen)
  12961. (goto-char (point-min))
  12962. (let ((cnt 0))
  12963. (while (re-search-forward
  12964. (org-re-property property)
  12965. nil t)
  12966. (setq cnt (1+ cnt))
  12967. (replace-match ""))
  12968. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12969. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12970. (defun org-compute-property-at-point ()
  12971. "Compute the property at point.
  12972. This looks for an enclosing column format, extracts the operator and
  12973. then applies it to the property in the column format's scope."
  12974. (interactive)
  12975. (unless (org-at-property-p)
  12976. (error "Not at a property"))
  12977. (let ((prop (org-match-string-no-properties 2)))
  12978. (org-columns-get-format-and-top-level)
  12979. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12980. (error "No operator defined for property %s" prop))
  12981. (org-columns-compute prop)))
  12982. (defvar org-property-allowed-value-functions nil
  12983. "Hook for functions supplying allowed values for a specific property.
  12984. The functions must take a single argument, the name of the property, and
  12985. return a flat list of allowed values. If \":ETC\" is one of
  12986. the values, this means that these values are intended as defaults for
  12987. completion, but that other values should be allowed too.
  12988. The functions must return nil if they are not responsible for this
  12989. property.")
  12990. (defun org-property-get-allowed-values (pom property &optional table)
  12991. "Get allowed values for the property PROPERTY.
  12992. When TABLE is non-nil, return an alist that can directly be used for
  12993. completion."
  12994. (let (vals)
  12995. (cond
  12996. ((equal property "TODO")
  12997. (setq vals (org-with-point-at pom
  12998. (append org-todo-keywords-1 '("")))))
  12999. ((equal property "PRIORITY")
  13000. (let ((n org-lowest-priority))
  13001. (while (>= n org-highest-priority)
  13002. (push (char-to-string n) vals)
  13003. (setq n (1- n)))))
  13004. ((member property org-special-properties))
  13005. ((setq vals (run-hook-with-args-until-success
  13006. 'org-property-allowed-value-functions property)))
  13007. (t
  13008. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13009. (when (and vals (string-match "\\S-" vals))
  13010. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13011. (setq vals (mapcar (lambda (x)
  13012. (cond ((stringp x) x)
  13013. ((numberp x) (number-to-string x))
  13014. ((symbolp x) (symbol-name x))
  13015. (t "???")))
  13016. vals)))))
  13017. (when (member ":ETC" vals)
  13018. (setq vals (remove ":ETC" vals))
  13019. (org-add-props (car vals) '(org-unrestricted t)))
  13020. (if table (mapcar 'list vals) vals)))
  13021. (defun org-property-previous-allowed-value (&optional previous)
  13022. "Switch to the next allowed value for this property."
  13023. (interactive)
  13024. (org-property-next-allowed-value t))
  13025. (defun org-property-next-allowed-value (&optional previous)
  13026. "Switch to the next allowed value for this property."
  13027. (interactive)
  13028. (unless (org-at-property-p)
  13029. (error "Not at a property"))
  13030. (let* ((key (match-string 2))
  13031. (value (match-string 3))
  13032. (allowed (or (org-property-get-allowed-values (point) key)
  13033. (and (member value '("[ ]" "[-]" "[X]"))
  13034. '("[ ]" "[X]"))))
  13035. nval)
  13036. (unless allowed
  13037. (error "Allowed values for this property have not been defined"))
  13038. (if previous (setq allowed (reverse allowed)))
  13039. (if (member value allowed)
  13040. (setq nval (car (cdr (member value allowed)))))
  13041. (setq nval (or nval (car allowed)))
  13042. (if (equal nval value)
  13043. (error "Only one allowed value for this property"))
  13044. (org-at-property-p)
  13045. (replace-match (concat " :" key ": " nval) t t)
  13046. (org-indent-line-function)
  13047. (beginning-of-line 1)
  13048. (skip-chars-forward " \t")
  13049. (run-hook-with-args 'org-property-changed-functions key nval)))
  13050. (defun org-find-olp (path &optional this-buffer)
  13051. "Return a marker pointing to the entry at outline path OLP.
  13052. If anything goes wrong, throw an error.
  13053. You can wrap this call to catch the error like this:
  13054. (condition-case msg
  13055. (org-mobile-locate-entry (match-string 4))
  13056. (error (nth 1 msg)))
  13057. The return value will then be either a string with the error message,
  13058. or a marker if everything is OK.
  13059. If THIS-BUFFER is set, the outline path does not contain a file,
  13060. only headings."
  13061. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13062. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13063. (level 1)
  13064. (lmin 1)
  13065. (lmax 1)
  13066. limit re end found pos heading cnt flevel)
  13067. (unless buffer (error "File not found :%s" file))
  13068. (with-current-buffer buffer
  13069. (save-excursion
  13070. (save-restriction
  13071. (widen)
  13072. (setq limit (point-max))
  13073. (goto-char (point-min))
  13074. (while (setq heading (pop path))
  13075. (setq re (format org-complex-heading-regexp-format
  13076. (regexp-quote heading)))
  13077. (setq cnt 0 pos (point))
  13078. (while (re-search-forward re end t)
  13079. (setq level (- (match-end 1) (match-beginning 1)))
  13080. (if (and (>= level lmin) (<= level lmax))
  13081. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13082. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13083. lmax heading))
  13084. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13085. lmax heading))
  13086. (goto-char found)
  13087. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13088. (setq end (save-excursion (org-end-of-subtree t t))))
  13089. (when (org-on-heading-p)
  13090. (move-marker (make-marker) (point))))))))
  13091. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13092. "Find node HEADING in BUFFER.
  13093. Return a marker to the heading if it was found, or nil if not.
  13094. If POS-ONLY is set, return just the position instead of a marker.
  13095. The heading text must match exact, but it may have a TODO keyword,
  13096. a priority cookie and tags in the standard locations."
  13097. (with-current-buffer (or buffer (current-buffer))
  13098. (save-excursion
  13099. (save-restriction
  13100. (widen)
  13101. (goto-char (point-min))
  13102. (let (case-fold-search)
  13103. (if (re-search-forward
  13104. (format org-complex-heading-regexp-format
  13105. (regexp-quote heading)) nil t)
  13106. (if pos-only
  13107. (match-beginning 0)
  13108. (move-marker (make-marker) (match-beginning 0)))))))))
  13109. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13110. "Find Org node headline HEADING in all .org files in directory DIR.
  13111. When the target headline is found, return a marker to this location."
  13112. (let ((files (directory-files (or dir default-directory)
  13113. nil "\\`[^.#].*\\.org\\'"))
  13114. file visiting m buffer)
  13115. (catch 'found
  13116. (while (setq file (pop files))
  13117. (message "trying %s" file)
  13118. (setq visiting (org-find-base-buffer-visiting file))
  13119. (setq buffer (or visiting (find-file-noselect file)))
  13120. (setq m (org-find-exact-headline-in-buffer
  13121. heading buffer))
  13122. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13123. (and m (throw 'found m))))))
  13124. (defun org-find-entry-with-id (ident)
  13125. "Locate the entry that contains the ID property with exact value IDENT.
  13126. IDENT can be a string, a symbol or a number, this function will search for
  13127. the string representation of it.
  13128. Return the position where this entry starts, or nil if there is no such entry."
  13129. (interactive "sID: ")
  13130. (let ((id (cond
  13131. ((stringp ident) ident)
  13132. ((symbol-name ident) (symbol-name ident))
  13133. ((numberp ident) (number-to-string ident))
  13134. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13135. (case-fold-search nil))
  13136. (save-excursion
  13137. (save-restriction
  13138. (widen)
  13139. (goto-char (point-min))
  13140. (when (re-search-forward
  13141. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13142. nil t)
  13143. (org-back-to-heading t)
  13144. (point))))))
  13145. ;;;; Timestamps
  13146. (defvar org-last-changed-timestamp nil)
  13147. (defvar org-last-inserted-timestamp nil
  13148. "The last time stamp inserted with `org-insert-time-stamp'.")
  13149. (defvar org-time-was-given) ; dynamically scoped parameter
  13150. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13151. (defvar org-ts-what) ; dynamically scoped parameter
  13152. (defun org-time-stamp (arg &optional inactive)
  13153. "Prompt for a date/time and insert a time stamp.
  13154. If the user specifies a time like HH:MM, or if this command is called
  13155. with a prefix argument, the time stamp will contain date and time.
  13156. Otherwise, only the date will be included. All parts of a date not
  13157. specified by the user will be filled in from the current date/time.
  13158. So if you press just return without typing anything, the time stamp
  13159. will represent the current date/time. If there is already a timestamp
  13160. at the cursor, it will be modified."
  13161. (interactive "P")
  13162. (let* ((ts nil)
  13163. (default-time
  13164. ;; Default time is either today, or, when entering a range,
  13165. ;; the range start.
  13166. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13167. (save-excursion
  13168. (re-search-backward
  13169. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13170. (- (point) 20) t)))
  13171. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13172. (current-time)))
  13173. (default-input (and ts (org-get-compact-tod ts)))
  13174. (repeater (save-excursion
  13175. (save-match-data
  13176. (beginning-of-line)
  13177. (when (re-search-forward
  13178. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13179. (save-excursion (progn (end-of-line) (point))) t)
  13180. (match-string 0)))))
  13181. org-time-was-given org-end-time-was-given time)
  13182. (cond
  13183. ((and (org-at-timestamp-p t)
  13184. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13185. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13186. (insert "--")
  13187. (setq time (let ((this-command this-command))
  13188. (org-read-date arg 'totime nil nil
  13189. default-time default-input)))
  13190. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13191. ((org-at-timestamp-p t)
  13192. (setq time (let ((this-command this-command))
  13193. (org-read-date arg 'totime nil nil default-time default-input)))
  13194. (when (org-at-timestamp-p t) ; just to get the match data
  13195. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13196. (replace-match "")
  13197. (setq org-last-changed-timestamp
  13198. (org-insert-time-stamp
  13199. time (or org-time-was-given arg)
  13200. inactive nil nil (list org-end-time-was-given)))
  13201. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13202. (setq org-last-changed-timestamp
  13203. (concat (substring org-last-inserted-timestamp 0 -1)
  13204. " " repeater ">"))))
  13205. (message "Timestamp updated"))
  13206. (t
  13207. (setq time (let ((this-command this-command))
  13208. (org-read-date arg 'totime nil nil default-time default-input)))
  13209. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13210. nil nil (list org-end-time-was-given))))))
  13211. ;; FIXME: can we use this for something else, like computing time differences?
  13212. (defun org-get-compact-tod (s)
  13213. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13214. (let* ((t1 (match-string 1 s))
  13215. (h1 (string-to-number (match-string 2 s)))
  13216. (m1 (string-to-number (match-string 3 s)))
  13217. (t2 (and (match-end 4) (match-string 5 s)))
  13218. (h2 (and t2 (string-to-number (match-string 6 s))))
  13219. (m2 (and t2 (string-to-number (match-string 7 s))))
  13220. dh dm)
  13221. (if (not t2)
  13222. t1
  13223. (setq dh (- h2 h1) dm (- m2 m1))
  13224. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13225. (concat t1 "+" (number-to-string dh)
  13226. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13227. (defun org-time-stamp-inactive (&optional arg)
  13228. "Insert an inactive time stamp.
  13229. An inactive time stamp is enclosed in square brackets instead of angle
  13230. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13231. does not link to the calendar and cannot be changed with the S-cursor keys.
  13232. So these are more for recording a certain time/date."
  13233. (interactive "P")
  13234. (org-time-stamp arg 'inactive))
  13235. (defvar org-date-ovl (make-overlay 1 1))
  13236. (overlay-put org-date-ovl 'face 'org-warning)
  13237. (org-detach-overlay org-date-ovl)
  13238. (defvar org-ans1) ; dynamically scoped parameter
  13239. (defvar org-ans2) ; dynamically scoped parameter
  13240. (defvar org-plain-time-of-day-regexp) ; defined below
  13241. (defvar org-overriding-default-time nil) ; dynamically scoped
  13242. (defvar org-read-date-overlay nil)
  13243. (defvar org-dcst nil) ; dynamically scoped
  13244. (defvar org-read-date-history nil)
  13245. (defvar org-read-date-final-answer nil)
  13246. (defvar org-read-date-analyze-futurep nil)
  13247. (defvar org-read-date-analyze-forced-year nil)
  13248. (defun org-read-date (&optional with-time to-time from-string prompt
  13249. default-time default-input)
  13250. "Read a date, possibly a time, and make things smooth for the user.
  13251. The prompt will suggest to enter an ISO date, but you can also enter anything
  13252. which will at least partially be understood by `parse-time-string'.
  13253. Unrecognized parts of the date will default to the current day, month, year,
  13254. hour and minute. If this command is called to replace a timestamp at point,
  13255. of to enter the second timestamp of a range, the default time is taken
  13256. from the existing stamp. Furthermore, the command prefers the future,
  13257. so if you are giving a date where the year is not given, and the day-month
  13258. combination is already past in the current year, it will assume you
  13259. mean next year. For details, see the manual. A few examples:
  13260. 3-2-5 --> 2003-02-05
  13261. feb 15 --> currentyear-02-15
  13262. 2/15 --> currentyear-02-15
  13263. sep 12 9 --> 2009-09-12
  13264. 12:45 --> today 12:45
  13265. 22 sept 0:34 --> currentyear-09-22 0:34
  13266. 12 --> currentyear-currentmonth-12
  13267. Fri --> nearest Friday (today or later)
  13268. etc.
  13269. Furthermore you can specify a relative date by giving, as the *first* thing
  13270. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13271. change in days weeks, months, years.
  13272. With a single plus or minus, the date is relative to today. With a double
  13273. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13274. +4d --> four days from today
  13275. +4 --> same as above
  13276. +2w --> two weeks from today
  13277. ++5 --> five days from default date
  13278. The function understands only English month and weekday abbreviations,
  13279. but this can be configured with the variables `parse-time-months' and
  13280. `parse-time-weekdays'.
  13281. While prompting, a calendar is popped up - you can also select the
  13282. date with the mouse (button 1). The calendar shows a period of three
  13283. months. To scroll it to other months, use the keys `>' and `<'.
  13284. If you don't like the calendar, turn it off with
  13285. \(setq org-read-date-popup-calendar nil)
  13286. With optional argument TO-TIME, the date will immediately be converted
  13287. to an internal time.
  13288. With an optional argument WITH-TIME, the prompt will suggest to also
  13289. insert a time. Note that when WITH-TIME is not set, you can still
  13290. enter a time, and this function will inform the calling routine about
  13291. this change. The calling routine may then choose to change the format
  13292. used to insert the time stamp into the buffer to include the time.
  13293. With optional argument FROM-STRING, read from this string instead from
  13294. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13295. the time/date that is used for everything that is not specified by the
  13296. user."
  13297. (require 'parse-time)
  13298. (let* ((org-time-stamp-rounding-minutes
  13299. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13300. (org-dcst org-display-custom-times)
  13301. (ct (org-current-time))
  13302. (def (or org-overriding-default-time default-time ct))
  13303. (defdecode (decode-time def))
  13304. (dummy (progn
  13305. (when (< (nth 2 defdecode) org-extend-today-until)
  13306. (setcar (nthcdr 2 defdecode) -1)
  13307. (setcar (nthcdr 1 defdecode) 59)
  13308. (setq def (apply 'encode-time defdecode)
  13309. defdecode (decode-time def)))))
  13310. (calendar-frame-setup nil)
  13311. (calendar-setup nil)
  13312. (calendar-move-hook nil)
  13313. (calendar-view-diary-initially-flag nil)
  13314. (calendar-view-holidays-initially-flag nil)
  13315. (timestr (format-time-string
  13316. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13317. (prompt (concat (if prompt (concat prompt " ") "")
  13318. (format "Date+time [%s]: " timestr)))
  13319. ans (org-ans0 "") org-ans1 org-ans2 final)
  13320. (cond
  13321. (from-string (setq ans from-string))
  13322. (org-read-date-popup-calendar
  13323. (save-excursion
  13324. (save-window-excursion
  13325. (calendar)
  13326. (unwind-protect
  13327. (progn
  13328. (calendar-forward-day (- (time-to-days def)
  13329. (calendar-absolute-from-gregorian
  13330. (calendar-current-date))))
  13331. (org-eval-in-calendar nil t)
  13332. (let* ((old-map (current-local-map))
  13333. (map (copy-keymap calendar-mode-map))
  13334. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13335. (org-defkey map (kbd "RET") 'org-calendar-select)
  13336. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13337. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13338. (org-defkey minibuffer-local-map [(meta shift left)]
  13339. (lambda () (interactive)
  13340. (org-eval-in-calendar '(calendar-backward-month 1))))
  13341. (org-defkey minibuffer-local-map [(meta shift right)]
  13342. (lambda () (interactive)
  13343. (org-eval-in-calendar '(calendar-forward-month 1))))
  13344. (org-defkey minibuffer-local-map [(meta shift up)]
  13345. (lambda () (interactive)
  13346. (org-eval-in-calendar '(calendar-backward-year 1))))
  13347. (org-defkey minibuffer-local-map [(meta shift down)]
  13348. (lambda () (interactive)
  13349. (org-eval-in-calendar '(calendar-forward-year 1))))
  13350. (org-defkey minibuffer-local-map [?\e (shift left)]
  13351. (lambda () (interactive)
  13352. (org-eval-in-calendar '(calendar-backward-month 1))))
  13353. (org-defkey minibuffer-local-map [?\e (shift right)]
  13354. (lambda () (interactive)
  13355. (org-eval-in-calendar '(calendar-forward-month 1))))
  13356. (org-defkey minibuffer-local-map [?\e (shift up)]
  13357. (lambda () (interactive)
  13358. (org-eval-in-calendar '(calendar-backward-year 1))))
  13359. (org-defkey minibuffer-local-map [?\e (shift down)]
  13360. (lambda () (interactive)
  13361. (org-eval-in-calendar '(calendar-forward-year 1))))
  13362. (org-defkey minibuffer-local-map [(shift up)]
  13363. (lambda () (interactive)
  13364. (org-eval-in-calendar '(calendar-backward-week 1))))
  13365. (org-defkey minibuffer-local-map [(shift down)]
  13366. (lambda () (interactive)
  13367. (org-eval-in-calendar '(calendar-forward-week 1))))
  13368. (org-defkey minibuffer-local-map [(shift left)]
  13369. (lambda () (interactive)
  13370. (org-eval-in-calendar '(calendar-backward-day 1))))
  13371. (org-defkey minibuffer-local-map [(shift right)]
  13372. (lambda () (interactive)
  13373. (org-eval-in-calendar '(calendar-forward-day 1))))
  13374. (org-defkey minibuffer-local-map ">"
  13375. (lambda () (interactive)
  13376. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13377. (org-defkey minibuffer-local-map "<"
  13378. (lambda () (interactive)
  13379. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13380. (org-defkey minibuffer-local-map "\C-v"
  13381. (lambda () (interactive)
  13382. (org-eval-in-calendar
  13383. '(calendar-scroll-left-three-months 1))))
  13384. (org-defkey minibuffer-local-map "\M-v"
  13385. (lambda () (interactive)
  13386. (org-eval-in-calendar
  13387. '(calendar-scroll-right-three-months 1))))
  13388. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13389. (unwind-protect
  13390. (progn
  13391. (use-local-map map)
  13392. (add-hook 'post-command-hook 'org-read-date-display)
  13393. (setq org-ans0 (read-string prompt default-input
  13394. 'org-read-date-history nil))
  13395. ;; org-ans0: from prompt
  13396. ;; org-ans1: from mouse click
  13397. ;; org-ans2: from calendar motion
  13398. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13399. (remove-hook 'post-command-hook 'org-read-date-display)
  13400. (use-local-map old-map)
  13401. (when org-read-date-overlay
  13402. (delete-overlay org-read-date-overlay)
  13403. (setq org-read-date-overlay nil)))))
  13404. (bury-buffer "*Calendar*")))))
  13405. (t ; Naked prompt only
  13406. (unwind-protect
  13407. (setq ans (read-string prompt default-input
  13408. 'org-read-date-history timestr))
  13409. (when org-read-date-overlay
  13410. (delete-overlay org-read-date-overlay)
  13411. (setq org-read-date-overlay nil)))))
  13412. (setq final (org-read-date-analyze ans def defdecode))
  13413. (when org-read-date-analyze-forced-year
  13414. (message "Year was forced into %s"
  13415. (if org-read-date-force-compatible-dates
  13416. "compatible range (1970-2037)"
  13417. "range representable on this machine"))
  13418. (ding))
  13419. ;; One round trip to get rid of 34th of August and stuff like that....
  13420. (setq final (decode-time (apply 'encode-time final)))
  13421. (setq org-read-date-final-answer ans)
  13422. (if to-time
  13423. (apply 'encode-time final)
  13424. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13425. (format "%04d-%02d-%02d %02d:%02d"
  13426. (nth 5 final) (nth 4 final) (nth 3 final)
  13427. (nth 2 final) (nth 1 final))
  13428. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13429. (defvar def)
  13430. (defvar defdecode)
  13431. (defvar with-time)
  13432. (defun org-read-date-display ()
  13433. "Display the current date prompt interpretation in the minibuffer."
  13434. (when org-read-date-display-live
  13435. (when org-read-date-overlay
  13436. (delete-overlay org-read-date-overlay))
  13437. (let ((p (point)))
  13438. (end-of-line 1)
  13439. (while (not (equal (buffer-substring
  13440. (max (point-min) (- (point) 4)) (point))
  13441. " "))
  13442. (insert " "))
  13443. (goto-char p))
  13444. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13445. " " (or org-ans1 org-ans2)))
  13446. (org-end-time-was-given nil)
  13447. (f (org-read-date-analyze ans def defdecode))
  13448. (fmts (if org-dcst
  13449. org-time-stamp-custom-formats
  13450. org-time-stamp-formats))
  13451. (fmt (if (or with-time
  13452. (and (boundp 'org-time-was-given) org-time-was-given))
  13453. (cdr fmts)
  13454. (car fmts)))
  13455. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13456. (when (and org-end-time-was-given
  13457. (string-match org-plain-time-of-day-regexp txt))
  13458. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13459. org-end-time-was-given
  13460. (substring txt (match-end 0)))))
  13461. (when org-read-date-analyze-futurep
  13462. (setq txt (concat txt " (=>F)")))
  13463. (setq org-read-date-overlay
  13464. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13465. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13466. (defun org-read-date-analyze (ans def defdecode)
  13467. "Analyze the combined answer of the date prompt."
  13468. ;; FIXME: cleanup and comment
  13469. (let ((nowdecode (decode-time (current-time)))
  13470. delta deltan deltaw deltadef year month day
  13471. hour minute second wday pm h2 m2 tl wday1
  13472. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13473. (setq org-read-date-analyze-futurep nil
  13474. org-read-date-analyze-forced-year nil)
  13475. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13476. (setq ans "+0"))
  13477. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13478. (setq ans (replace-match "" t t ans)
  13479. deltan (car delta)
  13480. deltaw (nth 1 delta)
  13481. deltadef (nth 2 delta)))
  13482. ;; Check if there is an iso week date in there
  13483. ;; If yes, store the info and postpone interpreting it until the rest
  13484. ;; of the parsing is done
  13485. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13486. (setq iso-year (if (match-end 1)
  13487. (org-small-year-to-year
  13488. (string-to-number (match-string 1 ans))))
  13489. iso-weekday (if (match-end 3)
  13490. (string-to-number (match-string 3 ans)))
  13491. iso-week (string-to-number (match-string 2 ans)))
  13492. (setq ans (replace-match "" t t ans)))
  13493. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13494. (when (string-match
  13495. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13496. (setq year (if (match-end 2)
  13497. (string-to-number (match-string 2 ans))
  13498. (progn (setq kill-year t)
  13499. (string-to-number (format-time-string "%Y"))))
  13500. month (string-to-number (match-string 3 ans))
  13501. day (string-to-number (match-string 4 ans)))
  13502. (if (< year 100) (setq year (+ 2000 year)))
  13503. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13504. t nil ans)))
  13505. ;; Help matching dottet european dates
  13506. (when (string-match
  13507. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13508. (setq year (if (match-end 3)
  13509. (string-to-number (match-string 3 ans))
  13510. (progn (setq kill-year t)
  13511. (string-to-number (format-time-string "%Y"))))
  13512. day (string-to-number (match-string 1 ans))
  13513. month (string-to-number (match-string 2 ans))
  13514. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13515. t nil ans)))
  13516. ;; Help matching american dates, like 5/30 or 5/30/7
  13517. (when (string-match
  13518. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13519. (setq year (if (match-end 4)
  13520. (string-to-number (match-string 4 ans))
  13521. (progn (setq kill-year t)
  13522. (string-to-number (format-time-string "%Y"))))
  13523. month (string-to-number (match-string 1 ans))
  13524. day (string-to-number (match-string 2 ans)))
  13525. (if (< year 100) (setq year (+ 2000 year)))
  13526. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13527. t nil ans)))
  13528. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13529. ;; If there is a time with am/pm, and *no* time without it, we convert
  13530. ;; so that matching will be successful.
  13531. (loop for i from 1 to 2 do ; twice, for end time as well
  13532. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13533. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13534. (setq hour (string-to-number (match-string 1 ans))
  13535. minute (if (match-end 3)
  13536. (string-to-number (match-string 3 ans))
  13537. 0)
  13538. pm (equal ?p
  13539. (string-to-char (downcase (match-string 4 ans)))))
  13540. (if (and (= hour 12) (not pm))
  13541. (setq hour 0)
  13542. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13543. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13544. t t ans))))
  13545. ;; Check if a time range is given as a duration
  13546. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13547. (setq hour (string-to-number (match-string 1 ans))
  13548. h2 (+ hour (string-to-number (match-string 3 ans)))
  13549. minute (string-to-number (match-string 2 ans))
  13550. m2 (+ minute (if (match-end 5) (string-to-number
  13551. (match-string 5 ans))0)))
  13552. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13553. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13554. t t ans)))
  13555. ;; Check if there is a time range
  13556. (when (boundp 'org-end-time-was-given)
  13557. (setq org-time-was-given nil)
  13558. (when (and (string-match org-plain-time-of-day-regexp ans)
  13559. (match-end 8))
  13560. (setq org-end-time-was-given (match-string 8 ans))
  13561. (setq ans (concat (substring ans 0 (match-beginning 7))
  13562. (substring ans (match-end 7))))))
  13563. (setq tl (parse-time-string ans)
  13564. day (or (nth 3 tl) (nth 3 defdecode))
  13565. month (or (nth 4 tl)
  13566. (if (and org-read-date-prefer-future
  13567. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13568. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13569. (nth 4 defdecode)))
  13570. year (or (and (not kill-year) (nth 5 tl))
  13571. (if (and org-read-date-prefer-future
  13572. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13573. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13574. (nth 5 defdecode)))
  13575. hour (or (nth 2 tl) (nth 2 defdecode))
  13576. minute (or (nth 1 tl) (nth 1 defdecode))
  13577. second (or (nth 0 tl) 0)
  13578. wday (nth 6 tl))
  13579. (when (and (eq org-read-date-prefer-future 'time)
  13580. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13581. (equal day (nth 3 nowdecode))
  13582. (equal month (nth 4 nowdecode))
  13583. (equal year (nth 5 nowdecode))
  13584. (nth 2 tl)
  13585. (or (< (nth 2 tl) (nth 2 nowdecode))
  13586. (and (= (nth 2 tl) (nth 2 nowdecode))
  13587. (nth 1 tl)
  13588. (< (nth 1 tl) (nth 1 nowdecode)))))
  13589. (setq day (1+ day)
  13590. futurep t))
  13591. ;; Special date definitions below
  13592. (cond
  13593. (iso-week
  13594. ;; There was an iso week
  13595. (require 'cal-iso)
  13596. (setq futurep nil)
  13597. (setq year (or iso-year year)
  13598. day (or iso-weekday wday 1)
  13599. wday nil ; to make sure that the trigger below does not match
  13600. iso-date (calendar-gregorian-from-absolute
  13601. (calendar-absolute-from-iso
  13602. (list iso-week day year))))
  13603. ; FIXME: Should we also push ISO weeks into the future?
  13604. ; (when (and org-read-date-prefer-future
  13605. ; (not iso-year)
  13606. ; (< (calendar-absolute-from-gregorian iso-date)
  13607. ; (time-to-days (current-time))))
  13608. ; (setq year (1+ year)
  13609. ; iso-date (calendar-gregorian-from-absolute
  13610. ; (calendar-absolute-from-iso
  13611. ; (list iso-week day year)))))
  13612. (setq month (car iso-date)
  13613. year (nth 2 iso-date)
  13614. day (nth 1 iso-date)))
  13615. (deltan
  13616. (setq futurep nil)
  13617. (unless deltadef
  13618. (let ((now (decode-time (current-time))))
  13619. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13620. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13621. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13622. ((equal deltaw "m") (setq month (+ month deltan)))
  13623. ((equal deltaw "y") (setq year (+ year deltan)))))
  13624. ((and wday (not (nth 3 tl)))
  13625. (setq futurep nil)
  13626. ;; Weekday was given, but no day, so pick that day in the week
  13627. ;; on or after the derived date.
  13628. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13629. (unless (equal wday wday1)
  13630. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13631. (if (and (boundp 'org-time-was-given)
  13632. (nth 2 tl))
  13633. (setq org-time-was-given t))
  13634. (if (< year 100) (setq year (+ 2000 year)))
  13635. ;; Check of the date is representable
  13636. (if org-read-date-force-compatible-dates
  13637. (progn
  13638. (if (< year 1970)
  13639. (setq year 1970 org-read-date-analyze-forced-year t))
  13640. (if (> year 2037)
  13641. (setq year 2037 org-read-date-analyze-forced-year t)))
  13642. (condition-case nil
  13643. (ignore (encode-time second minute hour day month year))
  13644. (error
  13645. (setq year (nth 5 defdecode))
  13646. (setq org-read-date-analyze-forced-year t))))
  13647. (setq org-read-date-analyze-futurep futurep)
  13648. (list second minute hour day month year)))
  13649. (defvar parse-time-weekdays)
  13650. (defun org-read-date-get-relative (s today default)
  13651. "Check string S for special relative date string.
  13652. TODAY and DEFAULT are internal times, for today and for a default.
  13653. Return shift list (N what def-flag)
  13654. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13655. N is the number of WHATs to shift.
  13656. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13657. the DEFAULT date rather than TODAY."
  13658. (when (and
  13659. (string-match
  13660. (concat
  13661. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13662. "\\([0-9]+\\)?"
  13663. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13664. "\\([ \t]\\|$\\)") s)
  13665. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13666. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13667. (string-to-char (substring (match-string 1 s) -1))
  13668. ?+))
  13669. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13670. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13671. (what (if (match-end 3) (match-string 3 s) "d"))
  13672. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13673. (date (if rel default today))
  13674. (wday (nth 6 (decode-time date)))
  13675. delta)
  13676. (if wday1
  13677. (progn
  13678. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13679. (if (= dir ?-) (setq delta (- delta 7)))
  13680. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13681. (list delta "d" rel))
  13682. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13683. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13684. "Turn a user-specified date into the internal representation.
  13685. The internal representation needed by the calendar is (month day year).
  13686. This is a wrapper to handle the brain-dead convention in calendar that
  13687. user function argument order change dependent on argument order."
  13688. (if (boundp 'calendar-date-style)
  13689. (cond
  13690. ((eq calendar-date-style 'american)
  13691. (list arg1 arg2 arg3))
  13692. ((eq calendar-date-style 'european)
  13693. (list arg2 arg1 arg3))
  13694. ((eq calendar-date-style 'iso)
  13695. (list arg2 arg3 arg1)))
  13696. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13697. (if (org-bound-and-true-p european-calendar-style)
  13698. (list arg2 arg1 arg3)
  13699. (list arg1 arg2 arg3)))))
  13700. (defun org-eval-in-calendar (form &optional keepdate)
  13701. "Eval FORM in the calendar window and return to current window.
  13702. Also, store the cursor date in variable org-ans2."
  13703. (let ((sf (selected-frame))
  13704. (sw (selected-window)))
  13705. (select-window (get-buffer-window "*Calendar*" t))
  13706. (eval form)
  13707. (when (and (not keepdate) (calendar-cursor-to-date))
  13708. (let* ((date (calendar-cursor-to-date))
  13709. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13710. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13711. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13712. (select-window sw)
  13713. (org-select-frame-set-input-focus sf)))
  13714. (defun org-calendar-select ()
  13715. "Return to `org-read-date' with the date currently selected.
  13716. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13717. (interactive)
  13718. (when (calendar-cursor-to-date)
  13719. (let* ((date (calendar-cursor-to-date))
  13720. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13721. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13722. (if (active-minibuffer-window) (exit-minibuffer))))
  13723. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13724. "Insert a date stamp for the date given by the internal TIME.
  13725. WITH-HM means use the stamp format that includes the time of the day.
  13726. INACTIVE means use square brackets instead of angular ones, so that the
  13727. stamp will not contribute to the agenda.
  13728. PRE and POST are optional strings to be inserted before and after the
  13729. stamp.
  13730. The command returns the inserted time stamp."
  13731. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13732. stamp)
  13733. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13734. (insert-before-markers (or pre ""))
  13735. (when (listp extra)
  13736. (setq extra (car extra))
  13737. (if (and (stringp extra)
  13738. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13739. (setq extra (format "-%02d:%02d"
  13740. (string-to-number (match-string 1 extra))
  13741. (string-to-number (match-string 2 extra))))
  13742. (setq extra nil)))
  13743. (when extra
  13744. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13745. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13746. (insert-before-markers (or post ""))
  13747. (setq org-last-inserted-timestamp stamp)))
  13748. (defun org-toggle-time-stamp-overlays ()
  13749. "Toggle the use of custom time stamp formats."
  13750. (interactive)
  13751. (setq org-display-custom-times (not org-display-custom-times))
  13752. (unless org-display-custom-times
  13753. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13754. (while (setq p (next-single-property-change p 'display))
  13755. (if (and (get-text-property p 'display)
  13756. (eq (get-text-property p 'face) 'org-date))
  13757. (remove-text-properties
  13758. p (setq p (next-single-property-change p 'display))
  13759. '(display t))))
  13760. (set-buffer-modified-p bmp)))
  13761. (if (featurep 'xemacs)
  13762. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13763. (org-restart-font-lock)
  13764. (setq org-table-may-need-update t)
  13765. (if org-display-custom-times
  13766. (message "Time stamps are overlayed with custom format")
  13767. (message "Time stamp overlays removed")))
  13768. (defun org-display-custom-time (beg end)
  13769. "Overlay modified time stamp format over timestamp between BEG and END."
  13770. (let* ((ts (buffer-substring beg end))
  13771. t1 w1 with-hm tf time str w2 (off 0))
  13772. (save-match-data
  13773. (setq t1 (org-parse-time-string ts t))
  13774. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13775. (setq off (- (match-end 0) (match-beginning 0)))))
  13776. (setq end (- end off))
  13777. (setq w1 (- end beg)
  13778. with-hm (and (nth 1 t1) (nth 2 t1))
  13779. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13780. time (org-fix-decoded-time t1)
  13781. str (org-add-props
  13782. (format-time-string
  13783. (substring tf 1 -1) (apply 'encode-time time))
  13784. nil 'mouse-face 'highlight)
  13785. w2 (length str))
  13786. (if (not (= w2 w1))
  13787. (add-text-properties (1+ beg) (+ 2 beg)
  13788. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13789. (if (featurep 'xemacs)
  13790. (progn
  13791. (put-text-property beg end 'invisible t)
  13792. (put-text-property beg end 'end-glyph (make-glyph str)))
  13793. (put-text-property beg end 'display str))))
  13794. (defun org-translate-time (string)
  13795. "Translate all timestamps in STRING to custom format.
  13796. But do this only if the variable `org-display-custom-times' is set."
  13797. (when org-display-custom-times
  13798. (save-match-data
  13799. (let* ((start 0)
  13800. (re org-ts-regexp-both)
  13801. t1 with-hm inactive tf time str beg end)
  13802. (while (setq start (string-match re string start))
  13803. (setq beg (match-beginning 0)
  13804. end (match-end 0)
  13805. t1 (save-match-data
  13806. (org-parse-time-string (substring string beg end) t))
  13807. with-hm (and (nth 1 t1) (nth 2 t1))
  13808. inactive (equal (substring string beg (1+ beg)) "[")
  13809. tf (funcall (if with-hm 'cdr 'car)
  13810. org-time-stamp-custom-formats)
  13811. time (org-fix-decoded-time t1)
  13812. str (format-time-string
  13813. (concat
  13814. (if inactive "[" "<") (substring tf 1 -1)
  13815. (if inactive "]" ">"))
  13816. (apply 'encode-time time))
  13817. string (replace-match str t t string)
  13818. start (+ start (length str)))))))
  13819. string)
  13820. (defun org-fix-decoded-time (time)
  13821. "Set 0 instead of nil for the first 6 elements of time.
  13822. Don't touch the rest."
  13823. (let ((n 0))
  13824. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13825. (defun org-days-to-time (timestamp-string)
  13826. "Difference between TIMESTAMP-STRING and now in days."
  13827. (- (time-to-days (org-time-string-to-time timestamp-string))
  13828. (time-to-days (current-time))))
  13829. (defun org-deadline-close (timestamp-string &optional ndays)
  13830. "Is the time in TIMESTAMP-STRING close to the current date?"
  13831. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13832. (and (< (org-days-to-time timestamp-string) ndays)
  13833. (not (org-entry-is-done-p))))
  13834. (defun org-get-wdays (ts)
  13835. "Get the deadline lead time appropriate for timestring TS."
  13836. (cond
  13837. ((<= org-deadline-warning-days 0)
  13838. ;; 0 or negative, enforce this value no matter what
  13839. (- org-deadline-warning-days))
  13840. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13841. ;; lead time is specified.
  13842. (floor (* (string-to-number (match-string 1 ts))
  13843. (cdr (assoc (match-string 2 ts)
  13844. '(("d" . 1) ("w" . 7)
  13845. ("m" . 30.4) ("y" . 365.25)))))))
  13846. ;; go for the default.
  13847. (t org-deadline-warning-days)))
  13848. (defun org-calendar-select-mouse (ev)
  13849. "Return to `org-read-date' with the date currently selected.
  13850. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13851. (interactive "e")
  13852. (mouse-set-point ev)
  13853. (when (calendar-cursor-to-date)
  13854. (let* ((date (calendar-cursor-to-date))
  13855. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13856. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13857. (if (active-minibuffer-window) (exit-minibuffer))))
  13858. (defun org-check-deadlines (ndays)
  13859. "Check if there are any deadlines due or past due.
  13860. A deadline is considered due if it happens within `org-deadline-warning-days'
  13861. days from today's date. If the deadline appears in an entry marked DONE,
  13862. it is not shown. The prefix arg NDAYS can be used to test that many
  13863. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13864. (interactive "P")
  13865. (let* ((org-warn-days
  13866. (cond
  13867. ((equal ndays '(4)) 100000)
  13868. (ndays (prefix-numeric-value ndays))
  13869. (t (abs org-deadline-warning-days))))
  13870. (case-fold-search nil)
  13871. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13872. (callback
  13873. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13874. (message "%d deadlines past-due or due within %d days"
  13875. (org-occur regexp nil callback)
  13876. org-warn-days)))
  13877. (defun org-check-before-date (date)
  13878. "Check if there are deadlines or scheduled entries before DATE."
  13879. (interactive (list (org-read-date)))
  13880. (let ((case-fold-search nil)
  13881. (regexp (concat "\\<\\(" org-deadline-string
  13882. "\\|" org-scheduled-string
  13883. "\\) *<\\([^>]+\\)>"))
  13884. (callback
  13885. (lambda () (time-less-p
  13886. (org-time-string-to-time (match-string 2))
  13887. (org-time-string-to-time date)))))
  13888. (message "%d entries before %s"
  13889. (org-occur regexp nil callback) date)))
  13890. (defun org-check-after-date (date)
  13891. "Check if there are deadlines or scheduled entries after DATE."
  13892. (interactive (list (org-read-date)))
  13893. (let ((case-fold-search nil)
  13894. (regexp (concat "\\<\\(" org-deadline-string
  13895. "\\|" org-scheduled-string
  13896. "\\) *<\\([^>]+\\)>"))
  13897. (callback
  13898. (lambda () (not
  13899. (time-less-p
  13900. (org-time-string-to-time (match-string 2))
  13901. (org-time-string-to-time date))))))
  13902. (message "%d entries after %s"
  13903. (org-occur regexp nil callback) date)))
  13904. (defun org-evaluate-time-range (&optional to-buffer)
  13905. "Evaluate a time range by computing the difference between start and end.
  13906. Normally the result is just printed in the echo area, but with prefix arg
  13907. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13908. If the time range is actually in a table, the result is inserted into the
  13909. next column.
  13910. For time difference computation, a year is assumed to be exactly 365
  13911. days in order to avoid rounding problems."
  13912. (interactive "P")
  13913. (or
  13914. (org-clock-update-time-maybe)
  13915. (save-excursion
  13916. (unless (org-at-date-range-p t)
  13917. (goto-char (point-at-bol))
  13918. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13919. (if (not (org-at-date-range-p t))
  13920. (error "Not at a time-stamp range, and none found in current line")))
  13921. (let* ((ts1 (match-string 1))
  13922. (ts2 (match-string 2))
  13923. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13924. (match-end (match-end 0))
  13925. (time1 (org-time-string-to-time ts1))
  13926. (time2 (org-time-string-to-time ts2))
  13927. (t1 (org-float-time time1))
  13928. (t2 (org-float-time time2))
  13929. (diff (abs (- t2 t1)))
  13930. (negative (< (- t2 t1) 0))
  13931. ;; (ys (floor (* 365 24 60 60)))
  13932. (ds (* 24 60 60))
  13933. (hs (* 60 60))
  13934. (fy "%dy %dd %02d:%02d")
  13935. (fy1 "%dy %dd")
  13936. (fd "%dd %02d:%02d")
  13937. (fd1 "%dd")
  13938. (fh "%02d:%02d")
  13939. y d h m align)
  13940. (if havetime
  13941. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13942. y 0
  13943. d (floor (/ diff ds)) diff (mod diff ds)
  13944. h (floor (/ diff hs)) diff (mod diff hs)
  13945. m (floor (/ diff 60)))
  13946. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13947. y 0
  13948. d (floor (+ (/ diff ds) 0.5))
  13949. h 0 m 0))
  13950. (if (not to-buffer)
  13951. (message "%s" (org-make-tdiff-string y d h m))
  13952. (if (org-at-table-p)
  13953. (progn
  13954. (goto-char match-end)
  13955. (setq align t)
  13956. (and (looking-at " *|") (goto-char (match-end 0))))
  13957. (goto-char match-end))
  13958. (if (looking-at
  13959. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13960. (replace-match ""))
  13961. (if negative (insert " -"))
  13962. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13963. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13964. (insert " " (format fh h m))))
  13965. (if align (org-table-align))
  13966. (message "Time difference inserted")))))
  13967. (defun org-make-tdiff-string (y d h m)
  13968. (let ((fmt "")
  13969. (l nil))
  13970. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13971. l (push y l)))
  13972. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13973. l (push d l)))
  13974. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13975. l (push h l)))
  13976. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13977. l (push m l)))
  13978. (apply 'format fmt (nreverse l))))
  13979. (defun org-time-string-to-time (s)
  13980. (apply 'encode-time (org-parse-time-string s)))
  13981. (defun org-time-string-to-seconds (s)
  13982. (org-float-time (org-time-string-to-time s)))
  13983. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13984. "Convert a time stamp to an absolute day number.
  13985. If there is a specifier for a cyclic time stamp, get the closest date to
  13986. DAYNR.
  13987. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13988. The variable date is bound by the calendar when this is called."
  13989. (cond
  13990. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13991. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13992. daynr
  13993. (+ daynr 1000)))
  13994. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13995. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13996. (time-to-days (current-time))) (match-string 0 s)
  13997. prefer show-all))
  13998. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13999. (defun org-days-to-iso-week (days)
  14000. "Return the iso week number."
  14001. (require 'cal-iso)
  14002. (car (calendar-iso-from-absolute days)))
  14003. (defun org-small-year-to-year (year)
  14004. "Convert 2-digit years into 4-digit years.
  14005. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14006. The year 2000 cannot be abbreviated. Any year larger than 99
  14007. is returned unchanged."
  14008. (if (< year 38)
  14009. (setq year (+ 2000 year))
  14010. (if (< year 100)
  14011. (setq year (+ 1900 year))))
  14012. year)
  14013. (defun org-time-from-absolute (d)
  14014. "Return the time corresponding to date D.
  14015. D may be an absolute day number, or a calendar-type list (month day year)."
  14016. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14017. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14018. (defun org-calendar-holiday ()
  14019. "List of holidays, for Diary display in Org-mode."
  14020. (require 'holidays)
  14021. (let ((hl (funcall
  14022. (if (fboundp 'calendar-check-holidays)
  14023. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14024. (if hl (mapconcat 'identity hl "; "))))
  14025. (defun org-diary-sexp-entry (sexp entry date)
  14026. "Process a SEXP diary ENTRY for DATE."
  14027. (require 'diary-lib)
  14028. (let ((result (if calendar-debug-sexp
  14029. (let ((stack-trace-on-error t))
  14030. (eval (car (read-from-string sexp))))
  14031. (condition-case nil
  14032. (eval (car (read-from-string sexp)))
  14033. (error
  14034. (beep)
  14035. (message "Bad sexp at line %d in %s: %s"
  14036. (org-current-line)
  14037. (buffer-file-name) sexp)
  14038. (sleep-for 2))))))
  14039. (cond ((stringp result) (split-string result "; "))
  14040. ((and (consp result)
  14041. (not (consp (cdr result)))
  14042. (stringp (cdr result))) (cdr result))
  14043. ((and (consp result)
  14044. (stringp (car result))) result)
  14045. (result entry)
  14046. (t nil))))
  14047. (defun org-diary-to-ical-string (frombuf)
  14048. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14049. This uses the icalendar.el library."
  14050. (let* ((tmpdir (if (featurep 'xemacs)
  14051. (temp-directory)
  14052. temporary-file-directory))
  14053. (tmpfile (make-temp-name
  14054. (expand-file-name "orgics" tmpdir)))
  14055. buf rtn b e)
  14056. (with-current-buffer frombuf
  14057. (icalendar-export-region (point-min) (point-max) tmpfile)
  14058. (setq buf (find-buffer-visiting tmpfile))
  14059. (set-buffer buf)
  14060. (goto-char (point-min))
  14061. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14062. (setq b (match-beginning 0)))
  14063. (goto-char (point-max))
  14064. (if (re-search-backward "^END:VEVENT" nil t)
  14065. (setq e (match-end 0)))
  14066. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14067. (kill-buffer buf)
  14068. (delete-file tmpfile)
  14069. rtn))
  14070. (defun org-closest-date (start current change prefer show-all)
  14071. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14072. When PREFER is `past', return a date that is either CURRENT or past.
  14073. When PREFER is `future', return a date that is either CURRENT or future.
  14074. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14075. ;; Make the proper lists from the dates
  14076. (catch 'exit
  14077. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14078. dn dw sday cday n1 n2 n0
  14079. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14080. (setq start (org-date-to-gregorian start)
  14081. current (org-date-to-gregorian
  14082. (if show-all
  14083. current
  14084. (time-to-days (current-time))))
  14085. sday (calendar-absolute-from-gregorian start)
  14086. cday (calendar-absolute-from-gregorian current))
  14087. (if (<= cday sday) (throw 'exit sday))
  14088. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14089. (setq dn (string-to-number (match-string 1 change))
  14090. dw (cdr (assoc (match-string 2 change) a1)))
  14091. (error "Invalid change specifier: %s" change))
  14092. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14093. (cond
  14094. ((eq dw 'day)
  14095. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14096. n2 (+ n1 dn)))
  14097. ((eq dw 'year)
  14098. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14099. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14100. (setq date1 (list m d y1)
  14101. n1 (calendar-absolute-from-gregorian date1)
  14102. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14103. n2 (calendar-absolute-from-gregorian date2)))
  14104. ((eq dw 'month)
  14105. ;; approx number of month between the two dates
  14106. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14107. ;; How often does dn fit in there?
  14108. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14109. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14110. m (+ m nm)
  14111. ny (floor (/ m 12))
  14112. y (+ y ny)
  14113. m (- m (* ny 12)))
  14114. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14115. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14116. (setq m2 (+ m dn) y2 y)
  14117. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14118. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14119. (while (<= n2 cday)
  14120. (setq n1 n2 m m2 y y2)
  14121. (setq m2 (+ m dn) y2 y)
  14122. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14123. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14124. ;; Make sure n1 is the earlier date
  14125. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14126. (if show-all
  14127. (cond
  14128. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14129. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14130. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14131. (cond
  14132. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14133. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14134. (t (if (= cday n1) n1 n2)))))))
  14135. (defun org-date-to-gregorian (date)
  14136. "Turn any specification of DATE into a Gregorian date for the calendar."
  14137. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14138. ((and (listp date) (= (length date) 3)) date)
  14139. ((stringp date)
  14140. (setq date (org-parse-time-string date))
  14141. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14142. ((listp date)
  14143. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14144. (defun org-parse-time-string (s &optional nodefault)
  14145. "Parse the standard Org-mode time string.
  14146. This should be a lot faster than the normal `parse-time-string'.
  14147. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14148. hour and minute fields will be nil if not given."
  14149. (if (string-match org-ts-regexp0 s)
  14150. (list 0
  14151. (if (or (match-beginning 8) (not nodefault))
  14152. (string-to-number (or (match-string 8 s) "0")))
  14153. (if (or (match-beginning 7) (not nodefault))
  14154. (string-to-number (or (match-string 7 s) "0")))
  14155. (string-to-number (match-string 4 s))
  14156. (string-to-number (match-string 3 s))
  14157. (string-to-number (match-string 2 s))
  14158. nil nil nil)
  14159. (error "Not a standard Org-mode time string: %s" s)))
  14160. (defun org-timestamp-up (&optional arg)
  14161. "Increase the date item at the cursor by one.
  14162. If the cursor is on the year, change the year. If it is on the month,
  14163. the day or the time, change that.
  14164. With prefix ARG, change by that many units."
  14165. (interactive "p")
  14166. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14167. (defun org-timestamp-down (&optional arg)
  14168. "Decrease the date item at the cursor by one.
  14169. If the cursor is on the year, change the year. If it is on the month,
  14170. the day or the time, change that.
  14171. With prefix ARG, change by that many units."
  14172. (interactive "p")
  14173. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14174. (defun org-timestamp-up-day (&optional arg)
  14175. "Increase the date in the time stamp by one day.
  14176. With prefix ARG, change that many days."
  14177. (interactive "p")
  14178. (if (and (not (org-at-timestamp-p t))
  14179. (org-on-heading-p))
  14180. (org-todo 'up)
  14181. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14182. (defun org-timestamp-down-day (&optional arg)
  14183. "Decrease the date in the time stamp by one day.
  14184. With prefix ARG, change that many days."
  14185. (interactive "p")
  14186. (if (and (not (org-at-timestamp-p t))
  14187. (org-on-heading-p))
  14188. (org-todo 'down)
  14189. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14190. (defun org-at-timestamp-p (&optional inactive-ok)
  14191. "Determine if the cursor is in or at a timestamp."
  14192. (interactive)
  14193. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14194. (pos (point))
  14195. (ans (or (looking-at tsr)
  14196. (save-excursion
  14197. (skip-chars-backward "^[<\n\r\t")
  14198. (if (> (point) (point-min)) (backward-char 1))
  14199. (and (looking-at tsr)
  14200. (> (- (match-end 0) pos) -1))))))
  14201. (and ans
  14202. (boundp 'org-ts-what)
  14203. (setq org-ts-what
  14204. (cond
  14205. ((= pos (match-beginning 0)) 'bracket)
  14206. ((= pos (1- (match-end 0))) 'bracket)
  14207. ((org-pos-in-match-range pos 2) 'year)
  14208. ((org-pos-in-match-range pos 3) 'month)
  14209. ((org-pos-in-match-range pos 7) 'hour)
  14210. ((org-pos-in-match-range pos 8) 'minute)
  14211. ((or (org-pos-in-match-range pos 4)
  14212. (org-pos-in-match-range pos 5)) 'day)
  14213. ((and (> pos (or (match-end 8) (match-end 5)))
  14214. (< pos (match-end 0)))
  14215. (- pos (or (match-end 8) (match-end 5))))
  14216. (t 'day))))
  14217. ans))
  14218. (defun org-toggle-timestamp-type ()
  14219. "Toggle the type (<active> or [inactive]) of a time stamp."
  14220. (interactive)
  14221. (when (org-at-timestamp-p t)
  14222. (let ((beg (match-beginning 0)) (end (match-end 0))
  14223. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14224. (save-excursion
  14225. (goto-char beg)
  14226. (while (re-search-forward "[][<>]" end t)
  14227. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14228. t t)))
  14229. (message "Timestamp is now %sactive"
  14230. (if (equal (char-after beg) ?<) "" "in")))))
  14231. (defun org-timestamp-change (n &optional what updown)
  14232. "Change the date in the time stamp at point.
  14233. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14234. `year', `minute', `second'. If WHAT is not given, the cursor position
  14235. in the timestamp determines what will be changed."
  14236. (let ((pos (point))
  14237. with-hm inactive
  14238. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14239. org-ts-what
  14240. extra rem
  14241. ts time time0)
  14242. (if (not (org-at-timestamp-p t))
  14243. (error "Not at a timestamp"))
  14244. (if (and (not what) (eq org-ts-what 'bracket))
  14245. (org-toggle-timestamp-type)
  14246. (if (and (not what) (not (eq org-ts-what 'day))
  14247. org-display-custom-times
  14248. (get-text-property (point) 'display)
  14249. (not (get-text-property (1- (point)) 'display)))
  14250. (setq org-ts-what 'day))
  14251. (setq org-ts-what (or what org-ts-what)
  14252. inactive (= (char-after (match-beginning 0)) ?\[)
  14253. ts (match-string 0))
  14254. (replace-match "")
  14255. (if (string-match
  14256. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14257. ts)
  14258. (setq extra (match-string 1 ts)))
  14259. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14260. (setq with-hm t))
  14261. (setq time0 (org-parse-time-string ts))
  14262. (when (and updown
  14263. (eq org-ts-what 'minute)
  14264. (not current-prefix-arg))
  14265. ;; This looks like s-up and s-down. Change by one rounding step.
  14266. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14267. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14268. (setcar (cdr time0) (+ (nth 1 time0)
  14269. (if (> n 0) (- rem) (- dm rem))))))
  14270. (setq time
  14271. (encode-time (or (car time0) 0)
  14272. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14273. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14274. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14275. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14276. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14277. (nthcdr 6 time0)))
  14278. (when (and (member org-ts-what '(hour minute))
  14279. extra
  14280. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14281. (setq extra (org-modify-ts-extra
  14282. extra
  14283. (if (eq org-ts-what 'hour) 2 5)
  14284. n dm)))
  14285. (when (integerp org-ts-what)
  14286. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14287. (if (eq what 'calendar)
  14288. (let ((cal-date (org-get-date-from-calendar)))
  14289. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14290. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14291. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14292. (setcar time0 (or (car time0) 0))
  14293. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14294. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14295. (setq time (apply 'encode-time time0))))
  14296. (setq org-last-changed-timestamp
  14297. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14298. (org-clock-update-time-maybe)
  14299. (goto-char pos)
  14300. ;; Try to recenter the calendar window, if any
  14301. (if (and org-calendar-follow-timestamp-change
  14302. (get-buffer-window "*Calendar*" t)
  14303. (memq org-ts-what '(day month year)))
  14304. (org-recenter-calendar (time-to-days time))))))
  14305. (defun org-modify-ts-extra (s pos n dm)
  14306. "Change the different parts of the lead-time and repeat fields in timestamp."
  14307. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14308. ng h m new rem)
  14309. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14310. (cond
  14311. ((or (org-pos-in-match-range pos 2)
  14312. (org-pos-in-match-range pos 3))
  14313. (setq m (string-to-number (match-string 3 s))
  14314. h (string-to-number (match-string 2 s)))
  14315. (if (org-pos-in-match-range pos 2)
  14316. (setq h (+ h n))
  14317. (setq n (* dm (org-no-warnings (signum n))))
  14318. (when (not (= 0 (setq rem (% m dm))))
  14319. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14320. (setq m (+ m n)))
  14321. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14322. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14323. (setq h (min 24 (max 0 h)))
  14324. (setq ng 1 new (format "-%02d:%02d" h m)))
  14325. ((org-pos-in-match-range pos 6)
  14326. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14327. ((org-pos-in-match-range pos 5)
  14328. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14329. ((org-pos-in-match-range pos 9)
  14330. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14331. ((org-pos-in-match-range pos 8)
  14332. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14333. (when ng
  14334. (setq s (concat
  14335. (substring s 0 (match-beginning ng))
  14336. new
  14337. (substring s (match-end ng))))))
  14338. s))
  14339. (defun org-recenter-calendar (date)
  14340. "If the calendar is visible, recenter it to DATE."
  14341. (let* ((win (selected-window))
  14342. (cwin (get-buffer-window "*Calendar*" t))
  14343. (calendar-move-hook nil))
  14344. (when cwin
  14345. (select-window cwin)
  14346. (calendar-goto-date (if (listp date) date
  14347. (calendar-gregorian-from-absolute date)))
  14348. (select-window win))))
  14349. (defun org-goto-calendar (&optional arg)
  14350. "Go to the Emacs calendar at the current date.
  14351. If there is a time stamp in the current line, go to that date.
  14352. A prefix ARG can be used to force the current date."
  14353. (interactive "P")
  14354. (let ((tsr org-ts-regexp) diff
  14355. (calendar-move-hook nil)
  14356. (calendar-view-holidays-initially-flag nil)
  14357. (calendar-view-diary-initially-flag nil))
  14358. (if (or (org-at-timestamp-p)
  14359. (save-excursion
  14360. (beginning-of-line 1)
  14361. (looking-at (concat ".*" tsr))))
  14362. (let ((d1 (time-to-days (current-time)))
  14363. (d2 (time-to-days
  14364. (org-time-string-to-time (match-string 1)))))
  14365. (setq diff (- d2 d1))))
  14366. (calendar)
  14367. (calendar-goto-today)
  14368. (if (and diff (not arg)) (calendar-forward-day diff))))
  14369. (defun org-get-date-from-calendar ()
  14370. "Return a list (month day year) of date at point in calendar."
  14371. (with-current-buffer "*Calendar*"
  14372. (save-match-data
  14373. (calendar-cursor-to-date))))
  14374. (defun org-date-from-calendar ()
  14375. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14376. If there is already a time stamp at the cursor position, update it."
  14377. (interactive)
  14378. (if (org-at-timestamp-p t)
  14379. (org-timestamp-change 0 'calendar)
  14380. (let ((cal-date (org-get-date-from-calendar)))
  14381. (org-insert-time-stamp
  14382. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14383. (defun org-minutes-to-hh:mm-string (m)
  14384. "Compute H:MM from a number of minutes."
  14385. (let ((h (/ m 60)))
  14386. (setq m (- m (* 60 h)))
  14387. (format org-time-clocksum-format h m)))
  14388. (defun org-hh:mm-string-to-minutes (s)
  14389. "Convert a string H:MM to a number of minutes.
  14390. If the string is just a number, interpret it as minutes.
  14391. In fact, the first hh:mm or number in the string will be taken,
  14392. there can be extra stuff in the string.
  14393. If no number is found, the return value is 0."
  14394. (cond
  14395. ((integerp s) s)
  14396. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14397. (+ (* (string-to-number (match-string 1 s)) 60)
  14398. (string-to-number (match-string 2 s))))
  14399. ((string-match "\\([0-9]+\\)" s)
  14400. (string-to-number (match-string 1 s)))
  14401. (t 0)))
  14402. (defcustom org-effort-durations
  14403. `(("h" . 60)
  14404. ("d" . ,(* 60 8))
  14405. ("w" . ,(* 60 8 5))
  14406. ("m" . ,(* 60 8 5 4))
  14407. ("y" . ,(* 60 8 5 40)))
  14408. "Conversion factor to minutes for an effort modifier.
  14409. Each entry has the form (MODIFIER . MINUTES).
  14410. In an effort string, a number followed by MODIFIER is multiplied
  14411. by the specified number of MINUTES to obtain an effort in
  14412. minutes.
  14413. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14414. effort string \"2hours\" is equivalent to 120 minutes."
  14415. :group 'org-agenda
  14416. :type '(alist :key-type (string :tag "Modifier")
  14417. :value-type (number :tag "Minutes")))
  14418. (defun org-duration-string-to-minutes (s)
  14419. "Convert a duration string S to minutes.
  14420. A bare number is interpreted as minutes, modifiers can be set by
  14421. customizing `org-effort-durations' (which see).
  14422. Entries containing a colon are interpreted as H:MM by
  14423. `org-hh:mm-string-to-minutes'."
  14424. (let ((result 0)
  14425. (re (concat "\\([0-9]+\\) *\\("
  14426. (regexp-opt (mapcar 'car org-effort-durations))
  14427. "\\)")))
  14428. (while (string-match re s)
  14429. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14430. (string-to-number (match-string 1 s))))
  14431. (setq s (replace-match "" nil t s)))
  14432. (incf result (org-hh:mm-string-to-minutes s))
  14433. result))
  14434. ;;;; Files
  14435. (defun org-save-all-org-buffers ()
  14436. "Save all Org-mode buffers without user confirmation."
  14437. (interactive)
  14438. (message "Saving all Org-mode buffers...")
  14439. (save-some-buffers t 'org-mode-p)
  14440. (when (featurep 'org-id) (org-id-locations-save))
  14441. (message "Saving all Org-mode buffers... done"))
  14442. (defun org-revert-all-org-buffers ()
  14443. "Revert all Org-mode buffers.
  14444. Prompt for confirmation when there are unsaved changes.
  14445. Be sure you know what you are doing before letting this function
  14446. overwrite your changes.
  14447. This function is useful in a setup where one tracks org files
  14448. with a version control system, to revert on one machine after pulling
  14449. changes from another. I believe the procedure must be like this:
  14450. 1. M-x org-save-all-org-buffers
  14451. 2. Pull changes from the other machine, resolve conflicts
  14452. 3. M-x org-revert-all-org-buffers"
  14453. (interactive)
  14454. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14455. (error "Abort"))
  14456. (save-excursion
  14457. (save-window-excursion
  14458. (mapc
  14459. (lambda (b)
  14460. (when (and (with-current-buffer b (org-mode-p))
  14461. (with-current-buffer b buffer-file-name))
  14462. (switch-to-buffer b)
  14463. (revert-buffer t 'no-confirm)))
  14464. (buffer-list))
  14465. (when (and (featurep 'org-id) org-id-track-globally)
  14466. (org-id-locations-load)))))
  14467. ;;;; Agenda files
  14468. ;;;###autoload
  14469. (defun org-switchb (&optional arg)
  14470. "Switch between Org buffers.
  14471. With a prefix argument, restrict available to files.
  14472. With two prefix arguments, restrict available buffers to agenda files.
  14473. Defaults to `iswitchb' for buffer name completion.
  14474. Set `org-completion-use-ido' to make it use ido instead."
  14475. (interactive "P")
  14476. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14477. ((equal arg '(16)) (org-buffer-list 'agenda))
  14478. (t (org-buffer-list))))
  14479. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14480. (org-completion-use-ido org-completion-use-ido))
  14481. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14482. (setq org-completion-use-iswitchb t))
  14483. (switch-to-buffer
  14484. (org-icompleting-read "Org buffer: "
  14485. (mapcar 'list (mapcar 'buffer-name blist))
  14486. nil t))))
  14487. ;;; Define some older names previously used for this functionality
  14488. ;;;###autoload
  14489. (defalias 'org-ido-switchb 'org-switchb)
  14490. ;;;###autoload
  14491. (defalias 'org-iswitchb 'org-switchb)
  14492. (defun org-buffer-list (&optional predicate exclude-tmp)
  14493. "Return a list of Org buffers.
  14494. PREDICATE can be `export', `files' or `agenda'.
  14495. export restrict the list to Export buffers.
  14496. files restrict the list to buffers visiting Org files.
  14497. agenda restrict the list to buffers visiting agenda files.
  14498. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14499. (let* ((bfn nil)
  14500. (agenda-files (and (eq predicate 'agenda)
  14501. (mapcar 'file-truename (org-agenda-files t))))
  14502. (filter
  14503. (cond
  14504. ((eq predicate 'files)
  14505. (lambda (b) (with-current-buffer b (org-mode-p))))
  14506. ((eq predicate 'export)
  14507. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14508. ((eq predicate 'agenda)
  14509. (lambda (b)
  14510. (with-current-buffer b
  14511. (and (org-mode-p)
  14512. (setq bfn (buffer-file-name b))
  14513. (member (file-truename bfn) agenda-files)))))
  14514. (t (lambda (b) (with-current-buffer b
  14515. (or (org-mode-p)
  14516. (string-match "\*Org .*Export"
  14517. (buffer-name b)))))))))
  14518. (delq nil
  14519. (mapcar
  14520. (lambda(b)
  14521. (if (and (funcall filter b)
  14522. (or (not exclude-tmp)
  14523. (not (string-match "tmp" (buffer-name b)))))
  14524. b
  14525. nil))
  14526. (buffer-list)))))
  14527. (defun org-agenda-files (&optional unrestricted archives)
  14528. "Get the list of agenda files.
  14529. Optional UNRESTRICTED means return the full list even if a restriction
  14530. is currently in place.
  14531. When ARCHIVES is t, include all archive files that are really being
  14532. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14533. `org-agenda-archives-mode' is t."
  14534. (let ((files
  14535. (cond
  14536. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14537. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14538. ((listp org-agenda-files) org-agenda-files)
  14539. (t (error "Invalid value of `org-agenda-files'")))))
  14540. (setq files (apply 'append
  14541. (mapcar (lambda (f)
  14542. (if (file-directory-p f)
  14543. (directory-files
  14544. f t org-agenda-file-regexp)
  14545. (list f)))
  14546. files)))
  14547. (when org-agenda-skip-unavailable-files
  14548. (setq files (delq nil
  14549. (mapcar (function
  14550. (lambda (file)
  14551. (and (file-readable-p file) file)))
  14552. files))))
  14553. (when (or (eq archives t)
  14554. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14555. (setq files (org-add-archive-files files)))
  14556. files))
  14557. (defun org-agenda-file-p (&optional file)
  14558. "Return non-nil, if FILE is an agenda file.
  14559. If FILE is omitted, use the file associated with the current
  14560. buffer."
  14561. (member (or file (buffer-file-name))
  14562. (org-agenda-files t)))
  14563. (defun org-edit-agenda-file-list ()
  14564. "Edit the list of agenda files.
  14565. Depending on setup, this either uses customize to edit the variable
  14566. `org-agenda-files', or it visits the file that is holding the list. In the
  14567. latter case, the buffer is set up in a way that saving it automatically kills
  14568. the buffer and restores the previous window configuration."
  14569. (interactive)
  14570. (if (stringp org-agenda-files)
  14571. (let ((cw (current-window-configuration)))
  14572. (find-file org-agenda-files)
  14573. (org-set-local 'org-window-configuration cw)
  14574. (org-add-hook 'after-save-hook
  14575. (lambda ()
  14576. (set-window-configuration
  14577. (prog1 org-window-configuration
  14578. (kill-buffer (current-buffer))))
  14579. (org-install-agenda-files-menu)
  14580. (message "New agenda file list installed"))
  14581. nil 'local)
  14582. (message "%s" (substitute-command-keys
  14583. "Edit list and finish with \\[save-buffer]")))
  14584. (customize-variable 'org-agenda-files)))
  14585. (defun org-store-new-agenda-file-list (list)
  14586. "Set new value for the agenda file list and save it correctly."
  14587. (if (stringp org-agenda-files)
  14588. (let ((fe (org-read-agenda-file-list t)) b u)
  14589. (while (setq b (find-buffer-visiting org-agenda-files))
  14590. (kill-buffer b))
  14591. (with-temp-file org-agenda-files
  14592. (insert
  14593. (mapconcat
  14594. (lambda (f) ;; Keep un-expanded entries.
  14595. (if (setq u (assoc f fe))
  14596. (cdr u)
  14597. f))
  14598. list "\n")
  14599. "\n")))
  14600. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14601. (org-insert-mode-line-in-empty-file nil))
  14602. (setq org-agenda-files list)
  14603. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14604. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14605. "Read the list of agenda files from a file.
  14606. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14607. filenames, used by `org-store-new-agenda-file-list' to write back
  14608. un-expanded file names."
  14609. (when (file-directory-p org-agenda-files)
  14610. (error "`org-agenda-files' cannot be a single directory"))
  14611. (when (stringp org-agenda-files)
  14612. (with-temp-buffer
  14613. (insert-file-contents org-agenda-files)
  14614. (mapcar
  14615. (lambda (f)
  14616. (let ((e (expand-file-name (substitute-in-file-name f)
  14617. org-directory)))
  14618. (if pair-with-expansion
  14619. (cons e f)
  14620. e)))
  14621. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14622. ;;;###autoload
  14623. (defun org-cycle-agenda-files ()
  14624. "Cycle through the files in `org-agenda-files'.
  14625. If the current buffer visits an agenda file, find the next one in the list.
  14626. If the current buffer does not, find the first agenda file."
  14627. (interactive)
  14628. (let* ((fs (org-agenda-files t))
  14629. (files (append fs (list (car fs))))
  14630. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14631. file)
  14632. (unless files (error "No agenda files"))
  14633. (catch 'exit
  14634. (while (setq file (pop files))
  14635. (if (equal (file-truename file) tcf)
  14636. (when (car files)
  14637. (find-file (car files))
  14638. (throw 'exit t))))
  14639. (find-file (car fs)))
  14640. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14641. (defun org-agenda-file-to-front (&optional to-end)
  14642. "Move/add the current file to the top of the agenda file list.
  14643. If the file is not present in the list, it is added to the front. If it is
  14644. present, it is moved there. With optional argument TO-END, add/move to the
  14645. end of the list."
  14646. (interactive "P")
  14647. (let ((org-agenda-skip-unavailable-files nil)
  14648. (file-alist (mapcar (lambda (x)
  14649. (cons (file-truename x) x))
  14650. (org-agenda-files t)))
  14651. (ctf (file-truename buffer-file-name))
  14652. x had)
  14653. (setq x (assoc ctf file-alist) had x)
  14654. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14655. (if to-end
  14656. (setq file-alist (append (delq x file-alist) (list x)))
  14657. (setq file-alist (cons x (delq x file-alist))))
  14658. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14659. (org-install-agenda-files-menu)
  14660. (message "File %s to %s of agenda file list"
  14661. (if had "moved" "added") (if to-end "end" "front"))))
  14662. (defun org-remove-file (&optional file)
  14663. "Remove current file from the list of files in variable `org-agenda-files'.
  14664. These are the files which are being checked for agenda entries.
  14665. Optional argument FILE means use this file instead of the current."
  14666. (interactive)
  14667. (let* ((org-agenda-skip-unavailable-files nil)
  14668. (file (or file buffer-file-name))
  14669. (true-file (file-truename file))
  14670. (afile (abbreviate-file-name file))
  14671. (files (delq nil (mapcar
  14672. (lambda (x)
  14673. (if (equal true-file
  14674. (file-truename x))
  14675. nil x))
  14676. (org-agenda-files t)))))
  14677. (if (not (= (length files) (length (org-agenda-files t))))
  14678. (progn
  14679. (org-store-new-agenda-file-list files)
  14680. (org-install-agenda-files-menu)
  14681. (message "Removed file: %s" afile))
  14682. (message "File was not in list: %s (not removed)" afile))))
  14683. (defun org-file-menu-entry (file)
  14684. (vector file (list 'find-file file) t))
  14685. (defun org-check-agenda-file (file)
  14686. "Make sure FILE exists. If not, ask user what to do."
  14687. (when (not (file-exists-p file))
  14688. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14689. (abbreviate-file-name file))
  14690. (let ((r (downcase (read-char-exclusive))))
  14691. (cond
  14692. ((equal r ?r)
  14693. (org-remove-file file)
  14694. (throw 'nextfile t))
  14695. (t (error "Abort"))))))
  14696. (defun org-get-agenda-file-buffer (file)
  14697. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14698. it to the list of buffers which might be released later."
  14699. (let ((buf (org-find-base-buffer-visiting file)))
  14700. (if buf
  14701. buf ; just return it
  14702. ;; Make a new buffer and remember it
  14703. (setq buf (find-file-noselect file))
  14704. (if buf (push buf org-agenda-new-buffers))
  14705. buf)))
  14706. (defun org-release-buffers (blist)
  14707. "Release all buffers in list, asking the user for confirmation when needed.
  14708. When a buffer is unmodified, it is just killed. When modified, it is saved
  14709. \(if the user agrees) and then killed."
  14710. (let (buf file)
  14711. (while (setq buf (pop blist))
  14712. (setq file (buffer-file-name buf))
  14713. (when (and (buffer-modified-p buf)
  14714. file
  14715. (y-or-n-p (format "Save file %s? " file)))
  14716. (with-current-buffer buf (save-buffer)))
  14717. (kill-buffer buf))))
  14718. (defun org-prepare-agenda-buffers (files)
  14719. "Create buffers for all agenda files, protect archived trees and comments."
  14720. (interactive)
  14721. (let ((pa '(:org-archived t))
  14722. (pc '(:org-comment t))
  14723. (pall '(:org-archived t :org-comment t))
  14724. (inhibit-read-only t)
  14725. (rea (concat ":" org-archive-tag ":"))
  14726. bmp file re)
  14727. (save-excursion
  14728. (save-restriction
  14729. (while (setq file (pop files))
  14730. (catch 'nextfile
  14731. (if (bufferp file)
  14732. (set-buffer file)
  14733. (org-check-agenda-file file)
  14734. (set-buffer (org-get-agenda-file-buffer file)))
  14735. (widen)
  14736. (setq bmp (buffer-modified-p))
  14737. (org-refresh-category-properties)
  14738. (setq org-todo-keywords-for-agenda
  14739. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14740. (setq org-done-keywords-for-agenda
  14741. (append org-done-keywords-for-agenda org-done-keywords))
  14742. (setq org-todo-keyword-alist-for-agenda
  14743. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14744. (setq org-drawers-for-agenda
  14745. (append org-drawers-for-agenda org-drawers))
  14746. (setq org-tag-alist-for-agenda
  14747. (append org-tag-alist-for-agenda org-tag-alist))
  14748. (save-excursion
  14749. (remove-text-properties (point-min) (point-max) pall)
  14750. (when org-agenda-skip-archived-trees
  14751. (goto-char (point-min))
  14752. (while (re-search-forward rea nil t)
  14753. (if (org-on-heading-p t)
  14754. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14755. (goto-char (point-min))
  14756. (setq re (concat org-outline-regexp-bol "+" org-comment-string "\\>"))
  14757. (while (re-search-forward re nil t)
  14758. (add-text-properties
  14759. (match-beginning 0) (org-end-of-subtree t) pc)))
  14760. (set-buffer-modified-p bmp)))))
  14761. (setq org-todo-keywords-for-agenda
  14762. (org-uniquify org-todo-keywords-for-agenda))
  14763. (setq org-todo-keyword-alist-for-agenda
  14764. (org-uniquify org-todo-keyword-alist-for-agenda)
  14765. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14766. ;;;; Embedded LaTeX
  14767. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14768. "Keymap for the minor `org-cdlatex-mode'.")
  14769. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14770. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14771. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14772. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14773. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14774. (defvar org-cdlatex-texmathp-advice-is-done nil
  14775. "Flag remembering if we have applied the advice to texmathp already.")
  14776. (define-minor-mode org-cdlatex-mode
  14777. "Toggle the minor `org-cdlatex-mode'.
  14778. This mode supports entering LaTeX environment and math in LaTeX fragments
  14779. in Org-mode.
  14780. \\{org-cdlatex-mode-map}"
  14781. nil " OCDL" nil
  14782. (when org-cdlatex-mode (require 'cdlatex))
  14783. (unless org-cdlatex-texmathp-advice-is-done
  14784. (setq org-cdlatex-texmathp-advice-is-done t)
  14785. (defadvice texmathp (around org-math-always-on activate)
  14786. "Always return t in org-mode buffers.
  14787. This is because we want to insert math symbols without dollars even outside
  14788. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14789. an embedded LaTeX fragment, let texmathp do its job.
  14790. \\[org-cdlatex-mode-map]"
  14791. (interactive)
  14792. (let (p)
  14793. (cond
  14794. ((not (org-mode-p)) ad-do-it)
  14795. ((eq this-command 'cdlatex-math-symbol)
  14796. (setq ad-return-value t
  14797. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14798. (t
  14799. (let ((p (org-inside-LaTeX-fragment-p)))
  14800. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14801. (setq ad-return-value t
  14802. texmathp-why '("Org-mode embedded math" . 0))
  14803. (if p ad-do-it)))))))))
  14804. (defun turn-on-org-cdlatex ()
  14805. "Unconditionally turn on `org-cdlatex-mode'."
  14806. (org-cdlatex-mode 1))
  14807. (defun org-inside-LaTeX-fragment-p ()
  14808. "Test if point is inside a LaTeX fragment.
  14809. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14810. sequence appearing also before point.
  14811. Even though the matchers for math are configurable, this function assumes
  14812. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14813. delimiters are skipped when they have been removed by customization.
  14814. The return value is nil, or a cons cell with the delimiter and the
  14815. position of this delimiter.
  14816. This function does a reasonably good job, but can locally be fooled by
  14817. for example currency specifications. For example it will assume being in
  14818. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14819. fragments that are properly closed, but during editing, we have to live
  14820. with the uncertainty caused by missing closing delimiters. This function
  14821. looks only before point, not after."
  14822. (catch 'exit
  14823. (let ((pos (point))
  14824. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14825. (lim (progn
  14826. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14827. (point)))
  14828. dd-on str (start 0) m re)
  14829. (goto-char pos)
  14830. (when dodollar
  14831. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14832. re (nth 1 (assoc "$" org-latex-regexps)))
  14833. (while (string-match re str start)
  14834. (cond
  14835. ((= (match-end 0) (length str))
  14836. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14837. ((= (match-end 0) (- (length str) 5))
  14838. (throw 'exit nil))
  14839. (t (setq start (match-end 0))))))
  14840. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14841. (goto-char pos)
  14842. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14843. (and (match-beginning 2) (throw 'exit nil))
  14844. ;; count $$
  14845. (while (re-search-backward "\\$\\$" lim t)
  14846. (setq dd-on (not dd-on)))
  14847. (goto-char pos)
  14848. (if dd-on (cons "$$" m))))))
  14849. (defun org-inside-latex-macro-p ()
  14850. "Is point inside a LaTeX macro or its arguments?"
  14851. (save-match-data
  14852. (org-in-regexp
  14853. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14854. (defun org-try-cdlatex-tab ()
  14855. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14856. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14857. - inside a LaTeX fragment, or
  14858. - after the first word in a line, where an abbreviation expansion could
  14859. insert a LaTeX environment."
  14860. (when org-cdlatex-mode
  14861. (cond
  14862. ((save-excursion
  14863. (skip-chars-backward "a-zA-Z0-9*")
  14864. (skip-chars-backward " \t")
  14865. (bolp))
  14866. (cdlatex-tab) t)
  14867. ((org-inside-LaTeX-fragment-p)
  14868. (cdlatex-tab) t)
  14869. (t nil))))
  14870. (defun org-cdlatex-underscore-caret (&optional arg)
  14871. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14872. Revert to the normal definition outside of these fragments."
  14873. (interactive "P")
  14874. (if (org-inside-LaTeX-fragment-p)
  14875. (call-interactively 'cdlatex-sub-superscript)
  14876. (let (org-cdlatex-mode)
  14877. (call-interactively (key-binding (vector last-input-event))))))
  14878. (defun org-cdlatex-math-modify (&optional arg)
  14879. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14880. Revert to the normal definition outside of these fragments."
  14881. (interactive "P")
  14882. (if (org-inside-LaTeX-fragment-p)
  14883. (call-interactively 'cdlatex-math-modify)
  14884. (let (org-cdlatex-mode)
  14885. (call-interactively (key-binding (vector last-input-event))))))
  14886. (defvar org-latex-fragment-image-overlays nil
  14887. "List of overlays carrying the images of latex fragments.")
  14888. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14889. (defun org-remove-latex-fragment-image-overlays ()
  14890. "Remove all overlays with LaTeX fragment images in current buffer."
  14891. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14892. (setq org-latex-fragment-image-overlays nil))
  14893. (defun org-preview-latex-fragment (&optional subtree)
  14894. "Preview the LaTeX fragment at point, or all locally or globally.
  14895. If the cursor is in a LaTeX fragment, create the image and overlay
  14896. it over the source code. If there is no fragment at point, display
  14897. all fragments in the current text, from one headline to the next. With
  14898. prefix SUBTREE, display all fragments in the current subtree. With a
  14899. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14900. the cursor is before the first headline,
  14901. display all fragments in the buffer.
  14902. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14903. (interactive "P")
  14904. (org-remove-latex-fragment-image-overlays)
  14905. (save-excursion
  14906. (save-restriction
  14907. (let (beg end at msg)
  14908. (cond
  14909. ((or (equal subtree '(16))
  14910. (not (save-excursion
  14911. (re-search-backward org-outline-regexp-bol nil t))))
  14912. (setq beg (point-min) end (point-max)
  14913. msg "Creating images for buffer...%s"))
  14914. ((equal subtree '(4))
  14915. (org-back-to-heading)
  14916. (setq beg (point) end (org-end-of-subtree t)
  14917. msg "Creating images for subtree...%s"))
  14918. (t
  14919. (if (setq at (org-inside-LaTeX-fragment-p))
  14920. (goto-char (max (point-min) (- (cdr at) 2)))
  14921. (org-back-to-heading))
  14922. (setq beg (point) end (progn (outline-next-heading) (point))
  14923. msg (if at "Creating image...%s"
  14924. "Creating images for entry...%s"))))
  14925. (message msg "")
  14926. (narrow-to-region beg end)
  14927. (goto-char beg)
  14928. (org-format-latex
  14929. (concat "ltxpng/" (file-name-sans-extension
  14930. (file-name-nondirectory
  14931. buffer-file-name)))
  14932. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14933. (message msg "done. Use `C-c C-c' to remove images.")))))
  14934. (defvar org-latex-regexps
  14935. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14936. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14937. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14938. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14939. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14940. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14941. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14942. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14943. "Regular expressions for matching embedded LaTeX.")
  14944. (defvar org-export-have-math nil) ;; dynamic scoping
  14945. (defun org-format-latex (prefix &optional dir overlays msg at
  14946. forbuffer processing-type)
  14947. "Replace LaTeX fragments with links to an image, and produce images.
  14948. Some of the options can be changed using the variable
  14949. `org-format-latex-options'."
  14950. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14951. (let* ((prefixnodir (file-name-nondirectory prefix))
  14952. (absprefix (expand-file-name prefix dir))
  14953. (todir (file-name-directory absprefix))
  14954. (opt org-format-latex-options)
  14955. (matchers (plist-get opt :matchers))
  14956. (re-list org-latex-regexps)
  14957. (org-format-latex-header-extra
  14958. (plist-get (org-infile-export-plist) :latex-header-extra))
  14959. (cnt 0) txt hash link beg end re e checkdir
  14960. executables-checked string
  14961. m n block linkfile movefile ov)
  14962. ;; Check the different regular expressions
  14963. (while (setq e (pop re-list))
  14964. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14965. block (if (nth 3 e) "\n\n" ""))
  14966. (when (member m matchers)
  14967. (goto-char (point-min))
  14968. (while (re-search-forward re nil t)
  14969. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14970. (not (get-text-property (match-beginning n)
  14971. 'org-protected))
  14972. (or (not overlays)
  14973. (not (eq (get-char-property (match-beginning n)
  14974. 'org-overlay-type)
  14975. 'org-latex-overlay))))
  14976. (setq org-export-have-math t)
  14977. (cond
  14978. ((eq processing-type 'verbatim)
  14979. ;; Leave the text verbatim, just protect it
  14980. (add-text-properties (match-beginning n) (match-end n)
  14981. '(org-protected t)))
  14982. ((eq processing-type 'mathjax)
  14983. ;; Prepare for MathJax processing
  14984. (setq string (match-string n))
  14985. (if (member m '("$" "$1"))
  14986. (save-excursion
  14987. (delete-region (match-beginning n) (match-end n))
  14988. (goto-char (match-beginning n))
  14989. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14990. "\\)")
  14991. '(org-protected t))))
  14992. (add-text-properties (match-beginning n) (match-end n)
  14993. '(org-protected t))))
  14994. ((or (eq processing-type 'dvipng) t)
  14995. ;; Process to an image
  14996. (setq txt (match-string n)
  14997. beg (match-beginning n) end (match-end n)
  14998. cnt (1+ cnt))
  14999. (let (print-length print-level) ; make sure full list is printed
  15000. (setq hash (sha1 (prin1-to-string
  15001. (list org-format-latex-header
  15002. org-format-latex-header-extra
  15003. org-export-latex-default-packages-alist
  15004. org-export-latex-packages-alist
  15005. org-format-latex-options
  15006. forbuffer txt)))
  15007. linkfile (format "%s_%s.png" prefix hash)
  15008. movefile (format "%s_%s.png" absprefix hash)))
  15009. (setq link (concat block "[[file:" linkfile "]]" block))
  15010. (if msg (message msg cnt))
  15011. (goto-char beg)
  15012. (unless checkdir ; make sure the directory exists
  15013. (setq checkdir t)
  15014. (or (file-directory-p todir) (make-directory todir t)))
  15015. (unless executables-checked
  15016. (org-check-external-command
  15017. "latex" "needed to convert LaTeX fragments to images")
  15018. (org-check-external-command
  15019. "dvipng" "needed to convert LaTeX fragments to images")
  15020. (setq executables-checked t))
  15021. (unless (file-exists-p movefile)
  15022. (org-create-formula-image
  15023. txt movefile opt forbuffer))
  15024. (if overlays
  15025. (progn
  15026. (mapc (lambda (o)
  15027. (if (eq (overlay-get o 'org-overlay-type)
  15028. 'org-latex-overlay)
  15029. (delete-overlay o)))
  15030. (overlays-in beg end))
  15031. (setq ov (make-overlay beg end))
  15032. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15033. (if (featurep 'xemacs)
  15034. (progn
  15035. (overlay-put ov 'invisible t)
  15036. (overlay-put
  15037. ov 'end-glyph
  15038. (make-glyph (vector 'png :file movefile))))
  15039. (overlay-put
  15040. ov 'display
  15041. (list 'image :type 'png :file movefile :ascent 'center)))
  15042. (push ov org-latex-fragment-image-overlays)
  15043. (goto-char end))
  15044. (delete-region beg end)
  15045. (insert (org-add-props link
  15046. (list 'org-latex-src
  15047. (replace-regexp-in-string
  15048. "\"" "" txt)))))))))))))
  15049. ;; This function borrows from Ganesh Swami's latex2png.el
  15050. (defun org-create-formula-image (string tofile options buffer)
  15051. "This calls dvipng."
  15052. (require 'org-latex)
  15053. (let* ((tmpdir (if (featurep 'xemacs)
  15054. (temp-directory)
  15055. temporary-file-directory))
  15056. (texfilebase (make-temp-name
  15057. (expand-file-name "orgtex" tmpdir)))
  15058. (texfile (concat texfilebase ".tex"))
  15059. (dvifile (concat texfilebase ".dvi"))
  15060. (pngfile (concat texfilebase ".png"))
  15061. (fnh (if (featurep 'xemacs)
  15062. (font-height (get-face-font 'default))
  15063. (face-attribute 'default :height nil)))
  15064. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15065. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15066. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15067. "Black"))
  15068. (bg (or (plist-get options (if buffer :background :html-background))
  15069. "Transparent")))
  15070. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15071. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15072. (with-temp-file texfile
  15073. (insert (org-splice-latex-header
  15074. org-format-latex-header
  15075. org-export-latex-default-packages-alist
  15076. org-export-latex-packages-alist t
  15077. org-format-latex-header-extra))
  15078. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15079. (require 'org-latex)
  15080. (org-export-latex-fix-inputenc))
  15081. (let ((dir default-directory))
  15082. (condition-case nil
  15083. (progn
  15084. (cd tmpdir)
  15085. (call-process "latex" nil nil nil texfile))
  15086. (error nil))
  15087. (cd dir))
  15088. (if (not (file-exists-p dvifile))
  15089. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15090. (condition-case nil
  15091. (call-process "dvipng" nil nil nil
  15092. "-fg" fg "-bg" bg
  15093. "-D" dpi
  15094. ;;"-x" scale "-y" scale
  15095. "-T" "tight"
  15096. "-o" pngfile
  15097. dvifile)
  15098. (error nil))
  15099. (if (not (file-exists-p pngfile))
  15100. (if org-format-latex-signal-error
  15101. (error "Failed to create png file from %s" texfile)
  15102. (message "Failed to create png file from %s" texfile)
  15103. nil)
  15104. ;; Use the requested file name and clean up
  15105. (copy-file pngfile tofile 'replace)
  15106. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15107. (delete-file (concat texfilebase e)))
  15108. pngfile))))
  15109. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15110. "Fill a LaTeX header template TPL.
  15111. In the template, the following place holders will be recognized:
  15112. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15113. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15114. [PACKAGES] \\usepackage statements for PKG
  15115. [NO-PACKAGES] do not include PKG
  15116. [EXTRA] the string EXTRA
  15117. [NO-EXTRA] do not include EXTRA
  15118. For backward compatibility, if both the positive and the negative place
  15119. holder is missing, the positive one (without the \"NO-\") will be
  15120. assumed to be present at the end of the template.
  15121. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15122. EXTRA is a string.
  15123. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15124. (let (rpl (end ""))
  15125. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15126. (setq rpl (if (or (match-end 1) (not def-pkg))
  15127. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15128. tpl (replace-match rpl t t tpl))
  15129. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15130. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15131. (setq rpl (if (or (match-end 1) (not pkg))
  15132. "" (org-latex-packages-to-string pkg snippets-p t))
  15133. tpl (replace-match rpl t t tpl))
  15134. (if pkg (setq end
  15135. (concat end "\n"
  15136. (org-latex-packages-to-string pkg snippets-p)))))
  15137. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15138. (setq rpl (if (or (match-end 1) (not extra))
  15139. "" (concat extra "\n"))
  15140. tpl (replace-match rpl t t tpl))
  15141. (if (and extra (string-match "\\S-" extra))
  15142. (setq end (concat end "\n" extra))))
  15143. (if (string-match "\\S-" end)
  15144. (concat tpl "\n" end)
  15145. tpl)))
  15146. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15147. "Turn an alist of packages into a string with the \\usepackage macros."
  15148. (setq pkg (mapconcat (lambda(p)
  15149. (cond
  15150. ((stringp p) p)
  15151. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15152. (format "%% Package %s omitted" (cadr p)))
  15153. ((equal "" (car p))
  15154. (format "\\usepackage{%s}" (cadr p)))
  15155. (t
  15156. (format "\\usepackage[%s]{%s}"
  15157. (car p) (cadr p)))))
  15158. pkg
  15159. "\n"))
  15160. (if newline (concat pkg "\n") pkg))
  15161. (defun org-dvipng-color (attr)
  15162. "Return an rgb color specification for dvipng."
  15163. (apply 'format "rgb %s %s %s"
  15164. (mapcar 'org-normalize-color
  15165. (color-values (face-attribute 'default attr nil)))))
  15166. (defun org-normalize-color (value)
  15167. "Return string to be used as color value for an RGB component."
  15168. (format "%g" (/ value 65535.0)))
  15169. ;; Image display
  15170. (defvar org-inline-image-overlays nil)
  15171. (make-variable-buffer-local 'org-inline-image-overlays)
  15172. (defun org-toggle-inline-images (&optional include-linked)
  15173. "Toggle the display of inline images.
  15174. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15175. (interactive "P")
  15176. (if org-inline-image-overlays
  15177. (progn
  15178. (org-remove-inline-images)
  15179. (message "Inline image display turned off"))
  15180. (org-display-inline-images include-linked)
  15181. (if org-inline-image-overlays
  15182. (message "%d images displayed inline"
  15183. (length org-inline-image-overlays))
  15184. (message "No images to display inline"))))
  15185. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15186. "Display inline images.
  15187. Normally only links without a description part are inlined, because this
  15188. is how it will work for export. When INCLUDE-LINKED is set, also links
  15189. with a description part will be inlined. This can be nice for a quick
  15190. look at those images, but it does not reflect what exported files will look
  15191. like.
  15192. When REFRESH is set, refresh existing images between BEG and END.
  15193. This will create new image displays only if necessary.
  15194. BEG and END default to the buffer boundaries."
  15195. (interactive "P")
  15196. (unless refresh
  15197. (org-remove-inline-images)
  15198. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15199. (save-excursion
  15200. (save-restriction
  15201. (widen)
  15202. (setq beg (or beg (point-min)) end (or end (point-max)))
  15203. (goto-char (point-min))
  15204. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15205. (substring (org-image-file-name-regexp) 0 -2)
  15206. "\\)\\]" (if include-linked "" "\\]")))
  15207. old file ov img)
  15208. (while (re-search-forward re end t)
  15209. (setq old (get-char-property-and-overlay (match-beginning 1)
  15210. 'org-image-overlay))
  15211. (setq file (expand-file-name
  15212. (concat (or (match-string 3) "") (match-string 4))))
  15213. (when (file-exists-p file)
  15214. (if (and (car-safe old) refresh)
  15215. (image-refresh (overlay-get (cdr old) 'display))
  15216. (setq img (save-match-data (create-image file)))
  15217. (when img
  15218. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15219. (overlay-put ov 'display img)
  15220. (overlay-put ov 'face 'default)
  15221. (overlay-put ov 'org-image-overlay t)
  15222. (overlay-put ov 'modification-hooks
  15223. (list 'org-display-inline-modification-hook))
  15224. (push ov org-inline-image-overlays)))))))))
  15225. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15226. "Remove inline-display overlay if a corresponding region is modified."
  15227. (let ((inhibit-modification-hooks t))
  15228. (when (and ov after)
  15229. (delete ov org-inline-image-overlays)
  15230. (delete-overlay ov))))
  15231. (defun org-remove-inline-images ()
  15232. "Remove inline display of images."
  15233. (interactive)
  15234. (mapc 'delete-overlay org-inline-image-overlays)
  15235. (setq org-inline-image-overlays nil))
  15236. ;;;; Key bindings
  15237. ;; Make `C-c C-x' a prefix key
  15238. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15239. ;; TAB key with modifiers
  15240. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15241. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15242. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15243. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15244. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15245. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15246. ;; The following line is necessary under Suse GNU/Linux
  15247. (unless (featurep 'xemacs)
  15248. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15249. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15250. (define-key org-mode-map [backtab] 'org-shifttab)
  15251. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15252. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15253. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15254. ;; Cursor keys with modifiers
  15255. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15256. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15257. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15258. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15259. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15260. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15261. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15262. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15263. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15264. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15265. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15266. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15267. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15268. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15269. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15270. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15271. ;; Babel keys
  15272. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15273. (mapc (lambda (pair)
  15274. (define-key org-babel-map (car pair) (cdr pair)))
  15275. org-babel-key-bindings)
  15276. ;;; Extra keys for tty access.
  15277. ;; We only set them when really needed because otherwise the
  15278. ;; menus don't show the simple keys
  15279. (when (or org-use-extra-keys
  15280. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15281. (not window-system))
  15282. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15283. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15284. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15285. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15286. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15287. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15288. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15289. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15290. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15291. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15292. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15293. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15294. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15295. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15296. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15297. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15298. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15299. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15300. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15301. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15302. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15303. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15304. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15305. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15306. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15307. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15308. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15309. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15310. ;; All the other keys
  15311. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15312. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15313. (if (boundp 'narrow-map)
  15314. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15315. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15316. (if (boundp 'narrow-map)
  15317. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15318. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15319. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15320. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15321. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15322. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15323. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15324. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15325. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15326. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15327. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15328. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15329. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15330. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15331. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15332. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15333. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15334. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15335. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15336. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15337. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15338. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15339. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15340. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15341. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15342. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15343. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15344. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15345. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15346. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15347. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15348. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15349. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15350. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15351. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15352. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15353. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15354. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15355. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15356. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15357. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15358. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15359. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15360. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15361. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15362. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15363. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15364. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15365. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15366. (org-defkey org-mode-map "\C-m" 'org-return)
  15367. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15368. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15369. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15370. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15371. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15372. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15373. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15374. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15375. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15376. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15377. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15378. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15379. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15380. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15381. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15382. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15383. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15384. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15385. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15386. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15387. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15388. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15389. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15390. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15391. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15392. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15393. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15394. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15395. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15396. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15397. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15398. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15399. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15400. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15401. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15402. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15403. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15404. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15405. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15406. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15407. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15408. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15409. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15410. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15411. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15412. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15413. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15414. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15415. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15416. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15417. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15418. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15419. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15420. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15421. (when (featurep 'xemacs)
  15422. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15423. (defconst org-speed-commands-default
  15424. '(
  15425. ("Outline Navigation")
  15426. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15427. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15428. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15429. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15430. ("u" . (org-speed-move-safe 'outline-up-heading))
  15431. ("j" . org-goto)
  15432. ("g" . (org-refile t))
  15433. ("Outline Visibility")
  15434. ("c" . org-cycle)
  15435. ("C" . org-shifttab)
  15436. (" " . org-display-outline-path)
  15437. ("Outline Structure Editing")
  15438. ("U" . org-shiftmetaup)
  15439. ("D" . org-shiftmetadown)
  15440. ("r" . org-metaright)
  15441. ("l" . org-metaleft)
  15442. ("R" . org-shiftmetaright)
  15443. ("L" . org-shiftmetaleft)
  15444. ("i" . (progn (forward-char 1) (call-interactively
  15445. 'org-insert-heading-respect-content)))
  15446. ("^" . org-sort)
  15447. ("w" . org-refile)
  15448. ("a" . org-archive-subtree-default-with-confirmation)
  15449. ("." . org-mark-subtree)
  15450. ("Clock Commands")
  15451. ("I" . org-clock-in)
  15452. ("O" . org-clock-out)
  15453. ("Meta Data Editing")
  15454. ("t" . org-todo)
  15455. ("0" . (org-priority ?\ ))
  15456. ("1" . (org-priority ?A))
  15457. ("2" . (org-priority ?B))
  15458. ("3" . (org-priority ?C))
  15459. (";" . org-set-tags-command)
  15460. ("e" . org-set-effort)
  15461. ("Agenda Views etc")
  15462. ("v" . org-agenda)
  15463. ("/" . org-sparse-tree)
  15464. ("Misc")
  15465. ("o" . org-open-at-point)
  15466. ("?" . org-speed-command-help)
  15467. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15468. (">" . (org-agenda-remove-restriction-lock))
  15469. )
  15470. "The default speed commands.")
  15471. (defun org-print-speed-command (e)
  15472. (if (> (length (car e)) 1)
  15473. (progn
  15474. (princ "\n")
  15475. (princ (car e))
  15476. (princ "\n")
  15477. (princ (make-string (length (car e)) ?-))
  15478. (princ "\n"))
  15479. (princ (car e))
  15480. (princ " ")
  15481. (if (symbolp (cdr e))
  15482. (princ (symbol-name (cdr e)))
  15483. (prin1 (cdr e)))
  15484. (princ "\n")))
  15485. (defun org-speed-command-help ()
  15486. "Show the available speed commands."
  15487. (interactive)
  15488. (if (not org-use-speed-commands)
  15489. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15490. (with-output-to-temp-buffer "*Help*"
  15491. (princ "User-defined Speed commands\n===========================\n")
  15492. (mapc 'org-print-speed-command org-speed-commands-user)
  15493. (princ "\n")
  15494. (princ "Built-in Speed commands\n=======================\n")
  15495. (mapc 'org-print-speed-command org-speed-commands-default))
  15496. (with-current-buffer "*Help*"
  15497. (setq truncate-lines t))))
  15498. (defun org-speed-move-safe (cmd)
  15499. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15500. If not, return to the original position and throw an error."
  15501. (interactive)
  15502. (let ((pos (point)))
  15503. (call-interactively cmd)
  15504. (unless (and (bolp) (org-on-heading-p))
  15505. (goto-char pos)
  15506. (error "Boundary reached while executing %s" cmd))))
  15507. (defvar org-self-insert-command-undo-counter 0)
  15508. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15509. (defvar org-speed-command nil)
  15510. (defun org-speed-command-default-hook (keys)
  15511. "Hook for activating single-letter speed commands.
  15512. `org-speed-commands-default' specifies a minimal command set. Use
  15513. `org-speed-commands-user' for further customization."
  15514. (when (or (and (bolp) (looking-at org-outline-regexp))
  15515. (and (functionp org-use-speed-commands)
  15516. (funcall org-use-speed-commands)))
  15517. (cdr (assoc keys (append org-speed-commands-user
  15518. org-speed-commands-default)))))
  15519. (defun org-babel-speed-command-hook (keys)
  15520. "Hook for activating single-letter code block commands."
  15521. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15522. (cdr (assoc keys org-babel-key-bindings))))
  15523. (defcustom org-speed-command-hook
  15524. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15525. "Hook for activating speed commands at strategic locations.
  15526. Hook functions are called in sequence until a valid handler is
  15527. found.
  15528. Each hook takes a single argument, a user-pressed command key
  15529. which is also a `self-insert-command' from the global map.
  15530. Within the hook, examine the cursor position and the command key
  15531. and return nil or a valid handler as appropriate. Handler could
  15532. be one of an interactive command, a function, or a form.
  15533. Set `org-use-speed-commands' to non-nil value to enable this
  15534. hook. The default setting is `org-speed-command-default-hook'."
  15535. :group 'org-structure
  15536. :type 'hook)
  15537. (defun org-self-insert-command (N)
  15538. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15539. If the cursor is in a table looking at whitespace, the whitespace is
  15540. overwritten, and the table is not marked as requiring realignment."
  15541. (interactive "p")
  15542. (cond
  15543. ((and org-use-speed-commands
  15544. (setq org-speed-command
  15545. (run-hook-with-args-until-success
  15546. 'org-speed-command-hook (this-command-keys))))
  15547. (cond
  15548. ((commandp org-speed-command)
  15549. (setq this-command org-speed-command)
  15550. (call-interactively org-speed-command))
  15551. ((functionp org-speed-command)
  15552. (funcall org-speed-command))
  15553. ((and org-speed-command (listp org-speed-command))
  15554. (eval org-speed-command))
  15555. (t (let (org-use-speed-commands)
  15556. (call-interactively 'org-self-insert-command)))))
  15557. ((and
  15558. (org-table-p)
  15559. (progn
  15560. ;; check if we blank the field, and if that triggers align
  15561. (and (featurep 'org-table) org-table-auto-blank-field
  15562. (member last-command
  15563. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15564. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15565. ;; got extra space, this field does not determine column width
  15566. (let (org-table-may-need-update) (org-table-blank-field))
  15567. ;; no extra space, this field may determine column width
  15568. (org-table-blank-field)))
  15569. t)
  15570. (eq N 1)
  15571. (looking-at "[^|\n]* |"))
  15572. (let (org-table-may-need-update)
  15573. (goto-char (1- (match-end 0)))
  15574. (delete-char -1)
  15575. (goto-char (match-beginning 0))
  15576. (self-insert-command N)))
  15577. (t
  15578. (setq org-table-may-need-update t)
  15579. (self-insert-command N)
  15580. (org-fix-tags-on-the-fly)
  15581. (if org-self-insert-cluster-for-undo
  15582. (if (not (eq last-command 'org-self-insert-command))
  15583. (setq org-self-insert-command-undo-counter 1)
  15584. (if (>= org-self-insert-command-undo-counter 20)
  15585. (setq org-self-insert-command-undo-counter 1)
  15586. (and (> org-self-insert-command-undo-counter 0)
  15587. buffer-undo-list (listp buffer-undo-list)
  15588. (not (cadr buffer-undo-list)) ; remove nil entry
  15589. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15590. (setq org-self-insert-command-undo-counter
  15591. (1+ org-self-insert-command-undo-counter))))))))
  15592. (defun org-fix-tags-on-the-fly ()
  15593. (when (and (equal (char-after (point-at-bol)) ?*)
  15594. (org-on-heading-p))
  15595. (org-align-tags-here org-tags-column)))
  15596. (defun org-delete-backward-char (N)
  15597. "Like `delete-backward-char', insert whitespace at field end in tables.
  15598. When deleting backwards, in tables this function will insert whitespace in
  15599. front of the next \"|\" separator, to keep the table aligned. The table will
  15600. still be marked for re-alignment if the field did fill the entire column,
  15601. because, in this case the deletion might narrow the column."
  15602. (interactive "p")
  15603. (if (and (org-table-p)
  15604. (eq N 1)
  15605. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15606. (looking-at ".*?|"))
  15607. (let ((pos (point))
  15608. (noalign (looking-at "[^|\n\r]* |"))
  15609. (c org-table-may-need-update))
  15610. (backward-delete-char N)
  15611. (if (not overwrite-mode)
  15612. (progn
  15613. (skip-chars-forward "^|")
  15614. (insert " ")
  15615. (goto-char (1- pos))))
  15616. ;; noalign: if there were two spaces at the end, this field
  15617. ;; does not determine the width of the column.
  15618. (if noalign (setq org-table-may-need-update c)))
  15619. (backward-delete-char N)
  15620. (org-fix-tags-on-the-fly)))
  15621. (defun org-delete-char (N)
  15622. "Like `delete-char', but insert whitespace at field end in tables.
  15623. When deleting characters, in tables this function will insert whitespace in
  15624. front of the next \"|\" separator, to keep the table aligned. The table will
  15625. still be marked for re-alignment if the field did fill the entire column,
  15626. because, in this case the deletion might narrow the column."
  15627. (interactive "p")
  15628. (if (and (org-table-p)
  15629. (not (bolp))
  15630. (not (= (char-after) ?|))
  15631. (eq N 1))
  15632. (if (looking-at ".*?|")
  15633. (let ((pos (point))
  15634. (noalign (looking-at "[^|\n\r]* |"))
  15635. (c org-table-may-need-update))
  15636. (replace-match (concat
  15637. (substring (match-string 0) 1 -1)
  15638. " |"))
  15639. (goto-char pos)
  15640. ;; noalign: if there were two spaces at the end, this field
  15641. ;; does not determine the width of the column.
  15642. (if noalign (setq org-table-may-need-update c)))
  15643. (delete-char N))
  15644. (delete-char N)
  15645. (org-fix-tags-on-the-fly)))
  15646. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15647. (put 'org-self-insert-command 'delete-selection t)
  15648. (put 'orgtbl-self-insert-command 'delete-selection t)
  15649. (put 'org-delete-char 'delete-selection 'supersede)
  15650. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15651. (put 'org-yank 'delete-selection 'yank)
  15652. ;; Make `flyspell-mode' delay after some commands
  15653. (put 'org-self-insert-command 'flyspell-delayed t)
  15654. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15655. (put 'org-delete-char 'flyspell-delayed t)
  15656. (put 'org-delete-backward-char 'flyspell-delayed t)
  15657. ;; Make pabbrev-mode expand after org-mode commands
  15658. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15659. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15660. ;; How to do this: Measure non-white length of current string
  15661. ;; If equal to column width, we should realign.
  15662. (defun org-remap (map &rest commands)
  15663. "In MAP, remap the functions given in COMMANDS.
  15664. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15665. (let (new old)
  15666. (while commands
  15667. (setq old (pop commands) new (pop commands))
  15668. (if (fboundp 'command-remapping)
  15669. (org-defkey map (vector 'remap old) new)
  15670. (substitute-key-definition old new map global-map)))))
  15671. (when (eq org-enable-table-editor 'optimized)
  15672. ;; If the user wants maximum table support, we need to hijack
  15673. ;; some standard editing functions
  15674. (org-remap org-mode-map
  15675. 'self-insert-command 'org-self-insert-command
  15676. 'delete-char 'org-delete-char
  15677. 'delete-backward-char 'org-delete-backward-char)
  15678. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15679. (defvar org-ctrl-c-ctrl-c-hook nil
  15680. "Hook for functions attaching themselves to `C-c C-c'.
  15681. This can be used to add additional functionality to the C-c C-c key which
  15682. executes context-dependent commands.
  15683. Each function will be called with no arguments. The function must check
  15684. if the context is appropriate for it to act. If yes, it should do its
  15685. thing and then return a non-nil value. If the context is wrong,
  15686. just do nothing and return nil.")
  15687. (defvar org-tab-first-hook nil
  15688. "Hook for functions to attach themselves to TAB.
  15689. See `org-ctrl-c-ctrl-c-hook' for more information.
  15690. This hook runs as the first action when TAB is pressed, even before
  15691. `org-cycle' messes around with the `outline-regexp' to cater for
  15692. inline tasks and plain list item folding.
  15693. If any function in this hook returns t, any other actions that
  15694. would have been caused by TAB (such as table field motion or visibility
  15695. cycling) will not occur.")
  15696. (defvar org-tab-after-check-for-table-hook nil
  15697. "Hook for functions to attach themselves to TAB.
  15698. See `org-ctrl-c-ctrl-c-hook' for more information.
  15699. This hook runs after it has been established that the cursor is not in a
  15700. table, but before checking if the cursor is in a headline or if global cycling
  15701. should be done.
  15702. If any function in this hook returns t, not other actions like visibility
  15703. cycling will be done.")
  15704. (defvar org-tab-after-check-for-cycling-hook nil
  15705. "Hook for functions to attach themselves to TAB.
  15706. See `org-ctrl-c-ctrl-c-hook' for more information.
  15707. This hook runs after it has been established that not table field motion and
  15708. not visibility should be done because of current context. This is probably
  15709. the place where a package like yasnippets can hook in.")
  15710. (defvar org-tab-before-tab-emulation-hook nil
  15711. "Hook for functions to attach themselves to TAB.
  15712. See `org-ctrl-c-ctrl-c-hook' for more information.
  15713. This hook runs after every other options for TAB have been exhausted, but
  15714. before indentation and \t insertion takes place.")
  15715. (defvar org-metaleft-hook nil
  15716. "Hook for functions attaching themselves to `M-left'.
  15717. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15718. (defvar org-metaright-hook nil
  15719. "Hook for functions attaching themselves to `M-right'.
  15720. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15721. (defvar org-metaup-hook nil
  15722. "Hook for functions attaching themselves to `M-up'.
  15723. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15724. (defvar org-metadown-hook nil
  15725. "Hook for functions attaching themselves to `M-down'.
  15726. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15727. (defvar org-shiftmetaleft-hook nil
  15728. "Hook for functions attaching themselves to `M-S-left'.
  15729. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15730. (defvar org-shiftmetaright-hook nil
  15731. "Hook for functions attaching themselves to `M-S-right'.
  15732. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15733. (defvar org-shiftmetaup-hook nil
  15734. "Hook for functions attaching themselves to `M-S-up'.
  15735. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15736. (defvar org-shiftmetadown-hook nil
  15737. "Hook for functions attaching themselves to `M-S-down'.
  15738. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15739. (defvar org-metareturn-hook nil
  15740. "Hook for functions attaching themselves to `M-RET'.
  15741. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15742. (defvar org-shiftup-hook nil
  15743. "Hook for functions attaching themselves to `S-up'.
  15744. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15745. (defvar org-shiftup-final-hook nil
  15746. "Hook for functions attaching themselves to `S-up'.
  15747. This one runs after all other options except shift-select have been excluded.
  15748. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15749. (defvar org-shiftdown-hook nil
  15750. "Hook for functions attaching themselves to `S-down'.
  15751. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15752. (defvar org-shiftdown-final-hook nil
  15753. "Hook for functions attaching themselves to `S-down'.
  15754. This one runs after all other options except shift-select have been excluded.
  15755. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15756. (defvar org-shiftleft-hook nil
  15757. "Hook for functions attaching themselves to `S-left'.
  15758. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15759. (defvar org-shiftleft-final-hook nil
  15760. "Hook for functions attaching themselves to `S-left'.
  15761. This one runs after all other options except shift-select have been excluded.
  15762. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15763. (defvar org-shiftright-hook nil
  15764. "Hook for functions attaching themselves to `S-right'.
  15765. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15766. (defvar org-shiftright-final-hook nil
  15767. "Hook for functions attaching themselves to `S-right'.
  15768. This one runs after all other options except shift-select have been excluded.
  15769. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15770. (defun org-modifier-cursor-error ()
  15771. "Throw an error, a modified cursor command was applied in wrong context."
  15772. (error "This command is active in special context like tables, headlines or items"))
  15773. (defun org-shiftselect-error ()
  15774. "Throw an error because Shift-Cursor command was applied in wrong context."
  15775. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15776. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15777. (error "This command works only in special context like headlines or timestamps")))
  15778. (defun org-call-for-shift-select (cmd)
  15779. (let ((this-command-keys-shift-translated t))
  15780. (call-interactively cmd)))
  15781. (defun org-shifttab (&optional arg)
  15782. "Global visibility cycling or move to previous table field.
  15783. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15784. on context.
  15785. See the individual commands for more information."
  15786. (interactive "P")
  15787. (cond
  15788. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15789. ((integerp arg)
  15790. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15791. (message "Content view to level: %d" arg)
  15792. (org-content (prefix-numeric-value arg2))
  15793. (setq org-cycle-global-status 'overview)))
  15794. (t (call-interactively 'org-global-cycle))))
  15795. (defun org-shiftmetaleft ()
  15796. "Promote subtree or delete table column.
  15797. Calls `org-promote-subtree', `org-outdent-item',
  15798. or `org-table-delete-column', depending on context.
  15799. See the individual commands for more information."
  15800. (interactive)
  15801. (cond
  15802. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15803. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15804. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15805. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15806. (t (org-modifier-cursor-error))))
  15807. (defun org-shiftmetaright ()
  15808. "Demote subtree or insert table column.
  15809. Calls `org-demote-subtree', `org-indent-item',
  15810. or `org-table-insert-column', depending on context.
  15811. See the individual commands for more information."
  15812. (interactive)
  15813. (cond
  15814. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15815. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15816. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15817. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15818. (t (org-modifier-cursor-error))))
  15819. (defun org-shiftmetaup (&optional arg)
  15820. "Move subtree up or kill table row.
  15821. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15822. `org-move-item-up' depending on context. See the individual commands
  15823. for more information."
  15824. (interactive "P")
  15825. (cond
  15826. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15827. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15828. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15829. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15830. (t (org-modifier-cursor-error))))
  15831. (defun org-shiftmetadown (&optional arg)
  15832. "Move subtree down or insert table row.
  15833. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15834. `org-move-item-down', depending on context. See the individual
  15835. commands for more information."
  15836. (interactive "P")
  15837. (cond
  15838. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15839. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15840. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15841. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15842. (t (org-modifier-cursor-error))))
  15843. (defsubst org-hidden-tree-error ()
  15844. (error
  15845. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15846. (defun org-metaleft (&optional arg)
  15847. "Promote heading or move table column to left.
  15848. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15849. With no specific context, calls the Emacs default `backward-word'.
  15850. See the individual commands for more information."
  15851. (interactive "P")
  15852. (cond
  15853. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15854. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15855. ((org-with-limited-levels
  15856. (or (org-on-heading-p)
  15857. (and (org-region-active-p)
  15858. (save-excursion
  15859. (goto-char (region-beginning))
  15860. (org-on-heading-p)))))
  15861. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15862. (call-interactively 'org-do-promote))
  15863. ;; At an inline task.
  15864. ((org-on-heading-p)
  15865. (call-interactively 'org-inlinetask-promote))
  15866. ((or (org-at-item-p)
  15867. (and (org-region-active-p)
  15868. (save-excursion
  15869. (goto-char (region-beginning))
  15870. (org-at-item-p))))
  15871. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15872. (call-interactively 'org-outdent-item))
  15873. (t (call-interactively 'backward-word))))
  15874. (defun org-metaright (&optional arg)
  15875. "Demote subtree or move table column to right.
  15876. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15877. With no specific context, calls the Emacs default `forward-word'.
  15878. See the individual commands for more information."
  15879. (interactive "P")
  15880. (cond
  15881. ((run-hook-with-args-until-success 'org-metaright-hook))
  15882. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15883. ((org-with-limited-levels
  15884. (or (org-on-heading-p)
  15885. (and (org-region-active-p)
  15886. (save-excursion
  15887. (goto-char (region-beginning))
  15888. (org-on-heading-p)))))
  15889. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15890. (call-interactively 'org-do-demote))
  15891. ;; At an inline task.
  15892. ((org-on-heading-p)
  15893. (call-interactively 'org-inlinetask-demote))
  15894. ((or (org-at-item-p)
  15895. (and (org-region-active-p)
  15896. (save-excursion
  15897. (goto-char (region-beginning))
  15898. (org-at-item-p))))
  15899. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15900. (call-interactively 'org-indent-item))
  15901. (t (call-interactively 'forward-word))))
  15902. (defun org-check-for-hidden (what)
  15903. "Check if there are hidden headlines/items in the current visual line.
  15904. WHAT can be either `headlines' or `items'. If the current line is
  15905. an outline or item heading and it has a folded subtree below it,
  15906. this function returns t, nil otherwise."
  15907. (let ((re (cond
  15908. ((eq what 'headlines) org-outline-regexp-bol)
  15909. ((eq what 'items) (org-item-beginning-re))
  15910. (t (error "This should not happen"))))
  15911. beg end)
  15912. (save-excursion
  15913. (catch 'exit
  15914. (unless (org-region-active-p)
  15915. (setq beg (point-at-bol))
  15916. (beginning-of-line 2)
  15917. (while (and (not (eobp)) ;; this is like `next-line'
  15918. (get-char-property (1- (point)) 'invisible))
  15919. (beginning-of-line 2))
  15920. (setq end (point))
  15921. (goto-char beg)
  15922. (goto-char (point-at-eol))
  15923. (setq end (max end (point)))
  15924. (while (re-search-forward re end t)
  15925. (if (get-char-property (match-beginning 0) 'invisible)
  15926. (throw 'exit t))))
  15927. nil))))
  15928. (defun org-metaup (&optional arg)
  15929. "Move subtree up or move table row up.
  15930. Calls `org-move-subtree-up' or `org-table-move-row' or
  15931. `org-move-item-up', depending on context. See the individual commands
  15932. for more information."
  15933. (interactive "P")
  15934. (cond
  15935. ((run-hook-with-args-until-success 'org-metaup-hook))
  15936. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15937. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15938. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15939. (t (transpose-lines 1) (beginning-of-line -1))))
  15940. (defun org-metadown (&optional arg)
  15941. "Move subtree down or move table row down.
  15942. Calls `org-move-subtree-down' or `org-table-move-row' or
  15943. `org-move-item-down', depending on context. See the individual
  15944. commands for more information."
  15945. (interactive "P")
  15946. (cond
  15947. ((run-hook-with-args-until-success 'org-metadown-hook))
  15948. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15949. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15950. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15951. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15952. (defun org-shiftup (&optional arg)
  15953. "Increase item in timestamp or increase priority of current headline.
  15954. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15955. depending on context. See the individual commands for more information."
  15956. (interactive "P")
  15957. (cond
  15958. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15959. ((and org-support-shift-select (org-region-active-p))
  15960. (org-call-for-shift-select 'previous-line))
  15961. ((org-at-timestamp-p t)
  15962. (call-interactively (if org-edit-timestamp-down-means-later
  15963. 'org-timestamp-down 'org-timestamp-up)))
  15964. ((and (not (eq org-support-shift-select 'always))
  15965. org-enable-priority-commands
  15966. (org-on-heading-p))
  15967. (call-interactively 'org-priority-up))
  15968. ((and (not org-support-shift-select) (org-at-item-p))
  15969. (call-interactively 'org-previous-item))
  15970. ((org-clocktable-try-shift 'up arg))
  15971. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15972. (org-support-shift-select
  15973. (org-call-for-shift-select 'previous-line))
  15974. (t (org-shiftselect-error))))
  15975. (defun org-shiftdown (&optional arg)
  15976. "Decrease item in timestamp or decrease priority of current headline.
  15977. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15978. depending on context. See the individual commands for more information."
  15979. (interactive "P")
  15980. (cond
  15981. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15982. ((and org-support-shift-select (org-region-active-p))
  15983. (org-call-for-shift-select 'next-line))
  15984. ((org-at-timestamp-p t)
  15985. (call-interactively (if org-edit-timestamp-down-means-later
  15986. 'org-timestamp-up 'org-timestamp-down)))
  15987. ((and (not (eq org-support-shift-select 'always))
  15988. org-enable-priority-commands
  15989. (org-on-heading-p))
  15990. (call-interactively 'org-priority-down))
  15991. ((and (not org-support-shift-select) (org-at-item-p))
  15992. (call-interactively 'org-next-item))
  15993. ((org-clocktable-try-shift 'down arg))
  15994. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15995. (org-support-shift-select
  15996. (org-call-for-shift-select 'next-line))
  15997. (t (org-shiftselect-error))))
  15998. (defun org-shiftright (&optional arg)
  15999. "Cycle the thing at point or in the current line, depending on context.
  16000. Depending on context, this does one of the following:
  16001. - switch a timestamp at point one day into the future
  16002. - on a headline, switch to the next TODO keyword.
  16003. - on an item, switch entire list to the next bullet type
  16004. - on a property line, switch to the next allowed value
  16005. - on a clocktable definition line, move time block into the future"
  16006. (interactive "P")
  16007. (cond
  16008. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16009. ((and org-support-shift-select (org-region-active-p))
  16010. (org-call-for-shift-select 'forward-char))
  16011. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16012. ((and (not (eq org-support-shift-select 'always))
  16013. (org-on-heading-p))
  16014. (let ((org-inhibit-logging
  16015. (not org-treat-S-cursor-todo-selection-as-state-change))
  16016. (org-inhibit-blocking
  16017. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16018. (org-call-with-arg 'org-todo 'right)))
  16019. ((or (and org-support-shift-select
  16020. (not (eq org-support-shift-select 'always))
  16021. (org-at-item-bullet-p))
  16022. (and (not org-support-shift-select) (org-at-item-p)))
  16023. (org-call-with-arg 'org-cycle-list-bullet nil))
  16024. ((and (not (eq org-support-shift-select 'always))
  16025. (org-at-property-p))
  16026. (call-interactively 'org-property-next-allowed-value))
  16027. ((org-clocktable-try-shift 'right arg))
  16028. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16029. (org-support-shift-select
  16030. (org-call-for-shift-select 'forward-char))
  16031. (t (org-shiftselect-error))))
  16032. (defun org-shiftleft (&optional arg)
  16033. "Cycle the thing at point or in the current line, depending on context.
  16034. Depending on context, this does one of the following:
  16035. - switch a timestamp at point one day into the past
  16036. - on a headline, switch to the previous TODO keyword.
  16037. - on an item, switch entire list to the previous bullet type
  16038. - on a property line, switch to the previous allowed value
  16039. - on a clocktable definition line, move time block into the past"
  16040. (interactive "P")
  16041. (cond
  16042. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16043. ((and org-support-shift-select (org-region-active-p))
  16044. (org-call-for-shift-select 'backward-char))
  16045. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16046. ((and (not (eq org-support-shift-select 'always))
  16047. (org-on-heading-p))
  16048. (let ((org-inhibit-logging
  16049. (not org-treat-S-cursor-todo-selection-as-state-change))
  16050. (org-inhibit-blocking
  16051. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16052. (org-call-with-arg 'org-todo 'left)))
  16053. ((or (and org-support-shift-select
  16054. (not (eq org-support-shift-select 'always))
  16055. (org-at-item-bullet-p))
  16056. (and (not org-support-shift-select) (org-at-item-p)))
  16057. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16058. ((and (not (eq org-support-shift-select 'always))
  16059. (org-at-property-p))
  16060. (call-interactively 'org-property-previous-allowed-value))
  16061. ((org-clocktable-try-shift 'left arg))
  16062. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16063. (org-support-shift-select
  16064. (org-call-for-shift-select 'backward-char))
  16065. (t (org-shiftselect-error))))
  16066. (defun org-shiftcontrolright ()
  16067. "Switch to next TODO set."
  16068. (interactive)
  16069. (cond
  16070. ((and org-support-shift-select (org-region-active-p))
  16071. (org-call-for-shift-select 'forward-word))
  16072. ((and (not (eq org-support-shift-select 'always))
  16073. (org-on-heading-p))
  16074. (org-call-with-arg 'org-todo 'nextset))
  16075. (org-support-shift-select
  16076. (org-call-for-shift-select 'forward-word))
  16077. (t (org-shiftselect-error))))
  16078. (defun org-shiftcontrolleft ()
  16079. "Switch to previous TODO set."
  16080. (interactive)
  16081. (cond
  16082. ((and org-support-shift-select (org-region-active-p))
  16083. (org-call-for-shift-select 'backward-word))
  16084. ((and (not (eq org-support-shift-select 'always))
  16085. (org-on-heading-p))
  16086. (org-call-with-arg 'org-todo 'previousset))
  16087. (org-support-shift-select
  16088. (org-call-for-shift-select 'backward-word))
  16089. (t (org-shiftselect-error))))
  16090. (defun org-shiftcontrolup ()
  16091. "Change timestamps synchronously up in CLOCK log lines."
  16092. (interactive)
  16093. (cond ((and (not org-support-shift-select)
  16094. (org-at-clock-log-p)
  16095. (org-at-timestamp-p t))
  16096. (org-clock-timestamps-up))
  16097. (t (org-shiftselect-error))))
  16098. (defun org-shiftcontroldown ()
  16099. "Change timestamps synchronously down in CLOCK log lines."
  16100. (interactive)
  16101. (cond ((and (not org-support-shift-select)
  16102. (org-at-clock-log-p)
  16103. (org-at-timestamp-p t))
  16104. (org-clock-timestamps-down))
  16105. (t (org-shiftselect-error))))
  16106. (defun org-ctrl-c-ret ()
  16107. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16108. (interactive)
  16109. (cond
  16110. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16111. (t (call-interactively 'org-insert-heading))))
  16112. (defun org-copy-special ()
  16113. "Copy region in table or copy current subtree.
  16114. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16115. See the individual commands for more information."
  16116. (interactive)
  16117. (call-interactively
  16118. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16119. (defun org-cut-special ()
  16120. "Cut region in table or cut current subtree.
  16121. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16122. See the individual commands for more information."
  16123. (interactive)
  16124. (call-interactively
  16125. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16126. (defun org-paste-special (arg)
  16127. "Paste rectangular region into table, or past subtree relative to level.
  16128. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16129. See the individual commands for more information."
  16130. (interactive "P")
  16131. (if (org-at-table-p)
  16132. (org-table-paste-rectangle)
  16133. (org-paste-subtree arg)))
  16134. (defun org-edit-special (&optional arg)
  16135. "Call a special editor for the stuff at point.
  16136. When at a table, call the formula editor with `org-table-edit-formulas'.
  16137. When at the first line of an src example, call `org-edit-src-code'.
  16138. When in an #+include line, visit the include file. Otherwise call
  16139. `ffap' to visit the file at point."
  16140. (interactive)
  16141. ;; possibly prep session before editing source
  16142. (when arg
  16143. (let* ((info (org-babel-get-src-block-info))
  16144. (lang (nth 0 info))
  16145. (params (nth 2 info))
  16146. (session (cdr (assoc :session params))))
  16147. (when (and info session) ;; we are in a source-code block with a session
  16148. (funcall
  16149. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16150. (cond ;; proceed with `org-edit-special'
  16151. ((save-excursion
  16152. (beginning-of-line 1)
  16153. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16154. (find-file (org-trim (match-string 1))))
  16155. ((org-edit-src-code))
  16156. ((org-edit-fixed-width-region))
  16157. ((org-at-table.el-p)
  16158. (org-edit-src-code))
  16159. ((or (org-at-table-p)
  16160. (save-excursion
  16161. (beginning-of-line 1)
  16162. (looking-at "[ \t]*#\\+TBLFM:")))
  16163. (call-interactively 'org-table-edit-formulas))
  16164. (t (call-interactively 'ffap))))
  16165. (defun org-ctrl-c-ctrl-c (&optional arg)
  16166. "Set tags in headline, or update according to changed information at point.
  16167. This command does many different things, depending on context:
  16168. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16169. this is what we do.
  16170. - If the cursor is on a statistics cookie, update it.
  16171. - If the cursor is in a headline, prompt for tags and insert them
  16172. into the current line, aligned to `org-tags-column'. When called
  16173. with prefix arg, realign all tags in the current buffer.
  16174. - If the cursor is in one of the special #+KEYWORD lines, this
  16175. triggers scanning the buffer for these lines and updating the
  16176. information.
  16177. - If the cursor is inside a table, realign the table. This command
  16178. works even if the automatic table editor has been turned off.
  16179. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16180. the entire table.
  16181. - If the cursor is at a footnote reference or definition, jump to
  16182. the corresponding definition or references, respectively.
  16183. - If the cursor is a the beginning of a dynamic block, update it.
  16184. - If the current buffer is a capture buffer, close note and file it.
  16185. - If the cursor is on a <<<target>>>, update radio targets and
  16186. corresponding links in this buffer.
  16187. - If the cursor is on a numbered item in a plain list, renumber the
  16188. ordered list.
  16189. - If the cursor is on a checkbox, toggle it.
  16190. - If the cursor is on a code block, evaluate it. The variable
  16191. `org-confirm-babel-evaluate' can be used to control prompting
  16192. before code block evaluation, by default every code block
  16193. evaluation requires confirmation. Code block evaluation can be
  16194. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16195. (interactive "P")
  16196. (let ((org-enable-table-editor t))
  16197. (cond
  16198. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16199. org-occur-highlights
  16200. org-latex-fragment-image-overlays)
  16201. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16202. (org-remove-occur-highlights)
  16203. (org-remove-latex-fragment-image-overlays)
  16204. (message "Temporary highlights/overlays removed from current buffer"))
  16205. ((and (local-variable-p 'org-finish-function (current-buffer))
  16206. (fboundp org-finish-function))
  16207. (funcall org-finish-function))
  16208. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16209. ((or (looking-at org-property-start-re)
  16210. (org-at-property-p))
  16211. (call-interactively 'org-property-action))
  16212. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  16213. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16214. (or (org-on-heading-p) (org-at-item-p)))
  16215. (call-interactively 'org-update-statistics-cookies))
  16216. ((org-on-heading-p) (call-interactively 'org-set-tags))
  16217. ((org-at-table.el-p)
  16218. (message "Use C-c ' to edit table.el tables"))
  16219. ((org-at-table-p)
  16220. (org-table-maybe-eval-formula)
  16221. (if arg
  16222. (call-interactively 'org-table-recalculate)
  16223. (org-table-maybe-recalculate-line))
  16224. (call-interactively 'org-table-align)
  16225. (orgtbl-send-table 'maybe))
  16226. ((or (org-footnote-at-reference-p)
  16227. (org-footnote-at-definition-p))
  16228. (call-interactively 'org-footnote-action))
  16229. ((org-at-item-checkbox-p)
  16230. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16231. ;; list only if at top item.
  16232. (let* ((cbox (match-string 1))
  16233. (struct (org-list-struct))
  16234. (old-struct (copy-tree struct))
  16235. (parents (org-list-parents-alist struct))
  16236. (prevs (org-list-prevs-alist struct))
  16237. (orderedp (org-entry-get nil "ORDERED"))
  16238. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16239. block-item)
  16240. ;; Use a light version of `org-toggle-checkbox' to avoid
  16241. ;; computing list structure twice.
  16242. (org-list-set-checkbox (point-at-bol) struct
  16243. (cond
  16244. ((equal arg '(16)) "[-]")
  16245. ((equal arg '(4)) nil)
  16246. ((equal "[X]" cbox) "[ ]")
  16247. (t "[X]")))
  16248. (org-list-struct-fix-ind struct parents)
  16249. (org-list-struct-fix-bul struct prevs)
  16250. (setq block-item
  16251. (org-list-struct-fix-box struct parents prevs orderedp))
  16252. (when block-item
  16253. (message
  16254. "Checkboxes were removed due to unchecked box at line %d"
  16255. (org-current-line block-item)))
  16256. (org-list-struct-apply-struct struct old-struct)
  16257. (org-update-checkbox-count-maybe)
  16258. (when firstp (org-list-send-list 'maybe))))
  16259. ((org-at-item-p)
  16260. ;; Cursor at an item: repair list. Do checkbox related actions
  16261. ;; only if function was called with an argument. Send list only
  16262. ;; if at top item.
  16263. (let* ((struct (org-list-struct))
  16264. (old-struct (copy-tree struct))
  16265. (parents (org-list-parents-alist struct))
  16266. (prevs (org-list-prevs-alist struct))
  16267. (firstp (= (org-list-get-top-point struct) (point-at-bol))))
  16268. (org-list-struct-fix-ind struct parents)
  16269. (org-list-struct-fix-bul struct prevs)
  16270. (when arg
  16271. (org-list-set-checkbox (point-at-bol) struct "[ ]")
  16272. (org-list-struct-fix-box struct parents prevs))
  16273. (org-list-struct-apply-struct struct old-struct)
  16274. (when arg (org-update-checkbox-count-maybe))
  16275. (when firstp (org-list-send-list 'maybe))))
  16276. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16277. ;; Dynamic block
  16278. (beginning-of-line 1)
  16279. (save-excursion (org-update-dblock)))
  16280. ((save-excursion
  16281. (beginning-of-line 1)
  16282. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16283. (cond
  16284. ((equal (match-string 1) "TBLFM")
  16285. ;; Recalculate the table before this line
  16286. (save-excursion
  16287. (beginning-of-line 1)
  16288. (skip-chars-backward " \r\n\t")
  16289. (if (org-at-table-p)
  16290. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16291. (t
  16292. (let ((org-inhibit-startup-visibility-stuff t)
  16293. (org-startup-align-all-tables nil))
  16294. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16295. (message "Local setup has been refreshed"))))
  16296. ((org-clock-update-time-maybe))
  16297. (t (error "C-c C-c can do nothing useful at this location")))))
  16298. (defun org-mode-restart ()
  16299. "Restart Org-mode, to scan again for special lines.
  16300. Also updates the keyword regular expressions."
  16301. (interactive)
  16302. (org-mode)
  16303. (message "Org-mode restarted"))
  16304. (defun org-kill-note-or-show-branches ()
  16305. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16306. (interactive)
  16307. (if (not org-finish-function)
  16308. (progn
  16309. (hide-subtree)
  16310. (call-interactively 'show-branches))
  16311. (let ((org-note-abort t))
  16312. (funcall org-finish-function))))
  16313. (defun org-return (&optional indent)
  16314. "Goto next table row or insert a newline.
  16315. Calls `org-table-next-row' or `newline', depending on context.
  16316. See the individual commands for more information."
  16317. (interactive)
  16318. (cond
  16319. ((bobp) (if indent (newline-and-indent) (newline)))
  16320. ((org-at-table-p)
  16321. (org-table-justify-field-maybe)
  16322. (call-interactively 'org-table-next-row))
  16323. ;; when `newline-and-indent' is called within a list, make sure
  16324. ;; text moved stays inside the item.
  16325. ((and (org-in-item-p) indent)
  16326. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16327. (progn
  16328. (newline)
  16329. (org-indent-line-to (length (match-string 0))))
  16330. (let ((ind (org-get-indentation)))
  16331. (newline)
  16332. (if (org-looking-back org-list-end-re)
  16333. (org-indent-line-function)
  16334. (org-indent-line-to ind)))))
  16335. ((and org-return-follows-link
  16336. (eq (get-text-property (point) 'face) 'org-link))
  16337. (call-interactively 'org-open-at-point))
  16338. ((and (org-at-heading-p)
  16339. (looking-at
  16340. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  16341. (org-show-entry)
  16342. (end-of-line 1)
  16343. (newline))
  16344. (t (if indent (newline-and-indent) (newline)))))
  16345. (defun org-return-indent ()
  16346. "Goto next table row or insert a newline and indent.
  16347. Calls `org-table-next-row' or `newline-and-indent', depending on
  16348. context. See the individual commands for more information."
  16349. (interactive)
  16350. (org-return t))
  16351. (defun org-ctrl-c-star ()
  16352. "Compute table, or change heading status of lines.
  16353. Calls `org-table-recalculate' or `org-toggle-heading',
  16354. depending on context."
  16355. (interactive)
  16356. (cond
  16357. ((org-at-table-p)
  16358. (call-interactively 'org-table-recalculate))
  16359. (t
  16360. ;; Convert all lines in region to list items
  16361. (call-interactively 'org-toggle-heading))))
  16362. (defun org-ctrl-c-minus ()
  16363. "Insert separator line in table or modify bullet status of line.
  16364. Also turns a plain line or a region of lines into list items.
  16365. Calls `org-table-insert-hline', `org-toggle-item', or
  16366. `org-cycle-list-bullet', depending on context."
  16367. (interactive)
  16368. (cond
  16369. ((org-at-table-p)
  16370. (call-interactively 'org-table-insert-hline))
  16371. ((org-region-active-p)
  16372. (call-interactively 'org-toggle-item))
  16373. ((org-in-item-p)
  16374. (call-interactively 'org-cycle-list-bullet))
  16375. (t
  16376. (call-interactively 'org-toggle-item))))
  16377. (defun org-toggle-item (arg)
  16378. "Convert headings or normal lines to items, items to normal lines.
  16379. If there is no active region, only the current line is considered.
  16380. If the first non blank line in the region is an headline, convert
  16381. all headlines to items, shifting text accordingly.
  16382. If it is an item, convert all items to normal lines.
  16383. If it is normal text, change region into an item. With a prefix
  16384. argument ARG, change each line in region into an item."
  16385. (interactive "P")
  16386. (let ((shift-text
  16387. (function
  16388. ;; Shift text in current section to IND, from point to END.
  16389. ;; The function leaves point to END line.
  16390. (lambda (ind end)
  16391. (let ((min-i 1000) (end (copy-marker end)))
  16392. ;; First determine the minimum indentation (MIN-I) of
  16393. ;; the text.
  16394. (save-excursion
  16395. (catch 'exit
  16396. (while (< (point) end)
  16397. (let ((i (org-get-indentation)))
  16398. (cond
  16399. ;; Skip blank lines and inline tasks.
  16400. ((looking-at "^[ \t]*$"))
  16401. ((looking-at org-outline-regexp-bol))
  16402. ;; We can't find less than 0 indentation.
  16403. ((zerop i) (throw 'exit (setq min-i 0)))
  16404. ((< i min-i) (setq min-i i))))
  16405. (forward-line))))
  16406. ;; Then indent each line so that a line indented to
  16407. ;; MIN-I becomes indented to IND. Ignore blank lines
  16408. ;; and inline tasks in the process.
  16409. (let ((delta (- ind min-i)))
  16410. (while (< (point) end)
  16411. (unless (or (looking-at "^[ \t]*$")
  16412. (looking-at org-outline-regexp-bol))
  16413. (org-indent-line-to (+ (org-get-indentation) delta)))
  16414. (forward-line)))))))
  16415. (skip-blanks
  16416. (function
  16417. ;; Return beginning of first non-blank line, starting from
  16418. ;; line at POS.
  16419. (lambda (pos)
  16420. (save-excursion
  16421. (goto-char pos)
  16422. (skip-chars-forward " \r\t\n")
  16423. (point-at-bol)))))
  16424. beg end)
  16425. ;; Determine boundaries of changes.
  16426. (if (org-region-active-p)
  16427. (setq beg (funcall skip-blanks (region-beginning))
  16428. end (copy-marker (region-end)))
  16429. (setq beg (funcall skip-blanks (point-at-bol))
  16430. end (copy-marker (point-at-eol))))
  16431. ;; Depending on the starting line, choose an action on the text
  16432. ;; between BEG and END.
  16433. (org-with-limited-levels
  16434. (save-excursion
  16435. (goto-char beg)
  16436. (cond
  16437. ;; Case 1. Start at an item: de-itemize. Note that it only
  16438. ;; happens when a region is active: `org-ctrl-c-minus'
  16439. ;; would call `org-cycle-list-bullet' otherwise.
  16440. ((org-at-item-p)
  16441. (while (< (point) end)
  16442. (when (org-at-item-p)
  16443. (skip-chars-forward " \t")
  16444. (delete-region (point) (match-end 0)))
  16445. (forward-line)))
  16446. ;; Case 2. Start at an heading: convert to items.
  16447. ((org-on-heading-p)
  16448. (let* ((bul (org-list-bullet-string "-"))
  16449. (bul-len (length bul))
  16450. ;; Indentation of the first heading. It should be
  16451. ;; relative to the indentation of its parent, if any.
  16452. (start-ind (save-excursion
  16453. (cond
  16454. ((not org-adapt-indentation) 0)
  16455. ((not (outline-previous-heading)) 0)
  16456. (t (length (match-string 0))))))
  16457. ;; Level of first heading. Further headings will be
  16458. ;; compared to it to determine hierarchy in the list.
  16459. (ref-level (org-reduced-level (org-outline-level))))
  16460. (while (< (point) end)
  16461. (let* ((level (org-reduced-level (org-outline-level)))
  16462. (delta (max 0 (- level ref-level))))
  16463. ;; If current headline is less indented than the first
  16464. ;; one, set it as reference, in order to preserve
  16465. ;; subtrees.
  16466. (when (< level ref-level) (setq ref-level level))
  16467. (replace-match bul t t)
  16468. (org-indent-line-to (+ start-ind (* delta bul-len)))
  16469. ;; Ensure all text down to END (or SECTION-END) belongs
  16470. ;; to the newly created item.
  16471. (let ((section-end (save-excursion
  16472. (or (outline-next-heading) (point)))))
  16473. (forward-line)
  16474. (funcall shift-text
  16475. (+ start-ind (* (1+ delta) bul-len))
  16476. (min end section-end)))))))
  16477. ;; Case 3. Normal line with ARG: turn each non-item line into
  16478. ;; an item.
  16479. (arg
  16480. (while (< (point) end)
  16481. (unless (or (org-on-heading-p) (org-at-item-p))
  16482. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16483. (replace-match
  16484. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  16485. (forward-line)))
  16486. ;; Case 4. Normal line without ARG: make the first line of
  16487. ;; region an item, and shift indentation of others
  16488. ;; lines to set them as item's body.
  16489. (t (let* ((bul (org-list-bullet-string "-"))
  16490. (bul-len (length bul))
  16491. (ref-ind (org-get-indentation)))
  16492. (skip-chars-forward " \t")
  16493. (insert bul)
  16494. (forward-line)
  16495. (while (< (point) end)
  16496. ;; Ensure that lines less indented than first one
  16497. ;; still get included in item body.
  16498. (funcall shift-text
  16499. (+ ref-ind bul-len)
  16500. (min end (save-excursion (or (outline-next-heading)
  16501. (point)))))
  16502. (forward-line)))))))))
  16503. (defun org-toggle-heading (&optional nstars)
  16504. "Convert headings to normal text, or items or text to headings.
  16505. If there is no active region, only the current line is considered.
  16506. If the first non blank line is an headline, remove the stars from
  16507. all headlines in the region.
  16508. If it is a plain list item, turn all plain list items into headings.
  16509. If it is a normal line, turn each and every normal line (i.e. not
  16510. an heading or an item) in the region into a heading.
  16511. When converting a line into a heading, the number of stars is chosen
  16512. such that the lines become children of the current entry. However,
  16513. when a prefix argument is given, its value determines the number of
  16514. stars to add."
  16515. (interactive "P")
  16516. (let ((skip-blanks
  16517. (function
  16518. ;; Return beginning of first non-blank line, starting from
  16519. ;; line at POS.
  16520. (lambda (pos)
  16521. (save-excursion
  16522. (goto-char pos)
  16523. (skip-chars-forward " \r\t\n")
  16524. (point-at-bol)))))
  16525. beg end)
  16526. ;; Determine boundaries of changes. If region ends at a bol, do
  16527. ;; not consider the last line to be in the region.
  16528. (if (org-region-active-p)
  16529. (setq beg (funcall skip-blanks (region-beginning))
  16530. end (copy-marker (save-excursion
  16531. (goto-char (region-end))
  16532. (if (bolp) (point) (point-at-eol)))))
  16533. (setq beg (funcall skip-blanks (point-at-bol))
  16534. end (copy-marker (point-at-eol))))
  16535. ;; Ensure inline tasks don't count as headings.
  16536. (org-with-limited-levels
  16537. (save-excursion
  16538. (goto-char beg)
  16539. (cond
  16540. ;; Case 1. Started at an heading: de-star headings.
  16541. ((org-on-heading-p)
  16542. (while (< (point) end)
  16543. (when (org-on-heading-p t)
  16544. (looking-at org-outline-regexp) (replace-match ""))
  16545. (forward-line)))
  16546. ;; Case 2. Started at an item: change items into headlines.
  16547. ;; One star will be added by `org-list-to-subtree'.
  16548. ((org-at-item-p)
  16549. (let* ((stars (make-string
  16550. (if nstars
  16551. ;; subtract the star that will be added again by
  16552. ;; `org-list-to-subtree'
  16553. (1- (prefix-numeric-value current-prefix-arg))
  16554. (or (org-current-level) 0))
  16555. ?*))
  16556. (add-stars
  16557. (cond (nstars "") ; stars from prefix only
  16558. ((equal stars "") "") ; before first heading
  16559. (org-odd-levels-only "*") ; inside heading, odd
  16560. (t "")))) ; inside heading, oddeven
  16561. (while (< (point) end)
  16562. (when (org-at-item-p)
  16563. ;; Pay attention to cases when region ends before list.
  16564. (let* ((struct (org-list-struct))
  16565. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  16566. (save-restriction
  16567. (narrow-to-region (point) list-end)
  16568. (insert
  16569. (org-list-to-subtree
  16570. (org-list-parse-list t)
  16571. '(:istart (concat stars add-stars (funcall get-stars depth))
  16572. :icount (concat stars add-stars (funcall get-stars depth))))))))
  16573. (forward-line))))
  16574. ;; Case 3. Started at normal text: make every line an heading,
  16575. ;; skipping headlines and items.
  16576. (t (let* ((stars (make-string
  16577. (if nstars
  16578. (prefix-numeric-value current-prefix-arg)
  16579. (or (org-current-level) 0))
  16580. ?*))
  16581. (add-stars
  16582. (cond (nstars "") ; stars from prefix only
  16583. ((equal stars "") "*") ; before first heading
  16584. (org-odd-levels-only "**") ; inside heading, odd
  16585. (t "*"))) ; inside heading, oddeven
  16586. (rpl (concat stars add-stars " ")))
  16587. (while (< (point) end)
  16588. (when (and (not (org-on-heading-p)) (not (org-at-item-p))
  16589. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  16590. (replace-match (concat rpl (match-string 2))))
  16591. (forward-line)))))))))
  16592. (defun org-meta-return (&optional arg)
  16593. "Insert a new heading or wrap a region in a table.
  16594. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  16595. See the individual commands for more information."
  16596. (interactive "P")
  16597. (cond
  16598. ((run-hook-with-args-until-success 'org-metareturn-hook))
  16599. ((org-at-table-p)
  16600. (call-interactively 'org-table-wrap-region))
  16601. (t (call-interactively 'org-insert-heading))))
  16602. ;;; Menu entries
  16603. ;; Define the Org-mode menus
  16604. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  16605. '("Tbl"
  16606. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  16607. ["Next Field" org-cycle (org-at-table-p)]
  16608. ["Previous Field" org-shifttab (org-at-table-p)]
  16609. ["Next Row" org-return (org-at-table-p)]
  16610. "--"
  16611. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16612. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16613. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16614. "--"
  16615. ("Column"
  16616. ["Move Column Left" org-metaleft (org-at-table-p)]
  16617. ["Move Column Right" org-metaright (org-at-table-p)]
  16618. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16619. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16620. ("Row"
  16621. ["Move Row Up" org-metaup (org-at-table-p)]
  16622. ["Move Row Down" org-metadown (org-at-table-p)]
  16623. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16624. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16625. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16626. "--"
  16627. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16628. ("Rectangle"
  16629. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16630. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16631. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16632. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16633. "--"
  16634. ("Calculate"
  16635. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16636. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16637. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16638. "--"
  16639. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16640. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16641. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16642. "--"
  16643. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16644. "--"
  16645. ["Sum Column/Rectangle" org-table-sum
  16646. (or (org-at-table-p) (org-region-active-p))]
  16647. ["Which Column?" org-table-current-column (org-at-table-p)])
  16648. ["Debug Formulas"
  16649. org-table-toggle-formula-debugger
  16650. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16651. ["Show Col/Row Numbers"
  16652. org-table-toggle-coordinate-overlays
  16653. :style toggle
  16654. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16655. "--"
  16656. ["Create" org-table-create (and (not (org-at-table-p))
  16657. org-enable-table-editor)]
  16658. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16659. ["Import from File" org-table-import (not (org-at-table-p))]
  16660. ["Export to File" org-table-export (org-at-table-p)]
  16661. "--"
  16662. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16663. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16664. '("Org"
  16665. ("Show/Hide"
  16666. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16667. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16668. ["Sparse Tree..." org-sparse-tree t]
  16669. ["Reveal Context" org-reveal t]
  16670. ["Show All" show-all t]
  16671. "--"
  16672. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16673. "--"
  16674. ["New Heading" org-insert-heading t]
  16675. ("Navigate Headings"
  16676. ["Up" outline-up-heading t]
  16677. ["Next" outline-next-visible-heading t]
  16678. ["Previous" outline-previous-visible-heading t]
  16679. ["Next Same Level" outline-forward-same-level t]
  16680. ["Previous Same Level" outline-backward-same-level t]
  16681. "--"
  16682. ["Jump" org-goto t])
  16683. ("Edit Structure"
  16684. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16685. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16686. "--"
  16687. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16688. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16689. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16690. "--"
  16691. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16692. "--"
  16693. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16694. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16695. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16696. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16697. "--"
  16698. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16699. "--"
  16700. ["Convert to odd levels" org-convert-to-odd-levels t]
  16701. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16702. ("Editing"
  16703. ["Emphasis..." org-emphasize t]
  16704. ["Edit Source Example" org-edit-special t]
  16705. "--"
  16706. ["Footnote new/jump" org-footnote-action t]
  16707. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16708. ("Archive"
  16709. ["Archive (default method)" org-archive-subtree-default t]
  16710. "--"
  16711. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16712. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16713. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16714. )
  16715. "--"
  16716. ("Hyperlinks"
  16717. ["Store Link (Global)" org-store-link t]
  16718. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16719. ["Insert Link" org-insert-link t]
  16720. ["Follow Link" org-open-at-point t]
  16721. "--"
  16722. ["Next link" org-next-link t]
  16723. ["Previous link" org-previous-link t]
  16724. "--"
  16725. ["Descriptive Links"
  16726. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16727. :style radio
  16728. :selected (member '(org-link) buffer-invisibility-spec)]
  16729. ["Literal Links"
  16730. (progn
  16731. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16732. :style radio
  16733. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16734. "--"
  16735. ("TODO Lists"
  16736. ["TODO/DONE/-" org-todo t]
  16737. ("Select keyword"
  16738. ["Next keyword" org-shiftright (org-on-heading-p)]
  16739. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16740. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  16741. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16742. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16743. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16744. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16745. "--"
  16746. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16747. :selected org-enforce-todo-dependencies :style toggle :active t]
  16748. "Settings for tree at point"
  16749. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16750. :selected (org-entry-get nil "ORDERED")
  16751. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16752. ["Do Children parallel" org-toggle-ordered-property :style radio
  16753. :selected (not (org-entry-get nil "ORDERED"))
  16754. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16755. "--"
  16756. ["Set Priority" org-priority t]
  16757. ["Priority Up" org-shiftup t]
  16758. ["Priority Down" org-shiftdown t]
  16759. "--"
  16760. ["Get news from all feeds" org-feed-update-all t]
  16761. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16762. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16763. ("TAGS and Properties"
  16764. ["Set Tags" org-set-tags-command t]
  16765. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16766. "--"
  16767. ["Set property" org-set-property t]
  16768. ["Column view of properties" org-columns t]
  16769. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16770. ("Dates and Scheduling"
  16771. ["Timestamp" org-time-stamp t]
  16772. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16773. ("Change Date"
  16774. ["1 Day Later" org-shiftright t]
  16775. ["1 Day Earlier" org-shiftleft t]
  16776. ["1 ... Later" org-shiftup t]
  16777. ["1 ... Earlier" org-shiftdown t])
  16778. ["Compute Time Range" org-evaluate-time-range t]
  16779. ["Schedule Item" org-schedule t]
  16780. ["Deadline" org-deadline t]
  16781. "--"
  16782. ["Custom time format" org-toggle-time-stamp-overlays
  16783. :style radio :selected org-display-custom-times]
  16784. "--"
  16785. ["Goto Calendar" org-goto-calendar t]
  16786. ["Date from Calendar" org-date-from-calendar t]
  16787. "--"
  16788. ["Start/Restart Timer" org-timer-start t]
  16789. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16790. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16791. ["Insert Timer String" org-timer t]
  16792. ["Insert Timer Item" org-timer-item t])
  16793. ("Logging work"
  16794. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16795. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16796. ["Clock out" org-clock-out t]
  16797. ["Clock cancel" org-clock-cancel t]
  16798. "--"
  16799. ["Mark as default task" org-clock-mark-default-task t]
  16800. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16801. ["Goto running clock" org-clock-goto t]
  16802. "--"
  16803. ["Display times" org-clock-display t]
  16804. ["Create clock table" org-clock-report t]
  16805. "--"
  16806. ["Record DONE time"
  16807. (progn (setq org-log-done (not org-log-done))
  16808. (message "Switching to %s will %s record a timestamp"
  16809. (car org-done-keywords)
  16810. (if org-log-done "automatically" "not")))
  16811. :style toggle :selected org-log-done])
  16812. "--"
  16813. ["Agenda Command..." org-agenda t]
  16814. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16815. ("File List for Agenda")
  16816. ("Special views current file"
  16817. ["TODO Tree" org-show-todo-tree t]
  16818. ["Check Deadlines" org-check-deadlines t]
  16819. ["Timeline" org-timeline t]
  16820. ["Tags/Property tree" org-match-sparse-tree t])
  16821. "--"
  16822. ["Export/Publish..." org-export t]
  16823. ("LaTeX"
  16824. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16825. :selected org-cdlatex-mode]
  16826. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16827. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16828. ["Modify math symbol" org-cdlatex-math-modify
  16829. (org-inside-LaTeX-fragment-p)]
  16830. ["Insert citation" org-reftex-citation t]
  16831. "--"
  16832. ["Template for BEAMER" org-insert-beamer-options-template t])
  16833. "--"
  16834. ("MobileOrg"
  16835. ["Push Files and Views" org-mobile-push t]
  16836. ["Get Captured and Flagged" org-mobile-pull t]
  16837. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16838. "--"
  16839. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16840. "--"
  16841. ("Documentation"
  16842. ["Show Version" org-version t]
  16843. ["Info Documentation" org-info t])
  16844. ("Customize"
  16845. ["Browse Org Group" org-customize t]
  16846. "--"
  16847. ["Expand This Menu" org-create-customize-menu
  16848. (fboundp 'customize-menu-create)])
  16849. ["Send bug report" org-submit-bug-report t]
  16850. "--"
  16851. ("Refresh/Reload"
  16852. ["Refresh setup current buffer" org-mode-restart t]
  16853. ["Reload Org (after update)" org-reload t]
  16854. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16855. ))
  16856. (defun org-info (&optional node)
  16857. "Read documentation for Org-mode in the info system.
  16858. With optional NODE, go directly to that node."
  16859. (interactive)
  16860. (info (format "(org)%s" (or node ""))))
  16861. ;;;###autoload
  16862. (defun org-submit-bug-report ()
  16863. "Submit a bug report on Org-mode via mail.
  16864. Don't hesitate to report any problems or inaccurate documentation.
  16865. If you don't have setup sending mail from (X)Emacs, please copy the
  16866. output buffer into your mail program, as it gives us important
  16867. information about your Org-mode version and configuration."
  16868. (interactive)
  16869. (require 'reporter)
  16870. (org-load-modules-maybe)
  16871. (org-require-autoloaded-modules)
  16872. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16873. (reporter-submit-bug-report
  16874. "emacs-orgmode@gnu.org"
  16875. (org-version)
  16876. (let (list)
  16877. (save-window-excursion
  16878. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16879. (delete-other-windows)
  16880. (erase-buffer)
  16881. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16882. We would like to add your full Org-mode and Outline configuration to the
  16883. bug report. This greatly simplifies the work of the maintainer and
  16884. other experts on the mailing list.
  16885. HOWEVER, some variables you have customized may contain private
  16886. information. The names of customers, colleagues, or friends, might
  16887. appear in the form of file names, tags, todo states, or search strings.
  16888. If you answer yes to the prompt, you might want to check and remove
  16889. such private information before sending the email.")
  16890. (add-text-properties (point-min) (point-max) '(face org-warning))
  16891. (when (yes-or-no-p "Include your Org-mode configuration ")
  16892. (mapatoms
  16893. (lambda (v)
  16894. (and (boundp v)
  16895. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16896. (or (and (symbol-value v)
  16897. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16898. (and
  16899. (get v 'custom-type) (get v 'standard-value)
  16900. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16901. (push v list)))))
  16902. (kill-buffer (get-buffer "*Warn about privacy*"))
  16903. list))
  16904. nil nil
  16905. "Remember to cover the basics, that is, what you expected to happen and
  16906. what in fact did happen. You don't know how to make a good report? See
  16907. http://orgmode.org/manual/Feedback.html#Feedback
  16908. Your bug report will be posted to the Org-mode mailing list.
  16909. ------------------------------------------------------------------------")
  16910. (save-excursion
  16911. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16912. (replace-match "\\1Bug: \\3 [\\2]")))))
  16913. (defun org-install-agenda-files-menu ()
  16914. (let ((bl (buffer-list)))
  16915. (save-excursion
  16916. (while bl
  16917. (set-buffer (pop bl))
  16918. (if (org-mode-p) (setq bl nil)))
  16919. (when (org-mode-p)
  16920. (easy-menu-change
  16921. '("Org") "File List for Agenda"
  16922. (append
  16923. (list
  16924. ["Edit File List" (org-edit-agenda-file-list) t]
  16925. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16926. ["Remove Current File from List" org-remove-file t]
  16927. ["Cycle through agenda files" org-cycle-agenda-files t]
  16928. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16929. "--")
  16930. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16931. ;;;; Documentation
  16932. ;;;###autoload
  16933. (defun org-require-autoloaded-modules ()
  16934. (interactive)
  16935. (mapc 'require
  16936. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16937. org-docbook org-exp org-html org-icalendar
  16938. org-id org-latex
  16939. org-publish org-remember org-table
  16940. org-timer org-xoxo)))
  16941. ;;;###autoload
  16942. (defun org-reload (&optional uncompiled)
  16943. "Reload all org lisp files.
  16944. With prefix arg UNCOMPILED, load the uncompiled versions."
  16945. (interactive "P")
  16946. (require 'find-func)
  16947. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16948. (dir-org (file-name-directory (org-find-library-name "org")))
  16949. (dir-org-contrib (ignore-errors
  16950. (file-name-directory
  16951. (org-find-library-name "org-contribdir"))))
  16952. (babel-files
  16953. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16954. (append (list nil "comint" "eval" "exp" "keys"
  16955. "lob" "ref" "table" "tangle")
  16956. (delq nil
  16957. (mapcar
  16958. (lambda (lang)
  16959. (when (cdr lang) (symbol-name (car lang))))
  16960. org-babel-load-languages)))))
  16961. (files
  16962. (append (directory-files dir-org t file-re)
  16963. babel-files
  16964. (and dir-org-contrib
  16965. (directory-files dir-org-contrib t file-re))))
  16966. (remove-re (concat (if (featurep 'xemacs)
  16967. "org-colview" "org-colview-xemacs")
  16968. "\\'")))
  16969. (setq files (mapcar 'file-name-sans-extension files))
  16970. (setq files (mapcar
  16971. (lambda (x) (if (string-match remove-re x) nil x))
  16972. files))
  16973. (setq files (delq nil files))
  16974. (mapc
  16975. (lambda (f)
  16976. (when (featurep (intern (file-name-nondirectory f)))
  16977. (if (and (not uncompiled)
  16978. (file-exists-p (concat f ".elc")))
  16979. (load (concat f ".elc") nil nil t)
  16980. (load (concat f ".el") nil nil t))))
  16981. files))
  16982. (org-version))
  16983. ;;;###autoload
  16984. (defun org-customize ()
  16985. "Call the customize function with org as argument."
  16986. (interactive)
  16987. (org-load-modules-maybe)
  16988. (org-require-autoloaded-modules)
  16989. (customize-browse 'org))
  16990. (defun org-create-customize-menu ()
  16991. "Create a full customization menu for Org-mode, insert it into the menu."
  16992. (interactive)
  16993. (org-load-modules-maybe)
  16994. (org-require-autoloaded-modules)
  16995. (if (fboundp 'customize-menu-create)
  16996. (progn
  16997. (easy-menu-change
  16998. '("Org") "Customize"
  16999. `(["Browse Org group" org-customize t]
  17000. "--"
  17001. ,(customize-menu-create 'org)
  17002. ["Set" Custom-set t]
  17003. ["Save" Custom-save t]
  17004. ["Reset to Current" Custom-reset-current t]
  17005. ["Reset to Saved" Custom-reset-saved t]
  17006. ["Reset to Standard Settings" Custom-reset-standard t]))
  17007. (message "\"Org\"-menu now contains full customization menu"))
  17008. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17009. ;;;; Miscellaneous stuff
  17010. ;;; Generally useful functions
  17011. (defun org-get-at-bol (property)
  17012. "Get text property PROPERTY at beginning of line."
  17013. (get-text-property (point-at-bol) property))
  17014. (defun org-find-text-property-in-string (prop s)
  17015. "Return the first non-nil value of property PROP in string S."
  17016. (or (get-text-property 0 prop s)
  17017. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17018. prop s)))
  17019. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17020. "Display the given MESSAGE as a warning."
  17021. (if (fboundp 'display-warning)
  17022. (display-warning 'org message
  17023. (if (featurep 'xemacs) 'warning :warning))
  17024. (let ((buf (get-buffer-create "*Org warnings*")))
  17025. (with-current-buffer buf
  17026. (goto-char (point-max))
  17027. (insert "Warning (Org): " message)
  17028. (unless (bolp)
  17029. (newline)))
  17030. (display-buffer buf)
  17031. (sit-for 0))))
  17032. (defun org-eval (form)
  17033. "Eval FORM and return result."
  17034. (condition-case error
  17035. (eval form)
  17036. (error (format "%%![Error: %s]" error))))
  17037. (defun org-in-commented-line ()
  17038. "Is point in a line starting with `#'?"
  17039. (equal (char-after (point-at-bol)) ?#))
  17040. (defun org-in-indented-comment-line ()
  17041. "Is point in a line starting with `#' after some white space?"
  17042. (save-excursion
  17043. (save-match-data
  17044. (goto-char (point-at-bol))
  17045. (looking-at "[ \t]*#"))))
  17046. (defun org-in-verbatim-emphasis ()
  17047. (save-match-data
  17048. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17049. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17050. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17051. (if (and marker (marker-buffer marker)
  17052. (buffer-live-p (marker-buffer marker)))
  17053. (progn
  17054. (switch-to-buffer (marker-buffer marker))
  17055. (if (or (> marker (point-max)) (< marker (point-min)))
  17056. (widen))
  17057. (goto-char marker)
  17058. (org-show-context 'org-goto))
  17059. (if bookmark
  17060. (bookmark-jump bookmark)
  17061. (error "Cannot find location"))))
  17062. (defun org-quote-csv-field (s)
  17063. "Quote field for inclusion in CSV material."
  17064. (if (string-match "[\",]" s)
  17065. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17066. s))
  17067. (defun org-force-self-insert (N)
  17068. "Needed to enforce self-insert under remapping."
  17069. (interactive "p")
  17070. (self-insert-command N))
  17071. (defun org-string-width (s)
  17072. "Compute width of string, ignoring invisible characters.
  17073. This ignores character with invisibility property `org-link', and also
  17074. characters with property `org-cwidth', because these will become invisible
  17075. upon the next fontification round."
  17076. (let (b l)
  17077. (when (or (eq t buffer-invisibility-spec)
  17078. (assq 'org-link buffer-invisibility-spec))
  17079. (while (setq b (text-property-any 0 (length s)
  17080. 'invisible 'org-link s))
  17081. (setq s (concat (substring s 0 b)
  17082. (substring s (or (next-single-property-change
  17083. b 'invisible s) (length s)))))))
  17084. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17085. (setq s (concat (substring s 0 b)
  17086. (substring s (or (next-single-property-change
  17087. b 'org-cwidth s) (length s))))))
  17088. (setq l (string-width s) b -1)
  17089. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17090. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17091. l))
  17092. (defun org-shorten-string (s maxlength)
  17093. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17094. If the string is shorter or has length MAXLENGTH, just return the
  17095. original string. If it is longer, the functions finds a space in the
  17096. string, breaks this string off at that locations and adds three dots
  17097. as ellipsis. Including the ellipsis, the string will not be longer
  17098. than MAXLENGTH. If finding a good breaking point in the string does
  17099. not work, the string is just chopped off in the middle of a word
  17100. if necessary."
  17101. (if (<= (length s) maxlength)
  17102. s
  17103. (let* ((n (max (- maxlength 4) 1))
  17104. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17105. (if (string-match re s)
  17106. (concat (match-string 1 s) "...")
  17107. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17108. (defun org-get-indentation (&optional line)
  17109. "Get the indentation of the current line, interpreting tabs.
  17110. When LINE is given, assume it represents a line and compute its indentation."
  17111. (if line
  17112. (if (string-match "^ *" (org-remove-tabs line))
  17113. (match-end 0))
  17114. (save-excursion
  17115. (beginning-of-line 1)
  17116. (skip-chars-forward " \t")
  17117. (current-column))))
  17118. (defun org-get-string-indentation (s)
  17119. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17120. (let ((n -1) (i 0) (w tab-width) c)
  17121. (catch 'exit
  17122. (while (< (setq n (1+ n)) (length s))
  17123. (setq c (aref s n))
  17124. (cond ((= c ?\ ) (setq i (1+ i)))
  17125. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17126. (t (throw 'exit t)))))
  17127. i))
  17128. (defun org-remove-tabs (s &optional width)
  17129. "Replace tabulators in S with spaces.
  17130. Assumes that s is a single line, starting in column 0."
  17131. (setq width (or width tab-width))
  17132. (while (string-match "\t" s)
  17133. (setq s (replace-match
  17134. (make-string
  17135. (- (* width (/ (+ (match-beginning 0) width) width))
  17136. (match-beginning 0)) ?\ )
  17137. t t s)))
  17138. s)
  17139. (defun org-fix-indentation (line ind)
  17140. "Fix indentation in LINE.
  17141. IND is a cons cell with target and minimum indentation.
  17142. If the current indentation in LINE is smaller than the minimum,
  17143. leave it alone. If it is larger than ind, set it to the target."
  17144. (let* ((l (org-remove-tabs line))
  17145. (i (org-get-indentation l))
  17146. (i1 (car ind)) (i2 (cdr ind)))
  17147. (if (>= i i2) (setq l (substring line i2)))
  17148. (if (> i1 0)
  17149. (concat (make-string i1 ?\ ) l)
  17150. l)))
  17151. (defun org-remove-indentation (code &optional n)
  17152. "Remove the maximum common indentation from the lines in CODE.
  17153. N may optionally be the number of spaces to remove."
  17154. (with-temp-buffer
  17155. (insert code)
  17156. (org-do-remove-indentation n)
  17157. (buffer-string)))
  17158. (defun org-do-remove-indentation (&optional n)
  17159. "Remove the maximum common indentation from the buffer."
  17160. (untabify (point-min) (point-max))
  17161. (let ((min 10000) re)
  17162. (if n
  17163. (setq min n)
  17164. (goto-char (point-min))
  17165. (while (re-search-forward "^ *[^ \n]" nil t)
  17166. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17167. (unless (or (= min 0) (= min 10000))
  17168. (setq re (format "^ \\{%d\\}" min))
  17169. (goto-char (point-min))
  17170. (while (re-search-forward re nil t)
  17171. (replace-match "")
  17172. (end-of-line 1))
  17173. min)))
  17174. (defun org-fill-template (template alist)
  17175. "Find each %key of ALIST in TEMPLATE and replace it."
  17176. (let ((case-fold-search nil)
  17177. entry key value)
  17178. (setq alist (sort (copy-sequence alist)
  17179. (lambda (a b) (< (length (car a)) (length (car b))))))
  17180. (while (setq entry (pop alist))
  17181. (setq template
  17182. (replace-regexp-in-string
  17183. (concat "%" (regexp-quote (car entry)))
  17184. (cdr entry) template t t)))
  17185. template))
  17186. (defun org-base-buffer (buffer)
  17187. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17188. (if (not buffer)
  17189. buffer
  17190. (or (buffer-base-buffer buffer)
  17191. buffer)))
  17192. (defun org-trim (s)
  17193. "Remove whitespace at beginning and end of string."
  17194. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17195. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17196. s)
  17197. (defun org-wrap (string &optional width lines)
  17198. "Wrap string to either a number of lines, or a width in characters.
  17199. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17200. that costs. If there is a word longer than WIDTH, the text is actually
  17201. wrapped to the length of that word.
  17202. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17203. many lines, whatever width that takes.
  17204. The return value is a list of lines, without newlines at the end."
  17205. (let* ((words (org-split-string string "[ \t\n]+"))
  17206. (maxword (apply 'max (mapcar 'org-string-width words)))
  17207. w ll)
  17208. (cond (width
  17209. (org-do-wrap words (max maxword width)))
  17210. (lines
  17211. (setq w maxword)
  17212. (setq ll (org-do-wrap words maxword))
  17213. (if (<= (length ll) lines)
  17214. ll
  17215. (setq ll words)
  17216. (while (> (length ll) lines)
  17217. (setq w (1+ w))
  17218. (setq ll (org-do-wrap words w)))
  17219. ll))
  17220. (t (error "Cannot wrap this")))))
  17221. (defun org-do-wrap (words width)
  17222. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17223. (let (lines line)
  17224. (while words
  17225. (setq line (pop words))
  17226. (while (and words (< (+ (length line) (length (car words))) width))
  17227. (setq line (concat line " " (pop words))))
  17228. (setq lines (push line lines)))
  17229. (nreverse lines)))
  17230. (defun org-split-string (string &optional separators)
  17231. "Splits STRING into substrings at SEPARATORS.
  17232. No empty strings are returned if there are matches at the beginning
  17233. and end of string."
  17234. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17235. (start 0)
  17236. notfirst
  17237. (list nil))
  17238. (while (and (string-match rexp string
  17239. (if (and notfirst
  17240. (= start (match-beginning 0))
  17241. (< start (length string)))
  17242. (1+ start) start))
  17243. (< (match-beginning 0) (length string)))
  17244. (setq notfirst t)
  17245. (or (eq (match-beginning 0) 0)
  17246. (and (eq (match-beginning 0) (match-end 0))
  17247. (eq (match-beginning 0) start))
  17248. (setq list
  17249. (cons (substring string start (match-beginning 0))
  17250. list)))
  17251. (setq start (match-end 0)))
  17252. (or (eq start (length string))
  17253. (setq list
  17254. (cons (substring string start)
  17255. list)))
  17256. (nreverse list)))
  17257. (defun org-quote-vert (s)
  17258. "Replace \"|\" with \"\\vert\"."
  17259. (while (string-match "|" s)
  17260. (setq s (replace-match "\\vert" t t s)))
  17261. s)
  17262. (defun org-uuidgen-p (s)
  17263. "Is S an ID created by UUIDGEN?"
  17264. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17265. (defun org-context ()
  17266. "Return a list of contexts of the current cursor position.
  17267. If several contexts apply, all are returned.
  17268. Each context entry is a list with a symbol naming the context, and
  17269. two positions indicating start and end of the context. Possible
  17270. contexts are:
  17271. :headline anywhere in a headline
  17272. :headline-stars on the leading stars in a headline
  17273. :todo-keyword on a TODO keyword (including DONE) in a headline
  17274. :tags on the TAGS in a headline
  17275. :priority on the priority cookie in a headline
  17276. :item on the first line of a plain list item
  17277. :item-bullet on the bullet/number of a plain list item
  17278. :checkbox on the checkbox in a plain list item
  17279. :table in an org-mode table
  17280. :table-special on a special filed in a table
  17281. :table-table in a table.el table
  17282. :link on a hyperlink
  17283. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17284. :target on a <<target>>
  17285. :radio-target on a <<<radio-target>>>
  17286. :latex-fragment on a LaTeX fragment
  17287. :latex-preview on a LaTeX fragment with overlayed preview image
  17288. This function expects the position to be visible because it uses font-lock
  17289. faces as a help to recognize the following contexts: :table-special, :link,
  17290. and :keyword."
  17291. (let* ((f (get-text-property (point) 'face))
  17292. (faces (if (listp f) f (list f)))
  17293. (p (point)) clist o)
  17294. ;; First the large context
  17295. (cond
  17296. ((org-on-heading-p t)
  17297. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17298. (when (progn
  17299. (beginning-of-line 1)
  17300. (looking-at org-todo-line-tags-regexp))
  17301. (push (org-point-in-group p 1 :headline-stars) clist)
  17302. (push (org-point-in-group p 2 :todo-keyword) clist)
  17303. (push (org-point-in-group p 4 :tags) clist))
  17304. (goto-char p)
  17305. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17306. (if (looking-at "\\[#[A-Z0-9]\\]")
  17307. (push (org-point-in-group p 0 :priority) clist)))
  17308. ((org-at-item-p)
  17309. (push (org-point-in-group p 2 :item-bullet) clist)
  17310. (push (list :item (point-at-bol)
  17311. (save-excursion (org-end-of-item) (point)))
  17312. clist)
  17313. (and (org-at-item-checkbox-p)
  17314. (push (org-point-in-group p 0 :checkbox) clist)))
  17315. ((org-at-table-p)
  17316. (push (list :table (org-table-begin) (org-table-end)) clist)
  17317. (if (memq 'org-formula faces)
  17318. (push (list :table-special
  17319. (previous-single-property-change p 'face)
  17320. (next-single-property-change p 'face)) clist)))
  17321. ((org-at-table-p 'any)
  17322. (push (list :table-table) clist)))
  17323. (goto-char p)
  17324. ;; Now the small context
  17325. (cond
  17326. ((org-at-timestamp-p)
  17327. (push (org-point-in-group p 0 :timestamp) clist))
  17328. ((memq 'org-link faces)
  17329. (push (list :link
  17330. (previous-single-property-change p 'face)
  17331. (next-single-property-change p 'face)) clist))
  17332. ((memq 'org-special-keyword faces)
  17333. (push (list :keyword
  17334. (previous-single-property-change p 'face)
  17335. (next-single-property-change p 'face)) clist))
  17336. ((org-on-target-p)
  17337. (push (org-point-in-group p 0 :target) clist)
  17338. (goto-char (1- (match-beginning 0)))
  17339. (if (looking-at org-radio-target-regexp)
  17340. (push (org-point-in-group p 0 :radio-target) clist))
  17341. (goto-char p))
  17342. ((setq o (car (delq nil
  17343. (mapcar
  17344. (lambda (x)
  17345. (if (memq x org-latex-fragment-image-overlays) x))
  17346. (overlays-at (point))))))
  17347. (push (list :latex-fragment
  17348. (overlay-start o) (overlay-end o)) clist)
  17349. (push (list :latex-preview
  17350. (overlay-start o) (overlay-end o)) clist))
  17351. ((org-inside-LaTeX-fragment-p)
  17352. ;; FIXME: positions wrong.
  17353. (push (list :latex-fragment (point) (point)) clist)))
  17354. (setq clist (nreverse (delq nil clist)))
  17355. clist))
  17356. ;; FIXME: Compare with at-regexp-p Do we need both?
  17357. (defun org-in-regexp (re &optional nlines visually)
  17358. "Check if point is inside a match of regexp.
  17359. Normally only the current line is checked, but you can include NLINES extra
  17360. lines both before and after point into the search.
  17361. If VISUALLY is set, require that the cursor is not after the match but
  17362. really on, so that the block visually is on the match."
  17363. (catch 'exit
  17364. (let ((pos (point))
  17365. (eol (point-at-eol (+ 1 (or nlines 0))))
  17366. (inc (if visually 1 0)))
  17367. (save-excursion
  17368. (beginning-of-line (- 1 (or nlines 0)))
  17369. (while (re-search-forward re eol t)
  17370. (if (and (<= (match-beginning 0) pos)
  17371. (>= (+ inc (match-end 0)) pos))
  17372. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  17373. (defun org-at-regexp-p (regexp)
  17374. "Is point inside a match of REGEXP in the current line?"
  17375. (catch 'exit
  17376. (save-excursion
  17377. (let ((pos (point)) (end (point-at-eol)))
  17378. (beginning-of-line 1)
  17379. (while (re-search-forward regexp end t)
  17380. (if (and (<= (match-beginning 0) pos)
  17381. (>= (match-end 0) pos))
  17382. (throw 'exit t)))
  17383. nil))))
  17384. (defun org-in-regexps-block-p (start-re end-re &optional bound)
  17385. "Return t if the current point is between matches of START-RE and END-RE.
  17386. This will also return t if point is on one of the two matches or
  17387. in an unfinished block. END-RE can be a string or a form
  17388. returning a string.
  17389. An optional third argument bounds the search for START-RE. It
  17390. defaults to previous heading or `point-min'."
  17391. (let ((pos (point))
  17392. (limit (or bound (save-excursion (outline-previous-heading)))))
  17393. (save-excursion
  17394. ;; we're on a block when point is on start-re...
  17395. (or (org-at-regexp-p start-re)
  17396. ;; ... or start-re can be found above...
  17397. (and (re-search-backward start-re limit t)
  17398. ;; ... but no end-re between start-re and point.
  17399. (not (re-search-forward (eval end-re) pos t)))))))
  17400. (defun org-in-block-p (names)
  17401. "Is point inside any block whose name belongs to NAMES?
  17402. NAMES is a list of strings containing names of blocks."
  17403. (save-match-data
  17404. (catch 'exit
  17405. (let ((case-fold-search t))
  17406. (mapc (lambda (name)
  17407. (let ((n (regexp-quote name)))
  17408. (when (org-in-regexps-block-p
  17409. (concat "^[ \t]*#\\+begin_" n)
  17410. (concat "^[ \t]*#\\+end_" n))
  17411. (throw 'exit t))))
  17412. names))
  17413. nil)))
  17414. (defun org-occur-in-agenda-files (regexp &optional nlines)
  17415. "Call `multi-occur' with buffers for all agenda files."
  17416. (interactive "sOrg-files matching: \np")
  17417. (let* ((files (org-agenda-files))
  17418. (tnames (mapcar 'file-truename files))
  17419. (extra org-agenda-text-search-extra-files)
  17420. f)
  17421. (when (eq (car extra) 'agenda-archives)
  17422. (setq extra (cdr extra))
  17423. (setq files (org-add-archive-files files)))
  17424. (while (setq f (pop extra))
  17425. (unless (member (file-truename f) tnames)
  17426. (add-to-list 'files f 'append)
  17427. (add-to-list 'tnames (file-truename f) 'append)))
  17428. (multi-occur
  17429. (mapcar (lambda (x)
  17430. (with-current-buffer
  17431. (or (get-file-buffer x) (find-file-noselect x))
  17432. (widen)
  17433. (current-buffer)))
  17434. files)
  17435. regexp)))
  17436. (if (boundp 'occur-mode-find-occurrence-hook)
  17437. ;; Emacs 23
  17438. (add-hook 'occur-mode-find-occurrence-hook
  17439. (lambda ()
  17440. (when (org-mode-p)
  17441. (org-reveal))))
  17442. ;; Emacs 22
  17443. (defadvice occur-mode-goto-occurrence
  17444. (after org-occur-reveal activate)
  17445. (and (org-mode-p) (org-reveal)))
  17446. (defadvice occur-mode-goto-occurrence-other-window
  17447. (after org-occur-reveal activate)
  17448. (and (org-mode-p) (org-reveal)))
  17449. (defadvice occur-mode-display-occurrence
  17450. (after org-occur-reveal activate)
  17451. (when (org-mode-p)
  17452. (let ((pos (occur-mode-find-occurrence)))
  17453. (with-current-buffer (marker-buffer pos)
  17454. (save-excursion
  17455. (goto-char pos)
  17456. (org-reveal)))))))
  17457. (defun org-occur-link-in-agenda-files ()
  17458. "Create a link and search for it in the agendas.
  17459. The link is not stored in `org-stored-links', it is just created
  17460. for the search purpose."
  17461. (interactive)
  17462. (let ((link (condition-case nil
  17463. (org-store-link nil)
  17464. (error "Unable to create a link to here"))))
  17465. (org-occur-in-agenda-files (regexp-quote link))))
  17466. (defun org-uniquify (list)
  17467. "Remove duplicate elements from LIST."
  17468. (let (res)
  17469. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  17470. res))
  17471. (defun org-delete-all (elts list)
  17472. "Remove all elements in ELTS from LIST."
  17473. (while elts
  17474. (setq list (delete (pop elts) list)))
  17475. list)
  17476. (defun org-count (cl-item cl-seq)
  17477. "Count the number of occurrences of ITEM in SEQ.
  17478. Taken from `count' in cl-seq.el with all keyword arguments removed."
  17479. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  17480. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  17481. (while (< cl-start cl-end)
  17482. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  17483. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  17484. (setq cl-start (1+ cl-start)))
  17485. cl-count))
  17486. (defun org-remove-if (predicate seq)
  17487. "Remove everything from SEQ that fulfills PREDICATE."
  17488. (let (res e)
  17489. (while seq
  17490. (setq e (pop seq))
  17491. (if (not (funcall predicate e)) (push e res)))
  17492. (nreverse res)))
  17493. (defun org-remove-if-not (predicate seq)
  17494. "Remove everything from SEQ that does not fulfill PREDICATE."
  17495. (let (res e)
  17496. (while seq
  17497. (setq e (pop seq))
  17498. (if (funcall predicate e) (push e res)))
  17499. (nreverse res)))
  17500. (defun org-back-over-empty-lines ()
  17501. "Move backwards over whitespace, to the beginning of the first empty line.
  17502. Returns the number of empty lines passed."
  17503. (let ((pos (point)))
  17504. (if (cdr (assoc 'heading org-blank-before-new-entry))
  17505. (skip-chars-backward " \t\n\r")
  17506. (forward-line -1))
  17507. (beginning-of-line 2)
  17508. (goto-char (min (point) pos))
  17509. (count-lines (point) pos)))
  17510. (defun org-skip-whitespace ()
  17511. (skip-chars-forward " \t\n\r"))
  17512. (defun org-point-in-group (point group &optional context)
  17513. "Check if POINT is in match-group GROUP.
  17514. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  17515. match. If the match group does not exist or point is not inside it,
  17516. return nil."
  17517. (and (match-beginning group)
  17518. (>= point (match-beginning group))
  17519. (<= point (match-end group))
  17520. (if context
  17521. (list context (match-beginning group) (match-end group))
  17522. t)))
  17523. (defun org-switch-to-buffer-other-window (&rest args)
  17524. "Switch to buffer in a second window on the current frame.
  17525. In particular, do not allow pop-up frames.
  17526. Returns the newly created buffer."
  17527. (let (pop-up-frames special-display-buffer-names special-display-regexps
  17528. special-display-function)
  17529. (apply 'switch-to-buffer-other-window args)))
  17530. (defun org-combine-plists (&rest plists)
  17531. "Create a single property list from all plists in PLISTS.
  17532. The process starts by copying the first list, and then setting properties
  17533. from the other lists. Settings in the last list are the most significant
  17534. ones and overrule settings in the other lists."
  17535. (let ((rtn (copy-sequence (pop plists)))
  17536. p v ls)
  17537. (while plists
  17538. (setq ls (pop plists))
  17539. (while ls
  17540. (setq p (pop ls) v (pop ls))
  17541. (setq rtn (plist-put rtn p v))))
  17542. rtn))
  17543. (defun org-move-line-down (arg)
  17544. "Move the current line down. With prefix argument, move it past ARG lines."
  17545. (interactive "p")
  17546. (let ((col (current-column))
  17547. beg end pos)
  17548. (beginning-of-line 1) (setq beg (point))
  17549. (beginning-of-line 2) (setq end (point))
  17550. (beginning-of-line (+ 1 arg))
  17551. (setq pos (move-marker (make-marker) (point)))
  17552. (insert (delete-and-extract-region beg end))
  17553. (goto-char pos)
  17554. (org-move-to-column col)))
  17555. (defun org-move-line-up (arg)
  17556. "Move the current line up. With prefix argument, move it past ARG lines."
  17557. (interactive "p")
  17558. (let ((col (current-column))
  17559. beg end pos)
  17560. (beginning-of-line 1) (setq beg (point))
  17561. (beginning-of-line 2) (setq end (point))
  17562. (beginning-of-line (- arg))
  17563. (setq pos (move-marker (make-marker) (point)))
  17564. (insert (delete-and-extract-region beg end))
  17565. (goto-char pos)
  17566. (org-move-to-column col)))
  17567. (defun org-replace-escapes (string table)
  17568. "Replace %-escapes in STRING with values in TABLE.
  17569. TABLE is an association list with keys like \"%a\" and string values.
  17570. The sequences in STRING may contain normal field width and padding information,
  17571. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  17572. so values can contain further %-escapes if they are define later in TABLE."
  17573. (let ((tbl (copy-alist table))
  17574. (case-fold-search nil)
  17575. (pchg 0)
  17576. e re rpl)
  17577. (while (setq e (pop tbl))
  17578. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  17579. (when (and (cdr e) (string-match re (cdr e)))
  17580. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  17581. (safe "SREF"))
  17582. (add-text-properties 0 3 (list 'sref sref) safe)
  17583. (setcdr e (replace-match safe t t (cdr e)))))
  17584. (while (string-match re string)
  17585. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  17586. (cdr e)))
  17587. (setq string (replace-match rpl t t string))))
  17588. (while (setq pchg (next-property-change pchg string))
  17589. (let ((sref (get-text-property pchg 'sref string)))
  17590. (when (and sref (string-match "SREF" string pchg))
  17591. (setq string (replace-match sref t t string)))))
  17592. string))
  17593. (defun org-sublist (list start end)
  17594. "Return a section of LIST, from START to END.
  17595. Counting starts at 1."
  17596. (let (rtn (c start))
  17597. (setq list (nthcdr (1- start) list))
  17598. (while (and list (<= c end))
  17599. (push (pop list) rtn)
  17600. (setq c (1+ c)))
  17601. (nreverse rtn)))
  17602. (defun org-find-base-buffer-visiting (file)
  17603. "Like `find-buffer-visiting' but always return the base buffer and
  17604. not an indirect buffer."
  17605. (let ((buf (or (get-file-buffer file)
  17606. (find-buffer-visiting file))))
  17607. (if buf
  17608. (or (buffer-base-buffer buf) buf)
  17609. nil)))
  17610. (defun org-image-file-name-regexp (&optional extensions)
  17611. "Return regexp matching the file names of images.
  17612. If EXTENSIONS is given, only match these."
  17613. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  17614. (image-file-name-regexp)
  17615. (let ((image-file-name-extensions
  17616. (or extensions
  17617. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  17618. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  17619. (concat "\\."
  17620. (regexp-opt (nconc (mapcar 'upcase
  17621. image-file-name-extensions)
  17622. image-file-name-extensions)
  17623. t)
  17624. "\\'"))))
  17625. (defun org-file-image-p (file &optional extensions)
  17626. "Return non-nil if FILE is an image."
  17627. (save-match-data
  17628. (string-match (org-image-file-name-regexp extensions) file)))
  17629. (defun org-get-cursor-date ()
  17630. "Return the date at cursor in as a time.
  17631. This works in the calendar and in the agenda, anywhere else it just
  17632. returns the current time."
  17633. (let (date day defd)
  17634. (cond
  17635. ((eq major-mode 'calendar-mode)
  17636. (setq date (calendar-cursor-to-date)
  17637. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17638. ((eq major-mode 'org-agenda-mode)
  17639. (setq day (get-text-property (point) 'day))
  17640. (if day
  17641. (setq date (calendar-gregorian-from-absolute day)
  17642. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17643. (nth 2 date))))))
  17644. (or defd (current-time))))
  17645. (defvar org-agenda-action-marker (make-marker)
  17646. "Marker pointing to the entry for the next agenda action.")
  17647. (defun org-mark-entry-for-agenda-action ()
  17648. "Mark the current entry as target of an agenda action.
  17649. Agenda actions are actions executed from the agenda with the key `k',
  17650. which make use of the date at the cursor."
  17651. (interactive)
  17652. (move-marker org-agenda-action-marker
  17653. (save-excursion (org-back-to-heading t) (point))
  17654. (current-buffer))
  17655. (message
  17656. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17657. (defun org-mark-subtree ()
  17658. "Mark the current subtree.
  17659. This puts point at the start of the current subtree, and mark at the end.
  17660. If point is in an inline task, mark that task instead."
  17661. (interactive)
  17662. (let ((inline-task-p
  17663. (and (featurep 'org-inlinetask)
  17664. (org-inlinetask-in-task-p)))
  17665. (beg))
  17666. ;; Get beginning of subtree
  17667. (cond
  17668. (inline-task-p (org-inlinetask-goto-beginning))
  17669. ((org-at-heading-p) (beginning-of-line))
  17670. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  17671. (setq beg (point))
  17672. ;; Get end of it
  17673. (if inline-task-p
  17674. (org-inlinetask-goto-end)
  17675. (org-end-of-subtree))
  17676. ;; Mark zone
  17677. (push-mark (point) nil t)
  17678. (goto-char beg)))
  17679. ;;; Paragraph filling stuff.
  17680. ;; We want this to be just right, so use the full arsenal.
  17681. (defun org-indent-line-function ()
  17682. "Indent line depending on context."
  17683. (interactive)
  17684. (let* ((pos (point))
  17685. (itemp (org-at-item-p))
  17686. (case-fold-search t)
  17687. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17688. (inline-task-p (and (featurep 'org-inlinetask)
  17689. (org-inlinetask-in-task-p)))
  17690. (inline-re (and inline-task-p
  17691. (org-inlinetask-outline-regexp)))
  17692. column)
  17693. (beginning-of-line 1)
  17694. (cond
  17695. ;; Comments
  17696. ((looking-at "# ") (setq column 0))
  17697. ;; Headings
  17698. ((looking-at org-outline-regexp) (setq column 0))
  17699. ;; Included files
  17700. ((looking-at "#\\+include:") (setq column 0))
  17701. ;; Footnote definition
  17702. ((looking-at org-footnote-definition-re) (setq column 0))
  17703. ;; Literal examples
  17704. ((looking-at "[ \t]*:[ \t]")
  17705. (setq column (org-get-indentation))) ; do nothing
  17706. ;; Lists
  17707. ((ignore-errors (goto-char (org-in-item-p)))
  17708. (setq column (if itemp
  17709. (org-get-indentation)
  17710. (org-list-item-body-column (point))))
  17711. (goto-char pos))
  17712. ;; Drawers
  17713. ((and (looking-at "[ \t]*:END:")
  17714. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17715. (save-excursion
  17716. (goto-char (1- (match-beginning 1)))
  17717. (setq column (current-column))))
  17718. ;; Special blocks
  17719. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17720. (save-excursion
  17721. (re-search-backward
  17722. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17723. (setq column (org-get-indentation (match-string 0))))
  17724. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17725. (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17726. (save-excursion
  17727. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17728. (setq column
  17729. (if (equal (downcase (match-string 1)) "src")
  17730. ;; src blocks: let `org-edit-src-exit' handle them
  17731. (org-get-indentation)
  17732. (org-get-indentation (match-string 0)))))
  17733. ;; This line has nothing special, look at the previous relevant
  17734. ;; line to compute indentation
  17735. (t
  17736. (beginning-of-line 0)
  17737. (while (and (not (bobp))
  17738. (not (looking-at org-drawer-regexp))
  17739. ;; When point started in an inline task, do not move
  17740. ;; above task starting line.
  17741. (not (and inline-task-p (looking-at inline-re)))
  17742. ;; Skip drawers, blocks, empty lines, verbatim,
  17743. ;; comments, tables, footnotes definitions, lists,
  17744. ;; inline tasks.
  17745. (or (and (looking-at "[ \t]*:END:")
  17746. (re-search-backward org-drawer-regexp nil t))
  17747. (and (looking-at "[ \t]*#\\+end_")
  17748. (re-search-backward "[ \t]*#\\+begin_"nil t))
  17749. (looking-at "[ \t]*[\n:#|]")
  17750. (looking-at org-footnote-definition-re)
  17751. (and (ignore-errors (goto-char (org-in-item-p)))
  17752. (goto-char
  17753. (org-list-get-top-point (org-list-struct))))
  17754. (and (not inline-task-p)
  17755. (featurep 'org-inlinetask)
  17756. (org-inlinetask-in-task-p)
  17757. (or (org-inlinetask-goto-beginning) t))))
  17758. (beginning-of-line 0))
  17759. (cond
  17760. ;; There was an heading above.
  17761. ((looking-at "\\*+[ \t]+")
  17762. (if (not org-adapt-indentation)
  17763. (setq column 0)
  17764. (goto-char (match-end 0))
  17765. (setq column (current-column))))
  17766. ;; A drawer had started and is unfinished
  17767. ((looking-at org-drawer-regexp)
  17768. (goto-char (1- (match-beginning 1)))
  17769. (setq column (current-column)))
  17770. ;; Else, nothing noticeable found: get indentation and go on.
  17771. (t (setq column (org-get-indentation))))))
  17772. ;; Now apply indentation and move cursor accordingly
  17773. (goto-char pos)
  17774. (if (<= (current-column) (current-indentation))
  17775. (org-indent-line-to column)
  17776. (save-excursion (org-indent-line-to column)))
  17777. ;; Special polishing for properties, see `org-property-format'
  17778. (setq column (current-column))
  17779. (beginning-of-line 1)
  17780. (if (looking-at
  17781. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17782. (replace-match (concat (match-string 1)
  17783. (format org-property-format
  17784. (match-string 2) (match-string 3)))
  17785. t t))
  17786. (org-move-to-column column)))
  17787. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17788. "Variable to store copy of `adaptive-fill-regexp'.
  17789. Since `adaptive-fill-regexp' is set to never match, we need to
  17790. store a backup of its value before entering `org-mode' so that
  17791. the functionality can be provided as a fall-back.")
  17792. (defun org-set-autofill-regexps ()
  17793. (interactive)
  17794. ;; In the paragraph separator we include headlines, because filling
  17795. ;; text in a line directly attached to a headline would otherwise
  17796. ;; fill the headline as well.
  17797. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17798. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17799. ;; The paragraph starter includes hand-formatted lists.
  17800. (org-set-local
  17801. 'paragraph-start
  17802. (concat
  17803. "\f" "\\|"
  17804. "[ ]*$" "\\|"
  17805. org-outline-regexp "\\|"
  17806. "[ \t]*#" "\\|"
  17807. (org-item-re) "\\|"
  17808. "[ \t]*[:|]" "\\|"
  17809. "\\$\\$" "\\|"
  17810. "\\\\\\(begin\\|end\\|[][]\\)"))
  17811. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17812. ;; But only if the user has not turned off tables or fixed-width regions
  17813. (org-set-local
  17814. 'auto-fill-inhibit-regexp
  17815. (concat org-outline-regexp
  17816. "\\|#\\+"
  17817. "\\|[ \t]*" org-keyword-time-regexp
  17818. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17819. (concat
  17820. "\\|[ \t]*["
  17821. (if org-enable-table-editor "|" "")
  17822. (if org-enable-fixed-width-editor ":" "")
  17823. "]"))))
  17824. ;; We use our own fill-paragraph function, to make sure that tables
  17825. ;; and fixed-width regions are not wrapped. That function will pass
  17826. ;; through to `fill-paragraph' when appropriate.
  17827. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17828. ;; Prevent auto-fill from inserting unwanted new items.
  17829. (org-set-local 'fill-nobreak-predicate
  17830. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  17831. fill-nobreak-predicate
  17832. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate)))
  17833. ;; Adaptive filling: To get full control, first make sure that
  17834. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17835. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17836. (org-set-local 'org-adaptive-fill-regexp-backup
  17837. adaptive-fill-regexp))
  17838. (org-set-local 'adaptive-fill-regexp "\000")
  17839. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  17840. (org-set-local 'adaptive-fill-function
  17841. 'org-adaptive-fill-function)
  17842. (org-set-local
  17843. 'align-mode-rules-list
  17844. '((org-in-buffer-settings
  17845. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17846. (modes . '(org-mode))))))
  17847. (defun org-fill-item-nobreak-p ()
  17848. "Non-nil when a line break at point would insert a new item."
  17849. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  17850. (defun org-fill-paragraph (&optional justify)
  17851. "Re-align a table, pass through to fill-paragraph if no table."
  17852. (let ((table-p (org-at-table-p))
  17853. (table.el-p (org-at-table.el-p))
  17854. (itemp (org-in-item-p)))
  17855. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17856. (save-excursion (goto-char (point-at-bol))
  17857. (looking-at org-outline-regexp)))
  17858. t) ; skip headlines
  17859. (table.el-p t) ; skip table.el tables
  17860. (table-p (org-table-align) t) ; align Org tables
  17861. (itemp ; align text in items
  17862. (let* ((struct (save-excursion (goto-char itemp)
  17863. (org-list-struct)))
  17864. (parents (org-list-parents-alist struct))
  17865. (children (org-list-get-children itemp struct parents))
  17866. beg end prev next prefix)
  17867. ;; Determine in which part of item point is: before
  17868. ;; first child, after last child, between two
  17869. ;; sub-lists, or simply in item if there's no child.
  17870. (cond
  17871. ((not children)
  17872. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17873. beg itemp
  17874. end (org-list-get-item-end itemp struct)))
  17875. ((< (point) (setq next (car children)))
  17876. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17877. beg itemp
  17878. end next))
  17879. ((> (point) (setq prev (car (last children))))
  17880. (setq beg (org-list-get-item-end prev struct)
  17881. end (org-list-get-item-end itemp struct)
  17882. prefix (save-excursion
  17883. (goto-char beg)
  17884. (skip-chars-forward " \t")
  17885. (make-string (current-column) ?\ ))))
  17886. (t (catch 'exit
  17887. (while (setq next (pop children))
  17888. (if (> (point) next)
  17889. (setq prev next)
  17890. (setq beg (org-list-get-item-end prev struct)
  17891. end next
  17892. prefix (save-excursion
  17893. (goto-char beg)
  17894. (skip-chars-forward " \t")
  17895. (make-string (current-column) ?\ )))
  17896. (throw 'exit nil))))))
  17897. ;; Use `fill-paragraph' with buffer narrowed to item
  17898. ;; without any child, and with our computed PREFIX.
  17899. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17900. (save-restriction
  17901. (narrow-to-region beg end)
  17902. (save-excursion (fill-paragraph justify)))) t))
  17903. ;; Special case where point is not in a list but is on
  17904. ;; a paragraph adjacent to a list: make sure this paragraph
  17905. ;; doesn't get merged with the end of the list by narrowing
  17906. ;; buffer first.
  17907. ((save-excursion (forward-paragraph -1)
  17908. (setq itemp (org-in-item-p)))
  17909. (let ((struct (save-excursion (goto-char itemp)
  17910. (org-list-struct))))
  17911. (save-restriction
  17912. (narrow-to-region (org-list-get-bottom-point struct)
  17913. (save-excursion (forward-paragraph 1)
  17914. (point)))
  17915. (fill-paragraph justify) t)))
  17916. ;; Else simply call `fill-paragraph'.
  17917. (t nil))))
  17918. ;; For reference, this is the default value of adaptive-fill-regexp
  17919. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17920. (defun org-adaptive-fill-function ()
  17921. "Return a fill prefix for org-mode files."
  17922. (let (itemp)
  17923. (save-excursion
  17924. (cond
  17925. ;; Comment line
  17926. ((looking-at "#[ \t]+")
  17927. (match-string-no-properties 0))
  17928. ;; Plain list item
  17929. ((org-at-item-p)
  17930. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  17931. ;; Point is in a list after `backward-paragraph': original
  17932. ;; point wasn't in the list, or filling would have been taken
  17933. ;; care of by `org-auto-fill-function', but the list and the
  17934. ;; real paragraph are not separated by a blank line. Thus, move
  17935. ;; point after the list to go back to real paragraph and
  17936. ;; determine fill-prefix.
  17937. ((setq itemp (org-in-item-p))
  17938. (goto-char itemp)
  17939. (let* ((struct (org-list-struct))
  17940. (bottom (org-list-get-bottom-point struct)))
  17941. (goto-char bottom)
  17942. (make-string (org-get-indentation) ?\ )))
  17943. ;; Other text
  17944. ((looking-at org-adaptive-fill-regexp-backup)
  17945. (match-string-no-properties 0))))))
  17946. (defun org-auto-fill-function ()
  17947. "Auto-fill function."
  17948. (let (itemp prefix)
  17949. ;; When in a list, compute an appropriate fill-prefix and make
  17950. ;; sure it will be used by `do-auto-fill'.
  17951. (if (setq itemp (org-in-item-p))
  17952. (progn
  17953. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  17954. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17955. (do-auto-fill)))
  17956. ;; Else just use `do-auto-fill'.
  17957. (do-auto-fill))))
  17958. ;;; Other stuff.
  17959. (defun org-toggle-fixed-width-section (arg)
  17960. "Toggle the fixed-width export.
  17961. If there is no active region, the QUOTE keyword at the current headline is
  17962. inserted or removed. When present, it causes the text between this headline
  17963. and the next to be exported as fixed-width text, and unmodified.
  17964. If there is an active region, this command adds or removes a colon as the
  17965. first character of this line. If the first character of a line is a colon,
  17966. this line is also exported in fixed-width font."
  17967. (interactive "P")
  17968. (let* ((cc 0)
  17969. (regionp (org-region-active-p))
  17970. (beg (if regionp (region-beginning) (point)))
  17971. (end (if regionp (region-end)))
  17972. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17973. (case-fold-search nil)
  17974. (re "[ \t]*\\(: \\)")
  17975. off)
  17976. (if regionp
  17977. (save-excursion
  17978. (goto-char beg)
  17979. (setq cc (current-column))
  17980. (beginning-of-line 1)
  17981. (setq off (looking-at re))
  17982. (while (> nlines 0)
  17983. (setq nlines (1- nlines))
  17984. (beginning-of-line 1)
  17985. (cond
  17986. (arg
  17987. (org-move-to-column cc t)
  17988. (insert ": \n")
  17989. (forward-line -1))
  17990. ((and off (looking-at re))
  17991. (replace-match "" t t nil 1))
  17992. ((not off) (org-move-to-column cc t) (insert ": ")))
  17993. (forward-line 1)))
  17994. (save-excursion
  17995. (org-back-to-heading)
  17996. (if (looking-at (concat org-outline-regexp
  17997. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17998. (replace-match "" t t nil 1)
  17999. (if (looking-at org-outline-regexp)
  18000. (progn
  18001. (goto-char (match-end 0))
  18002. (insert org-quote-string " "))))))))
  18003. (defun org-reftex-citation ()
  18004. "Use reftex-citation to insert a citation into the buffer.
  18005. This looks for a line like
  18006. #+BIBLIOGRAPHY: foo plain option:-d
  18007. and derives from it that foo.bib is the bibliography file relevant
  18008. for this document. It then installs the necessary environment for RefTeX
  18009. to work in this buffer and calls `reftex-citation' to insert a citation
  18010. into the buffer.
  18011. Export of such citations to both LaTeX and HTML is handled by the contributed
  18012. package org-exp-bibtex by Taru Karttunen."
  18013. (interactive)
  18014. (let ((reftex-docstruct-symbol 'rds)
  18015. (reftex-cite-format "\\cite{%l}")
  18016. rds bib)
  18017. (save-excursion
  18018. (save-restriction
  18019. (widen)
  18020. (let ((case-fold-search t)
  18021. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18022. (if (not (save-excursion
  18023. (or (re-search-forward re nil t)
  18024. (re-search-backward re nil t))))
  18025. (error "No bibliography defined in file")
  18026. (setq bib (concat (match-string 1) ".bib")
  18027. rds (list (list 'bib bib)))))))
  18028. (call-interactively 'reftex-citation)))
  18029. ;;;; Functions extending outline functionality
  18030. (defun org-beginning-of-line (&optional arg)
  18031. "Go to the beginning of the current line. If that is invisible, continue
  18032. to a visible line beginning. This makes the function of C-a more intuitive.
  18033. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18034. first attempt, and only move to after the tags when the cursor is already
  18035. beyond the end of the headline."
  18036. (interactive "P")
  18037. (let ((pos (point))
  18038. (special (if (consp org-special-ctrl-a/e)
  18039. (car org-special-ctrl-a/e)
  18040. org-special-ctrl-a/e))
  18041. refpos)
  18042. (if (org-bound-and-true-p line-move-visual)
  18043. (beginning-of-visual-line 1)
  18044. (beginning-of-line 1))
  18045. (if (and arg (fboundp 'move-beginning-of-line))
  18046. (call-interactively 'move-beginning-of-line)
  18047. (if (bobp)
  18048. nil
  18049. (backward-char 1)
  18050. (if (org-truely-invisible-p)
  18051. (while (and (not (bobp)) (org-truely-invisible-p))
  18052. (backward-char 1)
  18053. (beginning-of-line 1))
  18054. (forward-char 1))))
  18055. (when special
  18056. (cond
  18057. ((and (looking-at org-complex-heading-regexp)
  18058. (= (char-after (match-end 1)) ?\ ))
  18059. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18060. (point-at-eol)))
  18061. (goto-char
  18062. (if (eq special t)
  18063. (cond ((> pos refpos) refpos)
  18064. ((= pos (point)) refpos)
  18065. (t (point)))
  18066. (cond ((> pos (point)) (point))
  18067. ((not (eq last-command this-command)) (point))
  18068. (t refpos)))))
  18069. ((org-at-item-p)
  18070. (goto-char
  18071. (if (eq special t)
  18072. (cond ((> pos (match-end 0)) (match-end 0))
  18073. ((= pos (point)) (match-end 0))
  18074. (t (point)))
  18075. (cond ((> pos (point)) (point))
  18076. ((not (eq last-command this-command)) (point))
  18077. (t (match-end 0))))))))
  18078. (org-no-warnings
  18079. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18080. (defun org-end-of-line (&optional arg)
  18081. "Go to the end of the line.
  18082. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18083. first attempt, and only move to after the tags when the cursor is already
  18084. beyond the end of the headline."
  18085. (interactive "P")
  18086. (let ((special (if (consp org-special-ctrl-a/e)
  18087. (cdr org-special-ctrl-a/e)
  18088. org-special-ctrl-a/e)))
  18089. (if (or (not special)
  18090. (not (org-on-heading-p))
  18091. arg)
  18092. (call-interactively
  18093. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18094. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18095. (t 'end-of-line)))
  18096. (let ((pos (point)))
  18097. (beginning-of-line 1)
  18098. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18099. (if (eq special t)
  18100. (if (or (< pos (match-beginning 1))
  18101. (= pos (match-end 0)))
  18102. (goto-char (match-beginning 1))
  18103. (goto-char (match-end 0)))
  18104. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18105. (goto-char (match-end 0))
  18106. (goto-char (match-beginning 1))))
  18107. (call-interactively (if (fboundp 'move-end-of-line)
  18108. 'move-end-of-line
  18109. 'end-of-line)))))
  18110. (org-no-warnings
  18111. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18112. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18113. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18114. (defun org-backward-sentence (&optional arg)
  18115. "Go to beginning of sentence, or beginning of table field.
  18116. This will call `backward-sentence' or `org-table-beginning-of-field',
  18117. depending on context."
  18118. (interactive "P")
  18119. (cond
  18120. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18121. (t (call-interactively 'backward-sentence))))
  18122. (defun org-forward-sentence (&optional arg)
  18123. "Go to end of sentence, or end of table field.
  18124. This will call `forward-sentence' or `org-table-end-of-field',
  18125. depending on context."
  18126. (interactive "P")
  18127. (cond
  18128. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18129. (t (call-interactively 'forward-sentence))))
  18130. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18131. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18132. (defun org-kill-line (&optional arg)
  18133. "Kill line, to tags or end of line."
  18134. (interactive "P")
  18135. (cond
  18136. ((or (not org-special-ctrl-k)
  18137. (bolp)
  18138. (not (org-on-heading-p)))
  18139. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18140. org-ctrl-k-protect-subtree)
  18141. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18142. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18143. (error "C-k aborted - would kill hidden subtree")))
  18144. (call-interactively 'kill-line))
  18145. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18146. (kill-region (point) (match-beginning 1))
  18147. (org-set-tags nil t))
  18148. (t (kill-region (point) (point-at-eol)))))
  18149. (define-key org-mode-map "\C-k" 'org-kill-line)
  18150. (defun org-yank (&optional arg)
  18151. "Yank. If the kill is a subtree, treat it specially.
  18152. This command will look at the current kill and check if is a single
  18153. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18154. cursor is at the beginning of a line or after the stars of a currently
  18155. empty headline, then the yank is handled specially. How exactly depends
  18156. on the value of the following variables, both set by default.
  18157. org-yank-folded-subtrees
  18158. When set, the subtree(s) will be folded after insertion, but only
  18159. if doing so would now swallow text after the yanked text.
  18160. org-yank-adjusted-subtrees
  18161. When set, the subtree will be promoted or demoted in order to
  18162. fit into the local outline tree structure, which means that the level
  18163. will be adjusted so that it becomes the smaller one of the two
  18164. *visible* surrounding headings.
  18165. Any prefix to this command will cause `yank' to be called directly with
  18166. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18167. will just
  18168. plainly yank the text as it is.
  18169. \[1] The test checks if the first non-white line is a heading
  18170. and if there are no other headings with fewer stars."
  18171. (interactive "P")
  18172. (org-yank-generic 'yank arg))
  18173. (defun org-yank-generic (command arg)
  18174. "Perform some yank-like command.
  18175. This function implements the behavior described in the `org-yank'
  18176. documentation. However, it has been generalized to work for any
  18177. interactive command with similar behavior."
  18178. ;; pretend to be command COMMAND
  18179. (setq this-command command)
  18180. (if arg
  18181. (call-interactively command)
  18182. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18183. (and (org-kill-is-subtree-p)
  18184. (or (bolp)
  18185. (and (looking-at "[ \t]*$")
  18186. (string-match
  18187. "\\`\\*+\\'"
  18188. (buffer-substring (point-at-bol) (point)))))))
  18189. swallowp)
  18190. (cond
  18191. ((and subtreep org-yank-folded-subtrees)
  18192. (let ((beg (point))
  18193. end)
  18194. (if (and subtreep org-yank-adjusted-subtrees)
  18195. (org-paste-subtree nil nil 'for-yank)
  18196. (call-interactively command))
  18197. (setq end (point))
  18198. (goto-char beg)
  18199. (when (and (bolp) subtreep
  18200. (not (setq swallowp
  18201. (org-yank-folding-would-swallow-text beg end))))
  18202. (or (looking-at org-outline-regexp)
  18203. (re-search-forward org-outline-regexp-bol end t))
  18204. (while (and (< (point) end) (looking-at org-outline-regexp))
  18205. (hide-subtree)
  18206. (org-cycle-show-empty-lines 'folded)
  18207. (condition-case nil
  18208. (outline-forward-same-level 1)
  18209. (error (goto-char end)))))
  18210. (when swallowp
  18211. (message
  18212. "Inserted text not folded because that would swallow text"))
  18213. (goto-char end)
  18214. (skip-chars-forward " \t\n\r")
  18215. (beginning-of-line 1)
  18216. (push-mark beg 'nomsg)))
  18217. ((and subtreep org-yank-adjusted-subtrees)
  18218. (let ((beg (point-at-bol)))
  18219. (org-paste-subtree nil nil 'for-yank)
  18220. (push-mark beg 'nomsg)))
  18221. (t
  18222. (call-interactively command))))))
  18223. (defun org-yank-folding-would-swallow-text (beg end)
  18224. "Would hide-subtree at BEG swallow any text after END?"
  18225. (let (level)
  18226. (save-excursion
  18227. (goto-char beg)
  18228. (when (or (looking-at org-outline-regexp)
  18229. (re-search-forward org-outline-regexp-bol end t))
  18230. (setq level (org-outline-level)))
  18231. (goto-char end)
  18232. (skip-chars-forward " \t\r\n\v\f")
  18233. (if (or (eobp)
  18234. (and (bolp) (looking-at org-outline-regexp)
  18235. (<= (org-outline-level) level)))
  18236. nil ; Nothing would be swallowed
  18237. t)))) ; something would swallow
  18238. (define-key org-mode-map "\C-y" 'org-yank)
  18239. (defun org-truely-invisible-p ()
  18240. "Check if point is at a character currently not visible.
  18241. This version does not only check the character property, but also
  18242. `visible-mode'."
  18243. ;; Early versions of noutline don't have `outline-invisible-p'.
  18244. (if (org-bound-and-true-p visible-mode)
  18245. nil
  18246. (outline-invisible-p)))
  18247. (defun org-invisible-p2 ()
  18248. "Check if point is at a character currently not visible."
  18249. (save-excursion
  18250. (if (and (eolp) (not (bobp))) (backward-char 1))
  18251. ;; Early versions of noutline don't have `outline-invisible-p'.
  18252. (outline-invisible-p)))
  18253. (defun org-back-to-heading (&optional invisible-ok)
  18254. "Call `outline-back-to-heading', but provide a better error message."
  18255. (condition-case nil
  18256. (outline-back-to-heading invisible-ok)
  18257. (error (error "Before first headline at position %d in buffer %s"
  18258. (point) (current-buffer)))))
  18259. (defun org-beginning-of-defun ()
  18260. "Go to the beginning of the subtree, i.e. back to the heading."
  18261. (org-back-to-heading))
  18262. (defun org-end-of-defun ()
  18263. "Go to the end of the subtree."
  18264. (org-end-of-subtree nil t))
  18265. (defun org-before-first-heading-p ()
  18266. "Before first heading?"
  18267. (save-excursion
  18268. (end-of-line)
  18269. (null (re-search-backward org-outline-regexp-bol nil t))))
  18270. (defun org-on-heading-p (&optional ignored)
  18271. (outline-on-heading-p t))
  18272. (defun org-at-heading-p (&optional ignored)
  18273. (outline-on-heading-p t))
  18274. (defun org-point-at-end-of-empty-headline ()
  18275. "If point is at the end of an empty headline, return t, else nil.
  18276. If the heading only contains a TODO keyword, it is still still considered
  18277. empty."
  18278. (and (looking-at "[ \t]*$")
  18279. (save-excursion
  18280. (beginning-of-line 1)
  18281. (let ((case-fold-search nil))
  18282. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  18283. "\\)?[ \t]*$"))))))
  18284. (defun org-at-heading-or-item-p ()
  18285. (or (org-on-heading-p) (org-at-item-p)))
  18286. (defun org-on-target-p ()
  18287. (or (org-in-regexp org-radio-target-regexp)
  18288. (org-in-regexp org-target-regexp)))
  18289. (defun org-up-heading-all (arg)
  18290. "Move to the heading line of which the present line is a subheading.
  18291. This function considers both visible and invisible heading lines.
  18292. With argument, move up ARG levels."
  18293. (if (fboundp 'outline-up-heading-all)
  18294. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  18295. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  18296. (defun org-up-heading-safe ()
  18297. "Move to the heading line of which the present line is a subheading.
  18298. This version will not throw an error. It will return the level of the
  18299. headline found, or nil if no higher level is found.
  18300. Also, this function will be a lot faster than `outline-up-heading',
  18301. because it relies on stars being the outline starters. This can really
  18302. make a significant difference in outlines with very many siblings."
  18303. (let (start-level re)
  18304. (org-back-to-heading t)
  18305. (setq start-level (funcall outline-level))
  18306. (if (equal start-level 1)
  18307. nil
  18308. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  18309. (if (re-search-backward re nil t)
  18310. (funcall outline-level)))))
  18311. (defun org-first-sibling-p ()
  18312. "Is this heading the first child of its parents?"
  18313. (interactive)
  18314. (let ((re org-outline-regexp-bol)
  18315. level l)
  18316. (unless (org-at-heading-p t)
  18317. (error "Not at a heading"))
  18318. (setq level (funcall outline-level))
  18319. (save-excursion
  18320. (if (not (re-search-backward re nil t))
  18321. t
  18322. (setq l (funcall outline-level))
  18323. (< l level)))))
  18324. (defun org-goto-sibling (&optional previous)
  18325. "Goto the next sibling, even if it is invisible.
  18326. When PREVIOUS is set, go to the previous sibling instead. Returns t
  18327. when a sibling was found. When none is found, return nil and don't
  18328. move point."
  18329. (let ((fun (if previous 're-search-backward 're-search-forward))
  18330. (pos (point))
  18331. (re org-outline-regexp-bol)
  18332. level l)
  18333. (when (condition-case nil (org-back-to-heading t) (error nil))
  18334. (setq level (funcall outline-level))
  18335. (catch 'exit
  18336. (or previous (forward-char 1))
  18337. (while (funcall fun re nil t)
  18338. (setq l (funcall outline-level))
  18339. (when (< l level) (goto-char pos) (throw 'exit nil))
  18340. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  18341. (goto-char pos)
  18342. nil))))
  18343. (defun org-show-siblings ()
  18344. "Show all siblings of the current headline."
  18345. (save-excursion
  18346. (while (org-goto-sibling) (org-flag-heading nil)))
  18347. (save-excursion
  18348. (while (org-goto-sibling 'previous)
  18349. (org-flag-heading nil))))
  18350. (defun org-goto-first-child ()
  18351. "Goto the first child, even if it is invisible.
  18352. Return t when a child was found. Otherwise don't move point and
  18353. return nil."
  18354. (let (level (pos (point)) (re org-outline-regexp-bol))
  18355. (when (condition-case nil (org-back-to-heading t) (error nil))
  18356. (setq level (outline-level))
  18357. (forward-char 1)
  18358. (if (and (re-search-forward re nil t) (> (outline-level) level))
  18359. (progn (goto-char (match-beginning 0)) t)
  18360. (goto-char pos) nil))))
  18361. (defun org-show-hidden-entry ()
  18362. "Show an entry where even the heading is hidden."
  18363. (save-excursion
  18364. (org-show-entry)))
  18365. (defun org-flag-heading (flag &optional entry)
  18366. "Flag the current heading. FLAG non-nil means make invisible.
  18367. When ENTRY is non-nil, show the entire entry."
  18368. (save-excursion
  18369. (org-back-to-heading t)
  18370. ;; Check if we should show the entire entry
  18371. (if entry
  18372. (progn
  18373. (org-show-entry)
  18374. (save-excursion
  18375. (and (outline-next-heading)
  18376. (org-flag-heading nil))))
  18377. (outline-flag-region (max (point-min) (1- (point)))
  18378. (save-excursion (outline-end-of-heading) (point))
  18379. flag))))
  18380. (defun org-get-next-sibling ()
  18381. "Move to next heading of the same level, and return point.
  18382. If there is no such heading, return nil.
  18383. This is like outline-next-sibling, but invisible headings are ok."
  18384. (let ((level (funcall outline-level)))
  18385. (outline-next-heading)
  18386. (while (and (not (eobp)) (> (funcall outline-level) level))
  18387. (outline-next-heading))
  18388. (if (or (eobp) (< (funcall outline-level) level))
  18389. nil
  18390. (point))))
  18391. (defun org-get-last-sibling ()
  18392. "Move to previous heading of the same level, and return point.
  18393. If there is no such heading, return nil."
  18394. (let ((opoint (point))
  18395. (level (funcall outline-level)))
  18396. (outline-previous-heading)
  18397. (when (and (/= (point) opoint) (outline-on-heading-p t))
  18398. (while (and (> (funcall outline-level) level)
  18399. (not (bobp)))
  18400. (outline-previous-heading))
  18401. (if (< (funcall outline-level) level)
  18402. nil
  18403. (point)))))
  18404. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  18405. ;; This contains an exact copy of the original function, but it uses
  18406. ;; `org-back-to-heading', to make it work also in invisible
  18407. ;; trees. And is uses an invisible-OK argument.
  18408. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  18409. ;; Furthermore, when used inside Org, finding the end of a large subtree
  18410. ;; with many children and grandchildren etc, this can be much faster
  18411. ;; than the outline version.
  18412. (org-back-to-heading invisible-OK)
  18413. (let ((first t)
  18414. (level (funcall outline-level)))
  18415. (if (and (org-mode-p) (< level 1000))
  18416. ;; A true heading (not a plain list item), in Org-mode
  18417. ;; This means we can easily find the end by looking
  18418. ;; only for the right number of stars. Using a regexp to do
  18419. ;; this is so much faster than using a Lisp loop.
  18420. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  18421. (forward-char 1)
  18422. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  18423. ;; something else, do it the slow way
  18424. (while (and (not (eobp))
  18425. (or first (> (funcall outline-level) level)))
  18426. (setq first nil)
  18427. (outline-next-heading)))
  18428. (unless to-heading
  18429. (if (memq (preceding-char) '(?\n ?\^M))
  18430. (progn
  18431. ;; Go to end of line before heading
  18432. (forward-char -1)
  18433. (if (memq (preceding-char) '(?\n ?\^M))
  18434. ;; leave blank line before heading
  18435. (forward-char -1))))))
  18436. (point))
  18437. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  18438. "Use Org version in org-mode, for dramatic speed-up."
  18439. (if (org-mode-p)
  18440. (progn
  18441. (org-end-of-subtree nil t)
  18442. (unless (eobp) (backward-char 1)))
  18443. ad-do-it))
  18444. (defun org-end-of-meta-data-and-drawers ()
  18445. "Jump to the first text after meta data and drawers in the current entry.
  18446. This will move over empty lines, lines with planning time stamps,
  18447. clocking lines, and drawers."
  18448. (org-back-to-heading t)
  18449. (let ((end (save-excursion (outline-next-heading) (point)))
  18450. (re (concat "\\(" org-drawer-regexp "\\)"
  18451. "\\|" "[ \t]*" org-keyword-time-regexp)))
  18452. (forward-line 1)
  18453. (while (re-search-forward re end t)
  18454. (if (not (match-end 1))
  18455. ;; empty or planning line
  18456. (forward-line 1)
  18457. ;; a drawer, find the end
  18458. (re-search-forward "^[ \t]*:END:" end 'move)
  18459. (forward-line 1)))
  18460. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  18461. (point)))
  18462. (defun org-forward-same-level (arg &optional invisible-ok)
  18463. "Move forward to the arg'th subheading at same level as this one.
  18464. Stop at the first and last subheadings of a superior heading.
  18465. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  18466. it wil also look at invisible ones."
  18467. (interactive "p")
  18468. (org-back-to-heading invisible-ok)
  18469. (org-on-heading-p)
  18470. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18471. (re (format "^\\*\\{1,%d\\} " level))
  18472. l)
  18473. (forward-char 1)
  18474. (while (> arg 0)
  18475. (while (and (re-search-forward re nil 'move)
  18476. (setq l (- (match-end 0) (match-beginning 0) 1))
  18477. (= l level)
  18478. (not invisible-ok)
  18479. (progn (backward-char 1) (outline-invisible-p)))
  18480. (if (< l level) (setq arg 1)))
  18481. (setq arg (1- arg)))
  18482. (beginning-of-line 1)))
  18483. (defun org-backward-same-level (arg &optional invisible-ok)
  18484. "Move backward to the arg'th subheading at same level as this one.
  18485. Stop at the first and last subheadings of a superior heading."
  18486. (interactive "p")
  18487. (org-back-to-heading)
  18488. (org-on-heading-p)
  18489. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18490. (re (format "^\\*\\{1,%d\\} " level))
  18491. l)
  18492. (while (> arg 0)
  18493. (while (and (re-search-backward re nil 'move)
  18494. (setq l (- (match-end 0) (match-beginning 0) 1))
  18495. (= l level)
  18496. (not invisible-ok)
  18497. (outline-invisible-p))
  18498. (if (< l level) (setq arg 1)))
  18499. (setq arg (1- arg)))))
  18500. (defun org-show-subtree ()
  18501. "Show everything after this heading at deeper levels."
  18502. (outline-flag-region
  18503. (point)
  18504. (save-excursion
  18505. (org-end-of-subtree t t))
  18506. nil))
  18507. (defun org-show-entry ()
  18508. "Show the body directly following this heading.
  18509. Show the heading too, if it is currently invisible."
  18510. (interactive)
  18511. (save-excursion
  18512. (condition-case nil
  18513. (progn
  18514. (org-back-to-heading t)
  18515. (outline-flag-region
  18516. (max (point-min) (1- (point)))
  18517. (save-excursion
  18518. (if (re-search-forward
  18519. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  18520. (match-beginning 1)
  18521. (point-max)))
  18522. nil)
  18523. (org-cycle-hide-drawers 'children))
  18524. (error nil))))
  18525. (defun org-make-options-regexp (kwds &optional extra)
  18526. "Make a regular expression for keyword lines."
  18527. (concat
  18528. "^"
  18529. "#?[ \t]*\\+\\("
  18530. (mapconcat 'regexp-quote kwds "\\|")
  18531. (if extra (concat "\\|" extra))
  18532. "\\):[ \t]*"
  18533. "\\(.*\\)"))
  18534. ;; Make isearch reveal the necessary context
  18535. (defun org-isearch-end ()
  18536. "Reveal context after isearch exits."
  18537. (when isearch-success ; only if search was successful
  18538. (if (featurep 'xemacs)
  18539. ;; Under XEmacs, the hook is run in the correct place,
  18540. ;; we directly show the context.
  18541. (org-show-context 'isearch)
  18542. ;; In Emacs the hook runs *before* restoring the overlays.
  18543. ;; So we have to use a one-time post-command-hook to do this.
  18544. ;; (Emacs 22 has a special variable, see function `org-mode')
  18545. (unless (and (boundp 'isearch-mode-end-hook-quit)
  18546. isearch-mode-end-hook-quit)
  18547. ;; Only when the isearch was not quitted.
  18548. (org-add-hook 'post-command-hook 'org-isearch-post-command
  18549. 'append 'local)))))
  18550. (defun org-isearch-post-command ()
  18551. "Remove self from hook, and show context."
  18552. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  18553. (org-show-context 'isearch))
  18554. ;;;; Integration with and fixes for other packages
  18555. ;;; Imenu support
  18556. (defvar org-imenu-markers nil
  18557. "All markers currently used by Imenu.")
  18558. (make-variable-buffer-local 'org-imenu-markers)
  18559. (defun org-imenu-new-marker (&optional pos)
  18560. "Return a new marker for use by Imenu, and remember the marker."
  18561. (let ((m (make-marker)))
  18562. (move-marker m (or pos (point)))
  18563. (push m org-imenu-markers)
  18564. m))
  18565. (defun org-imenu-get-tree ()
  18566. "Produce the index for Imenu."
  18567. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  18568. (setq org-imenu-markers nil)
  18569. (let* ((n org-imenu-depth)
  18570. (re (concat "^" outline-regexp))
  18571. (subs (make-vector (1+ n) nil))
  18572. (last-level 0)
  18573. m level head)
  18574. (save-excursion
  18575. (save-restriction
  18576. (widen)
  18577. (goto-char (point-max))
  18578. (while (re-search-backward re nil t)
  18579. (setq level (org-reduced-level (funcall outline-level)))
  18580. (when (<= level n)
  18581. (looking-at org-complex-heading-regexp)
  18582. (setq head (org-link-display-format
  18583. (org-match-string-no-properties 4))
  18584. m (org-imenu-new-marker))
  18585. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  18586. (if (>= level last-level)
  18587. (push (cons head m) (aref subs level))
  18588. (push (cons head (aref subs (1+ level))) (aref subs level))
  18589. (loop for i from (1+ level) to n do (aset subs i nil)))
  18590. (setq last-level level)))))
  18591. (aref subs 1)))
  18592. (eval-after-load "imenu"
  18593. '(progn
  18594. (add-hook 'imenu-after-jump-hook
  18595. (lambda ()
  18596. (if (org-mode-p)
  18597. (org-show-context 'org-goto))))))
  18598. (defun org-link-display-format (link)
  18599. "Replace a link with either the description, or the link target
  18600. if no description is present"
  18601. (save-match-data
  18602. (if (string-match org-bracket-link-analytic-regexp link)
  18603. (replace-match (if (match-end 5)
  18604. (match-string 5 link)
  18605. (concat (match-string 1 link)
  18606. (match-string 3 link)))
  18607. nil t link)
  18608. link)))
  18609. ;; Speedbar support
  18610. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  18611. "Overlay marking the agenda restriction line in speedbar.")
  18612. (overlay-put org-speedbar-restriction-lock-overlay
  18613. 'face 'org-agenda-restriction-lock)
  18614. (overlay-put org-speedbar-restriction-lock-overlay
  18615. 'help-echo "Agendas are currently limited to this item.")
  18616. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  18617. (defun org-speedbar-set-agenda-restriction ()
  18618. "Restrict future agenda commands to the location at point in speedbar.
  18619. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  18620. (interactive)
  18621. (require 'org-agenda)
  18622. (let (p m tp np dir txt)
  18623. (cond
  18624. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18625. 'org-imenu t))
  18626. (setq m (get-text-property p 'org-imenu-marker))
  18627. (with-current-buffer (marker-buffer m)
  18628. (goto-char m)
  18629. (org-agenda-set-restriction-lock 'subtree)))
  18630. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18631. 'speedbar-function 'speedbar-find-file))
  18632. (setq tp (previous-single-property-change
  18633. (1+ p) 'speedbar-function)
  18634. np (next-single-property-change
  18635. tp 'speedbar-function)
  18636. dir (speedbar-line-directory)
  18637. txt (buffer-substring-no-properties (or tp (point-min))
  18638. (or np (point-max))))
  18639. (with-current-buffer (find-file-noselect
  18640. (let ((default-directory dir))
  18641. (expand-file-name txt)))
  18642. (unless (org-mode-p)
  18643. (error "Cannot restrict to non-Org-mode file"))
  18644. (org-agenda-set-restriction-lock 'file)))
  18645. (t (error "Don't know how to restrict Org-mode's agenda")))
  18646. (move-overlay org-speedbar-restriction-lock-overlay
  18647. (point-at-bol) (point-at-eol))
  18648. (setq current-prefix-arg nil)
  18649. (org-agenda-maybe-redo)))
  18650. (eval-after-load "speedbar"
  18651. '(progn
  18652. (speedbar-add-supported-extension ".org")
  18653. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  18654. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  18655. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  18656. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18657. (add-hook 'speedbar-visiting-tag-hook
  18658. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  18659. ;;; Fixes and Hacks for problems with other packages
  18660. ;; Make flyspell not check words in links, to not mess up our keymap
  18661. (defun org-mode-flyspell-verify ()
  18662. "Don't let flyspell put overlays at active buttons, or on
  18663. {todo,all-time,additional-option-like}-keywords."
  18664. (let ((pos (max (1- (point)) (point-min)))
  18665. (word (thing-at-point 'word)))
  18666. (and (not (get-text-property pos 'keymap))
  18667. (not (get-text-property pos 'org-no-flyspell))
  18668. (not (member word org-todo-keywords-1))
  18669. (not (member word org-all-time-keywords))
  18670. (not (member word org-additional-option-like-keywords)))))
  18671. (defun org-remove-flyspell-overlays-in (beg end)
  18672. "Remove flyspell overlays in region."
  18673. (and (org-bound-and-true-p flyspell-mode)
  18674. (fboundp 'flyspell-delete-region-overlays)
  18675. (flyspell-delete-region-overlays beg end))
  18676. (add-text-properties beg end '(org-no-flyspell t)))
  18677. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  18678. (eval-after-load "bookmark"
  18679. '(if (boundp 'bookmark-after-jump-hook)
  18680. ;; We can use the hook
  18681. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  18682. ;; Hook not available, use advice
  18683. (defadvice bookmark-jump (after org-make-visible activate)
  18684. "Make the position visible."
  18685. (org-bookmark-jump-unhide))))
  18686. ;; Make sure saveplace shows the location if it was hidden
  18687. (eval-after-load "saveplace"
  18688. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  18689. "Make the position visible."
  18690. (org-bookmark-jump-unhide)))
  18691. ;; Make sure ecb shows the location if it was hidden
  18692. (eval-after-load "ecb"
  18693. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  18694. "Make hierarchy visible when jumping into location from ECB tree buffer."
  18695. (if (org-mode-p)
  18696. (org-show-context))))
  18697. (defun org-bookmark-jump-unhide ()
  18698. "Unhide the current position, to show the bookmark location."
  18699. (and (org-mode-p)
  18700. (or (outline-invisible-p)
  18701. (save-excursion (goto-char (max (point-min) (1- (point))))
  18702. (outline-invisible-p)))
  18703. (org-show-context 'bookmark-jump)))
  18704. ;; Make session.el ignore our circular variable
  18705. (eval-after-load "session"
  18706. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  18707. ;;;; Experimental code
  18708. (defun org-closed-in-range ()
  18709. "Sparse tree of items closed in a certain time range.
  18710. Still experimental, may disappear in the future."
  18711. (interactive)
  18712. ;; Get the time interval from the user.
  18713. (let* ((time1 (org-float-time
  18714. (org-read-date nil 'to-time nil "Starting date: ")))
  18715. (time2 (org-float-time
  18716. (org-read-date nil 'to-time nil "End date:")))
  18717. ;; callback function
  18718. (callback (lambda ()
  18719. (let ((time
  18720. (org-float-time
  18721. (apply 'encode-time
  18722. (org-parse-time-string
  18723. (match-string 1))))))
  18724. ;; check if time in interval
  18725. (and (>= time time1) (<= time time2))))))
  18726. ;; make tree, check each match with the callback
  18727. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  18728. ;;;; Finish up
  18729. (provide 'org)
  18730. (run-hooks 'org-load-hook)
  18731. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  18732. ;;; org.el ends here