org.el 880 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t t)
  74. (require 'org-macs)
  75. (require 'org-compat)
  76. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  77. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  78. ;;
  79. ;; In Org buffers, the value of `outline-regexp' is that of
  80. ;; `org-outline-regexp'. The only function still directly relying on
  81. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  82. ;; job when `orgstruct-mode' is active.
  83. (defvar org-outline-regexp "\\*+ "
  84. "Regexp to match Org headlines.")
  85. (defvar org-outline-regexp-bol "^\\*+ "
  86. "Regexp to match Org headlines.
  87. This is similar to `org-outline-regexp' but additionally makes
  88. sure that we are at the beginning of the line.")
  89. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  90. "Matches a headline, putting stars and text into groups.
  91. Stars are put in group 1 and the trimmed body in group 2.")
  92. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  93. (when (fboundp 'defvaralias)
  94. (unless (boundp 'calendar-view-holidays-initially-flag)
  95. (defvaralias 'calendar-view-holidays-initially-flag
  96. 'view-calendar-holidays-initially))
  97. (unless (boundp 'calendar-view-diary-initially-flag)
  98. (defvaralias 'calendar-view-diary-initially-flag
  99. 'view-diary-entries-initially))
  100. (unless (boundp 'diary-fancy-buffer)
  101. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  102. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  103. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  104. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  105. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  106. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  107. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  108. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  109. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  110. (declare-function orgtbl-mode "org-table" (&optional arg))
  111. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  112. (declare-function org-beamer-mode "ox-beamer" ())
  113. (declare-function org-table-edit-field "org-table" (arg))
  114. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. (declare-function org-element--parse-objects "org-element"
  124. (beg end acc restriction))
  125. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  126. (declare-function org-element-contents "org-element" (element))
  127. (declare-function org-element-context "org-element" (&optional element))
  128. (declare-function org-element-interpret-data "org-element"
  129. (data &optional parent))
  130. (declare-function org-element-map "org-element"
  131. (data types fun &optional info first-match no-recursion))
  132. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  133. (declare-function org-element-parse-buffer "org-element"
  134. (&optional granularity visible-only))
  135. (declare-function org-element-property "org-element" (property element))
  136. (declare-function org-element-put-property "org-element"
  137. (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element--parse-objects "org-element"
  140. (beg end acc restriction))
  141. (declare-function org-element-parse-buffer "org-element"
  142. (&optional granularity visible-only))
  143. (declare-function org-element-restriction "org-element" (element))
  144. (declare-function org-element-type "org-element" (element))
  145. ;; load languages based on value of `org-babel-load-languages'
  146. (defvar org-babel-load-languages)
  147. ;;;###autoload
  148. (defun org-babel-do-load-languages (sym value)
  149. "Load the languages defined in `org-babel-load-languages'."
  150. (set-default sym value)
  151. (mapc (lambda (pair)
  152. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  153. (if active
  154. (progn
  155. (require (intern (concat "ob-" lang))))
  156. (progn
  157. (funcall 'fmakunbound
  158. (intern (concat "org-babel-execute:" lang)))
  159. (funcall 'fmakunbound
  160. (intern (concat "org-babel-expand-body:" lang)))))))
  161. org-babel-load-languages))
  162. (defcustom org-babel-load-languages '((emacs-lisp . t))
  163. "Languages which can be evaluated in Org-mode buffers.
  164. This list can be used to load support for any of the languages
  165. below, note that each language will depend on a different set of
  166. system executables and/or Emacs modes. When a language is
  167. \"loaded\", then code blocks in that language can be evaluated
  168. with `org-babel-execute-src-block' bound by default to C-c
  169. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  170. be set to remove code block evaluation from the C-c C-c
  171. keybinding. By default only Emacs Lisp (which has no
  172. requirements) is loaded."
  173. :group 'org-babel
  174. :set 'org-babel-do-load-languages
  175. :version "24.1"
  176. :type '(alist :tag "Babel Languages"
  177. :key-type
  178. (choice
  179. (const :tag "Awk" awk)
  180. (const :tag "C" C)
  181. (const :tag "R" R)
  182. (const :tag "Asymptote" asymptote)
  183. (const :tag "Calc" calc)
  184. (const :tag "Clojure" clojure)
  185. (const :tag "CSS" css)
  186. (const :tag "Ditaa" ditaa)
  187. (const :tag "Dot" dot)
  188. (const :tag "Emacs Lisp" emacs-lisp)
  189. (const :tag "Fortran" fortran)
  190. (const :tag "Gnuplot" gnuplot)
  191. (const :tag "Haskell" haskell)
  192. (const :tag "IO" io)
  193. (const :tag "Java" java)
  194. (const :tag "Javascript" js)
  195. (const :tag "LaTeX" latex)
  196. (const :tag "Ledger" ledger)
  197. (const :tag "Lilypond" lilypond)
  198. (const :tag "Lisp" lisp)
  199. (const :tag "Makefile" makefile)
  200. (const :tag "Maxima" maxima)
  201. (const :tag "Matlab" matlab)
  202. (const :tag "Mscgen" mscgen)
  203. (const :tag "Ocaml" ocaml)
  204. (const :tag "Octave" octave)
  205. (const :tag "Org" org)
  206. (const :tag "Perl" perl)
  207. (const :tag "Pico Lisp" picolisp)
  208. (const :tag "PlantUML" plantuml)
  209. (const :tag "Python" python)
  210. (const :tag "Ruby" ruby)
  211. (const :tag "Sass" sass)
  212. (const :tag "Scala" scala)
  213. (const :tag "Scheme" scheme)
  214. (const :tag "Screen" screen)
  215. (const :tag "Shell Script" sh)
  216. (const :tag "Shen" shen)
  217. (const :tag "Sql" sql)
  218. (const :tag "Sqlite" sqlite))
  219. :value-type (boolean :tag "Activate" :value t)))
  220. ;;;; Customization variables
  221. (defcustom org-clone-delete-id nil
  222. "Remove ID property of clones of a subtree.
  223. When non-nil, clones of a subtree don't inherit the ID property.
  224. Otherwise they inherit the ID property with a new unique
  225. identifier."
  226. :type 'boolean
  227. :version "24.1"
  228. :group 'org-id)
  229. ;;; Version
  230. (org-check-version)
  231. ;;;###autoload
  232. (defun org-version (&optional here full message)
  233. "Show the org-mode version in the echo area.
  234. With prefix argument HERE, insert it at point.
  235. When FULL is non-nil, use a verbose version string.
  236. When MESSAGE is non-nil, display a message with the version."
  237. (interactive "P")
  238. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  239. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  240. (load-suffixes (list ".el"))
  241. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  242. (org-trash (or
  243. (and (fboundp 'org-release) (fboundp 'org-git-version))
  244. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  245. (load-suffixes save-load-suffixes)
  246. (org-version (org-release))
  247. (git-version (org-git-version))
  248. (version (format "Org-mode version %s (%s @ %s)"
  249. org-version
  250. git-version
  251. (if org-install-dir
  252. (if (string= org-dir org-install-dir)
  253. org-install-dir
  254. (concat "mixed installation! " org-install-dir " and " org-dir))
  255. "org-loaddefs.el can not be found!")))
  256. (_version (if full version org-version)))
  257. (if (org-called-interactively-p 'interactive)
  258. (if here
  259. (insert version)
  260. (message version))
  261. (if message (message _version))
  262. _version)))
  263. (defconst org-version (org-version))
  264. ;;; Compatibility constants
  265. ;;; The custom variables
  266. (defgroup org nil
  267. "Outline-based notes management and organizer."
  268. :tag "Org"
  269. :group 'outlines
  270. :group 'calendar)
  271. (defcustom org-mode-hook nil
  272. "Mode hook for Org-mode, run after the mode was turned on."
  273. :group 'org
  274. :type 'hook)
  275. (defcustom org-load-hook nil
  276. "Hook that is run after org.el has been loaded."
  277. :group 'org
  278. :type 'hook)
  279. (defcustom org-log-buffer-setup-hook nil
  280. "Hook that is run after an Org log buffer is created."
  281. :group 'org
  282. :version "24.1"
  283. :type 'hook)
  284. (defvar org-modules) ; defined below
  285. (defvar org-modules-loaded nil
  286. "Have the modules been loaded already?")
  287. (defun org-load-modules-maybe (&optional force)
  288. "Load all extensions listed in `org-modules'."
  289. (when (or force (not org-modules-loaded))
  290. (mapc (lambda (ext)
  291. (condition-case nil (require ext)
  292. (error (message "Problems while trying to load feature `%s'" ext))))
  293. org-modules)
  294. (setq org-modules-loaded t)))
  295. (defun org-set-modules (var value)
  296. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  297. (set var value)
  298. (when (featurep 'org)
  299. (org-load-modules-maybe 'force)))
  300. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  301. "Modules that should always be loaded together with org.el.
  302. If a description starts with <C>, the file is not part of Emacs
  303. and loading it will require that you have downloaded and properly
  304. installed the Org mode distribution.
  305. You can also use this system to load external packages (i.e. neither Org
  306. core modules, nor modules from the CONTRIB directory). Just add symbols
  307. to the end of the list. If the package is called org-xyz.el, then you need
  308. to add the symbol `xyz', and the package must have a call to:
  309. \(provide 'org-xyz)
  310. For export specific modules, see also `org-export-backends'."
  311. :group 'org
  312. :set 'org-set-modules
  313. :version "24.4"
  314. :package-version '(Org . "8.0")
  315. :type
  316. '(set :greedy t
  317. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  318. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  319. (const :tag " crypt: Encryption of subtrees" org-crypt)
  320. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  321. (const :tag " docview: Links to doc-view buffers" org-docview)
  322. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  323. (const :tag " id: Global IDs for identifying entries" org-id)
  324. (const :tag " info: Links to Info nodes" org-info)
  325. (const :tag " habit: Track your consistency with habits" org-habit)
  326. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  327. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  328. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  329. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  330. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  331. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  332. (const :tag " mouse: Additional mouse support" org-mouse)
  333. (const :tag "C vm: Links to VM folders/messages" org-vm)
  334. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  335. (const :tag "C w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  336. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  337. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  338. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  339. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  340. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  341. (const :tag "C collector: Collect properties into tables" org-collector)
  342. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  343. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  344. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  345. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  346. (const :tag "C eval: Include command output as text" org-eval)
  347. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  348. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  349. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  350. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  351. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  352. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  353. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  354. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  355. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  356. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  357. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  358. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  359. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  360. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  361. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  362. (const :tag "C registry: A registry for Org-mode links" org-registry)
  363. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  364. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  365. (const :tag "C secretary: Team management with org-mode" org-secretary)
  366. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  367. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  368. (const :tag "C track: Keep up with Org-mode development" org-track)
  369. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  370. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  371. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  372. (defvar org-export-registered-backends) ; From ox.el
  373. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  374. (defcustom org-export-backends '(ascii html icalendar latex)
  375. "List of export back-ends that should be always available.
  376. If a description starts with <C>, the file is not part of Emacs
  377. and loading it will require that you have downloaded and properly
  378. installed the Org mode distribution.
  379. Unlike to `org-modules', libraries in this list will not be
  380. loaded along with Org, but only once the export framework is
  381. needed.
  382. This variable needs to be set before org.el is loaded. If you
  383. need to make a change while Emacs is running, use the customize
  384. interface or run the following code, where VALUE stands for the
  385. new value of the variable, after updating it:
  386. \(progn
  387. \(setq org-export-registered-backends
  388. \(org-remove-if-not
  389. \(lambda (backend)
  390. \(or (memq backend val)
  391. \(catch 'parentp
  392. \(mapc
  393. \(lambda (b)
  394. \(and (org-export-derived-backend-p b (car backend))
  395. \(throw 'parentp t)))
  396. val)
  397. nil)))
  398. org-export-registered-backends))
  399. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  400. \(dolist (backend val)
  401. \(cond
  402. \((not (load (format \"ox-%s\" backend) t t))
  403. \(message \"Problems while trying to load export back-end `%s'\"
  404. backend))
  405. \((not (memq backend new-list)) (push backend new-list))))
  406. \(set-default var new-list)))
  407. Adding a back-end to this list will also pull the back-end it
  408. depends on, if any."
  409. :group 'org
  410. :group 'org-export
  411. :version "24.4"
  412. :package-version '(Org . "8.0")
  413. :initialize 'custom-initialize-set
  414. :set (lambda (var val)
  415. (if (not (featurep 'ox)) (set-default var val)
  416. ;; Any back-end not required anymore (not present in VAL and not
  417. ;; a parent of any back-end in the new value) is removed from the
  418. ;; list of registered back-ends.
  419. (setq org-export-registered-backends
  420. (org-remove-if-not
  421. (lambda (backend)
  422. (or (memq backend val)
  423. (catch 'parentp
  424. (mapc
  425. (lambda (b)
  426. (and (org-export-derived-backend-p b (car backend))
  427. (throw 'parentp t)))
  428. val)
  429. nil)))
  430. org-export-registered-backends))
  431. ;; Now build NEW-LIST of both new back-ends and required
  432. ;; parents.
  433. (let ((new-list (mapcar 'car org-export-registered-backends)))
  434. (dolist (backend val)
  435. (cond
  436. ((not (load (format "ox-%s" backend) t t))
  437. (message "Problems while trying to load export back-end `%s'"
  438. backend))
  439. ((not (memq backend new-list)) (push backend new-list))))
  440. ;; Set VAR to that list with fixed dependencies.
  441. (set-default var new-list))))
  442. :type '(set :greedy t
  443. (const :tag " ascii Export buffer to ASCII format" ascii)
  444. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  445. (const :tag " html Export buffer to HTML format" html)
  446. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  447. (const :tag " latex Export buffer to LaTeX format" latex)
  448. (const :tag " man Export buffer to MAN format" man)
  449. (const :tag " md Export buffer to Markdown format" md)
  450. (const :tag " odt Export buffer to ODT format" odt)
  451. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  452. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  453. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  454. (const :tag "C groff Export buffer to Groff format" groff)
  455. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  456. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  457. (eval-after-load 'ox
  458. '(mapc
  459. (lambda (backend)
  460. (condition-case nil (require (intern (format "ox-%s" backend)))
  461. (error (message "Problems while trying to load export back-end `%s'"
  462. backend))))
  463. org-export-backends))
  464. (defcustom org-support-shift-select nil
  465. "Non-nil means make shift-cursor commands select text when possible.
  466. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  467. start selecting a region, or enlarge regions started in this way.
  468. In Org-mode, in special contexts, these same keys are used for
  469. other purposes, important enough to compete with shift selection.
  470. Org tries to balance these needs by supporting `shift-select-mode'
  471. outside these special contexts, under control of this variable.
  472. The default of this variable is nil, to avoid confusing behavior. Shifted
  473. cursor keys will then execute Org commands in the following contexts:
  474. - on a headline, changing TODO state (left/right) and priority (up/down)
  475. - on a time stamp, changing the time
  476. - in a plain list item, changing the bullet type
  477. - in a property definition line, switching between allowed values
  478. - in the BEGIN line of a clock table (changing the time block).
  479. Outside these contexts, the commands will throw an error.
  480. When this variable is t and the cursor is not in a special
  481. context, Org-mode will support shift-selection for making and
  482. enlarging regions. To make this more effective, the bullet
  483. cycling will no longer happen anywhere in an item line, but only
  484. if the cursor is exactly on the bullet.
  485. If you set this variable to the symbol `always', then the keys
  486. will not be special in headlines, property lines, and item lines,
  487. to make shift selection work there as well. If this is what you
  488. want, you can use the following alternative commands: `C-c C-t'
  489. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  490. can be used to switch TODO sets, `C-c -' to cycle item bullet
  491. types, and properties can be edited by hand or in column view.
  492. However, when the cursor is on a timestamp, shift-cursor commands
  493. will still edit the time stamp - this is just too good to give up.
  494. XEmacs user should have this variable set to nil, because
  495. `shift-select-mode' is in Emacs 23 or later only."
  496. :group 'org
  497. :type '(choice
  498. (const :tag "Never" nil)
  499. (const :tag "When outside special context" t)
  500. (const :tag "Everywhere except timestamps" always)))
  501. (defcustom org-loop-over-headlines-in-active-region nil
  502. "Shall some commands act upon headlines in the active region?
  503. When set to `t', some commands will be performed in all headlines
  504. within the active region.
  505. When set to `start-level', some commands will be performed in all
  506. headlines within the active region, provided that these headlines
  507. are of the same level than the first one.
  508. When set to a string, those commands will be performed on the
  509. matching headlines within the active region. Such string must be
  510. a tags/property/todo match as it is used in the agenda tags view.
  511. The list of commands is: `org-schedule', `org-deadline',
  512. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  513. `org-archive-to-archive-sibling'. The archiving commands skip
  514. already archived entries."
  515. :type '(choice (const :tag "Don't loop" nil)
  516. (const :tag "All headlines in active region" t)
  517. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  518. (string :tag "Tags/Property/Todo matcher"))
  519. :version "24.1"
  520. :group 'org-todo
  521. :group 'org-archive)
  522. (defgroup org-startup nil
  523. "Options concerning startup of Org-mode."
  524. :tag "Org Startup"
  525. :group 'org)
  526. (defcustom org-startup-folded t
  527. "Non-nil means entering Org-mode will switch to OVERVIEW.
  528. This can also be configured on a per-file basis by adding one of
  529. the following lines anywhere in the buffer:
  530. #+STARTUP: fold (or `overview', this is equivalent)
  531. #+STARTUP: nofold (or `showall', this is equivalent)
  532. #+STARTUP: content
  533. #+STARTUP: showeverything
  534. By default, this option is ignored when Org opens agenda files
  535. for the first time. If you want the agenda to honor the startup
  536. option, set `org-agenda-inhibit-startup' to nil."
  537. :group 'org-startup
  538. :type '(choice
  539. (const :tag "nofold: show all" nil)
  540. (const :tag "fold: overview" t)
  541. (const :tag "content: all headlines" content)
  542. (const :tag "show everything, even drawers" showeverything)))
  543. (defcustom org-startup-truncated t
  544. "Non-nil means entering Org-mode will set `truncate-lines'.
  545. This is useful since some lines containing links can be very long and
  546. uninteresting. Also tables look terrible when wrapped."
  547. :group 'org-startup
  548. :type 'boolean)
  549. (defcustom org-startup-indented nil
  550. "Non-nil means turn on `org-indent-mode' on startup.
  551. This can also be configured on a per-file basis by adding one of
  552. the following lines anywhere in the buffer:
  553. #+STARTUP: indent
  554. #+STARTUP: noindent"
  555. :group 'org-structure
  556. :type '(choice
  557. (const :tag "Not" nil)
  558. (const :tag "Globally (slow on startup in large files)" t)))
  559. (defcustom org-use-sub-superscripts t
  560. "Non-nil means interpret \"_\" and \"^\" for display.
  561. When this option is turned on, you can use TeX-like syntax for sub- and
  562. superscripts. Several characters after \"_\" or \"^\" will be
  563. considered as a single item - so grouping with {} is normally not
  564. needed. For example, the following things will be parsed as single
  565. sub- or superscripts.
  566. 10^24 or 10^tau several digits will be considered 1 item.
  567. 10^-12 or 10^-tau a leading sign with digits or a word
  568. x^2-y^3 will be read as x^2 - y^3, because items are
  569. terminated by almost any nonword/nondigit char.
  570. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  571. Still, ambiguity is possible - so when in doubt use {} to enclose
  572. the sub/superscript. If you set this variable to the symbol
  573. `{}', the braces are *required* in order to trigger
  574. interpretations as sub/superscript. This can be helpful in
  575. documents that need \"_\" frequently in plain text."
  576. :group 'org-startup
  577. :version "24.1"
  578. :type '(choice
  579. (const :tag "Always interpret" t)
  580. (const :tag "Only with braces" {})
  581. (const :tag "Never interpret" nil)))
  582. (defcustom org-startup-with-beamer-mode nil
  583. "Non-nil means turn on `org-beamer-mode' on startup.
  584. This can also be configured on a per-file basis by adding one of
  585. the following lines anywhere in the buffer:
  586. #+STARTUP: beamer"
  587. :group 'org-startup
  588. :version "24.1"
  589. :type 'boolean)
  590. (defcustom org-startup-align-all-tables nil
  591. "Non-nil means align all tables when visiting a file.
  592. This is useful when the column width in tables is forced with <N> cookies
  593. in table fields. Such tables will look correct only after the first re-align.
  594. This can also be configured on a per-file basis by adding one of
  595. the following lines anywhere in the buffer:
  596. #+STARTUP: align
  597. #+STARTUP: noalign"
  598. :group 'org-startup
  599. :type 'boolean)
  600. (defcustom org-startup-with-inline-images nil
  601. "Non-nil means show inline images when loading a new Org file.
  602. This can also be configured on a per-file basis by adding one of
  603. the following lines anywhere in the buffer:
  604. #+STARTUP: inlineimages
  605. #+STARTUP: noinlineimages"
  606. :group 'org-startup
  607. :version "24.1"
  608. :type 'boolean)
  609. (defcustom org-startup-with-latex-preview nil
  610. "Non-nil means preview LaTeX fragments when loading a new Org file.
  611. This can also be configured on a per-file basis by adding one of
  612. the followinglines anywhere in the buffer:
  613. #+STARTUP: latexpreview
  614. #+STARTUP: nolatexpreview"
  615. :group 'org-startup
  616. :version "24.4"
  617. :package-version '(Org . "8.0")
  618. :type 'boolean)
  619. (defcustom org-insert-mode-line-in-empty-file nil
  620. "Non-nil means insert the first line setting Org-mode in empty files.
  621. When the function `org-mode' is called interactively in an empty file, this
  622. normally means that the file name does not automatically trigger Org-mode.
  623. To ensure that the file will always be in Org-mode in the future, a
  624. line enforcing Org-mode will be inserted into the buffer, if this option
  625. has been set."
  626. :group 'org-startup
  627. :type 'boolean)
  628. (defcustom org-replace-disputed-keys nil
  629. "Non-nil means use alternative key bindings for some keys.
  630. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  631. These keys are also used by other packages like shift-selection-mode'
  632. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  633. If you want to use Org-mode together with one of these other modes,
  634. or more generally if you would like to move some Org-mode commands to
  635. other keys, set this variable and configure the keys with the variable
  636. `org-disputed-keys'.
  637. This option is only relevant at load-time of Org-mode, and must be set
  638. *before* org.el is loaded. Changing it requires a restart of Emacs to
  639. become effective."
  640. :group 'org-startup
  641. :type 'boolean)
  642. (defcustom org-use-extra-keys nil
  643. "Non-nil means use extra key sequence definitions for certain commands.
  644. This happens automatically if you run XEmacs or if `window-system'
  645. is nil. This variable lets you do the same manually. You must
  646. set it before loading org.
  647. Example: on Carbon Emacs 22 running graphically, with an external
  648. keyboard on a Powerbook, the default way of setting M-left might
  649. not work for either Alt or ESC. Setting this variable will make
  650. it work for ESC."
  651. :group 'org-startup
  652. :type 'boolean)
  653. (if (fboundp 'defvaralias)
  654. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  655. (defcustom org-disputed-keys
  656. '(([(shift up)] . [(meta p)])
  657. ([(shift down)] . [(meta n)])
  658. ([(shift left)] . [(meta -)])
  659. ([(shift right)] . [(meta +)])
  660. ([(control shift right)] . [(meta shift +)])
  661. ([(control shift left)] . [(meta shift -)]))
  662. "Keys for which Org-mode and other modes compete.
  663. This is an alist, cars are the default keys, second element specifies
  664. the alternative to use when `org-replace-disputed-keys' is t.
  665. Keys can be specified in any syntax supported by `define-key'.
  666. The value of this option takes effect only at Org-mode's startup,
  667. therefore you'll have to restart Emacs to apply it after changing."
  668. :group 'org-startup
  669. :type 'alist)
  670. (defun org-key (key)
  671. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  672. Or return the original if not disputed.
  673. Also apply the translations defined in `org-xemacs-key-equivalents'."
  674. (when org-replace-disputed-keys
  675. (let* ((nkey (key-description key))
  676. (x (org-find-if (lambda (x)
  677. (equal (key-description (car x)) nkey))
  678. org-disputed-keys)))
  679. (setq key (if x (cdr x) key))))
  680. (when (featurep 'xemacs)
  681. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  682. key)
  683. (defun org-find-if (predicate seq)
  684. (catch 'exit
  685. (while seq
  686. (if (funcall predicate (car seq))
  687. (throw 'exit (car seq))
  688. (pop seq)))))
  689. (defun org-defkey (keymap key def)
  690. "Define a key, possibly translated, as returned by `org-key'."
  691. (define-key keymap (org-key key) def))
  692. (defcustom org-ellipsis nil
  693. "The ellipsis to use in the Org-mode outline.
  694. When nil, just use the standard three dots. When a string, use that instead,
  695. When a face, use the standard 3 dots, but with the specified face.
  696. The change affects only Org-mode (which will then use its own display table).
  697. Changing this requires executing `M-x org-mode' in a buffer to become
  698. effective."
  699. :group 'org-startup
  700. :type '(choice (const :tag "Default" nil)
  701. (face :tag "Face" :value org-warning)
  702. (string :tag "String" :value "...#")))
  703. (defvar org-display-table nil
  704. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  705. (defgroup org-keywords nil
  706. "Keywords in Org-mode."
  707. :tag "Org Keywords"
  708. :group 'org)
  709. (defcustom org-deadline-string "DEADLINE:"
  710. "String to mark deadline entries.
  711. A deadline is this string, followed by a time stamp. Should be a word,
  712. terminated by a colon. You can insert a schedule keyword and
  713. a timestamp with \\[org-deadline].
  714. Changes become only effective after restarting Emacs."
  715. :group 'org-keywords
  716. :type 'string)
  717. (defcustom org-scheduled-string "SCHEDULED:"
  718. "String to mark scheduled TODO entries.
  719. A schedule is this string, followed by a time stamp. Should be a word,
  720. terminated by a colon. You can insert a schedule keyword and
  721. a timestamp with \\[org-schedule].
  722. Changes become only effective after restarting Emacs."
  723. :group 'org-keywords
  724. :type 'string)
  725. (defcustom org-closed-string "CLOSED:"
  726. "String used as the prefix for timestamps logging closing a TODO entry."
  727. :group 'org-keywords
  728. :type 'string)
  729. (defcustom org-clock-string "CLOCK:"
  730. "String used as prefix for timestamps clocking work hours on an item."
  731. :group 'org-keywords
  732. :type 'string)
  733. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  734. org-scheduled-string "\\|"
  735. org-deadline-string "\\|"
  736. org-closed-string "\\|"
  737. org-clock-string "\\)")
  738. "Matches a line with planning or clock info.")
  739. (defcustom org-comment-string "COMMENT"
  740. "Entries starting with this keyword will never be exported.
  741. An entry can be toggled between COMMENT and normal with
  742. \\[org-toggle-comment].
  743. Changes become only effective after restarting Emacs."
  744. :group 'org-keywords
  745. :type 'string)
  746. (defcustom org-quote-string "QUOTE"
  747. "Entries starting with this keyword will be exported in fixed-width font.
  748. Quoting applies only to the text in the entry following the headline, and does
  749. not extend beyond the next headline, even if that is lower level.
  750. An entry can be toggled between QUOTE and normal with
  751. \\[org-toggle-fixed-width-section]."
  752. :group 'org-keywords
  753. :type 'string)
  754. (defconst org-repeat-re
  755. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  756. "Regular expression for specifying repeated events.
  757. After a match, group 1 contains the repeat expression.")
  758. (defgroup org-structure nil
  759. "Options concerning the general structure of Org-mode files."
  760. :tag "Org Structure"
  761. :group 'org)
  762. (defgroup org-reveal-location nil
  763. "Options about how to make context of a location visible."
  764. :tag "Org Reveal Location"
  765. :group 'org-structure)
  766. (defconst org-context-choice
  767. '(choice
  768. (const :tag "Always" t)
  769. (const :tag "Never" nil)
  770. (repeat :greedy t :tag "Individual contexts"
  771. (cons
  772. (choice :tag "Context"
  773. (const agenda)
  774. (const org-goto)
  775. (const occur-tree)
  776. (const tags-tree)
  777. (const link-search)
  778. (const mark-goto)
  779. (const bookmark-jump)
  780. (const isearch)
  781. (const default))
  782. (boolean))))
  783. "Contexts for the reveal options.")
  784. (defcustom org-show-hierarchy-above '((default . t))
  785. "Non-nil means show full hierarchy when revealing a location.
  786. Org-mode often shows locations in an org-mode file which might have
  787. been invisible before. When this is set, the hierarchy of headings
  788. above the exposed location is shown.
  789. Turning this off for example for sparse trees makes them very compact.
  790. Instead of t, this can also be an alist specifying this option for different
  791. contexts. Valid contexts are
  792. agenda when exposing an entry from the agenda
  793. org-goto when using the command `org-goto' on key C-c C-j
  794. occur-tree when using the command `org-occur' on key C-c /
  795. tags-tree when constructing a sparse tree based on tags matches
  796. link-search when exposing search matches associated with a link
  797. mark-goto when exposing the jump goal of a mark
  798. bookmark-jump when exposing a bookmark location
  799. isearch when exiting from an incremental search
  800. default default for all contexts not set explicitly"
  801. :group 'org-reveal-location
  802. :type org-context-choice)
  803. (defcustom org-show-following-heading '((default . nil))
  804. "Non-nil means show following heading when revealing a location.
  805. Org-mode often shows locations in an org-mode file which might have
  806. been invisible before. When this is set, the heading following the
  807. match is shown.
  808. Turning this off for example for sparse trees makes them very compact,
  809. but makes it harder to edit the location of the match. In such a case,
  810. use the command \\[org-reveal] to show more context.
  811. Instead of t, this can also be an alist specifying this option for different
  812. contexts. See `org-show-hierarchy-above' for valid contexts."
  813. :group 'org-reveal-location
  814. :type org-context-choice)
  815. (defcustom org-show-siblings '((default . nil) (isearch t))
  816. "Non-nil means show all sibling heading when revealing a location.
  817. Org-mode often shows locations in an org-mode file which might have
  818. been invisible before. When this is set, the sibling of the current entry
  819. heading are all made visible. If `org-show-hierarchy-above' is t,
  820. the same happens on each level of the hierarchy above the current entry.
  821. By default this is on for the isearch context, off for all other contexts.
  822. Turning this off for example for sparse trees makes them very compact,
  823. but makes it harder to edit the location of the match. In such a case,
  824. use the command \\[org-reveal] to show more context.
  825. Instead of t, this can also be an alist specifying this option for different
  826. contexts. See `org-show-hierarchy-above' for valid contexts."
  827. :group 'org-reveal-location
  828. :type org-context-choice)
  829. (defcustom org-show-entry-below '((default . nil))
  830. "Non-nil means show the entry below a headline when revealing a location.
  831. Org-mode often shows locations in an org-mode file which might have
  832. been invisible before. When this is set, the text below the headline that is
  833. exposed is also shown.
  834. By default this is off for all contexts.
  835. Instead of t, this can also be an alist specifying this option for different
  836. contexts. See `org-show-hierarchy-above' for valid contexts."
  837. :group 'org-reveal-location
  838. :type org-context-choice)
  839. (defcustom org-indirect-buffer-display 'other-window
  840. "How should indirect tree buffers be displayed?
  841. This applies to indirect buffers created with the commands
  842. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  843. Valid values are:
  844. current-window Display in the current window
  845. other-window Just display in another window.
  846. dedicated-frame Create one new frame, and re-use it each time.
  847. new-frame Make a new frame each time. Note that in this case
  848. previously-made indirect buffers are kept, and you need to
  849. kill these buffers yourself."
  850. :group 'org-structure
  851. :group 'org-agenda-windows
  852. :type '(choice
  853. (const :tag "In current window" current-window)
  854. (const :tag "In current frame, other window" other-window)
  855. (const :tag "Each time a new frame" new-frame)
  856. (const :tag "One dedicated frame" dedicated-frame)))
  857. (defcustom org-use-speed-commands nil
  858. "Non-nil means activate single letter commands at beginning of a headline.
  859. This may also be a function to test for appropriate locations where speed
  860. commands should be active."
  861. :group 'org-structure
  862. :type '(choice
  863. (const :tag "Never" nil)
  864. (const :tag "At beginning of headline stars" t)
  865. (function)))
  866. (defcustom org-speed-commands-user nil
  867. "Alist of additional speed commands.
  868. This list will be checked before `org-speed-commands-default'
  869. when the variable `org-use-speed-commands' is non-nil
  870. and when the cursor is at the beginning of a headline.
  871. The car if each entry is a string with a single letter, which must
  872. be assigned to `self-insert-command' in the global map.
  873. The cdr is either a command to be called interactively, a function
  874. to be called, or a form to be evaluated.
  875. An entry that is just a list with a single string will be interpreted
  876. as a descriptive headline that will be added when listing the speed
  877. commands in the Help buffer using the `?' speed command."
  878. :group 'org-structure
  879. :type '(repeat :value ("k" . ignore)
  880. (choice :value ("k" . ignore)
  881. (list :tag "Descriptive Headline" (string :tag "Headline"))
  882. (cons :tag "Letter and Command"
  883. (string :tag "Command letter")
  884. (choice
  885. (function)
  886. (sexp))))))
  887. (defgroup org-cycle nil
  888. "Options concerning visibility cycling in Org-mode."
  889. :tag "Org Cycle"
  890. :group 'org-structure)
  891. (defcustom org-cycle-skip-children-state-if-no-children t
  892. "Non-nil means skip CHILDREN state in entries that don't have any."
  893. :group 'org-cycle
  894. :type 'boolean)
  895. (defcustom org-cycle-max-level nil
  896. "Maximum level which should still be subject to visibility cycling.
  897. Levels higher than this will, for cycling, be treated as text, not a headline.
  898. When `org-odd-levels-only' is set, a value of N in this variable actually
  899. means 2N-1 stars as the limiting headline.
  900. When nil, cycle all levels.
  901. Note that the limiting level of cycling is also influenced by
  902. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  903. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  904. than its value."
  905. :group 'org-cycle
  906. :type '(choice
  907. (const :tag "No limit" nil)
  908. (integer :tag "Maximum level")))
  909. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  910. "Names of drawers. Drawers are not opened by cycling on the headline above.
  911. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  912. this:
  913. :DRAWERNAME:
  914. .....
  915. :END:
  916. The drawer \"PROPERTIES\" is special for capturing properties through
  917. the property API.
  918. Drawers can be defined on the per-file basis with a line like:
  919. #+DRAWERS: HIDDEN STATE PROPERTIES"
  920. :group 'org-structure
  921. :group 'org-cycle
  922. :type '(repeat (string :tag "Drawer Name")))
  923. (defcustom org-hide-block-startup nil
  924. "Non-nil means entering Org-mode will fold all blocks.
  925. This can also be set in on a per-file basis with
  926. #+STARTUP: hideblocks
  927. #+STARTUP: showblocks"
  928. :group 'org-startup
  929. :group 'org-cycle
  930. :type 'boolean)
  931. (defcustom org-cycle-global-at-bob nil
  932. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  933. This makes it possible to do global cycling without having to use S-TAB or
  934. \\[universal-argument] TAB. For this special case to work, the first line
  935. of the buffer must not be a headline -- it may be empty or some other text.
  936. When used in this way, `org-cycle-hook' is disabled temporarily to make
  937. sure the cursor stays at the beginning of the buffer. When this option is
  938. nil, don't do anything special at the beginning of the buffer."
  939. :group 'org-cycle
  940. :type 'boolean)
  941. (defcustom org-cycle-level-after-item/entry-creation t
  942. "Non-nil means cycle entry level or item indentation in new empty entries.
  943. When the cursor is at the end of an empty headline, i.e., with only stars
  944. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  945. and then all possible ancestor states, before returning to the original state.
  946. This makes data entry extremely fast: M-RET to create a new headline,
  947. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  948. When the cursor is at the end of an empty plain list item, one TAB will
  949. make it a subitem, two or more tabs will back up to make this an item
  950. higher up in the item hierarchy."
  951. :group 'org-cycle
  952. :type 'boolean)
  953. (defcustom org-cycle-emulate-tab t
  954. "Where should `org-cycle' emulate TAB.
  955. nil Never
  956. white Only in completely white lines
  957. whitestart Only at the beginning of lines, before the first non-white char
  958. t Everywhere except in headlines
  959. exc-hl-bol Everywhere except at the start of a headline
  960. If TAB is used in a place where it does not emulate TAB, the current subtree
  961. visibility is cycled."
  962. :group 'org-cycle
  963. :type '(choice (const :tag "Never" nil)
  964. (const :tag "Only in completely white lines" white)
  965. (const :tag "Before first char in a line" whitestart)
  966. (const :tag "Everywhere except in headlines" t)
  967. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  968. ))
  969. (defcustom org-cycle-separator-lines 2
  970. "Number of empty lines needed to keep an empty line between collapsed trees.
  971. If you leave an empty line between the end of a subtree and the following
  972. headline, this empty line is hidden when the subtree is folded.
  973. Org-mode will leave (exactly) one empty line visible if the number of
  974. empty lines is equal or larger to the number given in this variable.
  975. So the default 2 means at least 2 empty lines after the end of a subtree
  976. are needed to produce free space between a collapsed subtree and the
  977. following headline.
  978. If the number is negative, and the number of empty lines is at least -N,
  979. all empty lines are shown.
  980. Special case: when 0, never leave empty lines in collapsed view."
  981. :group 'org-cycle
  982. :type 'integer)
  983. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  984. (defcustom org-pre-cycle-hook nil
  985. "Hook that is run before visibility cycling is happening.
  986. The function(s) in this hook must accept a single argument which indicates
  987. the new state that will be set right after running this hook. The
  988. argument is a symbol. Before a global state change, it can have the values
  989. `overview', `content', or `all'. Before a local state change, it can have
  990. the values `folded', `children', or `subtree'."
  991. :group 'org-cycle
  992. :type 'hook)
  993. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  994. org-cycle-hide-drawers
  995. org-cycle-hide-inline-tasks
  996. org-cycle-show-empty-lines
  997. org-optimize-window-after-visibility-change)
  998. "Hook that is run after `org-cycle' has changed the buffer visibility.
  999. The function(s) in this hook must accept a single argument which indicates
  1000. the new state that was set by the most recent `org-cycle' command. The
  1001. argument is a symbol. After a global state change, it can have the values
  1002. `overview', `contents', or `all'. After a local state change, it can have
  1003. the values `folded', `children', or `subtree'."
  1004. :group 'org-cycle
  1005. :type 'hook)
  1006. (defgroup org-edit-structure nil
  1007. "Options concerning structure editing in Org-mode."
  1008. :tag "Org Edit Structure"
  1009. :group 'org-structure)
  1010. (defcustom org-odd-levels-only nil
  1011. "Non-nil means skip even levels and only use odd levels for the outline.
  1012. This has the effect that two stars are being added/taken away in
  1013. promotion/demotion commands. It also influences how levels are
  1014. handled by the exporters.
  1015. Changing it requires restart of `font-lock-mode' to become effective
  1016. for fontification also in regions already fontified.
  1017. You may also set this on a per-file basis by adding one of the following
  1018. lines to the buffer:
  1019. #+STARTUP: odd
  1020. #+STARTUP: oddeven"
  1021. :group 'org-edit-structure
  1022. :group 'org-appearance
  1023. :type 'boolean)
  1024. (defcustom org-adapt-indentation t
  1025. "Non-nil means adapt indentation to outline node level.
  1026. When this variable is set, Org assumes that you write outlines by
  1027. indenting text in each node to align with the headline (after the stars).
  1028. The following issues are influenced by this variable:
  1029. - When this is set and the *entire* text in an entry is indented, the
  1030. indentation is increased by one space in a demotion command, and
  1031. decreased by one in a promotion command. If any line in the entry
  1032. body starts with text at column 0, indentation is not changed at all.
  1033. - Property drawers and planning information is inserted indented when
  1034. this variable s set. When nil, they will not be indented.
  1035. - TAB indents a line relative to context. The lines below a headline
  1036. will be indented when this variable is set.
  1037. Note that this is all about true indentation, by adding and removing
  1038. space characters. See also `org-indent.el' which does level-dependent
  1039. indentation in a virtual way, i.e. at display time in Emacs."
  1040. :group 'org-edit-structure
  1041. :type 'boolean)
  1042. (defcustom org-special-ctrl-a/e nil
  1043. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1044. When t, `C-a' will bring back the cursor to the beginning of the
  1045. headline text, i.e. after the stars and after a possible TODO
  1046. keyword. In an item, this will be the position after bullet and
  1047. check-box, if any. When the cursor is already at that position,
  1048. another `C-a' will bring it to the beginning of the line.
  1049. `C-e' will jump to the end of the headline, ignoring the presence
  1050. of tags in the headline. A second `C-e' will then jump to the
  1051. true end of the line, after any tags. This also means that, when
  1052. this variable is non-nil, `C-e' also will never jump beyond the
  1053. end of the heading of a folded section, i.e. not after the
  1054. ellipses.
  1055. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1056. normally, going to the true line boundary first. Only a directly
  1057. following, identical keypress will bring the cursor to the
  1058. special positions.
  1059. This may also be a cons cell where the behavior for `C-a' and
  1060. `C-e' is set separately."
  1061. :group 'org-edit-structure
  1062. :type '(choice
  1063. (const :tag "off" nil)
  1064. (const :tag "on: after stars/bullet and before tags first" t)
  1065. (const :tag "reversed: true line boundary first" reversed)
  1066. (cons :tag "Set C-a and C-e separately"
  1067. (choice :tag "Special C-a"
  1068. (const :tag "off" nil)
  1069. (const :tag "on: after stars/bullet first" t)
  1070. (const :tag "reversed: before stars/bullet first" reversed))
  1071. (choice :tag "Special C-e"
  1072. (const :tag "off" nil)
  1073. (const :tag "on: before tags first" t)
  1074. (const :tag "reversed: after tags first" reversed)))))
  1075. (if (fboundp 'defvaralias)
  1076. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  1077. (defcustom org-special-ctrl-k nil
  1078. "Non-nil means `C-k' will behave specially in headlines.
  1079. When nil, `C-k' will call the default `kill-line' command.
  1080. When t, the following will happen while the cursor is in the headline:
  1081. - When the cursor is at the beginning of a headline, kill the entire
  1082. line and possible the folded subtree below the line.
  1083. - When in the middle of the headline text, kill the headline up to the tags.
  1084. - When after the headline text, kill the tags."
  1085. :group 'org-edit-structure
  1086. :type 'boolean)
  1087. (defcustom org-ctrl-k-protect-subtree nil
  1088. "Non-nil means, do not delete a hidden subtree with C-k.
  1089. When set to the symbol `error', simply throw an error when C-k is
  1090. used to kill (part-of) a headline that has hidden text behind it.
  1091. Any other non-nil value will result in a query to the user, if it is
  1092. OK to kill that hidden subtree. When nil, kill without remorse."
  1093. :group 'org-edit-structure
  1094. :version "24.1"
  1095. :type '(choice
  1096. (const :tag "Do not protect hidden subtrees" nil)
  1097. (const :tag "Protect hidden subtrees with a security query" t)
  1098. (const :tag "Never kill a hidden subtree with C-k" error)))
  1099. (defcustom org-catch-invisible-edits nil
  1100. "Check if in invisible region before inserting or deleting a character.
  1101. Valid values are:
  1102. nil Do not check, so just do invisible edits.
  1103. error Throw an error and do nothing.
  1104. show Make point visible, and do the requested edit.
  1105. show-and-error Make point visible, then throw an error and abort the edit.
  1106. smart Make point visible, and do insertion/deletion if it is
  1107. adjacent to visible text and the change feels predictable.
  1108. Never delete a previously invisible character or add in the
  1109. middle or right after an invisible region. Basically, this
  1110. allows insertion and backward-delete right before ellipses.
  1111. FIXME: maybe in this case we should not even show?"
  1112. :group 'org-edit-structure
  1113. :version "24.1"
  1114. :type '(choice
  1115. (const :tag "Do not check" nil)
  1116. (const :tag "Throw error when trying to edit" error)
  1117. (const :tag "Unhide, but do not do the edit" show-and-error)
  1118. (const :tag "Show invisible part and do the edit" show)
  1119. (const :tag "Be smart and do the right thing" smart)))
  1120. (defcustom org-yank-folded-subtrees t
  1121. "Non-nil means when yanking subtrees, fold them.
  1122. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1123. it starts with a heading and all other headings in it are either children
  1124. or siblings, then fold all the subtrees. However, do this only if no
  1125. text after the yank would be swallowed into a folded tree by this action."
  1126. :group 'org-edit-structure
  1127. :type 'boolean)
  1128. (defcustom org-yank-adjusted-subtrees nil
  1129. "Non-nil means when yanking subtrees, adjust the level.
  1130. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1131. this function for details."
  1132. :group 'org-edit-structure
  1133. :type 'boolean)
  1134. (defcustom org-M-RET-may-split-line '((default . t))
  1135. "Non-nil means M-RET will split the line at the cursor position.
  1136. When nil, it will go to the end of the line before making a
  1137. new line.
  1138. You may also set this option in a different way for different
  1139. contexts. Valid contexts are:
  1140. headline when creating a new headline
  1141. item when creating a new item
  1142. table in a table field
  1143. default the value to be used for all contexts not explicitly
  1144. customized"
  1145. :group 'org-structure
  1146. :group 'org-table
  1147. :type '(choice
  1148. (const :tag "Always" t)
  1149. (const :tag "Never" nil)
  1150. (repeat :greedy t :tag "Individual contexts"
  1151. (cons
  1152. (choice :tag "Context"
  1153. (const headline)
  1154. (const item)
  1155. (const table)
  1156. (const default))
  1157. (boolean)))))
  1158. (defcustom org-insert-heading-respect-content nil
  1159. "Non-nil means insert new headings after the current subtree.
  1160. When nil, the new heading is created directly after the current line.
  1161. The commands \\[org-insert-heading-respect-content] and
  1162. \\[org-insert-todo-heading-respect-content] turn this variable on
  1163. for the duration of the command."
  1164. :group 'org-structure
  1165. :type 'boolean)
  1166. (defcustom org-blank-before-new-entry '((heading . auto)
  1167. (plain-list-item . auto))
  1168. "Should `org-insert-heading' leave a blank line before new heading/item?
  1169. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1170. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1171. which case Org will look at the surrounding headings/items and try to
  1172. make an intelligent decision whether to insert a blank line or not.
  1173. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1174. set, the setting here is ignored and no empty line is inserted, to avoid
  1175. breaking the list structure."
  1176. :group 'org-edit-structure
  1177. :type '(list
  1178. (cons (const heading)
  1179. (choice (const :tag "Never" nil)
  1180. (const :tag "Always" t)
  1181. (const :tag "Auto" auto)))
  1182. (cons (const plain-list-item)
  1183. (choice (const :tag "Never" nil)
  1184. (const :tag "Always" t)
  1185. (const :tag "Auto" auto)))))
  1186. (defcustom org-insert-heading-hook nil
  1187. "Hook being run after inserting a new heading."
  1188. :group 'org-edit-structure
  1189. :type 'hook)
  1190. (defcustom org-enable-fixed-width-editor t
  1191. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1192. This currently only means they are never auto-wrapped.
  1193. When nil, such lines will be treated like ordinary lines.
  1194. See also the QUOTE keyword."
  1195. :group 'org-edit-structure
  1196. :type 'boolean)
  1197. (defcustom org-goto-auto-isearch t
  1198. "Non-nil means typing characters in `org-goto' starts incremental search.
  1199. When nil, you can use these keybindings to navigate the buffer:
  1200. q Quit the org-goto interface
  1201. n Go to the next visible heading
  1202. p Go to the previous visible heading
  1203. f Go one heading forward on same level
  1204. b Go one heading backward on same level
  1205. u Go one heading up"
  1206. :group 'org-edit-structure
  1207. :type 'boolean)
  1208. (defgroup org-sparse-trees nil
  1209. "Options concerning sparse trees in Org-mode."
  1210. :tag "Org Sparse Trees"
  1211. :group 'org-structure)
  1212. (defcustom org-highlight-sparse-tree-matches t
  1213. "Non-nil means highlight all matches that define a sparse tree.
  1214. The highlights will automatically disappear the next time the buffer is
  1215. changed by an edit command."
  1216. :group 'org-sparse-trees
  1217. :type 'boolean)
  1218. (defcustom org-remove-highlights-with-change t
  1219. "Non-nil means any change to the buffer will remove temporary highlights.
  1220. Such highlights are created by `org-occur' and `org-clock-display'.
  1221. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1222. The highlights created by `org-preview-latex-fragment' always need
  1223. `C-c C-c' to be removed."
  1224. :group 'org-sparse-trees
  1225. :group 'org-time
  1226. :type 'boolean)
  1227. (defcustom org-occur-hook '(org-first-headline-recenter)
  1228. "Hook that is run after `org-occur' has constructed a sparse tree.
  1229. This can be used to recenter the window to show as much of the structure
  1230. as possible."
  1231. :group 'org-sparse-trees
  1232. :type 'hook)
  1233. (defgroup org-imenu-and-speedbar nil
  1234. "Options concerning imenu and speedbar in Org-mode."
  1235. :tag "Org Imenu and Speedbar"
  1236. :group 'org-structure)
  1237. (defcustom org-imenu-depth 2
  1238. "The maximum level for Imenu access to Org-mode headlines.
  1239. This also applied for speedbar access."
  1240. :group 'org-imenu-and-speedbar
  1241. :type 'integer)
  1242. (defgroup org-table nil
  1243. "Options concerning tables in Org-mode."
  1244. :tag "Org Table"
  1245. :group 'org)
  1246. (defcustom org-enable-table-editor 'optimized
  1247. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1248. When nil, such lines will be treated like ordinary lines.
  1249. When equal to the symbol `optimized', the table editor will be optimized to
  1250. do the following:
  1251. - Automatic overwrite mode in front of whitespace in table fields.
  1252. This makes the structure of the table stay in tact as long as the edited
  1253. field does not exceed the column width.
  1254. - Minimize the number of realigns. Normally, the table is aligned each time
  1255. TAB or RET are pressed to move to another field. With optimization this
  1256. happens only if changes to a field might have changed the column width.
  1257. Optimization requires replacing the functions `self-insert-command',
  1258. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1259. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1260. very good at guessing when a re-align will be necessary, but you can always
  1261. force one with \\[org-ctrl-c-ctrl-c].
  1262. If you would like to use the optimized version in Org-mode, but the
  1263. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1264. This variable can be used to turn on and off the table editor during a session,
  1265. but in order to toggle optimization, a restart is required.
  1266. See also the variable `org-table-auto-blank-field'."
  1267. :group 'org-table
  1268. :type '(choice
  1269. (const :tag "off" nil)
  1270. (const :tag "on" t)
  1271. (const :tag "on, optimized" optimized)))
  1272. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1273. (version<= emacs-version "24.1"))
  1274. "Non-nil means cluster self-insert commands for undo when possible.
  1275. If this is set, then, like in the Emacs command loop, 20 consecutive
  1276. characters will be undone together.
  1277. This is configurable, because there is some impact on typing performance."
  1278. :group 'org-table
  1279. :type 'boolean)
  1280. (defcustom org-table-tab-recognizes-table.el t
  1281. "Non-nil means TAB will automatically notice a table.el table.
  1282. When it sees such a table, it moves point into it and - if necessary -
  1283. calls `table-recognize-table'."
  1284. :group 'org-table-editing
  1285. :type 'boolean)
  1286. (defgroup org-link nil
  1287. "Options concerning links in Org-mode."
  1288. :tag "Org Link"
  1289. :group 'org)
  1290. (defvar org-link-abbrev-alist-local nil
  1291. "Buffer-local version of `org-link-abbrev-alist', which see.
  1292. The value of this is taken from the #+LINK lines.")
  1293. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1294. (defcustom org-link-abbrev-alist nil
  1295. "Alist of link abbreviations.
  1296. The car of each element is a string, to be replaced at the start of a link.
  1297. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1298. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1299. [[linkkey:tag][description]]
  1300. The 'linkkey' must be a word word, starting with a letter, followed
  1301. by letters, numbers, '-' or '_'.
  1302. If REPLACE is a string, the tag will simply be appended to create the link.
  1303. If the string contains \"%s\", the tag will be inserted there. If the string
  1304. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1305. at that point (see the function `url-hexify-string'). If the string contains
  1306. the specifier \"%(my-function)\", then the custom function `my-function' will
  1307. be invoked: this function takes the tag as its only argument and must return
  1308. a string.
  1309. REPLACE may also be a function that will be called with the tag as the
  1310. only argument to create the link, which should be returned as a string.
  1311. See the manual for examples."
  1312. :group 'org-link
  1313. :type '(repeat
  1314. (cons
  1315. (string :tag "Protocol")
  1316. (choice
  1317. (string :tag "Format")
  1318. (function)))))
  1319. (defcustom org-descriptive-links t
  1320. "Non-nil means Org will display descriptive links.
  1321. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1322. \"Org Website\", hiding the link itself and just displaying its
  1323. description. When set to `nil', Org will display the full links
  1324. literally.
  1325. You can interactively set the value of this variable by calling
  1326. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1327. :group 'org-link
  1328. :type 'boolean)
  1329. (defcustom org-link-file-path-type 'adaptive
  1330. "How the path name in file links should be stored.
  1331. Valid values are:
  1332. relative Relative to the current directory, i.e. the directory of the file
  1333. into which the link is being inserted.
  1334. absolute Absolute path, if possible with ~ for home directory.
  1335. noabbrev Absolute path, no abbreviation of home directory.
  1336. adaptive Use relative path for files in the current directory and sub-
  1337. directories of it. For other files, use an absolute path."
  1338. :group 'org-link
  1339. :type '(choice
  1340. (const relative)
  1341. (const absolute)
  1342. (const noabbrev)
  1343. (const adaptive)))
  1344. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1345. "Types of links that should be activated in Org-mode files.
  1346. This is a list of symbols, each leading to the activation of a certain link
  1347. type. In principle, it does not hurt to turn on most link types - there may
  1348. be a small gain when turning off unused link types. The types are:
  1349. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1350. angle Links in angular brackets that may contain whitespace like
  1351. <bbdb:Carsten Dominik>.
  1352. plain Plain links in normal text, no whitespace, like http://google.com.
  1353. radio Text that is matched by a radio target, see manual for details.
  1354. tag Tag settings in a headline (link to tag search).
  1355. date Time stamps (link to calendar).
  1356. footnote Footnote labels.
  1357. Changing this variable requires a restart of Emacs to become effective."
  1358. :group 'org-link
  1359. :type '(set :greedy t
  1360. (const :tag "Double bracket links" bracket)
  1361. (const :tag "Angular bracket links" angle)
  1362. (const :tag "Plain text links" plain)
  1363. (const :tag "Radio target matches" radio)
  1364. (const :tag "Tags" tag)
  1365. (const :tag "Timestamps" date)
  1366. (const :tag "Footnotes" footnote)))
  1367. (defcustom org-make-link-description-function nil
  1368. "Function to use for generating link descriptions from links.
  1369. When nil, the link location will be used. This function must take
  1370. two parameters: the first one is the link, the second one is the
  1371. description generated by `org-insert-link'. The function should
  1372. return the description to use."
  1373. :group 'org-link
  1374. :type 'function)
  1375. (defgroup org-link-store nil
  1376. "Options concerning storing links in Org-mode."
  1377. :tag "Org Store Link"
  1378. :group 'org-link)
  1379. (defcustom org-url-hexify-p t
  1380. "When non-nil, hexify URL when creating a link."
  1381. :type 'boolean
  1382. :version "24.3"
  1383. :group 'org-link-store)
  1384. (defcustom org-email-link-description-format "Email %c: %.30s"
  1385. "Format of the description part of a link to an email or usenet message.
  1386. The following %-escapes will be replaced by corresponding information:
  1387. %F full \"From\" field
  1388. %f name, taken from \"From\" field, address if no name
  1389. %T full \"To\" field
  1390. %t first name in \"To\" field, address if no name
  1391. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1392. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1393. %s subject
  1394. %d date
  1395. %m message-id.
  1396. You may use normal field width specification between the % and the letter.
  1397. This is for example useful to limit the length of the subject.
  1398. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1399. :group 'org-link-store
  1400. :type 'string)
  1401. (defcustom org-from-is-user-regexp
  1402. (let (r1 r2)
  1403. (when (and user-mail-address (not (string= user-mail-address "")))
  1404. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1405. (when (and user-full-name (not (string= user-full-name "")))
  1406. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1407. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1408. "Regexp matched against the \"From:\" header of an email or usenet message.
  1409. It should match if the message is from the user him/herself."
  1410. :group 'org-link-store
  1411. :type 'regexp)
  1412. (defcustom org-context-in-file-links t
  1413. "Non-nil means file links from `org-store-link' contain context.
  1414. A search string will be added to the file name with :: as separator and
  1415. used to find the context when the link is activated by the command
  1416. `org-open-at-point'. When this option is t, the entire active region
  1417. will be placed in the search string of the file link. If set to a
  1418. positive integer, only the first n lines of context will be stored.
  1419. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1420. negates this setting for the duration of the command."
  1421. :group 'org-link-store
  1422. :type '(choice boolean integer))
  1423. (defcustom org-keep-stored-link-after-insertion nil
  1424. "Non-nil means keep link in list for entire session.
  1425. The command `org-store-link' adds a link pointing to the current
  1426. location to an internal list. These links accumulate during a session.
  1427. The command `org-insert-link' can be used to insert links into any
  1428. Org-mode file (offering completion for all stored links). When this
  1429. option is nil, every link which has been inserted once using \\[org-insert-link]
  1430. will be removed from the list, to make completing the unused links
  1431. more efficient."
  1432. :group 'org-link-store
  1433. :type 'boolean)
  1434. (defgroup org-link-follow nil
  1435. "Options concerning following links in Org-mode."
  1436. :tag "Org Follow Link"
  1437. :group 'org-link)
  1438. (defcustom org-link-translation-function nil
  1439. "Function to translate links with different syntax to Org syntax.
  1440. This can be used to translate links created for example by the Planner
  1441. or emacs-wiki packages to Org syntax.
  1442. The function must accept two parameters, a TYPE containing the link
  1443. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1444. which is everything after the link protocol. It should return a cons
  1445. with possibly modified values of type and path.
  1446. Org contains a function for this, so if you set this variable to
  1447. `org-translate-link-from-planner', you should be able follow many
  1448. links created by planner."
  1449. :group 'org-link-follow
  1450. :type 'function)
  1451. (defcustom org-follow-link-hook nil
  1452. "Hook that is run after a link has been followed."
  1453. :group 'org-link-follow
  1454. :type 'hook)
  1455. (defcustom org-tab-follows-link nil
  1456. "Non-nil means on links TAB will follow the link.
  1457. Needs to be set before org.el is loaded.
  1458. This really should not be used, it does not make sense, and the
  1459. implementation is bad."
  1460. :group 'org-link-follow
  1461. :type 'boolean)
  1462. (defcustom org-return-follows-link nil
  1463. "Non-nil means on links RET will follow the link.
  1464. In tables, the special behavior of RET has precedence."
  1465. :group 'org-link-follow
  1466. :type 'boolean)
  1467. (defcustom org-mouse-1-follows-link
  1468. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1469. "Non-nil means mouse-1 on a link will follow the link.
  1470. A longer mouse click will still set point. Does not work on XEmacs.
  1471. Needs to be set before org.el is loaded."
  1472. :group 'org-link-follow
  1473. :type 'boolean)
  1474. (defcustom org-mark-ring-length 4
  1475. "Number of different positions to be recorded in the ring.
  1476. Changing this requires a restart of Emacs to work correctly."
  1477. :group 'org-link-follow
  1478. :type 'integer)
  1479. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1480. "Non-nil means internal links in Org files must exactly match a headline.
  1481. When nil, the link search tries to match a phrase with all words
  1482. in the search text."
  1483. :group 'org-link-follow
  1484. :version "24.1"
  1485. :type '(choice
  1486. (const :tag "Use fuzzy text search" nil)
  1487. (const :tag "Match only exact headline" t)
  1488. (const :tag "Match exact headline or query to create it"
  1489. query-to-create)))
  1490. (defcustom org-link-frame-setup
  1491. '((vm . vm-visit-folder-other-frame)
  1492. (vm-imap . vm-visit-imap-folder-other-frame)
  1493. (gnus . org-gnus-no-new-news)
  1494. (file . find-file-other-window)
  1495. (wl . wl-other-frame))
  1496. "Setup the frame configuration for following links.
  1497. When following a link with Emacs, it may often be useful to display
  1498. this link in another window or frame. This variable can be used to
  1499. set this up for the different types of links.
  1500. For VM, use any of
  1501. `vm-visit-folder'
  1502. `vm-visit-folder-other-window'
  1503. `vm-visit-folder-other-frame'
  1504. For Gnus, use any of
  1505. `gnus'
  1506. `gnus-other-frame'
  1507. `org-gnus-no-new-news'
  1508. For FILE, use any of
  1509. `find-file'
  1510. `find-file-other-window'
  1511. `find-file-other-frame'
  1512. For Wanderlust use any of
  1513. `wl'
  1514. `wl-other-frame'
  1515. For the calendar, use the variable `calendar-setup'.
  1516. For BBDB, it is currently only possible to display the matches in
  1517. another window."
  1518. :group 'org-link-follow
  1519. :type '(list
  1520. (cons (const vm)
  1521. (choice
  1522. (const vm-visit-folder)
  1523. (const vm-visit-folder-other-window)
  1524. (const vm-visit-folder-other-frame)))
  1525. (cons (const gnus)
  1526. (choice
  1527. (const gnus)
  1528. (const gnus-other-frame)
  1529. (const org-gnus-no-new-news)))
  1530. (cons (const file)
  1531. (choice
  1532. (const find-file)
  1533. (const find-file-other-window)
  1534. (const find-file-other-frame)))
  1535. (cons (const wl)
  1536. (choice
  1537. (const wl)
  1538. (const wl-other-frame)))))
  1539. (defcustom org-display-internal-link-with-indirect-buffer nil
  1540. "Non-nil means use indirect buffer to display infile links.
  1541. Activating internal links (from one location in a file to another location
  1542. in the same file) normally just jumps to the location. When the link is
  1543. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1544. is displayed in
  1545. another window. When this option is set, the other window actually displays
  1546. an indirect buffer clone of the current buffer, to avoid any visibility
  1547. changes to the current buffer."
  1548. :group 'org-link-follow
  1549. :type 'boolean)
  1550. (defcustom org-open-non-existing-files nil
  1551. "Non-nil means `org-open-file' will open non-existing files.
  1552. When nil, an error will be generated.
  1553. This variable applies only to external applications because they
  1554. might choke on non-existing files. If the link is to a file that
  1555. will be opened in Emacs, the variable is ignored."
  1556. :group 'org-link-follow
  1557. :type 'boolean)
  1558. (defcustom org-open-directory-means-index-dot-org nil
  1559. "Non-nil means a link to a directory really means to index.org.
  1560. When nil, following a directory link will run dired or open a finder/explorer
  1561. window on that directory."
  1562. :group 'org-link-follow
  1563. :type 'boolean)
  1564. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1565. "Function and arguments to call for following mailto links.
  1566. This is a list with the first element being a Lisp function, and the
  1567. remaining elements being arguments to the function. In string arguments,
  1568. %a will be replaced by the address, and %s will be replaced by the subject
  1569. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1570. :group 'org-link-follow
  1571. :type '(choice
  1572. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1573. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1574. (const :tag "message-mail" (message-mail "%a" "%s"))
  1575. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1576. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1577. "Non-nil means ask for confirmation before executing shell links.
  1578. Shell links can be dangerous: just think about a link
  1579. [[shell:rm -rf ~/*][Google Search]]
  1580. This link would show up in your Org-mode document as \"Google Search\",
  1581. but really it would remove your entire home directory.
  1582. Therefore we advise against setting this variable to nil.
  1583. Just change it to `y-or-n-p' if you want to confirm with a
  1584. single keystroke rather than having to type \"yes\"."
  1585. :group 'org-link-follow
  1586. :type '(choice
  1587. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1588. (const :tag "with y-or-n (faster)" y-or-n-p)
  1589. (const :tag "no confirmation (dangerous)" nil)))
  1590. (put 'org-confirm-shell-link-function
  1591. 'safe-local-variable
  1592. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1593. (defcustom org-confirm-shell-link-not-regexp ""
  1594. "A regexp to skip confirmation for shell links."
  1595. :group 'org-link-follow
  1596. :version "24.1"
  1597. :type 'regexp)
  1598. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1599. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1600. Elisp links can be dangerous: just think about a link
  1601. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1602. This link would show up in your Org-mode document as \"Google Search\",
  1603. but really it would remove your entire home directory.
  1604. Therefore we advise against setting this variable to nil.
  1605. Just change it to `y-or-n-p' if you want to confirm with a
  1606. single keystroke rather than having to type \"yes\"."
  1607. :group 'org-link-follow
  1608. :type '(choice
  1609. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1610. (const :tag "with y-or-n (faster)" y-or-n-p)
  1611. (const :tag "no confirmation (dangerous)" nil)))
  1612. (put 'org-confirm-shell-link-function
  1613. 'safe-local-variable
  1614. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1615. (defcustom org-confirm-elisp-link-not-regexp ""
  1616. "A regexp to skip confirmation for Elisp links."
  1617. :group 'org-link-follow
  1618. :version "24.1"
  1619. :type 'regexp)
  1620. (defconst org-file-apps-defaults-gnu
  1621. '((remote . emacs)
  1622. (system . mailcap)
  1623. (t . mailcap))
  1624. "Default file applications on a UNIX or GNU/Linux system.
  1625. See `org-file-apps'.")
  1626. (defconst org-file-apps-defaults-macosx
  1627. '((remote . emacs)
  1628. (t . "open %s")
  1629. (system . "open %s")
  1630. ("ps.gz" . "gv %s")
  1631. ("eps.gz" . "gv %s")
  1632. ("dvi" . "xdvi %s")
  1633. ("fig" . "xfig %s"))
  1634. "Default file applications on a MacOS X system.
  1635. The system \"open\" is known as a default, but we use X11 applications
  1636. for some files for which the OS does not have a good default.
  1637. See `org-file-apps'.")
  1638. (defconst org-file-apps-defaults-windowsnt
  1639. (list
  1640. '(remote . emacs)
  1641. (cons t
  1642. (list (if (featurep 'xemacs)
  1643. 'mswindows-shell-execute
  1644. 'w32-shell-execute)
  1645. "open" 'file))
  1646. (cons 'system
  1647. (list (if (featurep 'xemacs)
  1648. 'mswindows-shell-execute
  1649. 'w32-shell-execute)
  1650. "open" 'file)))
  1651. "Default file applications on a Windows NT system.
  1652. The system \"open\" is used for most files.
  1653. See `org-file-apps'.")
  1654. (defcustom org-file-apps
  1655. '(
  1656. (auto-mode . emacs)
  1657. ("\\.mm\\'" . default)
  1658. ("\\.x?html?\\'" . default)
  1659. ("\\.pdf\\'" . default)
  1660. )
  1661. "External applications for opening `file:path' items in a document.
  1662. Org-mode uses system defaults for different file types, but
  1663. you can use this variable to set the application for a given file
  1664. extension. The entries in this list are cons cells where the car identifies
  1665. files and the cdr the corresponding command. Possible values for the
  1666. file identifier are
  1667. \"string\" A string as a file identifier can be interpreted in different
  1668. ways, depending on its contents:
  1669. - Alphanumeric characters only:
  1670. Match links with this file extension.
  1671. Example: (\"pdf\" . \"evince %s\")
  1672. to open PDFs with evince.
  1673. - Regular expression: Match links where the
  1674. filename matches the regexp. If you want to
  1675. use groups here, use shy groups.
  1676. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1677. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1678. to open *.html and *.xhtml with firefox.
  1679. - Regular expression which contains (non-shy) groups:
  1680. Match links where the whole link, including \"::\", and
  1681. anything after that, matches the regexp.
  1682. In a custom command string, %1, %2, etc. are replaced with
  1683. the parts of the link that were matched by the groups.
  1684. For backwards compatibility, if a command string is given
  1685. that does not use any of the group matches, this case is
  1686. handled identically to the second one (i.e. match against
  1687. file name only).
  1688. In a custom lisp form, you can access the group matches with
  1689. (match-string n link).
  1690. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1691. to open [[file:document.pdf::5]] with evince at page 5.
  1692. `directory' Matches a directory
  1693. `remote' Matches a remote file, accessible through tramp or efs.
  1694. Remote files most likely should be visited through Emacs
  1695. because external applications cannot handle such paths.
  1696. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1697. so all files Emacs knows how to handle. Using this with
  1698. command `emacs' will open most files in Emacs. Beware that this
  1699. will also open html files inside Emacs, unless you add
  1700. (\"html\" . default) to the list as well.
  1701. t Default for files not matched by any of the other options.
  1702. `system' The system command to open files, like `open' on Windows
  1703. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1704. that will be selected if you call `C-c C-o' with a double
  1705. \\[universal-argument] \\[universal-argument] prefix.
  1706. Possible values for the command are:
  1707. `emacs' The file will be visited by the current Emacs process.
  1708. `default' Use the default application for this file type, which is the
  1709. association for t in the list, most likely in the system-specific
  1710. part.
  1711. This can be used to overrule an unwanted setting in the
  1712. system-specific variable.
  1713. `system' Use the system command for opening files, like \"open\".
  1714. This command is specified by the entry whose car is `system'.
  1715. Most likely, the system-specific version of this variable
  1716. does define this command, but you can overrule/replace it
  1717. here.
  1718. string A command to be executed by a shell; %s will be replaced
  1719. by the path to the file.
  1720. sexp A Lisp form which will be evaluated. The file path will
  1721. be available in the Lisp variable `file'.
  1722. For more examples, see the system specific constants
  1723. `org-file-apps-defaults-macosx'
  1724. `org-file-apps-defaults-windowsnt'
  1725. `org-file-apps-defaults-gnu'."
  1726. :group 'org-link-follow
  1727. :type '(repeat
  1728. (cons (choice :value ""
  1729. (string :tag "Extension")
  1730. (const :tag "System command to open files" system)
  1731. (const :tag "Default for unrecognized files" t)
  1732. (const :tag "Remote file" remote)
  1733. (const :tag "Links to a directory" directory)
  1734. (const :tag "Any files that have Emacs modes"
  1735. auto-mode))
  1736. (choice :value ""
  1737. (const :tag "Visit with Emacs" emacs)
  1738. (const :tag "Use default" default)
  1739. (const :tag "Use the system command" system)
  1740. (string :tag "Command")
  1741. (sexp :tag "Lisp form")))))
  1742. (defcustom org-doi-server-url "http://dx.doi.org/"
  1743. "The URL of the DOI server."
  1744. :type 'string
  1745. :version "24.3"
  1746. :group 'org-link-follow)
  1747. (defgroup org-refile nil
  1748. "Options concerning refiling entries in Org-mode."
  1749. :tag "Org Refile"
  1750. :group 'org)
  1751. (defcustom org-directory "~/org"
  1752. "Directory with org files.
  1753. This is just a default location to look for Org files. There is no need
  1754. at all to put your files into this directory. It is only used in the
  1755. following situations:
  1756. 1. When a capture template specifies a target file that is not an
  1757. absolute path. The path will then be interpreted relative to
  1758. `org-directory'
  1759. 2. When a capture note is filed away in an interactive way (when exiting the
  1760. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1761. with `org-directory' as the default path."
  1762. :group 'org-refile
  1763. :group 'org-remember
  1764. :group 'org-capture
  1765. :type 'directory)
  1766. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1767. "Default target for storing notes.
  1768. Used as a fall back file for org-remember.el and org-capture.el, for
  1769. templates that do not specify a target file."
  1770. :group 'org-refile
  1771. :group 'org-remember
  1772. :group 'org-capture
  1773. :type '(choice
  1774. (const :tag "Default from remember-data-file" nil)
  1775. file))
  1776. (defcustom org-goto-interface 'outline
  1777. "The default interface to be used for `org-goto'.
  1778. Allowed values are:
  1779. outline The interface shows an outline of the relevant file
  1780. and the correct heading is found by moving through
  1781. the outline or by searching with incremental search.
  1782. outline-path-completion Headlines in the current buffer are offered via
  1783. completion. This is the interface also used by
  1784. the refile command."
  1785. :group 'org-refile
  1786. :type '(choice
  1787. (const :tag "Outline" outline)
  1788. (const :tag "Outline-path-completion" outline-path-completion)))
  1789. (defcustom org-goto-max-level 5
  1790. "Maximum target level when running `org-goto' with refile interface."
  1791. :group 'org-refile
  1792. :type 'integer)
  1793. (defcustom org-reverse-note-order nil
  1794. "Non-nil means store new notes at the beginning of a file or entry.
  1795. When nil, new notes will be filed to the end of a file or entry.
  1796. This can also be a list with cons cells of regular expressions that
  1797. are matched against file names, and values."
  1798. :group 'org-remember
  1799. :group 'org-capture
  1800. :group 'org-refile
  1801. :type '(choice
  1802. (const :tag "Reverse always" t)
  1803. (const :tag "Reverse never" nil)
  1804. (repeat :tag "By file name regexp"
  1805. (cons regexp boolean))))
  1806. (defcustom org-log-refile nil
  1807. "Information to record when a task is refiled.
  1808. Possible values are:
  1809. nil Don't add anything
  1810. time Add a time stamp to the task
  1811. note Prompt for a note and add it with template `org-log-note-headings'
  1812. This option can also be set with on a per-file-basis with
  1813. #+STARTUP: nologrefile
  1814. #+STARTUP: logrefile
  1815. #+STARTUP: lognoterefile
  1816. You can have local logging settings for a subtree by setting the LOGGING
  1817. property to one or more of these keywords.
  1818. When bulk-refiling from the agenda, the value `note' is forbidden and
  1819. will temporarily be changed to `time'."
  1820. :group 'org-refile
  1821. :group 'org-progress
  1822. :version "24.1"
  1823. :type '(choice
  1824. (const :tag "No logging" nil)
  1825. (const :tag "Record timestamp" time)
  1826. (const :tag "Record timestamp with note." note)))
  1827. (defcustom org-refile-targets nil
  1828. "Targets for refiling entries with \\[org-refile].
  1829. This is a list of cons cells. Each cell contains:
  1830. - a specification of the files to be considered, either a list of files,
  1831. or a symbol whose function or variable value will be used to retrieve
  1832. a file name or a list of file names. If you use `org-agenda-files' for
  1833. that, all agenda files will be scanned for targets. Nil means consider
  1834. headings in the current buffer.
  1835. - A specification of how to find candidate refile targets. This may be
  1836. any of:
  1837. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1838. This tag has to be present in all target headlines, inheritance will
  1839. not be considered.
  1840. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1841. todo keyword.
  1842. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1843. headlines that are refiling targets.
  1844. - a cons cell (:level . N). Any headline of level N is considered a target.
  1845. Note that, when `org-odd-levels-only' is set, level corresponds to
  1846. order in hierarchy, not to the number of stars.
  1847. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1848. Note that, when `org-odd-levels-only' is set, level corresponds to
  1849. order in hierarchy, not to the number of stars.
  1850. Each element of this list generates a set of possible targets.
  1851. The union of these sets is presented (with completion) to
  1852. the user by `org-refile'.
  1853. You can set the variable `org-refile-target-verify-function' to a function
  1854. to verify each headline found by the simple criteria above.
  1855. When this variable is nil, all top-level headlines in the current buffer
  1856. are used, equivalent to the value `((nil . (:level . 1))'."
  1857. :group 'org-refile
  1858. :type '(repeat
  1859. (cons
  1860. (choice :value org-agenda-files
  1861. (const :tag "All agenda files" org-agenda-files)
  1862. (const :tag "Current buffer" nil)
  1863. (function) (variable) (file))
  1864. (choice :tag "Identify target headline by"
  1865. (cons :tag "Specific tag" (const :value :tag) (string))
  1866. (cons :tag "TODO keyword" (const :value :todo) (string))
  1867. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1868. (cons :tag "Level number" (const :value :level) (integer))
  1869. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1870. (defcustom org-refile-target-verify-function nil
  1871. "Function to verify if the headline at point should be a refile target.
  1872. The function will be called without arguments, with point at the
  1873. beginning of the headline. It should return t and leave point
  1874. where it is if the headline is a valid target for refiling.
  1875. If the target should not be selected, the function must return nil.
  1876. In addition to this, it may move point to a place from where the search
  1877. should be continued. For example, the function may decide that the entire
  1878. subtree of the current entry should be excluded and move point to the end
  1879. of the subtree."
  1880. :group 'org-refile
  1881. :type 'function)
  1882. (defcustom org-refile-use-cache nil
  1883. "Non-nil means cache refile targets to speed up the process.
  1884. The cache for a particular file will be updated automatically when
  1885. the buffer has been killed, or when any of the marker used for flagging
  1886. refile targets no longer points at a live buffer.
  1887. If you have added new entries to a buffer that might themselves be targets,
  1888. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1889. find that easier, `C-u C-u C-u C-c C-w'."
  1890. :group 'org-refile
  1891. :version "24.1"
  1892. :type 'boolean)
  1893. (defcustom org-refile-use-outline-path nil
  1894. "Non-nil means provide refile targets as paths.
  1895. So a level 3 headline will be available as level1/level2/level3.
  1896. When the value is `file', also include the file name (without directory)
  1897. into the path. In this case, you can also stop the completion after
  1898. the file name, to get entries inserted as top level in the file.
  1899. When `full-file-path', include the full file path."
  1900. :group 'org-refile
  1901. :type '(choice
  1902. (const :tag "Not" nil)
  1903. (const :tag "Yes" t)
  1904. (const :tag "Start with file name" file)
  1905. (const :tag "Start with full file path" full-file-path)))
  1906. (defcustom org-outline-path-complete-in-steps t
  1907. "Non-nil means complete the outline path in hierarchical steps.
  1908. When Org-mode uses the refile interface to select an outline path
  1909. \(see variable `org-refile-use-outline-path'), the completion of
  1910. the path can be done is a single go, or if can be done in steps down
  1911. the headline hierarchy. Going in steps is probably the best if you
  1912. do not use a special completion package like `ido' or `icicles'.
  1913. However, when using these packages, going in one step can be very
  1914. fast, while still showing the whole path to the entry."
  1915. :group 'org-refile
  1916. :type 'boolean)
  1917. (defcustom org-refile-allow-creating-parent-nodes nil
  1918. "Non-nil means allow to create new nodes as refile targets.
  1919. New nodes are then created by adding \"/new node name\" to the completion
  1920. of an existing node. When the value of this variable is `confirm',
  1921. new node creation must be confirmed by the user (recommended)
  1922. When nil, the completion must match an existing entry.
  1923. Note that, if the new heading is not seen by the criteria
  1924. listed in `org-refile-targets', multiple instances of the same
  1925. heading would be created by trying again to file under the new
  1926. heading."
  1927. :group 'org-refile
  1928. :type '(choice
  1929. (const :tag "Never" nil)
  1930. (const :tag "Always" t)
  1931. (const :tag "Prompt for confirmation" confirm)))
  1932. (defcustom org-refile-active-region-within-subtree nil
  1933. "Non-nil means also refile active region within a subtree.
  1934. By default `org-refile' doesn't allow refiling regions if they
  1935. don't contain a set of subtrees, but it might be convenient to
  1936. do so sometimes: in that case, the first line of the region is
  1937. converted to a headline before refiling."
  1938. :group 'org-refile
  1939. :version "24.1"
  1940. :type 'boolean)
  1941. (defgroup org-todo nil
  1942. "Options concerning TODO items in Org-mode."
  1943. :tag "Org TODO"
  1944. :group 'org)
  1945. (defgroup org-progress nil
  1946. "Options concerning Progress logging in Org-mode."
  1947. :tag "Org Progress"
  1948. :group 'org-time)
  1949. (defvar org-todo-interpretation-widgets
  1950. '((:tag "Sequence (cycling hits every state)" sequence)
  1951. (:tag "Type (cycling directly to DONE)" type))
  1952. "The available interpretation symbols for customizing `org-todo-keywords'.
  1953. Interested libraries should add to this list.")
  1954. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1955. "List of TODO entry keyword sequences and their interpretation.
  1956. \\<org-mode-map>This is a list of sequences.
  1957. Each sequence starts with a symbol, either `sequence' or `type',
  1958. indicating if the keywords should be interpreted as a sequence of
  1959. action steps, or as different types of TODO items. The first
  1960. keywords are states requiring action - these states will select a headline
  1961. for inclusion into the global TODO list Org-mode produces. If one of
  1962. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1963. signify that no further action is necessary. If \"|\" is not found,
  1964. the last keyword is treated as the only DONE state of the sequence.
  1965. The command \\[org-todo] cycles an entry through these states, and one
  1966. additional state where no keyword is present. For details about this
  1967. cycling, see the manual.
  1968. TODO keywords and interpretation can also be set on a per-file basis with
  1969. the special #+SEQ_TODO and #+TYP_TODO lines.
  1970. Each keyword can optionally specify a character for fast state selection
  1971. \(in combination with the variable `org-use-fast-todo-selection')
  1972. and specifiers for state change logging, using the same syntax that
  1973. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1974. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1975. indicates to record a time stamp each time this state is selected.
  1976. Each keyword may also specify if a timestamp or a note should be
  1977. recorded when entering or leaving the state, by adding additional
  1978. characters in the parenthesis after the keyword. This looks like this:
  1979. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1980. record only the time of the state change. With X and Y being either
  1981. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1982. Y when leaving the state if and only if the *target* state does not
  1983. define X. You may omit any of the fast-selection key or X or /Y,
  1984. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1985. For backward compatibility, this variable may also be just a list
  1986. of keywords. In this case the interpretation (sequence or type) will be
  1987. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1988. :group 'org-todo
  1989. :group 'org-keywords
  1990. :type '(choice
  1991. (repeat :tag "Old syntax, just keywords"
  1992. (string :tag "Keyword"))
  1993. (repeat :tag "New syntax"
  1994. (cons
  1995. (choice
  1996. :tag "Interpretation"
  1997. ;;Quick and dirty way to see
  1998. ;;`org-todo-interpretations'. This takes the
  1999. ;;place of item arguments
  2000. :convert-widget
  2001. (lambda (widget)
  2002. (widget-put widget
  2003. :args (mapcar
  2004. #'(lambda (x)
  2005. (widget-convert
  2006. (cons 'const x)))
  2007. org-todo-interpretation-widgets))
  2008. widget))
  2009. (repeat
  2010. (string :tag "Keyword"))))))
  2011. (defvar org-todo-keywords-1 nil
  2012. "All TODO and DONE keywords active in a buffer.")
  2013. (make-variable-buffer-local 'org-todo-keywords-1)
  2014. (defvar org-todo-keywords-for-agenda nil)
  2015. (defvar org-done-keywords-for-agenda nil)
  2016. (defvar org-drawers-for-agenda nil)
  2017. (defvar org-todo-keyword-alist-for-agenda nil)
  2018. (defvar org-tag-alist-for-agenda nil)
  2019. (defvar org-agenda-contributing-files nil)
  2020. (defvar org-not-done-keywords nil)
  2021. (make-variable-buffer-local 'org-not-done-keywords)
  2022. (defvar org-done-keywords nil)
  2023. (make-variable-buffer-local 'org-done-keywords)
  2024. (defvar org-todo-heads nil)
  2025. (make-variable-buffer-local 'org-todo-heads)
  2026. (defvar org-todo-sets nil)
  2027. (make-variable-buffer-local 'org-todo-sets)
  2028. (defvar org-todo-log-states nil)
  2029. (make-variable-buffer-local 'org-todo-log-states)
  2030. (defvar org-todo-kwd-alist nil)
  2031. (make-variable-buffer-local 'org-todo-kwd-alist)
  2032. (defvar org-todo-key-alist nil)
  2033. (make-variable-buffer-local 'org-todo-key-alist)
  2034. (defvar org-todo-key-trigger nil)
  2035. (make-variable-buffer-local 'org-todo-key-trigger)
  2036. (defcustom org-todo-interpretation 'sequence
  2037. "Controls how TODO keywords are interpreted.
  2038. This variable is in principle obsolete and is only used for
  2039. backward compatibility, if the interpretation of todo keywords is
  2040. not given already in `org-todo-keywords'. See that variable for
  2041. more information."
  2042. :group 'org-todo
  2043. :group 'org-keywords
  2044. :type '(choice (const sequence)
  2045. (const type)))
  2046. (defcustom org-use-fast-todo-selection t
  2047. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2048. This variable describes if and under what circumstances the cycling
  2049. mechanism for TODO keywords will be replaced by a single-key, direct
  2050. selection scheme.
  2051. When nil, fast selection is never used.
  2052. When the symbol `prefix', it will be used when `org-todo' is called
  2053. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2054. `C-u t' in an agenda buffer.
  2055. When t, fast selection is used by default. In this case, the prefix
  2056. argument forces cycling instead.
  2057. In all cases, the special interface is only used if access keys have
  2058. actually been assigned by the user, i.e. if keywords in the configuration
  2059. are followed by a letter in parenthesis, like TODO(t)."
  2060. :group 'org-todo
  2061. :type '(choice
  2062. (const :tag "Never" nil)
  2063. (const :tag "By default" t)
  2064. (const :tag "Only with C-u C-c C-t" prefix)))
  2065. (defcustom org-provide-todo-statistics t
  2066. "Non-nil means update todo statistics after insert and toggle.
  2067. ALL-HEADLINES means update todo statistics by including headlines
  2068. with no TODO keyword as well, counting them as not done.
  2069. A list of TODO keywords means the same, but skip keywords that are
  2070. not in this list.
  2071. When this is set, todo statistics is updated in the parent of the
  2072. current entry each time a todo state is changed."
  2073. :group 'org-todo
  2074. :type '(choice
  2075. (const :tag "Yes, only for TODO entries" t)
  2076. (const :tag "Yes, including all entries" 'all-headlines)
  2077. (repeat :tag "Yes, for TODOs in this list"
  2078. (string :tag "TODO keyword"))
  2079. (other :tag "No TODO statistics" nil)))
  2080. (defcustom org-hierarchical-todo-statistics t
  2081. "Non-nil means TODO statistics covers just direct children.
  2082. When nil, all entries in the subtree are considered.
  2083. This has only an effect if `org-provide-todo-statistics' is set.
  2084. To set this to nil for only a single subtree, use a COOKIE_DATA
  2085. property and include the word \"recursive\" into the value."
  2086. :group 'org-todo
  2087. :type 'boolean)
  2088. (defcustom org-after-todo-state-change-hook nil
  2089. "Hook which is run after the state of a TODO item was changed.
  2090. The new state (a string with a TODO keyword, or nil) is available in the
  2091. Lisp variable `org-state'."
  2092. :group 'org-todo
  2093. :type 'hook)
  2094. (defvar org-blocker-hook nil
  2095. "Hook for functions that are allowed to block a state change.
  2096. Functions in this hook should not modify the buffer.
  2097. Each function gets as its single argument a property list,
  2098. see `org-trigger-hook' for more information about this list.
  2099. If any of the functions in this hook returns nil, the state change
  2100. is blocked.")
  2101. (defvar org-trigger-hook nil
  2102. "Hook for functions that are triggered by a state change.
  2103. Each function gets as its single argument a property list with at
  2104. least the following elements:
  2105. (:type type-of-change :position pos-at-entry-start
  2106. :from old-state :to new-state)
  2107. Depending on the type, more properties may be present.
  2108. This mechanism is currently implemented for:
  2109. TODO state changes
  2110. ------------------
  2111. :type todo-state-change
  2112. :from previous state (keyword as a string), or nil, or a symbol
  2113. 'todo' or 'done', to indicate the general type of state.
  2114. :to new state, like in :from")
  2115. (defcustom org-enforce-todo-dependencies nil
  2116. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2117. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2118. be blocked if any prior sibling is not yet done.
  2119. Finally, if the parent is blocked because of ordered siblings of its own,
  2120. the child will also be blocked."
  2121. :set (lambda (var val)
  2122. (set var val)
  2123. (if val
  2124. (add-hook 'org-blocker-hook
  2125. 'org-block-todo-from-children-or-siblings-or-parent)
  2126. (remove-hook 'org-blocker-hook
  2127. 'org-block-todo-from-children-or-siblings-or-parent)))
  2128. :group 'org-todo
  2129. :type 'boolean)
  2130. (defcustom org-enforce-todo-checkbox-dependencies nil
  2131. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2132. When this is nil, checkboxes have no influence on switching TODO states.
  2133. When non-nil, you first need to check off all check boxes before the TODO
  2134. entry can be switched to DONE.
  2135. This variable needs to be set before org.el is loaded, and you need to
  2136. restart Emacs after a change to make the change effective. The only way
  2137. to change is while Emacs is running is through the customize interface."
  2138. :set (lambda (var val)
  2139. (set var val)
  2140. (if val
  2141. (add-hook 'org-blocker-hook
  2142. 'org-block-todo-from-checkboxes)
  2143. (remove-hook 'org-blocker-hook
  2144. 'org-block-todo-from-checkboxes)))
  2145. :group 'org-todo
  2146. :type 'boolean)
  2147. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2148. "Non-nil means inserting a TODO heading is treated as state change.
  2149. So when the command \\[org-insert-todo-heading] is used, state change
  2150. logging will apply if appropriate. When nil, the new TODO item will
  2151. be inserted directly, and no logging will take place."
  2152. :group 'org-todo
  2153. :type 'boolean)
  2154. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2155. "Non-nil means switching TODO states with S-cursor counts as state change.
  2156. This is the default behavior. However, setting this to nil allows a
  2157. convenient way to select a TODO state and bypass any logging associated
  2158. with that."
  2159. :group 'org-todo
  2160. :type 'boolean)
  2161. (defcustom org-todo-state-tags-triggers nil
  2162. "Tag changes that should be triggered by TODO state changes.
  2163. This is a list. Each entry is
  2164. (state-change (tag . flag) .......)
  2165. State-change can be a string with a state, and empty string to indicate the
  2166. state that has no TODO keyword, or it can be one of the symbols `todo'
  2167. or `done', meaning any not-done or done state, respectively."
  2168. :group 'org-todo
  2169. :group 'org-tags
  2170. :type '(repeat
  2171. (cons (choice :tag "When changing to"
  2172. (const :tag "Not-done state" todo)
  2173. (const :tag "Done state" done)
  2174. (string :tag "State"))
  2175. (repeat
  2176. (cons :tag "Tag action"
  2177. (string :tag "Tag")
  2178. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2179. (defcustom org-log-done nil
  2180. "Information to record when a task moves to the DONE state.
  2181. Possible values are:
  2182. nil Don't add anything, just change the keyword
  2183. time Add a time stamp to the task
  2184. note Prompt for a note and add it with template `org-log-note-headings'
  2185. This option can also be set with on a per-file-basis with
  2186. #+STARTUP: nologdone
  2187. #+STARTUP: logdone
  2188. #+STARTUP: lognotedone
  2189. You can have local logging settings for a subtree by setting the LOGGING
  2190. property to one or more of these keywords."
  2191. :group 'org-todo
  2192. :group 'org-progress
  2193. :type '(choice
  2194. (const :tag "No logging" nil)
  2195. (const :tag "Record CLOSED timestamp" time)
  2196. (const :tag "Record CLOSED timestamp with note." note)))
  2197. ;; Normalize old uses of org-log-done.
  2198. (cond
  2199. ((eq org-log-done t) (setq org-log-done 'time))
  2200. ((and (listp org-log-done) (memq 'done org-log-done))
  2201. (setq org-log-done 'note)))
  2202. (defcustom org-log-reschedule nil
  2203. "Information to record when the scheduling date of a tasks is modified.
  2204. Possible values are:
  2205. nil Don't add anything, just change the date
  2206. time Add a time stamp to the task
  2207. note Prompt for a note and add it with template `org-log-note-headings'
  2208. This option can also be set with on a per-file-basis with
  2209. #+STARTUP: nologreschedule
  2210. #+STARTUP: logreschedule
  2211. #+STARTUP: lognotereschedule"
  2212. :group 'org-todo
  2213. :group 'org-progress
  2214. :type '(choice
  2215. (const :tag "No logging" nil)
  2216. (const :tag "Record timestamp" time)
  2217. (const :tag "Record timestamp with note." note)))
  2218. (defcustom org-log-redeadline nil
  2219. "Information to record when the deadline date of a tasks is modified.
  2220. Possible values are:
  2221. nil Don't add anything, just change the date
  2222. time Add a time stamp to the task
  2223. note Prompt for a note and add it with template `org-log-note-headings'
  2224. This option can also be set with on a per-file-basis with
  2225. #+STARTUP: nologredeadline
  2226. #+STARTUP: logredeadline
  2227. #+STARTUP: lognoteredeadline
  2228. You can have local logging settings for a subtree by setting the LOGGING
  2229. property to one or more of these keywords."
  2230. :group 'org-todo
  2231. :group 'org-progress
  2232. :type '(choice
  2233. (const :tag "No logging" nil)
  2234. (const :tag "Record timestamp" time)
  2235. (const :tag "Record timestamp with note." note)))
  2236. (defcustom org-log-note-clock-out nil
  2237. "Non-nil means record a note when clocking out of an item.
  2238. This can also be configured on a per-file basis by adding one of
  2239. the following lines anywhere in the buffer:
  2240. #+STARTUP: lognoteclock-out
  2241. #+STARTUP: nolognoteclock-out"
  2242. :group 'org-todo
  2243. :group 'org-progress
  2244. :type 'boolean)
  2245. (defcustom org-log-done-with-time t
  2246. "Non-nil means the CLOSED time stamp will contain date and time.
  2247. When nil, only the date will be recorded."
  2248. :group 'org-progress
  2249. :type 'boolean)
  2250. (defcustom org-log-note-headings
  2251. '((done . "CLOSING NOTE %t")
  2252. (state . "State %-12s from %-12S %t")
  2253. (note . "Note taken on %t")
  2254. (reschedule . "Rescheduled from %S on %t")
  2255. (delschedule . "Not scheduled, was %S on %t")
  2256. (redeadline . "New deadline from %S on %t")
  2257. (deldeadline . "Removed deadline, was %S on %t")
  2258. (refile . "Refiled on %t")
  2259. (clock-out . ""))
  2260. "Headings for notes added to entries.
  2261. The value is an alist, with the car being a symbol indicating the note
  2262. context, and the cdr is the heading to be used. The heading may also be the
  2263. empty string.
  2264. %t in the heading will be replaced by a time stamp.
  2265. %T will be an active time stamp instead the default inactive one
  2266. %d will be replaced by a short-format time stamp.
  2267. %D will be replaced by an active short-format time stamp.
  2268. %s will be replaced by the new TODO state, in double quotes.
  2269. %S will be replaced by the old TODO state, in double quotes.
  2270. %u will be replaced by the user name.
  2271. %U will be replaced by the full user name.
  2272. In fact, it is not a good idea to change the `state' entry, because
  2273. agenda log mode depends on the format of these entries."
  2274. :group 'org-todo
  2275. :group 'org-progress
  2276. :type '(list :greedy t
  2277. (cons (const :tag "Heading when closing an item" done) string)
  2278. (cons (const :tag
  2279. "Heading when changing todo state (todo sequence only)"
  2280. state) string)
  2281. (cons (const :tag "Heading when just taking a note" note) string)
  2282. (cons (const :tag "Heading when clocking out" clock-out) string)
  2283. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2284. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2285. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2286. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2287. (cons (const :tag "Heading when refiling" refile) string)))
  2288. (unless (assq 'note org-log-note-headings)
  2289. (push '(note . "%t") org-log-note-headings))
  2290. (defcustom org-log-into-drawer nil
  2291. "Non-nil means insert state change notes and time stamps into a drawer.
  2292. When nil, state changes notes will be inserted after the headline and
  2293. any scheduling and clock lines, but not inside a drawer.
  2294. The value of this variable should be the name of the drawer to use.
  2295. LOGBOOK is proposed as the default drawer for this purpose, you can
  2296. also set this to a string to define the drawer of your choice.
  2297. A value of t is also allowed, representing \"LOGBOOK\".
  2298. A value of t or nil can also be set with on a per-file-basis with
  2299. #+STARTUP: logdrawer
  2300. #+STARTUP: nologdrawer
  2301. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2302. will be ignored.
  2303. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2304. a subtree."
  2305. :group 'org-todo
  2306. :group 'org-progress
  2307. :type '(choice
  2308. (const :tag "Not into a drawer" nil)
  2309. (const :tag "LOGBOOK" t)
  2310. (string :tag "Other")))
  2311. (if (fboundp 'defvaralias)
  2312. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2313. (defun org-log-into-drawer ()
  2314. "Return the value of `org-log-into-drawer', but let properties overrule.
  2315. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2316. used instead of the default value."
  2317. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2318. (cond
  2319. ((not p) org-log-into-drawer)
  2320. ((equal p "nil") nil)
  2321. ((equal p "t") "LOGBOOK")
  2322. (t p))))
  2323. (defcustom org-log-state-notes-insert-after-drawers nil
  2324. "Non-nil means insert state change notes after any drawers in entry.
  2325. Only the drawers that *immediately* follow the headline and the
  2326. deadline/scheduled line are skipped.
  2327. When nil, insert notes right after the heading and perhaps the line
  2328. with deadline/scheduling if present.
  2329. This variable will have no effect if `org-log-into-drawer' is
  2330. set."
  2331. :group 'org-todo
  2332. :group 'org-progress
  2333. :type 'boolean)
  2334. (defcustom org-log-states-order-reversed t
  2335. "Non-nil means the latest state note will be directly after heading.
  2336. When nil, the state change notes will be ordered according to time.
  2337. This option can also be set with on a per-file-basis with
  2338. #+STARTUP: logstatesreversed
  2339. #+STARTUP: nologstatesreversed"
  2340. :group 'org-todo
  2341. :group 'org-progress
  2342. :type 'boolean)
  2343. (defcustom org-todo-repeat-to-state nil
  2344. "The TODO state to which a repeater should return the repeating task.
  2345. By default this is the first task in a TODO sequence, or the previous state
  2346. in a TODO_TYP set. But you can specify another task here.
  2347. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2348. :group 'org-todo
  2349. :version "24.1"
  2350. :type '(choice (const :tag "Head of sequence" nil)
  2351. (string :tag "Specific state")))
  2352. (defcustom org-log-repeat 'time
  2353. "Non-nil means record moving through the DONE state when triggering repeat.
  2354. An auto-repeating task is immediately switched back to TODO when
  2355. marked DONE. If you are not logging state changes (by adding \"@\"
  2356. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2357. record a closing note, there will be no record of the task moving
  2358. through DONE. This variable forces taking a note anyway.
  2359. nil Don't force a record
  2360. time Record a time stamp
  2361. note Prompt for a note and add it with template `org-log-note-headings'
  2362. This option can also be set with on a per-file-basis with
  2363. #+STARTUP: nologrepeat
  2364. #+STARTUP: logrepeat
  2365. #+STARTUP: lognoterepeat
  2366. You can have local logging settings for a subtree by setting the LOGGING
  2367. property to one or more of these keywords."
  2368. :group 'org-todo
  2369. :group 'org-progress
  2370. :type '(choice
  2371. (const :tag "Don't force a record" nil)
  2372. (const :tag "Force recording the DONE state" time)
  2373. (const :tag "Force recording a note with the DONE state" note)))
  2374. (defgroup org-priorities nil
  2375. "Priorities in Org-mode."
  2376. :tag "Org Priorities"
  2377. :group 'org-todo)
  2378. (defcustom org-enable-priority-commands t
  2379. "Non-nil means priority commands are active.
  2380. When nil, these commands will be disabled, so that you never accidentally
  2381. set a priority."
  2382. :group 'org-priorities
  2383. :type 'boolean)
  2384. (defcustom org-highest-priority ?A
  2385. "The highest priority of TODO items. A character like ?A, ?B etc.
  2386. Must have a smaller ASCII number than `org-lowest-priority'."
  2387. :group 'org-priorities
  2388. :type 'character)
  2389. (defcustom org-lowest-priority ?C
  2390. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2391. Must have a larger ASCII number than `org-highest-priority'."
  2392. :group 'org-priorities
  2393. :type 'character)
  2394. (defcustom org-default-priority ?B
  2395. "The default priority of TODO items.
  2396. This is the priority an item gets if no explicit priority is given.
  2397. When starting to cycle on an empty priority the first step in the cycle
  2398. depends on `org-priority-start-cycle-with-default'. The resulting first
  2399. step priority must not exceed the range from `org-highest-priority' to
  2400. `org-lowest-priority' which means that `org-default-priority' has to be
  2401. in this range exclusive or inclusive the range boundaries. Else the
  2402. first step refuses to set the default and the second will fall back
  2403. to (depending on the command used) the highest or lowest priority."
  2404. :group 'org-priorities
  2405. :type 'character)
  2406. (defcustom org-priority-start-cycle-with-default t
  2407. "Non-nil means start with default priority when starting to cycle.
  2408. When this is nil, the first step in the cycle will be (depending on the
  2409. command used) one higher or lower than the default priority.
  2410. See also `org-default-priority'."
  2411. :group 'org-priorities
  2412. :type 'boolean)
  2413. (defcustom org-get-priority-function nil
  2414. "Function to extract the priority from a string.
  2415. The string is normally the headline. If this is nil Org computes the
  2416. priority from the priority cookie like [#A] in the headline. It returns
  2417. an integer, increasing by 1000 for each priority level.
  2418. The user can set a different function here, which should take a string
  2419. as an argument and return the numeric priority."
  2420. :group 'org-priorities
  2421. :version "24.1"
  2422. :type 'function)
  2423. (defgroup org-time nil
  2424. "Options concerning time stamps and deadlines in Org-mode."
  2425. :tag "Org Time"
  2426. :group 'org)
  2427. (defcustom org-insert-labeled-timestamps-at-point nil
  2428. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2429. When nil, these labeled time stamps are forces into the second line of an
  2430. entry, just after the headline. When scheduling from the global TODO list,
  2431. the time stamp will always be forced into the second line."
  2432. :group 'org-time
  2433. :type 'boolean)
  2434. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2435. "Formats for `format-time-string' which are used for time stamps.
  2436. It is not recommended to change this constant.")
  2437. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2438. "Number of minutes to round time stamps to.
  2439. These are two values, the first applies when first creating a time stamp.
  2440. The second applies when changing it with the commands `S-up' and `S-down'.
  2441. When changing the time stamp, this means that it will change in steps
  2442. of N minutes, as given by the second value.
  2443. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2444. numbers should be factors of 60, so for example 5, 10, 15.
  2445. When this is larger than 1, you can still force an exact time stamp by using
  2446. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2447. and by using a prefix arg to `S-up/down' to specify the exact number
  2448. of minutes to shift."
  2449. :group 'org-time
  2450. :get #'(lambda (var) ; Make sure both elements are there
  2451. (if (integerp (default-value var))
  2452. (list (default-value var) 5)
  2453. (default-value var)))
  2454. :type '(list
  2455. (integer :tag "when inserting times")
  2456. (integer :tag "when modifying times")))
  2457. ;; Normalize old customizations of this variable.
  2458. (when (integerp org-time-stamp-rounding-minutes)
  2459. (setq org-time-stamp-rounding-minutes
  2460. (list org-time-stamp-rounding-minutes
  2461. org-time-stamp-rounding-minutes)))
  2462. (defcustom org-display-custom-times nil
  2463. "Non-nil means overlay custom formats over all time stamps.
  2464. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2465. To turn this on on a per-file basis, insert anywhere in the file:
  2466. #+STARTUP: customtime"
  2467. :group 'org-time
  2468. :set 'set-default
  2469. :type 'sexp)
  2470. (make-variable-buffer-local 'org-display-custom-times)
  2471. (defcustom org-time-stamp-custom-formats
  2472. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2473. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2474. These are overlaid over the default ISO format if the variable
  2475. `org-display-custom-times' is set. Time like %H:%M should be at the
  2476. end of the second format. The custom formats are also honored by export
  2477. commands, if custom time display is turned on at the time of export."
  2478. :group 'org-time
  2479. :type 'sexp)
  2480. (defun org-time-stamp-format (&optional long inactive)
  2481. "Get the right format for a time string."
  2482. (let ((f (if long (cdr org-time-stamp-formats)
  2483. (car org-time-stamp-formats))))
  2484. (if inactive
  2485. (concat "[" (substring f 1 -1) "]")
  2486. f)))
  2487. (defcustom org-time-clocksum-format
  2488. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2489. "The format string used when creating CLOCKSUM lines.
  2490. This is also used when Org mode generates a time duration.
  2491. The value can be a single format string containing two
  2492. %-sequences, which will be filled with the number of hours and
  2493. minutes in that order.
  2494. Alternatively, the value can be a plist associating any of the
  2495. keys :years, :months, :weeks, :days, :hours or :minutes with
  2496. format strings. The time duration is formatted using only the
  2497. time components that are needed and concatenating the results.
  2498. If a time unit in absent, it falls back to the next smallest
  2499. unit.
  2500. The keys :require-years, :require-months, :require-days,
  2501. :require-weeks, :require-hours, :require-minutes are also
  2502. meaningful. A non-nil value for these keys indicates that the
  2503. corresponding time component should always be included, even if
  2504. its value is 0.
  2505. For example,
  2506. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2507. :require-minutes t)
  2508. means durations longer than a day will be expressed in days,
  2509. hours and minutes, and durations less than a day will always be
  2510. expressed in hours and minutes (even for durations less than an
  2511. hour).
  2512. The value
  2513. \(:days \"%dd\" :minutes \"%dm\")
  2514. means durations longer than a day will be expressed in days and
  2515. minutes, and durations less than a day will be expressed entirely
  2516. in minutes (even for durations longer than an hour)."
  2517. :group 'org-time
  2518. :group 'org-clock
  2519. :version "24.4"
  2520. :package-version '(Org . "8.0")
  2521. :type '(choice (string :tag "Format string")
  2522. (set :tag "Plist"
  2523. (group :inline t (const :tag "Years" :years)
  2524. (string :tag "Format string"))
  2525. (group :inline t
  2526. (const :tag "Always show years" :require-years)
  2527. (const t))
  2528. (group :inline t (const :tag "Months" :months)
  2529. (string :tag "Format string"))
  2530. (group :inline t
  2531. (const :tag "Always show months" :require-months)
  2532. (const t))
  2533. (group :inline t (const :tag "Weeks" :weeks)
  2534. (string :tag "Format string"))
  2535. (group :inline t
  2536. (const :tag "Always show weeks" :require-weeks)
  2537. (const t))
  2538. (group :inline t (const :tag "Days" :days)
  2539. (string :tag "Format string"))
  2540. (group :inline t
  2541. (const :tag "Always show days" :require-days)
  2542. (const t))
  2543. (group :inline t (const :tag "Hours" :hours)
  2544. (string :tag "Format string"))
  2545. (group :inline t
  2546. (const :tag "Always show hours" :require-hours)
  2547. (const t))
  2548. (group :inline t (const :tag "Minutes" :minutes)
  2549. (string :tag "Format string"))
  2550. (group :inline t
  2551. (const :tag "Always show minutes" :require-minutes)
  2552. (const t)))))
  2553. (defcustom org-time-clocksum-use-fractional nil
  2554. "When non-nil, \\[org-clock-display] uses fractional times.
  2555. See `org-time-clocksum-format' for more on time clock formats."
  2556. :group 'org-time
  2557. :group 'org-clock
  2558. :version "24.3"
  2559. :type 'boolean)
  2560. (defcustom org-time-clocksum-use-effort-durations nil
  2561. "When non-nil, \\[org-clock-display] uses effort durations.
  2562. E.g. by default, one day is considered to be a 8 hours effort,
  2563. so a task that has been clocked for 16 hours will be displayed
  2564. as during 2 days in the clock display or in the clocktable.
  2565. See `org-effort-durations' on how to set effort durations
  2566. and `org-time-clocksum-format' for more on time clock formats."
  2567. :group 'org-time
  2568. :group 'org-clock
  2569. :version "24.4"
  2570. :package-version '(Org . "8.0")
  2571. :type 'boolean)
  2572. (defcustom org-time-clocksum-fractional-format "%.2f"
  2573. "The format string used when creating CLOCKSUM lines,
  2574. or when Org mode generates a time duration, if
  2575. `org-time-clocksum-use-fractional' is enabled.
  2576. The value can be a single format string containing one
  2577. %-sequence, which will be filled with the number of hours as
  2578. a float.
  2579. Alternatively, the value can be a plist associating any of the
  2580. keys :years, :months, :weeks, :days, :hours or :minutes with
  2581. a format string. The time duration is formatted using the
  2582. largest time unit which gives a non-zero integer part. If all
  2583. specified formats have zero integer part, the smallest time unit
  2584. is used."
  2585. :group 'org-time
  2586. :type '(choice (string :tag "Format string")
  2587. (set (group :inline t (const :tag "Years" :years)
  2588. (string :tag "Format string"))
  2589. (group :inline t (const :tag "Months" :months)
  2590. (string :tag "Format string"))
  2591. (group :inline t (const :tag "Weeks" :weeks)
  2592. (string :tag "Format string"))
  2593. (group :inline t (const :tag "Days" :days)
  2594. (string :tag "Format string"))
  2595. (group :inline t (const :tag "Hours" :hours)
  2596. (string :tag "Format string"))
  2597. (group :inline t (const :tag "Minutes" :minutes)
  2598. (string :tag "Format string")))))
  2599. (defcustom org-deadline-warning-days 14
  2600. "Number of days before expiration during which a deadline becomes active.
  2601. This variable governs the display in sparse trees and in the agenda.
  2602. When 0 or negative, it means use this number (the absolute value of it)
  2603. even if a deadline has a different individual lead time specified.
  2604. Custom commands can set this variable in the options section."
  2605. :group 'org-time
  2606. :group 'org-agenda-daily/weekly
  2607. :type 'integer)
  2608. (defcustom org-scheduled-delay-days 0
  2609. "Number of days before a scheduled item becomes active.
  2610. This variable governs the display in sparse trees and in the agenda.
  2611. The default value (i.e. 0) means: don't delay scheduled item.
  2612. When negative, it means use this number (the absolute value of it)
  2613. even if a scheduled item has a different individual delay time
  2614. specified.
  2615. Custom commands can set this variable in the options section."
  2616. :group 'org-time
  2617. :group 'org-agenda-daily/weekly
  2618. :version "24.4"
  2619. :package-version '(Org . "8.0")
  2620. :type 'integer)
  2621. (defcustom org-read-date-prefer-future t
  2622. "Non-nil means assume future for incomplete date input from user.
  2623. This affects the following situations:
  2624. 1. The user gives a month but not a year.
  2625. For example, if it is April and you enter \"feb 2\", this will be read
  2626. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2627. 2. The user gives a day, but no month.
  2628. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2629. read this as the third of *next* month. However, if you enter \"17\",
  2630. it will be considered as *this* month.
  2631. If you set this variable to the symbol `time', then also the following
  2632. will work:
  2633. 3. If the user gives a time.
  2634. If the time is before now, it will be interpreted as tomorrow.
  2635. Currently none of this works for ISO week specifications.
  2636. When this option is nil, the current day, month and year will always be
  2637. used as defaults.
  2638. See also `org-agenda-jump-prefer-future'."
  2639. :group 'org-time
  2640. :type '(choice
  2641. (const :tag "Never" nil)
  2642. (const :tag "Check month and day" t)
  2643. (const :tag "Check month, day, and time" time)))
  2644. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2645. "Should the agenda jump command prefer the future for incomplete dates?
  2646. The default is to do the same as configured in `org-read-date-prefer-future'.
  2647. But you can also set a deviating value here.
  2648. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2649. :group 'org-agenda
  2650. :group 'org-time
  2651. :version "24.1"
  2652. :type '(choice
  2653. (const :tag "Use org-read-date-prefer-future"
  2654. org-read-date-prefer-future)
  2655. (const :tag "Never" nil)
  2656. (const :tag "Always" t)))
  2657. (defcustom org-read-date-force-compatible-dates t
  2658. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2659. Depending on the system Emacs is running on, certain dates cannot
  2660. be represented with the type used internally to represent time.
  2661. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2662. correctly. Some systems allow for earlier dates, some for later,
  2663. some for both. One way to find out it to insert any date into an
  2664. Org buffer, putting the cursor on the year and hitting S-up and
  2665. S-down to test the range.
  2666. When this variable is set to t, the date/time prompt will not let
  2667. you specify dates outside the 1970-2037 range, so it is certain that
  2668. these dates will work in whatever version of Emacs you are
  2669. running, and also that you can move a file from one Emacs implementation
  2670. to another. WHenever Org is forcing the year for you, it will display
  2671. a message and beep.
  2672. When this variable is nil, Org will check if the date is
  2673. representable in the specific Emacs implementation you are using.
  2674. If not, it will force a year, usually the current year, and beep
  2675. to remind you. Currently this setting is not recommended because
  2676. the likelihood that you will open your Org files in an Emacs that
  2677. has limited date range is not negligible.
  2678. A workaround for this problem is to use diary sexp dates for time
  2679. stamps outside of this range."
  2680. :group 'org-time
  2681. :version "24.1"
  2682. :type 'boolean)
  2683. (defcustom org-read-date-display-live t
  2684. "Non-nil means display current interpretation of date prompt live.
  2685. This display will be in an overlay, in the minibuffer."
  2686. :group 'org-time
  2687. :type 'boolean)
  2688. (defcustom org-read-date-popup-calendar t
  2689. "Non-nil means pop up a calendar when prompting for a date.
  2690. In the calendar, the date can be selected with mouse-1. However, the
  2691. minibuffer will also be active, and you can simply enter the date as well.
  2692. When nil, only the minibuffer will be available."
  2693. :group 'org-time
  2694. :type 'boolean)
  2695. (if (fboundp 'defvaralias)
  2696. (defvaralias 'org-popup-calendar-for-date-prompt
  2697. 'org-read-date-popup-calendar))
  2698. (make-obsolete-variable
  2699. 'org-read-date-minibuffer-setup-hook
  2700. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2701. (defcustom org-read-date-minibuffer-setup-hook nil
  2702. "Hook to be used to set up keys for the date/time interface.
  2703. Add key definitions to `minibuffer-local-map', which will be a
  2704. temporary copy.
  2705. WARNING: This option is obsolete, you should use
  2706. `org-read-date-minibuffer-local-map' to set up keys."
  2707. :group 'org-time
  2708. :type 'hook)
  2709. (defcustom org-extend-today-until 0
  2710. "The hour when your day really ends. Must be an integer.
  2711. This has influence for the following applications:
  2712. - When switching the agenda to \"today\". It it is still earlier than
  2713. the time given here, the day recognized as TODAY is actually yesterday.
  2714. - When a date is read from the user and it is still before the time given
  2715. here, the current date and time will be assumed to be yesterday, 23:59.
  2716. Also, timestamps inserted in capture templates follow this rule.
  2717. IMPORTANT: This is a feature whose implementation is and likely will
  2718. remain incomplete. Really, it is only here because past midnight seems to
  2719. be the favorite working time of John Wiegley :-)"
  2720. :group 'org-time
  2721. :type 'integer)
  2722. (defcustom org-use-effective-time nil
  2723. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2724. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2725. \"effective time\" of any timestamps between midnight and 8am will be
  2726. 23:59 of the previous day."
  2727. :group 'org-time
  2728. :version "24.1"
  2729. :type 'boolean)
  2730. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2731. "When non-nil, use the last clock out time for `org-todo'.
  2732. Note that this option has precedence over the combined use of
  2733. `org-use-effective-time' and `org-extend-today-until'."
  2734. :group 'org-time
  2735. :version "24.4"
  2736. :package-version '(Org . "8.0")
  2737. :type 'boolean)
  2738. (defcustom org-edit-timestamp-down-means-later nil
  2739. "Non-nil means S-down will increase the time in a time stamp.
  2740. When nil, S-up will increase."
  2741. :group 'org-time
  2742. :type 'boolean)
  2743. (defcustom org-calendar-follow-timestamp-change t
  2744. "Non-nil means make the calendar window follow timestamp changes.
  2745. When a timestamp is modified and the calendar window is visible, it will be
  2746. moved to the new date."
  2747. :group 'org-time
  2748. :type 'boolean)
  2749. (defgroup org-tags nil
  2750. "Options concerning tags in Org-mode."
  2751. :tag "Org Tags"
  2752. :group 'org)
  2753. (defcustom org-tag-alist nil
  2754. "List of tags allowed in Org-mode files.
  2755. When this list is nil, Org-mode will base TAG input on what is already in the
  2756. buffer.
  2757. The value of this variable is an alist, the car of each entry must be a
  2758. keyword as a string, the cdr may be a character that is used to select
  2759. that tag through the fast-tag-selection interface.
  2760. See the manual for details."
  2761. :group 'org-tags
  2762. :type '(repeat
  2763. (choice
  2764. (cons (string :tag "Tag name")
  2765. (character :tag "Access char"))
  2766. (list :tag "Start radio group"
  2767. (const :startgroup)
  2768. (option (string :tag "Group description")))
  2769. (list :tag "End radio group"
  2770. (const :endgroup)
  2771. (option (string :tag "Group description")))
  2772. (const :tag "New line" (:newline)))))
  2773. (defcustom org-tag-persistent-alist nil
  2774. "List of tags that will always appear in all Org-mode files.
  2775. This is in addition to any in buffer settings or customizations
  2776. of `org-tag-alist'.
  2777. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2778. The value of this variable is an alist, the car of each entry must be a
  2779. keyword as a string, the cdr may be a character that is used to select
  2780. that tag through the fast-tag-selection interface.
  2781. See the manual for details.
  2782. To disable these tags on a per-file basis, insert anywhere in the file:
  2783. #+STARTUP: noptag"
  2784. :group 'org-tags
  2785. :type '(repeat
  2786. (choice
  2787. (cons (string :tag "Tag name")
  2788. (character :tag "Access char"))
  2789. (const :tag "Start radio group" (:startgroup))
  2790. (const :tag "End radio group" (:endgroup))
  2791. (const :tag "New line" (:newline)))))
  2792. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2793. "If non-nil, always offer completion for all tags of all agenda files.
  2794. Instead of customizing this variable directly, you might want to
  2795. set it locally for capture buffers, because there no list of
  2796. tags in that file can be created dynamically (there are none).
  2797. (add-hook 'org-capture-mode-hook
  2798. (lambda ()
  2799. (set (make-local-variable
  2800. 'org-complete-tags-always-offer-all-agenda-tags)
  2801. t)))"
  2802. :group 'org-tags
  2803. :version "24.1"
  2804. :type 'boolean)
  2805. (defvar org-file-tags nil
  2806. "List of tags that can be inherited by all entries in the file.
  2807. The tags will be inherited if the variable `org-use-tag-inheritance'
  2808. says they should be.
  2809. This variable is populated from #+FILETAGS lines.")
  2810. (defcustom org-use-fast-tag-selection 'auto
  2811. "Non-nil means use fast tag selection scheme.
  2812. This is a special interface to select and deselect tags with single keys.
  2813. When nil, fast selection is never used.
  2814. When the symbol `auto', fast selection is used if and only if selection
  2815. characters for tags have been configured, either through the variable
  2816. `org-tag-alist' or through a #+TAGS line in the buffer.
  2817. When t, fast selection is always used and selection keys are assigned
  2818. automatically if necessary."
  2819. :group 'org-tags
  2820. :type '(choice
  2821. (const :tag "Always" t)
  2822. (const :tag "Never" nil)
  2823. (const :tag "When selection characters are configured" 'auto)))
  2824. (defcustom org-fast-tag-selection-single-key nil
  2825. "Non-nil means fast tag selection exits after first change.
  2826. When nil, you have to press RET to exit it.
  2827. During fast tag selection, you can toggle this flag with `C-c'.
  2828. This variable can also have the value `expert'. In this case, the window
  2829. displaying the tags menu is not even shown, until you press C-c again."
  2830. :group 'org-tags
  2831. :type '(choice
  2832. (const :tag "No" nil)
  2833. (const :tag "Yes" t)
  2834. (const :tag "Expert" expert)))
  2835. (defvar org-fast-tag-selection-include-todo nil
  2836. "Non-nil means fast tags selection interface will also offer TODO states.
  2837. This is an undocumented feature, you should not rely on it.")
  2838. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2839. "The column to which tags should be indented in a headline.
  2840. If this number is positive, it specifies the column. If it is negative,
  2841. it means that the tags should be flushright to that column. For example,
  2842. -80 works well for a normal 80 character screen.
  2843. When 0, place tags directly after headline text, with only one space in
  2844. between."
  2845. :group 'org-tags
  2846. :type 'integer)
  2847. (defcustom org-auto-align-tags t
  2848. "Non-nil keeps tags aligned when modifying headlines.
  2849. Some operations (i.e. demoting) change the length of a headline and
  2850. therefore shift the tags around. With this option turned on, after
  2851. each such operation the tags are again aligned to `org-tags-column'."
  2852. :group 'org-tags
  2853. :type 'boolean)
  2854. (defcustom org-use-tag-inheritance t
  2855. "Non-nil means tags in levels apply also for sublevels.
  2856. When nil, only the tags directly given in a specific line apply there.
  2857. This may also be a list of tags that should be inherited, or a regexp that
  2858. matches tags that should be inherited. Additional control is possible
  2859. with the variable `org-tags-exclude-from-inheritance' which gives an
  2860. explicit list of tags to be excluded from inheritance, even if the value of
  2861. `org-use-tag-inheritance' would select it for inheritance.
  2862. If this option is t, a match early-on in a tree can lead to a large
  2863. number of matches in the subtree when constructing the agenda or creating
  2864. a sparse tree. If you only want to see the first match in a tree during
  2865. a search, check out the variable `org-tags-match-list-sublevels'."
  2866. :group 'org-tags
  2867. :type '(choice
  2868. (const :tag "Not" nil)
  2869. (const :tag "Always" t)
  2870. (repeat :tag "Specific tags" (string :tag "Tag"))
  2871. (regexp :tag "Tags matched by regexp")))
  2872. (defcustom org-tags-exclude-from-inheritance nil
  2873. "List of tags that should never be inherited.
  2874. This is a way to exclude a few tags from inheritance. For way to do
  2875. the opposite, to actively allow inheritance for selected tags,
  2876. see the variable `org-use-tag-inheritance'."
  2877. :group 'org-tags
  2878. :type '(repeat (string :tag "Tag")))
  2879. (defun org-tag-inherit-p (tag)
  2880. "Check if TAG is one that should be inherited."
  2881. (cond
  2882. ((member tag org-tags-exclude-from-inheritance) nil)
  2883. ((eq org-use-tag-inheritance t) t)
  2884. ((not org-use-tag-inheritance) nil)
  2885. ((stringp org-use-tag-inheritance)
  2886. (string-match org-use-tag-inheritance tag))
  2887. ((listp org-use-tag-inheritance)
  2888. (member tag org-use-tag-inheritance))
  2889. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2890. (defcustom org-tags-match-list-sublevels t
  2891. "Non-nil means list also sublevels of headlines matching a search.
  2892. This variable applies to tags/property searches, and also to stuck
  2893. projects because this search is based on a tags match as well.
  2894. When set to the symbol `indented', sublevels are indented with
  2895. leading dots.
  2896. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2897. the sublevels of a headline matching a tag search often also match
  2898. the same search. Listing all of them can create very long lists.
  2899. Setting this variable to nil causes subtrees of a match to be skipped.
  2900. This variable is semi-obsolete and probably should always be true. It
  2901. is better to limit inheritance to certain tags using the variables
  2902. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2903. :group 'org-tags
  2904. :type '(choice
  2905. (const :tag "No, don't list them" nil)
  2906. (const :tag "Yes, do list them" t)
  2907. (const :tag "List them, indented with leading dots" indented)))
  2908. (defcustom org-tags-sort-function nil
  2909. "When set, tags are sorted using this function as a comparator."
  2910. :group 'org-tags
  2911. :type '(choice
  2912. (const :tag "No sorting" nil)
  2913. (const :tag "Alphabetical" string<)
  2914. (const :tag "Reverse alphabetical" string>)
  2915. (function :tag "Custom function" nil)))
  2916. (defvar org-tags-history nil
  2917. "History of minibuffer reads for tags.")
  2918. (defvar org-last-tags-completion-table nil
  2919. "The last used completion table for tags.")
  2920. (defvar org-after-tags-change-hook nil
  2921. "Hook that is run after the tags in a line have changed.")
  2922. (defgroup org-properties nil
  2923. "Options concerning properties in Org-mode."
  2924. :tag "Org Properties"
  2925. :group 'org)
  2926. (defcustom org-property-format "%-10s %s"
  2927. "How property key/value pairs should be formatted by `indent-line'.
  2928. When `indent-line' hits a property definition, it will format the line
  2929. according to this format, mainly to make sure that the values are
  2930. lined-up with respect to each other."
  2931. :group 'org-properties
  2932. :type 'string)
  2933. (defcustom org-properties-postprocess-alist nil
  2934. "Alist of properties and functions to adjust inserted values.
  2935. Elements of this alist must be of the form
  2936. ([string] [function])
  2937. where [string] must be a property name and [function] must be a
  2938. lambda expression: this lambda expression must take one argument,
  2939. the value to adjust, and return the new value as a string.
  2940. For example, this element will allow the property \"Remaining\"
  2941. to be updated wrt the relation between the \"Effort\" property
  2942. and the clock summary:
  2943. ((\"Remaining\" (lambda(value)
  2944. (let ((clocksum (org-clock-sum-current-item))
  2945. (effort (org-duration-string-to-minutes
  2946. (org-entry-get (point) \"Effort\"))))
  2947. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2948. :group 'org-properties
  2949. :version "24.1"
  2950. :type '(alist :key-type (string :tag "Property")
  2951. :value-type (function :tag "Function")))
  2952. (defcustom org-use-property-inheritance nil
  2953. "Non-nil means properties apply also for sublevels.
  2954. This setting is chiefly used during property searches. Turning it on can
  2955. cause significant overhead when doing a search, which is why it is not
  2956. on by default.
  2957. When nil, only the properties directly given in the current entry count.
  2958. When t, every property is inherited. The value may also be a list of
  2959. properties that should have inheritance, or a regular expression matching
  2960. properties that should be inherited.
  2961. However, note that some special properties use inheritance under special
  2962. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2963. and the properties ending in \"_ALL\" when they are used as descriptor
  2964. for valid values of a property.
  2965. Note for programmers:
  2966. When querying an entry with `org-entry-get', you can control if inheritance
  2967. should be used. By default, `org-entry-get' looks only at the local
  2968. properties. You can request inheritance by setting the inherit argument
  2969. to t (to force inheritance) or to `selective' (to respect the setting
  2970. in this variable)."
  2971. :group 'org-properties
  2972. :type '(choice
  2973. (const :tag "Not" nil)
  2974. (const :tag "Always" t)
  2975. (repeat :tag "Specific properties" (string :tag "Property"))
  2976. (regexp :tag "Properties matched by regexp")))
  2977. (defun org-property-inherit-p (property)
  2978. "Check if PROPERTY is one that should be inherited."
  2979. (cond
  2980. ((eq org-use-property-inheritance t) t)
  2981. ((not org-use-property-inheritance) nil)
  2982. ((stringp org-use-property-inheritance)
  2983. (string-match org-use-property-inheritance property))
  2984. ((listp org-use-property-inheritance)
  2985. (member property org-use-property-inheritance))
  2986. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2987. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2988. "The default column format, if no other format has been defined.
  2989. This variable can be set on the per-file basis by inserting a line
  2990. #+COLUMNS: %25ITEM ....."
  2991. :group 'org-properties
  2992. :type 'string)
  2993. (defcustom org-columns-ellipses ".."
  2994. "The ellipses to be used when a field in column view is truncated.
  2995. When this is the empty string, as many characters as possible are shown,
  2996. but then there will be no visual indication that the field has been truncated.
  2997. When this is a string of length N, the last N characters of a truncated
  2998. field are replaced by this string. If the column is narrower than the
  2999. ellipses string, only part of the ellipses string will be shown."
  3000. :group 'org-properties
  3001. :type 'string)
  3002. (defcustom org-columns-modify-value-for-display-function nil
  3003. "Function that modifies values for display in column view.
  3004. For example, it can be used to cut out a certain part from a time stamp.
  3005. The function must take 2 arguments:
  3006. column-title The title of the column (*not* the property name)
  3007. value The value that should be modified.
  3008. The function should return the value that should be displayed,
  3009. or nil if the normal value should be used."
  3010. :group 'org-properties
  3011. :type 'function)
  3012. (defcustom org-effort-property "Effort"
  3013. "The property that is being used to keep track of effort estimates.
  3014. Effort estimates given in this property need to have the format H:MM."
  3015. :group 'org-properties
  3016. :group 'org-progress
  3017. :type '(string :tag "Property"))
  3018. (defconst org-global-properties-fixed
  3019. '(("VISIBILITY_ALL" . "folded children content all")
  3020. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3021. "List of property/value pairs that can be inherited by any entry.
  3022. These are fixed values, for the preset properties. The user variable
  3023. that can be used to add to this list is `org-global-properties'.
  3024. The entries in this list are cons cells where the car is a property
  3025. name and cdr is a string with the value. If the value represents
  3026. multiple items like an \"_ALL\" property, separate the items by
  3027. spaces.")
  3028. (defcustom org-global-properties nil
  3029. "List of property/value pairs that can be inherited by any entry.
  3030. This list will be combined with the constant `org-global-properties-fixed'.
  3031. The entries in this list are cons cells where the car is a property
  3032. name and cdr is a string with the value.
  3033. You can set buffer-local values for the same purpose in the variable
  3034. `org-file-properties' this by adding lines like
  3035. #+PROPERTY: NAME VALUE"
  3036. :group 'org-properties
  3037. :type '(repeat
  3038. (cons (string :tag "Property")
  3039. (string :tag "Value"))))
  3040. (defvar org-file-properties nil
  3041. "List of property/value pairs that can be inherited by any entry.
  3042. Valid for the current buffer.
  3043. This variable is populated from #+PROPERTY lines.")
  3044. (make-variable-buffer-local 'org-file-properties)
  3045. (defgroup org-agenda nil
  3046. "Options concerning agenda views in Org-mode."
  3047. :tag "Org Agenda"
  3048. :group 'org)
  3049. (defvar org-category nil
  3050. "Variable used by org files to set a category for agenda display.
  3051. Such files should use a file variable to set it, for example
  3052. # -*- mode: org; org-category: \"ELisp\"
  3053. or contain a special line
  3054. #+CATEGORY: ELisp
  3055. If the file does not specify a category, then file's base name
  3056. is used instead.")
  3057. (make-variable-buffer-local 'org-category)
  3058. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3059. (defcustom org-agenda-files nil
  3060. "The files to be used for agenda display.
  3061. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3062. \\[org-remove-file]. You can also use customize to edit the list.
  3063. If an entry is a directory, all files in that directory that are matched by
  3064. `org-agenda-file-regexp' will be part of the file list.
  3065. If the value of the variable is not a list but a single file name, then
  3066. the list of agenda files is actually stored and maintained in that file, one
  3067. agenda file per line. In this file paths can be given relative to
  3068. `org-directory'. Tilde expansion and environment variable substitution
  3069. are also made."
  3070. :group 'org-agenda
  3071. :type '(choice
  3072. (repeat :tag "List of files and directories" file)
  3073. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3074. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3075. "Regular expression to match files for `org-agenda-files'.
  3076. If any element in the list in that variable contains a directory instead
  3077. of a normal file, all files in that directory that are matched by this
  3078. regular expression will be included."
  3079. :group 'org-agenda
  3080. :type 'regexp)
  3081. (defcustom org-agenda-text-search-extra-files nil
  3082. "List of extra files to be searched by text search commands.
  3083. These files will be search in addition to the agenda files by the
  3084. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3085. Note that these files will only be searched for text search commands,
  3086. not for the other agenda views like todo lists, tag searches or the weekly
  3087. agenda. This variable is intended to list notes and possibly archive files
  3088. that should also be searched by these two commands.
  3089. In fact, if the first element in the list is the symbol `agenda-archives',
  3090. than all archive files of all agenda files will be added to the search
  3091. scope."
  3092. :group 'org-agenda
  3093. :type '(set :greedy t
  3094. (const :tag "Agenda Archives" agenda-archives)
  3095. (repeat :inline t (file))))
  3096. (if (fboundp 'defvaralias)
  3097. (defvaralias 'org-agenda-multi-occur-extra-files
  3098. 'org-agenda-text-search-extra-files))
  3099. (defcustom org-agenda-skip-unavailable-files nil
  3100. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3101. A nil value means to remove them, after a query, from the list."
  3102. :group 'org-agenda
  3103. :type 'boolean)
  3104. (defcustom org-calendar-to-agenda-key [?c]
  3105. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3106. The command `org-calendar-goto-agenda' will be bound to this key. The
  3107. default is the character `c' because then `c' can be used to switch back and
  3108. forth between agenda and calendar."
  3109. :group 'org-agenda
  3110. :type 'sexp)
  3111. (defcustom org-calendar-insert-diary-entry-key [?i]
  3112. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3113. This option is irrelevant until `org-agenda-diary-file' has been configured
  3114. to point to an Org-mode file. When that is the case, the command
  3115. `org-agenda-diary-entry' will be bound to the key given here, by default
  3116. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3117. if you want to continue doing this, you need to change this to a different
  3118. key."
  3119. :group 'org-agenda
  3120. :type 'sexp)
  3121. (defcustom org-agenda-diary-file 'diary-file
  3122. "File to which to add new entries with the `i' key in agenda and calendar.
  3123. When this is the symbol `diary-file', the functionality in the Emacs
  3124. calendar will be used to add entries to the `diary-file'. But when this
  3125. points to a file, `org-agenda-diary-entry' will be used instead."
  3126. :group 'org-agenda
  3127. :type '(choice
  3128. (const :tag "The standard Emacs diary file" diary-file)
  3129. (file :tag "Special Org file diary entries")))
  3130. (eval-after-load "calendar"
  3131. '(progn
  3132. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3133. 'org-calendar-goto-agenda)
  3134. (add-hook 'calendar-mode-hook
  3135. (lambda ()
  3136. (unless (eq org-agenda-diary-file 'diary-file)
  3137. (define-key calendar-mode-map
  3138. org-calendar-insert-diary-entry-key
  3139. 'org-agenda-diary-entry))))))
  3140. (defgroup org-latex nil
  3141. "Options for embedding LaTeX code into Org-mode."
  3142. :tag "Org LaTeX"
  3143. :group 'org)
  3144. (defcustom org-format-latex-options
  3145. '(:foreground default :background default :scale 1.0
  3146. :html-foreground "Black" :html-background "Transparent"
  3147. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3148. "Options for creating images from LaTeX fragments.
  3149. This is a property list with the following properties:
  3150. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3151. `default' means use the foreground of the default face.
  3152. `auto' means use the foreground from the text face.
  3153. :background the background color, or \"Transparent\".
  3154. `default' means use the background of the default face.
  3155. `auto' means use the background from the text face.
  3156. :scale a scaling factor for the size of the images, to get more pixels
  3157. :html-foreground, :html-background, :html-scale
  3158. the same numbers for HTML export.
  3159. :matchers a list indicating which matchers should be used to
  3160. find LaTeX fragments. Valid members of this list are:
  3161. \"begin\" find environments
  3162. \"$1\" find single characters surrounded by $.$
  3163. \"$\" find math expressions surrounded by $...$
  3164. \"$$\" find math expressions surrounded by $$....$$
  3165. \"\\(\" find math expressions surrounded by \\(...\\)
  3166. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3167. :group 'org-latex
  3168. :type 'plist)
  3169. (defcustom org-format-latex-signal-error t
  3170. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3171. When nil, just push out a message."
  3172. :group 'org-latex
  3173. :version "24.1"
  3174. :type 'boolean)
  3175. (defcustom org-latex-to-mathml-jar-file nil
  3176. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3177. Use this to specify additional executable file say a jar file.
  3178. When using MathToWeb as the converter, specify the full-path to
  3179. your mathtoweb.jar file."
  3180. :group 'org-latex
  3181. :version "24.1"
  3182. :type '(choice
  3183. (const :tag "None" nil)
  3184. (file :tag "JAR file" :must-match t)))
  3185. (defcustom org-latex-to-mathml-convert-command nil
  3186. "Command to convert LaTeX fragments to MathML.
  3187. Replace format-specifiers in the command as noted below and use
  3188. `shell-command' to convert LaTeX to MathML.
  3189. %j: Executable file in fully expanded form as specified by
  3190. `org-latex-to-mathml-jar-file'.
  3191. %I: Input LaTeX file in fully expanded form
  3192. %o: Output MathML file
  3193. This command is used by `org-create-math-formula'.
  3194. When using MathToWeb as the converter, set this to
  3195. \"java -jar %j -unicode -force -df %o %I\"."
  3196. :group 'org-latex
  3197. :version "24.1"
  3198. :type '(choice
  3199. (const :tag "None" nil)
  3200. (string :tag "\nShell command")))
  3201. (defcustom org-latex-create-formula-image-program 'dvipng
  3202. "Program to convert LaTeX fragments with.
  3203. dvipng Process the LaTeX fragments to dvi file, then convert
  3204. dvi files to png files using dvipng.
  3205. This will also include processing of non-math environments.
  3206. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3207. to convert pdf files to png files"
  3208. :group 'org-latex
  3209. :version "24.1"
  3210. :type '(choice
  3211. (const :tag "dvipng" dvipng)
  3212. (const :tag "imagemagick" imagemagick)))
  3213. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3214. "Path to store latex preview images.
  3215. A relative path here creates many directories relative to the
  3216. processed org files paths. An absolute path puts all preview
  3217. images at the same place."
  3218. :group 'org-latex
  3219. :version "24.3"
  3220. :type 'string)
  3221. (defun org-format-latex-mathml-available-p ()
  3222. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3223. (save-match-data
  3224. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3225. org-latex-to-mathml-convert-command)
  3226. (let ((executable (car (split-string
  3227. org-latex-to-mathml-convert-command))))
  3228. (when (executable-find executable)
  3229. (if (string-match
  3230. "%j" org-latex-to-mathml-convert-command)
  3231. (file-readable-p org-latex-to-mathml-jar-file)
  3232. t))))))
  3233. (defcustom org-format-latex-header "\\documentclass{article}
  3234. \\usepackage[usenames]{color}
  3235. \\usepackage{amsmath}
  3236. \\usepackage[mathscr]{eucal}
  3237. \\pagestyle{empty} % do not remove
  3238. \[PACKAGES]
  3239. \[DEFAULT-PACKAGES]
  3240. % The settings below are copied from fullpage.sty
  3241. \\setlength{\\textwidth}{\\paperwidth}
  3242. \\addtolength{\\textwidth}{-3cm}
  3243. \\setlength{\\oddsidemargin}{1.5cm}
  3244. \\addtolength{\\oddsidemargin}{-2.54cm}
  3245. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3246. \\setlength{\\textheight}{\\paperheight}
  3247. \\addtolength{\\textheight}{-\\headheight}
  3248. \\addtolength{\\textheight}{-\\headsep}
  3249. \\addtolength{\\textheight}{-\\footskip}
  3250. \\addtolength{\\textheight}{-3cm}
  3251. \\setlength{\\topmargin}{1.5cm}
  3252. \\addtolength{\\topmargin}{-2.54cm}"
  3253. "The document header used for processing LaTeX fragments.
  3254. It is imperative that this header make sure that no page number
  3255. appears on the page. The package defined in the variables
  3256. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3257. will either replace the placeholder \"[PACKAGES]\" in this
  3258. header, or they will be appended."
  3259. :group 'org-latex
  3260. :type 'string)
  3261. (defun org-set-packages-alist (var val)
  3262. "Set the packages alist and make sure it has 3 elements per entry."
  3263. (set var (mapcar (lambda (x)
  3264. (if (and (consp x) (= (length x) 2))
  3265. (list (car x) (nth 1 x) t)
  3266. x))
  3267. val)))
  3268. (defun org-get-packages-alist (var)
  3269. "Get the packages alist and make sure it has 3 elements per entry."
  3270. (mapcar (lambda (x)
  3271. (if (and (consp x) (= (length x) 2))
  3272. (list (car x) (nth 1 x) t)
  3273. x))
  3274. (default-value var)))
  3275. (defcustom org-latex-default-packages-alist
  3276. '(("AUTO" "inputenc" t)
  3277. ("T1" "fontenc" t)
  3278. ("" "fixltx2e" nil)
  3279. ("" "graphicx" t)
  3280. ("" "longtable" nil)
  3281. ("" "float" nil)
  3282. ("" "wrapfig" nil)
  3283. ("" "soul" t)
  3284. ("" "textcomp" t)
  3285. ("" "marvosym" t)
  3286. ("" "wasysym" t)
  3287. ("" "latexsym" t)
  3288. ("" "amssymb" t)
  3289. ("" "hyperref" nil)
  3290. "\\tolerance=1000")
  3291. "Alist of default packages to be inserted in the header.
  3292. Change this only if one of the packages here causes an
  3293. incompatibility with another package you are using.
  3294. The packages in this list are needed by one part or another of
  3295. Org mode to function properly:
  3296. - inputenc, fontenc: for basic font and character selection
  3297. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3298. symbols used for interpreting the entities in `org-entities'.
  3299. You can skip some of these packages if you don't use any of the
  3300. symbols in it.
  3301. - graphicx: for including images
  3302. - float, wrapfig: for figure placement
  3303. - longtable: for long tables
  3304. - hyperref: for cross references
  3305. Therefore you should not modify this variable unless you know
  3306. what you are doing. The one reason to change it anyway is that
  3307. you might be loading some other package that conflicts with one
  3308. of the default packages. Each cell is of the format
  3309. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3310. the package also needs to be included when compiling LaTeX
  3311. snippets into images for inclusion into non-LaTeX output."
  3312. :group 'org-latex
  3313. :group 'org-export-latex
  3314. :set 'org-set-packages-alist
  3315. :get 'org-get-packages-alist
  3316. :version "24.1"
  3317. :type '(repeat
  3318. (choice
  3319. (list :tag "options/package pair"
  3320. (string :tag "options")
  3321. (string :tag "package")
  3322. (boolean :tag "Snippet"))
  3323. (string :tag "A line of LaTeX"))))
  3324. (defcustom org-latex-packages-alist nil
  3325. "Alist of packages to be inserted in every LaTeX header.
  3326. These will be inserted after `org-latex-default-packages-alist'.
  3327. Each cell is of the format:
  3328. \(\"options\" \"package\" snippet-flag)
  3329. SNIPPET-FLAG, when t, indicates that this package is also needed
  3330. when turning LaTeX snippets into images for inclusion into
  3331. non-LaTeX output.
  3332. Make sure that you only list packages here which:
  3333. - you want in every file
  3334. - do not conflict with the setup in `org-format-latex-header'.
  3335. - do not conflict with the default packages in
  3336. `org-latex-default-packages-alist'."
  3337. :group 'org-latex
  3338. :group 'org-export-latex
  3339. :set 'org-set-packages-alist
  3340. :get 'org-get-packages-alist
  3341. :type '(repeat
  3342. (choice
  3343. (list :tag "options/package pair"
  3344. (string :tag "options")
  3345. (string :tag "package")
  3346. (boolean :tag "Snippet"))
  3347. (string :tag "A line of LaTeX"))))
  3348. (defgroup org-appearance nil
  3349. "Settings for Org-mode appearance."
  3350. :tag "Org Appearance"
  3351. :group 'org)
  3352. (defcustom org-level-color-stars-only nil
  3353. "Non-nil means fontify only the stars in each headline.
  3354. When nil, the entire headline is fontified.
  3355. Changing it requires restart of `font-lock-mode' to become effective
  3356. also in regions already fontified."
  3357. :group 'org-appearance
  3358. :type 'boolean)
  3359. (defcustom org-hide-leading-stars nil
  3360. "Non-nil means hide the first N-1 stars in a headline.
  3361. This works by using the face `org-hide' for these stars. This
  3362. face is white for a light background, and black for a dark
  3363. background. You may have to customize the face `org-hide' to
  3364. make this work.
  3365. Changing it requires restart of `font-lock-mode' to become effective
  3366. also in regions already fontified.
  3367. You may also set this on a per-file basis by adding one of the following
  3368. lines to the buffer:
  3369. #+STARTUP: hidestars
  3370. #+STARTUP: showstars"
  3371. :group 'org-appearance
  3372. :type 'boolean)
  3373. (defcustom org-hidden-keywords nil
  3374. "List of symbols corresponding to keywords to be hidden the org buffer.
  3375. For example, a value '(title) for this list will make the document's title
  3376. appear in the buffer without the initial #+TITLE: keyword."
  3377. :group 'org-appearance
  3378. :version "24.1"
  3379. :type '(set (const :tag "#+AUTHOR" author)
  3380. (const :tag "#+DATE" date)
  3381. (const :tag "#+EMAIL" email)
  3382. (const :tag "#+TITLE" title)))
  3383. (defcustom org-custom-properties nil
  3384. "List of properties (as strings) with a special meaning.
  3385. The default use of these custom properties is to let the user
  3386. hide them with `org-toggle-custom-properties-visibility'."
  3387. :group 'org-properties
  3388. :group 'org-appearance
  3389. :version "24.3"
  3390. :type '(repeat (string :tag "Property Name")))
  3391. (defcustom org-fontify-done-headline nil
  3392. "Non-nil means change the face of a headline if it is marked DONE.
  3393. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3394. When this is non-nil, the headline after the keyword is set to the
  3395. `org-headline-done' as an additional indication."
  3396. :group 'org-appearance
  3397. :type 'boolean)
  3398. (defcustom org-fontify-emphasized-text t
  3399. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3400. Changing this variable requires a restart of Emacs to take effect."
  3401. :group 'org-appearance
  3402. :type 'boolean)
  3403. (defcustom org-fontify-whole-heading-line nil
  3404. "Non-nil means fontify the whole line for headings.
  3405. This is useful when setting a background color for the
  3406. org-level-* faces."
  3407. :group 'org-appearance
  3408. :type 'boolean)
  3409. (defcustom org-highlight-latex-and-related nil
  3410. "Non-nil means highlight LaTeX related syntax in the buffer.
  3411. When non nil, the value should be a list containing any of the
  3412. following symbols:
  3413. `latex' Highlight LaTeX snippets and environments.
  3414. `script' Highlight subscript and superscript.
  3415. `entities' Highlight entities."
  3416. :group 'org-appearance
  3417. :version "24.4"
  3418. :package-version '(Org . "8.0")
  3419. :type '(choice
  3420. (const :tag "No highlighting" nil)
  3421. (set :greedy t :tag "Highlight"
  3422. (const :tag "LaTeX snippets and environments" latex)
  3423. (const :tag "Subscript and superscript" script)
  3424. (const :tag "Entities" entities))))
  3425. (defcustom org-hide-emphasis-markers nil
  3426. "Non-nil mean font-lock should hide the emphasis marker characters."
  3427. :group 'org-appearance
  3428. :type 'boolean)
  3429. (defcustom org-pretty-entities nil
  3430. "Non-nil means show entities as UTF8 characters.
  3431. When nil, the \\name form remains in the buffer."
  3432. :group 'org-appearance
  3433. :version "24.1"
  3434. :type 'boolean)
  3435. (defcustom org-pretty-entities-include-sub-superscripts t
  3436. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3437. :group 'org-appearance
  3438. :version "24.1"
  3439. :type 'boolean)
  3440. (defvar org-emph-re nil
  3441. "Regular expression for matching emphasis.
  3442. After a match, the match groups contain these elements:
  3443. 0 The match of the full regular expression, including the characters
  3444. before and after the proper match
  3445. 1 The character before the proper match, or empty at beginning of line
  3446. 2 The proper match, including the leading and trailing markers
  3447. 3 The leading marker like * or /, indicating the type of highlighting
  3448. 4 The text between the emphasis markers, not including the markers
  3449. 5 The character after the match, empty at the end of a line")
  3450. (defvar org-verbatim-re nil
  3451. "Regular expression for matching verbatim text.")
  3452. (defvar org-emphasis-regexp-components) ; defined just below
  3453. (defvar org-emphasis-alist) ; defined just below
  3454. (defun org-set-emph-re (var val)
  3455. "Set variable and compute the emphasis regular expression."
  3456. (set var val)
  3457. (when (and (boundp 'org-emphasis-alist)
  3458. (boundp 'org-emphasis-regexp-components)
  3459. org-emphasis-alist org-emphasis-regexp-components)
  3460. (let* ((e org-emphasis-regexp-components)
  3461. (pre (car e))
  3462. (post (nth 1 e))
  3463. (border (nth 2 e))
  3464. (body (nth 3 e))
  3465. (nl (nth 4 e))
  3466. (body1 (concat body "*?"))
  3467. (markers (mapconcat 'car org-emphasis-alist ""))
  3468. (vmarkers (mapconcat
  3469. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3470. org-emphasis-alist "")))
  3471. ;; make sure special characters appear at the right position in the class
  3472. (if (string-match "\\^" markers)
  3473. (setq markers (concat (replace-match "" t t markers) "^")))
  3474. (if (string-match "-" markers)
  3475. (setq markers (concat (replace-match "" t t markers) "-")))
  3476. (if (string-match "\\^" vmarkers)
  3477. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3478. (if (string-match "-" vmarkers)
  3479. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3480. (if (> nl 0)
  3481. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3482. (int-to-string nl) "\\}")))
  3483. ;; Make the regexp
  3484. (setq org-emph-re
  3485. (concat "\\([" pre "]\\|^\\)"
  3486. "\\("
  3487. "\\([" markers "]\\)"
  3488. "\\("
  3489. "[^" border "]\\|"
  3490. "[^" border "]"
  3491. body1
  3492. "[^" border "]"
  3493. "\\)"
  3494. "\\3\\)"
  3495. "\\([" post "]\\|$\\)"))
  3496. (setq org-verbatim-re
  3497. (concat "\\([" pre "]\\|^\\)"
  3498. "\\("
  3499. "\\([" vmarkers "]\\)"
  3500. "\\("
  3501. "[^" border "]\\|"
  3502. "[^" border "]"
  3503. body1
  3504. "[^" border "]"
  3505. "\\)"
  3506. "\\3\\)"
  3507. "\\([" post "]\\|$\\)")))))
  3508. (defcustom org-emphasis-regexp-components
  3509. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3510. "Components used to build the regular expression for emphasis.
  3511. This is a list with five entries. Terminology: In an emphasis string
  3512. like \" *strong word* \", we call the initial space PREMATCH, the final
  3513. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3514. and \"trong wor\" is the body. The different components in this variable
  3515. specify what is allowed/forbidden in each part:
  3516. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3517. post Chars allowed as postmatch. End of line will be allowed too.
  3518. border The chars *forbidden* as border characters.
  3519. body-regexp A regexp like \".\" to match a body character. Don't use
  3520. non-shy groups here, and don't allow newline here.
  3521. newline The maximum number of newlines allowed in an emphasis exp.
  3522. Use customize to modify this, or restart Emacs after changing it."
  3523. :group 'org-appearance
  3524. :set 'org-set-emph-re
  3525. :type '(list
  3526. (sexp :tag "Allowed chars in pre ")
  3527. (sexp :tag "Allowed chars in post ")
  3528. (sexp :tag "Forbidden chars in border ")
  3529. (sexp :tag "Regexp for body ")
  3530. (integer :tag "number of newlines allowed")
  3531. (option (boolean :tag "Please ignore this button"))))
  3532. (defcustom org-emphasis-alist
  3533. `(("*" bold "<b>" "</b>")
  3534. ("/" italic "<i>" "</i>")
  3535. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3536. ("=" org-code "<code>" "</code>" verbatim)
  3537. ("~" org-verbatim "<code>" "</code>" verbatim)
  3538. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3539. "<del>" "</del>")
  3540. )
  3541. "Special syntax for emphasized text.
  3542. Text starting and ending with a special character will be emphasized, for
  3543. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3544. characters, the face to be used by font-lock for highlighting in Org-mode
  3545. Emacs buffers, and the HTML tags to be used for this.
  3546. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3547. Use customize to modify this, or restart Emacs after changing it."
  3548. :group 'org-appearance
  3549. :set 'org-set-emph-re
  3550. :type '(repeat
  3551. (list
  3552. (string :tag "Marker character")
  3553. (choice
  3554. (face :tag "Font-lock-face")
  3555. (plist :tag "Face property list"))
  3556. (string :tag "HTML start tag")
  3557. (string :tag "HTML end tag")
  3558. (option (const verbatim)))))
  3559. (defvar org-protecting-blocks
  3560. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3561. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3562. This is needed for font-lock setup.")
  3563. ;;; Miscellaneous options
  3564. (defgroup org-completion nil
  3565. "Completion in Org-mode."
  3566. :tag "Org Completion"
  3567. :group 'org)
  3568. (defcustom org-completion-use-ido nil
  3569. "Non-nil means use ido completion wherever possible.
  3570. Note that `ido-mode' must be active for this variable to be relevant.
  3571. If you decide to turn this variable on, you might well want to turn off
  3572. `org-outline-path-complete-in-steps'.
  3573. See also `org-completion-use-iswitchb'."
  3574. :group 'org-completion
  3575. :type 'boolean)
  3576. (defcustom org-completion-use-iswitchb nil
  3577. "Non-nil means use iswitchb completion wherever possible.
  3578. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3579. If you decide to turn this variable on, you might well want to turn off
  3580. `org-outline-path-complete-in-steps'.
  3581. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3582. :group 'org-completion
  3583. :type 'boolean)
  3584. (defcustom org-completion-fallback-command 'hippie-expand
  3585. "The expansion command called by \\[pcomplete] in normal context.
  3586. Normal means, no org-mode-specific context."
  3587. :group 'org-completion
  3588. :type 'function)
  3589. ;;; Functions and variables from their packages
  3590. ;; Declared here to avoid compiler warnings
  3591. ;; XEmacs only
  3592. (defvar outline-mode-menu-heading)
  3593. (defvar outline-mode-menu-show)
  3594. (defvar outline-mode-menu-hide)
  3595. (defvar zmacs-regions) ; XEmacs regions
  3596. ;; Emacs only
  3597. (defvar mark-active)
  3598. ;; Various packages
  3599. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3600. (declare-function calendar-forward-day "cal-move" (arg))
  3601. (declare-function calendar-goto-date "cal-move" (date))
  3602. (declare-function calendar-goto-today "cal-move" ())
  3603. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3604. (defvar calc-embedded-close-formula)
  3605. (defvar calc-embedded-open-formula)
  3606. (declare-function cdlatex-tab "ext:cdlatex" ())
  3607. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3608. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3609. (defvar font-lock-unfontify-region-function)
  3610. (declare-function iswitchb-read-buffer "iswitchb"
  3611. (prompt &optional default require-match start matches-set))
  3612. (defvar iswitchb-temp-buflist)
  3613. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3614. (defvar org-agenda-tags-todo-honor-ignore-options)
  3615. (declare-function org-agenda-skip "org-agenda" ())
  3616. (declare-function
  3617. org-agenda-format-item "org-agenda"
  3618. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3619. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3620. (declare-function org-agenda-change-all-lines "org-agenda"
  3621. (newhead hdmarker &optional fixface just-this))
  3622. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3623. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3624. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3625. (beg end))
  3626. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3627. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3628. "org-agenda" (&optional end))
  3629. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3630. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3631. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3632. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3633. (declare-function org-indent-mode "org-indent" (&optional arg))
  3634. (declare-function parse-time-string "parse-time" (string))
  3635. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3636. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3637. (defvar remember-data-file)
  3638. (defvar texmathp-why)
  3639. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3640. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3641. (defvar org-latex-regexps)
  3642. ;;; Autoload and prepare some org modules
  3643. ;; Some table stuff that needs to be defined here, because it is used
  3644. ;; by the functions setting up org-mode or checking for table context.
  3645. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3646. "Detect an org-type or table-type table.")
  3647. (defconst org-table-line-regexp "^[ \t]*|"
  3648. "Detect an org-type table line.")
  3649. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3650. "Detect an org-type table line.")
  3651. (defconst org-table-hline-regexp "^[ \t]*|-"
  3652. "Detect an org-type table hline.")
  3653. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3654. "Detect a table-type table hline.")
  3655. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3656. "Detect the first line outside a table when searching from within it.
  3657. This works for both table types.")
  3658. ;; Autoload the functions in org-table.el that are needed by functions here.
  3659. (eval-and-compile
  3660. (org-autoload "org-table"
  3661. '(org-table-begin org-table-blank-field org-table-end)))
  3662. ;;;###autoload
  3663. (defun turn-on-orgtbl ()
  3664. "Unconditionally turn on `orgtbl-mode'."
  3665. (require 'org-table)
  3666. (orgtbl-mode 1))
  3667. (defun org-at-table-p (&optional table-type)
  3668. "Return t if the cursor is inside an org-type table.
  3669. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3670. (if org-enable-table-editor
  3671. (save-excursion
  3672. (beginning-of-line 1)
  3673. (looking-at (if table-type org-table-any-line-regexp
  3674. org-table-line-regexp)))
  3675. nil))
  3676. (defsubst org-table-p () (org-at-table-p))
  3677. (defun org-at-table.el-p ()
  3678. "Return t if and only if we are at a table.el table."
  3679. (and (org-at-table-p 'any)
  3680. (save-excursion
  3681. (goto-char (org-table-begin 'any))
  3682. (looking-at org-table1-hline-regexp))))
  3683. (defun org-table-recognize-table.el ()
  3684. "If there is a table.el table nearby, recognize it and move into it."
  3685. (if org-table-tab-recognizes-table.el
  3686. (if (org-at-table.el-p)
  3687. (progn
  3688. (beginning-of-line 1)
  3689. (if (looking-at org-table-dataline-regexp)
  3690. nil
  3691. (if (looking-at org-table1-hline-regexp)
  3692. (progn
  3693. (beginning-of-line 2)
  3694. (if (looking-at org-table-any-border-regexp)
  3695. (beginning-of-line -1)))))
  3696. (if (re-search-forward "|" (org-table-end t) t)
  3697. (progn
  3698. (require 'table)
  3699. (if (table--at-cell-p (point))
  3700. t
  3701. (message "recognizing table.el table...")
  3702. (table-recognize-table)
  3703. (message "recognizing table.el table...done")))
  3704. (error "This should not happen"))
  3705. t)
  3706. nil)
  3707. nil))
  3708. (defun org-at-table-hline-p ()
  3709. "Return t if the cursor is inside a hline in a table."
  3710. (if org-enable-table-editor
  3711. (save-excursion
  3712. (beginning-of-line 1)
  3713. (looking-at org-table-hline-regexp))
  3714. nil))
  3715. (defvar org-table-clean-did-remove-column nil)
  3716. (defun org-table-map-tables (function &optional quietly)
  3717. "Apply FUNCTION to the start of all tables in the buffer."
  3718. (save-excursion
  3719. (save-restriction
  3720. (widen)
  3721. (goto-char (point-min))
  3722. (while (re-search-forward org-table-any-line-regexp nil t)
  3723. (unless quietly
  3724. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3725. (beginning-of-line 1)
  3726. (when (and (looking-at org-table-line-regexp)
  3727. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3728. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3729. (save-excursion (funcall function))
  3730. (or (looking-at org-table-line-regexp)
  3731. (forward-char 1)))
  3732. (re-search-forward org-table-any-border-regexp nil 1))))
  3733. (unless quietly (message "Mapping tables: done")))
  3734. ;; Declare and autoload functions from ox.el and al.
  3735. (declare-function org-export-get-environment "ox"
  3736. (&optional backend subtreep ext-plist))
  3737. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3738. ;; Declare and autoload functions from org-agenda.el
  3739. (eval-and-compile
  3740. (org-autoload "org-agenda"
  3741. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3742. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3743. (declare-function org-clock-update-mode-line "org-clock" ())
  3744. (declare-function org-resolve-clocks "org-clock"
  3745. (&optional also-non-dangling-p prompt last-valid))
  3746. (defvar org-clock-start-time)
  3747. (defvar org-clock-marker (make-marker)
  3748. "Marker recording the last clock-in.")
  3749. (defvar org-clock-hd-marker (make-marker)
  3750. "Marker recording the last clock-in, but the headline position.")
  3751. (defvar org-clock-heading ""
  3752. "The heading of the current clock entry.")
  3753. (defun org-clock-is-active ()
  3754. "Return non-nil if clock is currently running.
  3755. The return value is actually the clock marker."
  3756. (marker-buffer org-clock-marker))
  3757. (eval-and-compile
  3758. (org-autoload "org-clock" '(org-clock-remove-overlays
  3759. org-clock-update-time-maybe
  3760. org-clocktable-shift)))
  3761. (defun org-check-running-clock ()
  3762. "Check if the current buffer contains the running clock.
  3763. If yes, offer to stop it and to save the buffer with the changes."
  3764. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3765. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3766. (buffer-name))))
  3767. (org-clock-out)
  3768. (when (y-or-n-p "Save changed buffer?")
  3769. (save-buffer))))
  3770. (defun org-clocktable-try-shift (dir n)
  3771. "Check if this line starts a clock table, if yes, shift the time block."
  3772. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3773. (org-clocktable-shift dir n)))
  3774. ;;;###autoload
  3775. (defun org-clock-persistence-insinuate ()
  3776. "Set up hooks for clock persistence."
  3777. (require 'org-clock)
  3778. (add-hook 'org-mode-hook 'org-clock-load)
  3779. (add-hook 'kill-emacs-hook 'org-clock-save))
  3780. ;; Define the variable already here, to make sure we have it.
  3781. (defvar org-indent-mode nil
  3782. "Non-nil if Org-Indent mode is enabled.
  3783. Use the command `org-indent-mode' to change this variable.")
  3784. ;; Autoload archiving code
  3785. ;; The stuff that is needed for cycling and tags has to be defined here.
  3786. (defgroup org-archive nil
  3787. "Options concerning archiving in Org-mode."
  3788. :tag "Org Archive"
  3789. :group 'org-structure)
  3790. (defcustom org-archive-location "%s_archive::"
  3791. "The location where subtrees should be archived.
  3792. The value of this variable is a string, consisting of two parts,
  3793. separated by a double-colon. The first part is a filename and
  3794. the second part is a headline.
  3795. When the filename is omitted, archiving happens in the same file.
  3796. %s in the filename will be replaced by the current file
  3797. name (without the directory part). Archiving to a different file
  3798. is useful to keep archived entries from contributing to the
  3799. Org-mode Agenda.
  3800. The archived entries will be filed as subtrees of the specified
  3801. headline. When the headline is omitted, the subtrees are simply
  3802. filed away at the end of the file, as top-level entries. Also in
  3803. the heading you can use %s to represent the file name, this can be
  3804. useful when using the same archive for a number of different files.
  3805. Here are a few examples:
  3806. \"%s_archive::\"
  3807. If the current file is Projects.org, archive in file
  3808. Projects.org_archive, as top-level trees. This is the default.
  3809. \"::* Archived Tasks\"
  3810. Archive in the current file, under the top-level headline
  3811. \"* Archived Tasks\".
  3812. \"~/org/archive.org::\"
  3813. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3814. \"~/org/archive.org::* From %s\"
  3815. Archive in file ~/org/archive.org (absolute path), under headlines
  3816. \"From FILENAME\" where file name is the current file name.
  3817. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3818. The \"datetree/\" string is special, signifying to archive
  3819. items to the datetree. Items are placed in either the CLOSED
  3820. date of the item, or the current date if there is no CLOSED date.
  3821. The heading will be a subentry to the current date. There doesn't
  3822. need to be a heading, but there always needs to be a slash after
  3823. datetree. For example, to store archived items directly in the
  3824. datetree, use \"~/org/datetree.org::datetree/\".
  3825. \"basement::** Finished Tasks\"
  3826. Archive in file ./basement (relative path), as level 3 trees
  3827. below the level 2 heading \"** Finished Tasks\".
  3828. You may set this option on a per-file basis by adding to the buffer a
  3829. line like
  3830. #+ARCHIVE: basement::** Finished Tasks
  3831. You may also define it locally for a subtree by setting an ARCHIVE property
  3832. in the entry. If such a property is found in an entry, or anywhere up
  3833. the hierarchy, it will be used."
  3834. :group 'org-archive
  3835. :type 'string)
  3836. (defcustom org-archive-tag "ARCHIVE"
  3837. "The tag that marks a subtree as archived.
  3838. An archived subtree does not open during visibility cycling, and does
  3839. not contribute to the agenda listings.
  3840. After changing this, font-lock must be restarted in the relevant buffers to
  3841. get the proper fontification."
  3842. :group 'org-archive
  3843. :group 'org-keywords
  3844. :type 'string)
  3845. (defcustom org-agenda-skip-archived-trees t
  3846. "Non-nil means the agenda will skip any items located in archived trees.
  3847. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3848. variable is no longer recommended, you should leave it at the value t.
  3849. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3850. :group 'org-archive
  3851. :group 'org-agenda-skip
  3852. :type 'boolean)
  3853. (defcustom org-columns-skip-archived-trees t
  3854. "Non-nil means ignore archived trees when creating column view."
  3855. :group 'org-archive
  3856. :group 'org-properties
  3857. :type 'boolean)
  3858. (defcustom org-cycle-open-archived-trees nil
  3859. "Non-nil means `org-cycle' will open archived trees.
  3860. An archived tree is a tree marked with the tag ARCHIVE.
  3861. When nil, archived trees will stay folded. You can still open them with
  3862. normal outline commands like `show-all', but not with the cycling commands."
  3863. :group 'org-archive
  3864. :group 'org-cycle
  3865. :type 'boolean)
  3866. (defcustom org-sparse-tree-open-archived-trees nil
  3867. "Non-nil means sparse tree construction shows matches in archived trees.
  3868. When nil, matches in these trees are highlighted, but the trees are kept in
  3869. collapsed state."
  3870. :group 'org-archive
  3871. :group 'org-sparse-trees
  3872. :type 'boolean)
  3873. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3874. "The default date type when building a sparse tree.
  3875. When this is nil, a date is a scheduled or a deadline timestamp.
  3876. Otherwise, these types are allowed:
  3877. all: all timestamps
  3878. active: only active timestamps (<...>)
  3879. inactive: only inactive timestamps (<...)
  3880. scheduled: only scheduled timestamps
  3881. deadline: only deadline timestamps"
  3882. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3883. (const :tag "All timestamps" all)
  3884. (const :tag "Only active timestamps" active)
  3885. (const :tag "Only inactive timestamps" inactive)
  3886. (const :tag "Only scheduled timestamps" scheduled)
  3887. (const :tag "Only deadline timestamps" deadline))
  3888. :version "24.3"
  3889. :group 'org-sparse-trees)
  3890. (defun org-cycle-hide-archived-subtrees (state)
  3891. "Re-hide all archived subtrees after a visibility state change."
  3892. (when (and (not org-cycle-open-archived-trees)
  3893. (not (memq state '(overview folded))))
  3894. (save-excursion
  3895. (let* ((globalp (memq state '(contents all)))
  3896. (beg (if globalp (point-min) (point)))
  3897. (end (if globalp (point-max) (org-end-of-subtree t))))
  3898. (org-hide-archived-subtrees beg end)
  3899. (goto-char beg)
  3900. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3901. (message "%s" (substitute-command-keys
  3902. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3903. (defun org-force-cycle-archived ()
  3904. "Cycle subtree even if it is archived."
  3905. (interactive)
  3906. (setq this-command 'org-cycle)
  3907. (let ((org-cycle-open-archived-trees t))
  3908. (call-interactively 'org-cycle)))
  3909. (defun org-hide-archived-subtrees (beg end)
  3910. "Re-hide all archived subtrees after a visibility state change."
  3911. (save-excursion
  3912. (let* ((re (concat ":" org-archive-tag ":")))
  3913. (goto-char beg)
  3914. (while (re-search-forward re end t)
  3915. (when (org-at-heading-p)
  3916. (org-flag-subtree t)
  3917. (org-end-of-subtree t))))))
  3918. (declare-function outline-end-of-heading "outline" ())
  3919. (declare-function outline-flag-region "outline" (from to flag))
  3920. (defun org-flag-subtree (flag)
  3921. (save-excursion
  3922. (org-back-to-heading t)
  3923. (outline-end-of-heading)
  3924. (outline-flag-region (point)
  3925. (progn (org-end-of-subtree t) (point))
  3926. flag)))
  3927. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3928. (eval-and-compile
  3929. (org-autoload "org-archive"
  3930. '(org-add-archive-files)))
  3931. ;; Autoload Column View Code
  3932. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3933. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3934. (declare-function org-columns-compute "org-colview" (property))
  3935. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3936. '(org-columns-number-to-string
  3937. org-columns-get-format-and-top-level
  3938. org-columns-compute
  3939. org-columns-remove-overlays))
  3940. ;; Autoload ID code
  3941. (declare-function org-id-store-link "org-id")
  3942. (declare-function org-id-locations-load "org-id")
  3943. (declare-function org-id-locations-save "org-id")
  3944. (defvar org-id-track-globally)
  3945. (org-autoload "org-id"
  3946. '(org-id-new
  3947. org-id-copy
  3948. org-id-get-with-outline-path-completion
  3949. org-id-get-with-outline-drilling))
  3950. ;;; Variables for pre-computed regular expressions, all buffer local
  3951. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3952. "Matches first line of a hidden block.")
  3953. (make-variable-buffer-local 'org-drawer-regexp)
  3954. (defvar org-todo-regexp nil
  3955. "Matches any of the TODO state keywords.")
  3956. (make-variable-buffer-local 'org-todo-regexp)
  3957. (defvar org-not-done-regexp nil
  3958. "Matches any of the TODO state keywords except the last one.")
  3959. (make-variable-buffer-local 'org-not-done-regexp)
  3960. (defvar org-not-done-heading-regexp nil
  3961. "Matches a TODO headline that is not done.")
  3962. (make-variable-buffer-local 'org-not-done-regexp)
  3963. (defvar org-todo-line-regexp nil
  3964. "Matches a headline and puts TODO state into group 2 if present.")
  3965. (make-variable-buffer-local 'org-todo-line-regexp)
  3966. (defvar org-complex-heading-regexp nil
  3967. "Matches a headline and puts everything into groups:
  3968. group 1: the stars
  3969. group 2: The todo keyword, maybe
  3970. group 3: Priority cookie
  3971. group 4: True headline
  3972. group 5: Tags")
  3973. (make-variable-buffer-local 'org-complex-heading-regexp)
  3974. (defvar org-complex-heading-regexp-format nil
  3975. "Printf format to make regexp to match an exact headline.
  3976. This regexp will match the headline of any node which has the
  3977. exact headline text that is put into the format, but may have any
  3978. TODO state, priority and tags.")
  3979. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3980. (defvar org-todo-line-tags-regexp nil
  3981. "Matches a headline and puts TODO state into group 2 if present.
  3982. Also put tags into group 4 if tags are present.")
  3983. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3984. (defvar org-ds-keyword-length 12
  3985. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3986. (make-variable-buffer-local 'org-ds-keyword-length)
  3987. (defvar org-deadline-regexp nil
  3988. "Matches the DEADLINE keyword.")
  3989. (make-variable-buffer-local 'org-deadline-regexp)
  3990. (defvar org-deadline-time-regexp nil
  3991. "Matches the DEADLINE keyword together with a time stamp.")
  3992. (make-variable-buffer-local 'org-deadline-time-regexp)
  3993. (defvar org-deadline-line-regexp nil
  3994. "Matches the DEADLINE keyword and the rest of the line.")
  3995. (make-variable-buffer-local 'org-deadline-line-regexp)
  3996. (defvar org-scheduled-regexp nil
  3997. "Matches the SCHEDULED keyword.")
  3998. (make-variable-buffer-local 'org-scheduled-regexp)
  3999. (defvar org-scheduled-time-regexp nil
  4000. "Matches the SCHEDULED keyword together with a time stamp.")
  4001. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4002. (defvar org-closed-time-regexp nil
  4003. "Matches the CLOSED keyword together with a time stamp.")
  4004. (make-variable-buffer-local 'org-closed-time-regexp)
  4005. (defvar org-keyword-time-regexp nil
  4006. "Matches any of the 4 keywords, together with the time stamp.")
  4007. (make-variable-buffer-local 'org-keyword-time-regexp)
  4008. (defvar org-keyword-time-not-clock-regexp nil
  4009. "Matches any of the 3 keywords, together with the time stamp.")
  4010. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4011. (defvar org-maybe-keyword-time-regexp nil
  4012. "Matches a timestamp, possibly preceded by a keyword.")
  4013. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4014. (defvar org-all-time-keywords nil
  4015. "List of time keywords.")
  4016. (make-variable-buffer-local 'org-all-time-keywords)
  4017. (defconst org-plain-time-of-day-regexp
  4018. (concat
  4019. "\\(\\<[012]?[0-9]"
  4020. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4021. "\\(--?"
  4022. "\\(\\<[012]?[0-9]"
  4023. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4024. "\\)?")
  4025. "Regular expression to match a plain time or time range.
  4026. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4027. groups carry important information:
  4028. 0 the full match
  4029. 1 the first time, range or not
  4030. 8 the second time, if it is a range.")
  4031. (defconst org-plain-time-extension-regexp
  4032. (concat
  4033. "\\(\\<[012]?[0-9]"
  4034. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4035. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4036. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4037. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4038. groups carry important information:
  4039. 0 the full match
  4040. 7 hours of duration
  4041. 9 minutes of duration")
  4042. (defconst org-stamp-time-of-day-regexp
  4043. (concat
  4044. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4045. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4046. "\\(--?"
  4047. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4048. "Regular expression to match a timestamp time or time range.
  4049. After a match, the following groups carry important information:
  4050. 0 the full match
  4051. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4052. 2 the first time, range or not
  4053. 4 the second time, if it is a range.")
  4054. (defconst org-startup-options
  4055. '(("fold" org-startup-folded t)
  4056. ("overview" org-startup-folded t)
  4057. ("nofold" org-startup-folded nil)
  4058. ("showall" org-startup-folded nil)
  4059. ("showeverything" org-startup-folded showeverything)
  4060. ("content" org-startup-folded content)
  4061. ("indent" org-startup-indented t)
  4062. ("noindent" org-startup-indented nil)
  4063. ("hidestars" org-hide-leading-stars t)
  4064. ("showstars" org-hide-leading-stars nil)
  4065. ("odd" org-odd-levels-only t)
  4066. ("oddeven" org-odd-levels-only nil)
  4067. ("align" org-startup-align-all-tables t)
  4068. ("noalign" org-startup-align-all-tables nil)
  4069. ("inlineimages" org-startup-with-inline-images t)
  4070. ("noinlineimages" org-startup-with-inline-images nil)
  4071. ("latexpreview" org-startup-with-latex-preview t)
  4072. ("nolatexpreview" org-startup-with-latex-preview nil)
  4073. ("customtime" org-display-custom-times t)
  4074. ("logdone" org-log-done time)
  4075. ("lognotedone" org-log-done note)
  4076. ("nologdone" org-log-done nil)
  4077. ("lognoteclock-out" org-log-note-clock-out t)
  4078. ("nolognoteclock-out" org-log-note-clock-out nil)
  4079. ("logrepeat" org-log-repeat state)
  4080. ("lognoterepeat" org-log-repeat note)
  4081. ("logdrawer" org-log-into-drawer t)
  4082. ("nologdrawer" org-log-into-drawer nil)
  4083. ("logstatesreversed" org-log-states-order-reversed t)
  4084. ("nologstatesreversed" org-log-states-order-reversed nil)
  4085. ("nologrepeat" org-log-repeat nil)
  4086. ("logreschedule" org-log-reschedule time)
  4087. ("lognotereschedule" org-log-reschedule note)
  4088. ("nologreschedule" org-log-reschedule nil)
  4089. ("logredeadline" org-log-redeadline time)
  4090. ("lognoteredeadline" org-log-redeadline note)
  4091. ("nologredeadline" org-log-redeadline nil)
  4092. ("logrefile" org-log-refile time)
  4093. ("lognoterefile" org-log-refile note)
  4094. ("nologrefile" org-log-refile nil)
  4095. ("fninline" org-footnote-define-inline t)
  4096. ("nofninline" org-footnote-define-inline nil)
  4097. ("fnlocal" org-footnote-section nil)
  4098. ("fnauto" org-footnote-auto-label t)
  4099. ("fnprompt" org-footnote-auto-label nil)
  4100. ("fnconfirm" org-footnote-auto-label confirm)
  4101. ("fnplain" org-footnote-auto-label plain)
  4102. ("fnadjust" org-footnote-auto-adjust t)
  4103. ("nofnadjust" org-footnote-auto-adjust nil)
  4104. ("constcgs" constants-unit-system cgs)
  4105. ("constSI" constants-unit-system SI)
  4106. ("noptag" org-tag-persistent-alist nil)
  4107. ("hideblocks" org-hide-block-startup t)
  4108. ("nohideblocks" org-hide-block-startup nil)
  4109. ("beamer" org-startup-with-beamer-mode t)
  4110. ("entitiespretty" org-pretty-entities t)
  4111. ("entitiesplain" org-pretty-entities nil))
  4112. "Variable associated with STARTUP options for org-mode.
  4113. Each element is a list of three items: the startup options (as written
  4114. in the #+STARTUP line), the corresponding variable, and the value to set
  4115. this variable to if the option is found. An optional forth element PUSH
  4116. means to push this value onto the list in the variable.")
  4117. (defun org-update-property-plist (key val props)
  4118. "Update PROPS with KEY and VAL."
  4119. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4120. (key (if appending (substring key 0 (- (length key) 1)) key))
  4121. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4122. (previous (cdr (assoc key props))))
  4123. (if appending
  4124. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4125. (cons (cons key val) remainder))))
  4126. (defconst org-block-regexp
  4127. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4128. "Regular expression for hiding blocks.")
  4129. (defconst org-heading-keyword-regexp-format
  4130. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4131. "Printf format for a regexp matching a headline with some keyword.
  4132. This regexp will match the headline of any node which has the
  4133. exact keyword that is put into the format. The keyword isn't in
  4134. any group by default, but the stars and the body are.")
  4135. (defconst org-heading-keyword-maybe-regexp-format
  4136. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4137. "Printf format for a regexp matching a headline, possibly with some keyword.
  4138. This regexp can match any headline with the specified keyword, or
  4139. without a keyword. The keyword isn't in any group by default,
  4140. but the stars and the body are.")
  4141. (defun org-set-regexps-and-options ()
  4142. "Precompute regular expressions for current buffer."
  4143. (when (derived-mode-p 'org-mode)
  4144. (org-set-local 'org-todo-kwd-alist nil)
  4145. (org-set-local 'org-todo-key-alist nil)
  4146. (org-set-local 'org-todo-key-trigger nil)
  4147. (org-set-local 'org-todo-keywords-1 nil)
  4148. (org-set-local 'org-done-keywords nil)
  4149. (org-set-local 'org-todo-heads nil)
  4150. (org-set-local 'org-todo-sets nil)
  4151. (org-set-local 'org-todo-log-states nil)
  4152. (org-set-local 'org-file-properties nil)
  4153. (org-set-local 'org-file-tags nil)
  4154. (let ((re (org-make-options-regexp
  4155. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
  4156. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4157. "SETUPFILE" "OPTIONS")
  4158. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4159. (splitre "[ \t]+")
  4160. (scripts org-use-sub-superscripts)
  4161. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4162. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4163. (start 0))
  4164. (save-excursion
  4165. (save-restriction
  4166. (widen)
  4167. (goto-char (point-min))
  4168. (while (or (and ext-setup-or-nil
  4169. (string-match re ext-setup-or-nil start)
  4170. (setq start (match-end 0)))
  4171. (and (setq ext-setup-or-nil nil start 0)
  4172. (re-search-forward re nil t)))
  4173. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4174. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4175. (if (stringp value) (setq value (org-trim value)))
  4176. (cond
  4177. ((equal key "CATEGORY")
  4178. (setq cat value))
  4179. ((member key '("SEQ_TODO" "TODO"))
  4180. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4181. ((equal key "TYP_TODO")
  4182. (push (cons 'type (org-split-string value splitre)) kwds))
  4183. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4184. ;; general TODO-like setup
  4185. (push (cons (intern (downcase (match-string 1 key)))
  4186. (org-split-string value splitre)) kwds))
  4187. ((equal key "TAGS")
  4188. (setq tags (append tags (if tags '("\\n") nil)
  4189. (org-split-string value splitre))))
  4190. ((equal key "COLUMNS")
  4191. (org-set-local 'org-columns-default-format value))
  4192. ((equal key "LINK")
  4193. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4194. (push (cons (match-string 1 value)
  4195. (org-trim (match-string 2 value)))
  4196. links)))
  4197. ((equal key "PRIORITIES")
  4198. (setq prio (org-split-string value " +")))
  4199. ((equal key "PROPERTY")
  4200. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4201. (setq props (org-update-property-plist (match-string 1 value)
  4202. (match-string 2 value)
  4203. props))))
  4204. ((equal key "FILETAGS")
  4205. (when (string-match "\\S-" value)
  4206. (setq ftags
  4207. (append
  4208. ftags
  4209. (apply 'append
  4210. (mapcar (lambda (x) (org-split-string x ":"))
  4211. (org-split-string value)))))))
  4212. ((equal key "DRAWERS")
  4213. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4214. ((equal key "CONSTANTS")
  4215. (setq const (append const (org-split-string value splitre))))
  4216. ((equal key "STARTUP")
  4217. (let ((opts (org-split-string value splitre))
  4218. l var val)
  4219. (while (setq l (pop opts))
  4220. (when (setq l (assoc l org-startup-options))
  4221. (setq var (nth 1 l) val (nth 2 l))
  4222. (if (not (nth 3 l))
  4223. (set (make-local-variable var) val)
  4224. (if (not (listp (symbol-value var)))
  4225. (set (make-local-variable var) nil))
  4226. (set (make-local-variable var) (symbol-value var))
  4227. (add-to-list var val))))))
  4228. ((equal key "ARCHIVE")
  4229. (setq arch value)
  4230. (remove-text-properties 0 (length arch)
  4231. '(face t fontified t) arch))
  4232. ((equal key "OPTIONS")
  4233. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4234. (setq scripts (read (match-string 2 value)))))
  4235. ((equal key "SETUPFILE")
  4236. (setq setup-contents (org-file-contents
  4237. (expand-file-name
  4238. (org-remove-double-quotes value))
  4239. 'noerror))
  4240. (if (not ext-setup-or-nil)
  4241. (setq ext-setup-or-nil setup-contents start 0)
  4242. (setq ext-setup-or-nil
  4243. (concat (substring ext-setup-or-nil 0 start)
  4244. "\n" setup-contents "\n"
  4245. (substring ext-setup-or-nil start)))))))
  4246. ;; search for property blocks
  4247. (goto-char (point-min))
  4248. (while (re-search-forward org-block-regexp nil t)
  4249. (when (equal "PROPERTY" (upcase (match-string 1)))
  4250. (setq value (replace-regexp-in-string
  4251. "[\n\r]" " " (match-string 4)))
  4252. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4253. (setq props (org-update-property-plist (match-string 1 value)
  4254. (match-string 2 value)
  4255. props)))))))
  4256. (org-set-local 'org-use-sub-superscripts scripts)
  4257. (when cat
  4258. (org-set-local 'org-category (intern cat))
  4259. (push (cons "CATEGORY" cat) props))
  4260. (when prio
  4261. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4262. (setq prio (mapcar 'string-to-char prio))
  4263. (org-set-local 'org-highest-priority (nth 0 prio))
  4264. (org-set-local 'org-lowest-priority (nth 1 prio))
  4265. (org-set-local 'org-default-priority (nth 2 prio)))
  4266. (and props (org-set-local 'org-file-properties (nreverse props)))
  4267. (and ftags (org-set-local 'org-file-tags
  4268. (mapcar 'org-add-prop-inherited ftags)))
  4269. (and drawers (org-set-local 'org-drawers drawers))
  4270. (and arch (org-set-local 'org-archive-location arch))
  4271. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4272. ;; Process the TODO keywords
  4273. (unless kwds
  4274. ;; Use the global values as if they had been given locally.
  4275. (setq kwds (default-value 'org-todo-keywords))
  4276. (if (stringp (car kwds))
  4277. (setq kwds (list (cons org-todo-interpretation
  4278. (default-value 'org-todo-keywords)))))
  4279. (setq kwds (reverse kwds)))
  4280. (setq kwds (nreverse kwds))
  4281. (let (inter kws kw)
  4282. (while (setq kws (pop kwds))
  4283. (let ((kws (or
  4284. (run-hook-with-args-until-success
  4285. 'org-todo-setup-filter-hook kws)
  4286. kws)))
  4287. (setq inter (pop kws) sep (member "|" kws)
  4288. kws0 (delete "|" (copy-sequence kws))
  4289. kwsa nil
  4290. kws1 (mapcar
  4291. (lambda (x)
  4292. ;; 1 2
  4293. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4294. (progn
  4295. (setq kw (match-string 1 x)
  4296. key (and (match-end 2) (match-string 2 x))
  4297. log (org-extract-log-state-settings x))
  4298. (push (cons kw (and key (string-to-char key))) kwsa)
  4299. (and log (push log org-todo-log-states))
  4300. kw)
  4301. (error "Invalid TODO keyword %s" x)))
  4302. kws0)
  4303. kwsa (if kwsa (append '((:startgroup))
  4304. (nreverse kwsa)
  4305. '((:endgroup))))
  4306. hw (car kws1)
  4307. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4308. tail (list inter hw (car dws) (org-last dws))))
  4309. (add-to-list 'org-todo-heads hw 'append)
  4310. (push kws1 org-todo-sets)
  4311. (setq org-done-keywords (append org-done-keywords dws nil))
  4312. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4313. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4314. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4315. (setq org-todo-sets (nreverse org-todo-sets)
  4316. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4317. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4318. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4319. ;; Process the constants
  4320. (when const
  4321. (let (e cst)
  4322. (while (setq e (pop const))
  4323. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4324. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4325. (setq org-table-formula-constants-local cst)))
  4326. ;; Process the tags.
  4327. (when tags
  4328. (let (e tgs)
  4329. (while (setq e (pop tags))
  4330. (cond
  4331. ((equal e "{") (push '(:startgroup) tgs))
  4332. ((equal e "}") (push '(:endgroup) tgs))
  4333. ((equal e "\\n") (push '(:newline) tgs))
  4334. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4335. (push (cons (match-string 1 e)
  4336. (string-to-char (match-string 2 e)))
  4337. tgs))
  4338. (t (push (list e) tgs))))
  4339. (org-set-local 'org-tag-alist nil)
  4340. (while (setq e (pop tgs))
  4341. (or (and (stringp (car e))
  4342. (assoc (car e) org-tag-alist))
  4343. (push e org-tag-alist)))))
  4344. ;; Compute the regular expressions and other local variables.
  4345. ;; Using `org-outline-regexp-bol' would complicate them much,
  4346. ;; because of the fixed white space at the end of that string.
  4347. (if (not org-done-keywords)
  4348. (setq org-done-keywords (and org-todo-keywords-1
  4349. (list (org-last org-todo-keywords-1)))))
  4350. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4351. (length org-scheduled-string)
  4352. (length org-clock-string)
  4353. (length org-closed-string)))
  4354. org-drawer-regexp
  4355. (concat "^[ \t]*:\\("
  4356. (mapconcat 'regexp-quote org-drawers "\\|")
  4357. "\\):[ \t]*$")
  4358. org-not-done-keywords
  4359. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4360. org-todo-regexp
  4361. (concat "\\("
  4362. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4363. "\\)")
  4364. org-not-done-regexp
  4365. (concat "\\("
  4366. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4367. "\\)")
  4368. org-not-done-heading-regexp
  4369. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4370. org-todo-line-regexp
  4371. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4372. org-complex-heading-regexp
  4373. (concat "^\\(\\*+\\)"
  4374. "\\(?: +" org-todo-regexp "\\)?"
  4375. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4376. "\\(?: +\\(.*?\\)\\)??"
  4377. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4378. "[ \t]*$")
  4379. org-complex-heading-regexp-format
  4380. (concat "^\\(\\*+\\)"
  4381. "\\(?: +" org-todo-regexp "\\)?"
  4382. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4383. "\\(?: +"
  4384. ;; Stats cookies can be stuck to body.
  4385. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4386. "\\(%s\\)"
  4387. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4388. "\\)"
  4389. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4390. "[ \t]*$")
  4391. org-todo-line-tags-regexp
  4392. (concat "^\\(\\*+\\)"
  4393. "\\(?: +" org-todo-regexp "\\)?"
  4394. "\\(?: +\\(.*?\\)\\)??"
  4395. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4396. "[ \t]*$")
  4397. org-deadline-regexp (concat "\\<" org-deadline-string)
  4398. org-deadline-time-regexp
  4399. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4400. org-deadline-line-regexp
  4401. (concat "\\<\\(" org-deadline-string "\\).*")
  4402. org-scheduled-regexp
  4403. (concat "\\<" org-scheduled-string)
  4404. org-scheduled-time-regexp
  4405. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4406. org-closed-time-regexp
  4407. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4408. org-keyword-time-regexp
  4409. (concat "\\<\\(" org-scheduled-string
  4410. "\\|" org-deadline-string
  4411. "\\|" org-closed-string
  4412. "\\|" org-clock-string "\\)"
  4413. " *[[<]\\([^]>]+\\)[]>]")
  4414. org-keyword-time-not-clock-regexp
  4415. (concat "\\<\\(" org-scheduled-string
  4416. "\\|" org-deadline-string
  4417. "\\|" org-closed-string
  4418. "\\)"
  4419. " *[[<]\\([^]>]+\\)[]>]")
  4420. org-maybe-keyword-time-regexp
  4421. (concat "\\(\\<\\(" org-scheduled-string
  4422. "\\|" org-deadline-string
  4423. "\\|" org-closed-string
  4424. "\\|" org-clock-string "\\)\\)?"
  4425. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4426. org-all-time-keywords
  4427. (mapcar (lambda (w) (substring w 0 -1))
  4428. (list org-scheduled-string org-deadline-string
  4429. org-clock-string org-closed-string)))
  4430. (org-compute-latex-and-related-regexp)
  4431. (org-set-font-lock-defaults))))
  4432. (defun org-file-contents (file &optional noerror)
  4433. "Return the contents of FILE, as a string."
  4434. (if (or (not file)
  4435. (not (file-readable-p file)))
  4436. (if noerror
  4437. (progn
  4438. (message "Cannot read file \"%s\"" file)
  4439. (ding) (sit-for 2)
  4440. "")
  4441. (error "Cannot read file \"%s\"" file))
  4442. (with-temp-buffer
  4443. (insert-file-contents file)
  4444. (buffer-string))))
  4445. (defun org-extract-log-state-settings (x)
  4446. "Extract the log state setting from a TODO keyword string.
  4447. This will extract info from a string like \"WAIT(w@/!)\"."
  4448. (let (kw key log1 log2)
  4449. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4450. (setq kw (match-string 1 x)
  4451. key (and (match-end 2) (match-string 2 x))
  4452. log1 (and (match-end 3) (match-string 3 x))
  4453. log2 (and (match-end 4) (match-string 4 x)))
  4454. (and (or log1 log2)
  4455. (list kw
  4456. (and log1 (if (equal log1 "!") 'time 'note))
  4457. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4458. (defun org-remove-keyword-keys (list)
  4459. "Remove a pair of parenthesis at the end of each string in LIST."
  4460. (mapcar (lambda (x)
  4461. (if (string-match "(.*)$" x)
  4462. (substring x 0 (match-beginning 0))
  4463. x))
  4464. list))
  4465. (defun org-assign-fast-keys (alist)
  4466. "Assign fast keys to a keyword-key alist.
  4467. Respect keys that are already there."
  4468. (let (new e (alt ?0))
  4469. (while (setq e (pop alist))
  4470. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4471. (cdr e)) ;; Key already assigned.
  4472. (push e new)
  4473. (let ((clist (string-to-list (downcase (car e))))
  4474. (used (append new alist)))
  4475. (when (= (car clist) ?@)
  4476. (pop clist))
  4477. (while (and clist (rassoc (car clist) used))
  4478. (pop clist))
  4479. (unless clist
  4480. (while (rassoc alt used)
  4481. (incf alt)))
  4482. (push (cons (car e) (or (car clist) alt)) new))))
  4483. (nreverse new)))
  4484. ;;; Some variables used in various places
  4485. (defvar org-window-configuration nil
  4486. "Used in various places to store a window configuration.")
  4487. (defvar org-selected-window nil
  4488. "Used in various places to store a window configuration.")
  4489. (defvar org-finish-function nil
  4490. "Function to be called when `C-c C-c' is used.
  4491. This is for getting out of special buffers like capture.")
  4492. ;; FIXME: Occasionally check by commenting these, to make sure
  4493. ;; no other functions uses these, forgetting to let-bind them.
  4494. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4495. (defvar org-last-state)
  4496. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4497. ;; Defined somewhere in this file, but used before definition.
  4498. (defvar org-entities) ;; defined in org-entities.el
  4499. (defvar org-struct-menu)
  4500. (defvar org-org-menu)
  4501. (defvar org-tbl-menu)
  4502. ;;;; Define the Org-mode
  4503. ;; We use a before-change function to check if a table might need
  4504. ;; an update.
  4505. (defvar org-table-may-need-update t
  4506. "Indicates that a table might need an update.
  4507. This variable is set by `org-before-change-function'.
  4508. `org-table-align' sets it back to nil.")
  4509. (defun org-before-change-function (beg end)
  4510. "Every change indicates that a table might need an update."
  4511. (setq org-table-may-need-update t))
  4512. (defvar org-mode-map)
  4513. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4514. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4515. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4516. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4517. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4518. (defvar org-table-buffer-is-an nil)
  4519. (defvar bidi-paragraph-direction)
  4520. (defvar buffer-face-mode-face)
  4521. (require 'outline)
  4522. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4523. (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"))
  4524. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4525. ;; Other stuff we need.
  4526. (require 'time-date)
  4527. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4528. (require 'easymenu)
  4529. (require 'overlay)
  4530. ;; (require 'org-macs) moved higher up in the file before it is first used
  4531. (require 'org-entities)
  4532. ;; (require 'org-compat) moved higher up in the file before it is first used
  4533. (require 'org-faces)
  4534. (require 'org-list)
  4535. (require 'org-pcomplete)
  4536. (require 'org-src)
  4537. (require 'org-footnote)
  4538. (require 'org-macro)
  4539. ;; babel
  4540. (require 'ob)
  4541. ;;;###autoload
  4542. (define-derived-mode org-mode outline-mode "Org"
  4543. "Outline-based notes management and organizer, alias
  4544. \"Carsten's outline-mode for keeping track of everything.\"
  4545. Org-mode develops organizational tasks around a NOTES file which
  4546. contains information about projects as plain text. Org-mode is
  4547. implemented on top of outline-mode, which is ideal to keep the content
  4548. of large files well structured. It supports ToDo items, deadlines and
  4549. time stamps, which magically appear in the diary listing of the Emacs
  4550. calendar. Tables are easily created with a built-in table editor.
  4551. Plain text URL-like links connect to websites, emails (VM), Usenet
  4552. messages (Gnus), BBDB entries, and any files related to the project.
  4553. For printing and sharing of notes, an Org-mode file (or a part of it)
  4554. can be exported as a structured ASCII or HTML file.
  4555. The following commands are available:
  4556. \\{org-mode-map}"
  4557. ;; Get rid of Outline menus, they are not needed
  4558. ;; Need to do this here because define-derived-mode sets up
  4559. ;; the keymap so late. Still, it is a waste to call this each time
  4560. ;; we switch another buffer into org-mode.
  4561. (if (featurep 'xemacs)
  4562. (when (boundp 'outline-mode-menu-heading)
  4563. ;; Assume this is Greg's port, it uses easymenu
  4564. (easy-menu-remove outline-mode-menu-heading)
  4565. (easy-menu-remove outline-mode-menu-show)
  4566. (easy-menu-remove outline-mode-menu-hide))
  4567. (define-key org-mode-map [menu-bar headings] 'undefined)
  4568. (define-key org-mode-map [menu-bar hide] 'undefined)
  4569. (define-key org-mode-map [menu-bar show] 'undefined))
  4570. (org-load-modules-maybe)
  4571. (easy-menu-add org-org-menu)
  4572. (easy-menu-add org-tbl-menu)
  4573. (org-install-agenda-files-menu)
  4574. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4575. (add-to-invisibility-spec '(org-cwidth))
  4576. (add-to-invisibility-spec '(org-hide-block . t))
  4577. (when (featurep 'xemacs)
  4578. (org-set-local 'line-move-ignore-invisible t))
  4579. (org-set-local 'outline-regexp org-outline-regexp)
  4580. (org-set-local 'outline-level 'org-outline-level)
  4581. (setq bidi-paragraph-direction 'left-to-right)
  4582. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4583. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4584. (when (and org-ellipsis
  4585. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4586. (fboundp 'make-glyph-code))
  4587. (unless org-display-table
  4588. (setq org-display-table (make-display-table)))
  4589. (set-display-table-slot
  4590. org-display-table 4
  4591. (vconcat (mapcar
  4592. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4593. org-ellipsis)))
  4594. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4595. (setq buffer-display-table org-display-table))
  4596. (org-set-regexps-and-options)
  4597. (when (and org-tag-faces (not org-tags-special-faces-re))
  4598. ;; tag faces set outside customize.... force initialization.
  4599. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4600. ;; Calc embedded
  4601. (org-set-local 'calc-embedded-open-mode "# ")
  4602. ;; Modify a few syntax entries
  4603. (modify-syntax-entry ?@ "w")
  4604. (modify-syntax-entry ?\" "\"")
  4605. (if org-startup-truncated (setq truncate-lines t))
  4606. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4607. (org-set-local 'font-lock-unfontify-region-function
  4608. 'org-unfontify-region)
  4609. ;; Activate before-change-function
  4610. (org-set-local 'org-table-may-need-update t)
  4611. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4612. 'local)
  4613. ;; Check for running clock before killing a buffer
  4614. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4615. ;; Initialize macros templates.
  4616. (org-macro-initialize-templates)
  4617. ;; Initialize radio targets.
  4618. (org-update-radio-target-regexp)
  4619. ;; Indentation.
  4620. (org-set-local 'indent-line-function 'org-indent-line)
  4621. (org-set-local 'indent-region-function 'org-indent-region)
  4622. ;; Filling and auto-filling.
  4623. (org-setup-filling)
  4624. ;; Comments.
  4625. (org-setup-comments-handling)
  4626. ;; Beginning/end of defun
  4627. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4628. (org-set-local 'end-of-defun-function 'org-forward-element)
  4629. ;; Next error for sparse trees
  4630. (org-set-local 'next-error-function 'org-occur-next-match)
  4631. ;; Make sure dependence stuff works reliably, even for users who set it
  4632. ;; too late :-(
  4633. (if org-enforce-todo-dependencies
  4634. (add-hook 'org-blocker-hook
  4635. 'org-block-todo-from-children-or-siblings-or-parent)
  4636. (remove-hook 'org-blocker-hook
  4637. 'org-block-todo-from-children-or-siblings-or-parent))
  4638. (if org-enforce-todo-checkbox-dependencies
  4639. (add-hook 'org-blocker-hook
  4640. 'org-block-todo-from-checkboxes)
  4641. (remove-hook 'org-blocker-hook
  4642. 'org-block-todo-from-checkboxes))
  4643. ;; Align options lines
  4644. (org-set-local
  4645. 'align-mode-rules-list
  4646. '((org-in-buffer-settings
  4647. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4648. (modes . '(org-mode)))))
  4649. ;; Imenu
  4650. (org-set-local 'imenu-create-index-function
  4651. 'org-imenu-get-tree)
  4652. ;; Make isearch reveal context
  4653. (if (or (featurep 'xemacs)
  4654. (not (boundp 'outline-isearch-open-invisible-function)))
  4655. ;; Emacs 21 and XEmacs make use of the hook
  4656. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4657. ;; Emacs 22 deals with this through a special variable
  4658. (org-set-local 'outline-isearch-open-invisible-function
  4659. (lambda (&rest ignore) (org-show-context 'isearch)))
  4660. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4661. ;; Setup the pcomplete hooks
  4662. (set (make-local-variable 'pcomplete-command-completion-function)
  4663. 'org-pcomplete-initial)
  4664. (set (make-local-variable 'pcomplete-command-name-function)
  4665. 'org-command-at-point)
  4666. (set (make-local-variable 'pcomplete-default-completion-function)
  4667. 'ignore)
  4668. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4669. 'org-parse-arguments)
  4670. (set (make-local-variable 'pcomplete-termination-string) "")
  4671. (when (>= emacs-major-version 23)
  4672. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4673. ;; If empty file that did not turn on org-mode automatically, make it to.
  4674. (if (and org-insert-mode-line-in-empty-file
  4675. (org-called-interactively-p 'any)
  4676. (= (point-min) (point-max)))
  4677. (insert "# -*- mode: org -*-\n\n"))
  4678. (unless org-inhibit-startup
  4679. (org-unmodified
  4680. (and org-startup-with-beamer-mode (org-beamer-mode))
  4681. (when org-startup-align-all-tables
  4682. (org-table-map-tables 'org-table-align 'quietly))
  4683. (when org-startup-with-inline-images
  4684. (org-display-inline-images))
  4685. (when org-startup-with-latex-preview
  4686. (org-preview-latex-fragment))
  4687. (unless org-inhibit-startup-visibility-stuff
  4688. (org-set-startup-visibility))))
  4689. ;; Try to set org-hide correctly
  4690. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4691. ;; Update `customize-package-emacs-version-alist'
  4692. (add-to-list 'customize-package-emacs-version-alist
  4693. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4694. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4695. ("8.0" . "24.4")))
  4696. (defvar org-mode-transpose-word-syntax-table
  4697. (let ((st (make-syntax-table)))
  4698. (mapc (lambda(c) (modify-syntax-entry
  4699. (string-to-char (car c)) "w p" st))
  4700. org-emphasis-alist)
  4701. st))
  4702. (when (fboundp 'abbrev-table-put)
  4703. (abbrev-table-put org-mode-abbrev-table
  4704. :parents (list text-mode-abbrev-table)))
  4705. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4706. (defsubst org-fix-ellipsis-at-bol ()
  4707. (save-excursion (goto-char (window-start)) (recenter 0)))
  4708. (defun org-find-invisible-foreground ()
  4709. (let ((candidates (remove
  4710. "unspecified-bg"
  4711. (nconc
  4712. (list (face-background 'default)
  4713. (face-background 'org-default))
  4714. (mapcar
  4715. (lambda (alist)
  4716. (when (boundp alist)
  4717. (cdr (assoc 'background-color (symbol-value alist)))))
  4718. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4719. (list (face-foreground 'org-hide))))))
  4720. (car (remove nil candidates))))
  4721. (defun org-current-time (&optional rounding-minutes past)
  4722. "Current time, possibly rounded to ROUNDING-MINUTES.
  4723. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4724. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4725. the rounding returns a past time."
  4726. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4727. (car org-time-stamp-rounding-minutes)))
  4728. (time (decode-time)) res)
  4729. (if (< r 1)
  4730. (current-time)
  4731. (setq res
  4732. (apply 'encode-time
  4733. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4734. (nthcdr 2 time))))
  4735. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4736. (seconds-to-time (- (org-float-time res) (* r 60)))
  4737. res))))
  4738. (defun org-today ()
  4739. "Return today date, considering `org-extend-today-until'."
  4740. (time-to-days
  4741. (time-subtract (current-time)
  4742. (list 0 (* 3600 org-extend-today-until) 0))))
  4743. ;;;; Font-Lock stuff, including the activators
  4744. (defvar org-mouse-map (make-sparse-keymap))
  4745. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4746. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4747. (when org-mouse-1-follows-link
  4748. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4749. (when org-tab-follows-link
  4750. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4751. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4752. (require 'font-lock)
  4753. (defconst org-non-link-chars "]\t\n\r<>")
  4754. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4755. "shell" "elisp" "doi" "message"))
  4756. (defvar org-link-types-re nil
  4757. "Matches a link that has a url-like prefix like \"http:\"")
  4758. (defvar org-link-re-with-space nil
  4759. "Matches a link with spaces, optional angular brackets around it.")
  4760. (defvar org-link-re-with-space2 nil
  4761. "Matches a link with spaces, optional angular brackets around it.")
  4762. (defvar org-link-re-with-space3 nil
  4763. "Matches a link with spaces, only for internal part in bracket links.")
  4764. (defvar org-angle-link-re nil
  4765. "Matches link with angular brackets, spaces are allowed.")
  4766. (defvar org-plain-link-re nil
  4767. "Matches plain link, without spaces.")
  4768. (defvar org-bracket-link-regexp nil
  4769. "Matches a link in double brackets.")
  4770. (defvar org-bracket-link-analytic-regexp nil
  4771. "Regular expression used to analyze links.
  4772. Here is what the match groups contain after a match:
  4773. 1: http:
  4774. 2: http
  4775. 3: path
  4776. 4: [desc]
  4777. 5: desc")
  4778. (defvar org-bracket-link-analytic-regexp++ nil
  4779. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4780. (defvar org-any-link-re nil
  4781. "Regular expression matching any link.")
  4782. (defconst org-match-sexp-depth 3
  4783. "Number of stacked braces for sub/superscript matching.")
  4784. (defun org-create-multibrace-regexp (left right n)
  4785. "Create a regular expression which will match a balanced sexp.
  4786. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4787. as single character strings.
  4788. The regexp returned will match the entire expression including the
  4789. delimiters. It will also define a single group which contains the
  4790. match except for the outermost delimiters. The maximum depth of
  4791. stacked delimiters is N. Escaping delimiters is not possible."
  4792. (let* ((nothing (concat "[^" left right "]*?"))
  4793. (or "\\|")
  4794. (re nothing)
  4795. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4796. (while (> n 1)
  4797. (setq n (1- n)
  4798. re (concat re or next)
  4799. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4800. (concat left "\\(" re "\\)" right)))
  4801. (defvar org-match-substring-regexp
  4802. (concat
  4803. "\\(\\S-\\)\\([_^]\\)\\("
  4804. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4805. "\\|"
  4806. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4807. "\\|"
  4808. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4809. "The regular expression matching a sub- or superscript.")
  4810. (defvar org-match-substring-with-braces-regexp
  4811. (concat
  4812. "\\(\\S-\\)\\([_^]\\)\\("
  4813. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4814. "\\)")
  4815. "The regular expression matching a sub- or superscript, forcing braces.")
  4816. (defun org-make-link-regexps ()
  4817. "Update the link regular expressions.
  4818. This should be called after the variable `org-link-types' has changed."
  4819. (setq org-link-types-re
  4820. (concat
  4821. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4822. org-link-re-with-space
  4823. (concat
  4824. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4825. "\\([^" org-non-link-chars " ]"
  4826. "[^" org-non-link-chars "]*"
  4827. "[^" org-non-link-chars " ]\\)>?")
  4828. org-link-re-with-space2
  4829. (concat
  4830. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4831. "\\([^" org-non-link-chars " ]"
  4832. "[^\t\n\r]*"
  4833. "[^" org-non-link-chars " ]\\)>?")
  4834. org-link-re-with-space3
  4835. (concat
  4836. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4837. "\\([^" org-non-link-chars " ]"
  4838. "[^\t\n\r]*\\)")
  4839. org-angle-link-re
  4840. (concat
  4841. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4842. "\\([^" org-non-link-chars " ]"
  4843. "[^" org-non-link-chars "]*"
  4844. "\\)>")
  4845. org-plain-link-re
  4846. (concat
  4847. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4848. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4849. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4850. org-bracket-link-regexp
  4851. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4852. org-bracket-link-analytic-regexp
  4853. (concat
  4854. "\\[\\["
  4855. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4856. "\\([^]]+\\)"
  4857. "\\]"
  4858. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4859. "\\]")
  4860. org-bracket-link-analytic-regexp++
  4861. (concat
  4862. "\\[\\["
  4863. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4864. "\\([^]]+\\)"
  4865. "\\]"
  4866. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4867. "\\]")
  4868. org-any-link-re
  4869. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4870. org-angle-link-re "\\)\\|\\("
  4871. org-plain-link-re "\\)")))
  4872. (org-make-link-regexps)
  4873. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4874. "Regular expression for fast time stamp matching.")
  4875. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4876. "Regular expression for fast time stamp matching.")
  4877. (defconst org-ts-regexp0
  4878. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4879. "Regular expression matching time strings for analysis.
  4880. This one does not require the space after the date, so it can be used
  4881. on a string that terminates immediately after the date.")
  4882. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4883. "Regular expression matching time strings for analysis.")
  4884. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4885. "Regular expression matching time stamps, with groups.")
  4886. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4887. "Regular expression matching time stamps (also [..]), with groups.")
  4888. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4889. "Regular expression matching a time stamp range.")
  4890. (defconst org-tr-regexp-both
  4891. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4892. "Regular expression matching a time stamp range.")
  4893. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4894. org-ts-regexp "\\)?")
  4895. "Regular expression matching a time stamp or time stamp range.")
  4896. (defconst org-tsr-regexp-both
  4897. (concat org-ts-regexp-both "\\(--?-?"
  4898. org-ts-regexp-both "\\)?")
  4899. "Regular expression matching a time stamp or time stamp range.
  4900. The time stamps may be either active or inactive.")
  4901. (defvar org-emph-face nil)
  4902. (defun org-do-emphasis-faces (limit)
  4903. "Run through the buffer and add overlays to emphasized strings."
  4904. (let (rtn a)
  4905. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4906. (if (not (= (char-after (match-beginning 3))
  4907. (char-after (match-beginning 4))))
  4908. (progn
  4909. (setq rtn t)
  4910. (setq a (assoc (match-string 3) org-emphasis-alist))
  4911. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4912. 'face
  4913. (nth 1 a))
  4914. (and (nth 4 a)
  4915. (org-remove-flyspell-overlays-in
  4916. (match-beginning 0) (match-end 0)))
  4917. (add-text-properties (match-beginning 2) (match-end 2)
  4918. '(font-lock-multiline t org-emphasis t))
  4919. (when org-hide-emphasis-markers
  4920. (add-text-properties (match-end 4) (match-beginning 5)
  4921. '(invisible org-link))
  4922. (add-text-properties (match-beginning 3) (match-end 3)
  4923. '(invisible org-link)))))
  4924. (backward-char 1))
  4925. rtn))
  4926. (defun org-emphasize (&optional char)
  4927. "Insert or change an emphasis, i.e. a font like bold or italic.
  4928. If there is an active region, change that region to a new emphasis.
  4929. If there is no region, just insert the marker characters and position
  4930. the cursor between them.
  4931. CHAR should be either the marker character, or the first character of the
  4932. HTML tag associated with that emphasis. If CHAR is a space, the means
  4933. to remove the emphasis of the selected region.
  4934. If char is not given (for example in an interactive call) it
  4935. will be prompted for."
  4936. (interactive)
  4937. (let ((eal org-emphasis-alist) e det
  4938. (erc org-emphasis-regexp-components)
  4939. (prompt "")
  4940. (string "") beg end move tag c s)
  4941. (if (org-region-active-p)
  4942. (setq beg (region-beginning) end (region-end)
  4943. string (buffer-substring beg end))
  4944. (setq move t))
  4945. (while (setq e (pop eal))
  4946. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4947. c (aref tag 0))
  4948. (push (cons c (string-to-char (car e))) det)
  4949. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4950. (substring tag 1)))))
  4951. (setq det (nreverse det))
  4952. (unless char
  4953. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4954. (setq char (read-char-exclusive)))
  4955. (setq char (or (cdr (assoc char det)) char))
  4956. (if (equal char ?\ )
  4957. (setq s "" move nil)
  4958. (unless (assoc (char-to-string char) org-emphasis-alist)
  4959. (error "No such emphasis marker: \"%c\"" char))
  4960. (setq s (char-to-string char)))
  4961. (while (and (> (length string) 1)
  4962. (equal (substring string 0 1) (substring string -1))
  4963. (assoc (substring string 0 1) org-emphasis-alist))
  4964. (setq string (substring string 1 -1)))
  4965. (setq string (concat s string s))
  4966. (if beg (delete-region beg end))
  4967. (unless (or (bolp)
  4968. (string-match (concat "[" (nth 0 erc) "\n]")
  4969. (char-to-string (char-before (point)))))
  4970. (insert " "))
  4971. (unless (or (eobp)
  4972. (string-match (concat "[" (nth 1 erc) "\n]")
  4973. (char-to-string (char-after (point)))))
  4974. (insert " ") (backward-char 1))
  4975. (insert string)
  4976. (and move (backward-char 1))))
  4977. (defconst org-nonsticky-props
  4978. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4979. (defsubst org-rear-nonsticky-at (pos)
  4980. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4981. (defun org-activate-plain-links (limit)
  4982. "Run through the buffer and add overlays to links."
  4983. (let (f hl)
  4984. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4985. (not (org-in-src-block-p)))
  4986. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4987. (setq f (get-text-property (match-beginning 0) 'face))
  4988. (setq hl (org-match-string-no-properties 0))
  4989. (if (or (eq f 'org-tag)
  4990. (and (listp f) (memq 'org-tag f)))
  4991. nil
  4992. (add-text-properties (match-beginning 0) (match-end 0)
  4993. (list 'mouse-face 'highlight
  4994. 'face 'org-link
  4995. 'htmlize-link `(:uri ,hl)
  4996. 'keymap org-mouse-map))
  4997. (org-rear-nonsticky-at (match-end 0)))
  4998. t)))
  4999. (defun org-activate-code (limit)
  5000. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5001. (progn
  5002. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5003. (remove-text-properties (match-beginning 0) (match-end 0)
  5004. '(display t invisible t intangible t))
  5005. t)))
  5006. (defcustom org-src-fontify-natively nil
  5007. "When non-nil, fontify code in code blocks."
  5008. :type 'boolean
  5009. :version "24.1"
  5010. :group 'org-appearance
  5011. :group 'org-babel)
  5012. (defcustom org-allow-promoting-top-level-subtree nil
  5013. "When non-nil, allow promoting a top level subtree.
  5014. The leading star of the top level headline will be replaced
  5015. by a #."
  5016. :type 'boolean
  5017. :version "24.1"
  5018. :group 'org-appearance)
  5019. (defun org-fontify-meta-lines-and-blocks (limit)
  5020. (condition-case nil
  5021. (org-fontify-meta-lines-and-blocks-1 limit)
  5022. (error (message "org-mode fontification error"))))
  5023. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5024. "Fontify #+ lines and blocks, in the correct ways."
  5025. (let ((case-fold-search t))
  5026. (if (re-search-forward
  5027. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5028. limit t)
  5029. (let ((beg (match-beginning 0))
  5030. (block-start (match-end 0))
  5031. (block-end nil)
  5032. (lang (match-string 7))
  5033. (beg1 (line-beginning-position 2))
  5034. (dc1 (downcase (match-string 2)))
  5035. (dc3 (downcase (match-string 3)))
  5036. end end1 quoting block-type ovl)
  5037. (cond
  5038. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5039. ;; a single line of backend-specific content
  5040. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5041. (remove-text-properties (match-beginning 0) (match-end 0)
  5042. '(display t invisible t intangible t))
  5043. (add-text-properties (match-beginning 1) (match-end 3)
  5044. '(font-lock-fontified t face org-meta-line))
  5045. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5046. '(font-lock-fontified t face org-block))
  5047. ; for backend-specific code
  5048. t)
  5049. ((and (match-end 4) (equal dc3 "+begin"))
  5050. ;; Truly a block
  5051. (setq block-type (downcase (match-string 5))
  5052. quoting (member block-type org-protecting-blocks))
  5053. (when (re-search-forward
  5054. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5055. nil t) ;; on purpose, we look further than LIMIT
  5056. (setq end (min (point-max) (match-end 0))
  5057. end1 (min (point-max) (1- (match-beginning 0))))
  5058. (setq block-end (match-beginning 0))
  5059. (when quoting
  5060. (remove-text-properties beg end
  5061. '(display t invisible t intangible t)))
  5062. (add-text-properties
  5063. beg end
  5064. '(font-lock-fontified t font-lock-multiline t))
  5065. (add-text-properties beg beg1 '(face org-meta-line))
  5066. (add-text-properties end1 (min (point-max) (1+ end))
  5067. '(face org-meta-line)) ; for end_src
  5068. (cond
  5069. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5070. (org-src-font-lock-fontify-block lang block-start block-end)
  5071. ;; remove old background overlays
  5072. (mapc (lambda (ov)
  5073. (if (eq (overlay-get ov 'face) 'org-block-background)
  5074. (delete-overlay ov)))
  5075. (overlays-at (/ (+ beg1 block-end) 2)))
  5076. ;; add a background overlay
  5077. (setq ovl (make-overlay beg1 block-end))
  5078. (overlay-put ovl 'face 'org-block-background)
  5079. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5080. (quoting
  5081. (add-text-properties beg1 (min (point-max) (1+ end1))
  5082. '(face org-block))) ; end of source block
  5083. ((not org-fontify-quote-and-verse-blocks))
  5084. ((string= block-type "quote")
  5085. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5086. ((string= block-type "verse")
  5087. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5088. (add-text-properties beg beg1 '(face org-block-begin-line))
  5089. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5090. '(face org-block-end-line))
  5091. t))
  5092. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5093. (add-text-properties
  5094. beg (match-end 3)
  5095. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5096. '(font-lock-fontified t invisible t)
  5097. '(font-lock-fontified t face org-document-info-keyword)))
  5098. (add-text-properties
  5099. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5100. (if (string-equal dc1 "+title:")
  5101. '(font-lock-fontified t face org-document-title)
  5102. '(font-lock-fontified t face org-document-info))))
  5103. ((or (equal dc1 "+results")
  5104. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5105. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5106. "+call:" "+header:" "+headers:" "+name:"))
  5107. (and (match-end 4) (equal dc3 "+attr")))
  5108. (add-text-properties
  5109. beg (match-end 0)
  5110. '(font-lock-fontified t face org-meta-line))
  5111. t)
  5112. ((member dc3 '(" " ""))
  5113. (add-text-properties
  5114. beg (match-end 0)
  5115. '(font-lock-fontified t face font-lock-comment-face)))
  5116. ((not (member (char-after beg) '(?\ ?\t)))
  5117. ;; just any other in-buffer setting, but not indented
  5118. (add-text-properties
  5119. beg (match-end 0)
  5120. '(font-lock-fontified t face org-meta-line))
  5121. t)
  5122. (t nil))))))
  5123. (defun org-activate-angle-links (limit)
  5124. "Run through the buffer and add overlays to links."
  5125. (if (and (re-search-forward org-angle-link-re limit t)
  5126. (not (org-in-src-block-p)))
  5127. (progn
  5128. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5129. (add-text-properties (match-beginning 0) (match-end 0)
  5130. (list 'mouse-face 'highlight
  5131. 'keymap org-mouse-map))
  5132. (org-rear-nonsticky-at (match-end 0))
  5133. t)))
  5134. (defun org-activate-footnote-links (limit)
  5135. "Run through the buffer and add overlays to footnotes."
  5136. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5137. (when fn
  5138. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5139. (org-remove-flyspell-overlays-in beg end)
  5140. (add-text-properties beg end
  5141. (list 'mouse-face 'highlight
  5142. 'keymap org-mouse-map
  5143. 'help-echo
  5144. (if (= (point-at-bol) beg)
  5145. "Footnote definition"
  5146. "Footnote reference")
  5147. 'font-lock-fontified t
  5148. 'font-lock-multiline t
  5149. 'face 'org-footnote))))))
  5150. (defun org-activate-bracket-links (limit)
  5151. "Run through the buffer and add overlays to bracketed links."
  5152. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5153. (not (org-in-src-block-p)))
  5154. (let* ((hl (org-match-string-no-properties 1))
  5155. (help (concat "LINK: " hl))
  5156. ;; FIXME: Above we should remove the escapes. But that
  5157. ;; requires another match, protecting match data, a lot
  5158. ;; of overhead for font-lock.
  5159. (ip (org-maybe-intangible
  5160. (list 'invisible 'org-link
  5161. 'keymap org-mouse-map 'mouse-face 'highlight
  5162. 'font-lock-multiline t 'help-echo help
  5163. 'htmlize-link `(:uri ,hl))))
  5164. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5165. 'font-lock-multiline t 'help-echo help
  5166. 'htmlize-link `(:uri ,hl))))
  5167. ;; We need to remove the invisible property here. Table narrowing
  5168. ;; may have made some of this invisible.
  5169. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5170. (remove-text-properties (match-beginning 0) (match-end 0)
  5171. '(invisible nil))
  5172. (if (match-end 3)
  5173. (progn
  5174. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5175. (org-rear-nonsticky-at (match-beginning 3))
  5176. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5177. (org-rear-nonsticky-at (match-end 3))
  5178. (add-text-properties (match-end 3) (match-end 0) ip)
  5179. (org-rear-nonsticky-at (match-end 0)))
  5180. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5181. (org-rear-nonsticky-at (match-beginning 1))
  5182. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5183. (org-rear-nonsticky-at (match-end 1))
  5184. (add-text-properties (match-end 1) (match-end 0) ip)
  5185. (org-rear-nonsticky-at (match-end 0)))
  5186. t)))
  5187. (defun org-activate-dates (limit)
  5188. "Run through the buffer and add overlays to dates."
  5189. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5190. (not (equal (char-before (match-beginning 0)) 91)))
  5191. (progn
  5192. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5193. (add-text-properties (match-beginning 0) (match-end 0)
  5194. (list 'mouse-face 'highlight
  5195. 'keymap org-mouse-map))
  5196. (org-rear-nonsticky-at (match-end 0))
  5197. (when org-display-custom-times
  5198. (if (match-end 3)
  5199. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5200. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5201. t)))
  5202. (defvar org-target-link-regexp nil
  5203. "Regular expression matching radio targets in plain text.")
  5204. (make-variable-buffer-local 'org-target-link-regexp)
  5205. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5206. "Regular expression matching a link target.")
  5207. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5208. "Regular expression matching a radio target.")
  5209. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5210. "Regular expression matching any target.")
  5211. (defun org-activate-target-links (limit)
  5212. "Run through the buffer and add overlays to target matches."
  5213. (when org-target-link-regexp
  5214. (let ((case-fold-search t))
  5215. (if (re-search-forward org-target-link-regexp limit t)
  5216. (progn
  5217. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5218. (add-text-properties (match-beginning 0) (match-end 0)
  5219. (list 'mouse-face 'highlight
  5220. 'keymap org-mouse-map
  5221. 'help-echo "Radio target link"
  5222. 'org-linked-text t))
  5223. (org-rear-nonsticky-at (match-end 0))
  5224. t)))))
  5225. (defun org-update-radio-target-regexp ()
  5226. "Find all radio targets in this file and update the regular expression."
  5227. (interactive)
  5228. (when (memq 'radio org-activate-links)
  5229. (setq org-target-link-regexp
  5230. (org-make-target-link-regexp (org-all-targets 'radio)))
  5231. (org-restart-font-lock)))
  5232. (defun org-hide-wide-columns (limit)
  5233. (let (s e)
  5234. (setq s (text-property-any (point) (or limit (point-max))
  5235. 'org-cwidth t))
  5236. (when s
  5237. (setq e (next-single-property-change s 'org-cwidth))
  5238. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5239. (goto-char e)
  5240. t)))
  5241. (defvar org-latex-and-related-regexp nil
  5242. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5243. (defvar org-match-substring-regexp)
  5244. (defvar org-match-substring-with-braces-regexp)
  5245. (defun org-compute-latex-and-related-regexp ()
  5246. "Compute regular expression for LaTeX, entities and sub/superscript.
  5247. Result depends on variable `org-highlight-latex-and-related'."
  5248. (org-set-local
  5249. 'org-latex-and-related-regexp
  5250. (let* ((re-sub
  5251. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5252. ((eq org-use-sub-superscripts '{})
  5253. (list org-match-substring-with-braces-regexp))
  5254. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5255. (re-latex
  5256. (when (memq 'latex org-highlight-latex-and-related)
  5257. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5258. (delq nil
  5259. (mapcar (lambda (x)
  5260. (and (member (car x) matchers) (nth 1 x)))
  5261. org-latex-regexps)))))
  5262. (re-entities
  5263. (when (memq 'entities org-highlight-latex-and-related)
  5264. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5265. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5266. (defun org-do-latex-and-related (limit)
  5267. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5268. LIMIT bounds the search for syntax to highlight. Stop at first
  5269. highlighted object, if any. Return t if some highlighting was
  5270. done, nil otherwise."
  5271. (when (org-string-nw-p org-latex-and-related-regexp)
  5272. (catch 'found
  5273. (while (re-search-forward org-latex-and-related-regexp limit t)
  5274. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5275. 'face))
  5276. '(org-code org-verbatim underline))
  5277. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5278. '(?_ ?^))
  5279. 1
  5280. 0)))
  5281. (font-lock-prepend-text-property
  5282. (+ offset (match-beginning 0)) (match-end 0)
  5283. 'face 'org-latex-and-related)
  5284. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5285. '(font-lock-multiline t)))
  5286. (throw 'found t)))
  5287. nil)))
  5288. (defun org-restart-font-lock ()
  5289. "Restart `font-lock-mode', to force refontification."
  5290. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5291. (font-lock-mode -1)
  5292. (font-lock-mode 1)))
  5293. (defun org-all-targets (&optional radio)
  5294. "Return a list of all targets in this file.
  5295. When optional argument RADIO is non-nil, only find radio
  5296. targets."
  5297. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5298. (save-excursion
  5299. (goto-char (point-min))
  5300. (while (re-search-forward re nil t)
  5301. ;; Make sure point is really within the object.
  5302. (backward-char)
  5303. (let ((obj (org-element-context)))
  5304. (when (memq (org-element-type obj) '(radio-target target))
  5305. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5306. rtn)))
  5307. (defun org-make-target-link-regexp (targets)
  5308. "Make regular expression matching all strings in TARGETS.
  5309. The regular expression finds the targets also if there is a line break
  5310. between words."
  5311. (and targets
  5312. (concat
  5313. "\\<\\("
  5314. (mapconcat
  5315. (lambda (x)
  5316. (setq x (regexp-quote x))
  5317. (while (string-match " +" x)
  5318. (setq x (replace-match "\\s-+" t t x)))
  5319. x)
  5320. targets
  5321. "\\|")
  5322. "\\)\\>")))
  5323. (defun org-activate-tags (limit)
  5324. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5325. (progn
  5326. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5327. (add-text-properties (match-beginning 1) (match-end 1)
  5328. (list 'mouse-face 'highlight
  5329. 'keymap org-mouse-map))
  5330. (org-rear-nonsticky-at (match-end 1))
  5331. t)))
  5332. (defun org-outline-level ()
  5333. "Compute the outline level of the heading at point.
  5334. If this is called at a normal headline, the level is the number of stars.
  5335. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5336. (save-excursion
  5337. (if (not (condition-case nil
  5338. (org-back-to-heading t)
  5339. (error nil)))
  5340. 0
  5341. (looking-at org-outline-regexp)
  5342. (1- (- (match-end 0) (match-beginning 0))))))
  5343. (defvar org-font-lock-keywords nil)
  5344. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5345. "Regular expression matching a property line.")
  5346. (defvar org-font-lock-hook nil
  5347. "Functions to be called for special font lock stuff.")
  5348. (defvar org-font-lock-set-keywords-hook nil
  5349. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5350. This is called after `org-font-lock-extra-keywords' is defined, but before
  5351. it is installed to be used by font lock. This can be useful if something
  5352. needs to be inserted at a specific position in the font-lock sequence.")
  5353. (defun org-font-lock-hook (limit)
  5354. "Run `org-font-lock-hook' within LIMIT."
  5355. (run-hook-with-args 'org-font-lock-hook limit))
  5356. (defun org-set-font-lock-defaults ()
  5357. "Set font lock defaults for the current buffer."
  5358. (let* ((em org-fontify-emphasized-text)
  5359. (lk org-activate-links)
  5360. (org-font-lock-extra-keywords
  5361. (list
  5362. ;; Call the hook
  5363. '(org-font-lock-hook)
  5364. ;; Headlines
  5365. `(,(if org-fontify-whole-heading-line
  5366. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5367. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5368. (1 (org-get-level-face 1))
  5369. (2 (org-get-level-face 2))
  5370. (3 (org-get-level-face 3)))
  5371. ;; Table lines
  5372. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5373. (1 'org-table t))
  5374. ;; Table internals
  5375. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5376. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5377. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5378. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5379. ;; Drawers
  5380. (list org-drawer-regexp '(0 'org-special-keyword t))
  5381. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5382. ;; Properties
  5383. (list org-property-re
  5384. '(1 'org-special-keyword t)
  5385. '(3 'org-property-value t))
  5386. ;; Links
  5387. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5388. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5389. (if (memq 'plain lk) '(org-activate-plain-links))
  5390. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5391. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5392. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5393. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5394. ;; Targets.
  5395. (list org-any-target-regexp '(0 'org-target t))
  5396. ;; Diary sexps.
  5397. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5398. '(org-hide-wide-columns (0 nil append))
  5399. ;; TODO keyword
  5400. (list (format org-heading-keyword-regexp-format
  5401. org-todo-regexp)
  5402. '(2 (org-get-todo-face 2) t))
  5403. ;; DONE
  5404. (if org-fontify-done-headline
  5405. (list (format org-heading-keyword-regexp-format
  5406. (concat
  5407. "\\(?:"
  5408. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5409. "\\)"))
  5410. '(2 'org-headline-done t))
  5411. nil)
  5412. ;; Priorities
  5413. '(org-font-lock-add-priority-faces)
  5414. ;; Tags
  5415. '(org-font-lock-add-tag-faces)
  5416. ;; Special keywords
  5417. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5418. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5419. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5420. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5421. ;; Emphasis
  5422. (if em
  5423. (if (featurep 'xemacs)
  5424. '(org-do-emphasis-faces (0 nil append))
  5425. '(org-do-emphasis-faces)))
  5426. ;; Checkboxes
  5427. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5428. 1 'org-checkbox prepend)
  5429. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5430. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5431. (0 (org-get-checkbox-statistics-face) t)))
  5432. ;; Description list items
  5433. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5434. 1 'org-list-dt prepend)
  5435. ;; ARCHIVEd headings
  5436. (list (concat
  5437. org-outline-regexp-bol
  5438. "\\(.*:" org-archive-tag ":.*\\)")
  5439. '(1 'org-archived prepend))
  5440. ;; Specials
  5441. '(org-do-latex-and-related)
  5442. '(org-fontify-entities)
  5443. '(org-raise-scripts)
  5444. ;; Code
  5445. '(org-activate-code (1 'org-code t))
  5446. ;; COMMENT
  5447. (list (format org-heading-keyword-regexp-format
  5448. (concat "\\("
  5449. org-comment-string "\\|" org-quote-string
  5450. "\\)"))
  5451. '(2 'org-special-keyword t))
  5452. ;; Blocks and meta lines
  5453. '(org-fontify-meta-lines-and-blocks)
  5454. )))
  5455. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5456. (run-hooks 'org-font-lock-set-keywords-hook)
  5457. ;; Now set the full font-lock-keywords
  5458. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5459. (org-set-local 'font-lock-defaults
  5460. '(org-font-lock-keywords t nil nil backward-paragraph))
  5461. (kill-local-variable 'font-lock-keywords) nil))
  5462. (defun org-toggle-pretty-entities ()
  5463. "Toggle the composition display of entities as UTF8 characters."
  5464. (interactive)
  5465. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5466. (org-restart-font-lock)
  5467. (if org-pretty-entities
  5468. (message "Entities are displayed as UTF8 characters")
  5469. (save-restriction
  5470. (widen)
  5471. (org-decompose-region (point-min) (point-max))
  5472. (message "Entities are displayed plain"))))
  5473. (defvar org-custom-properties-overlays nil
  5474. "List of overlays used for custom properties.")
  5475. (make-variable-buffer-local 'org-custom-properties-overlays)
  5476. (defun org-toggle-custom-properties-visibility ()
  5477. "Display or hide properties in `org-custom-properties'."
  5478. (interactive)
  5479. (if org-custom-properties-overlays
  5480. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5481. (setq org-custom-properties-overlays nil))
  5482. (unless (not org-custom-properties)
  5483. (save-excursion
  5484. (save-restriction
  5485. (widen)
  5486. (goto-char (point-min))
  5487. (while (re-search-forward org-property-re nil t)
  5488. (mapc (lambda(p)
  5489. (when (equal p (substring (match-string 1) 1 -1))
  5490. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5491. (overlay-put o 'invisible t)
  5492. (overlay-put o 'org-custom-property t)
  5493. (push o org-custom-properties-overlays))))
  5494. org-custom-properties)))))))
  5495. (defun org-fontify-entities (limit)
  5496. "Find an entity to fontify."
  5497. (let (ee)
  5498. (when org-pretty-entities
  5499. (catch 'match
  5500. (while (re-search-forward
  5501. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5502. limit t)
  5503. (if (and (not (org-in-indented-comment-line))
  5504. (setq ee (org-entity-get (match-string 1)))
  5505. (= (length (nth 6 ee)) 1))
  5506. (let*
  5507. ((end (if (equal (match-string 2) "{}")
  5508. (match-end 2)
  5509. (match-end 1))))
  5510. (add-text-properties
  5511. (match-beginning 0) end
  5512. (list 'font-lock-fontified t))
  5513. (compose-region (match-beginning 0) end
  5514. (nth 6 ee) nil)
  5515. (backward-char 1)
  5516. (throw 'match t))))
  5517. nil))))
  5518. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5519. "Fontify string S like in Org-mode."
  5520. (with-temp-buffer
  5521. (insert s)
  5522. (let ((org-odd-levels-only odd-levels))
  5523. (org-mode)
  5524. (font-lock-fontify-buffer)
  5525. (buffer-string))))
  5526. (defvar org-m nil)
  5527. (defvar org-l nil)
  5528. (defvar org-f nil)
  5529. (defun org-get-level-face (n)
  5530. "Get the right face for match N in font-lock matching of headlines."
  5531. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5532. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5533. (if org-cycle-level-faces
  5534. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5535. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5536. (cond
  5537. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5538. ((eq n 2) org-f)
  5539. (t (if org-level-color-stars-only nil org-f))))
  5540. (defun org-get-todo-face (kwd)
  5541. "Get the right face for a TODO keyword KWD.
  5542. If KWD is a number, get the corresponding match group."
  5543. (if (numberp kwd) (setq kwd (match-string kwd)))
  5544. (or (org-face-from-face-or-color
  5545. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5546. (and (member kwd org-done-keywords) 'org-done)
  5547. 'org-todo))
  5548. (defun org-face-from-face-or-color (context inherit face-or-color)
  5549. "Create a face list that inherits INHERIT, but sets the foreground color.
  5550. When FACE-OR-COLOR is not a string, just return it."
  5551. (if (stringp face-or-color)
  5552. (list :inherit inherit
  5553. (cdr (assoc context org-faces-easy-properties))
  5554. face-or-color)
  5555. face-or-color))
  5556. (defun org-font-lock-add-tag-faces (limit)
  5557. "Add the special tag faces."
  5558. (when (and org-tag-faces org-tags-special-faces-re)
  5559. (while (re-search-forward org-tags-special-faces-re limit t)
  5560. (add-text-properties (match-beginning 1) (match-end 1)
  5561. (list 'face (org-get-tag-face 1)
  5562. 'font-lock-fontified t))
  5563. (backward-char 1))))
  5564. (defun org-font-lock-add-priority-faces (limit)
  5565. "Add the special priority faces."
  5566. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5567. (when (save-match-data (org-at-heading-p))
  5568. (add-text-properties
  5569. (match-beginning 0) (match-end 0)
  5570. (list 'face (or (org-face-from-face-or-color
  5571. 'priority 'org-priority
  5572. (cdr (assoc (char-after (match-beginning 1))
  5573. org-priority-faces)))
  5574. 'org-priority)
  5575. 'font-lock-fontified t)))))
  5576. (defun org-get-tag-face (kwd)
  5577. "Get the right face for a TODO keyword KWD.
  5578. If KWD is a number, get the corresponding match group."
  5579. (if (numberp kwd) (setq kwd (match-string kwd)))
  5580. (or (org-face-from-face-or-color
  5581. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5582. 'org-tag))
  5583. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5584. "Remove fontification and activation overlays from links."
  5585. (font-lock-default-unfontify-region beg end)
  5586. (let* ((buffer-undo-list t)
  5587. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5588. (inhibit-modification-hooks t)
  5589. deactivate-mark buffer-file-name buffer-file-truename)
  5590. (org-decompose-region beg end)
  5591. (remove-text-properties beg end
  5592. '(mouse-face t keymap t org-linked-text t
  5593. invisible t intangible t
  5594. org-no-flyspell t org-emphasis t))
  5595. (org-remove-font-lock-display-properties beg end)))
  5596. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5597. ((raise 0.3) (height 0.7))
  5598. ((raise -0.5))
  5599. ((raise 0.5)))
  5600. "Display properties for showing superscripts and subscripts.")
  5601. (defun org-remove-font-lock-display-properties (beg end)
  5602. "Remove specific display properties that have been added by font lock.
  5603. The will remove the raise properties that are used to show superscripts
  5604. and subscripts."
  5605. (let (next prop)
  5606. (while (< beg end)
  5607. (setq next (next-single-property-change beg 'display nil end)
  5608. prop (get-text-property beg 'display))
  5609. (if (member prop org-script-display)
  5610. (put-text-property beg next 'display nil))
  5611. (setq beg next))))
  5612. (defun org-raise-scripts (limit)
  5613. "Add raise properties to sub/superscripts."
  5614. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5615. (if (re-search-forward
  5616. (if (eq org-use-sub-superscripts t)
  5617. org-match-substring-regexp
  5618. org-match-substring-with-braces-regexp)
  5619. limit t)
  5620. (let* ((pos (point)) table-p comment-p
  5621. (mpos (match-beginning 3))
  5622. (emph-p (get-text-property mpos 'org-emphasis))
  5623. (link-p (get-text-property mpos 'mouse-face))
  5624. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5625. (goto-char (point-at-bol))
  5626. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5627. comment-p (org-looking-at-p "[ \t]*#"))
  5628. (goto-char pos)
  5629. ;; FIXME: Should we go back one character here, for a_b^c
  5630. ;; (goto-char (1- pos)) ;????????????????????
  5631. (if (or comment-p emph-p link-p keyw-p)
  5632. t
  5633. (put-text-property (match-beginning 3) (match-end 0)
  5634. 'display
  5635. (if (equal (char-after (match-beginning 2)) ?^)
  5636. (nth (if table-p 3 1) org-script-display)
  5637. (nth (if table-p 2 0) org-script-display)))
  5638. (add-text-properties (match-beginning 2) (match-end 2)
  5639. (list 'invisible t
  5640. 'org-dwidth t 'org-dwidth-n 1))
  5641. (if (and (eq (char-after (match-beginning 3)) ?{)
  5642. (eq (char-before (match-end 3)) ?}))
  5643. (progn
  5644. (add-text-properties
  5645. (match-beginning 3) (1+ (match-beginning 3))
  5646. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5647. (add-text-properties
  5648. (1- (match-end 3)) (match-end 3)
  5649. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5650. t)))))
  5651. ;;;; Visibility cycling, including org-goto and indirect buffer
  5652. ;;; Cycling
  5653. (defvar org-cycle-global-status nil)
  5654. (make-variable-buffer-local 'org-cycle-global-status)
  5655. (put 'org-cycle-global-status 'org-state t)
  5656. (defvar org-cycle-subtree-status nil)
  5657. (make-variable-buffer-local 'org-cycle-subtree-status)
  5658. (put 'org-cycle-subtree-status 'org-state t)
  5659. (defvar org-inlinetask-min-level)
  5660. ;;;###autoload
  5661. (defun org-cycle (&optional arg)
  5662. "TAB-action and visibility cycling for Org-mode.
  5663. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5664. is outline visibility cycling, but it also invokes other actions
  5665. in special contexts.
  5666. - When this function is called with a prefix argument, rotate the entire
  5667. buffer through 3 states (global cycling)
  5668. 1. OVERVIEW: Show only top-level headlines.
  5669. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5670. 3. SHOW ALL: Show everything.
  5671. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5672. determined by the variable `org-startup-folded', and by any VISIBILITY
  5673. properties in the buffer.
  5674. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5675. including any drawers.
  5676. - When inside a table, re-align the table and move to the next field.
  5677. - When point is at the beginning of a headline, rotate the subtree started
  5678. by this line through 3 different states (local cycling)
  5679. 1. FOLDED: Only the main headline is shown.
  5680. 2. CHILDREN: The main headline and the direct children are shown.
  5681. From this state, you can move to one of the children
  5682. and zoom in further.
  5683. 3. SUBTREE: Show the entire subtree, including body text.
  5684. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5685. - When point is at the beginning of an empty headline and the variable
  5686. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5687. of the headline by demoting and promoting it to likely levels. This
  5688. speeds up creation document structure by pressing TAB once or several
  5689. times right after creating a new headline.
  5690. - When there is a numeric prefix, go up to a heading with level ARG, do
  5691. a `show-subtree' and return to the previous cursor position. If ARG
  5692. is negative, go up that many levels.
  5693. - When point is not at the beginning of a headline, execute the global
  5694. binding for TAB, which is re-indenting the line. See the option
  5695. `org-cycle-emulate-tab' for details.
  5696. - Special case: if point is at the beginning of the buffer and there is
  5697. no headline in line 1, this function will act as if called with prefix arg
  5698. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5699. But only if also the variable `org-cycle-global-at-bob' is t."
  5700. (interactive "P")
  5701. (org-load-modules-maybe)
  5702. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5703. (and org-cycle-level-after-item/entry-creation
  5704. (or (org-cycle-level)
  5705. (org-cycle-item-indentation))))
  5706. (let* (message-log-max ; Don't populate the *Messages* buffer
  5707. (limit-level
  5708. (or org-cycle-max-level
  5709. (and (boundp 'org-inlinetask-min-level)
  5710. org-inlinetask-min-level
  5711. (1- org-inlinetask-min-level))))
  5712. (nstars (and limit-level
  5713. (if org-odd-levels-only
  5714. (and limit-level (1- (* limit-level 2)))
  5715. limit-level)))
  5716. (org-outline-regexp
  5717. (if (not (derived-mode-p 'org-mode))
  5718. outline-regexp
  5719. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5720. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5721. (not (looking-at org-outline-regexp))))
  5722. (org-cycle-hook
  5723. (if bob-special
  5724. (delq 'org-optimize-window-after-visibility-change
  5725. (copy-sequence org-cycle-hook))
  5726. org-cycle-hook))
  5727. (pos (point)))
  5728. (if (or bob-special (equal arg '(4)))
  5729. ;; special case: use global cycling
  5730. (setq arg t))
  5731. (cond
  5732. ((equal arg '(16))
  5733. (setq last-command 'dummy)
  5734. (org-set-startup-visibility)
  5735. (message "Startup visibility, plus VISIBILITY properties"))
  5736. ((equal arg '(64))
  5737. (show-all)
  5738. (message "Entire buffer visible, including drawers"))
  5739. ;; Table: enter it or move to the next field.
  5740. ((org-at-table-p 'any)
  5741. (if (org-at-table.el-p)
  5742. (message "Use C-c ' to edit table.el tables")
  5743. (if arg (org-table-edit-field t)
  5744. (org-table-justify-field-maybe)
  5745. (call-interactively 'org-table-next-field))))
  5746. ((run-hook-with-args-until-success
  5747. 'org-tab-after-check-for-table-hook))
  5748. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5749. ((eq arg t) (org-cycle-internal-global))
  5750. ;; Drawers: delegate to `org-flag-drawer'.
  5751. ((and org-drawers org-drawer-regexp
  5752. (save-excursion
  5753. (beginning-of-line 1)
  5754. (looking-at org-drawer-regexp)))
  5755. (org-flag-drawer ; toggle block visibility
  5756. (not (get-char-property (match-end 0) 'invisible))))
  5757. ;; Show-subtree, ARG levels up from here.
  5758. ((integerp arg)
  5759. (save-excursion
  5760. (org-back-to-heading)
  5761. (outline-up-heading (if (< arg 0) (- arg)
  5762. (- (funcall outline-level) arg)))
  5763. (org-show-subtree)))
  5764. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5765. ((and (featurep 'org-inlinetask)
  5766. (org-inlinetask-at-task-p)
  5767. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5768. (org-inlinetask-toggle-visibility))
  5769. ((org-try-cdlatex-tab))
  5770. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5771. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5772. (save-excursion (beginning-of-line 1)
  5773. (looking-at org-outline-regexp)))
  5774. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5775. (org-cycle-internal-local))
  5776. ;; From there: TAB emulation and template completion.
  5777. (buffer-read-only (org-back-to-heading))
  5778. ((run-hook-with-args-until-success
  5779. 'org-tab-after-check-for-cycling-hook))
  5780. ((org-try-structure-completion))
  5781. ((run-hook-with-args-until-success
  5782. 'org-tab-before-tab-emulation-hook))
  5783. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5784. (or (not (bolp))
  5785. (not (looking-at org-outline-regexp))))
  5786. (call-interactively (global-key-binding "\t")))
  5787. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5788. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5789. (or (and (eq org-cycle-emulate-tab 'white)
  5790. (= (match-end 0) (point-at-eol)))
  5791. (and (eq org-cycle-emulate-tab 'whitestart)
  5792. (>= (match-end 0) pos))))
  5793. t
  5794. (eq org-cycle-emulate-tab t))
  5795. (call-interactively (global-key-binding "\t")))
  5796. (t (save-excursion
  5797. (org-back-to-heading)
  5798. (org-cycle)))))))
  5799. (defun org-cycle-internal-global ()
  5800. "Do the global cycling action."
  5801. ;; Hack to avoid display of messages for .org attachments in Gnus
  5802. (let (message-log-max ; Don't populate the *Messages* buffer
  5803. (ga (string-match "\\*fontification" (buffer-name))))
  5804. (cond
  5805. ((and (eq last-command this-command)
  5806. (eq org-cycle-global-status 'overview))
  5807. ;; We just created the overview - now do table of contents
  5808. ;; This can be slow in very large buffers, so indicate action
  5809. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5810. (unless ga (message "CONTENTS..."))
  5811. (org-content)
  5812. (unless ga (message "CONTENTS...done"))
  5813. (setq org-cycle-global-status 'contents)
  5814. (run-hook-with-args 'org-cycle-hook 'contents))
  5815. ((and (eq last-command this-command)
  5816. (eq org-cycle-global-status 'contents))
  5817. ;; We just showed the table of contents - now show everything
  5818. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5819. (show-all)
  5820. (unless ga (message "SHOW ALL"))
  5821. (setq org-cycle-global-status 'all)
  5822. (run-hook-with-args 'org-cycle-hook 'all))
  5823. (t
  5824. ;; Default action: go to overview
  5825. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5826. (org-overview)
  5827. (unless ga (message "OVERVIEW"))
  5828. (setq org-cycle-global-status 'overview)
  5829. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5830. (defun org-cycle-internal-local ()
  5831. "Do the local cycling action."
  5832. (let (message-log-max ; Don't populate the *Messages* buffer
  5833. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5834. ;; First, determine end of headline (EOH), end of subtree or item
  5835. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5836. (save-excursion
  5837. (if (org-at-item-p)
  5838. (progn
  5839. (beginning-of-line)
  5840. (setq struct (org-list-struct))
  5841. (setq eoh (point-at-eol))
  5842. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5843. (setq has-children (org-list-has-child-p (point) struct)))
  5844. (org-back-to-heading)
  5845. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5846. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5847. (setq has-children
  5848. (or (save-excursion
  5849. (let ((level (funcall outline-level)))
  5850. (outline-next-heading)
  5851. (and (org-at-heading-p t)
  5852. (> (funcall outline-level) level))))
  5853. (save-excursion
  5854. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5855. ;; Determine end invisible part of buffer (EOL)
  5856. (beginning-of-line 2)
  5857. ;; XEmacs doesn't have `next-single-char-property-change'
  5858. (if (featurep 'xemacs)
  5859. (while (and (not (eobp)) ;; this is like `next-line'
  5860. (get-char-property (1- (point)) 'invisible))
  5861. (beginning-of-line 2))
  5862. (while (and (not (eobp)) ;; this is like `next-line'
  5863. (get-char-property (1- (point)) 'invisible))
  5864. (goto-char (next-single-char-property-change (point) 'invisible))
  5865. (and (eolp) (beginning-of-line 2))))
  5866. (setq eol (point)))
  5867. ;; Find out what to do next and set `this-command'
  5868. (cond
  5869. ((= eos eoh)
  5870. ;; Nothing is hidden behind this heading
  5871. (unless (org-before-first-heading-p)
  5872. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5873. (message "EMPTY ENTRY")
  5874. (setq org-cycle-subtree-status nil)
  5875. (save-excursion
  5876. (goto-char eos)
  5877. (outline-next-heading)
  5878. (if (outline-invisible-p) (org-flag-heading nil))))
  5879. ((and (or (>= eol eos)
  5880. (not (string-match "\\S-" (buffer-substring eol eos))))
  5881. (or has-children
  5882. (not (setq children-skipped
  5883. org-cycle-skip-children-state-if-no-children))))
  5884. ;; Entire subtree is hidden in one line: children view
  5885. (unless (org-before-first-heading-p)
  5886. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5887. (if (org-at-item-p)
  5888. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5889. (org-show-entry)
  5890. (org-with-limited-levels (show-children))
  5891. ;; FIXME: This slows down the func way too much.
  5892. ;; How keep drawers hidden in subtree anyway?
  5893. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5894. ;; (org-cycle-hide-drawers 'subtree))
  5895. ;; Fold every list in subtree to top-level items.
  5896. (when (eq org-cycle-include-plain-lists 'integrate)
  5897. (save-excursion
  5898. (org-back-to-heading)
  5899. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5900. (beginning-of-line 1)
  5901. (let* ((struct (org-list-struct))
  5902. (prevs (org-list-prevs-alist struct))
  5903. (end (org-list-get-bottom-point struct)))
  5904. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5905. (org-list-get-all-items (point) struct prevs))
  5906. (goto-char end))))))
  5907. (message "CHILDREN")
  5908. (save-excursion
  5909. (goto-char eos)
  5910. (outline-next-heading)
  5911. (if (outline-invisible-p) (org-flag-heading nil)))
  5912. (setq org-cycle-subtree-status 'children)
  5913. (unless (org-before-first-heading-p)
  5914. (run-hook-with-args 'org-cycle-hook 'children)))
  5915. ((or children-skipped
  5916. (and (eq last-command this-command)
  5917. (eq org-cycle-subtree-status 'children)))
  5918. ;; We just showed the children, or no children are there,
  5919. ;; now show everything.
  5920. (unless (org-before-first-heading-p)
  5921. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5922. (outline-flag-region eoh eos nil)
  5923. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5924. (setq org-cycle-subtree-status 'subtree)
  5925. (unless (org-before-first-heading-p)
  5926. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5927. (t
  5928. ;; Default action: hide the subtree.
  5929. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5930. (outline-flag-region eoh eos t)
  5931. (message "FOLDED")
  5932. (setq org-cycle-subtree-status 'folded)
  5933. (unless (org-before-first-heading-p)
  5934. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5935. ;;;###autoload
  5936. (defun org-global-cycle (&optional arg)
  5937. "Cycle the global visibility. For details see `org-cycle'.
  5938. With \\[universal-argument] prefix arg, switch to startup visibility.
  5939. With a numeric prefix, show all headlines up to that level."
  5940. (interactive "P")
  5941. (let ((org-cycle-include-plain-lists
  5942. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5943. (cond
  5944. ((integerp arg)
  5945. (show-all)
  5946. (hide-sublevels arg)
  5947. (setq org-cycle-global-status 'contents))
  5948. ((equal arg '(4))
  5949. (org-set-startup-visibility)
  5950. (message "Startup visibility, plus VISIBILITY properties."))
  5951. (t
  5952. (org-cycle '(4))))))
  5953. (defun org-set-startup-visibility ()
  5954. "Set the visibility required by startup options and properties."
  5955. (cond
  5956. ((eq org-startup-folded t)
  5957. (org-cycle '(4)))
  5958. ((eq org-startup-folded 'content)
  5959. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5960. (org-cycle '(4)) (org-cycle '(4)))))
  5961. (unless (eq org-startup-folded 'showeverything)
  5962. (if org-hide-block-startup (org-hide-block-all))
  5963. (org-set-visibility-according-to-property 'no-cleanup)
  5964. (org-cycle-hide-archived-subtrees 'all)
  5965. (org-cycle-hide-drawers 'all)
  5966. (org-cycle-show-empty-lines t)))
  5967. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5968. "Switch subtree visibilities according to :VISIBILITY: property."
  5969. (interactive)
  5970. (let (org-show-entry-below state)
  5971. (save-excursion
  5972. (goto-char (point-min))
  5973. (while (re-search-forward
  5974. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5975. nil t)
  5976. (setq state (match-string 1))
  5977. (save-excursion
  5978. (org-back-to-heading t)
  5979. (hide-subtree)
  5980. (org-reveal)
  5981. (cond
  5982. ((equal state '("fold" "folded"))
  5983. (hide-subtree))
  5984. ((equal state "children")
  5985. (org-show-hidden-entry)
  5986. (show-children))
  5987. ((equal state "content")
  5988. (save-excursion
  5989. (save-restriction
  5990. (org-narrow-to-subtree)
  5991. (org-content))))
  5992. ((member state '("all" "showall"))
  5993. (show-subtree)))))
  5994. (unless no-cleanup
  5995. (org-cycle-hide-archived-subtrees 'all)
  5996. (org-cycle-hide-drawers 'all)
  5997. (org-cycle-show-empty-lines 'all)))))
  5998. ;; This function uses outline-regexp instead of the more fundamental
  5999. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6000. ;; buffers, where outline-regexp is needed.
  6001. (defun org-overview ()
  6002. "Switch to overview mode, showing only top-level headlines.
  6003. Really, this shows all headlines with level equal or greater than the level
  6004. of the first headline in the buffer. This is important, because if the
  6005. first headline is not level one, then (hide-sublevels 1) gives confusing
  6006. results."
  6007. (interactive)
  6008. (let ((l (org-current-line))
  6009. (level (save-excursion
  6010. (goto-char (point-min))
  6011. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6012. (progn
  6013. (goto-char (match-beginning 0))
  6014. (funcall outline-level))))))
  6015. (and level (hide-sublevels level))
  6016. (recenter '(4))
  6017. (org-goto-line l)))
  6018. (defun org-content (&optional arg)
  6019. "Show all headlines in the buffer, like a table of contents.
  6020. With numerical argument N, show content up to level N."
  6021. (interactive "P")
  6022. (save-excursion
  6023. ;; Visit all headings and show their offspring
  6024. (and (integerp arg) (org-overview))
  6025. (goto-char (point-max))
  6026. (catch 'exit
  6027. (while (and (progn (condition-case nil
  6028. (outline-previous-visible-heading 1)
  6029. (error (goto-char (point-min))))
  6030. t)
  6031. (looking-at org-outline-regexp))
  6032. (if (integerp arg)
  6033. (show-children (1- arg))
  6034. (show-branches))
  6035. (if (bobp) (throw 'exit nil))))))
  6036. (defun org-optimize-window-after-visibility-change (state)
  6037. "Adjust the window after a change in outline visibility.
  6038. This function is the default value of the hook `org-cycle-hook'."
  6039. (when (get-buffer-window (current-buffer))
  6040. (cond
  6041. ((eq state 'content) nil)
  6042. ((eq state 'all) nil)
  6043. ((eq state 'folded) nil)
  6044. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6045. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6046. (defun org-remove-empty-overlays-at (pos)
  6047. "Remove outline overlays that do not contain non-white stuff."
  6048. (mapc
  6049. (lambda (o)
  6050. (and (eq 'outline (overlay-get o 'invisible))
  6051. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6052. (overlay-end o))))
  6053. (delete-overlay o)))
  6054. (overlays-at pos)))
  6055. (defun org-clean-visibility-after-subtree-move ()
  6056. "Fix visibility issues after moving a subtree."
  6057. ;; First, find a reasonable region to look at:
  6058. ;; Start two siblings above, end three below
  6059. (let* ((beg (save-excursion
  6060. (and (org-get-last-sibling)
  6061. (org-get-last-sibling))
  6062. (point)))
  6063. (end (save-excursion
  6064. (and (org-get-next-sibling)
  6065. (org-get-next-sibling)
  6066. (org-get-next-sibling))
  6067. (if (org-at-heading-p)
  6068. (point-at-eol)
  6069. (point))))
  6070. (level (looking-at "\\*+"))
  6071. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6072. (save-excursion
  6073. (save-restriction
  6074. (narrow-to-region beg end)
  6075. (when re
  6076. ;; Properly fold already folded siblings
  6077. (goto-char (point-min))
  6078. (while (re-search-forward re nil t)
  6079. (if (and (not (outline-invisible-p))
  6080. (save-excursion
  6081. (goto-char (point-at-eol)) (outline-invisible-p)))
  6082. (hide-entry))))
  6083. (org-cycle-show-empty-lines 'overview)
  6084. (org-cycle-hide-drawers 'overview)))))
  6085. (defun org-cycle-show-empty-lines (state)
  6086. "Show empty lines above all visible headlines.
  6087. The region to be covered depends on STATE when called through
  6088. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6089. entire buffer covered. Note that an empty line is only shown if there
  6090. are at least `org-cycle-separator-lines' empty lines before the headline."
  6091. (when (not (= org-cycle-separator-lines 0))
  6092. (save-excursion
  6093. (let* ((n (abs org-cycle-separator-lines))
  6094. (re (cond
  6095. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6096. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6097. (t (let ((ns (number-to-string (- n 2))))
  6098. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6099. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6100. beg end b e)
  6101. (cond
  6102. ((memq state '(overview contents t))
  6103. (setq beg (point-min) end (point-max)))
  6104. ((memq state '(children folded))
  6105. (setq beg (point) end (progn (org-end-of-subtree t t)
  6106. (beginning-of-line 2)
  6107. (point)))))
  6108. (when beg
  6109. (goto-char beg)
  6110. (while (re-search-forward re end t)
  6111. (unless (get-char-property (match-end 1) 'invisible)
  6112. (setq e (match-end 1))
  6113. (if (< org-cycle-separator-lines 0)
  6114. (setq b (save-excursion
  6115. (goto-char (match-beginning 0))
  6116. (org-back-over-empty-lines)
  6117. (if (save-excursion
  6118. (goto-char (max (point-min) (1- (point))))
  6119. (org-at-heading-p))
  6120. (1- (point))
  6121. (point))))
  6122. (setq b (match-beginning 1)))
  6123. (outline-flag-region b e nil)))))))
  6124. ;; Never hide empty lines at the end of the file.
  6125. (save-excursion
  6126. (goto-char (point-max))
  6127. (outline-previous-heading)
  6128. (outline-end-of-heading)
  6129. (if (and (looking-at "[ \t\n]+")
  6130. (= (match-end 0) (point-max)))
  6131. (outline-flag-region (point) (match-end 0) nil))))
  6132. (defun org-show-empty-lines-in-parent ()
  6133. "Move to the parent and re-show empty lines before visible headlines."
  6134. (save-excursion
  6135. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6136. (org-cycle-show-empty-lines context))))
  6137. (defun org-files-list ()
  6138. "Return `org-agenda-files' list, plus all open org-mode files.
  6139. This is useful for operations that need to scan all of a user's
  6140. open and agenda-wise Org files."
  6141. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6142. (dolist (buf (buffer-list))
  6143. (with-current-buffer buf
  6144. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6145. (let ((file (expand-file-name (buffer-file-name))))
  6146. (unless (member file files)
  6147. (push file files))))))
  6148. files))
  6149. (defsubst org-entry-beginning-position ()
  6150. "Return the beginning position of the current entry."
  6151. (save-excursion (outline-back-to-heading t) (point)))
  6152. (defsubst org-entry-end-position ()
  6153. "Return the end position of the current entry."
  6154. (save-excursion (outline-next-heading) (point)))
  6155. (defun org-cycle-hide-drawers (state)
  6156. "Re-hide all drawers after a visibility state change."
  6157. (when (and (derived-mode-p 'org-mode)
  6158. (not (memq state '(overview folded contents))))
  6159. (save-excursion
  6160. (let* ((globalp (memq state '(contents all)))
  6161. (beg (if globalp (point-min) (point)))
  6162. (end (if globalp (point-max)
  6163. (if (eq state 'children)
  6164. (save-excursion (outline-next-heading) (point))
  6165. (org-end-of-subtree t)))))
  6166. (goto-char beg)
  6167. (while (re-search-forward org-drawer-regexp end t)
  6168. (org-flag-drawer t))))))
  6169. (defun org-cycle-hide-inline-tasks (state)
  6170. "Re-hide inline task when switching to 'contents visibility state."
  6171. (when (and (eq state 'contents)
  6172. (boundp 'org-inlinetask-min-level)
  6173. org-inlinetask-min-level)
  6174. (hide-sublevels (1- org-inlinetask-min-level))))
  6175. (defun org-flag-drawer (flag)
  6176. "When FLAG is non-nil, hide the drawer we are within.
  6177. Otherwise make it visible."
  6178. (save-excursion
  6179. (beginning-of-line 1)
  6180. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6181. (let ((b (match-end 0)))
  6182. (if (re-search-forward
  6183. "^[ \t]*:END:"
  6184. (save-excursion (outline-next-heading) (point)) t)
  6185. (outline-flag-region b (point-at-eol) flag)
  6186. (error ":END: line missing at position %s" b))))))
  6187. (defun org-subtree-end-visible-p ()
  6188. "Is the end of the current subtree visible?"
  6189. (pos-visible-in-window-p
  6190. (save-excursion (org-end-of-subtree t) (point))))
  6191. (defun org-first-headline-recenter (&optional N)
  6192. "Move cursor to the first headline and recenter the headline.
  6193. Optional argument N means put the headline into the Nth line of the window."
  6194. (goto-char (point-min))
  6195. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6196. (beginning-of-line)
  6197. (recenter (prefix-numeric-value N))))
  6198. ;;; Saving and restoring visibility
  6199. (defun org-outline-overlay-data (&optional use-markers)
  6200. "Return a list of the locations of all outline overlays.
  6201. These are overlays with the `invisible' property value `outline'.
  6202. The return value is a list of cons cells, with start and stop
  6203. positions for each overlay.
  6204. If USE-MARKERS is set, return the positions as markers."
  6205. (let (beg end)
  6206. (save-excursion
  6207. (save-restriction
  6208. (widen)
  6209. (delq nil
  6210. (mapcar (lambda (o)
  6211. (when (eq (overlay-get o 'invisible) 'outline)
  6212. (setq beg (overlay-start o)
  6213. end (overlay-end o))
  6214. (and beg end (> end beg)
  6215. (if use-markers
  6216. (cons (move-marker (make-marker) beg)
  6217. (move-marker (make-marker) end))
  6218. (cons beg end)))))
  6219. (overlays-in (point-min) (point-max))))))))
  6220. (defun org-set-outline-overlay-data (data)
  6221. "Create visibility overlays for all positions in DATA.
  6222. DATA should have been made by `org-outline-overlay-data'."
  6223. (let (o)
  6224. (save-excursion
  6225. (save-restriction
  6226. (widen)
  6227. (show-all)
  6228. (mapc (lambda (c)
  6229. (outline-flag-region (car c) (cdr c) t))
  6230. data)))))
  6231. ;;; Folding of blocks
  6232. (defvar org-hide-block-overlays nil
  6233. "Overlays hiding blocks.")
  6234. (make-variable-buffer-local 'org-hide-block-overlays)
  6235. (defun org-block-map (function &optional start end)
  6236. "Call FUNCTION at the head of all source blocks in the current buffer.
  6237. Optional arguments START and END can be used to limit the range."
  6238. (let ((start (or start (point-min)))
  6239. (end (or end (point-max))))
  6240. (save-excursion
  6241. (goto-char start)
  6242. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6243. (save-excursion
  6244. (save-match-data
  6245. (goto-char (match-beginning 0))
  6246. (funcall function)))))))
  6247. (defun org-hide-block-toggle-all ()
  6248. "Toggle the visibility of all blocks in the current buffer."
  6249. (org-block-map #'org-hide-block-toggle))
  6250. (defun org-hide-block-all ()
  6251. "Fold all blocks in the current buffer."
  6252. (interactive)
  6253. (org-show-block-all)
  6254. (org-block-map #'org-hide-block-toggle-maybe))
  6255. (defun org-show-block-all ()
  6256. "Unfold all blocks in the current buffer."
  6257. (interactive)
  6258. (mapc 'delete-overlay org-hide-block-overlays)
  6259. (setq org-hide-block-overlays nil))
  6260. (defun org-hide-block-toggle-maybe ()
  6261. "Toggle visibility of block at point."
  6262. (interactive)
  6263. (let ((case-fold-search t))
  6264. (if (save-excursion
  6265. (beginning-of-line 1)
  6266. (looking-at org-block-regexp))
  6267. (progn (org-hide-block-toggle)
  6268. t) ;; to signal that we took action
  6269. nil))) ;; to signal that we did not
  6270. (defun org-hide-block-toggle (&optional force)
  6271. "Toggle the visibility of the current block."
  6272. (interactive)
  6273. (save-excursion
  6274. (beginning-of-line)
  6275. (if (re-search-forward org-block-regexp nil t)
  6276. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6277. (end (match-end 0)) ;; end of entire body
  6278. ov)
  6279. (if (memq t (mapcar (lambda (overlay)
  6280. (eq (overlay-get overlay 'invisible)
  6281. 'org-hide-block))
  6282. (overlays-at start)))
  6283. (if (or (not force) (eq force 'off))
  6284. (mapc (lambda (ov)
  6285. (when (member ov org-hide-block-overlays)
  6286. (setq org-hide-block-overlays
  6287. (delq ov org-hide-block-overlays)))
  6288. (when (eq (overlay-get ov 'invisible)
  6289. 'org-hide-block)
  6290. (delete-overlay ov)))
  6291. (overlays-at start)))
  6292. (setq ov (make-overlay start end))
  6293. (overlay-put ov 'invisible 'org-hide-block)
  6294. ;; make the block accessible to isearch
  6295. (overlay-put
  6296. ov 'isearch-open-invisible
  6297. (lambda (ov)
  6298. (when (member ov org-hide-block-overlays)
  6299. (setq org-hide-block-overlays
  6300. (delq ov org-hide-block-overlays)))
  6301. (when (eq (overlay-get ov 'invisible)
  6302. 'org-hide-block)
  6303. (delete-overlay ov))))
  6304. (push ov org-hide-block-overlays)))
  6305. (error "Not looking at a source block"))))
  6306. ;; org-tab-after-check-for-cycling-hook
  6307. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6308. ;; Remove overlays when changing major mode
  6309. (add-hook 'org-mode-hook
  6310. (lambda () (org-add-hook 'change-major-mode-hook
  6311. 'org-show-block-all 'append 'local)))
  6312. ;;; Org-goto
  6313. (defvar org-goto-window-configuration nil)
  6314. (defvar org-goto-marker nil)
  6315. (defvar org-goto-map)
  6316. (defun org-goto-map ()
  6317. "Set the keymap `org-goto'."
  6318. (setq org-goto-map
  6319. (let ((map (make-sparse-keymap)))
  6320. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6321. mouse-drag-region universal-argument org-occur))
  6322. cmd)
  6323. (while (setq cmd (pop cmds))
  6324. (substitute-key-definition cmd cmd map global-map)))
  6325. (suppress-keymap map)
  6326. (org-defkey map "\C-m" 'org-goto-ret)
  6327. (org-defkey map [(return)] 'org-goto-ret)
  6328. (org-defkey map [(left)] 'org-goto-left)
  6329. (org-defkey map [(right)] 'org-goto-right)
  6330. (org-defkey map [(control ?g)] 'org-goto-quit)
  6331. (org-defkey map "\C-i" 'org-cycle)
  6332. (org-defkey map [(tab)] 'org-cycle)
  6333. (org-defkey map [(down)] 'outline-next-visible-heading)
  6334. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6335. (if org-goto-auto-isearch
  6336. (if (fboundp 'define-key-after)
  6337. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6338. nil)
  6339. (org-defkey map "q" 'org-goto-quit)
  6340. (org-defkey map "n" 'outline-next-visible-heading)
  6341. (org-defkey map "p" 'outline-previous-visible-heading)
  6342. (org-defkey map "f" 'outline-forward-same-level)
  6343. (org-defkey map "b" 'outline-backward-same-level)
  6344. (org-defkey map "u" 'outline-up-heading))
  6345. (org-defkey map "/" 'org-occur)
  6346. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6347. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6348. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6349. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6350. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6351. map)))
  6352. (defconst org-goto-help
  6353. "Browse buffer copy, to find location or copy text.%s
  6354. RET=jump to location C-g=quit and return to previous location
  6355. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6356. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6357. ;; FIXME: Docstring does not mention both interfaces
  6358. (defun org-goto (&optional alternative-interface)
  6359. "Look up a different location in the current file, keeping current visibility.
  6360. When you want look-up or go to a different location in a
  6361. document, the fastest way is often to fold the entire buffer and
  6362. then dive into the tree. This method has the disadvantage, that
  6363. the previous location will be folded, which may not be what you
  6364. want.
  6365. This command works around this by showing a copy of the current
  6366. buffer in an indirect buffer, in overview mode. You can dive
  6367. into the tree in that copy, use org-occur and incremental search
  6368. to find a location. When pressing RET or `Q', the command
  6369. returns to the original buffer in which the visibility is still
  6370. unchanged. After RET it will also jump to the location selected
  6371. in the indirect buffer and expose the headline hierarchy above.
  6372. With a prefix argument, use the alternative interface: e.g. if
  6373. `org-goto-interface' is 'outline use 'outline-path-completion."
  6374. (interactive "P")
  6375. (org-goto-map)
  6376. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6377. (org-refile-use-outline-path t)
  6378. (org-refile-target-verify-function nil)
  6379. (interface
  6380. (if (not alternative-interface)
  6381. org-goto-interface
  6382. (if (eq org-goto-interface 'outline)
  6383. 'outline-path-completion
  6384. 'outline)))
  6385. (org-goto-start-pos (point))
  6386. (selected-point
  6387. (if (eq interface 'outline)
  6388. (car (org-get-location (current-buffer) org-goto-help))
  6389. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6390. (org-refile-check-position pa)
  6391. (nth 3 pa)))))
  6392. (if selected-point
  6393. (progn
  6394. (org-mark-ring-push org-goto-start-pos)
  6395. (goto-char selected-point)
  6396. (if (or (outline-invisible-p) (org-invisible-p2))
  6397. (org-show-context 'org-goto)))
  6398. (message "Quit"))))
  6399. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6400. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6401. (defvar org-goto-local-auto-isearch-map) ; defined below
  6402. (defun org-get-location (buf help)
  6403. "Let the user select a location in the Org-mode buffer BUF.
  6404. This function uses a recursive edit. It returns the selected position
  6405. or nil."
  6406. (org-no-popups
  6407. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6408. (isearch-hide-immediately nil)
  6409. (isearch-search-fun-function
  6410. (lambda () 'org-goto-local-search-headings))
  6411. (org-goto-selected-point org-goto-exit-command))
  6412. (save-excursion
  6413. (save-window-excursion
  6414. (delete-other-windows)
  6415. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6416. (org-pop-to-buffer-same-window
  6417. (condition-case nil
  6418. (make-indirect-buffer (current-buffer) "*org-goto*")
  6419. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6420. (with-output-to-temp-buffer "*Org Help*"
  6421. (princ (format help (if org-goto-auto-isearch
  6422. " Just type for auto-isearch."
  6423. " n/p/f/b/u to navigate, q to quit."))))
  6424. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6425. (setq buffer-read-only nil)
  6426. (let ((org-startup-truncated t)
  6427. (org-startup-folded nil)
  6428. (org-startup-align-all-tables nil))
  6429. (org-mode)
  6430. (org-overview))
  6431. (setq buffer-read-only t)
  6432. (if (and (boundp 'org-goto-start-pos)
  6433. (integer-or-marker-p org-goto-start-pos))
  6434. (let ((org-show-hierarchy-above t)
  6435. (org-show-siblings t)
  6436. (org-show-following-heading t))
  6437. (goto-char org-goto-start-pos)
  6438. (and (outline-invisible-p) (org-show-context)))
  6439. (goto-char (point-min)))
  6440. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6441. (message "Select location and press RET")
  6442. (use-local-map org-goto-map)
  6443. (recursive-edit)))
  6444. (kill-buffer "*org-goto*")
  6445. (cons org-goto-selected-point org-goto-exit-command))))
  6446. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6447. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6448. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6449. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6450. (defun org-goto-local-search-headings (string bound noerror)
  6451. "Search and make sure that any matches are in headlines."
  6452. (catch 'return
  6453. (while (if isearch-forward
  6454. (search-forward string bound noerror)
  6455. (search-backward string bound noerror))
  6456. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6457. (and (member :headline context)
  6458. (not (member :tags context))))
  6459. (throw 'return (point))))))
  6460. (defun org-goto-local-auto-isearch ()
  6461. "Start isearch."
  6462. (interactive)
  6463. (goto-char (point-min))
  6464. (let ((keys (this-command-keys)))
  6465. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6466. (isearch-mode t)
  6467. (isearch-process-search-char (string-to-char keys)))))
  6468. (defun org-goto-ret (&optional arg)
  6469. "Finish `org-goto' by going to the new location."
  6470. (interactive "P")
  6471. (setq org-goto-selected-point (point)
  6472. org-goto-exit-command 'return)
  6473. (throw 'exit nil))
  6474. (defun org-goto-left ()
  6475. "Finish `org-goto' by going to the new location."
  6476. (interactive)
  6477. (if (org-at-heading-p)
  6478. (progn
  6479. (beginning-of-line 1)
  6480. (setq org-goto-selected-point (point)
  6481. org-goto-exit-command 'left)
  6482. (throw 'exit nil))
  6483. (error "Not on a heading")))
  6484. (defun org-goto-right ()
  6485. "Finish `org-goto' by going to the new location."
  6486. (interactive)
  6487. (if (org-at-heading-p)
  6488. (progn
  6489. (setq org-goto-selected-point (point)
  6490. org-goto-exit-command 'right)
  6491. (throw 'exit nil))
  6492. (error "Not on a heading")))
  6493. (defun org-goto-quit ()
  6494. "Finish `org-goto' without cursor motion."
  6495. (interactive)
  6496. (setq org-goto-selected-point nil)
  6497. (setq org-goto-exit-command 'quit)
  6498. (throw 'exit nil))
  6499. ;;; Indirect buffer display of subtrees
  6500. (defvar org-indirect-dedicated-frame nil
  6501. "This is the frame being used for indirect tree display.")
  6502. (defvar org-last-indirect-buffer nil)
  6503. (defun org-tree-to-indirect-buffer (&optional arg)
  6504. "Create indirect buffer and narrow it to current subtree.
  6505. With a numerical prefix ARG, go up to this level and then take that tree.
  6506. If ARG is negative, go up that many levels.
  6507. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6508. indirect buffer previously made with this command, to avoid proliferation of
  6509. indirect buffers. However, when you call the command with a \
  6510. \\[universal-argument] prefix, or
  6511. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6512. is kept so that you can work with several indirect buffers at the same time.
  6513. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6514. \\[universal-argument] prefix also
  6515. requests that a new frame be made for the new buffer, so that the dedicated
  6516. frame is not changed."
  6517. (interactive "P")
  6518. (let ((cbuf (current-buffer))
  6519. (cwin (selected-window))
  6520. (pos (point))
  6521. beg end level heading ibuf)
  6522. (save-excursion
  6523. (org-back-to-heading t)
  6524. (when (numberp arg)
  6525. (setq level (org-outline-level))
  6526. (if (< arg 0) (setq arg (+ level arg)))
  6527. (while (> (setq level (org-outline-level)) arg)
  6528. (org-up-heading-safe)))
  6529. (setq beg (point)
  6530. heading (org-get-heading))
  6531. (org-end-of-subtree t t)
  6532. (if (org-at-heading-p) (backward-char 1))
  6533. (setq end (point)))
  6534. (if (and (buffer-live-p org-last-indirect-buffer)
  6535. (not (eq org-indirect-buffer-display 'new-frame))
  6536. (not arg))
  6537. (kill-buffer org-last-indirect-buffer))
  6538. (setq ibuf (org-get-indirect-buffer cbuf)
  6539. org-last-indirect-buffer ibuf)
  6540. (cond
  6541. ((or (eq org-indirect-buffer-display 'new-frame)
  6542. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6543. (select-frame (make-frame))
  6544. (delete-other-windows)
  6545. (org-pop-to-buffer-same-window ibuf)
  6546. (org-set-frame-title heading))
  6547. ((eq org-indirect-buffer-display 'dedicated-frame)
  6548. (raise-frame
  6549. (select-frame (or (and org-indirect-dedicated-frame
  6550. (frame-live-p org-indirect-dedicated-frame)
  6551. org-indirect-dedicated-frame)
  6552. (setq org-indirect-dedicated-frame (make-frame)))))
  6553. (delete-other-windows)
  6554. (org-pop-to-buffer-same-window ibuf)
  6555. (org-set-frame-title (concat "Indirect: " heading)))
  6556. ((eq org-indirect-buffer-display 'current-window)
  6557. (org-pop-to-buffer-same-window ibuf))
  6558. ((eq org-indirect-buffer-display 'other-window)
  6559. (pop-to-buffer ibuf))
  6560. (t (error "Invalid value")))
  6561. (if (featurep 'xemacs)
  6562. (save-excursion (org-mode) (turn-on-font-lock)))
  6563. (narrow-to-region beg end)
  6564. (show-all)
  6565. (goto-char pos)
  6566. (run-hook-with-args 'org-cycle-hook 'all)
  6567. (and (window-live-p cwin) (select-window cwin))))
  6568. (defun org-get-indirect-buffer (&optional buffer)
  6569. (setq buffer (or buffer (current-buffer)))
  6570. (let ((n 1) (base (buffer-name buffer)) bname)
  6571. (while (buffer-live-p
  6572. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6573. (setq n (1+ n)))
  6574. (condition-case nil
  6575. (make-indirect-buffer buffer bname 'clone)
  6576. (error (make-indirect-buffer buffer bname)))))
  6577. (defun org-set-frame-title (title)
  6578. "Set the title of the current frame to the string TITLE."
  6579. ;; FIXME: how to name a single frame in XEmacs???
  6580. (unless (featurep 'xemacs)
  6581. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6582. ;;;; Structure editing
  6583. ;;; Inserting headlines
  6584. (defun org-previous-line-empty-p (&optional next)
  6585. "Is the previous line a blank line?
  6586. When NEXT is non-nil, check the next line instead."
  6587. (save-excursion
  6588. (and (not (bobp))
  6589. (or (beginning-of-line (if next 2 0)) t)
  6590. (save-match-data
  6591. (looking-at "[ \t]*$")))))
  6592. (defun org-insert-heading (&optional arg invisible-ok)
  6593. "Insert a new heading or item with same depth at point.
  6594. If point is in a plain list and ARG is nil, create a new list item.
  6595. With one universal prefix argument, insert a heading even in lists.
  6596. With two universal prefix arguments, insert the heading at the end
  6597. of the parent subtree.
  6598. If point is at the beginning of a headline, insert a sibling before
  6599. the current headline. If point is not at the beginning, split the line
  6600. and create a new headline with the text in the current line after point
  6601. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6602. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6603. This is important for non-interactive uses of the command."
  6604. (interactive "P")
  6605. (if (or (= (buffer-size) 0)
  6606. (and (not (save-excursion
  6607. (and (ignore-errors (org-back-to-heading invisible-ok))
  6608. (org-at-heading-p))))
  6609. (or arg (not (org-in-item-p)))))
  6610. (progn
  6611. (insert
  6612. (if (org-previous-line-empty-p) "" "\n")
  6613. (if (org-in-src-block-p) ",* " "* "))
  6614. (run-hooks 'org-insert-heading-hook))
  6615. (when (or arg
  6616. (not (org-insert-item
  6617. (save-excursion
  6618. (beginning-of-line)
  6619. (looking-at org-list-full-item-re)
  6620. (match-string 3)))))
  6621. (let* ((empty-line-p nil)
  6622. (eops (equal arg '(16))) ; insert at end of parent subtree
  6623. (org-insert-heading-respect-content
  6624. (or (not (null arg)) org-insert-heading-respect-content))
  6625. (level nil)
  6626. (on-heading (org-at-heading-p))
  6627. (head (save-excursion
  6628. (condition-case nil
  6629. (progn
  6630. (org-back-to-heading invisible-ok)
  6631. (when (and (not on-heading)
  6632. (featurep 'org-inlinetask)
  6633. (integerp org-inlinetask-min-level)
  6634. (>= (length (match-string 0))
  6635. org-inlinetask-min-level))
  6636. ;; Find a heading level before the inline task
  6637. (while (and (setq level (org-up-heading-safe))
  6638. (>= level org-inlinetask-min-level)))
  6639. (if (org-at-heading-p)
  6640. (org-back-to-heading invisible-ok)
  6641. (error "This should not happen")))
  6642. (unless (and (save-excursion
  6643. (save-match-data
  6644. (org-backward-heading-same-level 1 invisible-ok))
  6645. (= (point) (match-beginning 0)))
  6646. (not (org-previous-line-empty-p t)))
  6647. (setq empty-line-p (org-previous-line-empty-p)))
  6648. (match-string 0))
  6649. (error "* "))))
  6650. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6651. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6652. pos hide-previous previous-pos)
  6653. (cond
  6654. ;; At the beginning of a heading, open a new line for insertiong
  6655. ((and (bolp) (org-at-heading-p)
  6656. (not eops)
  6657. (or (bobp)
  6658. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6659. (open-line (if blank 2 1)))
  6660. (t
  6661. (save-excursion
  6662. (setq previous-pos (point-at-bol))
  6663. (end-of-line)
  6664. (setq hide-previous (outline-invisible-p)))
  6665. (and org-insert-heading-respect-content
  6666. (save-excursion
  6667. (while (outline-invisible-p)
  6668. (org-show-subtree)
  6669. (org-up-heading-safe))))
  6670. (let ((split
  6671. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6672. (save-excursion
  6673. (let ((p (point)))
  6674. (goto-char (point-at-bol))
  6675. (and (looking-at org-complex-heading-regexp)
  6676. (match-beginning 4)
  6677. (> p (match-beginning 4)))))))
  6678. tags pos)
  6679. (cond
  6680. ;; Insert a new line, possibly at end of parent subtree
  6681. (org-insert-heading-respect-content
  6682. (if (not eops)
  6683. (progn
  6684. (org-end-of-subtree nil t)
  6685. (and (looking-at "^\\*") (backward-char 1))
  6686. (while (and (not (bobp))
  6687. ;; Don't delete spaces in empty headlines
  6688. (not (looking-back org-outline-regexp))
  6689. (member (char-before) '(?\ ?\t ?\n)))
  6690. (backward-delete-char 1)))
  6691. (let ((p (point)))
  6692. (org-up-heading-safe)
  6693. (if (= p (point))
  6694. (goto-char (point-max))
  6695. (org-end-of-subtree nil t))))
  6696. (when (featurep 'org-inlinetask)
  6697. (while (and (not (eobp))
  6698. (looking-at "\\(\\*+\\)[ \t]+")
  6699. (>= (length (match-string 1))
  6700. org-inlinetask-min-level))
  6701. (org-end-of-subtree nil t)))
  6702. (or (bolp) (newline))
  6703. (or (org-previous-line-empty-p)
  6704. (and blank (newline)))
  6705. (if (or empty-line-p eops) (open-line 1)))
  6706. ;; Insert a headling containing text after point
  6707. ((org-at-heading-p)
  6708. (when hide-previous
  6709. (show-children)
  6710. (org-show-entry))
  6711. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6712. (setq tags (and (match-end 2) (match-string 2)))
  6713. (and (match-end 1)
  6714. (delete-region (match-beginning 1) (match-end 1)))
  6715. (setq pos (point-at-bol))
  6716. (or split (end-of-line 1))
  6717. (delete-horizontal-space)
  6718. (if (string-match "\\`\\*+\\'"
  6719. (buffer-substring (point-at-bol) (point)))
  6720. (insert " "))
  6721. (newline (if blank 2 1))
  6722. (when tags
  6723. (save-excursion
  6724. (goto-char pos)
  6725. (end-of-line 1)
  6726. (insert " " tags)
  6727. (org-set-tags nil 'align))))
  6728. (t
  6729. (or split (end-of-line 1))
  6730. (newline (if blank 2 1)))))))
  6731. (insert head) (just-one-space)
  6732. (setq pos (point))
  6733. (end-of-line 1)
  6734. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6735. (when (and org-insert-heading-respect-content hide-previous)
  6736. (save-excursion
  6737. (goto-char previous-pos)
  6738. (hide-subtree)))
  6739. (run-hooks 'org-insert-heading-hook)))))
  6740. (defun org-get-heading (&optional no-tags no-todo)
  6741. "Return the heading of the current entry, without the stars.
  6742. When NO-TAGS is non-nil, don't include tags.
  6743. When NO-TODO is non-nil, don't include TODO keywords."
  6744. (save-excursion
  6745. (org-back-to-heading t)
  6746. (cond
  6747. ((and no-tags no-todo)
  6748. (looking-at org-complex-heading-regexp)
  6749. (match-string 4))
  6750. (no-tags
  6751. (looking-at (concat org-outline-regexp
  6752. "\\(.*?\\)"
  6753. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6754. (match-string 1))
  6755. (no-todo
  6756. (looking-at org-todo-line-regexp)
  6757. (match-string 3))
  6758. (t (looking-at org-heading-regexp)
  6759. (match-string 2)))))
  6760. (defvar orgstruct-mode) ; defined below
  6761. (defun org-heading-components ()
  6762. "Return the components of the current heading.
  6763. This is a list with the following elements:
  6764. - the level as an integer
  6765. - the reduced level, different if `org-odd-levels-only' is set.
  6766. - the TODO keyword, or nil
  6767. - the priority character, like ?A, or nil if no priority is given
  6768. - the headline text itself, or the tags string if no headline text
  6769. - the tags string, or nil."
  6770. (save-excursion
  6771. (org-back-to-heading t)
  6772. (if (let (case-fold-search)
  6773. (looking-at
  6774. (if orgstruct-mode
  6775. org-heading-regexp
  6776. org-complex-heading-regexp)))
  6777. (if orgstruct-mode
  6778. (list (length (match-string 1))
  6779. (org-reduced-level (length (match-string 1)))
  6780. nil
  6781. nil
  6782. (match-string 2)
  6783. nil)
  6784. (list (length (match-string 1))
  6785. (org-reduced-level (length (match-string 1)))
  6786. (org-match-string-no-properties 2)
  6787. (and (match-end 3) (aref (match-string 3) 2))
  6788. (org-match-string-no-properties 4)
  6789. (org-match-string-no-properties 5))))))
  6790. (defun org-get-entry ()
  6791. "Get the entry text, after heading, entire subtree."
  6792. (save-excursion
  6793. (org-back-to-heading t)
  6794. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6795. (defun org-insert-heading-after-current ()
  6796. "Insert a new heading with same level as current, after current subtree."
  6797. (interactive)
  6798. (org-back-to-heading)
  6799. (org-insert-heading)
  6800. (org-move-subtree-down)
  6801. (end-of-line 1))
  6802. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6803. "Insert heading with `org-insert-heading-respect-content' set to t."
  6804. (interactive "P")
  6805. (let ((org-insert-heading-respect-content t))
  6806. (org-insert-heading arg invisible-ok)))
  6807. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6808. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6809. (interactive "P")
  6810. (let ((org-insert-heading-respect-content t))
  6811. (org-insert-todo-heading force-state t)))
  6812. (defun org-insert-todo-heading (arg &optional force-heading)
  6813. "Insert a new heading with the same level and TODO state as current heading.
  6814. If the heading has no TODO state, or if the state is DONE, use the first
  6815. state (TODO by default). Also one prefix arg, force first state. With two
  6816. prefix args, force inserting at the end of the parent subtree."
  6817. (interactive "P")
  6818. (when (or force-heading (not (org-insert-item 'checkbox)))
  6819. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6820. force-heading))
  6821. (save-excursion
  6822. (org-back-to-heading)
  6823. (outline-previous-heading)
  6824. (looking-at org-todo-line-regexp))
  6825. (let*
  6826. ((new-mark-x
  6827. (if (or arg
  6828. (not (match-beginning 2))
  6829. (member (match-string 2) org-done-keywords))
  6830. (car org-todo-keywords-1)
  6831. (match-string 2)))
  6832. (new-mark
  6833. (or
  6834. (run-hook-with-args-until-success
  6835. 'org-todo-get-default-hook new-mark-x nil)
  6836. new-mark-x)))
  6837. (beginning-of-line 1)
  6838. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6839. (if org-treat-insert-todo-heading-as-state-change
  6840. (org-todo new-mark)
  6841. (insert new-mark " "))))
  6842. (when org-provide-todo-statistics
  6843. (org-update-parent-todo-statistics))))
  6844. (defun org-insert-subheading (arg)
  6845. "Insert a new subheading and demote it.
  6846. Works for outline headings and for plain lists alike."
  6847. (interactive "P")
  6848. (org-insert-heading arg)
  6849. (cond
  6850. ((org-at-heading-p) (org-do-demote))
  6851. ((org-at-item-p) (org-indent-item))))
  6852. (defun org-insert-todo-subheading (arg)
  6853. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6854. Works for outline headings and for plain lists alike."
  6855. (interactive "P")
  6856. (org-insert-todo-heading arg)
  6857. (cond
  6858. ((org-at-heading-p) (org-do-demote))
  6859. ((org-at-item-p) (org-indent-item))))
  6860. ;;; Promotion and Demotion
  6861. (defvar org-after-demote-entry-hook nil
  6862. "Hook run after an entry has been demoted.
  6863. The cursor will be at the beginning of the entry.
  6864. When a subtree is being demoted, the hook will be called for each node.")
  6865. (defvar org-after-promote-entry-hook nil
  6866. "Hook run after an entry has been promoted.
  6867. The cursor will be at the beginning of the entry.
  6868. When a subtree is being promoted, the hook will be called for each node.")
  6869. (defun org-promote-subtree ()
  6870. "Promote the entire subtree.
  6871. See also `org-promote'."
  6872. (interactive)
  6873. (save-excursion
  6874. (org-with-limited-levels (org-map-tree 'org-promote)))
  6875. (org-fix-position-after-promote))
  6876. (defun org-demote-subtree ()
  6877. "Demote the entire subtree. See `org-demote'.
  6878. See also `org-promote'."
  6879. (interactive)
  6880. (save-excursion
  6881. (org-with-limited-levels (org-map-tree 'org-demote)))
  6882. (org-fix-position-after-promote))
  6883. (defun org-do-promote ()
  6884. "Promote the current heading higher up the tree.
  6885. If the region is active in `transient-mark-mode', promote all headings
  6886. in the region."
  6887. (interactive)
  6888. (save-excursion
  6889. (if (org-region-active-p)
  6890. (org-map-region 'org-promote (region-beginning) (region-end))
  6891. (org-promote)))
  6892. (org-fix-position-after-promote))
  6893. (defun org-do-demote ()
  6894. "Demote the current heading lower down the tree.
  6895. If the region is active in `transient-mark-mode', demote all headings
  6896. in the region."
  6897. (interactive)
  6898. (save-excursion
  6899. (if (org-region-active-p)
  6900. (org-map-region 'org-demote (region-beginning) (region-end))
  6901. (org-demote)))
  6902. (org-fix-position-after-promote))
  6903. (defun org-fix-position-after-promote ()
  6904. "Make sure that after pro/demotion cursor position is right."
  6905. (let ((pos (point)))
  6906. (when (save-excursion
  6907. (beginning-of-line 1)
  6908. (looking-at org-todo-line-regexp)
  6909. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6910. (cond ((eobp) (insert " "))
  6911. ((eolp) (insert " "))
  6912. ((equal (char-after) ?\ ) (forward-char 1))))))
  6913. (defun org-current-level ()
  6914. "Return the level of the current entry, or nil if before the first headline.
  6915. The level is the number of stars at the beginning of the headline."
  6916. (save-excursion
  6917. (org-with-limited-levels
  6918. (if (ignore-errors (org-back-to-heading t))
  6919. (funcall outline-level)))))
  6920. (defun org-get-previous-line-level ()
  6921. "Return the outline depth of the last headline before the current line.
  6922. Returns 0 for the first headline in the buffer, and nil if before the
  6923. first headline."
  6924. (let ((current-level (org-current-level))
  6925. (prev-level (when (> (line-number-at-pos) 1)
  6926. (save-excursion
  6927. (beginning-of-line 0)
  6928. (org-current-level)))))
  6929. (cond ((null current-level) nil) ; Before first headline
  6930. ((null prev-level) 0) ; At first headline
  6931. (prev-level))))
  6932. (defun org-reduced-level (l)
  6933. "Compute the effective level of a heading.
  6934. This takes into account the setting of `org-odd-levels-only'."
  6935. (cond
  6936. ((zerop l) 0)
  6937. (org-odd-levels-only (1+ (floor (/ l 2))))
  6938. (t l)))
  6939. (defun org-level-increment ()
  6940. "Return the number of stars that will be added or removed at a
  6941. time to headlines when structure editing, based on the value of
  6942. `org-odd-levels-only'."
  6943. (if org-odd-levels-only 2 1))
  6944. (defun org-get-valid-level (level &optional change)
  6945. "Rectify a level change under the influence of `org-odd-levels-only'
  6946. LEVEL is a current level, CHANGE is by how much the level should be
  6947. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6948. even level numbers will become the next higher odd number."
  6949. (if org-odd-levels-only
  6950. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6951. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6952. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6953. (max 1 (+ level (or change 0)))))
  6954. (if (boundp 'define-obsolete-function-alias)
  6955. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6956. (define-obsolete-function-alias 'org-get-legal-level
  6957. 'org-get-valid-level)
  6958. (define-obsolete-function-alias 'org-get-legal-level
  6959. 'org-get-valid-level "23.1")))
  6960. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6961. ;; ̀org-with-limited-levels'
  6962. (defun org-promote ()
  6963. "Promote the current heading higher up the tree.
  6964. If the region is active in `transient-mark-mode', promote all headings
  6965. in the region."
  6966. (org-back-to-heading t)
  6967. (let* ((level (save-match-data (funcall outline-level)))
  6968. (after-change-functions (remove 'flyspell-after-change-function
  6969. after-change-functions))
  6970. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6971. (diff (abs (- level (length up-head) -1))))
  6972. (cond ((and (= level 1) org-called-with-limited-levels
  6973. org-allow-promoting-top-level-subtree)
  6974. (replace-match "# " nil t))
  6975. ((= level 1)
  6976. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6977. (t (replace-match up-head nil t)))
  6978. ;; Fixup tag positioning
  6979. (unless (= level 1)
  6980. (and org-auto-align-tags (org-set-tags nil t))
  6981. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6982. (run-hooks 'org-after-promote-entry-hook)))
  6983. (defun org-demote ()
  6984. "Demote the current heading lower down the tree.
  6985. If the region is active in `transient-mark-mode', demote all headings
  6986. in the region."
  6987. (org-back-to-heading t)
  6988. (let* ((level (save-match-data (funcall outline-level)))
  6989. (after-change-functions (remove 'flyspell-after-change-function
  6990. after-change-functions))
  6991. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6992. (diff (abs (- level (length down-head) -1))))
  6993. (replace-match down-head nil t)
  6994. ;; Fixup tag positioning
  6995. (and org-auto-align-tags (org-set-tags nil t))
  6996. (if org-adapt-indentation (org-fixup-indentation diff))
  6997. (run-hooks 'org-after-demote-entry-hook)))
  6998. (defun org-cycle-level ()
  6999. "Cycle the level of an empty headline through possible states.
  7000. This goes first to child, then to parent, level, then up the hierarchy.
  7001. After top level, it switches back to sibling level."
  7002. (interactive)
  7003. (let ((org-adapt-indentation nil))
  7004. (when (org-point-at-end-of-empty-headline)
  7005. (setq this-command 'org-cycle-level) ; Only needed for caching
  7006. (let ((cur-level (org-current-level))
  7007. (prev-level (org-get-previous-line-level)))
  7008. (cond
  7009. ;; If first headline in file, promote to top-level.
  7010. ((= prev-level 0)
  7011. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7012. do (org-do-promote)))
  7013. ;; If same level as prev, demote one.
  7014. ((= prev-level cur-level)
  7015. (org-do-demote))
  7016. ;; If parent is top-level, promote to top level if not already.
  7017. ((= prev-level 1)
  7018. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7019. do (org-do-promote)))
  7020. ;; If top-level, return to prev-level.
  7021. ((= cur-level 1)
  7022. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7023. do (org-do-demote)))
  7024. ;; If less than prev-level, promote one.
  7025. ((< cur-level prev-level)
  7026. (org-do-promote))
  7027. ;; If deeper than prev-level, promote until higher than
  7028. ;; prev-level.
  7029. ((> cur-level prev-level)
  7030. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7031. do (org-do-promote))))
  7032. t))))
  7033. (defun org-map-tree (fun)
  7034. "Call FUN for every heading underneath the current one."
  7035. (org-back-to-heading)
  7036. (let ((level (funcall outline-level)))
  7037. (save-excursion
  7038. (funcall fun)
  7039. (while (and (progn
  7040. (outline-next-heading)
  7041. (> (funcall outline-level) level))
  7042. (not (eobp)))
  7043. (funcall fun)))))
  7044. (defun org-map-region (fun beg end)
  7045. "Call FUN for every heading between BEG and END."
  7046. (let ((org-ignore-region t))
  7047. (save-excursion
  7048. (setq end (copy-marker end))
  7049. (goto-char beg)
  7050. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7051. (< (point) end))
  7052. (funcall fun))
  7053. (while (and (progn
  7054. (outline-next-heading)
  7055. (< (point) end))
  7056. (not (eobp)))
  7057. (funcall fun)))))
  7058. (defvar org-property-end-re) ; silence byte-compiler
  7059. (defun org-fixup-indentation (diff)
  7060. "Change the indentation in the current entry by DIFF.
  7061. However, if any line in the current entry has no indentation, or if it
  7062. would end up with no indentation after the change, nothing at all is done."
  7063. (save-excursion
  7064. (let ((end (save-excursion (outline-next-heading)
  7065. (point-marker)))
  7066. (prohibit (if (> diff 0)
  7067. "^\\S-"
  7068. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7069. col)
  7070. (unless (save-excursion (end-of-line 1)
  7071. (re-search-forward prohibit end t))
  7072. (while (and (< (point) end)
  7073. (re-search-forward "^[ \t]+" end t))
  7074. (goto-char (match-end 0))
  7075. (setq col (current-column))
  7076. (if (< diff 0) (replace-match ""))
  7077. (org-indent-to-column (+ diff col))))
  7078. (move-marker end nil))))
  7079. (defun org-convert-to-odd-levels ()
  7080. "Convert an org-mode file with all levels allowed to one with odd levels.
  7081. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7082. level 5 etc."
  7083. (interactive)
  7084. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7085. (let ((outline-level 'org-outline-level)
  7086. (org-odd-levels-only nil) n)
  7087. (save-excursion
  7088. (goto-char (point-min))
  7089. (while (re-search-forward "^\\*\\*+ " nil t)
  7090. (setq n (- (length (match-string 0)) 2))
  7091. (while (>= (setq n (1- n)) 0)
  7092. (org-demote))
  7093. (end-of-line 1))))))
  7094. (defun org-convert-to-oddeven-levels ()
  7095. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7096. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7097. file contains a section with an even level, conversion would
  7098. destroy the structure of the file. An error is signaled in this
  7099. case."
  7100. (interactive)
  7101. (goto-char (point-min))
  7102. ;; First check if there are no even levels
  7103. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7104. (org-show-context t)
  7105. (error "Not all levels are odd in this file. Conversion not possible"))
  7106. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7107. (let ((outline-regexp org-outline-regexp)
  7108. (outline-level 'org-outline-level)
  7109. (org-odd-levels-only nil) n)
  7110. (save-excursion
  7111. (goto-char (point-min))
  7112. (while (re-search-forward "^\\*\\*+ " nil t)
  7113. (setq n (/ (1- (length (match-string 0))) 2))
  7114. (while (>= (setq n (1- n)) 0)
  7115. (org-promote))
  7116. (end-of-line 1))))))
  7117. (defun org-tr-level (n)
  7118. "Make N odd if required."
  7119. (if org-odd-levels-only (1+ (/ n 2)) n))
  7120. ;;; Vertical tree motion, cutting and pasting of subtrees
  7121. (defun org-move-subtree-up (&optional arg)
  7122. "Move the current subtree up past ARG headlines of the same level."
  7123. (interactive "p")
  7124. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7125. (defun org-move-subtree-down (&optional arg)
  7126. "Move the current subtree down past ARG headlines of the same level."
  7127. (interactive "p")
  7128. (setq arg (prefix-numeric-value arg))
  7129. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7130. 'org-get-last-sibling))
  7131. (ins-point (make-marker))
  7132. (cnt (abs arg))
  7133. (col (current-column))
  7134. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7135. ;; Select the tree
  7136. (org-back-to-heading)
  7137. (setq beg0 (point))
  7138. (save-excursion
  7139. (setq ne-beg (org-back-over-empty-lines))
  7140. (setq beg (point)))
  7141. (save-match-data
  7142. (save-excursion (outline-end-of-heading)
  7143. (setq folded (outline-invisible-p)))
  7144. (outline-end-of-subtree))
  7145. (outline-next-heading)
  7146. (setq ne-end (org-back-over-empty-lines))
  7147. (setq end (point))
  7148. (goto-char beg0)
  7149. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7150. ;; include less whitespace
  7151. (save-excursion
  7152. (goto-char beg)
  7153. (forward-line (- ne-beg ne-end))
  7154. (setq beg (point))))
  7155. ;; Find insertion point, with error handling
  7156. (while (> cnt 0)
  7157. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7158. (progn (goto-char beg0)
  7159. (user-error "Cannot move past superior level or buffer limit")))
  7160. (setq cnt (1- cnt)))
  7161. (if (> arg 0)
  7162. ;; Moving forward - still need to move over subtree
  7163. (progn (org-end-of-subtree t t)
  7164. (save-excursion
  7165. (org-back-over-empty-lines)
  7166. (or (bolp) (newline)))))
  7167. (setq ne-ins (org-back-over-empty-lines))
  7168. (move-marker ins-point (point))
  7169. (setq txt (buffer-substring beg end))
  7170. (org-save-markers-in-region beg end)
  7171. (delete-region beg end)
  7172. (org-remove-empty-overlays-at beg)
  7173. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7174. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7175. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7176. (let ((bbb (point)))
  7177. (insert-before-markers txt)
  7178. (org-reinstall-markers-in-region bbb)
  7179. (move-marker ins-point bbb))
  7180. (or (bolp) (insert "\n"))
  7181. (setq ins-end (point))
  7182. (goto-char ins-point)
  7183. (org-skip-whitespace)
  7184. (when (and (< arg 0)
  7185. (org-first-sibling-p)
  7186. (> ne-ins ne-beg))
  7187. ;; Move whitespace back to beginning
  7188. (save-excursion
  7189. (goto-char ins-end)
  7190. (let ((kill-whole-line t))
  7191. (kill-line (- ne-ins ne-beg)) (point)))
  7192. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7193. (move-marker ins-point nil)
  7194. (if folded
  7195. (hide-subtree)
  7196. (org-show-entry)
  7197. (show-children)
  7198. (org-cycle-hide-drawers 'children))
  7199. (org-clean-visibility-after-subtree-move)
  7200. ;; move back to the initial column we were at
  7201. (move-to-column col)))
  7202. (defvar org-subtree-clip ""
  7203. "Clipboard for cut and paste of subtrees.
  7204. This is actually only a copy of the kill, because we use the normal kill
  7205. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7206. (defvar org-subtree-clip-folded nil
  7207. "Was the last copied subtree folded?
  7208. This is used to fold the tree back after pasting.")
  7209. (defun org-cut-subtree (&optional n)
  7210. "Cut the current subtree into the clipboard.
  7211. With prefix arg N, cut this many sequential subtrees.
  7212. This is a short-hand for marking the subtree and then cutting it."
  7213. (interactive "p")
  7214. (org-copy-subtree n 'cut))
  7215. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7216. "Cut the current subtree into the clipboard.
  7217. With prefix arg N, cut this many sequential subtrees.
  7218. This is a short-hand for marking the subtree and then copying it.
  7219. If CUT is non-nil, actually cut the subtree.
  7220. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7221. of some markers in the region, even if CUT is non-nil. This is
  7222. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7223. (interactive "p")
  7224. (let (beg end folded (beg0 (point)))
  7225. (if (org-called-interactively-p 'any)
  7226. (org-back-to-heading nil) ; take what looks like a subtree
  7227. (org-back-to-heading t)) ; take what is really there
  7228. (setq beg (point))
  7229. (skip-chars-forward " \t\r\n")
  7230. (save-match-data
  7231. (if nosubtrees
  7232. (outline-next-heading)
  7233. (save-excursion (outline-end-of-heading)
  7234. (setq folded (outline-invisible-p)))
  7235. (condition-case nil
  7236. (org-forward-heading-same-level (1- n) t)
  7237. (error nil))
  7238. (org-end-of-subtree t t)))
  7239. (setq end (point))
  7240. (goto-char beg0)
  7241. (when (> end beg)
  7242. (setq org-subtree-clip-folded folded)
  7243. (when (or cut force-store-markers)
  7244. (org-save-markers-in-region beg end))
  7245. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7246. (setq org-subtree-clip (current-kill 0))
  7247. (message "%s: Subtree(s) with %d characters"
  7248. (if cut "Cut" "Copied")
  7249. (length org-subtree-clip)))))
  7250. (defun org-paste-subtree (&optional level tree for-yank)
  7251. "Paste the clipboard as a subtree, with modification of headline level.
  7252. The entire subtree is promoted or demoted in order to match a new headline
  7253. level.
  7254. If the cursor is at the beginning of a headline, the same level as
  7255. that headline is used to paste the tree.
  7256. If not, the new level is derived from the *visible* headings
  7257. before and after the insertion point, and taken to be the inferior headline
  7258. level of the two. So if the previous visible heading is level 3 and the
  7259. next is level 4 (or vice versa), level 4 will be used for insertion.
  7260. This makes sure that the subtree remains an independent subtree and does
  7261. not swallow low level entries.
  7262. You can also force a different level, either by using a numeric prefix
  7263. argument, or by inserting the heading marker by hand. For example, if the
  7264. cursor is after \"*****\", then the tree will be shifted to level 5.
  7265. If optional TREE is given, use this text instead of the kill ring.
  7266. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7267. move back over whitespace before inserting, and move point to the end of
  7268. the inserted text when done."
  7269. (interactive "P")
  7270. (setq tree (or tree (and kill-ring (current-kill 0))))
  7271. (unless (org-kill-is-subtree-p tree)
  7272. (error "%s"
  7273. (substitute-command-keys
  7274. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7275. (org-with-limited-levels
  7276. (let* ((visp (not (outline-invisible-p)))
  7277. (txt tree)
  7278. (^re_ "\\(\\*+\\)[ \t]*")
  7279. (old-level (if (string-match org-outline-regexp-bol txt)
  7280. (- (match-end 0) (match-beginning 0) 1)
  7281. -1))
  7282. (force-level (cond (level (prefix-numeric-value level))
  7283. ((and (looking-at "[ \t]*$")
  7284. (string-match
  7285. "^\\*+$" (buffer-substring
  7286. (point-at-bol) (point))))
  7287. (- (match-end 1) (match-beginning 1)))
  7288. ((and (bolp)
  7289. (looking-at org-outline-regexp))
  7290. (- (match-end 0) (point) 1))))
  7291. (previous-level (save-excursion
  7292. (condition-case nil
  7293. (progn
  7294. (outline-previous-visible-heading 1)
  7295. (if (looking-at ^re_)
  7296. (- (match-end 0) (match-beginning 0) 1)
  7297. 1))
  7298. (error 1))))
  7299. (next-level (save-excursion
  7300. (condition-case nil
  7301. (progn
  7302. (or (looking-at org-outline-regexp)
  7303. (outline-next-visible-heading 1))
  7304. (if (looking-at ^re_)
  7305. (- (match-end 0) (match-beginning 0) 1)
  7306. 1))
  7307. (error 1))))
  7308. (new-level (or force-level (max previous-level next-level)))
  7309. (shift (if (or (= old-level -1)
  7310. (= new-level -1)
  7311. (= old-level new-level))
  7312. 0
  7313. (- new-level old-level)))
  7314. (delta (if (> shift 0) -1 1))
  7315. (func (if (> shift 0) 'org-demote 'org-promote))
  7316. (org-odd-levels-only nil)
  7317. beg end newend)
  7318. ;; Remove the forced level indicator
  7319. (if force-level
  7320. (delete-region (point-at-bol) (point)))
  7321. ;; Paste
  7322. (beginning-of-line (if (bolp) 1 2))
  7323. (setq beg (point))
  7324. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7325. (insert-before-markers txt)
  7326. (unless (string-match "\n\\'" txt) (insert "\n"))
  7327. (setq newend (point))
  7328. (org-reinstall-markers-in-region beg)
  7329. (setq end (point))
  7330. (goto-char beg)
  7331. (skip-chars-forward " \t\n\r")
  7332. (setq beg (point))
  7333. (if (and (outline-invisible-p) visp)
  7334. (save-excursion (outline-show-heading)))
  7335. ;; Shift if necessary
  7336. (unless (= shift 0)
  7337. (save-restriction
  7338. (narrow-to-region beg end)
  7339. (while (not (= shift 0))
  7340. (org-map-region func (point-min) (point-max))
  7341. (setq shift (+ delta shift)))
  7342. (goto-char (point-min))
  7343. (setq newend (point-max))))
  7344. (when (or (org-called-interactively-p 'interactive) for-yank)
  7345. (message "Clipboard pasted as level %d subtree" new-level))
  7346. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7347. kill-ring
  7348. (eq org-subtree-clip (current-kill 0))
  7349. org-subtree-clip-folded)
  7350. ;; The tree was folded before it was killed/copied
  7351. (hide-subtree))
  7352. (and for-yank (goto-char newend)))))
  7353. (defun org-kill-is-subtree-p (&optional txt)
  7354. "Check if the current kill is an outline subtree, or a set of trees.
  7355. Returns nil if kill does not start with a headline, or if the first
  7356. headline level is not the largest headline level in the tree.
  7357. So this will actually accept several entries of equal levels as well,
  7358. which is OK for `org-paste-subtree'.
  7359. If optional TXT is given, check this string instead of the current kill."
  7360. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7361. (re (org-get-limited-outline-regexp))
  7362. (^re (concat "^" re))
  7363. (start-level (and kill
  7364. (string-match
  7365. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7366. kill)
  7367. (- (match-end 2) (match-beginning 2) 1)))
  7368. (start (1+ (or (match-beginning 2) -1))))
  7369. (if (not start-level)
  7370. (progn
  7371. nil) ;; does not even start with a heading
  7372. (catch 'exit
  7373. (while (setq start (string-match ^re kill (1+ start)))
  7374. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7375. (throw 'exit nil)))
  7376. t))))
  7377. (defvar org-markers-to-move nil
  7378. "Markers that should be moved with a cut-and-paste operation.
  7379. Those markers are stored together with their positions relative to
  7380. the start of the region.")
  7381. (defun org-save-markers-in-region (beg end)
  7382. "Check markers in region.
  7383. If these markers are between BEG and END, record their position relative
  7384. to BEG, so that after moving the block of text, we can put the markers back
  7385. into place.
  7386. This function gets called just before an entry or tree gets cut from the
  7387. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7388. called immediately, to move the markers with the entries."
  7389. (setq org-markers-to-move nil)
  7390. (when (featurep 'org-clock)
  7391. (org-clock-save-markers-for-cut-and-paste beg end))
  7392. (when (featurep 'org-agenda)
  7393. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7394. (defun org-check-and-save-marker (marker beg end)
  7395. "Check if MARKER is between BEG and END.
  7396. If yes, remember the marker and the distance to BEG."
  7397. (when (and (marker-buffer marker)
  7398. (equal (marker-buffer marker) (current-buffer)))
  7399. (if (and (>= marker beg) (< marker end))
  7400. (push (cons marker (- marker beg)) org-markers-to-move))))
  7401. (defun org-reinstall-markers-in-region (beg)
  7402. "Move all remembered markers to their position relative to BEG."
  7403. (mapc (lambda (x)
  7404. (move-marker (car x) (+ beg (cdr x))))
  7405. org-markers-to-move)
  7406. (setq org-markers-to-move nil))
  7407. (defun org-narrow-to-subtree ()
  7408. "Narrow buffer to the current subtree."
  7409. (interactive)
  7410. (save-excursion
  7411. (save-match-data
  7412. (org-with-limited-levels
  7413. (narrow-to-region
  7414. (progn (org-back-to-heading t) (point))
  7415. (progn (org-end-of-subtree t t)
  7416. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7417. (point)))))))
  7418. (defun org-narrow-to-block ()
  7419. "Narrow buffer to the current block."
  7420. (interactive)
  7421. (let* ((case-fold-search t)
  7422. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7423. "^[ \t]*#\\+end_.*")))
  7424. (if blockp
  7425. (narrow-to-region (car blockp) (cdr blockp))
  7426. (error "Not in a block"))))
  7427. (eval-when-compile
  7428. (defvar org-property-drawer-re))
  7429. (defvar org-property-start-re) ;; defined below
  7430. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7431. "Clone the task (subtree) at point N times.
  7432. The clones will be inserted as siblings.
  7433. In interactive use, the user will be prompted for the number of
  7434. clones to be produced, and for a time SHIFT, which may be a
  7435. repeater as used in time stamps, for example `+3d'.
  7436. When a valid repeater is given and the entry contains any time
  7437. stamps, the clones will become a sequence in time, with time
  7438. stamps in the subtree shifted for each clone produced. If SHIFT
  7439. is nil or the empty string, time stamps will be left alone. The
  7440. ID property of the original subtree is removed.
  7441. If the original subtree did contain time stamps with a repeater,
  7442. the following will happen:
  7443. - the repeater will be removed in each clone
  7444. - an additional clone will be produced, with the current, unshifted
  7445. date(s) in the entry.
  7446. - the original entry will be placed *after* all the clones, with
  7447. repeater intact.
  7448. - the start days in the repeater in the original entry will be shifted
  7449. to past the last clone.
  7450. In this way you can spell out a number of instances of a repeating task,
  7451. and still retain the repeater to cover future instances of the task."
  7452. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7453. (let (beg end template task idprop
  7454. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7455. (drawer-re org-drawer-regexp))
  7456. (if (not (and (integerp n) (> n 0)))
  7457. (error "Invalid number of replications %s" n))
  7458. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7459. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7460. shift)))
  7461. (error "Invalid shift specification %s" shift))
  7462. (when doshift
  7463. (setq shift-n (string-to-number (match-string 1 shift))
  7464. shift-what (cdr (assoc (match-string 2 shift)
  7465. '(("d" . day) ("w" . week)
  7466. ("m" . month) ("y" . year))))))
  7467. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7468. (setq nmin 1 nmax n)
  7469. (org-back-to-heading t)
  7470. (setq beg (point))
  7471. (setq idprop (org-entry-get nil "ID"))
  7472. (org-end-of-subtree t t)
  7473. (or (bolp) (insert "\n"))
  7474. (setq end (point))
  7475. (setq template (buffer-substring beg end))
  7476. (when (and doshift
  7477. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7478. (delete-region beg end)
  7479. (setq end beg)
  7480. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7481. (goto-char end)
  7482. (loop for n from nmin to nmax do
  7483. ;; prepare clone
  7484. (with-temp-buffer
  7485. (insert template)
  7486. (org-mode)
  7487. (goto-char (point-min))
  7488. (org-show-subtree)
  7489. (and idprop (if org-clone-delete-id
  7490. (org-entry-delete nil "ID")
  7491. (org-id-get-create t)))
  7492. (unless (= n 0)
  7493. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7494. (kill-whole-line))
  7495. (goto-char (point-min))
  7496. (while (re-search-forward drawer-re nil t)
  7497. (mapc (lambda (d)
  7498. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7499. (goto-char (point-min))
  7500. (when doshift
  7501. (while (re-search-forward org-ts-regexp-both nil t)
  7502. (org-timestamp-change (* n shift-n) shift-what))
  7503. (unless (= n n-no-remove)
  7504. (goto-char (point-min))
  7505. (while (re-search-forward org-ts-regexp nil t)
  7506. (save-excursion
  7507. (goto-char (match-beginning 0))
  7508. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7509. (delete-region (match-beginning 1) (match-end 1)))))))
  7510. (setq task (buffer-string)))
  7511. (insert task))
  7512. (goto-char beg)))
  7513. ;;; Outline Sorting
  7514. (defun org-sort (with-case)
  7515. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7516. Optional argument WITH-CASE means sort case-sensitively."
  7517. (interactive "P")
  7518. (cond
  7519. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7520. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7521. (t
  7522. (org-call-with-arg 'org-sort-entries with-case))))
  7523. (defun org-sort-remove-invisible (s)
  7524. "Remove invisible links from string S."
  7525. (remove-text-properties 0 (length s) org-rm-props s)
  7526. (while (string-match org-bracket-link-regexp s)
  7527. (setq s (replace-match (if (match-end 2)
  7528. (match-string 3 s)
  7529. (match-string 1 s)) t t s)))
  7530. (let ((st (format " %s " s)))
  7531. (while (string-match org-emph-re st)
  7532. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7533. (setq s (substring st 1 -1)))
  7534. s)
  7535. (defvar org-priority-regexp) ; defined later in the file
  7536. (defvar org-after-sorting-entries-or-items-hook nil
  7537. "Hook that is run after a bunch of entries or items have been sorted.
  7538. When children are sorted, the cursor is in the parent line when this
  7539. hook gets called. When a region or a plain list is sorted, the cursor
  7540. will be in the first entry of the sorted region/list.")
  7541. (defun org-sort-entries
  7542. (&optional with-case sorting-type getkey-func compare-func property)
  7543. "Sort entries on a certain level of an outline tree.
  7544. If there is an active region, the entries in the region are sorted.
  7545. Else, if the cursor is before the first entry, sort the top-level items.
  7546. Else, the children of the entry at point are sorted.
  7547. Sorting can be alphabetically, numerically, by date/time as given by
  7548. a time stamp, by a property, by priority order, or by a custom function.
  7549. The command prompts for the sorting type unless it has been given to the
  7550. function through the SORTING-TYPE argument, which needs to be a character,
  7551. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7552. precise meaning of each character:
  7553. n Numerically, by converting the beginning of the entry/item to a number.
  7554. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7555. o By order of TODO keywords.
  7556. t By date/time, either the first active time stamp in the entry, or, if
  7557. none exist, by the first inactive one.
  7558. s By the scheduled date/time.
  7559. d By deadline date/time.
  7560. c By creation time, which is assumed to be the first inactive time stamp
  7561. at the beginning of a line.
  7562. p By priority according to the cookie.
  7563. r By the value of a property.
  7564. Capital letters will reverse the sort order.
  7565. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7566. called with point at the beginning of the record. It must return either
  7567. a string or a number that should serve as the sorting key for that record.
  7568. Comparing entries ignores case by default. However, with an optional argument
  7569. WITH-CASE, the sorting considers case as well.
  7570. Sorting is done against the visible part of the headlines, it ignores hidden
  7571. links."
  7572. (interactive "P")
  7573. (let ((case-func (if with-case 'identity 'downcase))
  7574. (cmstr
  7575. ;; The clock marker is lost when using `sort-subr', let's
  7576. ;; store the clocking string.
  7577. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7578. (save-excursion
  7579. (goto-char org-clock-marker)
  7580. (looking-back "^.*") (match-string-no-properties 0))))
  7581. start beg end stars re re2
  7582. txt what tmp)
  7583. ;; Find beginning and end of region to sort
  7584. (cond
  7585. ((org-region-active-p)
  7586. ;; we will sort the region
  7587. (setq end (region-end)
  7588. what "region")
  7589. (goto-char (region-beginning))
  7590. (if (not (org-at-heading-p)) (outline-next-heading))
  7591. (setq start (point)))
  7592. ((or (org-at-heading-p)
  7593. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7594. ;; we will sort the children of the current headline
  7595. (org-back-to-heading)
  7596. (setq start (point)
  7597. end (progn (org-end-of-subtree t t)
  7598. (or (bolp) (insert "\n"))
  7599. (org-back-over-empty-lines)
  7600. (point))
  7601. what "children")
  7602. (goto-char start)
  7603. (show-subtree)
  7604. (outline-next-heading))
  7605. (t
  7606. ;; we will sort the top-level entries in this file
  7607. (goto-char (point-min))
  7608. (or (org-at-heading-p) (outline-next-heading))
  7609. (setq start (point))
  7610. (goto-char (point-max))
  7611. (beginning-of-line 1)
  7612. (when (looking-at ".*?\\S-")
  7613. ;; File ends in a non-white line
  7614. (end-of-line 1)
  7615. (insert "\n"))
  7616. (setq end (point-max))
  7617. (setq what "top-level")
  7618. (goto-char start)
  7619. (show-all)))
  7620. (setq beg (point))
  7621. (if (>= beg end) (error "Nothing to sort"))
  7622. (looking-at "\\(\\*+\\)")
  7623. (setq stars (match-string 1)
  7624. re (concat "^" (regexp-quote stars) " +")
  7625. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7626. txt (buffer-substring beg end))
  7627. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7628. (if (and (not (equal stars "*")) (string-match re2 txt))
  7629. (error "Region to sort contains a level above the first entry"))
  7630. (unless sorting-type
  7631. (message
  7632. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7633. [t]ime [s]cheduled [d]eadline [c]reated
  7634. A/N/P/R/O/F/T/S/D/C means reversed:"
  7635. what)
  7636. (setq sorting-type (read-char-exclusive))
  7637. (unless getkey-func
  7638. (and (= (downcase sorting-type) ?f)
  7639. (setq getkey-func
  7640. (org-icompleting-read "Sort using function: "
  7641. obarray 'fboundp t nil nil))
  7642. (setq getkey-func (intern getkey-func))))
  7643. (and (= (downcase sorting-type) ?r)
  7644. (not property)
  7645. (setq property
  7646. (org-icompleting-read "Property: "
  7647. (mapcar 'list (org-buffer-property-keys t))
  7648. nil t))))
  7649. (message "Sorting entries...")
  7650. (save-restriction
  7651. (narrow-to-region start end)
  7652. (let ((dcst (downcase sorting-type))
  7653. (case-fold-search nil)
  7654. (now (current-time)))
  7655. (sort-subr
  7656. (/= dcst sorting-type)
  7657. ;; This function moves to the beginning character of the "record" to
  7658. ;; be sorted.
  7659. (lambda nil
  7660. (if (re-search-forward re nil t)
  7661. (goto-char (match-beginning 0))
  7662. (goto-char (point-max))))
  7663. ;; This function moves to the last character of the "record" being
  7664. ;; sorted.
  7665. (lambda nil
  7666. (save-match-data
  7667. (condition-case nil
  7668. (outline-forward-same-level 1)
  7669. (error
  7670. (goto-char (point-max))))))
  7671. ;; This function returns the value that gets sorted against.
  7672. (lambda nil
  7673. (cond
  7674. ((= dcst ?n)
  7675. (if (looking-at org-complex-heading-regexp)
  7676. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7677. nil))
  7678. ((= dcst ?a)
  7679. (if (looking-at org-complex-heading-regexp)
  7680. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7681. nil))
  7682. ((= dcst ?t)
  7683. (let ((end (save-excursion (outline-next-heading) (point))))
  7684. (if (or (re-search-forward org-ts-regexp end t)
  7685. (re-search-forward org-ts-regexp-both end t))
  7686. (org-time-string-to-seconds (match-string 0))
  7687. (org-float-time now))))
  7688. ((= dcst ?c)
  7689. (let ((end (save-excursion (outline-next-heading) (point))))
  7690. (if (re-search-forward
  7691. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7692. end t)
  7693. (org-time-string-to-seconds (match-string 0))
  7694. (org-float-time now))))
  7695. ((= dcst ?s)
  7696. (let ((end (save-excursion (outline-next-heading) (point))))
  7697. (if (re-search-forward org-scheduled-time-regexp end t)
  7698. (org-time-string-to-seconds (match-string 1))
  7699. (org-float-time now))))
  7700. ((= dcst ?d)
  7701. (let ((end (save-excursion (outline-next-heading) (point))))
  7702. (if (re-search-forward org-deadline-time-regexp end t)
  7703. (org-time-string-to-seconds (match-string 1))
  7704. (org-float-time now))))
  7705. ((= dcst ?p)
  7706. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7707. (string-to-char (match-string 2))
  7708. org-default-priority))
  7709. ((= dcst ?r)
  7710. (or (org-entry-get nil property) ""))
  7711. ((= dcst ?o)
  7712. (if (looking-at org-complex-heading-regexp)
  7713. (- 9999 (length (member (match-string 2)
  7714. org-todo-keywords-1)))))
  7715. ((= dcst ?f)
  7716. (if getkey-func
  7717. (progn
  7718. (setq tmp (funcall getkey-func))
  7719. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7720. tmp)
  7721. (error "Invalid key function `%s'" getkey-func)))
  7722. (t (error "Invalid sorting type `%c'" sorting-type))))
  7723. nil
  7724. (cond
  7725. ((= dcst ?a) 'string<)
  7726. ((= dcst ?f) compare-func)
  7727. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7728. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7729. ;; Reset the clock marker if needed
  7730. (when cmstr
  7731. (save-excursion
  7732. (goto-char start)
  7733. (search-forward cmstr nil t)
  7734. (move-marker org-clock-marker (point))))
  7735. (message "Sorting entries...done")))
  7736. (defun org-do-sort (table what &optional with-case sorting-type)
  7737. "Sort TABLE of WHAT according to SORTING-TYPE.
  7738. The user will be prompted for the SORTING-TYPE if the call to this
  7739. function does not specify it. WHAT is only for the prompt, to indicate
  7740. what is being sorted. The sorting key will be extracted from
  7741. the car of the elements of the table.
  7742. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7743. (unless sorting-type
  7744. (message
  7745. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7746. what)
  7747. (setq sorting-type (read-char-exclusive)))
  7748. (let ((dcst (downcase sorting-type))
  7749. extractfun comparefun)
  7750. ;; Define the appropriate functions
  7751. (cond
  7752. ((= dcst ?n)
  7753. (setq extractfun 'string-to-number
  7754. comparefun (if (= dcst sorting-type) '< '>)))
  7755. ((= dcst ?a)
  7756. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7757. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7758. comparefun (if (= dcst sorting-type)
  7759. 'string<
  7760. (lambda (a b) (and (not (string< a b))
  7761. (not (string= a b)))))))
  7762. ((= dcst ?t)
  7763. (setq extractfun
  7764. (lambda (x)
  7765. (if (or (string-match org-ts-regexp x)
  7766. (string-match org-ts-regexp-both x))
  7767. (org-float-time
  7768. (org-time-string-to-time (match-string 0 x)))
  7769. 0))
  7770. comparefun (if (= dcst sorting-type) '< '>)))
  7771. (t (error "Invalid sorting type `%c'" sorting-type)))
  7772. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7773. table)
  7774. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7775. ;;; The orgstruct minor mode
  7776. ;; Define a minor mode which can be used in other modes in order to
  7777. ;; integrate the org-mode structure editing commands.
  7778. ;; This is really a hack, because the org-mode structure commands use
  7779. ;; keys which normally belong to the major mode. Here is how it
  7780. ;; works: The minor mode defines all the keys necessary to operate the
  7781. ;; structure commands, but wraps the commands into a function which
  7782. ;; tests if the cursor is currently at a headline or a plain list
  7783. ;; item. If that is the case, the structure command is used,
  7784. ;; temporarily setting many Org-mode variables like regular
  7785. ;; expressions for filling etc. However, when any of those keys is
  7786. ;; used at a different location, function uses `key-binding' to look
  7787. ;; up if the key has an associated command in another currently active
  7788. ;; keymap (minor modes, major mode, global), and executes that
  7789. ;; command. There might be problems if any of the keys is otherwise
  7790. ;; used as a prefix key.
  7791. (defcustom orgstruct-heading-prefix-regexp ""
  7792. "Regexp that matches the custom prefix of Org headlines in
  7793. orgstruct(++)-mode."
  7794. :group 'org
  7795. :version "24.4"
  7796. :package-version '(Org . "8.0")
  7797. :type 'string)
  7798. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7799. (defcustom orgstruct-setup-hook nil
  7800. "Hook run after orgstruct-mode-map is filled."
  7801. :group 'org
  7802. :version "24.4"
  7803. :package-version '(Org . "8.0")
  7804. :type 'hook)
  7805. (defvar orgstruct-initialized nil)
  7806. (defvar org-local-vars nil
  7807. "List of local variables, for use by `orgstruct-mode'.")
  7808. ;;;###autoload
  7809. (define-minor-mode orgstruct-mode
  7810. "Toggle the minor mode `orgstruct-mode'.
  7811. This mode is for using Org-mode structure commands in other
  7812. modes. The following keys behave as if Org-mode were active, if
  7813. the cursor is on a headline, or on a plain list item (both as
  7814. defined by Org-mode)."
  7815. nil " OrgStruct" (make-sparse-keymap)
  7816. (when orgstruct-mode
  7817. (org-load-modules-maybe)
  7818. (unless orgstruct-initialized
  7819. (orgstruct-setup)
  7820. (setq orgstruct-initialized t))))
  7821. ;;;###autoload
  7822. (defun turn-on-orgstruct ()
  7823. "Unconditionally turn on `orgstruct-mode'."
  7824. (orgstruct-mode 1))
  7825. (defvar org-fb-vars nil)
  7826. (make-variable-buffer-local 'org-fb-vars)
  7827. (defun orgstruct++-mode (&optional arg)
  7828. "Toggle `orgstruct-mode', the enhanced version of it.
  7829. In addition to setting orgstruct-mode, this also exports all
  7830. indentation and autofilling variables from org-mode into the
  7831. buffer. It will also recognize item context in multiline items."
  7832. (interactive "P")
  7833. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7834. (if (< arg 1)
  7835. (progn (orgstruct-mode -1)
  7836. (mapc (lambda(v)
  7837. (org-set-local (car v)
  7838. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7839. org-fb-vars))
  7840. (orgstruct-mode 1)
  7841. (setq org-fb-vars nil)
  7842. (let (var val)
  7843. (mapc
  7844. (lambda (x)
  7845. (when (string-match
  7846. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7847. (symbol-name (car x)))
  7848. (setq var (car x) val (nth 1 x))
  7849. (push (list var `(quote ,(eval var))) org-fb-vars)
  7850. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7851. org-local-vars)
  7852. (org-set-local 'orgstruct-is-++ t))))
  7853. (defvar orgstruct-is-++ nil
  7854. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7855. (make-variable-buffer-local 'orgstruct-is-++)
  7856. ;;;###autoload
  7857. (defun turn-on-orgstruct++ ()
  7858. "Unconditionally turn on `orgstruct++-mode'."
  7859. (orgstruct++-mode 1))
  7860. (defun orgstruct-error ()
  7861. "Error when there is no default binding for a structure key."
  7862. (interactive)
  7863. (error "This key has no function outside structure elements"))
  7864. (defun orgstruct-setup ()
  7865. "Setup orgstruct keymap."
  7866. (dolist (f
  7867. '("org-meta"
  7868. "org-shift"
  7869. "org-shiftmeta"
  7870. org-shifttab
  7871. org-backward-element
  7872. org-backward-heading-same-level
  7873. org-ctrl-c-ret
  7874. org-cycle
  7875. org-forward-heading-same-level
  7876. org-insert-heading
  7877. org-insert-heading-respect-content
  7878. org-kill-note-or-show-branches
  7879. org-mark-subtree
  7880. org-narrow-to-subtree
  7881. org-promote-subtree
  7882. org-reveal
  7883. org-show-subtree
  7884. org-sort
  7885. org-up-element
  7886. outline-demote
  7887. outline-next-visible-heading
  7888. outline-previous-visible-heading
  7889. outline-promote
  7890. outline-up-heading
  7891. show-children))
  7892. (dolist (f (if (stringp f)
  7893. (let ((flist))
  7894. (dolist (postfix
  7895. '("-return" "tab" "left" "right" "up" "down")
  7896. flist)
  7897. (let ((f (intern (concat f postfix))))
  7898. (when (fboundp f)
  7899. (push f flist)))))
  7900. (list f)))
  7901. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7902. (where-is-internal f outline-mode-map)))
  7903. ;; TODO use local-function-key-map
  7904. (dolist (rep '(("<tab>" . "TAB")
  7905. ("<return>" . "RET")
  7906. ("<escape>" . "ESC")
  7907. ("<delete>" . "DEL")))
  7908. (setq binding (read-kbd-macro (replace-regexp-in-string
  7909. (regexp-quote (car rep))
  7910. (cdr rep)
  7911. (key-description binding)))))
  7912. (let ((key (lookup-key orgstruct-mode-map binding)))
  7913. (when (or (not key) (numberp key))
  7914. (condition-case nil
  7915. (org-defkey orgstruct-mode-map
  7916. binding
  7917. (orgstruct-make-binding f binding))
  7918. (error nil)))))))
  7919. (run-hooks 'orgstruct-setup-hook))
  7920. (defun orgstruct-make-binding (fun key)
  7921. "Create a function for binding in the structure minor mode.
  7922. FUN is the command to call inside a table. KEY is the key that
  7923. should be checked in for a command to execute outside of tables."
  7924. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7925. (let ((nname name)
  7926. (i 0))
  7927. (while (fboundp (intern nname))
  7928. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7929. (setq name (intern nname)))
  7930. (eval
  7931. (let ((bindings '((org-heading-regexp
  7932. (concat "^"
  7933. orgstruct-heading-prefix-regexp
  7934. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7935. (org-outline-regexp
  7936. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7937. (org-outline-regexp-bol
  7938. (concat "^" org-outline-regexp))
  7939. (outline-regexp org-outline-regexp)
  7940. (outline-heading-end-regexp "\n")
  7941. (outline-level 'outline-level)
  7942. (outline-heading-alist))))
  7943. `(defun ,name (arg)
  7944. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7945. "Outside of structure, run the binding of `"
  7946. (key-description key) "'.")
  7947. (interactive "p")
  7948. (unless
  7949. (let* ,bindings
  7950. (when (org-context-p 'headline 'item
  7951. ,(when (memq fun '(org-insert-heading))
  7952. '(when orgstruct-is-++
  7953. 'item-body)))
  7954. (org-run-like-in-org-mode
  7955. (lambda ()
  7956. (interactive)
  7957. (let* ,bindings
  7958. (call-interactively ',fun))))
  7959. t))
  7960. (let* ((orgstruct-mode)
  7961. (binding (key-binding ,key)))
  7962. (if (keymapp binding)
  7963. (set-temporary-overlay-map binding)
  7964. (call-interactively
  7965. (or binding 'orgstruct-error))))))))
  7966. name))
  7967. (defun org-contextualize-keys (alist contexts)
  7968. "Return valid elements in ALIST depending on CONTEXTS.
  7969. `org-agenda-custom-commands' or `org-capture-templates' are the
  7970. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7971. or `org-capture-templates-contexts' are the associated contexts
  7972. definitions."
  7973. (let ((contexts
  7974. ;; normalize contexts
  7975. (mapcar
  7976. (lambda(c) (cond ((listp (cadr c))
  7977. (list (car c) (car c) (cadr c)))
  7978. ((string= "" (cadr c))
  7979. (list (car c) (car c) (caddr c)))
  7980. (t c))) contexts))
  7981. (a alist) c r s)
  7982. ;; loop over all commands or templates
  7983. (while (setq c (pop a))
  7984. (let (vrules repl)
  7985. (cond
  7986. ((not (assoc (car c) contexts))
  7987. (push c r))
  7988. ((and (assoc (car c) contexts)
  7989. (setq vrules (org-contextualize-validate-key
  7990. (car c) contexts)))
  7991. (mapc (lambda (vr)
  7992. (when (not (equal (car vr) (cadr vr)))
  7993. (setq repl vr))) vrules)
  7994. (if (not repl) (push c r)
  7995. (push (cadr repl) s)
  7996. (push
  7997. (cons (car c)
  7998. (cdr (or (assoc (cadr repl) alist)
  7999. (error "Undefined key `%s' as contextual replacement for `%s'"
  8000. (cadr repl) (car c)))))
  8001. r))))))
  8002. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8003. (delq
  8004. nil
  8005. (delete-dups
  8006. (mapcar (lambda (x)
  8007. (let ((tpl (car x)))
  8008. (when (not (delq
  8009. nil
  8010. (mapcar (lambda(y)
  8011. (equal y tpl)) s))) x)))
  8012. (reverse r))))))
  8013. (defun org-contextualize-validate-key (key contexts)
  8014. "Check CONTEXTS for agenda or capture KEY."
  8015. (let (r rr res)
  8016. (while (setq r (pop contexts))
  8017. (mapc
  8018. (lambda (rr)
  8019. (when
  8020. (and (equal key (car r))
  8021. (if (functionp rr) (funcall rr)
  8022. (or (and (eq (car rr) 'in-file)
  8023. (buffer-file-name)
  8024. (string-match (cdr rr) (buffer-file-name)))
  8025. (and (eq (car rr) 'in-mode)
  8026. (string-match (cdr rr) (symbol-name major-mode)))
  8027. (and (eq (car rr) 'in-buffer)
  8028. (string-match (cdr rr) (buffer-name)))
  8029. (when (and (eq (car rr) 'not-in-file)
  8030. (buffer-file-name))
  8031. (not (string-match (cdr rr) (buffer-file-name))))
  8032. (when (eq (car rr) 'not-in-mode)
  8033. (not (string-match (cdr rr) (symbol-name major-mode))))
  8034. (when (eq (car rr) 'not-in-buffer)
  8035. (not (string-match (cdr rr) (buffer-name)))))))
  8036. (push r res)))
  8037. (car (last r))))
  8038. (delete-dups (delq nil res))))
  8039. (defun org-context-p (&rest contexts)
  8040. "Check if local context is any of CONTEXTS.
  8041. Possible values in the list of contexts are `table', `headline', and `item'."
  8042. (let ((pos (point)))
  8043. (goto-char (point-at-bol))
  8044. (prog1 (or (and (memq 'table contexts)
  8045. (looking-at "[ \t]*|"))
  8046. (and (memq 'headline contexts)
  8047. (looking-at org-outline-regexp))
  8048. (and (memq 'item contexts)
  8049. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8050. (and (memq 'item-body contexts)
  8051. (org-in-item-p)))
  8052. (goto-char pos))))
  8053. (defun org-get-local-variables ()
  8054. "Return a list of all local variables in an Org mode buffer."
  8055. (let (varlist)
  8056. (with-current-buffer (get-buffer-create "*Org tmp*")
  8057. (erase-buffer)
  8058. (org-mode)
  8059. (setq varlist (buffer-local-variables)))
  8060. (kill-buffer "*Org tmp*")
  8061. (delq nil
  8062. (mapcar
  8063. (lambda (x)
  8064. (setq x
  8065. (if (symbolp x)
  8066. (list x)
  8067. (list (car x) (cdr x))))
  8068. (if (and (not (get (car x) 'org-state))
  8069. (string-match
  8070. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8071. (symbol-name (car x))))
  8072. x nil))
  8073. varlist))))
  8074. (defun org-clone-local-variables (from-buffer &optional regexp)
  8075. "Clone local variables from FROM-BUFFER.
  8076. Optional argument REGEXP selects variables to clone."
  8077. (mapc
  8078. (lambda (pair)
  8079. (and (symbolp (car pair))
  8080. (or (null regexp)
  8081. (string-match regexp (symbol-name (car pair))))
  8082. (set (make-local-variable (car pair))
  8083. (cdr pair))))
  8084. (buffer-local-variables from-buffer)))
  8085. ;;;###autoload
  8086. (defun org-run-like-in-org-mode (cmd)
  8087. "Run a command, pretending that the current buffer is in Org-mode.
  8088. This will temporarily bind local variables that are typically bound in
  8089. Org-mode to the values they have in Org-mode, and then interactively
  8090. call CMD."
  8091. (org-load-modules-maybe)
  8092. (unless org-local-vars
  8093. (setq org-local-vars (org-get-local-variables)))
  8094. (let (binds)
  8095. (dolist (var org-local-vars)
  8096. (when (or (not (boundp (car var)))
  8097. (eq (symbol-value (car var))
  8098. (default-value (car var))))
  8099. (push (list (car var) `(quote ,(cadr var))) binds)))
  8100. (eval `(let ,binds
  8101. (call-interactively (quote ,cmd))))))
  8102. ;;;; Archiving
  8103. (defun org-get-category (&optional pos force-refresh)
  8104. "Get the category applying to position POS."
  8105. (save-match-data
  8106. (if force-refresh (org-refresh-category-properties))
  8107. (let ((pos (or pos (point))))
  8108. (or (get-text-property pos 'org-category)
  8109. (progn (org-refresh-category-properties)
  8110. (get-text-property pos 'org-category))))))
  8111. (defun org-refresh-category-properties ()
  8112. "Refresh category text properties in the buffer."
  8113. (let ((case-fold-search t)
  8114. (inhibit-read-only t)
  8115. (def-cat (cond
  8116. ((null org-category)
  8117. (if buffer-file-name
  8118. (file-name-sans-extension
  8119. (file-name-nondirectory buffer-file-name))
  8120. "???"))
  8121. ((symbolp org-category) (symbol-name org-category))
  8122. (t org-category)))
  8123. beg end cat pos optionp)
  8124. (org-with-silent-modifications
  8125. (save-excursion
  8126. (save-restriction
  8127. (widen)
  8128. (goto-char (point-min))
  8129. (put-text-property (point) (point-max) 'org-category def-cat)
  8130. (while (re-search-forward
  8131. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8132. (setq pos (match-end 0)
  8133. optionp (equal (char-after (match-beginning 0)) ?#)
  8134. cat (org-trim (match-string 2)))
  8135. (if optionp
  8136. (setq beg (point-at-bol) end (point-max))
  8137. (org-back-to-heading t)
  8138. (setq beg (point) end (org-end-of-subtree t t)))
  8139. (put-text-property beg end 'org-category cat)
  8140. (put-text-property beg end 'org-category-position beg)
  8141. (goto-char pos)))))))
  8142. (defun org-refresh-properties (dprop tprop)
  8143. "Refresh buffer text properties.
  8144. DPROP is the drawer property and TPROP is the corresponding text
  8145. property to set."
  8146. (let ((case-fold-search t)
  8147. (inhibit-read-only t) p)
  8148. (org-with-silent-modifications
  8149. (save-excursion
  8150. (save-restriction
  8151. (widen)
  8152. (goto-char (point-min))
  8153. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8154. (setq p (org-match-string-no-properties 1))
  8155. (save-excursion
  8156. (org-back-to-heading t)
  8157. (put-text-property
  8158. (point-at-bol) (point-at-eol) tprop p))))))))
  8159. ;;;; Link Stuff
  8160. ;;; Link abbreviations
  8161. (defun org-link-expand-abbrev (link)
  8162. "Apply replacements as defined in `org-link-abbrev-alist'."
  8163. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8164. (let* ((key (match-string 1 link))
  8165. (as (or (assoc key org-link-abbrev-alist-local)
  8166. (assoc key org-link-abbrev-alist)))
  8167. (tag (and (match-end 2) (match-string 3 link)))
  8168. rpl)
  8169. (if (not as)
  8170. link
  8171. (setq rpl (cdr as))
  8172. (cond
  8173. ((symbolp rpl) (funcall rpl tag))
  8174. ((string-match "%(\\([^)]+\\))" rpl)
  8175. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8176. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8177. ((string-match "%h" rpl)
  8178. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8179. (t (concat rpl tag)))))
  8180. link))
  8181. ;;; Storing and inserting links
  8182. (defvar org-insert-link-history nil
  8183. "Minibuffer history for links inserted with `org-insert-link'.")
  8184. (defvar org-stored-links nil
  8185. "Contains the links stored with `org-store-link'.")
  8186. (defvar org-store-link-plist nil
  8187. "Plist with info about the most recently link created with `org-store-link'.")
  8188. (defvar org-link-protocols nil
  8189. "Link protocols added to Org-mode using `org-add-link-type'.")
  8190. (defvar org-store-link-functions nil
  8191. "List of functions that are called to create and store a link.
  8192. Each function will be called in turn until one returns a non-nil
  8193. value. Each function should check if it is responsible for creating
  8194. this link (for example by looking at the major mode).
  8195. If not, it must exit and return nil.
  8196. If yes, it should return a non-nil value after a calling
  8197. `org-store-link-props' with a list of properties and values.
  8198. Special properties are:
  8199. :type The link prefix, like \"http\". This must be given.
  8200. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8201. This is obligatory as well.
  8202. :description Optional default description for the second pair
  8203. of brackets in an Org-mode link. The user can still change
  8204. this when inserting this link into an Org-mode buffer.
  8205. In addition to these, any additional properties can be specified
  8206. and then used in capture templates.")
  8207. (defun org-add-link-type (type &optional follow export)
  8208. "Add TYPE to the list of `org-link-types'.
  8209. Re-compute all regular expressions depending on `org-link-types'
  8210. FOLLOW and EXPORT are two functions.
  8211. FOLLOW should take the link path as the single argument and do whatever
  8212. is necessary to follow the link, for example find a file or display
  8213. a mail message.
  8214. EXPORT should format the link path for export to one of the export formats.
  8215. It should be a function accepting three arguments:
  8216. path the path of the link, the text after the prefix (like \"http:\")
  8217. desc the description of the link, if any, or a description added by
  8218. org-export-normalize-links if there is none
  8219. format the export format, a symbol like `html' or `latex' or `ascii'..
  8220. The function may use the FORMAT information to return different values
  8221. depending on the format. The return value will be put literally into
  8222. the exported file. If the return value is nil, this means Org should
  8223. do what it normally does with links which do not have EXPORT defined.
  8224. Org-mode has a built-in default for exporting links. If you are happy with
  8225. this default, there is no need to define an export function for the link
  8226. type. For a simple example of an export function, see `org-bbdb.el'."
  8227. (add-to-list 'org-link-types type t)
  8228. (org-make-link-regexps)
  8229. (if (assoc type org-link-protocols)
  8230. (setcdr (assoc type org-link-protocols) (list follow export))
  8231. (push (list type follow export) org-link-protocols)))
  8232. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8233. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8234. ;;;###autoload
  8235. (defun org-store-link (arg)
  8236. "\\<org-mode-map>Store an org-link to the current location.
  8237. This link is added to `org-stored-links' and can later be inserted
  8238. into an org-buffer with \\[org-insert-link].
  8239. For some link types, a prefix arg is interpreted.
  8240. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8241. For file links, arg negates `org-context-in-file-links'.
  8242. A double prefix arg force skipping storing functions that are not
  8243. part of Org's core."
  8244. (interactive "P")
  8245. (org-load-modules-maybe)
  8246. (setq org-store-link-plist nil) ; reset
  8247. (org-with-limited-levels
  8248. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8249. (cond
  8250. ((and (not (equal arg '(16)))
  8251. (setq sfuns
  8252. (delq
  8253. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8254. org-store-link-functions))
  8255. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8256. (or (and (cdr sfuns)
  8257. (funcall (intern
  8258. (completing-read "Which function for creating the link? "
  8259. sfunsn t (car sfunsn)))))
  8260. (funcall (caar sfuns)))
  8261. (setq link (plist-get org-store-link-plist :link)
  8262. desc (or (plist-get org-store-link-plist :description) link))))
  8263. ((org-src-edit-buffer-p)
  8264. (let (label gc)
  8265. (while (or (not label)
  8266. (save-excursion
  8267. (save-restriction
  8268. (widen)
  8269. (goto-char (point-min))
  8270. (re-search-forward
  8271. (regexp-quote (format org-coderef-label-format label))
  8272. nil t))))
  8273. (when label (message "Label exists already") (sit-for 2))
  8274. (setq label (read-string "Code line label: " label)))
  8275. (end-of-line 1)
  8276. (setq link (format org-coderef-label-format label))
  8277. (setq gc (- 79 (length link)))
  8278. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8279. (insert link)
  8280. (setq link (concat "(" label ")") desc nil)))
  8281. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8282. ;; We are in the agenda, link to referenced location
  8283. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8284. (get-text-property (point) 'org-marker))))
  8285. (when m
  8286. (org-with-point-at m
  8287. (setq agenda-link
  8288. (if (org-called-interactively-p 'any)
  8289. (call-interactively 'org-store-link)
  8290. (org-store-link nil)))))))
  8291. ((eq major-mode 'calendar-mode)
  8292. (let ((cd (calendar-cursor-to-date)))
  8293. (setq link
  8294. (format-time-string
  8295. (car org-time-stamp-formats)
  8296. (apply 'encode-time
  8297. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8298. nil nil nil))))
  8299. (org-store-link-props :type "calendar" :date cd)))
  8300. ((eq major-mode 'help-mode)
  8301. (setq link (concat "help:" (save-excursion
  8302. (goto-char (point-min))
  8303. (looking-at "^[^ ]+")
  8304. (match-string 0))))
  8305. (org-store-link-props :type "help"))
  8306. ((eq major-mode 'w3-mode)
  8307. (setq cpltxt (if (and (buffer-name)
  8308. (not (string-match "Untitled" (buffer-name))))
  8309. (buffer-name)
  8310. (url-view-url t))
  8311. link (url-view-url t))
  8312. (org-store-link-props :type "w3" :url (url-view-url t)))
  8313. ((setq search (run-hook-with-args-until-success
  8314. 'org-create-file-search-functions))
  8315. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8316. "::" search))
  8317. (setq cpltxt (or description link)))
  8318. ((eq major-mode 'image-mode)
  8319. (setq cpltxt (concat "file:"
  8320. (abbreviate-file-name buffer-file-name))
  8321. link cpltxt)
  8322. (org-store-link-props :type "image" :file buffer-file-name))
  8323. ((eq major-mode 'dired-mode)
  8324. ;; link to the file in the current line
  8325. (let ((file (dired-get-filename nil t)))
  8326. (setq file (if file
  8327. (abbreviate-file-name
  8328. (expand-file-name (dired-get-filename nil t)))
  8329. ;; otherwise, no file so use current directory.
  8330. default-directory))
  8331. (setq cpltxt (concat "file:" file)
  8332. link cpltxt)))
  8333. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8334. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8335. (cond
  8336. ((org-in-regexp "<<\\(.*?\\)>>")
  8337. (setq cpltxt
  8338. (concat "file:"
  8339. (abbreviate-file-name
  8340. (buffer-file-name (buffer-base-buffer)))
  8341. "::" (match-string 1))
  8342. link cpltxt))
  8343. ((and (featurep 'org-id)
  8344. (or (eq org-id-link-to-org-use-id t)
  8345. (and (org-called-interactively-p 'any)
  8346. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8347. (and (eq org-id-link-to-org-use-id
  8348. 'create-if-interactive-and-no-custom-id)
  8349. (not custom-id))))
  8350. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8351. ;; We can make a link using the ID.
  8352. (setq link (condition-case nil
  8353. (prog1 (org-id-store-link)
  8354. (setq desc (plist-get org-store-link-plist :description)))
  8355. (error
  8356. ;; probably before first headline, link to file only
  8357. (concat "file:"
  8358. (abbreviate-file-name
  8359. (buffer-file-name (buffer-base-buffer))))))))
  8360. (t
  8361. ;; Just link to current headline
  8362. (setq cpltxt (concat "file:"
  8363. (abbreviate-file-name
  8364. (buffer-file-name (buffer-base-buffer)))))
  8365. ;; Add a context search string
  8366. (when (org-xor org-context-in-file-links arg)
  8367. (let* ((ee (org-element-at-point))
  8368. (et (org-element-type ee))
  8369. (ev (plist-get (cadr ee) :value)))
  8370. (setq txt (cond
  8371. ((org-at-heading-p) nil)
  8372. ((eq et 'keyword) ev)
  8373. ((org-region-active-p)
  8374. (buffer-substring (region-beginning) (region-end)))))
  8375. (when (or (null txt) (string-match "\\S-" txt))
  8376. (setq cpltxt
  8377. (concat cpltxt "::"
  8378. (condition-case nil
  8379. (org-make-org-heading-search-string txt)
  8380. (error "")))
  8381. desc (or (and (eq et 'keyword) ev)
  8382. (nth 4 (ignore-errors (org-heading-components)))
  8383. "NONE")))))
  8384. (if (string-match "::\\'" cpltxt)
  8385. (setq cpltxt (substring cpltxt 0 -2)))
  8386. (setq link cpltxt))))
  8387. ((buffer-file-name (buffer-base-buffer))
  8388. ;; Just link to this file here.
  8389. (setq cpltxt (concat "file:"
  8390. (abbreviate-file-name
  8391. (buffer-file-name (buffer-base-buffer)))))
  8392. ;; Add a context string
  8393. (when (org-xor org-context-in-file-links arg)
  8394. (setq txt (if (org-region-active-p)
  8395. (buffer-substring (region-beginning) (region-end))
  8396. (buffer-substring (point-at-bol) (point-at-eol))))
  8397. ;; Only use search option if there is some text.
  8398. (when (string-match "\\S-" txt)
  8399. (setq cpltxt
  8400. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8401. desc "NONE")))
  8402. (setq link cpltxt))
  8403. ((org-called-interactively-p 'interactive)
  8404. (user-error "No method for storing a link from this buffer"))
  8405. (t (setq link nil)))
  8406. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8407. (setq link (or link cpltxt)
  8408. desc (or desc cpltxt))
  8409. (cond ((equal desc "NONE") (setq desc nil))
  8410. ((string-match org-bracket-link-regexp desc)
  8411. (setq desc (replace-regexp-in-string
  8412. org-bracket-link-regexp
  8413. (concat "\\3" (if (equal (length (match-string 0 desc))
  8414. (length desc)) "*" "")) desc))))
  8415. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8416. (progn
  8417. (setq org-stored-links
  8418. (cons (list link desc) org-stored-links))
  8419. (message "Stored: %s" (or desc link))
  8420. (when custom-id
  8421. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8422. "::#" custom-id))
  8423. (setq org-stored-links
  8424. (cons (list link desc) org-stored-links))))
  8425. (or agenda-link (and link (org-make-link-string link desc)))))))
  8426. (defun org-store-link-props (&rest plist)
  8427. "Store link properties, extract names and addresses."
  8428. (let (x adr)
  8429. (when (setq x (plist-get plist :from))
  8430. (setq adr (mail-extract-address-components x))
  8431. (setq plist (plist-put plist :fromname (car adr)))
  8432. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8433. (when (setq x (plist-get plist :to))
  8434. (setq adr (mail-extract-address-components x))
  8435. (setq plist (plist-put plist :toname (car adr)))
  8436. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8437. (let ((from (plist-get plist :from))
  8438. (to (plist-get plist :to)))
  8439. (when (and from to org-from-is-user-regexp)
  8440. (setq plist
  8441. (plist-put plist :fromto
  8442. (if (string-match org-from-is-user-regexp from)
  8443. (concat "to %t")
  8444. (concat "from %f"))))))
  8445. (setq org-store-link-plist plist))
  8446. (defun org-add-link-props (&rest plist)
  8447. "Add these properties to the link property list."
  8448. (let (key value)
  8449. (while plist
  8450. (setq key (pop plist) value (pop plist))
  8451. (setq org-store-link-plist
  8452. (plist-put org-store-link-plist key value)))))
  8453. (defun org-email-link-description (&optional fmt)
  8454. "Return the description part of an email link.
  8455. This takes information from `org-store-link-plist' and formats it
  8456. according to FMT (default from `org-email-link-description-format')."
  8457. (setq fmt (or fmt org-email-link-description-format))
  8458. (let* ((p org-store-link-plist)
  8459. (to (plist-get p :toaddress))
  8460. (from (plist-get p :fromaddress))
  8461. (table
  8462. (list
  8463. (cons "%c" (plist-get p :fromto))
  8464. (cons "%F" (plist-get p :from))
  8465. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8466. (cons "%T" (plist-get p :to))
  8467. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8468. (cons "%s" (plist-get p :subject))
  8469. (cons "%d" (plist-get p :date))
  8470. (cons "%m" (plist-get p :message-id)))))
  8471. (when (string-match "%c" fmt)
  8472. ;; Check if the user wrote this message
  8473. (if (and org-from-is-user-regexp from to
  8474. (save-match-data (string-match org-from-is-user-regexp from)))
  8475. (setq fmt (replace-match "to %t" t t fmt))
  8476. (setq fmt (replace-match "from %f" t t fmt))))
  8477. (org-replace-escapes fmt table)))
  8478. (defun org-make-org-heading-search-string (&optional string)
  8479. "Make search string for the current headline or STRING."
  8480. (let ((s (or string
  8481. (and (derived-mode-p 'org-mode)
  8482. (save-excursion
  8483. (org-back-to-heading t)
  8484. (org-element-property :raw-value (org-element-at-point))))))
  8485. (lines org-context-in-file-links))
  8486. (or string (setq s (concat "*" s))) ; Add * for headlines
  8487. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8488. (when (and string (integerp lines) (> lines 0))
  8489. (let ((slines (org-split-string s "\n")))
  8490. (when (< lines (length slines))
  8491. (setq s (mapconcat
  8492. 'identity
  8493. (reverse (nthcdr (- (length slines) lines)
  8494. (reverse slines))) "\n")))))
  8495. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8496. (defun org-make-link-string (link &optional description)
  8497. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8498. (unless (string-match "\\S-" link)
  8499. (error "Empty link"))
  8500. (when (and description
  8501. (stringp description)
  8502. (not (string-match "\\S-" description)))
  8503. (setq description nil))
  8504. (when (stringp description)
  8505. ;; Remove brackets from the description, they are fatal.
  8506. (while (string-match "\\[" description)
  8507. (setq description (replace-match "{" t t description)))
  8508. (while (string-match "\\]" description)
  8509. (setq description (replace-match "}" t t description))))
  8510. (when (equal link description)
  8511. ;; No description needed, it is identical
  8512. (setq description nil))
  8513. (when (and (not description)
  8514. (not (string-match (org-image-file-name-regexp) link))
  8515. (not (equal link (org-link-escape link))))
  8516. (setq description (org-extract-attributes link)))
  8517. (setq link
  8518. (cond ((string-match (org-image-file-name-regexp) link) link)
  8519. ((string-match org-link-types-re link)
  8520. (concat (match-string 1 link)
  8521. (org-link-escape (substring link (match-end 1)))))
  8522. (t (org-link-escape link))))
  8523. (concat "[[" link "]"
  8524. (if description (concat "[" description "]") "")
  8525. "]"))
  8526. (defconst org-link-escape-chars
  8527. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8528. "List of characters that should be escaped in link.
  8529. This is the list that is used for internal purposes.")
  8530. (defconst org-link-escape-chars-browser
  8531. '(?\ )
  8532. "List of escapes for characters that are problematic in links.
  8533. This is the list that is used before handing over to the browser.")
  8534. (defun org-link-escape (text &optional table merge)
  8535. "Return percent escaped representation of TEXT.
  8536. TEXT is a string with the text to escape.
  8537. Optional argument TABLE is a list with characters that should be
  8538. escaped. When nil, `org-link-escape-chars' is used.
  8539. If optional argument MERGE is set, merge TABLE into
  8540. `org-link-escape-chars'."
  8541. (cond
  8542. ((and table merge)
  8543. (mapc (lambda (defchr)
  8544. (unless (member defchr table)
  8545. (setq table (cons defchr table)))) org-link-escape-chars))
  8546. ((null table)
  8547. (setq table org-link-escape-chars)))
  8548. (mapconcat
  8549. (lambda (char)
  8550. (if (or (member char table)
  8551. (and (or (< char 32) (= char 37) (> char 126))
  8552. org-url-hexify-p))
  8553. (mapconcat (lambda (sequence-element)
  8554. (format "%%%.2X" sequence-element))
  8555. (or (encode-coding-char char 'utf-8)
  8556. (error "Unable to percent escape character: %s"
  8557. (char-to-string char))) "")
  8558. (char-to-string char))) text ""))
  8559. (defun org-link-unescape (str)
  8560. "Unhex hexified Unicode strings as returned from the JavaScript function
  8561. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8562. (unless (and (null str) (string= "" str))
  8563. (let ((pos 0) (case-fold-search t) unhexed)
  8564. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8565. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8566. (setq str (replace-match unhexed t t str))
  8567. (setq pos (+ pos (length unhexed))))))
  8568. str)
  8569. (defun org-link-unescape-compound (hex)
  8570. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8571. Note: this function also decodes single byte encodings like
  8572. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8573. (save-match-data
  8574. (let* ((bytes (cdr (split-string hex "%")))
  8575. (ret "")
  8576. (eat 0)
  8577. (sum 0))
  8578. (while bytes
  8579. (let* ((val (string-to-number (pop bytes) 16))
  8580. (shift-xor
  8581. (if (= 0 eat)
  8582. (cond
  8583. ((>= val 252) (cons 6 252))
  8584. ((>= val 248) (cons 5 248))
  8585. ((>= val 240) (cons 4 240))
  8586. ((>= val 224) (cons 3 224))
  8587. ((>= val 192) (cons 2 192))
  8588. (t (cons 0 0)))
  8589. (cons 6 128))))
  8590. (if (>= val 192) (setq eat (car shift-xor)))
  8591. (setq val (logxor val (cdr shift-xor)))
  8592. (setq sum (+ (lsh sum (car shift-xor)) val))
  8593. (if (> eat 0) (setq eat (- eat 1)))
  8594. (cond
  8595. ((= 0 eat) ;multi byte
  8596. (setq ret (concat ret (org-char-to-string sum)))
  8597. (setq sum 0))
  8598. ((not bytes) ; single byte(s)
  8599. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8600. )) ;; end (while bytes
  8601. ret )))
  8602. (defun org-link-unescape-single-byte-sequence (hex)
  8603. "Unhexify hex-encoded single byte character sequences."
  8604. (mapconcat (lambda (byte)
  8605. (char-to-string (string-to-number byte 16)))
  8606. (cdr (split-string hex "%")) ""))
  8607. (defun org-xor (a b)
  8608. "Exclusive or."
  8609. (if a (not b) b))
  8610. (defun org-fixup-message-id-for-http (s)
  8611. "Replace special characters in a message id, so it can be used in an http query."
  8612. (when (string-match "%" s)
  8613. (setq s (mapconcat (lambda (c)
  8614. (if (eq c ?%)
  8615. "%25"
  8616. (char-to-string c)))
  8617. s "")))
  8618. (while (string-match "<" s)
  8619. (setq s (replace-match "%3C" t t s)))
  8620. (while (string-match ">" s)
  8621. (setq s (replace-match "%3E" t t s)))
  8622. (while (string-match "@" s)
  8623. (setq s (replace-match "%40" t t s)))
  8624. s)
  8625. (defun org-link-prettify (link)
  8626. "Return a human-readable representation of LINK.
  8627. The car of LINK must be a raw link the cdr of LINK must be either
  8628. a link description or nil."
  8629. (let ((desc (or (cadr link) "<no description>")))
  8630. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8631. "<" (car link) ">")))
  8632. ;;;###autoload
  8633. (defun org-insert-link-global ()
  8634. "Insert a link like Org-mode does.
  8635. This command can be called in any mode to insert a link in Org-mode syntax."
  8636. (interactive)
  8637. (org-load-modules-maybe)
  8638. (org-run-like-in-org-mode 'org-insert-link))
  8639. (defun org-insert-all-links (&optional keep)
  8640. "Insert all links in `org-stored-links'."
  8641. (interactive "P")
  8642. (let ((links (copy-sequence org-stored-links)) l)
  8643. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8644. (insert "- ")
  8645. (org-insert-link nil (car l) (cadr l))
  8646. (insert "\n"))))
  8647. (defun org-link-fontify-links-to-this-file ()
  8648. "Fontify links to the current file in `org-stored-links'."
  8649. (let ((f (buffer-file-name)) a b)
  8650. (setq a (mapcar (lambda(l)
  8651. (let ((ll (car l)))
  8652. (when (and (string-match "^file:\\(.+\\)::" ll)
  8653. (equal f (expand-file-name (match-string 1 ll))))
  8654. ll)))
  8655. org-stored-links))
  8656. (when (featurep 'org-id)
  8657. (setq b (mapcar (lambda(l)
  8658. (let ((ll (car l)))
  8659. (when (and (string-match "^id:\\(.+\\)$" ll)
  8660. (equal f (expand-file-name
  8661. (or (org-id-find-id-file
  8662. (match-string 1 ll)) ""))))
  8663. ll)))
  8664. org-stored-links)))
  8665. (mapcar (lambda(l)
  8666. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8667. (delq nil (append a b)))))
  8668. (defvar org-link-links-in-this-file nil)
  8669. (defun org-insert-link (&optional complete-file link-location default-description)
  8670. "Insert a link. At the prompt, enter the link.
  8671. Completion can be used to insert any of the link protocol prefixes like
  8672. http or ftp in use.
  8673. The history can be used to select a link previously stored with
  8674. `org-store-link'. When the empty string is entered (i.e. if you just
  8675. press RET at the prompt), the link defaults to the most recently
  8676. stored link. As SPC triggers completion in the minibuffer, you need to
  8677. use M-SPC or C-q SPC to force the insertion of a space character.
  8678. You will also be prompted for a description, and if one is given, it will
  8679. be displayed in the buffer instead of the link.
  8680. If there is already a link at point, this command will allow you to edit link
  8681. and description parts.
  8682. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8683. be selected using completion. The path to the file will be relative to the
  8684. current directory if the file is in the current directory or a subdirectory.
  8685. Otherwise, the link will be the absolute path as completed in the minibuffer
  8686. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8687. option `org-link-file-path-type'.
  8688. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8689. the current directory or below.
  8690. With three \\[universal-argument] prefixes, negate the meaning of
  8691. `org-keep-stored-link-after-insertion'.
  8692. If `org-make-link-description-function' is non-nil, this function will be
  8693. called with the link target, and the result will be the default
  8694. link description.
  8695. If the LINK-LOCATION parameter is non-nil, this value will be
  8696. used as the link location instead of reading one interactively.
  8697. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8698. be used as the default description."
  8699. (interactive "P")
  8700. (let* ((wcf (current-window-configuration))
  8701. (origbuf (current-buffer))
  8702. (region (if (org-region-active-p)
  8703. (buffer-substring (region-beginning) (region-end))))
  8704. (remove (and region (list (region-beginning) (region-end))))
  8705. (desc region)
  8706. tmphist ; byte-compile incorrectly complains about this
  8707. (link link-location)
  8708. (abbrevs org-link-abbrev-alist-local)
  8709. entry file all-prefixes auto-desc)
  8710. (cond
  8711. (link-location) ; specified by arg, just use it.
  8712. ((org-in-regexp org-bracket-link-regexp 1)
  8713. ;; We do have a link at point, and we are going to edit it.
  8714. (setq remove (list (match-beginning 0) (match-end 0)))
  8715. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8716. (setq link (read-string "Link: "
  8717. (org-link-unescape
  8718. (org-match-string-no-properties 1)))))
  8719. ((or (org-in-regexp org-angle-link-re)
  8720. (org-in-regexp org-plain-link-re))
  8721. ;; Convert to bracket link
  8722. (setq remove (list (match-beginning 0) (match-end 0))
  8723. link (read-string "Link: "
  8724. (org-remove-angle-brackets (match-string 0)))))
  8725. ((member complete-file '((4) (16)))
  8726. ;; Completing read for file names.
  8727. (setq link (org-file-complete-link complete-file)))
  8728. (t
  8729. ;; Read link, with completion for stored links.
  8730. (org-link-fontify-links-to-this-file)
  8731. (org-switch-to-buffer-other-window "*Org Links*")
  8732. (with-current-buffer "*Org Links*"
  8733. (erase-buffer)
  8734. (insert "Insert a link.
  8735. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8736. (when org-stored-links
  8737. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8738. (insert (mapconcat 'org-link-prettify
  8739. (reverse org-stored-links) "\n")))
  8740. (goto-char (point-min)))
  8741. (let ((cw (selected-window)))
  8742. (select-window (get-buffer-window "*Org Links*" 'visible))
  8743. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8744. (unless (pos-visible-in-window-p (point-max))
  8745. (org-fit-window-to-buffer))
  8746. (and (window-live-p cw) (select-window cw)))
  8747. ;; Fake a link history, containing the stored links.
  8748. (setq tmphist (append (mapcar 'car org-stored-links)
  8749. org-insert-link-history))
  8750. (setq all-prefixes (append (mapcar 'car abbrevs)
  8751. (mapcar 'car org-link-abbrev-alist)
  8752. org-link-types))
  8753. (unwind-protect
  8754. (progn
  8755. (setq link
  8756. (org-completing-read
  8757. "Link: "
  8758. (append
  8759. (mapcar (lambda (x) (concat x ":"))
  8760. all-prefixes)
  8761. (mapcar 'car org-stored-links))
  8762. nil nil nil
  8763. 'tmphist
  8764. (caar org-stored-links)))
  8765. (if (not (string-match "\\S-" link))
  8766. (error "No link selected"))
  8767. (mapc (lambda(l)
  8768. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8769. org-stored-links)
  8770. (if (or (member link all-prefixes)
  8771. (and (equal ":" (substring link -1))
  8772. (member (substring link 0 -1) all-prefixes)
  8773. (setq link (substring link 0 -1))))
  8774. (setq link (with-current-buffer origbuf
  8775. (org-link-try-special-completion link)))))
  8776. (set-window-configuration wcf)
  8777. (kill-buffer "*Org Links*"))
  8778. (setq entry (assoc link org-stored-links))
  8779. (or entry (push link org-insert-link-history))
  8780. (setq desc (or desc (nth 1 entry)))))
  8781. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8782. (not org-keep-stored-link-after-insertion))
  8783. (setq org-stored-links (delq (assoc link org-stored-links)
  8784. org-stored-links)))
  8785. (if (string-match org-plain-link-re link)
  8786. ;; URL-like link, normalize the use of angular brackets.
  8787. (setq link (org-remove-angle-brackets link)))
  8788. ;; Check if we are linking to the current file with a search
  8789. ;; option If yes, simplify the link by using only the search
  8790. ;; option.
  8791. (when (and buffer-file-name
  8792. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8793. (let* ((path (match-string 1 link))
  8794. (case-fold-search nil)
  8795. (search (match-string 2 link)))
  8796. (save-match-data
  8797. (if (equal (file-truename buffer-file-name) (file-truename path))
  8798. ;; We are linking to this same file, with a search option
  8799. (setq link search)))))
  8800. ;; Check if we can/should use a relative path. If yes, simplify the link
  8801. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8802. (let* ((type (match-string 1 link))
  8803. (path (match-string 2 link))
  8804. (origpath path)
  8805. (case-fold-search nil))
  8806. (cond
  8807. ((or (eq org-link-file-path-type 'absolute)
  8808. (equal complete-file '(16)))
  8809. (setq path (abbreviate-file-name (expand-file-name path))))
  8810. ((eq org-link-file-path-type 'noabbrev)
  8811. (setq path (expand-file-name path)))
  8812. ((eq org-link-file-path-type 'relative)
  8813. (setq path (file-relative-name path)))
  8814. (t
  8815. (save-match-data
  8816. (if (string-match (concat "^" (regexp-quote
  8817. (expand-file-name
  8818. (file-name-as-directory
  8819. default-directory))))
  8820. (expand-file-name path))
  8821. ;; We are linking a file with relative path name.
  8822. (setq path (substring (expand-file-name path)
  8823. (match-end 0)))
  8824. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8825. (setq link (concat type path))
  8826. (if (equal desc origpath)
  8827. (setq desc path))))
  8828. (if org-make-link-description-function
  8829. (setq desc
  8830. (or (condition-case nil
  8831. (funcall org-make-link-description-function link desc)
  8832. (error (progn (message "Can't get link description from `%s'"
  8833. (symbol-name org-make-link-description-function))
  8834. (sit-for 2) nil)))
  8835. (read-string "Description: " default-description)))
  8836. (if default-description (setq desc default-description)
  8837. (setq desc (or (and auto-desc desc)
  8838. (read-string "Description: " desc)))))
  8839. (unless (string-match "\\S-" desc) (setq desc nil))
  8840. (if remove (apply 'delete-region remove))
  8841. (insert (org-make-link-string link desc))))
  8842. (defun org-link-try-special-completion (type)
  8843. "If there is completion support for link type TYPE, offer it."
  8844. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8845. (if (functionp fun)
  8846. (funcall fun)
  8847. (read-string "Link (no completion support): " (concat type ":")))))
  8848. (defun org-file-complete-link (&optional arg)
  8849. "Create a file link using completion."
  8850. (let (file link)
  8851. (setq file (org-iread-file-name "File: "))
  8852. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8853. (pwd1 (file-name-as-directory (abbreviate-file-name
  8854. (expand-file-name ".")))))
  8855. (cond
  8856. ((equal arg '(16))
  8857. (setq link (concat
  8858. "file:"
  8859. (abbreviate-file-name (expand-file-name file)))))
  8860. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8861. (setq link (concat "file:" (match-string 1 file))))
  8862. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8863. (expand-file-name file))
  8864. (setq link (concat
  8865. "file:" (match-string 1 (expand-file-name file)))))
  8866. (t (setq link (concat "file:" file)))))
  8867. link))
  8868. (defun org-iread-file-name (&rest args)
  8869. "Read-file-name using `ido-mode' speedup if available.
  8870. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8871. See `read-file-name' for a description of parameters."
  8872. (org-without-partial-completion
  8873. (if (and org-completion-use-ido
  8874. (fboundp 'ido-read-file-name)
  8875. (boundp 'ido-mode) ido-mode
  8876. (listp (second args)))
  8877. (let ((ido-enter-matching-directory nil))
  8878. (apply 'ido-read-file-name args))
  8879. (apply 'read-file-name args))))
  8880. (defun org-completing-read (&rest args)
  8881. "Completing-read with SPACE being a normal character."
  8882. (let ((enable-recursive-minibuffers t)
  8883. (minibuffer-local-completion-map
  8884. (copy-keymap minibuffer-local-completion-map)))
  8885. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8886. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8887. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8888. (apply 'org-icompleting-read args)))
  8889. (defun org-completing-read-no-i (&rest args)
  8890. (let (org-completion-use-ido org-completion-use-iswitchb)
  8891. (apply 'org-completing-read args)))
  8892. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8893. "Use iswitch as a completing-read replacement to choose from choices.
  8894. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8895. from."
  8896. (let* ((iswitchb-use-virtual-buffers nil)
  8897. (iswitchb-make-buflist-hook
  8898. (lambda ()
  8899. (setq iswitchb-temp-buflist choices))))
  8900. (iswitchb-read-buffer prompt)))
  8901. (defun org-icompleting-read (&rest args)
  8902. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8903. (org-without-partial-completion
  8904. (if (and org-completion-use-ido
  8905. (fboundp 'ido-completing-read)
  8906. (boundp 'ido-mode) ido-mode
  8907. (listp (second args)))
  8908. (let ((ido-enter-matching-directory nil))
  8909. (apply 'ido-completing-read (concat (car args))
  8910. (if (consp (car (nth 1 args)))
  8911. (mapcar 'car (nth 1 args))
  8912. (nth 1 args))
  8913. (cddr args)))
  8914. (if (and org-completion-use-iswitchb
  8915. (boundp 'iswitchb-mode) iswitchb-mode
  8916. (listp (second args)))
  8917. (apply 'org-iswitchb-completing-read (concat (car args))
  8918. (if (consp (car (nth 1 args)))
  8919. (mapcar 'car (nth 1 args))
  8920. (nth 1 args))
  8921. (cddr args))
  8922. (apply 'completing-read args)))))
  8923. (defun org-extract-attributes (s)
  8924. "Extract the attributes cookie from a string and set as text property."
  8925. (let (a attr (start 0) key value)
  8926. (save-match-data
  8927. (when (string-match "{{\\([^}]+\\)}}$" s)
  8928. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8929. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8930. (setq key (match-string 1 a) value (match-string 2 a)
  8931. start (match-end 0)
  8932. attr (plist-put attr (intern key) value))))
  8933. (org-add-props s nil 'org-attr attr))
  8934. s))
  8935. (defun org-extract-attributes-from-string (tag)
  8936. (let (key value attr)
  8937. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8938. (setq key (match-string 1 tag) value (match-string 2 tag)
  8939. tag (replace-match "" t t tag)
  8940. attr (plist-put attr (intern key) value)))
  8941. (cons tag attr)))
  8942. (defun org-attributes-to-string (plist)
  8943. "Format a property list into an HTML attribute list."
  8944. (let ((s "") key value)
  8945. (while plist
  8946. (setq key (pop plist) value (pop plist))
  8947. (and value
  8948. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8949. s))
  8950. ;;; Opening/following a link
  8951. (defvar org-link-search-failed nil)
  8952. (defvar org-open-link-functions nil
  8953. "Hook for functions finding a plain text link.
  8954. These functions must take a single argument, the link content.
  8955. They will be called for links that look like [[link text][description]]
  8956. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8957. like a filename (e.g. \"./blue.png\").
  8958. These functions will be called *before* Org attempts to resolve the
  8959. link by doing text searches in the current buffer - so if you want a
  8960. link \"[[target]]\" to still find \"<<target>>\", your function should
  8961. handle this as a special case.
  8962. When the function does handle the link, it must return a non-nil value.
  8963. If it decides that it is not responsible for this link, it must return
  8964. nil to indicate that that Org-mode can continue with other options
  8965. like exact and fuzzy text search.")
  8966. (defun org-next-link (&optional search-backward)
  8967. "Move forward to the next link.
  8968. If the link is in hidden text, expose it."
  8969. (interactive "P")
  8970. (when (and org-link-search-failed (eq this-command last-command))
  8971. (goto-char (point-min))
  8972. (message "Link search wrapped back to beginning of buffer"))
  8973. (setq org-link-search-failed nil)
  8974. (let* ((pos (point))
  8975. (ct (org-context))
  8976. (a (assoc :link ct))
  8977. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  8978. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  8979. ((looking-at org-any-link-re)
  8980. ;; Don't stay stuck at link without an org-link face
  8981. (forward-char (if search-backward -1 1))))
  8982. (if (funcall srch-fun org-any-link-re nil t)
  8983. (progn
  8984. (goto-char (match-beginning 0))
  8985. (if (outline-invisible-p) (org-show-context)))
  8986. (goto-char pos)
  8987. (setq org-link-search-failed t)
  8988. (message "No further link found"))))
  8989. (defun org-previous-link ()
  8990. "Move backward to the previous link.
  8991. If the link is in hidden text, expose it."
  8992. (interactive)
  8993. (funcall 'org-next-link t))
  8994. (defun org-translate-link (s)
  8995. "Translate a link string if a translation function has been defined."
  8996. (if (and org-link-translation-function
  8997. (fboundp org-link-translation-function)
  8998. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8999. (progn
  9000. (setq s (funcall org-link-translation-function
  9001. (match-string 1 s) (match-string 2 s)))
  9002. (concat (car s) ":" (cdr s)))
  9003. s))
  9004. (defun org-translate-link-from-planner (type path)
  9005. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9006. This is still an experimental function, your mileage may vary."
  9007. (cond
  9008. ((member type '("http" "https" "news" "ftp"))
  9009. ;; standard Internet links are the same.
  9010. nil)
  9011. ((and (equal type "irc") (string-match "^//" path))
  9012. ;; Planner has two / at the beginning of an irc link, we have 1.
  9013. ;; We should have zero, actually....
  9014. (setq path (substring path 1)))
  9015. ((and (equal type "lisp") (string-match "^/" path))
  9016. ;; Planner has a slash, we do not.
  9017. (setq type "elisp" path (substring path 1)))
  9018. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9019. ;; A typical message link. Planner has the id after the final slash,
  9020. ;; we separate it with a hash mark
  9021. (setq path (concat (match-string 1 path) "#"
  9022. (org-remove-angle-brackets (match-string 2 path)))))
  9023. )
  9024. (cons type path))
  9025. (defun org-find-file-at-mouse (ev)
  9026. "Open file link or URL at mouse."
  9027. (interactive "e")
  9028. (mouse-set-point ev)
  9029. (org-open-at-point 'in-emacs))
  9030. (defun org-open-at-mouse (ev)
  9031. "Open file link or URL at mouse.
  9032. See the docstring of `org-open-file' for details."
  9033. (interactive "e")
  9034. (mouse-set-point ev)
  9035. (if (eq major-mode 'org-agenda-mode)
  9036. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9037. (org-open-at-point))
  9038. (defvar org-window-config-before-follow-link nil
  9039. "The window configuration before following a link.
  9040. This is saved in case the need arises to restore it.")
  9041. (defvar org-open-link-marker (make-marker)
  9042. "Marker pointing to the location where `org-open-at-point; was called.")
  9043. ;;;###autoload
  9044. (defun org-open-at-point-global ()
  9045. "Follow a link like Org-mode does.
  9046. This command can be called in any mode to follow a link that has
  9047. Org-mode syntax."
  9048. (interactive)
  9049. (org-run-like-in-org-mode 'org-open-at-point))
  9050. ;;;###autoload
  9051. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9052. "Open a link in the string S, as if it was in Org-mode."
  9053. (interactive "sLink: \nP")
  9054. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9055. (with-temp-buffer
  9056. (let ((org-inhibit-startup (not reference-buffer)))
  9057. (org-mode)
  9058. (insert s)
  9059. (goto-char (point-min))
  9060. (when reference-buffer
  9061. (setq org-link-abbrev-alist-local
  9062. (with-current-buffer reference-buffer
  9063. org-link-abbrev-alist-local)))
  9064. (org-open-at-point arg reference-buffer)))))
  9065. (defvar org-open-at-point-functions nil
  9066. "Hook that is run when following a link at point.
  9067. Functions in this hook must return t if they identify and follow
  9068. a link at point. If they don't find anything interesting at point,
  9069. they must return nil.")
  9070. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9071. (defun org-open-at-point (&optional arg reference-buffer)
  9072. "Open link at or after point.
  9073. If there is no link at point, this function will search forward up to
  9074. the end of the current line.
  9075. Normally, files will be opened by an appropriate application. If the
  9076. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9077. With a double prefix argument, try to open outside of Emacs, in the
  9078. application the system uses for this file type."
  9079. (interactive "P")
  9080. ;; if in a code block, then open the block's results
  9081. (unless (call-interactively #'org-babel-open-src-block-result)
  9082. (org-load-modules-maybe)
  9083. (move-marker org-open-link-marker (point))
  9084. (setq org-window-config-before-follow-link (current-window-configuration))
  9085. (org-remove-occur-highlights nil nil t)
  9086. (cond
  9087. ((and (org-at-heading-p)
  9088. (not (org-at-timestamp-p t))
  9089. (not (org-in-regexp
  9090. (concat org-plain-link-re "\\|"
  9091. org-bracket-link-regexp "\\|"
  9092. org-angle-link-re "\\|"
  9093. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9094. (not (get-text-property (point) 'org-linked-text)))
  9095. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9096. (lk0 (car lkall))
  9097. (lk (if (stringp lk0) (list lk0) lk0))
  9098. (lkend (cdr lkall)))
  9099. (mapcar (lambda(l)
  9100. (search-forward l nil lkend)
  9101. (goto-char (match-beginning 0))
  9102. (org-open-at-point))
  9103. lk))
  9104. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9105. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9106. ((and (org-at-timestamp-p t)
  9107. (not (org-in-regexp org-bracket-link-regexp)))
  9108. (org-follow-timestamp-link))
  9109. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9110. (not (org-in-regexp org-any-link-re)))
  9111. (org-footnote-action))
  9112. (t
  9113. (let (type path link line search (pos (point)))
  9114. (catch 'match
  9115. (save-excursion
  9116. (skip-chars-forward "^]\n\r")
  9117. (when (org-in-regexp org-bracket-link-regexp 1)
  9118. (setq link (org-extract-attributes
  9119. (org-link-unescape (org-match-string-no-properties 1))))
  9120. (while (string-match " *\n *" link)
  9121. (setq link (replace-match " " t t link)))
  9122. (setq link (org-link-expand-abbrev link))
  9123. (cond
  9124. ((or (file-name-absolute-p link)
  9125. (string-match "^\\.\\.?/" link))
  9126. (setq type "file" path link))
  9127. ((string-match org-link-re-with-space3 link)
  9128. (setq type (match-string 1 link) path (match-string 2 link)))
  9129. ((string-match "^help:+\\(.+\\)" link)
  9130. (setq type "help" path (match-string 1 link)))
  9131. (t (setq type "thisfile" path link)))
  9132. (throw 'match t)))
  9133. (when (get-text-property (point) 'org-linked-text)
  9134. (setq type "thisfile"
  9135. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9136. (1+ (point)) (point))
  9137. path (buffer-substring
  9138. (or (previous-single-property-change pos 'org-linked-text)
  9139. (point-min))
  9140. (or (next-single-property-change pos 'org-linked-text)
  9141. (point-max))))
  9142. (throw 'match t))
  9143. (save-excursion
  9144. (when (or (org-in-regexp org-angle-link-re)
  9145. (let ((match (org-in-regexp org-plain-link-re)))
  9146. ;; Check a plain link is not within a bracket link
  9147. (and match
  9148. (save-excursion
  9149. (progn
  9150. (goto-char (car match))
  9151. (not (org-in-regexp org-bracket-link-regexp))))))
  9152. (let ((line_ending (save-excursion (end-of-line) (point))))
  9153. ;; We are in a line before a plain or bracket link
  9154. (or (re-search-forward org-plain-link-re line_ending t)
  9155. (re-search-forward org-bracket-link-regexp line_ending t))))
  9156. (setq type (match-string 1)
  9157. path (org-link-unescape (match-string 2)))
  9158. (throw 'match t)))
  9159. (save-excursion
  9160. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9161. (setq type "tags"
  9162. path (match-string 1))
  9163. (while (string-match ":" path)
  9164. (setq path (replace-match "+" t t path)))
  9165. (throw 'match t)))
  9166. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9167. (setq type "tree-match"
  9168. path (match-string 1))
  9169. (throw 'match t)))
  9170. (unless path
  9171. (user-error "No link found"))
  9172. ;; switch back to reference buffer
  9173. ;; needed when if called in a temporary buffer through
  9174. ;; org-open-link-from-string
  9175. (with-current-buffer (or reference-buffer (current-buffer))
  9176. ;; Remove any trailing spaces in path
  9177. (if (string-match " +\\'" path)
  9178. (setq path (replace-match "" t t path)))
  9179. (if (and org-link-translation-function
  9180. (fboundp org-link-translation-function))
  9181. ;; Check if we need to translate the link
  9182. (let ((tmp (funcall org-link-translation-function type path)))
  9183. (setq type (car tmp) path (cdr tmp))))
  9184. (cond
  9185. ((assoc type org-link-protocols)
  9186. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9187. ((equal type "help")
  9188. (let ((f-or-v (intern path)))
  9189. (cond ((fboundp f-or-v)
  9190. (describe-function f-or-v))
  9191. ((boundp f-or-v)
  9192. (describe-variable f-or-v))
  9193. (t (error "Not a known function or variable")))))
  9194. ((equal type "mailto")
  9195. (let ((cmd (car org-link-mailto-program))
  9196. (args (cdr org-link-mailto-program)) args1
  9197. (address path) (subject "") a)
  9198. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9199. (setq address (match-string 1 path)
  9200. subject (org-link-escape (match-string 2 path))))
  9201. (while args
  9202. (cond
  9203. ((not (stringp (car args))) (push (pop args) args1))
  9204. (t (setq a (pop args))
  9205. (if (string-match "%a" a)
  9206. (setq a (replace-match address t t a)))
  9207. (if (string-match "%s" a)
  9208. (setq a (replace-match subject t t a)))
  9209. (push a args1))))
  9210. (apply cmd (nreverse args1))))
  9211. ((member type '("http" "https" "ftp" "news"))
  9212. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9213. (org-link-escape
  9214. path org-link-escape-chars-browser)
  9215. path))))
  9216. ((string= type "doi")
  9217. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9218. (org-link-escape
  9219. path org-link-escape-chars-browser)
  9220. path))))
  9221. ((member type '("message"))
  9222. (browse-url (concat type ":" path)))
  9223. ((string= type "tags")
  9224. (org-tags-view arg path))
  9225. ((string= type "tree-match")
  9226. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9227. ((string= type "file")
  9228. (if (string-match "::\\([0-9]+\\)\\'" path)
  9229. (setq line (string-to-number (match-string 1 path))
  9230. path (substring path 0 (match-beginning 0)))
  9231. (if (string-match "::\\(.+\\)\\'" path)
  9232. (setq search (match-string 1 path)
  9233. path (substring path 0 (match-beginning 0)))))
  9234. (if (string-match "[*?{]" (file-name-nondirectory path))
  9235. (dired path)
  9236. (org-open-file path arg line search)))
  9237. ((string= type "shell")
  9238. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9239. (cmd path))
  9240. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9241. (string-match org-confirm-shell-link-not-regexp cmd))
  9242. (not org-confirm-shell-link-function)
  9243. (funcall org-confirm-shell-link-function
  9244. (format "Execute \"%s\" in shell? "
  9245. (org-add-props cmd nil
  9246. 'face 'org-warning))))
  9247. (progn
  9248. (message "Executing %s" cmd)
  9249. (shell-command cmd buf)
  9250. (if (featurep 'midnight)
  9251. (setq clean-buffer-list-kill-buffer-names
  9252. (cons buf clean-buffer-list-kill-buffer-names))))
  9253. (error "Abort"))))
  9254. ((string= type "elisp")
  9255. (let ((cmd path))
  9256. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9257. (string-match org-confirm-elisp-link-not-regexp cmd))
  9258. (not org-confirm-elisp-link-function)
  9259. (funcall org-confirm-elisp-link-function
  9260. (format "Execute \"%s\" as elisp? "
  9261. (org-add-props cmd nil
  9262. 'face 'org-warning))))
  9263. (message "%s => %s" cmd
  9264. (if (equal (string-to-char cmd) ?\()
  9265. (eval (read cmd))
  9266. (call-interactively (read cmd))))
  9267. (error "Abort"))))
  9268. ((and (string= type "thisfile")
  9269. (run-hook-with-args-until-success
  9270. 'org-open-link-functions path)))
  9271. ((string= type "thisfile")
  9272. (if arg
  9273. (switch-to-buffer-other-window
  9274. (org-get-buffer-for-internal-link (current-buffer)))
  9275. (org-mark-ring-push))
  9276. (let ((cmd `(org-link-search
  9277. ,path
  9278. ,(cond ((equal arg '(4)) ''occur)
  9279. ((equal arg '(16)) ''org-occur))
  9280. ,pos)))
  9281. (condition-case nil (let ((org-link-search-inhibit-query t))
  9282. (eval cmd))
  9283. (error (progn (widen) (eval cmd))))))
  9284. (t (browse-url-at-point)))))))
  9285. (move-marker org-open-link-marker nil)
  9286. (run-hook-with-args 'org-follow-link-hook)))
  9287. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9288. "Offer links in the current entry and return the selected link.
  9289. If there is only one link, return it.
  9290. If NTH is an integer, return the NTH link found.
  9291. If ZERO is a string, check also this string for a link, and if
  9292. there is one, return it."
  9293. (with-current-buffer buffer
  9294. (save-excursion
  9295. (save-restriction
  9296. (widen)
  9297. (goto-char marker)
  9298. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9299. "\\(" org-angle-link-re "\\)\\|"
  9300. "\\(" org-plain-link-re "\\)"))
  9301. (cnt ?0)
  9302. (in-emacs (if (integerp nth) nil nth))
  9303. have-zero end links link c)
  9304. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9305. (push (match-string 0 zero) links)
  9306. (setq cnt (1- cnt) have-zero t))
  9307. (save-excursion
  9308. (org-back-to-heading t)
  9309. (setq end (save-excursion (outline-next-heading) (point)))
  9310. (while (re-search-forward re end t)
  9311. (push (match-string 0) links))
  9312. (setq links (org-uniquify (reverse links))))
  9313. (cond
  9314. ((null links)
  9315. (message "No links"))
  9316. ((equal (length links) 1)
  9317. (setq link (car links)))
  9318. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9319. (setq link (nth (if have-zero nth (1- nth)) links)))
  9320. (t ; we have to select a link
  9321. (save-excursion
  9322. (save-window-excursion
  9323. (delete-other-windows)
  9324. (with-output-to-temp-buffer "*Select Link*"
  9325. (mapc (lambda (l)
  9326. (if (not (string-match org-bracket-link-regexp l))
  9327. (princ (format "[%c] %s\n" (incf cnt)
  9328. (org-remove-angle-brackets l)))
  9329. (if (match-end 3)
  9330. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9331. (match-string 3 l) (match-string 1 l)))
  9332. (princ (format "[%c] %s\n" (incf cnt)
  9333. (match-string 1 l))))))
  9334. links))
  9335. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9336. (message "Select link to open, RET to open all:")
  9337. (setq c (read-char-exclusive))
  9338. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9339. (when (equal c ?q) (error "Abort"))
  9340. (if (equal c ?\C-m)
  9341. (setq link links)
  9342. (setq nth (- c ?0))
  9343. (if have-zero (setq nth (1+ nth)))
  9344. (unless (and (integerp nth) (>= (length links) nth))
  9345. (error "Invalid link selection"))
  9346. (setq link (nth (1- nth) links)))))
  9347. (cons link end))))))
  9348. ;; Add special file links that specify the way of opening
  9349. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9350. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9351. (defun org-open-file-with-system (path)
  9352. "Open file at PATH using the system way of opening it."
  9353. (org-open-file path 'system))
  9354. (defun org-open-file-with-emacs (path)
  9355. "Open file at PATH in Emacs."
  9356. (org-open-file path 'emacs))
  9357. ;;; File search
  9358. (defvar org-create-file-search-functions nil
  9359. "List of functions to construct the right search string for a file link.
  9360. These functions are called in turn with point at the location to
  9361. which the link should point.
  9362. A function in the hook should first test if it would like to
  9363. handle this file type, for example by checking the `major-mode'
  9364. or the file extension. If it decides not to handle this file, it
  9365. should just return nil to give other functions a chance. If it
  9366. does handle the file, it must return the search string to be used
  9367. when following the link. The search string will be part of the
  9368. file link, given after a double colon, and `org-open-at-point'
  9369. will automatically search for it. If special measures must be
  9370. taken to make the search successful, another function should be
  9371. added to the companion hook `org-execute-file-search-functions',
  9372. which see.
  9373. A function in this hook may also use `setq' to set the variable
  9374. `description' to provide a suggestion for the descriptive text to
  9375. be used for this link when it gets inserted into an Org-mode
  9376. buffer with \\[org-insert-link].")
  9377. (defvar org-execute-file-search-functions nil
  9378. "List of functions to execute a file search triggered by a link.
  9379. Functions added to this hook must accept a single argument, the
  9380. search string that was part of the file link, the part after the
  9381. double colon. The function must first check if it would like to
  9382. handle this search, for example by checking the `major-mode' or
  9383. the file extension. If it decides not to handle this search, it
  9384. should just return nil to give other functions a chance. If it
  9385. does handle the search, it must return a non-nil value to keep
  9386. other functions from trying.
  9387. Each function can access the current prefix argument through the
  9388. variable `current-prefix-argument'. Note that a single prefix is
  9389. used to force opening a link in Emacs, so it may be good to only
  9390. use a numeric or double prefix to guide the search function.
  9391. In case this is needed, a function in this hook can also restore
  9392. the window configuration before `org-open-at-point' was called using:
  9393. (set-window-configuration org-window-config-before-follow-link)")
  9394. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9395. (defun org-link-search (s &optional type avoid-pos stealth)
  9396. "Search for a link search option.
  9397. If S is surrounded by forward slashes, it is interpreted as a
  9398. regular expression. In org-mode files, this will create an `org-occur'
  9399. sparse tree. In ordinary files, `occur' will be used to list matches.
  9400. If the current buffer is in `dired-mode', grep will be used to search
  9401. in all files. If AVOID-POS is given, ignore matches near that position.
  9402. When optional argument STEALTH is non-nil, do not modify
  9403. visibility around point, thus ignoring
  9404. `org-show-hierarchy-above', `org-show-following-heading' and
  9405. `org-show-siblings' variables."
  9406. (let ((case-fold-search t)
  9407. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9408. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9409. (append '(("") (" ") ("\t") ("\n"))
  9410. org-emphasis-alist)
  9411. "\\|") "\\)"))
  9412. (pos (point))
  9413. (pre nil) (post nil)
  9414. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9415. (cond
  9416. ;; First check if there are any special search functions
  9417. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9418. ;; Now try the builtin stuff
  9419. ((and (equal (string-to-char s0) ?#)
  9420. (> (length s0) 1)
  9421. (save-excursion
  9422. (goto-char (point-min))
  9423. (and
  9424. (re-search-forward
  9425. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9426. (setq type 'dedicated
  9427. pos (match-beginning 0))))
  9428. ;; There is an exact target for this
  9429. (goto-char pos)
  9430. (org-back-to-heading t)))
  9431. ((save-excursion
  9432. (goto-char (point-min))
  9433. (and
  9434. (re-search-forward
  9435. (concat "<<" (regexp-quote s0) ">>") nil t)
  9436. (setq type 'dedicated
  9437. pos (match-beginning 0))))
  9438. ;; There is an exact target for this
  9439. (goto-char pos))
  9440. ((save-excursion
  9441. (goto-char (point-min))
  9442. (and
  9443. (re-search-forward
  9444. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9445. (setq type 'dedicated pos (match-beginning 0))))
  9446. ;; Found an invisible target.
  9447. (goto-char pos))
  9448. ((save-excursion
  9449. (goto-char (point-min))
  9450. (and
  9451. (re-search-forward
  9452. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9453. (setq type 'dedicated pos (match-beginning 0))))
  9454. ;; Found an element with a matching #+name affiliated keyword.
  9455. (goto-char pos))
  9456. ((and (string-match "^(\\(.*\\))$" s0)
  9457. (save-excursion
  9458. (goto-char (point-min))
  9459. (and
  9460. (re-search-forward
  9461. (concat "[^[]" (regexp-quote
  9462. (format org-coderef-label-format
  9463. (match-string 1 s0))))
  9464. nil t)
  9465. (setq type 'dedicated
  9466. pos (1+ (match-beginning 0))))))
  9467. ;; There is a coderef target for this
  9468. (goto-char pos))
  9469. ((string-match "^/\\(.*\\)/$" s)
  9470. ;; A regular expression
  9471. (cond
  9472. ((derived-mode-p 'org-mode)
  9473. (org-occur (match-string 1 s)))
  9474. (t (org-do-occur (match-string 1 s)))))
  9475. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9476. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9477. (goto-char (point-min))
  9478. (cond
  9479. ((let (case-fold-search)
  9480. (re-search-forward (format org-complex-heading-regexp-format
  9481. (regexp-quote s))
  9482. nil t))
  9483. ;; OK, found a match
  9484. (setq type 'dedicated)
  9485. (goto-char (match-beginning 0)))
  9486. ((and (not org-link-search-inhibit-query)
  9487. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9488. (y-or-n-p "No match - create this as a new heading? "))
  9489. (goto-char (point-max))
  9490. (or (bolp) (newline))
  9491. (insert "* " s "\n")
  9492. (beginning-of-line 0))
  9493. (t
  9494. (goto-char pos)
  9495. (error "No match"))))
  9496. (t
  9497. ;; A normal search string
  9498. (when (equal (string-to-char s) ?*)
  9499. ;; Anchor on headlines, post may include tags.
  9500. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9501. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9502. s (substring s 1)))
  9503. (remove-text-properties
  9504. 0 (length s)
  9505. '(face nil mouse-face nil keymap nil fontified nil) s)
  9506. ;; Make a series of regular expressions to find a match
  9507. (setq words (org-split-string s "[ \n\r\t]+")
  9508. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9509. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9510. "\\)" markers)
  9511. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9512. re2a (concat "[ \t\r\n]" re2a_)
  9513. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9514. re4 (concat "[^a-zA-Z_]" re4_)
  9515. re1 (concat pre re2 post)
  9516. re3 (concat pre (if pre re4_ re4) post)
  9517. re5 (concat pre ".*" re4)
  9518. re2 (concat pre re2)
  9519. re2a (concat pre (if pre re2a_ re2a))
  9520. re4 (concat pre (if pre re4_ re4))
  9521. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9522. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9523. re5 "\\)"
  9524. ))
  9525. (cond
  9526. ((eq type 'org-occur) (org-occur reall))
  9527. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9528. (t (goto-char (point-min))
  9529. (setq type 'fuzzy)
  9530. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9531. (org-search-not-self 1 re1 nil t)
  9532. (org-search-not-self 1 re2 nil t)
  9533. (org-search-not-self 1 re2a nil t)
  9534. (org-search-not-self 1 re3 nil t)
  9535. (org-search-not-self 1 re4 nil t)
  9536. (org-search-not-self 1 re5 nil t)
  9537. )
  9538. (goto-char (match-beginning 1))
  9539. (goto-char pos)
  9540. (error "No match"))))))
  9541. (and (derived-mode-p 'org-mode)
  9542. (not stealth)
  9543. (org-show-context 'link-search))
  9544. type))
  9545. (defun org-search-not-self (group &rest args)
  9546. "Execute `re-search-forward', but only accept matches that do not
  9547. enclose the position of `org-open-link-marker'."
  9548. (let ((m org-open-link-marker))
  9549. (catch 'exit
  9550. (while (apply 're-search-forward args)
  9551. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9552. (goto-char (match-end group))
  9553. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9554. (> (match-beginning 0) (marker-position m))
  9555. (< (match-end 0) (marker-position m)))
  9556. (save-match-data
  9557. (or (not (org-in-regexp
  9558. org-bracket-link-analytic-regexp 1))
  9559. (not (match-end 4)) ; no description
  9560. (and (<= (match-beginning 4) (point))
  9561. (>= (match-end 4) (point))))))
  9562. (throw 'exit (point))))))))
  9563. (defun org-get-buffer-for-internal-link (buffer)
  9564. "Return a buffer to be used for displaying the link target of internal links."
  9565. (cond
  9566. ((not org-display-internal-link-with-indirect-buffer)
  9567. buffer)
  9568. ((string-match "(Clone)$" (buffer-name buffer))
  9569. (message "Buffer is already a clone, not making another one")
  9570. ;; we also do not modify visibility in this case
  9571. buffer)
  9572. (t ; make a new indirect buffer for displaying the link
  9573. (let* ((bn (buffer-name buffer))
  9574. (ibn (concat bn "(Clone)"))
  9575. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9576. (with-current-buffer ib (org-overview))
  9577. ib))))
  9578. (defun org-do-occur (regexp &optional cleanup)
  9579. "Call the Emacs command `occur'.
  9580. If CLEANUP is non-nil, remove the printout of the regular expression
  9581. in the *Occur* buffer. This is useful if the regex is long and not useful
  9582. to read."
  9583. (occur regexp)
  9584. (when cleanup
  9585. (let ((cwin (selected-window)) win beg end)
  9586. (when (setq win (get-buffer-window "*Occur*"))
  9587. (select-window win))
  9588. (goto-char (point-min))
  9589. (when (re-search-forward "match[a-z]+" nil t)
  9590. (setq beg (match-end 0))
  9591. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9592. (setq end (1- (match-beginning 0)))))
  9593. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9594. (goto-char (point-min))
  9595. (select-window cwin))))
  9596. ;;; The mark ring for links jumps
  9597. (defvar org-mark-ring nil
  9598. "Mark ring for positions before jumps in Org-mode.")
  9599. (defvar org-mark-ring-last-goto nil
  9600. "Last position in the mark ring used to go back.")
  9601. ;; Fill and close the ring
  9602. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9603. (loop for i from 1 to org-mark-ring-length do
  9604. (push (make-marker) org-mark-ring))
  9605. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9606. org-mark-ring)
  9607. (defun org-mark-ring-push (&optional pos buffer)
  9608. "Put the current position or POS into the mark ring and rotate it."
  9609. (interactive)
  9610. (setq pos (or pos (point)))
  9611. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9612. (move-marker (car org-mark-ring)
  9613. (or pos (point))
  9614. (or buffer (current-buffer)))
  9615. (message "%s"
  9616. (substitute-command-keys
  9617. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9618. (defun org-mark-ring-goto (&optional n)
  9619. "Jump to the previous position in the mark ring.
  9620. With prefix arg N, jump back that many stored positions. When
  9621. called several times in succession, walk through the entire ring.
  9622. Org-mode commands jumping to a different position in the current file,
  9623. or to another Org-mode file, automatically push the old position
  9624. onto the ring."
  9625. (interactive "p")
  9626. (let (p m)
  9627. (if (eq last-command this-command)
  9628. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9629. (setq p org-mark-ring))
  9630. (setq org-mark-ring-last-goto p)
  9631. (setq m (car p))
  9632. (org-pop-to-buffer-same-window (marker-buffer m))
  9633. (goto-char m)
  9634. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9635. (defun org-remove-angle-brackets (s)
  9636. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9637. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9638. s)
  9639. (defun org-add-angle-brackets (s)
  9640. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9641. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9642. s)
  9643. (defun org-remove-double-quotes (s)
  9644. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9645. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9646. s)
  9647. ;;; Following specific links
  9648. (defun org-follow-timestamp-link ()
  9649. "Open an agenda view for the time-stamp date/range at point."
  9650. (cond
  9651. ((org-at-date-range-p t)
  9652. (let ((org-agenda-start-on-weekday)
  9653. (t1 (match-string 1))
  9654. (t2 (match-string 2)) tt1 tt2)
  9655. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9656. tt2 (time-to-days (org-time-string-to-time t2)))
  9657. (let ((org-agenda-buffer-tmp-name
  9658. (format "*Org Agenda(a:%s)"
  9659. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9660. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9661. ((org-at-timestamp-p t)
  9662. (let ((org-agenda-buffer-tmp-name
  9663. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9664. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9665. (substring (match-string 1) 0 10)))
  9666. 1)))
  9667. (t (error "This should not happen"))))
  9668. ;;; Following file links
  9669. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9670. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9671. (declare-function mailcap-mime-info
  9672. "mailcap" (string &optional request no-decode))
  9673. (defvar org-wait nil)
  9674. (defun org-open-file (path &optional in-emacs line search)
  9675. "Open the file at PATH.
  9676. First, this expands any special file name abbreviations. Then the
  9677. configuration variable `org-file-apps' is checked if it contains an
  9678. entry for this file type, and if yes, the corresponding command is launched.
  9679. If no application is found, Emacs simply visits the file.
  9680. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9681. With a double \\[universal-argument] \\[universal-argument] \
  9682. prefix arg, Org tries to avoid opening in Emacs
  9683. and to use an external application to visit the file.
  9684. Optional LINE specifies a line to go to, optional SEARCH a string
  9685. to search for. If LINE or SEARCH is given, the file will be
  9686. opened in Emacs, unless an entry from org-file-apps that makes
  9687. use of groups in a regexp matches.
  9688. If you want to change the way frames are used when following a
  9689. link, please customize `org-link-frame-setup'.
  9690. If the file does not exist, an error is thrown."
  9691. (let* ((file (if (equal path "")
  9692. buffer-file-name
  9693. (substitute-in-file-name (expand-file-name path))))
  9694. (file-apps (append org-file-apps (org-default-apps)))
  9695. (apps (org-remove-if
  9696. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9697. (apps-dlink (org-remove-if-not
  9698. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9699. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9700. (dirp (if remp nil (file-directory-p file)))
  9701. (file (if (and dirp org-open-directory-means-index-dot-org)
  9702. (concat (file-name-as-directory file) "index.org")
  9703. file))
  9704. (a-m-a-p (assq 'auto-mode apps))
  9705. (dfile (downcase file))
  9706. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9707. (link (cond ((and (eq line nil)
  9708. (eq search nil))
  9709. file)
  9710. (line
  9711. (concat file "::" (number-to-string line)))
  9712. (search
  9713. (concat file "::" search))))
  9714. (dlink (downcase link))
  9715. (old-buffer (current-buffer))
  9716. (old-pos (point))
  9717. (old-mode major-mode)
  9718. ext cmd link-match-data)
  9719. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9720. (setq ext (match-string 1 dfile))
  9721. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9722. (setq ext (match-string 1 dfile))))
  9723. (cond
  9724. ((member in-emacs '((16) system))
  9725. (setq cmd (cdr (assoc 'system apps))))
  9726. (in-emacs (setq cmd 'emacs))
  9727. (t
  9728. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9729. (and dirp (cdr (assoc 'directory apps)))
  9730. ; first, try matching against apps-dlink
  9731. ; if we get a match here, store the match data for later
  9732. (let ((match (assoc-default dlink apps-dlink
  9733. 'string-match)))
  9734. (if match
  9735. (progn (setq link-match-data (match-data))
  9736. match)
  9737. (progn (setq in-emacs (or in-emacs line search))
  9738. nil))) ; if we have no match in apps-dlink,
  9739. ; always open the file in emacs if line or search
  9740. ; is given (for backwards compatibility)
  9741. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9742. 'string-match)
  9743. (cdr (assoc ext apps))
  9744. (cdr (assoc t apps))))))
  9745. (when (eq cmd 'system)
  9746. (setq cmd (cdr (assoc 'system apps))))
  9747. (when (eq cmd 'default)
  9748. (setq cmd (cdr (assoc t apps))))
  9749. (when (eq cmd 'mailcap)
  9750. (require 'mailcap)
  9751. (mailcap-parse-mailcaps)
  9752. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9753. (command (mailcap-mime-info mime-type)))
  9754. (if (stringp command)
  9755. (setq cmd command)
  9756. (setq cmd 'emacs))))
  9757. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9758. (not (file-exists-p file))
  9759. (not org-open-non-existing-files))
  9760. (error "No such file: %s" file))
  9761. (cond
  9762. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9763. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9764. (while (string-match "['\"]%s['\"]" cmd)
  9765. (setq cmd (replace-match "%s" t t cmd)))
  9766. (while (string-match "%s" cmd)
  9767. (setq cmd (replace-match
  9768. (save-match-data
  9769. (shell-quote-argument
  9770. (convert-standard-filename file)))
  9771. t t cmd)))
  9772. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9773. (save-match-data
  9774. (let ((match-index 1)
  9775. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9776. (set-match-data link-match-data)
  9777. (while (<= match-index number-of-groups)
  9778. (let ((regex (concat "%" (number-to-string match-index)))
  9779. (replace-with (match-string match-index dlink)))
  9780. (while (string-match regex cmd)
  9781. (setq cmd (replace-match replace-with t t cmd))))
  9782. (setq match-index (+ match-index 1)))))
  9783. (save-window-excursion
  9784. (message "Running %s...done" cmd)
  9785. (start-process-shell-command cmd nil cmd)
  9786. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9787. ((or (stringp cmd)
  9788. (eq cmd 'emacs))
  9789. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9790. (widen)
  9791. (if line (org-goto-line line)
  9792. (if search (org-link-search search))))
  9793. ((consp cmd)
  9794. (let ((file (convert-standard-filename file)))
  9795. (save-match-data
  9796. (set-match-data link-match-data)
  9797. (eval cmd))))
  9798. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9799. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9800. (or (not (equal old-buffer (current-buffer)))
  9801. (not (equal old-pos (point))))
  9802. (org-mark-ring-push old-pos old-buffer))))
  9803. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9804. "This function returns non-nil if `entry' uses a regular
  9805. expression which should be matched against the whole link by
  9806. org-open-file.
  9807. It assumes that is the case when the entry uses a regular
  9808. expression which has at least one grouping construct and the
  9809. action is either a lisp form or a command string containing
  9810. '%1', i.e. using at least one subexpression match as a
  9811. parameter."
  9812. (let ((selector (car entry))
  9813. (action (cdr entry)))
  9814. (if (stringp selector)
  9815. (and (> (regexp-opt-depth selector) 0)
  9816. (or (and (stringp action)
  9817. (string-match "%[0-9]" action))
  9818. (consp action)))
  9819. nil)))
  9820. (defun org-default-apps ()
  9821. "Return the default applications for this operating system."
  9822. (cond
  9823. ((eq system-type 'darwin)
  9824. org-file-apps-defaults-macosx)
  9825. ((eq system-type 'windows-nt)
  9826. org-file-apps-defaults-windowsnt)
  9827. (t org-file-apps-defaults-gnu)))
  9828. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9829. "Convert extensions to regular expressions in the cars of LIST.
  9830. Also, weed out any non-string entries, because the return value is used
  9831. only for regexp matching.
  9832. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9833. point to the symbol `emacs', indicating that the file should
  9834. be opened in Emacs."
  9835. (append
  9836. (delq nil
  9837. (mapcar (lambda (x)
  9838. (if (not (stringp (car x)))
  9839. nil
  9840. (if (string-match "\\W" (car x))
  9841. x
  9842. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9843. list))
  9844. (if add-auto-mode
  9845. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9846. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9847. (defun org-file-remote-p (file)
  9848. "Test whether FILE specifies a location on a remote system.
  9849. Return non-nil if the location is indeed remote.
  9850. For example, the filename \"/user@host:/foo\" specifies a location
  9851. on the system \"/user@host:\"."
  9852. (cond ((fboundp 'file-remote-p)
  9853. (file-remote-p file))
  9854. ((fboundp 'tramp-handle-file-remote-p)
  9855. (tramp-handle-file-remote-p file))
  9856. ((and (boundp 'ange-ftp-name-format)
  9857. (string-match (car ange-ftp-name-format) file))
  9858. t)))
  9859. ;;;; Refiling
  9860. (defun org-get-org-file ()
  9861. "Read a filename, with default directory `org-directory'."
  9862. (let ((default (or org-default-notes-file remember-data-file)))
  9863. (read-file-name (format "File name [%s]: " default)
  9864. (file-name-as-directory org-directory)
  9865. default)))
  9866. (defun org-notes-order-reversed-p ()
  9867. "Check if the current file should receive notes in reversed order."
  9868. (cond
  9869. ((not org-reverse-note-order) nil)
  9870. ((eq t org-reverse-note-order) t)
  9871. ((not (listp org-reverse-note-order)) nil)
  9872. (t (catch 'exit
  9873. (let ((all org-reverse-note-order)
  9874. entry)
  9875. (while (setq entry (pop all))
  9876. (if (string-match (car entry) buffer-file-name)
  9877. (throw 'exit (cdr entry))))
  9878. nil)))))
  9879. (defvar org-refile-target-table nil
  9880. "The list of refile targets, created by `org-refile'.")
  9881. (defvar org-agenda-new-buffers nil
  9882. "Buffers created to visit agenda files.")
  9883. (defvar org-refile-cache nil
  9884. "Cache for refile targets.")
  9885. (defvar org-refile-markers nil
  9886. "All the markers used for caching refile locations.")
  9887. (defun org-refile-marker (pos)
  9888. "Get a new refile marker, but only if caching is in use."
  9889. (if (not org-refile-use-cache)
  9890. pos
  9891. (let ((m (make-marker)))
  9892. (move-marker m pos)
  9893. (push m org-refile-markers)
  9894. m)))
  9895. (defun org-refile-cache-clear ()
  9896. "Clear the refile cache and disable all the markers."
  9897. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9898. (setq org-refile-markers nil)
  9899. (setq org-refile-cache nil)
  9900. (message "Refile cache has been cleared"))
  9901. (defun org-refile-cache-check-set (set)
  9902. "Check if all the markers in the cache still have live buffers."
  9903. (let (marker)
  9904. (catch 'exit
  9905. (while (and set (setq marker (nth 3 (pop set))))
  9906. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9907. (when (and marker (null (marker-buffer marker)))
  9908. (message "not found") (sit-for 3)
  9909. (throw 'exit nil)))
  9910. t)))
  9911. (defun org-refile-cache-put (set &rest identifiers)
  9912. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9913. (let* ((key (sha1 (prin1-to-string identifiers)))
  9914. (entry (assoc key org-refile-cache)))
  9915. (if entry
  9916. (setcdr entry set)
  9917. (push (cons key set) org-refile-cache))))
  9918. (defun org-refile-cache-get (&rest identifiers)
  9919. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9920. (cond
  9921. ((not org-refile-cache) nil)
  9922. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9923. (t
  9924. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9925. org-refile-cache))))
  9926. (and set (org-refile-cache-check-set set) set)))))
  9927. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9928. "Produce a table with refile targets."
  9929. (let ((case-fold-search nil)
  9930. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9931. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9932. targets tgs txt re files f desc descre fast-path-p level pos0)
  9933. (message "Getting targets...")
  9934. (with-current-buffer (or default-buffer (current-buffer))
  9935. (while (setq entry (pop entries))
  9936. (setq files (car entry) desc (cdr entry))
  9937. (setq fast-path-p nil)
  9938. (cond
  9939. ((null files) (setq files (list (current-buffer))))
  9940. ((eq files 'org-agenda-files)
  9941. (setq files (org-agenda-files 'unrestricted)))
  9942. ((and (symbolp files) (fboundp files))
  9943. (setq files (funcall files)))
  9944. ((and (symbolp files) (boundp files))
  9945. (setq files (symbol-value files))))
  9946. (if (stringp files) (setq files (list files)))
  9947. (cond
  9948. ((eq (car desc) :tag)
  9949. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9950. ((eq (car desc) :todo)
  9951. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9952. ((eq (car desc) :regexp)
  9953. (setq descre (cdr desc)))
  9954. ((eq (car desc) :level)
  9955. (setq descre (concat "^\\*\\{" (number-to-string
  9956. (if org-odd-levels-only
  9957. (1- (* 2 (cdr desc)))
  9958. (cdr desc)))
  9959. "\\}[ \t]")))
  9960. ((eq (car desc) :maxlevel)
  9961. (setq fast-path-p t)
  9962. (setq descre (concat "^\\*\\{1," (number-to-string
  9963. (if org-odd-levels-only
  9964. (1- (* 2 (cdr desc)))
  9965. (cdr desc)))
  9966. "\\}[ \t]")))
  9967. (t (error "Bad refiling target description %s" desc)))
  9968. (while (setq f (pop files))
  9969. (with-current-buffer
  9970. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9971. (or
  9972. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9973. (progn
  9974. (if (bufferp f) (setq f (buffer-file-name
  9975. (buffer-base-buffer f))))
  9976. (setq f (and f (expand-file-name f)))
  9977. (if (eq org-refile-use-outline-path 'file)
  9978. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9979. (save-excursion
  9980. (save-restriction
  9981. (widen)
  9982. (goto-char (point-min))
  9983. (while (re-search-forward descre nil t)
  9984. (goto-char (setq pos0 (point-at-bol)))
  9985. (catch 'next
  9986. (when org-refile-target-verify-function
  9987. (save-match-data
  9988. (or (funcall org-refile-target-verify-function)
  9989. (throw 'next t))))
  9990. (when (and (looking-at org-complex-heading-regexp)
  9991. (not (member (match-string 4) excluded-entries))
  9992. (match-string 4))
  9993. (setq level (org-reduced-level
  9994. (- (match-end 1) (match-beginning 1)))
  9995. txt (org-link-display-format (match-string 4))
  9996. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9997. re (format org-complex-heading-regexp-format
  9998. (regexp-quote (match-string 4))))
  9999. (when org-refile-use-outline-path
  10000. (setq txt (mapconcat
  10001. 'org-protect-slash
  10002. (append
  10003. (if (eq org-refile-use-outline-path
  10004. 'file)
  10005. (list (file-name-nondirectory
  10006. (buffer-file-name
  10007. (buffer-base-buffer))))
  10008. (if (eq org-refile-use-outline-path
  10009. 'full-file-path)
  10010. (list (buffer-file-name
  10011. (buffer-base-buffer)))))
  10012. (org-get-outline-path fast-path-p
  10013. level txt)
  10014. (list txt))
  10015. "/")))
  10016. (push (list txt f re (org-refile-marker (point)))
  10017. tgs)))
  10018. (when (= (point) pos0)
  10019. ;; verification function has not moved point
  10020. (goto-char (point-at-eol))))))))
  10021. (when org-refile-use-cache
  10022. (org-refile-cache-put tgs (buffer-file-name) descre))
  10023. (setq targets (append tgs targets))
  10024. ))))
  10025. (message "Getting targets...done")
  10026. (nreverse targets)))
  10027. (defun org-protect-slash (s)
  10028. (while (string-match "/" s)
  10029. (setq s (replace-match "\\" t t s)))
  10030. s)
  10031. (defvar org-olpa (make-vector 20 nil))
  10032. (defun org-get-outline-path (&optional fastp level heading)
  10033. "Return the outline path to the current entry, as a list.
  10034. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10035. routine which makes outline path derivations for an entire file,
  10036. avoiding backtracing. Refile target collection makes use of that."
  10037. (if fastp
  10038. (progn
  10039. (if (> level 19)
  10040. (error "Outline path failure, more than 19 levels"))
  10041. (loop for i from level upto 19 do
  10042. (aset org-olpa i nil))
  10043. (prog1
  10044. (delq nil (append org-olpa nil))
  10045. (aset org-olpa level heading)))
  10046. (let (rtn case-fold-search)
  10047. (save-excursion
  10048. (save-restriction
  10049. (widen)
  10050. (while (org-up-heading-safe)
  10051. (when (looking-at org-complex-heading-regexp)
  10052. (push (org-match-string-no-properties 4) rtn)))
  10053. rtn)))))
  10054. (defun org-format-outline-path (path &optional width prefix separator)
  10055. "Format the outline path PATH for display.
  10056. WIDTH is the maximum number of characters that is available.
  10057. PREFIX is a prefix to be included in the returned string,
  10058. such as the file name.
  10059. SEPARATOR is inserted between the different parts of the path,
  10060. the default is \"/\"."
  10061. (setq width (or width 79))
  10062. (if prefix (setq width (- width (length prefix))))
  10063. (if (not path)
  10064. (or prefix "")
  10065. (let* ((nsteps (length path))
  10066. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10067. (maxwidth (if (<= total-width width)
  10068. 10000 ;; everything fits
  10069. ;; we need to shorten the level headings
  10070. (/ (- width nsteps) nsteps)))
  10071. (org-odd-levels-only nil)
  10072. (n 0)
  10073. (total (1+ (length prefix))))
  10074. (setq maxwidth (max maxwidth 10))
  10075. (concat prefix
  10076. (if prefix (or separator "/"))
  10077. (mapconcat
  10078. (lambda (h)
  10079. (setq n (1+ n))
  10080. (if (and (= n nsteps) (< maxwidth 10000))
  10081. (setq maxwidth (- total-width total)))
  10082. (if (< (length h) maxwidth)
  10083. (progn (setq total (+ total (length h) 1)) h)
  10084. (setq h (substring h 0 (- maxwidth 2))
  10085. total (+ total maxwidth 1))
  10086. (if (string-match "[ \t]+\\'" h)
  10087. (setq h (substring h 0 (match-beginning 0))))
  10088. (setq h (concat h "..")))
  10089. (org-add-props h nil 'face
  10090. (nth (% (1- n) org-n-level-faces)
  10091. org-level-faces))
  10092. h)
  10093. path (or separator "/"))))))
  10094. (defun org-display-outline-path (&optional file current separator just-return-string)
  10095. "Display the current outline path in the echo area.
  10096. If FILE is non-nil, prepend the output with the file name.
  10097. If CURRENT is non-nil, append the current heading to the output.
  10098. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10099. the different parts of the path and defaults to \"/\".
  10100. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10101. (interactive "P")
  10102. (let* (case-fold-search
  10103. message-log-max ; Don't populate the *Messages* buffer
  10104. (bfn (buffer-file-name (buffer-base-buffer)))
  10105. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10106. res)
  10107. (if current (setq path (append path
  10108. (save-excursion
  10109. (org-back-to-heading t)
  10110. (if (looking-at org-complex-heading-regexp)
  10111. (list (match-string 4)))))))
  10112. (setq res
  10113. (org-format-outline-path
  10114. path
  10115. (1- (frame-width))
  10116. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10117. separator))
  10118. (if just-return-string
  10119. (org-no-properties res)
  10120. (message "%s" res))))
  10121. (defvar org-refile-history nil
  10122. "History for refiling operations.")
  10123. (defvar org-after-refile-insert-hook nil
  10124. "Hook run after `org-refile' has inserted its stuff at the new location.
  10125. Note that this is still *before* the stuff will be removed from
  10126. the *old* location.")
  10127. (defvar org-capture-last-stored-marker)
  10128. (defvar org-refile-keep nil
  10129. "Non-nil means `org-refile' will copy instead of refile.")
  10130. (defun org-copy ()
  10131. "Like `org-refile', but copy."
  10132. (interactive)
  10133. (let ((org-refile-keep t))
  10134. (funcall 'org-refile nil nil nil "Copy")))
  10135. (defun org-refile (&optional goto default-buffer rfloc msg)
  10136. "Move the entry or entries at point to another heading.
  10137. The list of target headings is compiled using the information in
  10138. `org-refile-targets', which see.
  10139. At the target location, the entry is filed as a subitem of the target
  10140. heading. Depending on `org-reverse-note-order', the new subitem will
  10141. either be the first or the last subitem.
  10142. If there is an active region, all entries in that region will be moved.
  10143. However, the region must fulfill the requirement that the first heading
  10144. is the first one sets the top-level of the moved text - at most siblings
  10145. below it are allowed.
  10146. With prefix arg GOTO, the command will only visit the target location
  10147. and not actually move anything.
  10148. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10149. go to the location where the last refiling operation has put the subtree.
  10150. With a prefix argument of `2', refile to the running clock.
  10151. RFLOC can be a refile location obtained in a different way.
  10152. MSG is a string to replace \"Refile\" in the default prompt with
  10153. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10154. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10155. If you are using target caching (see `org-refile-use-cache'),
  10156. you have to clear the target cache in order to find new targets.
  10157. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10158. prefix argument (`C-u C-u C-u C-c C-w')."
  10159. (interactive "P")
  10160. (if (member goto '(0 (64)))
  10161. (org-refile-cache-clear)
  10162. (let* ((actionmsg (or msg "Refile"))
  10163. (cbuf (current-buffer))
  10164. (regionp (org-region-active-p))
  10165. (region-start (and regionp (region-beginning)))
  10166. (region-end (and regionp (region-end)))
  10167. (region-length (and regionp (- region-end region-start)))
  10168. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10169. pos it nbuf file re level reversed)
  10170. (setq last-command nil)
  10171. (when regionp
  10172. (goto-char region-start)
  10173. (or (bolp) (goto-char (point-at-bol)))
  10174. (setq region-start (point))
  10175. (unless (or (org-kill-is-subtree-p
  10176. (buffer-substring region-start region-end))
  10177. (prog1 org-refile-active-region-within-subtree
  10178. (org-toggle-heading)))
  10179. (error "The region is not a (sequence of) subtree(s)")))
  10180. (if (equal goto '(16))
  10181. (org-refile-goto-last-stored)
  10182. (when (or
  10183. (and (equal goto 2)
  10184. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10185. (prog1
  10186. (setq it (list (or org-clock-heading "running clock")
  10187. (buffer-file-name
  10188. (marker-buffer org-clock-hd-marker))
  10189. ""
  10190. (marker-position org-clock-hd-marker)))
  10191. (setq goto nil)))
  10192. (setq it (or rfloc
  10193. (let (heading-text)
  10194. (save-excursion
  10195. (unless goto
  10196. (org-back-to-heading t)
  10197. (setq heading-text
  10198. (nth 4 (org-heading-components))))
  10199. (org-refile-get-location
  10200. (cond (goto "Goto")
  10201. (regionp (concat actionmsg " region to"))
  10202. (t (concat actionmsg " subtree \""
  10203. heading-text "\" to")))
  10204. default-buffer
  10205. (and (not (equal '(4) goto))
  10206. org-refile-allow-creating-parent-nodes)
  10207. goto))))))
  10208. (setq file (nth 1 it)
  10209. re (nth 2 it)
  10210. pos (nth 3 it))
  10211. (if (and (not goto)
  10212. pos
  10213. (equal (buffer-file-name) file)
  10214. (if regionp
  10215. (and (>= pos region-start)
  10216. (<= pos region-end))
  10217. (and (>= pos (point))
  10218. (< pos (save-excursion
  10219. (org-end-of-subtree t t))))))
  10220. (error "Cannot refile to position inside the tree or region"))
  10221. (setq nbuf (or (find-buffer-visiting file)
  10222. (find-file-noselect file)))
  10223. (if goto
  10224. (progn
  10225. (org-pop-to-buffer-same-window nbuf)
  10226. (goto-char pos)
  10227. (org-show-context 'org-goto))
  10228. (if regionp
  10229. (progn
  10230. (org-kill-new (buffer-substring region-start region-end))
  10231. (org-save-markers-in-region region-start region-end))
  10232. (org-copy-subtree 1 nil t))
  10233. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10234. (find-file-noselect file)))
  10235. (setq reversed (org-notes-order-reversed-p))
  10236. (save-excursion
  10237. (save-restriction
  10238. (widen)
  10239. (if pos
  10240. (progn
  10241. (goto-char pos)
  10242. (looking-at org-outline-regexp)
  10243. (setq level (org-get-valid-level (funcall outline-level) 1))
  10244. (goto-char
  10245. (if reversed
  10246. (or (outline-next-heading) (point-max))
  10247. (or (save-excursion (org-get-next-sibling))
  10248. (org-end-of-subtree t t)
  10249. (point-max)))))
  10250. (setq level 1)
  10251. (if (not reversed)
  10252. (goto-char (point-max))
  10253. (goto-char (point-min))
  10254. (or (outline-next-heading) (goto-char (point-max)))))
  10255. (if (not (bolp)) (newline))
  10256. (org-paste-subtree level)
  10257. (when org-log-refile
  10258. (org-add-log-setup 'refile nil nil 'findpos
  10259. org-log-refile)
  10260. (unless (eq org-log-refile 'note)
  10261. (save-excursion (org-add-log-note))))
  10262. (and org-auto-align-tags
  10263. (let ((org-loop-over-headlines-in-active-region nil))
  10264. (org-set-tags nil t)))
  10265. (with-demoted-errors
  10266. (bookmark-set "org-refile-last-stored"))
  10267. ;; If we are refiling for capture, make sure that the
  10268. ;; last-capture pointers point here
  10269. (when (org-bound-and-true-p org-refile-for-capture)
  10270. (with-demoted-errors
  10271. (bookmark-set "org-capture-last-stored-marker"))
  10272. (move-marker org-capture-last-stored-marker (point)))
  10273. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10274. (run-hooks 'org-after-refile-insert-hook))))
  10275. (unless org-refile-keep
  10276. (if regionp
  10277. (delete-region (point) (+ (point) region-length))
  10278. (org-cut-subtree)))
  10279. (when (featurep 'org-inlinetask)
  10280. (org-inlinetask-remove-END-maybe))
  10281. (setq org-markers-to-move nil)
  10282. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10283. (defun org-refile-goto-last-stored ()
  10284. "Go to the location where the last refile was stored."
  10285. (interactive)
  10286. (bookmark-jump "org-refile-last-stored")
  10287. (message "This is the location of the last refile"))
  10288. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10289. no-exclude)
  10290. "Prompt the user for a refile location, using PROMPT.
  10291. PROMPT should not be suffixed with a colon and a space, because
  10292. this function appends the default value from
  10293. `org-refile-history' automatically, if that is not empty.
  10294. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10295. this is used for the GOTO interface."
  10296. (let ((org-refile-targets org-refile-targets)
  10297. (org-refile-use-outline-path org-refile-use-outline-path)
  10298. excluded-entries)
  10299. (when (and (derived-mode-p 'org-mode)
  10300. (not org-refile-use-cache)
  10301. (not no-exclude))
  10302. (org-map-tree
  10303. (lambda()
  10304. (setq excluded-entries
  10305. (append excluded-entries (list (org-get-heading t t)))))))
  10306. (setq org-refile-target-table
  10307. (org-refile-get-targets default-buffer excluded-entries)))
  10308. (unless org-refile-target-table
  10309. (error "No refile targets"))
  10310. (let* ((cbuf (current-buffer))
  10311. (partial-completion-mode nil)
  10312. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10313. (cfunc (if (and org-refile-use-outline-path
  10314. org-outline-path-complete-in-steps)
  10315. 'org-olpath-completing-read
  10316. 'org-icompleting-read))
  10317. (extra (if org-refile-use-outline-path "/" ""))
  10318. (cbnex (concat (buffer-name) extra))
  10319. (filename (and cfn (expand-file-name cfn)))
  10320. (tbl (mapcar
  10321. (lambda (x)
  10322. (if (and (not (member org-refile-use-outline-path
  10323. '(file full-file-path)))
  10324. (not (equal filename (nth 1 x))))
  10325. (cons (concat (car x) extra " ("
  10326. (file-name-nondirectory (nth 1 x)) ")")
  10327. (cdr x))
  10328. (cons (concat (car x) extra) (cdr x))))
  10329. org-refile-target-table))
  10330. (completion-ignore-case t)
  10331. cdef
  10332. (prompt (concat prompt
  10333. (or (and (car org-refile-history)
  10334. (concat " (default " (car org-refile-history) ")"))
  10335. (and (assoc cbnex tbl) (setq cdef cbnex)
  10336. (concat " (default " cbnex ")"))) ": "))
  10337. pa answ parent-target child parent old-hist)
  10338. (setq old-hist org-refile-history)
  10339. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10340. nil 'org-refile-history (or cdef (car org-refile-history))))
  10341. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10342. (org-refile-check-position pa)
  10343. (if pa
  10344. (progn
  10345. (when (or (not org-refile-history)
  10346. (not (eq old-hist org-refile-history))
  10347. (not (equal (car pa) (car org-refile-history))))
  10348. (setq org-refile-history
  10349. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10350. org-refile-history
  10351. (cdr org-refile-history))))
  10352. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10353. (pop org-refile-history)))
  10354. pa)
  10355. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10356. (progn
  10357. (setq parent (match-string 1 answ)
  10358. child (match-string 2 answ))
  10359. (setq parent-target (or (assoc parent tbl)
  10360. (assoc (concat parent "/") tbl)))
  10361. (when (and parent-target
  10362. (or (eq new-nodes t)
  10363. (and (eq new-nodes 'confirm)
  10364. (y-or-n-p (format "Create new node \"%s\"? "
  10365. child)))))
  10366. (org-refile-new-child parent-target child)))
  10367. (error "Invalid target location")))))
  10368. (declare-function org-string-nw-p "org-macs" (s))
  10369. (defun org-refile-check-position (refile-pointer)
  10370. "Check if the refile pointer matches the headline to which it points."
  10371. (let* ((file (nth 1 refile-pointer))
  10372. (re (nth 2 refile-pointer))
  10373. (pos (nth 3 refile-pointer))
  10374. buffer)
  10375. (if (and (not (markerp pos)) (not file))
  10376. (error "Please save the buffer to a file before refiling")
  10377. (when (org-string-nw-p re)
  10378. (setq buffer (if (markerp pos)
  10379. (marker-buffer pos)
  10380. (or (find-buffer-visiting file)
  10381. (find-file-noselect file))))
  10382. (with-current-buffer buffer
  10383. (save-excursion
  10384. (save-restriction
  10385. (widen)
  10386. (goto-char pos)
  10387. (beginning-of-line 1)
  10388. (unless (org-looking-at-p re)
  10389. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10390. (defun org-refile-new-child (parent-target child)
  10391. "Use refile target PARENT-TARGET to add new CHILD below it."
  10392. (unless parent-target
  10393. (error "Cannot find parent for new node"))
  10394. (let ((file (nth 1 parent-target))
  10395. (pos (nth 3 parent-target))
  10396. level)
  10397. (with-current-buffer (or (find-buffer-visiting file)
  10398. (find-file-noselect file))
  10399. (save-excursion
  10400. (save-restriction
  10401. (widen)
  10402. (if pos
  10403. (goto-char pos)
  10404. (goto-char (point-max))
  10405. (if (not (bolp)) (newline)))
  10406. (when (looking-at org-outline-regexp)
  10407. (setq level (funcall outline-level))
  10408. (org-end-of-subtree t t))
  10409. (org-back-over-empty-lines)
  10410. (insert "\n" (make-string
  10411. (if pos (org-get-valid-level level 1) 1) ?*)
  10412. " " child "\n")
  10413. (beginning-of-line 0)
  10414. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10415. (defun org-olpath-completing-read (prompt collection &rest args)
  10416. "Read an outline path like a file name."
  10417. (let ((thetable collection)
  10418. (org-completion-use-ido nil) ; does not work with ido.
  10419. (org-completion-use-iswitchb nil)) ; or iswitchb
  10420. (apply
  10421. 'org-icompleting-read prompt
  10422. (lambda (string predicate &optional flag)
  10423. (let (rtn r f (l (length string)))
  10424. (cond
  10425. ((eq flag nil)
  10426. ;; try completion
  10427. (try-completion string thetable))
  10428. ((eq flag t)
  10429. ;; all-completions
  10430. (setq rtn (all-completions string thetable predicate))
  10431. (mapcar
  10432. (lambda (x)
  10433. (setq r (substring x l))
  10434. (if (string-match " ([^)]*)$" x)
  10435. (setq f (match-string 0 x))
  10436. (setq f ""))
  10437. (if (string-match "/" r)
  10438. (concat string (substring r 0 (match-end 0)) f)
  10439. x))
  10440. rtn))
  10441. ((eq flag 'lambda)
  10442. ;; exact match?
  10443. (assoc string thetable)))))
  10444. args)))
  10445. ;;;; Dynamic blocks
  10446. (defun org-find-dblock (name)
  10447. "Find the first dynamic block with name NAME in the buffer.
  10448. If not found, stay at current position and return nil."
  10449. (let ((case-fold-search t) pos)
  10450. (save-excursion
  10451. (goto-char (point-min))
  10452. (setq pos (and (re-search-forward
  10453. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10454. (match-beginning 0))))
  10455. (if pos (goto-char pos))
  10456. pos))
  10457. (defconst org-dblock-start-re
  10458. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10459. "Matches the start line of a dynamic block, with parameters.")
  10460. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10461. "Matches the end of a dynamic block.")
  10462. (defun org-create-dblock (plist)
  10463. "Create a dynamic block section, with parameters taken from PLIST.
  10464. PLIST must contain a :name entry which is used as name of the block."
  10465. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10466. (end-of-line 1)
  10467. (newline))
  10468. (let ((col (current-column))
  10469. (name (plist-get plist :name)))
  10470. (insert "#+BEGIN: " name)
  10471. (while plist
  10472. (if (eq (car plist) :name)
  10473. (setq plist (cddr plist))
  10474. (insert " " (prin1-to-string (pop plist)))))
  10475. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10476. (beginning-of-line -2)))
  10477. (defun org-prepare-dblock ()
  10478. "Prepare dynamic block for refresh.
  10479. This empties the block, puts the cursor at the insert position and returns
  10480. the property list including an extra property :name with the block name."
  10481. (unless (looking-at org-dblock-start-re)
  10482. (error "Not at a dynamic block"))
  10483. (let* ((begdel (1+ (match-end 0)))
  10484. (name (org-no-properties (match-string 1)))
  10485. (params (append (list :name name)
  10486. (read (concat "(" (match-string 3) ")")))))
  10487. (save-excursion
  10488. (beginning-of-line 1)
  10489. (skip-chars-forward " \t")
  10490. (setq params (plist-put params :indentation-column (current-column))))
  10491. (unless (re-search-forward org-dblock-end-re nil t)
  10492. (error "Dynamic block not terminated"))
  10493. (setq params
  10494. (append params
  10495. (list :content (buffer-substring
  10496. begdel (match-beginning 0)))))
  10497. (delete-region begdel (match-beginning 0))
  10498. (goto-char begdel)
  10499. (open-line 1)
  10500. params))
  10501. (defun org-map-dblocks (&optional command)
  10502. "Apply COMMAND to all dynamic blocks in the current buffer.
  10503. If COMMAND is not given, use `org-update-dblock'."
  10504. (let ((cmd (or command 'org-update-dblock)))
  10505. (save-excursion
  10506. (goto-char (point-min))
  10507. (while (re-search-forward org-dblock-start-re nil t)
  10508. (goto-char (match-beginning 0))
  10509. (save-excursion
  10510. (condition-case nil
  10511. (funcall cmd)
  10512. (error (message "Error during update of dynamic block"))))
  10513. (unless (re-search-forward org-dblock-end-re nil t)
  10514. (error "Dynamic block not terminated"))))))
  10515. (defun org-dblock-update (&optional arg)
  10516. "User command for updating dynamic blocks.
  10517. Update the dynamic block at point. With prefix ARG, update all dynamic
  10518. blocks in the buffer."
  10519. (interactive "P")
  10520. (if arg
  10521. (org-update-all-dblocks)
  10522. (or (looking-at org-dblock-start-re)
  10523. (org-beginning-of-dblock))
  10524. (org-update-dblock)))
  10525. (defun org-update-dblock ()
  10526. "Update the dynamic block at point.
  10527. This means to empty the block, parse for parameters and then call
  10528. the correct writing function."
  10529. (interactive)
  10530. (save-window-excursion
  10531. (let* ((pos (point))
  10532. (line (org-current-line))
  10533. (params (org-prepare-dblock))
  10534. (name (plist-get params :name))
  10535. (indent (plist-get params :indentation-column))
  10536. (cmd (intern (concat "org-dblock-write:" name))))
  10537. (message "Updating dynamic block `%s' at line %d..." name line)
  10538. (funcall cmd params)
  10539. (message "Updating dynamic block `%s' at line %d...done" name line)
  10540. (goto-char pos)
  10541. (when (and indent (> indent 0))
  10542. (setq indent (make-string indent ?\ ))
  10543. (save-excursion
  10544. (org-beginning-of-dblock)
  10545. (forward-line 1)
  10546. (while (not (looking-at org-dblock-end-re))
  10547. (insert indent)
  10548. (beginning-of-line 2))
  10549. (when (looking-at org-dblock-end-re)
  10550. (and (looking-at "[ \t]+")
  10551. (replace-match ""))
  10552. (insert indent)))))))
  10553. (defun org-beginning-of-dblock ()
  10554. "Find the beginning of the dynamic block at point.
  10555. Error if there is no such block at point."
  10556. (let ((pos (point))
  10557. beg)
  10558. (end-of-line 1)
  10559. (if (and (re-search-backward org-dblock-start-re nil t)
  10560. (setq beg (match-beginning 0))
  10561. (re-search-forward org-dblock-end-re nil t)
  10562. (> (match-end 0) pos))
  10563. (goto-char beg)
  10564. (goto-char pos)
  10565. (error "Not in a dynamic block"))))
  10566. (defun org-update-all-dblocks ()
  10567. "Update all dynamic blocks in the buffer.
  10568. This function can be used in a hook."
  10569. (interactive)
  10570. (when (derived-mode-p 'org-mode)
  10571. (org-map-dblocks 'org-update-dblock)))
  10572. ;;;; Completion
  10573. (defun org-get-export-keywords ()
  10574. "Return a list of all currently understood export keywords.
  10575. Export keywords include options, block names, attributes and
  10576. keywords relative to each registered export back-end."
  10577. (delq nil
  10578. (let (keywords)
  10579. (mapc
  10580. (lambda (back-end)
  10581. (let ((props (cdr back-end)))
  10582. ;; Back-end name (for keywords, like #+LATEX:)
  10583. (push (upcase (symbol-name (car back-end))) keywords)
  10584. ;; Back-end options.
  10585. (mapc (lambda (option) (push (cadr option) keywords))
  10586. (plist-get (cdr back-end) :options-alist))))
  10587. (org-bound-and-true-p org-export-registered-backends))
  10588. keywords)))
  10589. (defconst org-options-keywords
  10590. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10591. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10592. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10593. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10594. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
  10595. (defcustom org-structure-template-alist
  10596. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10597. "<src lang=\"?\">\n\n</src>")
  10598. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10599. "<example>\n?\n</example>")
  10600. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10601. "<quote>\n?\n</quote>")
  10602. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10603. "<verse>\n?\n</verse>")
  10604. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10605. "<verbatim>\n?\n</verbatim>")
  10606. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10607. "<center>\n?\n</center>")
  10608. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10609. "<literal style=\"latex\">\n?\n</literal>")
  10610. ("L" "#+LaTeX: "
  10611. "<literal style=\"latex\">?</literal>")
  10612. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10613. "<literal style=\"html\">\n?\n</literal>")
  10614. ("H" "#+HTML: "
  10615. "<literal style=\"html\">?</literal>")
  10616. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10617. ("A" "#+ASCII: ")
  10618. ("i" "#+INDEX: ?"
  10619. "#+INDEX: ?")
  10620. ("I" "#+INCLUDE: %file ?"
  10621. "<include file=%file markup=\"?\">"))
  10622. "Structure completion elements.
  10623. This is a list of abbreviation keys and values. The value gets inserted
  10624. if you type `<' followed by the key and then press the completion key,
  10625. usually `M-TAB'. %file will be replaced by a file name after prompting
  10626. for the file using completion. The cursor will be placed at the position
  10627. of the `?` in the template.
  10628. There are two templates for each key, the first uses the original Org syntax,
  10629. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10630. the default when the /org-mtags.el/ module has been loaded. See also the
  10631. variable `org-mtags-prefer-muse-templates'."
  10632. :group 'org-completion
  10633. :type '(repeat
  10634. (string :tag "Key")
  10635. (string :tag "Template")
  10636. (string :tag "Muse Template")))
  10637. (defun org-try-structure-completion ()
  10638. "Try to complete a structure template before point.
  10639. This looks for strings like \"<e\" on an otherwise empty line and
  10640. expands them."
  10641. (let ((l (buffer-substring (point-at-bol) (point)))
  10642. a)
  10643. (when (and (looking-at "[ \t]*$")
  10644. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10645. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10646. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10647. (match-beginning 1)) a)
  10648. t)))
  10649. (defun org-complete-expand-structure-template (start cell)
  10650. "Expand a structure template."
  10651. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10652. (rpl (nth (if musep 2 1) cell))
  10653. (ind ""))
  10654. (delete-region start (point))
  10655. (when (string-match "\\`#\\+" rpl)
  10656. (cond
  10657. ((bolp))
  10658. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10659. (setq ind (buffer-substring (point-at-bol) (point))))
  10660. (t (newline))))
  10661. (setq start (point))
  10662. (if (string-match "%file" rpl)
  10663. (setq rpl (replace-match
  10664. (concat
  10665. "\""
  10666. (save-match-data
  10667. (abbreviate-file-name (read-file-name "Include file: ")))
  10668. "\"")
  10669. t t rpl)))
  10670. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10671. (concat "\n" ind)))
  10672. (insert rpl)
  10673. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10674. ;;;; TODO, DEADLINE, Comments
  10675. (defun org-toggle-comment ()
  10676. "Change the COMMENT state of an entry."
  10677. (interactive)
  10678. (save-excursion
  10679. (org-back-to-heading)
  10680. (let (case-fold-search)
  10681. (cond
  10682. ((looking-at (format org-heading-keyword-regexp-format
  10683. org-comment-string))
  10684. (goto-char (match-end 1))
  10685. (looking-at (concat " +" org-comment-string))
  10686. (replace-match "" t t)
  10687. (when (eolp) (insert " ")))
  10688. ((looking-at org-outline-regexp)
  10689. (goto-char (match-end 0))
  10690. (insert org-comment-string " "))))))
  10691. (defvar org-last-todo-state-is-todo nil
  10692. "This is non-nil when the last TODO state change led to a TODO state.
  10693. If the last change removed the TODO tag or switched to DONE, then
  10694. this is nil.")
  10695. (defvar org-setting-tags nil) ; dynamically skipped
  10696. (defvar org-todo-setup-filter-hook nil
  10697. "Hook for functions that pre-filter todo specs.
  10698. Each function takes a todo spec and returns either nil or the spec
  10699. transformed into canonical form." )
  10700. (defvar org-todo-get-default-hook nil
  10701. "Hook for functions that get a default item for todo.
  10702. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10703. nil or a string to be used for the todo mark." )
  10704. (defvar org-agenda-headline-snapshot-before-repeat)
  10705. (defun org-current-effective-time ()
  10706. "Return current time adjusted for `org-extend-today-until' variable."
  10707. (let* ((ct (org-current-time))
  10708. (dct (decode-time ct))
  10709. (ct1
  10710. (cond
  10711. (org-use-last-clock-out-time-as-effective-time
  10712. (or (org-clock-get-last-clock-out-time) ct))
  10713. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10714. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10715. (t ct))))
  10716. ct1))
  10717. (defun org-todo-yesterday (&optional arg)
  10718. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10719. (interactive "P")
  10720. (if (eq major-mode 'org-agenda-mode)
  10721. (apply 'org-agenda-todo-yesterday arg)
  10722. (let* ((hour (third (decode-time
  10723. (org-current-time))))
  10724. (org-extend-today-until (1+ hour)))
  10725. (org-todo arg))))
  10726. (defvar org-block-entry-blocking ""
  10727. "First entry preventing the TODO state change.")
  10728. (defun org-todo (&optional arg)
  10729. "Change the TODO state of an item.
  10730. The state of an item is given by a keyword at the start of the heading,
  10731. like
  10732. *** TODO Write paper
  10733. *** DONE Call mom
  10734. The different keywords are specified in the variable `org-todo-keywords'.
  10735. By default the available states are \"TODO\" and \"DONE\".
  10736. So for this example: when the item starts with TODO, it is changed to DONE.
  10737. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10738. DONE are present, add TODO at the beginning of the heading.
  10739. With \\[universal-argument] prefix arg, use completion to determine the new \
  10740. state.
  10741. With numeric prefix arg, switch to that state.
  10742. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10743. keywords (nextset).
  10744. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10745. With a numeric prefix arg of 0, inhibit note taking for the change.
  10746. For calling through lisp, arg is also interpreted in the following way:
  10747. 'none -> empty state
  10748. \"\"(empty string) -> switch to empty state
  10749. 'done -> switch to DONE
  10750. 'nextset -> switch to the next set of keywords
  10751. 'previousset -> switch to the previous set of keywords
  10752. \"WAITING\" -> switch to the specified keyword, but only if it
  10753. really is a member of `org-todo-keywords'."
  10754. (interactive "P")
  10755. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10756. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10757. 'region-start-level 'region))
  10758. org-loop-over-headlines-in-active-region)
  10759. (org-map-entries
  10760. `(org-todo ,arg)
  10761. org-loop-over-headlines-in-active-region
  10762. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10763. (if (equal arg '(16)) (setq arg 'nextset))
  10764. (let ((org-blocker-hook org-blocker-hook)
  10765. commentp
  10766. case-fold-search)
  10767. (when (equal arg '(64))
  10768. (setq arg nil org-blocker-hook nil))
  10769. (when (and org-blocker-hook
  10770. (or org-inhibit-blocking
  10771. (org-entry-get nil "NOBLOCKING")))
  10772. (setq org-blocker-hook nil))
  10773. (save-excursion
  10774. (catch 'exit
  10775. (org-back-to-heading t)
  10776. (when (looking-at (concat "^\\*+ " org-comment-string))
  10777. (org-toggle-comment)
  10778. (setq commentp t))
  10779. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10780. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10781. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10782. (let* ((match-data (match-data))
  10783. (startpos (point-at-bol))
  10784. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10785. (org-log-done org-log-done)
  10786. (org-log-repeat org-log-repeat)
  10787. (org-todo-log-states org-todo-log-states)
  10788. (org-inhibit-logging
  10789. (if (equal arg 0)
  10790. (progn (setq arg nil) 'note) org-inhibit-logging))
  10791. (this (match-string 1))
  10792. (hl-pos (match-beginning 0))
  10793. (head (org-get-todo-sequence-head this))
  10794. (ass (assoc head org-todo-kwd-alist))
  10795. (interpret (nth 1 ass))
  10796. (done-word (nth 3 ass))
  10797. (final-done-word (nth 4 ass))
  10798. (org-last-state (or this ""))
  10799. (completion-ignore-case t)
  10800. (member (member this org-todo-keywords-1))
  10801. (tail (cdr member))
  10802. (org-state (cond
  10803. ((and org-todo-key-trigger
  10804. (or (and (equal arg '(4))
  10805. (eq org-use-fast-todo-selection 'prefix))
  10806. (and (not arg) org-use-fast-todo-selection
  10807. (not (eq org-use-fast-todo-selection
  10808. 'prefix)))))
  10809. ;; Use fast selection
  10810. (org-fast-todo-selection))
  10811. ((and (equal arg '(4))
  10812. (or (not org-use-fast-todo-selection)
  10813. (not org-todo-key-trigger)))
  10814. ;; Read a state with completion
  10815. (org-icompleting-read
  10816. "State: " (mapcar (lambda(x) (list x))
  10817. org-todo-keywords-1)
  10818. nil t))
  10819. ((eq arg 'right)
  10820. (if this
  10821. (if tail (car tail) nil)
  10822. (car org-todo-keywords-1)))
  10823. ((eq arg 'left)
  10824. (if (equal member org-todo-keywords-1)
  10825. nil
  10826. (if this
  10827. (nth (- (length org-todo-keywords-1)
  10828. (length tail) 2)
  10829. org-todo-keywords-1)
  10830. (org-last org-todo-keywords-1))))
  10831. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10832. (setq arg nil))) ; hack to fall back to cycling
  10833. (arg
  10834. ;; user or caller requests a specific state
  10835. (cond
  10836. ((equal arg "") nil)
  10837. ((eq arg 'none) nil)
  10838. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10839. ((eq arg 'nextset)
  10840. (or (car (cdr (member head org-todo-heads)))
  10841. (car org-todo-heads)))
  10842. ((eq arg 'previousset)
  10843. (let ((org-todo-heads (reverse org-todo-heads)))
  10844. (or (car (cdr (member head org-todo-heads)))
  10845. (car org-todo-heads))))
  10846. ((car (member arg org-todo-keywords-1)))
  10847. ((stringp arg)
  10848. (error "State `%s' not valid in this file" arg))
  10849. ((nth (1- (prefix-numeric-value arg))
  10850. org-todo-keywords-1))))
  10851. ((null member) (or head (car org-todo-keywords-1)))
  10852. ((equal this final-done-word) nil) ;; -> make empty
  10853. ((null tail) nil) ;; -> first entry
  10854. ((memq interpret '(type priority))
  10855. (if (eq this-command last-command)
  10856. (car tail)
  10857. (if (> (length tail) 0)
  10858. (or done-word (car org-done-keywords))
  10859. nil)))
  10860. (t
  10861. (car tail))))
  10862. (org-state (or
  10863. (run-hook-with-args-until-success
  10864. 'org-todo-get-default-hook org-state org-last-state)
  10865. org-state))
  10866. (next (if org-state (concat " " org-state " ") " "))
  10867. (change-plist (list :type 'todo-state-change :from this :to org-state
  10868. :position startpos))
  10869. dolog now-done-p)
  10870. (when org-blocker-hook
  10871. (setq org-last-todo-state-is-todo
  10872. (not (member this org-done-keywords)))
  10873. (unless (save-excursion
  10874. (save-match-data
  10875. (org-with-wide-buffer
  10876. (run-hook-with-args-until-failure
  10877. 'org-blocker-hook change-plist))))
  10878. (if (org-called-interactively-p 'interactive)
  10879. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10880. this org-state org-block-entry-blocking)
  10881. ;; fail silently
  10882. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10883. this org-state org-block-entry-blocking)
  10884. (throw 'exit nil))))
  10885. (store-match-data match-data)
  10886. (replace-match next t t)
  10887. (unless (pos-visible-in-window-p hl-pos)
  10888. (message "TODO state changed to %s" (org-trim next)))
  10889. (unless head
  10890. (setq head (org-get-todo-sequence-head org-state)
  10891. ass (assoc head org-todo-kwd-alist)
  10892. interpret (nth 1 ass)
  10893. done-word (nth 3 ass)
  10894. final-done-word (nth 4 ass)))
  10895. (when (memq arg '(nextset previousset))
  10896. (message "Keyword-Set %d/%d: %s"
  10897. (- (length org-todo-sets) -1
  10898. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10899. (length org-todo-sets)
  10900. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10901. (setq org-last-todo-state-is-todo
  10902. (not (member org-state org-done-keywords)))
  10903. (setq now-done-p (and (member org-state org-done-keywords)
  10904. (not (member this org-done-keywords))))
  10905. (and logging (org-local-logging logging))
  10906. (when (and (or org-todo-log-states org-log-done)
  10907. (not (eq org-inhibit-logging t))
  10908. (not (memq arg '(nextset previousset))))
  10909. ;; we need to look at recording a time and note
  10910. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10911. (nth 2 (assoc this org-todo-log-states))))
  10912. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10913. (setq dolog 'time))
  10914. (when (and org-state
  10915. (member org-state org-not-done-keywords)
  10916. (not (member this org-not-done-keywords)))
  10917. ;; This is now a todo state and was not one before
  10918. ;; If there was a CLOSED time stamp, get rid of it.
  10919. (org-add-planning-info nil nil 'closed))
  10920. (when (and now-done-p org-log-done)
  10921. ;; It is now done, and it was not done before
  10922. (org-add-planning-info 'closed (org-current-effective-time))
  10923. (if (and (not dolog) (eq 'note org-log-done))
  10924. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10925. (when (and org-state dolog)
  10926. ;; This is a non-nil state, and we need to log it
  10927. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10928. ;; Fixup tag positioning
  10929. (org-todo-trigger-tag-changes org-state)
  10930. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10931. (when org-provide-todo-statistics
  10932. (org-update-parent-todo-statistics))
  10933. (run-hooks 'org-after-todo-state-change-hook)
  10934. (if (and arg (not (member org-state org-done-keywords)))
  10935. (setq head (org-get-todo-sequence-head org-state)))
  10936. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10937. ;; Do we need to trigger a repeat?
  10938. (when now-done-p
  10939. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10940. ;; This is for the agenda, take a snapshot of the headline.
  10941. (save-match-data
  10942. (setq org-agenda-headline-snapshot-before-repeat
  10943. (org-get-heading))))
  10944. (org-auto-repeat-maybe org-state))
  10945. ;; Fixup cursor location if close to the keyword
  10946. (if (and (outline-on-heading-p)
  10947. (not (bolp))
  10948. (save-excursion (beginning-of-line 1)
  10949. (looking-at org-todo-line-regexp))
  10950. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10951. (progn
  10952. (goto-char (or (match-end 2) (match-end 1)))
  10953. (and (looking-at " ") (just-one-space))))
  10954. (when org-trigger-hook
  10955. (save-excursion
  10956. (run-hook-with-args 'org-trigger-hook change-plist)))
  10957. (when commentp (org-toggle-comment))))))))
  10958. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10959. "Block turning an entry into a TODO, using the hierarchy.
  10960. This checks whether the current task should be blocked from state
  10961. changes. Such blocking occurs when:
  10962. 1. The task has children which are not all in a completed state.
  10963. 2. A task has a parent with the property :ORDERED:, and there
  10964. are siblings prior to the current task with incomplete
  10965. status.
  10966. 3. The parent of the task is blocked because it has siblings that should
  10967. be done first, or is child of a block grandparent TODO entry."
  10968. (if (not org-enforce-todo-dependencies)
  10969. t ; if locally turned off don't block
  10970. (catch 'dont-block
  10971. ;; If this is not a todo state change, or if this entry is already DONE,
  10972. ;; do not block
  10973. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10974. (member (plist-get change-plist :from)
  10975. (cons 'done org-done-keywords))
  10976. (member (plist-get change-plist :to)
  10977. (cons 'todo org-not-done-keywords))
  10978. (not (plist-get change-plist :to)))
  10979. (throw 'dont-block t))
  10980. ;; If this task has children, and any are undone, it's blocked
  10981. (save-excursion
  10982. (org-back-to-heading t)
  10983. (let ((this-level (funcall outline-level)))
  10984. (outline-next-heading)
  10985. (let ((child-level (funcall outline-level)))
  10986. (while (and (not (eobp))
  10987. (> child-level this-level))
  10988. ;; this todo has children, check whether they are all
  10989. ;; completed
  10990. (if (and (not (org-entry-is-done-p))
  10991. (org-entry-is-todo-p))
  10992. (progn (setq org-block-entry-blocking (org-get-heading))
  10993. (throw 'dont-block nil)))
  10994. (outline-next-heading)
  10995. (setq child-level (funcall outline-level))))))
  10996. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10997. ;; any previous siblings are undone, it's blocked
  10998. (save-excursion
  10999. (org-back-to-heading t)
  11000. (let* ((pos (point))
  11001. (parent-pos (and (org-up-heading-safe) (point))))
  11002. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11003. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11004. (forward-line 1)
  11005. (re-search-forward org-not-done-heading-regexp pos t))
  11006. (setq org-block-entry-blocking (match-string 0))
  11007. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11008. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11009. (while t
  11010. (goto-char parent-pos)
  11011. (if (not (looking-at org-not-done-heading-regexp))
  11012. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11013. (setq pos (point))
  11014. (setq parent-pos (and (org-up-heading-safe) (point)))
  11015. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11016. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11017. (forward-line 1)
  11018. (re-search-forward org-not-done-heading-regexp pos t)
  11019. (setq org-block-entry-blocking (org-get-heading)))
  11020. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11021. (defcustom org-track-ordered-property-with-tag nil
  11022. "Should the ORDERED property also be shown as a tag?
  11023. The ORDERED property decides if an entry should require subtasks to be
  11024. completed in sequence. Since a property is not very visible, setting
  11025. this option means that toggling the ORDERED property with the command
  11026. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11027. not relevant for the behavior, but it makes things more visible.
  11028. Note that toggling the tag with tags commands will not change the property
  11029. and therefore not influence behavior!
  11030. This can be t, meaning the tag ORDERED should be used, It can also be a
  11031. string to select a different tag for this task."
  11032. :group 'org-todo
  11033. :type '(choice
  11034. (const :tag "No tracking" nil)
  11035. (const :tag "Track with ORDERED tag" t)
  11036. (string :tag "Use other tag")))
  11037. (defun org-toggle-ordered-property ()
  11038. "Toggle the ORDERED property of the current entry.
  11039. For better visibility, you can track the value of this property with a tag.
  11040. See variable `org-track-ordered-property-with-tag'."
  11041. (interactive)
  11042. (let* ((t1 org-track-ordered-property-with-tag)
  11043. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11044. (save-excursion
  11045. (org-back-to-heading)
  11046. (if (org-entry-get nil "ORDERED")
  11047. (progn
  11048. (org-delete-property "ORDERED" "PROPERTIES")
  11049. (and tag (org-toggle-tag tag 'off))
  11050. (message "Subtasks can be completed in arbitrary order"))
  11051. (org-entry-put nil "ORDERED" "t")
  11052. (and tag (org-toggle-tag tag 'on))
  11053. (message "Subtasks must be completed in sequence")))))
  11054. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11055. (defun org-block-todo-from-checkboxes (change-plist)
  11056. "Block turning an entry into a TODO, using checkboxes.
  11057. This checks whether the current task should be blocked from state
  11058. changes because there are unchecked boxes in this entry."
  11059. (if (not org-enforce-todo-checkbox-dependencies)
  11060. t ; if locally turned off don't block
  11061. (catch 'dont-block
  11062. ;; If this is not a todo state change, or if this entry is already DONE,
  11063. ;; do not block
  11064. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11065. (member (plist-get change-plist :from)
  11066. (cons 'done org-done-keywords))
  11067. (member (plist-get change-plist :to)
  11068. (cons 'todo org-not-done-keywords))
  11069. (not (plist-get change-plist :to)))
  11070. (throw 'dont-block t))
  11071. ;; If this task has checkboxes that are not checked, it's blocked
  11072. (save-excursion
  11073. (org-back-to-heading t)
  11074. (let ((beg (point)) end)
  11075. (outline-next-heading)
  11076. (setq end (point))
  11077. (goto-char beg)
  11078. (if (org-list-search-forward
  11079. (concat (org-item-beginning-re)
  11080. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11081. "\\[[- ]\\]")
  11082. end t)
  11083. (progn
  11084. (if (boundp 'org-blocked-by-checkboxes)
  11085. (setq org-blocked-by-checkboxes t))
  11086. (throw 'dont-block nil)))))
  11087. t))) ; do not block
  11088. (defun org-entry-blocked-p ()
  11089. "Is the current entry blocked?"
  11090. (org-with-silent-modifications
  11091. (if (org-entry-get nil "NOBLOCKING")
  11092. nil ;; Never block this entry
  11093. (not (run-hook-with-args-until-failure
  11094. 'org-blocker-hook
  11095. (list :type 'todo-state-change
  11096. :position (point)
  11097. :from 'todo
  11098. :to 'done))))))
  11099. (defun org-update-statistics-cookies (all)
  11100. "Update the statistics cookie, either from TODO or from checkboxes.
  11101. This should be called with the cursor in a line with a statistics cookie."
  11102. (interactive "P")
  11103. (if all
  11104. (progn
  11105. (org-update-checkbox-count 'all)
  11106. (org-map-entries 'org-update-parent-todo-statistics))
  11107. (if (not (org-at-heading-p))
  11108. (org-update-checkbox-count)
  11109. (let ((pos (point-marker))
  11110. end l1 l2)
  11111. (ignore-errors (org-back-to-heading t))
  11112. (if (not (org-at-heading-p))
  11113. (org-update-checkbox-count)
  11114. (setq l1 (org-outline-level))
  11115. (setq end (save-excursion
  11116. (outline-next-heading)
  11117. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11118. (point)))
  11119. (if (and (save-excursion
  11120. (re-search-forward
  11121. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11122. (not (save-excursion (re-search-forward
  11123. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11124. (org-update-checkbox-count)
  11125. (if (and l2 (> l2 l1))
  11126. (progn
  11127. (goto-char end)
  11128. (org-update-parent-todo-statistics))
  11129. (goto-char pos)
  11130. (beginning-of-line 1)
  11131. (while (re-search-forward
  11132. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11133. (point-at-eol) t)
  11134. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11135. (goto-char pos)
  11136. (move-marker pos nil)))))
  11137. (defvar org-entry-property-inherited-from) ;; defined below
  11138. (defun org-update-parent-todo-statistics ()
  11139. "Update any statistics cookie in the parent of the current headline.
  11140. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11141. the entire subtree and this will travel up the hierarchy and update
  11142. statistics everywhere."
  11143. (let* ((prop (save-excursion (org-up-heading-safe)
  11144. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11145. (recursive (or (not org-hierarchical-todo-statistics)
  11146. (and prop (string-match "\\<recursive\\>" prop))))
  11147. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11148. 0))
  11149. (first t)
  11150. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11151. level ltoggle l1 new ndel
  11152. (cnt-all 0) (cnt-done 0) is-percent kwd
  11153. checkbox-beg ov ovs ove cookie-present)
  11154. (catch 'exit
  11155. (save-excursion
  11156. (beginning-of-line 1)
  11157. (setq ltoggle (funcall outline-level))
  11158. ;; Three situations are to consider:
  11159. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11160. ;; to the top-level ancestor on the headline;
  11161. ;; 2. If parent has "recursive" property, repeat up to the
  11162. ;; headline setting that property, taking inheritance into
  11163. ;; account;
  11164. ;; 3. Else, move up to direct parent and proceed only once.
  11165. (while (and (setq level (org-up-heading-safe))
  11166. (or recursive first)
  11167. (>= (point) lim))
  11168. (setq first nil cookie-present nil)
  11169. (unless (and level
  11170. (not (string-match
  11171. "\\<checkbox\\>"
  11172. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11173. "")))))
  11174. (throw 'exit nil))
  11175. (while (re-search-forward box-re (point-at-eol) t)
  11176. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11177. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11178. (save-match-data
  11179. (unless (outline-next-heading) (throw 'exit nil))
  11180. (while (and (looking-at org-complex-heading-regexp)
  11181. (> (setq l1 (length (match-string 1))) level))
  11182. (setq kwd (and (or recursive (= l1 ltoggle))
  11183. (match-string 2)))
  11184. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11185. (and (listp org-provide-todo-statistics)
  11186. (or (member kwd org-provide-todo-statistics)
  11187. (member kwd org-done-keywords))))
  11188. (setq cnt-all (1+ cnt-all))
  11189. (if (eq org-provide-todo-statistics t)
  11190. (and kwd (setq cnt-all (1+ cnt-all)))))
  11191. (and (member kwd org-done-keywords)
  11192. (setq cnt-done (1+ cnt-done)))
  11193. (outline-next-heading)))
  11194. (setq new
  11195. (if is-percent
  11196. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11197. (format "[%d/%d]" cnt-done cnt-all))
  11198. ndel (- (match-end 0) checkbox-beg))
  11199. ;; handle overlays when updating cookie from column view
  11200. (when (setq ov (car (overlays-at checkbox-beg)))
  11201. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11202. (delete-overlay ov))
  11203. (goto-char checkbox-beg)
  11204. (insert new)
  11205. (delete-region (point) (+ (point) ndel))
  11206. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11207. (when ov (move-overlay ov ovs ove)))
  11208. (when cookie-present
  11209. (run-hook-with-args 'org-after-todo-statistics-hook
  11210. cnt-done (- cnt-all cnt-done))))))
  11211. (run-hooks 'org-todo-statistics-hook)))
  11212. (defvar org-after-todo-statistics-hook nil
  11213. "Hook that is called after a TODO statistics cookie has been updated.
  11214. Each function is called with two arguments: the number of not-done entries
  11215. and the number of done entries.
  11216. For example, the following function, when added to this hook, will switch
  11217. an entry to DONE when all children are done, and back to TODO when new
  11218. entries are set to a TODO status. Note that this hook is only called
  11219. when there is a statistics cookie in the headline!
  11220. (defun org-summary-todo (n-done n-not-done)
  11221. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11222. (let (org-log-done org-log-states) ; turn off logging
  11223. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11224. ")
  11225. (defvar org-todo-statistics-hook nil
  11226. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11227. This hook runs even if there is no statistics cookie present, in which case
  11228. `org-after-todo-statistics-hook' would not run.")
  11229. (defun org-todo-trigger-tag-changes (state)
  11230. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11231. (let ((l org-todo-state-tags-triggers)
  11232. changes)
  11233. (when (or (not state) (equal state ""))
  11234. (setq changes (append changes (cdr (assoc "" l)))))
  11235. (when (and (stringp state) (> (length state) 0))
  11236. (setq changes (append changes (cdr (assoc state l)))))
  11237. (when (member state org-not-done-keywords)
  11238. (setq changes (append changes (cdr (assoc 'todo l)))))
  11239. (when (member state org-done-keywords)
  11240. (setq changes (append changes (cdr (assoc 'done l)))))
  11241. (dolist (c changes)
  11242. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11243. (defun org-local-logging (value)
  11244. "Get logging settings from a property VALUE."
  11245. (let* (words w a)
  11246. ;; directly set the variables, they are already local.
  11247. (setq org-log-done nil
  11248. org-log-repeat nil
  11249. org-todo-log-states nil)
  11250. (setq words (org-split-string value))
  11251. (while (setq w (pop words))
  11252. (cond
  11253. ((setq a (assoc w org-startup-options))
  11254. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11255. (set (nth 1 a) (nth 2 a))))
  11256. ((setq a (org-extract-log-state-settings w))
  11257. (and (member (car a) org-todo-keywords-1)
  11258. (push a org-todo-log-states)))))))
  11259. (defun org-get-todo-sequence-head (kwd)
  11260. "Return the head of the TODO sequence to which KWD belongs.
  11261. If KWD is not set, check if there is a text property remembering the
  11262. right sequence."
  11263. (let (p)
  11264. (cond
  11265. ((not kwd)
  11266. (or (get-text-property (point-at-bol) 'org-todo-head)
  11267. (progn
  11268. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11269. nil (point-at-eol)))
  11270. (get-text-property p 'org-todo-head))))
  11271. ((not (member kwd org-todo-keywords-1))
  11272. (car org-todo-keywords-1))
  11273. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11274. (defun org-fast-todo-selection ()
  11275. "Fast TODO keyword selection with single keys.
  11276. Returns the new TODO keyword, or nil if no state change should occur."
  11277. (let* ((fulltable org-todo-key-alist)
  11278. (done-keywords org-done-keywords) ;; needed for the faces.
  11279. (maxlen (apply 'max (mapcar
  11280. (lambda (x)
  11281. (if (stringp (car x)) (string-width (car x)) 0))
  11282. fulltable)))
  11283. (expert nil)
  11284. (fwidth (+ maxlen 3 1 3))
  11285. (ncol (/ (- (window-width) 4) fwidth))
  11286. tg cnt e c tbl
  11287. groups ingroup)
  11288. (save-excursion
  11289. (save-window-excursion
  11290. (if expert
  11291. (set-buffer (get-buffer-create " *Org todo*"))
  11292. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11293. (erase-buffer)
  11294. (org-set-local 'org-done-keywords done-keywords)
  11295. (setq tbl fulltable cnt 0)
  11296. (while (setq e (pop tbl))
  11297. (cond
  11298. ((equal e '(:startgroup))
  11299. (push '() groups) (setq ingroup t)
  11300. (when (not (= cnt 0))
  11301. (setq cnt 0)
  11302. (insert "\n"))
  11303. (insert "{ "))
  11304. ((equal e '(:endgroup))
  11305. (setq ingroup nil cnt 0)
  11306. (insert "}\n"))
  11307. ((equal e '(:newline))
  11308. (when (not (= cnt 0))
  11309. (setq cnt 0)
  11310. (insert "\n")
  11311. (setq e (car tbl))
  11312. (while (equal (car tbl) '(:newline))
  11313. (insert "\n")
  11314. (setq tbl (cdr tbl)))))
  11315. (t
  11316. (setq tg (car e) c (cdr e))
  11317. (if ingroup (push tg (car groups)))
  11318. (setq tg (org-add-props tg nil 'face
  11319. (org-get-todo-face tg)))
  11320. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11321. (insert "[" c "] " tg (make-string
  11322. (- fwidth 4 (length tg)) ?\ ))
  11323. (when (= (setq cnt (1+ cnt)) ncol)
  11324. (insert "\n")
  11325. (if ingroup (insert " "))
  11326. (setq cnt 0)))))
  11327. (insert "\n")
  11328. (goto-char (point-min))
  11329. (if (not expert) (org-fit-window-to-buffer))
  11330. (message "[a-z..]:Set [SPC]:clear")
  11331. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11332. (cond
  11333. ((or (= c ?\C-g)
  11334. (and (= c ?q) (not (rassoc c fulltable))))
  11335. (setq quit-flag t))
  11336. ((= c ?\ ) nil)
  11337. ((setq e (rassoc c fulltable) tg (car e))
  11338. tg)
  11339. (t (setq quit-flag t)))))))
  11340. (defun org-entry-is-todo-p ()
  11341. (member (org-get-todo-state) org-not-done-keywords))
  11342. (defun org-entry-is-done-p ()
  11343. (member (org-get-todo-state) org-done-keywords))
  11344. (defun org-get-todo-state ()
  11345. (save-excursion
  11346. (org-back-to-heading t)
  11347. (and (looking-at org-todo-line-regexp)
  11348. (match-end 2)
  11349. (match-string 2))))
  11350. (defun org-at-date-range-p (&optional inactive-ok)
  11351. "Is the cursor inside a date range?"
  11352. (interactive)
  11353. (save-excursion
  11354. (catch 'exit
  11355. (let ((pos (point)))
  11356. (skip-chars-backward "^[<\r\n")
  11357. (skip-chars-backward "<[")
  11358. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11359. (>= (match-end 0) pos)
  11360. (throw 'exit t))
  11361. (skip-chars-backward "^<[\r\n")
  11362. (skip-chars-backward "<[")
  11363. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11364. (>= (match-end 0) pos)
  11365. (throw 'exit t)))
  11366. nil)))
  11367. (defun org-get-repeat (&optional tagline)
  11368. "Check if there is a deadline/schedule with repeater in this entry."
  11369. (save-match-data
  11370. (save-excursion
  11371. (org-back-to-heading t)
  11372. (and (re-search-forward (if tagline
  11373. (concat tagline "\\s-*" org-repeat-re)
  11374. org-repeat-re)
  11375. (org-entry-end-position) t)
  11376. (match-string-no-properties 1)))))
  11377. (defvar org-last-changed-timestamp)
  11378. (defvar org-last-inserted-timestamp)
  11379. (defvar org-log-post-message)
  11380. (defvar org-log-note-purpose)
  11381. (defvar org-log-note-how)
  11382. (defvar org-log-note-extra)
  11383. (defun org-auto-repeat-maybe (done-word)
  11384. "Check if the current headline contains a repeated deadline/schedule.
  11385. If yes, set TODO state back to what it was and change the base date
  11386. of repeating deadline/scheduled time stamps to new date.
  11387. This function is run automatically after each state change to a DONE state."
  11388. ;; last-state is dynamically scoped into this function
  11389. (let* ((repeat (org-get-repeat))
  11390. (aa (assoc org-last-state org-todo-kwd-alist))
  11391. (interpret (nth 1 aa))
  11392. (head (nth 2 aa))
  11393. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11394. (msg "Entry repeats: ")
  11395. (org-log-done nil)
  11396. (org-todo-log-states nil)
  11397. re type n what ts time to-state)
  11398. (when repeat
  11399. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11400. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11401. org-todo-repeat-to-state))
  11402. (unless (and to-state (member to-state org-todo-keywords-1))
  11403. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11404. (org-todo to-state)
  11405. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11406. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11407. (org-time-stamp-format t t))))
  11408. (when org-log-repeat
  11409. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11410. (memq 'org-add-log-note post-command-hook))
  11411. ;; OK, we are already setup for some record
  11412. (if (eq org-log-repeat 'note)
  11413. ;; make sure we take a note, not only a time stamp
  11414. (setq org-log-note-how 'note))
  11415. ;; Set up for taking a record
  11416. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11417. org-last-state
  11418. 'findpos org-log-repeat)))
  11419. (org-back-to-heading t)
  11420. (org-add-planning-info nil nil 'closed)
  11421. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11422. org-deadline-time-regexp "\\)\\|\\("
  11423. org-ts-regexp "\\)"))
  11424. (while (re-search-forward
  11425. re (save-excursion (outline-next-heading) (point)) t)
  11426. (setq type (if (match-end 1) org-scheduled-string
  11427. (if (match-end 3) org-deadline-string "Plain:"))
  11428. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11429. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11430. (setq n (string-to-number (match-string 2 ts))
  11431. what (match-string 3 ts))
  11432. (if (equal what "w") (setq n (* n 7) what "d"))
  11433. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11434. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11435. ;; Preparation, see if we need to modify the start date for the change
  11436. (when (match-end 1)
  11437. (setq time (save-match-data (org-time-string-to-time ts)))
  11438. (cond
  11439. ((equal (match-string 1 ts) ".")
  11440. ;; Shift starting date to today
  11441. (org-timestamp-change
  11442. (- (org-today) (time-to-days time))
  11443. 'day))
  11444. ((equal (match-string 1 ts) "+")
  11445. (let ((nshiftmax 10) (nshift 0))
  11446. (while (or (= nshift 0)
  11447. (<= (time-to-days time)
  11448. (time-to-days (current-time))))
  11449. (when (= (incf nshift) nshiftmax)
  11450. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11451. (error "Abort")))
  11452. (org-timestamp-change n (cdr (assoc what whata)))
  11453. (org-at-timestamp-p t)
  11454. (setq ts (match-string 1))
  11455. (setq time (save-match-data (org-time-string-to-time ts)))))
  11456. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11457. ;; rematch, so that we have everything in place for the real shift
  11458. (org-at-timestamp-p t)
  11459. (setq ts (match-string 1))
  11460. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11461. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11462. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11463. (setq org-log-post-message msg)
  11464. (message "%s" msg))))
  11465. (defun org-show-todo-tree (arg)
  11466. "Make a compact tree which shows all headlines marked with TODO.
  11467. The tree will show the lines where the regexp matches, and all higher
  11468. headlines above the match.
  11469. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11470. With a numeric prefix N, construct a sparse tree for the Nth element
  11471. of `org-todo-keywords-1'."
  11472. (interactive "P")
  11473. (let ((case-fold-search nil)
  11474. (kwd-re
  11475. (cond ((null arg) org-not-done-regexp)
  11476. ((equal arg '(4))
  11477. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11478. (mapcar 'list org-todo-keywords-1))))
  11479. (concat "\\("
  11480. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11481. "\\)\\>")))
  11482. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11483. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11484. org-todo-keywords-1)))
  11485. (t (error "Invalid prefix argument: %s" arg)))))
  11486. (message "%d TODO entries found"
  11487. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11488. (defun org-deadline (arg &optional time)
  11489. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11490. With one universal prefix argument, remove any deadline from the item.
  11491. With two universal prefix arguments, prompt for a warning delay.
  11492. With argument TIME, set the deadline at the corresponding date. TIME
  11493. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11494. (interactive "P")
  11495. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11496. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11497. 'region-start-level 'region))
  11498. org-loop-over-headlines-in-active-region)
  11499. (org-map-entries
  11500. `(org-deadline ',arg ,time)
  11501. org-loop-over-headlines-in-active-region
  11502. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11503. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11504. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11505. (repeater (and old-date
  11506. (string-match
  11507. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11508. old-date)
  11509. (match-string 1 old-date))))
  11510. (cond
  11511. ((equal arg '(4))
  11512. (when (and old-date org-log-redeadline)
  11513. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11514. org-log-redeadline))
  11515. (org-remove-timestamp-with-keyword org-deadline-string)
  11516. (message "Item no longer has a deadline."))
  11517. ((equal arg '(16))
  11518. (save-excursion
  11519. (if (re-search-forward
  11520. org-deadline-time-regexp
  11521. (save-excursion (outline-next-heading) (point)) t)
  11522. (let* ((rpl0 (match-string 1))
  11523. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11524. (replace-match
  11525. (concat org-deadline-string
  11526. " <" rpl
  11527. (format " -%dd"
  11528. (abs
  11529. (- (time-to-days
  11530. (save-match-data
  11531. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11532. (time-to-days old-date-time))))
  11533. ">") t t))
  11534. (user-error "No deadline information to update"))))
  11535. (t
  11536. (org-add-planning-info 'deadline time 'closed)
  11537. (when (and old-date org-log-redeadline
  11538. (not (equal old-date
  11539. (substring org-last-inserted-timestamp 1 -1))))
  11540. (org-add-log-setup 'redeadline nil old-date 'findpos
  11541. org-log-redeadline))
  11542. (when repeater
  11543. (save-excursion
  11544. (org-back-to-heading t)
  11545. (when (re-search-forward (concat org-deadline-string " "
  11546. org-last-inserted-timestamp)
  11547. (save-excursion
  11548. (outline-next-heading) (point)) t)
  11549. (goto-char (1- (match-end 0)))
  11550. (insert " " repeater)
  11551. (setq org-last-inserted-timestamp
  11552. (concat (substring org-last-inserted-timestamp 0 -1)
  11553. " " repeater
  11554. (substring org-last-inserted-timestamp -1))))))
  11555. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11556. (defun org-schedule (arg &optional time)
  11557. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11558. With one universal prefix argument, remove any scheduling date from the item.
  11559. With two universal prefix arguments, prompt for a delay cookie.
  11560. With argument TIME, scheduled at the corresponding date. TIME can
  11561. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11562. (interactive "P")
  11563. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11564. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11565. 'region-start-level 'region))
  11566. org-loop-over-headlines-in-active-region)
  11567. (org-map-entries
  11568. `(org-schedule ',arg ,time)
  11569. org-loop-over-headlines-in-active-region
  11570. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11571. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11572. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11573. (repeater (and old-date
  11574. (string-match
  11575. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11576. old-date)
  11577. (match-string 1 old-date))))
  11578. (cond
  11579. ((equal arg '(4))
  11580. (progn
  11581. (when (and old-date org-log-reschedule)
  11582. (org-add-log-setup 'delschedule nil old-date 'findpos
  11583. org-log-reschedule))
  11584. (org-remove-timestamp-with-keyword org-scheduled-string)
  11585. (message "Item is no longer scheduled.")))
  11586. ((equal arg '(16))
  11587. (save-excursion
  11588. (if (re-search-forward
  11589. org-scheduled-time-regexp
  11590. (save-excursion (outline-next-heading) (point)) t)
  11591. (let* ((rpl0 (match-string 1))
  11592. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11593. (replace-match
  11594. (concat org-scheduled-string
  11595. " <" rpl
  11596. (format " -%dd"
  11597. (abs
  11598. (- (time-to-days
  11599. (save-match-data
  11600. (org-read-date nil t nil "Delay until" old-date-time)))
  11601. (time-to-days old-date-time))))
  11602. ">") t t))
  11603. (user-error "No scheduled information to update"))))
  11604. (t
  11605. (org-add-planning-info 'scheduled time 'closed)
  11606. (when (and old-date org-log-reschedule
  11607. (not (equal old-date
  11608. (substring org-last-inserted-timestamp 1 -1))))
  11609. (org-add-log-setup 'reschedule nil old-date 'findpos
  11610. org-log-reschedule))
  11611. (when repeater
  11612. (save-excursion
  11613. (org-back-to-heading t)
  11614. (when (re-search-forward (concat org-scheduled-string " "
  11615. org-last-inserted-timestamp)
  11616. (save-excursion
  11617. (outline-next-heading) (point)) t)
  11618. (goto-char (1- (match-end 0)))
  11619. (insert " " repeater)
  11620. (setq org-last-inserted-timestamp
  11621. (concat (substring org-last-inserted-timestamp 0 -1)
  11622. " " repeater
  11623. (substring org-last-inserted-timestamp -1))))))
  11624. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11625. (defun org-get-scheduled-time (pom &optional inherit)
  11626. "Get the scheduled time as a time tuple, of a format suitable
  11627. for calling org-schedule with, or if there is no scheduling,
  11628. returns nil."
  11629. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11630. (when time
  11631. (apply 'encode-time (org-parse-time-string time)))))
  11632. (defun org-get-deadline-time (pom &optional inherit)
  11633. "Get the deadline as a time tuple, of a format suitable for
  11634. calling org-deadline with, or if there is no scheduling, returns
  11635. nil."
  11636. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11637. (when time
  11638. (apply 'encode-time (org-parse-time-string time)))))
  11639. (defun org-remove-timestamp-with-keyword (keyword)
  11640. "Remove all time stamps with KEYWORD in the current entry."
  11641. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11642. beg)
  11643. (save-excursion
  11644. (org-back-to-heading t)
  11645. (setq beg (point))
  11646. (outline-next-heading)
  11647. (while (re-search-backward re beg t)
  11648. (replace-match "")
  11649. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11650. (equal (char-before) ?\ ))
  11651. (backward-delete-char 1)
  11652. (if (string-match "^[ \t]*$" (buffer-substring
  11653. (point-at-bol) (point-at-eol)))
  11654. (delete-region (point-at-bol)
  11655. (min (point-max) (1+ (point-at-eol))))))))))
  11656. (defun org-add-planning-info (what &optional time &rest remove)
  11657. "Insert new timestamp with keyword in the line directly after the headline.
  11658. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11659. If non is given, the user is prompted for a date.
  11660. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11661. be removed."
  11662. (interactive)
  11663. (let (org-time-was-given org-end-time-was-given ts
  11664. end default-time default-input)
  11665. (catch 'exit
  11666. (when (and (memq what '(scheduled deadline))
  11667. (or (not time)
  11668. (and (stringp time)
  11669. (string-match "^[-+]+[0-9]" time))))
  11670. ;; Try to get a default date/time from existing timestamp
  11671. (save-excursion
  11672. (org-back-to-heading t)
  11673. (setq end (save-excursion (outline-next-heading) (point)))
  11674. (when (re-search-forward (if (eq what 'scheduled)
  11675. org-scheduled-time-regexp
  11676. org-deadline-time-regexp)
  11677. end t)
  11678. (setq ts (match-string 1)
  11679. default-time
  11680. (apply 'encode-time (org-parse-time-string ts))
  11681. default-input (and ts (org-get-compact-tod ts))))))
  11682. (when what
  11683. (setq time
  11684. (if (stringp time)
  11685. ;; This is a string (relative or absolute), set proper date
  11686. (apply 'encode-time
  11687. (org-read-date-analyze
  11688. time default-time (decode-time default-time)))
  11689. ;; If necessary, get the time from the user
  11690. (or time (org-read-date nil 'to-time nil nil
  11691. default-time default-input)))))
  11692. (when (and org-insert-labeled-timestamps-at-point
  11693. (member what '(scheduled deadline)))
  11694. (insert
  11695. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11696. (org-insert-time-stamp time org-time-was-given
  11697. nil nil nil (list org-end-time-was-given))
  11698. (setq what nil))
  11699. (save-excursion
  11700. (save-restriction
  11701. (let (col list elt ts buffer-invisibility-spec)
  11702. (org-back-to-heading t)
  11703. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11704. (goto-char (match-end 1))
  11705. (setq col (current-column))
  11706. (goto-char (match-end 0))
  11707. (if (eobp) (insert "\n") (forward-char 1))
  11708. (when (and (not what)
  11709. (not (looking-at
  11710. (concat "[ \t]*"
  11711. org-keyword-time-not-clock-regexp))))
  11712. ;; Nothing to add, nothing to remove...... :-)
  11713. (throw 'exit nil))
  11714. (if (and (not (looking-at org-outline-regexp))
  11715. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11716. "[^\r\n]*"))
  11717. (not (equal (match-string 1) org-clock-string)))
  11718. (narrow-to-region (match-beginning 0) (match-end 0))
  11719. (insert-before-markers "\n")
  11720. (backward-char 1)
  11721. (narrow-to-region (point) (point))
  11722. (and org-adapt-indentation (org-indent-to-column col)))
  11723. ;; Check if we have to remove something.
  11724. (setq list (cons what remove))
  11725. (while list
  11726. (setq elt (pop list))
  11727. (when (or (and (eq elt 'scheduled)
  11728. (re-search-forward org-scheduled-time-regexp nil t))
  11729. (and (eq elt 'deadline)
  11730. (re-search-forward org-deadline-time-regexp nil t))
  11731. (and (eq elt 'closed)
  11732. (re-search-forward org-closed-time-regexp nil t)))
  11733. (replace-match "")
  11734. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11735. (and (looking-at "[ \t]+") (replace-match ""))
  11736. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11737. (when what
  11738. (insert
  11739. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11740. (cond ((eq what 'scheduled) org-scheduled-string)
  11741. ((eq what 'deadline) org-deadline-string)
  11742. ((eq what 'closed) org-closed-string))
  11743. " ")
  11744. (setq ts (org-insert-time-stamp
  11745. time
  11746. (or org-time-was-given
  11747. (and (eq what 'closed) org-log-done-with-time))
  11748. (eq what 'closed)
  11749. nil nil (list org-end-time-was-given)))
  11750. (insert
  11751. (if (not (or (bolp) (eq (char-before) ?\ )
  11752. (memq (char-after) '(32 10))
  11753. (eobp))) " " ""))
  11754. (end-of-line 1))
  11755. (goto-char (point-min))
  11756. (widen)
  11757. (if (and (looking-at "[ \t]*\n")
  11758. (equal (char-before) ?\n))
  11759. (delete-region (1- (point)) (point-at-eol)))
  11760. ts))))))
  11761. (defvar org-log-note-marker (make-marker))
  11762. (defvar org-log-note-purpose nil)
  11763. (defvar org-log-note-state nil)
  11764. (defvar org-log-note-previous-state nil)
  11765. (defvar org-log-note-how nil)
  11766. (defvar org-log-note-extra nil)
  11767. (defvar org-log-note-window-configuration nil)
  11768. (defvar org-log-note-return-to (make-marker))
  11769. (defvar org-log-note-effective-time nil
  11770. "Remembered current time so that dynamically scoped
  11771. `org-extend-today-until' affects tha timestamps in state change
  11772. log")
  11773. (defvar org-log-post-message nil
  11774. "Message to be displayed after a log note has been stored.
  11775. The auto-repeater uses this.")
  11776. (defun org-add-note ()
  11777. "Add a note to the current entry.
  11778. This is done in the same way as adding a state change note."
  11779. (interactive)
  11780. (org-add-log-setup 'note nil nil 'findpos nil))
  11781. (defvar org-property-end-re)
  11782. (defun org-add-log-setup (&optional purpose state prev-state
  11783. findpos how extra)
  11784. "Set up the post command hook to take a note.
  11785. If this is about to TODO state change, the new state is expected in STATE.
  11786. When FINDPOS is non-nil, find the correct position for the note in
  11787. the current entry. If not, assume that it can be inserted at point.
  11788. HOW is an indicator what kind of note should be created.
  11789. EXTRA is additional text that will be inserted into the notes buffer."
  11790. (let* ((org-log-into-drawer (org-log-into-drawer))
  11791. (drawer (cond ((stringp org-log-into-drawer)
  11792. org-log-into-drawer)
  11793. (org-log-into-drawer "LOGBOOK"))))
  11794. (save-restriction
  11795. (save-excursion
  11796. (when findpos
  11797. (org-back-to-heading t)
  11798. (narrow-to-region (point) (save-excursion
  11799. (outline-next-heading) (point)))
  11800. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11801. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11802. "[^\r\n]*\\)?"))
  11803. (goto-char (match-end 0))
  11804. (cond
  11805. (drawer
  11806. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11807. nil t)
  11808. (progn
  11809. (goto-char (match-end 0))
  11810. (or org-log-states-order-reversed
  11811. (and (re-search-forward org-property-end-re nil t)
  11812. (goto-char (1- (match-beginning 0))))))
  11813. (insert "\n:" drawer ":\n:END:")
  11814. (beginning-of-line 0)
  11815. (org-indent-line)
  11816. (beginning-of-line 2)
  11817. (org-indent-line)
  11818. (end-of-line 0)))
  11819. ((and org-log-state-notes-insert-after-drawers
  11820. (save-excursion
  11821. (forward-line) (looking-at org-drawer-regexp)))
  11822. (forward-line)
  11823. (while (looking-at org-drawer-regexp)
  11824. (goto-char (match-end 0))
  11825. (re-search-forward org-property-end-re (point-max) t)
  11826. (forward-line))
  11827. (forward-line -1)))
  11828. (unless org-log-states-order-reversed
  11829. (and (= (char-after) ?\n) (forward-char 1))
  11830. (org-skip-over-state-notes)
  11831. (skip-chars-backward " \t\n\r")))
  11832. (move-marker org-log-note-marker (point))
  11833. (setq org-log-note-purpose purpose
  11834. org-log-note-state state
  11835. org-log-note-previous-state prev-state
  11836. org-log-note-how how
  11837. org-log-note-extra extra
  11838. org-log-note-effective-time (org-current-effective-time))
  11839. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11840. (defun org-skip-over-state-notes ()
  11841. "Skip past the list of State notes in an entry."
  11842. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11843. (when (ignore-errors (goto-char (org-in-item-p)))
  11844. (let* ((struct (org-list-struct))
  11845. (prevs (org-list-prevs-alist struct)))
  11846. (while (looking-at "[ \t]*- State")
  11847. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11848. (org-list-get-item-end (point) struct)))))))
  11849. (defun org-add-log-note (&optional purpose)
  11850. "Pop up a window for taking a note, and add this note later at point."
  11851. (remove-hook 'post-command-hook 'org-add-log-note)
  11852. (setq org-log-note-window-configuration (current-window-configuration))
  11853. (delete-other-windows)
  11854. (move-marker org-log-note-return-to (point))
  11855. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11856. (goto-char org-log-note-marker)
  11857. (org-switch-to-buffer-other-window "*Org Note*")
  11858. (erase-buffer)
  11859. (if (memq org-log-note-how '(time state))
  11860. (let (current-prefix-arg) (org-store-log-note))
  11861. (let ((org-inhibit-startup t)) (org-mode))
  11862. (insert (format "# Insert note for %s.
  11863. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11864. (cond
  11865. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11866. ((eq org-log-note-purpose 'done) "closed todo item")
  11867. ((eq org-log-note-purpose 'state)
  11868. (format "state change from \"%s\" to \"%s\""
  11869. (or org-log-note-previous-state "")
  11870. (or org-log-note-state "")))
  11871. ((eq org-log-note-purpose 'reschedule)
  11872. "rescheduling")
  11873. ((eq org-log-note-purpose 'delschedule)
  11874. "no longer scheduled")
  11875. ((eq org-log-note-purpose 'redeadline)
  11876. "changing deadline")
  11877. ((eq org-log-note-purpose 'deldeadline)
  11878. "removing deadline")
  11879. ((eq org-log-note-purpose 'refile)
  11880. "refiling")
  11881. ((eq org-log-note-purpose 'note)
  11882. "this entry")
  11883. (t (error "This should not happen")))))
  11884. (if org-log-note-extra (insert org-log-note-extra))
  11885. (org-set-local 'org-finish-function 'org-store-log-note)
  11886. (run-hooks 'org-log-buffer-setup-hook)))
  11887. (defvar org-note-abort nil) ; dynamically scoped
  11888. (defun org-store-log-note ()
  11889. "Finish taking a log note, and insert it to where it belongs."
  11890. (let ((txt (buffer-string)))
  11891. (kill-buffer (current-buffer))
  11892. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11893. lines ind bul)
  11894. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11895. (setq txt (replace-match "" t t txt)))
  11896. (if (string-match "\\s-+\\'" txt)
  11897. (setq txt (replace-match "" t t txt)))
  11898. (setq lines (org-split-string txt "\n"))
  11899. (when (and note (string-match "\\S-" note))
  11900. (setq note
  11901. (org-replace-escapes
  11902. note
  11903. (list (cons "%u" (user-login-name))
  11904. (cons "%U" user-full-name)
  11905. (cons "%t" (format-time-string
  11906. (org-time-stamp-format 'long 'inactive)
  11907. org-log-note-effective-time))
  11908. (cons "%T" (format-time-string
  11909. (org-time-stamp-format 'long nil)
  11910. org-log-note-effective-time))
  11911. (cons "%d" (format-time-string
  11912. (org-time-stamp-format nil 'inactive)
  11913. org-log-note-effective-time))
  11914. (cons "%D" (format-time-string
  11915. (org-time-stamp-format nil nil)
  11916. org-log-note-effective-time))
  11917. (cons "%s" (if org-log-note-state
  11918. (concat "\"" org-log-note-state "\"")
  11919. ""))
  11920. (cons "%S" (if org-log-note-previous-state
  11921. (concat "\"" org-log-note-previous-state "\"")
  11922. "\"\"")))))
  11923. (if lines (setq note (concat note " \\\\")))
  11924. (push note lines))
  11925. (when (or current-prefix-arg org-note-abort)
  11926. (when org-log-into-drawer
  11927. (org-remove-empty-drawer-at
  11928. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11929. org-log-note-marker))
  11930. (setq lines nil))
  11931. (when lines
  11932. (with-current-buffer (marker-buffer org-log-note-marker)
  11933. (save-excursion
  11934. (goto-char org-log-note-marker)
  11935. (move-marker org-log-note-marker nil)
  11936. (end-of-line 1)
  11937. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11938. (setq ind (save-excursion
  11939. (if (ignore-errors (goto-char (org-in-item-p)))
  11940. (let ((struct (org-list-struct)))
  11941. (org-list-get-ind
  11942. (org-list-get-top-point struct) struct))
  11943. (skip-chars-backward " \r\t\n")
  11944. (cond
  11945. ((and (org-at-heading-p)
  11946. org-adapt-indentation)
  11947. (1+ (org-current-level)))
  11948. ((org-at-heading-p) 0)
  11949. (t (org-get-indentation))))))
  11950. (setq bul (org-list-bullet-string "-"))
  11951. (org-indent-line-to ind)
  11952. (insert bul (pop lines))
  11953. (let ((ind-body (+ (length bul) ind)))
  11954. (while lines
  11955. (insert "\n")
  11956. (org-indent-line-to ind-body)
  11957. (insert (pop lines))))
  11958. (message "Note stored")
  11959. (org-back-to-heading t)
  11960. (org-cycle-hide-drawers 'children))))))
  11961. (set-window-configuration org-log-note-window-configuration)
  11962. (with-current-buffer (marker-buffer org-log-note-return-to)
  11963. (goto-char org-log-note-return-to))
  11964. (move-marker org-log-note-return-to nil)
  11965. (and org-log-post-message (message "%s" org-log-post-message)))
  11966. (defun org-remove-empty-drawer-at (drawer pos)
  11967. "Remove an empty drawer DRAWER at position POS.
  11968. POS may also be a marker."
  11969. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11970. (save-excursion
  11971. (save-restriction
  11972. (widen)
  11973. (goto-char pos)
  11974. (if (org-in-regexp
  11975. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11976. (replace-match ""))))))
  11977. (defvar org-ts-type nil)
  11978. (defun org-sparse-tree (&optional arg type)
  11979. "Create a sparse tree, prompt for the details.
  11980. This command can create sparse trees. You first need to select the type
  11981. of match used to create the tree:
  11982. t Show all TODO entries.
  11983. T Show entries with a specific TODO keyword.
  11984. m Show entries selected by a tags/property match.
  11985. p Enter a property name and its value (both with completion on existing
  11986. names/values) and show entries with that property.
  11987. r Show entries matching a regular expression (`/' can be used as well).
  11988. b Show deadlines and scheduled items before a date.
  11989. a Show deadlines and scheduled items after a date.
  11990. d Show deadlines due within `org-deadline-warning-days'.
  11991. D Show deadlines and scheduled items between a date range."
  11992. (interactive "P")
  11993. (let (ans kwd value ts-type)
  11994. (setq type (or type org-sparse-tree-default-date-type))
  11995. (setq org-ts-type type)
  11996. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  11997. (cond ((eq type 'all) "all timestamps")
  11998. ((eq type 'scheduled) "only scheduled")
  11999. ((eq type 'deadline) "only deadline")
  12000. ((eq type 'active) "only active timestamps")
  12001. ((eq type 'inactive) "only inactive timestamps")
  12002. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12003. (t "scheduled/deadline")))
  12004. (setq ans (read-char-exclusive))
  12005. (cond
  12006. ((equal ans ?c)
  12007. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  12008. ((equal ans ?d)
  12009. (call-interactively 'org-check-deadlines))
  12010. ((equal ans ?b)
  12011. (call-interactively 'org-check-before-date))
  12012. ((equal ans ?a)
  12013. (call-interactively 'org-check-after-date))
  12014. ((equal ans ?D)
  12015. (call-interactively 'org-check-dates-range))
  12016. ((equal ans ?t)
  12017. (call-interactively 'org-show-todo-tree))
  12018. ((equal ans ?T)
  12019. (org-show-todo-tree '(4)))
  12020. ((member ans '(?T ?m))
  12021. (call-interactively 'org-match-sparse-tree))
  12022. ((member ans '(?p ?P))
  12023. (setq kwd (org-icompleting-read "Property: "
  12024. (mapcar 'list (org-buffer-property-keys))))
  12025. (setq value (org-icompleting-read "Value: "
  12026. (mapcar 'list (org-property-values kwd))))
  12027. (unless (string-match "\\`{.*}\\'" value)
  12028. (setq value (concat "\"" value "\"")))
  12029. (org-match-sparse-tree arg (concat kwd "=" value)))
  12030. ((member ans '(?r ?R ?/))
  12031. (call-interactively 'org-occur))
  12032. (t (error "No such sparse tree command \"%c\"" ans)))))
  12033. (defvar org-occur-highlights nil
  12034. "List of overlays used for occur matches.")
  12035. (make-variable-buffer-local 'org-occur-highlights)
  12036. (defvar org-occur-parameters nil
  12037. "Parameters of the active org-occur calls.
  12038. This is a list, each call to org-occur pushes as cons cell,
  12039. containing the regular expression and the callback, onto the list.
  12040. The list can contain several entries if `org-occur' has been called
  12041. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12042. will only contain one set of parameters. When the highlights are
  12043. removed (for example with `C-c C-c', or with the next edit (depending
  12044. on `org-remove-highlights-with-change'), this variable is emptied
  12045. as well.")
  12046. (make-variable-buffer-local 'org-occur-parameters)
  12047. (defun org-occur (regexp &optional keep-previous callback)
  12048. "Make a compact tree which shows all matches of REGEXP.
  12049. The tree will show the lines where the regexp matches, and all higher
  12050. headlines above the match. It will also show the heading after the match,
  12051. to make sure editing the matching entry is easy.
  12052. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12053. call to `org-occur' will be kept, to allow stacking of calls to this
  12054. command.
  12055. If CALLBACK is non-nil, it is a function which is called to confirm
  12056. that the match should indeed be shown."
  12057. (interactive "sRegexp: \nP")
  12058. (when (equal regexp "")
  12059. (error "Regexp cannot be empty"))
  12060. (unless keep-previous
  12061. (org-remove-occur-highlights nil nil t))
  12062. (push (cons regexp callback) org-occur-parameters)
  12063. (let ((cnt 0))
  12064. (save-excursion
  12065. (goto-char (point-min))
  12066. (if (or (not keep-previous) ; do not want to keep
  12067. (not org-occur-highlights)) ; no previous matches
  12068. ;; hide everything
  12069. (org-overview))
  12070. (while (re-search-forward regexp nil t)
  12071. (when (or (not callback)
  12072. (save-match-data (funcall callback)))
  12073. (setq cnt (1+ cnt))
  12074. (when org-highlight-sparse-tree-matches
  12075. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12076. (org-show-context 'occur-tree))))
  12077. (when org-remove-highlights-with-change
  12078. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12079. nil 'local))
  12080. (unless org-sparse-tree-open-archived-trees
  12081. (org-hide-archived-subtrees (point-min) (point-max)))
  12082. (run-hooks 'org-occur-hook)
  12083. (if (org-called-interactively-p 'interactive)
  12084. (message "%d match(es) for regexp %s" cnt regexp))
  12085. cnt))
  12086. (defun org-occur-next-match (&optional n reset)
  12087. "Function for `next-error-function' to find sparse tree matches.
  12088. N is the number of matches to move, when negative move backwards.
  12089. RESET is entirely ignored - this function always goes back to the
  12090. starting point when no match is found."
  12091. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12092. (search-func (if (< n 0)
  12093. 'previous-single-char-property-change
  12094. 'next-single-char-property-change))
  12095. (n (abs n))
  12096. (pos (point))
  12097. p1)
  12098. (catch 'exit
  12099. (while (setq p1 (funcall search-func (point) 'org-type))
  12100. (when (equal p1 limit)
  12101. (goto-char pos)
  12102. (error "No more matches"))
  12103. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12104. (setq n (1- n))
  12105. (when (= n 0)
  12106. (goto-char p1)
  12107. (throw 'exit (point))))
  12108. (goto-char p1))
  12109. (goto-char p1)
  12110. (error "No more matches"))))
  12111. (defun org-show-context (&optional key)
  12112. "Make sure point and context are visible.
  12113. How much context is shown depends upon the variables
  12114. `org-show-hierarchy-above', `org-show-following-heading',
  12115. `org-show-entry-below' and `org-show-siblings'."
  12116. (let ((heading-p (org-at-heading-p t))
  12117. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12118. (following-p (org-get-alist-option org-show-following-heading key))
  12119. (entry-p (org-get-alist-option org-show-entry-below key))
  12120. (siblings-p (org-get-alist-option org-show-siblings key)))
  12121. ;; Show heading or entry text
  12122. (if (and heading-p (not entry-p))
  12123. (org-flag-heading nil) ; only show the heading
  12124. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12125. (org-show-hidden-entry))) ; show entire entry
  12126. (when following-p
  12127. ;; Show next sibling, or heading below text
  12128. (save-excursion
  12129. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12130. (org-flag-heading nil))))
  12131. (when siblings-p (org-show-siblings))
  12132. (when hierarchy-p
  12133. ;; show all higher headings, possibly with siblings
  12134. (save-excursion
  12135. (while (and (condition-case nil
  12136. (progn (org-up-heading-all 1) t)
  12137. (error nil))
  12138. (not (bobp)))
  12139. (org-flag-heading nil)
  12140. (when siblings-p (org-show-siblings)))))
  12141. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12142. (defvar org-reveal-start-hook nil
  12143. "Hook run before revealing a location.")
  12144. (defun org-reveal (&optional siblings)
  12145. "Show current entry, hierarchy above it, and the following headline.
  12146. This can be used to show a consistent set of context around locations
  12147. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12148. not t for the search context.
  12149. With optional argument SIBLINGS, on each level of the hierarchy all
  12150. siblings are shown. This repairs the tree structure to what it would
  12151. look like when opened with hierarchical calls to `org-cycle'.
  12152. With double optional argument \\[universal-argument] \\[universal-argument], \
  12153. go to the parent and show the
  12154. entire tree."
  12155. (interactive "P")
  12156. (run-hooks 'org-reveal-start-hook)
  12157. (let ((org-show-hierarchy-above t)
  12158. (org-show-following-heading t)
  12159. (org-show-siblings (if siblings t org-show-siblings)))
  12160. (org-show-context nil))
  12161. (when (equal siblings '(16))
  12162. (save-excursion
  12163. (when (org-up-heading-safe)
  12164. (org-show-subtree)
  12165. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12166. (defun org-highlight-new-match (beg end)
  12167. "Highlight from BEG to END and mark the highlight is an occur headline."
  12168. (let ((ov (make-overlay beg end)))
  12169. (overlay-put ov 'face 'secondary-selection)
  12170. (overlay-put ov 'org-type 'org-occur)
  12171. (push ov org-occur-highlights)))
  12172. (defun org-remove-occur-highlights (&optional beg end noremove)
  12173. "Remove the occur highlights from the buffer.
  12174. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12175. from the `before-change-functions' in the current buffer."
  12176. (interactive)
  12177. (unless org-inhibit-highlight-removal
  12178. (mapc 'delete-overlay org-occur-highlights)
  12179. (setq org-occur-highlights nil)
  12180. (setq org-occur-parameters nil)
  12181. (unless noremove
  12182. (remove-hook 'before-change-functions
  12183. 'org-remove-occur-highlights 'local))))
  12184. ;;;; Priorities
  12185. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12186. "Regular expression matching the priority indicator.")
  12187. (defvar org-remove-priority-next-time nil)
  12188. (defun org-priority-up ()
  12189. "Increase the priority of the current item."
  12190. (interactive)
  12191. (org-priority 'up))
  12192. (defun org-priority-down ()
  12193. "Decrease the priority of the current item."
  12194. (interactive)
  12195. (org-priority 'down))
  12196. (defun org-priority (&optional action show)
  12197. "Change the priority of an item.
  12198. ACTION can be `set', `up', `down', or a character."
  12199. (interactive "P")
  12200. (if (equal action '(4))
  12201. (org-show-priority)
  12202. (unless org-enable-priority-commands
  12203. (error "Priority commands are disabled"))
  12204. (setq action (or action 'set))
  12205. (let (current new news have remove)
  12206. (save-excursion
  12207. (org-back-to-heading t)
  12208. (if (looking-at org-priority-regexp)
  12209. (setq current (string-to-char (match-string 2))
  12210. have t))
  12211. (cond
  12212. ((eq action 'remove)
  12213. (setq remove t new ?\ ))
  12214. ((or (eq action 'set)
  12215. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12216. (if (not (eq action 'set))
  12217. (setq new action)
  12218. (message "Priority %c-%c, SPC to remove: "
  12219. org-highest-priority org-lowest-priority)
  12220. (save-match-data
  12221. (setq new (read-char-exclusive))))
  12222. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12223. (= (upcase org-lowest-priority) org-lowest-priority))
  12224. (setq new (upcase new)))
  12225. (cond ((equal new ?\ ) (setq remove t))
  12226. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12227. (error "Priority must be between `%c' and `%c'"
  12228. org-highest-priority org-lowest-priority))))
  12229. ((eq action 'up)
  12230. (setq new (if have
  12231. (1- current) ; normal cycling
  12232. ;; last priority was empty
  12233. (if (eq last-command this-command)
  12234. org-lowest-priority ; wrap around empty to lowest
  12235. ;; default
  12236. (if org-priority-start-cycle-with-default
  12237. org-default-priority
  12238. (1- org-default-priority))))))
  12239. ((eq action 'down)
  12240. (setq new (if have
  12241. (1+ current) ; normal cycling
  12242. ;; last priority was empty
  12243. (if (eq last-command this-command)
  12244. org-highest-priority ; wrap around empty to highest
  12245. ;; default
  12246. (if org-priority-start-cycle-with-default
  12247. org-default-priority
  12248. (1+ org-default-priority))))))
  12249. (t (error "Invalid action")))
  12250. (if (or (< (upcase new) org-highest-priority)
  12251. (> (upcase new) org-lowest-priority))
  12252. (if (and (memq action '(up down))
  12253. (not have) (not (eq last-command this-command)))
  12254. ;; `new' is from default priority
  12255. (error
  12256. "The default can not be set, see `org-default-priority' why")
  12257. ;; normal cycling: `new' is beyond highest/lowest priority
  12258. ;; and is wrapped around to the empty priority
  12259. (setq remove t)))
  12260. (setq news (format "%c" new))
  12261. (if have
  12262. (if remove
  12263. (replace-match "" t t nil 1)
  12264. (replace-match news t t nil 2))
  12265. (if remove
  12266. (error "No priority cookie found in line")
  12267. (let ((case-fold-search nil))
  12268. (looking-at org-todo-line-regexp))
  12269. (if (match-end 2)
  12270. (progn
  12271. (goto-char (match-end 2))
  12272. (insert " [#" news "]"))
  12273. (goto-char (match-beginning 3))
  12274. (insert "[#" news "] "))))
  12275. (org-preserve-lc (org-set-tags nil 'align)))
  12276. (if remove
  12277. (message "Priority removed")
  12278. (message "Priority of current item set to %s" news)))))
  12279. (defun org-show-priority ()
  12280. "Show the priority of the current item.
  12281. This priority is composed of the main priority given with the [#A] cookies,
  12282. and by additional input from the age of a schedules or deadline entry."
  12283. (interactive)
  12284. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12285. (org-get-at-bol 'priority)
  12286. (save-excursion
  12287. (save-match-data
  12288. (beginning-of-line)
  12289. (and (looking-at org-heading-regexp)
  12290. (org-get-priority (match-string 0))))))))
  12291. (message "Priority is %d" (if pri pri -1000))))
  12292. (defun org-get-priority (s)
  12293. "Find priority cookie and return priority."
  12294. (save-match-data
  12295. (if (functionp org-get-priority-function)
  12296. (funcall org-get-priority-function)
  12297. (if (not (string-match org-priority-regexp s))
  12298. (* 1000 (- org-lowest-priority org-default-priority))
  12299. (* 1000 (- org-lowest-priority
  12300. (string-to-char (match-string 2 s))))))))
  12301. ;;;; Tags
  12302. (defvar org-agenda-archives-mode)
  12303. (defvar org-map-continue-from nil
  12304. "Position from where mapping should continue.
  12305. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12306. (defvar org-scanner-tags nil
  12307. "The current tag list while the tags scanner is running.")
  12308. (defvar org-trust-scanner-tags nil
  12309. "Should `org-get-tags-at' use the tags for the scanner.
  12310. This is for internal dynamical scoping only.
  12311. When this is non-nil, the function `org-get-tags-at' will return the value
  12312. of `org-scanner-tags' instead of building the list by itself. This
  12313. can lead to large speed-ups when the tags scanner is used in a file with
  12314. many entries, and when the list of tags is retrieved, for example to
  12315. obtain a list of properties. Building the tags list for each entry in such
  12316. a file becomes an N^2 operation - but with this variable set, it scales
  12317. as N.")
  12318. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12319. "Sca headline tags with inheritance and produce output ACTION.
  12320. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12321. or `agenda' to produce an entry list for an agenda view. It can also be
  12322. a Lisp form or a function that should be called at each matched headline, in
  12323. this case the return value is a list of all return values from these calls.
  12324. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12325. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12326. only lines with a not-done TODO keyword are included in the output.
  12327. This should be the same variable that was scoped into
  12328. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12329. START-LEVEL can be a string with asterisks, reducing the scope to
  12330. headlines matching this string."
  12331. (require 'org-agenda)
  12332. (let* ((re (concat "^"
  12333. (if start-level
  12334. ;; Get the correct level to match
  12335. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12336. org-outline-regexp)
  12337. " *\\(\\<\\("
  12338. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12339. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12340. (props (list 'face 'default
  12341. 'done-face 'org-agenda-done
  12342. 'undone-face 'default
  12343. 'mouse-face 'highlight
  12344. 'org-not-done-regexp org-not-done-regexp
  12345. 'org-todo-regexp org-todo-regexp
  12346. 'org-complex-heading-regexp org-complex-heading-regexp
  12347. 'help-echo
  12348. (format "mouse-2 or RET jump to org file %s"
  12349. (abbreviate-file-name
  12350. (or (buffer-file-name (buffer-base-buffer))
  12351. (buffer-name (buffer-base-buffer)))))))
  12352. (case-fold-search nil)
  12353. (org-map-continue-from nil)
  12354. lspos tags tags-list
  12355. (tags-alist (list (cons 0 org-file-tags)))
  12356. (llast 0) rtn rtn1 level category i txt
  12357. todo marker entry priority)
  12358. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12359. (setq action (list 'lambda nil action)))
  12360. (save-excursion
  12361. (goto-char (point-min))
  12362. (when (eq action 'sparse-tree)
  12363. (org-overview)
  12364. (org-remove-occur-highlights))
  12365. (while (re-search-forward re nil t)
  12366. (setq org-map-continue-from nil)
  12367. (catch :skip
  12368. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12369. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12370. (goto-char (setq lspos (match-beginning 0)))
  12371. (setq level (org-reduced-level (org-outline-level))
  12372. category (org-get-category))
  12373. (setq i llast llast level)
  12374. ;; remove tag lists from same and sublevels
  12375. (while (>= i level)
  12376. (when (setq entry (assoc i tags-alist))
  12377. (setq tags-alist (delete entry tags-alist)))
  12378. (setq i (1- i)))
  12379. ;; add the next tags
  12380. (when tags
  12381. (setq tags (org-split-string tags ":")
  12382. tags-alist
  12383. (cons (cons level tags) tags-alist)))
  12384. ;; compile tags for current headline
  12385. (setq tags-list
  12386. (if org-use-tag-inheritance
  12387. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12388. tags)
  12389. org-scanner-tags tags-list)
  12390. (when org-use-tag-inheritance
  12391. (setcdr (car tags-alist)
  12392. (mapcar (lambda (x)
  12393. (setq x (copy-sequence x))
  12394. (org-add-prop-inherited x))
  12395. (cdar tags-alist))))
  12396. (when (and tags org-use-tag-inheritance
  12397. (or (not (eq t org-use-tag-inheritance))
  12398. org-tags-exclude-from-inheritance))
  12399. ;; selective inheritance, remove uninherited ones
  12400. (setcdr (car tags-alist)
  12401. (org-remove-uninherited-tags (cdar tags-alist))))
  12402. (when (and
  12403. ;; eval matcher only when the todo condition is OK
  12404. (and (or (not todo-only) (member todo org-not-done-keywords))
  12405. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12406. (eval matcher)))
  12407. ;; Call the skipper, but return t if it does not skip,
  12408. ;; so that the `and' form continues evaluating
  12409. (progn
  12410. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12411. t)
  12412. ;; Check if timestamps are deselecting this entry
  12413. (or (not todo-only)
  12414. (and (member todo org-not-done-keywords)
  12415. (or (not org-agenda-tags-todo-honor-ignore-options)
  12416. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12417. ;; select this headline
  12418. (cond
  12419. ((eq action 'sparse-tree)
  12420. (and org-highlight-sparse-tree-matches
  12421. (org-get-heading) (match-end 0)
  12422. (org-highlight-new-match
  12423. (match-beginning 1) (match-end 1)))
  12424. (org-show-context 'tags-tree))
  12425. ((eq action 'agenda)
  12426. (setq txt (org-agenda-format-item
  12427. ""
  12428. (concat
  12429. (if (eq org-tags-match-list-sublevels 'indented)
  12430. (make-string (1- level) ?.) "")
  12431. (org-get-heading))
  12432. level category
  12433. tags-list)
  12434. priority (org-get-priority txt))
  12435. (goto-char lspos)
  12436. (setq marker (org-agenda-new-marker))
  12437. (org-add-props txt props
  12438. 'org-marker marker 'org-hd-marker marker 'org-category category
  12439. 'todo-state todo
  12440. 'priority priority 'type "tagsmatch")
  12441. (push txt rtn))
  12442. ((functionp action)
  12443. (setq org-map-continue-from nil)
  12444. (save-excursion
  12445. (setq rtn1 (funcall action))
  12446. (push rtn1 rtn)))
  12447. (t (error "Invalid action")))
  12448. ;; if we are to skip sublevels, jump to end of subtree
  12449. (unless org-tags-match-list-sublevels
  12450. (org-end-of-subtree t)
  12451. (backward-char 1))))
  12452. ;; Get the correct position from where to continue
  12453. (if org-map-continue-from
  12454. (goto-char org-map-continue-from)
  12455. (and (= (point) lspos) (end-of-line 1)))))
  12456. (when (and (eq action 'sparse-tree)
  12457. (not org-sparse-tree-open-archived-trees))
  12458. (org-hide-archived-subtrees (point-min) (point-max)))
  12459. (nreverse rtn)))
  12460. (defun org-remove-uninherited-tags (tags)
  12461. "Remove all tags that are not inherited from the list TAGS."
  12462. (cond
  12463. ((eq org-use-tag-inheritance t)
  12464. (if org-tags-exclude-from-inheritance
  12465. (org-delete-all org-tags-exclude-from-inheritance tags)
  12466. tags))
  12467. ((not org-use-tag-inheritance) nil)
  12468. ((stringp org-use-tag-inheritance)
  12469. (delq nil (mapcar
  12470. (lambda (x)
  12471. (if (and (string-match org-use-tag-inheritance x)
  12472. (not (member x org-tags-exclude-from-inheritance)))
  12473. x nil))
  12474. tags)))
  12475. ((listp org-use-tag-inheritance)
  12476. (delq nil (mapcar
  12477. (lambda (x)
  12478. (if (member x org-use-tag-inheritance) x nil))
  12479. tags)))))
  12480. (defun org-match-sparse-tree (&optional todo-only match)
  12481. "Create a sparse tree according to tags string MATCH.
  12482. MATCH can contain positive and negative selection of tags, like
  12483. \"+WORK+URGENT-WITHBOSS\".
  12484. If optional argument TODO-ONLY is non-nil, only select lines that are
  12485. also TODO lines."
  12486. (interactive "P")
  12487. (org-agenda-prepare-buffers (list (current-buffer)))
  12488. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12489. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12490. (defvar org-cached-props nil)
  12491. (defun org-cached-entry-get (pom property)
  12492. (if (or (eq t org-use-property-inheritance)
  12493. (and (stringp org-use-property-inheritance)
  12494. (string-match org-use-property-inheritance property))
  12495. (and (listp org-use-property-inheritance)
  12496. (member property org-use-property-inheritance)))
  12497. ;; Caching is not possible, check it directly
  12498. (org-entry-get pom property 'inherit)
  12499. ;; Get all properties, so that we can do complicated checks easily
  12500. (cdr (assoc property (or org-cached-props
  12501. (setq org-cached-props
  12502. (org-entry-properties pom)))))))
  12503. (defun org-global-tags-completion-table (&optional files)
  12504. "Return the list of all tags in all agenda buffer/files.
  12505. Optional FILES argument is a list of files which can be used
  12506. instead of the agenda files."
  12507. (save-excursion
  12508. (org-uniquify
  12509. (delq nil
  12510. (apply 'append
  12511. (mapcar
  12512. (lambda (file)
  12513. (set-buffer (find-file-noselect file))
  12514. (append (org-get-buffer-tags)
  12515. (mapcar (lambda (x) (if (stringp (car-safe x))
  12516. (list (car-safe x)) nil))
  12517. org-tag-alist)))
  12518. (if (and files (car files))
  12519. files
  12520. (org-agenda-files))))))))
  12521. (defun org-make-tags-matcher (match)
  12522. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12523. The variable `todo-only' is scoped dynamically into this function.
  12524. It will be set to t if the matcher restricts matching to TODO entries,
  12525. otherwise will not be touched.
  12526. Returns a cons of the selection string MATCH and the constructed
  12527. lisp form implementing the matcher. The matcher is to be evaluated
  12528. at an Org entry, with point on the headline, and returns t if the
  12529. entry matches the selection string MATCH. The returned lisp form
  12530. references two variables with information about the entry, which
  12531. must be bound around the form's evaluation: todo, the TODO keyword
  12532. at the entry (or nil of none); and tags-list, the list of all tags
  12533. at the entry including inherited ones. Additionally, the category
  12534. of the entry (if any) must be specified as the text property
  12535. 'org-category on the headline.
  12536. See also `org-scan-tags'.
  12537. "
  12538. (declare (special todo-only))
  12539. (unless (boundp 'todo-only)
  12540. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12541. (unless match
  12542. ;; Get a new match request, with completion
  12543. (let ((org-last-tags-completion-table
  12544. (org-global-tags-completion-table)))
  12545. (setq match (org-completing-read-no-i
  12546. "Match: " 'org-tags-completion-function nil nil nil
  12547. 'org-tags-history))))
  12548. ;; Parse the string and create a lisp form
  12549. (let ((match0 match)
  12550. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12551. minus tag mm
  12552. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12553. orterms term orlist re-p str-p level-p level-op time-p
  12554. prop-p pn pv po gv rest)
  12555. (if (string-match "/+" match)
  12556. ;; match contains also a todo-matching request
  12557. (progn
  12558. (setq tagsmatch (substring match 0 (match-beginning 0))
  12559. todomatch (substring match (match-end 0)))
  12560. (if (string-match "^!" todomatch)
  12561. (setq todo-only t todomatch (substring todomatch 1)))
  12562. (if (string-match "^\\s-*$" todomatch)
  12563. (setq todomatch nil)))
  12564. ;; only matching tags
  12565. (setq tagsmatch match todomatch nil))
  12566. ;; Make the tags matcher
  12567. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12568. (setq tagsmatcher t)
  12569. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12570. (while (setq term (pop orterms))
  12571. (while (and (equal (substring term -1) "\\") orterms)
  12572. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12573. (while (string-match re term)
  12574. (setq rest (substring term (match-end 0))
  12575. minus (and (match-end 1)
  12576. (equal (match-string 1 term) "-"))
  12577. tag (save-match-data (replace-regexp-in-string
  12578. "\\\\-" "-"
  12579. (match-string 2 term)))
  12580. re-p (equal (string-to-char tag) ?{)
  12581. level-p (match-end 4)
  12582. prop-p (match-end 5)
  12583. mm (cond
  12584. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12585. (level-p
  12586. (setq level-op (org-op-to-function (match-string 3 term)))
  12587. `(,level-op level ,(string-to-number
  12588. (match-string 4 term))))
  12589. (prop-p
  12590. (setq pn (match-string 5 term)
  12591. po (match-string 6 term)
  12592. pv (match-string 7 term)
  12593. re-p (equal (string-to-char pv) ?{)
  12594. str-p (equal (string-to-char pv) ?\")
  12595. time-p (save-match-data
  12596. (string-match "^\"[[<].*[]>]\"$" pv))
  12597. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12598. (if time-p (setq pv (org-matcher-time pv)))
  12599. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12600. (cond
  12601. ((equal pn "CATEGORY")
  12602. (setq gv '(get-text-property (point) 'org-category)))
  12603. ((equal pn "TODO")
  12604. (setq gv 'todo))
  12605. (t
  12606. (setq gv `(org-cached-entry-get nil ,pn))))
  12607. (if re-p
  12608. (if (eq po 'org<>)
  12609. `(not (string-match ,pv (or ,gv "")))
  12610. `(string-match ,pv (or ,gv "")))
  12611. (if str-p
  12612. `(,po (or ,gv "") ,pv)
  12613. `(,po (string-to-number (or ,gv ""))
  12614. ,(string-to-number pv) ))))
  12615. (t `(member ,tag tags-list)))
  12616. mm (if minus (list 'not mm) mm)
  12617. term rest)
  12618. (push mm tagsmatcher))
  12619. (push (if (> (length tagsmatcher) 1)
  12620. (cons 'and tagsmatcher)
  12621. (car tagsmatcher))
  12622. orlist)
  12623. (setq tagsmatcher nil))
  12624. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12625. (setq tagsmatcher
  12626. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12627. ;; Make the todo matcher
  12628. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12629. (setq todomatcher t)
  12630. (setq orterms (org-split-string todomatch "|") orlist nil)
  12631. (while (setq term (pop orterms))
  12632. (while (string-match re term)
  12633. (setq minus (and (match-end 1)
  12634. (equal (match-string 1 term) "-"))
  12635. kwd (match-string 2 term)
  12636. re-p (equal (string-to-char kwd) ?{)
  12637. term (substring term (match-end 0))
  12638. mm (if re-p
  12639. `(string-match ,(substring kwd 1 -1) todo)
  12640. (list 'equal 'todo kwd))
  12641. mm (if minus (list 'not mm) mm))
  12642. (push mm todomatcher))
  12643. (push (if (> (length todomatcher) 1)
  12644. (cons 'and todomatcher)
  12645. (car todomatcher))
  12646. orlist)
  12647. (setq todomatcher nil))
  12648. (setq todomatcher (if (> (length orlist) 1)
  12649. (cons 'or orlist) (car orlist))))
  12650. ;; Return the string and lisp forms of the matcher
  12651. (setq matcher (if todomatcher
  12652. (list 'and tagsmatcher todomatcher)
  12653. tagsmatcher))
  12654. (when todo-only
  12655. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12656. matcher)))
  12657. (cons match0 matcher)))
  12658. (defun org-op-to-function (op &optional stringp)
  12659. "Turn an operator into the appropriate function."
  12660. (setq op
  12661. (cond
  12662. ((equal op "<" ) '(< string< org-time<))
  12663. ((equal op ">" ) '(> org-string> org-time>))
  12664. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12665. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12666. ((member op '("=" "==")) '(= string= org-time=))
  12667. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12668. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12669. (defun org<> (a b) (not (= a b)))
  12670. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12671. (defun org-string>= (a b) (not (string< a b)))
  12672. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12673. (defun org-string<> (a b) (not (string= a b)))
  12674. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12675. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12676. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12677. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12678. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12679. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12680. (defun org-2ft (s)
  12681. "Convert S to a floating point time.
  12682. If S is already a number, just return it. If it is a string, parse
  12683. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12684. (cond
  12685. ((numberp s) s)
  12686. ((stringp s)
  12687. (condition-case nil
  12688. (float-time (apply 'encode-time (org-parse-time-string s)))
  12689. (error 0.)))
  12690. (t 0.)))
  12691. (defun org-time-today ()
  12692. "Time in seconds today at 0:00.
  12693. Returns the float number of seconds since the beginning of the
  12694. epoch to the beginning of today (00:00)."
  12695. (float-time (apply 'encode-time
  12696. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12697. (defun org-matcher-time (s)
  12698. "Interpret a time comparison value."
  12699. (save-match-data
  12700. (cond
  12701. ((string= s "<now>") (float-time))
  12702. ((string= s "<today>") (org-time-today))
  12703. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12704. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12705. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12706. (+ (org-time-today)
  12707. (* (string-to-number (match-string 1 s))
  12708. (cdr (assoc (match-string 2 s)
  12709. '(("d" . 86400.0) ("w" . 604800.0)
  12710. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12711. (t (org-2ft s)))))
  12712. (defun org-match-any-p (re list)
  12713. "Does re match any element of list?"
  12714. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12715. (delq nil list))
  12716. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12717. (defvar org-tags-overlay (make-overlay 1 1))
  12718. (org-detach-overlay org-tags-overlay)
  12719. (defun org-get-local-tags-at (&optional pos)
  12720. "Get a list of tags defined in the current headline."
  12721. (org-get-tags-at pos 'local))
  12722. (defun org-get-local-tags ()
  12723. "Get a list of tags defined in the current headline."
  12724. (org-get-tags-at nil 'local))
  12725. (defun org-get-tags-at (&optional pos local)
  12726. "Get a list of all headline tags applicable at POS.
  12727. POS defaults to point. If tags are inherited, the list contains
  12728. the targets in the same sequence as the headlines appear, i.e.
  12729. the tags of the current headline come last.
  12730. When LOCAL is non-nil, only return tags from the current headline,
  12731. ignore inherited ones."
  12732. (interactive)
  12733. (if (and org-trust-scanner-tags
  12734. (or (not pos) (equal pos (point)))
  12735. (not local))
  12736. org-scanner-tags
  12737. (let (tags ltags lastpos parent)
  12738. (save-excursion
  12739. (save-restriction
  12740. (widen)
  12741. (goto-char (or pos (point)))
  12742. (save-match-data
  12743. (catch 'done
  12744. (condition-case nil
  12745. (progn
  12746. (org-back-to-heading t)
  12747. (while (not (equal lastpos (point)))
  12748. (setq lastpos (point))
  12749. (when (looking-at
  12750. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12751. (setq ltags (org-split-string
  12752. (org-match-string-no-properties 1) ":"))
  12753. (when parent
  12754. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12755. (setq tags (append
  12756. (if parent
  12757. (org-remove-uninherited-tags ltags)
  12758. ltags)
  12759. tags)))
  12760. (or org-use-tag-inheritance (throw 'done t))
  12761. (if local (throw 'done t))
  12762. (or (org-up-heading-safe) (error nil))
  12763. (setq parent t)))
  12764. (error nil)))))
  12765. (if local
  12766. tags
  12767. (reverse (delete-dups
  12768. (reverse (append
  12769. (org-remove-uninherited-tags
  12770. org-file-tags) tags)))))))))
  12771. (defun org-add-prop-inherited (s)
  12772. (add-text-properties 0 (length s) '(inherited t) s)
  12773. s)
  12774. (defun org-toggle-tag (tag &optional onoff)
  12775. "Toggle the tag TAG for the current line.
  12776. If ONOFF is `on' or `off', don't toggle but set to this state."
  12777. (let (res current)
  12778. (save-excursion
  12779. (org-back-to-heading t)
  12780. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12781. (point-at-eol) t)
  12782. (progn
  12783. (setq current (match-string 1))
  12784. (replace-match ""))
  12785. (setq current ""))
  12786. (setq current (nreverse (org-split-string current ":")))
  12787. (cond
  12788. ((eq onoff 'on)
  12789. (setq res t)
  12790. (or (member tag current) (push tag current)))
  12791. ((eq onoff 'off)
  12792. (or (not (member tag current)) (setq current (delete tag current))))
  12793. (t (if (member tag current)
  12794. (setq current (delete tag current))
  12795. (setq res t)
  12796. (push tag current))))
  12797. (end-of-line 1)
  12798. (if current
  12799. (progn
  12800. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12801. (org-set-tags nil t))
  12802. (delete-horizontal-space))
  12803. (run-hooks 'org-after-tags-change-hook))
  12804. res))
  12805. (defun org-align-tags-here (to-col)
  12806. ;; Assumes that this is a headline
  12807. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12808. (beginning-of-line 1)
  12809. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12810. (< pos (match-beginning 2)))
  12811. (progn
  12812. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12813. (goto-char (match-beginning 1))
  12814. (insert " ")
  12815. (delete-region (point) (1+ (match-beginning 2)))
  12816. (setq ncol (max (current-column)
  12817. (1+ col)
  12818. (if (> to-col 0)
  12819. to-col
  12820. (- (abs to-col) tags-l))))
  12821. (setq p (point))
  12822. (insert (make-string (- ncol (current-column)) ?\ ))
  12823. (setq ncol (current-column))
  12824. (when indent-tabs-mode (tabify p (point-at-eol)))
  12825. (org-move-to-column (min ncol col) t))
  12826. (goto-char pos))))
  12827. (defun org-set-tags-command (&optional arg just-align)
  12828. "Call the set-tags command for the current entry."
  12829. (interactive "P")
  12830. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12831. (org-set-tags arg just-align)
  12832. (save-excursion
  12833. (unless (and (org-region-active-p)
  12834. org-loop-over-headlines-in-active-region)
  12835. (org-back-to-heading t))
  12836. (org-set-tags arg just-align))))
  12837. (defun org-set-tags-to (data)
  12838. "Set the tags of the current entry to DATA, replacing the current tags.
  12839. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12840. If DATA is nil or the empty string, any tags will be removed."
  12841. (interactive "sTags: ")
  12842. (setq data
  12843. (cond
  12844. ((eq data nil) "")
  12845. ((equal data "") "")
  12846. ((stringp data)
  12847. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12848. ":"))
  12849. ((listp data)
  12850. (concat ":" (mapconcat 'identity data ":") ":"))))
  12851. (when data
  12852. (save-excursion
  12853. (org-back-to-heading t)
  12854. (when (looking-at org-complex-heading-regexp)
  12855. (if (match-end 5)
  12856. (progn
  12857. (goto-char (match-beginning 5))
  12858. (insert data)
  12859. (delete-region (point) (point-at-eol))
  12860. (org-set-tags nil 'align))
  12861. (goto-char (point-at-eol))
  12862. (insert " " data)
  12863. (org-set-tags nil 'align)))
  12864. (beginning-of-line 1)
  12865. (if (looking-at ".*?\\([ \t]+\\)$")
  12866. (delete-region (match-beginning 1) (match-end 1))))))
  12867. (defun org-align-all-tags ()
  12868. "Align the tags i all headings."
  12869. (interactive)
  12870. (save-excursion
  12871. (or (ignore-errors (org-back-to-heading t))
  12872. (outline-next-heading))
  12873. (if (org-at-heading-p)
  12874. (org-set-tags t)
  12875. (message "No headings"))))
  12876. (defvar org-indent-indentation-per-level)
  12877. (defun org-set-tags (&optional arg just-align)
  12878. "Set the tags for the current headline.
  12879. With prefix ARG, realign all tags in headings in the current buffer."
  12880. (interactive "P")
  12881. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12882. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12883. 'region-start-level 'region))
  12884. org-loop-over-headlines-in-active-region)
  12885. (org-map-entries
  12886. ;; We don't use ARG and JUST-ALIGN here these args are not
  12887. ;; useful when looping over headlines
  12888. `(org-set-tags)
  12889. org-loop-over-headlines-in-active-region
  12890. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12891. (let* ((re org-outline-regexp-bol)
  12892. (current (unless arg (org-get-tags-string)))
  12893. (col (current-column))
  12894. (org-setting-tags t)
  12895. table current-tags inherited-tags ; computed below when needed
  12896. tags p0 c0 c1 rpl di tc level)
  12897. (if arg
  12898. (save-excursion
  12899. (goto-char (point-min))
  12900. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12901. (while (re-search-forward re nil t)
  12902. (org-set-tags nil t)
  12903. (end-of-line 1)))
  12904. (message "All tags realigned to column %d" org-tags-column))
  12905. (if just-align
  12906. (setq tags current)
  12907. ;; Get a new set of tags from the user
  12908. (save-excursion
  12909. (setq table (append org-tag-persistent-alist
  12910. (or org-tag-alist (org-get-buffer-tags))
  12911. (and
  12912. org-complete-tags-always-offer-all-agenda-tags
  12913. (org-global-tags-completion-table
  12914. (org-agenda-files))))
  12915. org-last-tags-completion-table table
  12916. current-tags (org-split-string current ":")
  12917. inherited-tags (nreverse
  12918. (nthcdr (length current-tags)
  12919. (nreverse (org-get-tags-at))))
  12920. tags
  12921. (if (or (eq t org-use-fast-tag-selection)
  12922. (and org-use-fast-tag-selection
  12923. (delq nil (mapcar 'cdr table))))
  12924. (org-fast-tag-selection
  12925. current-tags inherited-tags table
  12926. (if org-fast-tag-selection-include-todo
  12927. org-todo-key-alist))
  12928. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12929. (org-trim
  12930. (org-icompleting-read "Tags: "
  12931. 'org-tags-completion-function
  12932. nil nil current 'org-tags-history))))))
  12933. (while (string-match "[-+&]+" tags)
  12934. ;; No boolean logic, just a list
  12935. (setq tags (replace-match ":" t t tags))))
  12936. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12937. (if org-tags-sort-function
  12938. (setq tags (mapconcat 'identity
  12939. (sort (org-split-string
  12940. tags (org-re "[^[:alnum:]_@#%]+"))
  12941. org-tags-sort-function) ":")))
  12942. (if (string-match "\\`[\t ]*\\'" tags)
  12943. (setq tags "")
  12944. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12945. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12946. ;; Insert new tags at the correct column
  12947. (beginning-of-line 1)
  12948. (setq level (or (and (looking-at org-outline-regexp)
  12949. (- (match-end 0) (point) 1))
  12950. 1))
  12951. (cond
  12952. ((and (equal current "") (equal tags "")))
  12953. ((re-search-forward
  12954. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12955. (point-at-eol) t)
  12956. (if (equal tags "")
  12957. (setq rpl "")
  12958. (goto-char (match-beginning 0))
  12959. (setq c0 (current-column)
  12960. ;; compute offset for the case of org-indent-mode active
  12961. di (if org-indent-mode
  12962. (* (1- org-indent-indentation-per-level) (1- level))
  12963. 0)
  12964. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12965. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12966. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12967. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12968. (replace-match rpl t t)
  12969. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12970. tags)
  12971. (t (error "Tags alignment failed")))
  12972. (org-move-to-column col)
  12973. (unless just-align
  12974. (run-hooks 'org-after-tags-change-hook))))))
  12975. (defun org-change-tag-in-region (beg end tag off)
  12976. "Add or remove TAG for each entry in the region.
  12977. This works in the agenda, and also in an org-mode buffer."
  12978. (interactive
  12979. (list (region-beginning) (region-end)
  12980. (let ((org-last-tags-completion-table
  12981. (if (derived-mode-p 'org-mode)
  12982. (org-get-buffer-tags)
  12983. (org-global-tags-completion-table))))
  12984. (org-icompleting-read
  12985. "Tag: " 'org-tags-completion-function nil nil nil
  12986. 'org-tags-history))
  12987. (progn
  12988. (message "[s]et or [r]emove? ")
  12989. (equal (read-char-exclusive) ?r))))
  12990. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12991. (let ((agendap (equal major-mode 'org-agenda-mode))
  12992. l1 l2 m buf pos newhead (cnt 0))
  12993. (goto-char end)
  12994. (setq l2 (1- (org-current-line)))
  12995. (goto-char beg)
  12996. (setq l1 (org-current-line))
  12997. (loop for l from l1 to l2 do
  12998. (org-goto-line l)
  12999. (setq m (get-text-property (point) 'org-hd-marker))
  13000. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13001. (and agendap m))
  13002. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13003. pos (if agendap m (point)))
  13004. (with-current-buffer buf
  13005. (save-excursion
  13006. (save-restriction
  13007. (goto-char pos)
  13008. (setq cnt (1+ cnt))
  13009. (org-toggle-tag tag (if off 'off 'on))
  13010. (setq newhead (org-get-heading)))))
  13011. (and agendap (org-agenda-change-all-lines newhead m))))
  13012. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13013. (defun org-tags-completion-function (string predicate &optional flag)
  13014. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13015. (confirm (lambda (x) (stringp (car x)))))
  13016. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13017. (setq s1 (match-string 1 string)
  13018. s2 (match-string 2 string))
  13019. (setq s1 "" s2 string))
  13020. (cond
  13021. ((eq flag nil)
  13022. ;; try completion
  13023. (setq rtn (try-completion s2 ctable confirm))
  13024. (if (stringp rtn)
  13025. (setq rtn
  13026. (concat s1 s2 (substring rtn (length s2))
  13027. (if (and org-add-colon-after-tag-completion
  13028. (assoc rtn ctable))
  13029. ":" ""))))
  13030. rtn)
  13031. ((eq flag t)
  13032. ;; all-completions
  13033. (all-completions s2 ctable confirm)
  13034. )
  13035. ((eq flag 'lambda)
  13036. ;; exact match?
  13037. (assoc s2 ctable)))
  13038. ))
  13039. (defun org-fast-tag-insert (kwd tags face &optional end)
  13040. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  13041. (insert (format "%-12s" (concat kwd ":"))
  13042. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13043. (or end "")))
  13044. (defun org-fast-tag-show-exit (flag)
  13045. (save-excursion
  13046. (org-goto-line 3)
  13047. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13048. (replace-match ""))
  13049. (when flag
  13050. (end-of-line 1)
  13051. (org-move-to-column (- (window-width) 19) t)
  13052. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13053. (defun org-set-current-tags-overlay (current prefix)
  13054. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13055. (if (featurep 'xemacs)
  13056. (org-overlay-display org-tags-overlay (concat prefix s)
  13057. 'secondary-selection)
  13058. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13059. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13060. (defvar org-last-tag-selection-key nil)
  13061. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13062. "Fast tag selection with single keys.
  13063. CURRENT is the current list of tags in the headline, INHERITED is the
  13064. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13065. possibly with grouping information. TODO-TABLE is a similar table with
  13066. TODO keywords, should these have keys assigned to them.
  13067. If the keys are nil, a-z are automatically assigned.
  13068. Returns the new tags string, or nil to not change the current settings."
  13069. (let* ((fulltable (append table todo-table))
  13070. (maxlen (apply 'max (mapcar
  13071. (lambda (x)
  13072. (if (stringp (car x)) (string-width (car x)) 0))
  13073. fulltable)))
  13074. (buf (current-buffer))
  13075. (expert (eq org-fast-tag-selection-single-key 'expert))
  13076. (buffer-tags nil)
  13077. (fwidth (+ maxlen 3 1 3))
  13078. (ncol (/ (- (window-width) 4) fwidth))
  13079. (i-face 'org-done)
  13080. (c-face 'org-todo)
  13081. tg cnt e c char c1 c2 ntable tbl rtn
  13082. ov-start ov-end ov-prefix
  13083. (exit-after-next org-fast-tag-selection-single-key)
  13084. (done-keywords org-done-keywords)
  13085. groups ingroup)
  13086. (save-excursion
  13087. (beginning-of-line 1)
  13088. (if (looking-at
  13089. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13090. (setq ov-start (match-beginning 1)
  13091. ov-end (match-end 1)
  13092. ov-prefix "")
  13093. (setq ov-start (1- (point-at-eol))
  13094. ov-end (1+ ov-start))
  13095. (skip-chars-forward "^\n\r")
  13096. (setq ov-prefix
  13097. (concat
  13098. (buffer-substring (1- (point)) (point))
  13099. (if (> (current-column) org-tags-column)
  13100. " "
  13101. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13102. (move-overlay org-tags-overlay ov-start ov-end)
  13103. (save-window-excursion
  13104. (if expert
  13105. (set-buffer (get-buffer-create " *Org tags*"))
  13106. (delete-other-windows)
  13107. (split-window-vertically)
  13108. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13109. (erase-buffer)
  13110. (org-set-local 'org-done-keywords done-keywords)
  13111. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13112. (org-fast-tag-insert "Current" current c-face "\n\n")
  13113. (org-fast-tag-show-exit exit-after-next)
  13114. (org-set-current-tags-overlay current ov-prefix)
  13115. (setq tbl fulltable char ?a cnt 0)
  13116. (while (setq e (pop tbl))
  13117. (cond
  13118. ((equal (car e) :startgroup)
  13119. (push '() groups) (setq ingroup t)
  13120. (when (not (= cnt 0))
  13121. (setq cnt 0)
  13122. (insert "\n"))
  13123. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13124. ((equal (car e) :endgroup)
  13125. (setq ingroup nil cnt 0)
  13126. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13127. ((equal e '(:newline))
  13128. (when (not (= cnt 0))
  13129. (setq cnt 0)
  13130. (insert "\n")
  13131. (setq e (car tbl))
  13132. (while (equal (car tbl) '(:newline))
  13133. (insert "\n")
  13134. (setq tbl (cdr tbl)))))
  13135. (t
  13136. (setq tg (copy-sequence (car e)) c2 nil)
  13137. (if (cdr e)
  13138. (setq c (cdr e))
  13139. ;; automatically assign a character.
  13140. (setq c1 (string-to-char
  13141. (downcase (substring
  13142. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13143. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13144. (while (or (rassoc char ntable) (rassoc char table))
  13145. (setq char (1+ char)))
  13146. (setq c2 c1))
  13147. (setq c (or c2 char)))
  13148. (if ingroup (push tg (car groups)))
  13149. (setq tg (org-add-props tg nil 'face
  13150. (cond
  13151. ((not (assoc tg table))
  13152. (org-get-todo-face tg))
  13153. ((member tg current) c-face)
  13154. ((member tg inherited) i-face))))
  13155. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13156. (insert "[" c "] " tg (make-string
  13157. (- fwidth 4 (length tg)) ?\ ))
  13158. (push (cons tg c) ntable)
  13159. (when (= (setq cnt (1+ cnt)) ncol)
  13160. (insert "\n")
  13161. (if ingroup (insert " "))
  13162. (setq cnt 0)))))
  13163. (setq ntable (nreverse ntable))
  13164. (insert "\n")
  13165. (goto-char (point-min))
  13166. (if (not expert) (org-fit-window-to-buffer))
  13167. (setq rtn
  13168. (catch 'exit
  13169. (while t
  13170. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13171. (if (not groups) "no " "")
  13172. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13173. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13174. (setq org-last-tag-selection-key c)
  13175. (cond
  13176. ((= c ?\r) (throw 'exit t))
  13177. ((= c ?!)
  13178. (setq groups (not groups))
  13179. (goto-char (point-min))
  13180. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13181. ((= c ?\C-c)
  13182. (if (not expert)
  13183. (org-fast-tag-show-exit
  13184. (setq exit-after-next (not exit-after-next)))
  13185. (setq expert nil)
  13186. (delete-other-windows)
  13187. (set-window-buffer (split-window-vertically) " *Org tags*")
  13188. (org-switch-to-buffer-other-window " *Org tags*")
  13189. (org-fit-window-to-buffer)))
  13190. ((or (= c ?\C-g)
  13191. (and (= c ?q) (not (rassoc c ntable))))
  13192. (org-detach-overlay org-tags-overlay)
  13193. (setq quit-flag t))
  13194. ((= c ?\ )
  13195. (setq current nil)
  13196. (if exit-after-next (setq exit-after-next 'now)))
  13197. ((= c ?\t)
  13198. (condition-case nil
  13199. (setq tg (org-icompleting-read
  13200. "Tag: "
  13201. (or buffer-tags
  13202. (with-current-buffer buf
  13203. (org-get-buffer-tags)))))
  13204. (quit (setq tg "")))
  13205. (when (string-match "\\S-" tg)
  13206. (add-to-list 'buffer-tags (list tg))
  13207. (if (member tg current)
  13208. (setq current (delete tg current))
  13209. (push tg current)))
  13210. (if exit-after-next (setq exit-after-next 'now)))
  13211. ((setq e (rassoc c todo-table) tg (car e))
  13212. (with-current-buffer buf
  13213. (save-excursion (org-todo tg)))
  13214. (if exit-after-next (setq exit-after-next 'now)))
  13215. ((setq e (rassoc c ntable) tg (car e))
  13216. (if (member tg current)
  13217. (setq current (delete tg current))
  13218. (loop for g in groups do
  13219. (if (member tg g)
  13220. (mapc (lambda (x)
  13221. (setq current (delete x current)))
  13222. g)))
  13223. (push tg current))
  13224. (if exit-after-next (setq exit-after-next 'now))))
  13225. ;; Create a sorted list
  13226. (setq current
  13227. (sort current
  13228. (lambda (a b)
  13229. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13230. (if (eq exit-after-next 'now) (throw 'exit t))
  13231. (goto-char (point-min))
  13232. (beginning-of-line 2)
  13233. (delete-region (point) (point-at-eol))
  13234. (org-fast-tag-insert "Current" current c-face)
  13235. (org-set-current-tags-overlay current ov-prefix)
  13236. (while (re-search-forward
  13237. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13238. (setq tg (match-string 1))
  13239. (add-text-properties
  13240. (match-beginning 1) (match-end 1)
  13241. (list 'face
  13242. (cond
  13243. ((member tg current) c-face)
  13244. ((member tg inherited) i-face)
  13245. (t (get-text-property (match-beginning 1) 'face))))))
  13246. (goto-char (point-min)))))
  13247. (org-detach-overlay org-tags-overlay)
  13248. (if rtn
  13249. (mapconcat 'identity current ":")
  13250. nil))))
  13251. (defun org-get-tags-string ()
  13252. "Get the TAGS string in the current headline."
  13253. (unless (org-at-heading-p t)
  13254. (error "Not on a heading"))
  13255. (save-excursion
  13256. (beginning-of-line 1)
  13257. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13258. (org-match-string-no-properties 1)
  13259. "")))
  13260. (defun org-get-tags ()
  13261. "Get the list of tags specified in the current headline."
  13262. (org-split-string (org-get-tags-string) ":"))
  13263. (defun org-get-buffer-tags ()
  13264. "Get a table of all tags used in the buffer, for completion."
  13265. (let (tags)
  13266. (save-excursion
  13267. (goto-char (point-min))
  13268. (while (re-search-forward
  13269. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13270. (when (equal (char-after (point-at-bol 0)) ?*)
  13271. (mapc (lambda (x) (add-to-list 'tags x))
  13272. (org-split-string (org-match-string-no-properties 1) ":")))))
  13273. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13274. (mapcar 'list tags)))
  13275. ;;;; The mapping API
  13276. (defun org-map-entries (func &optional match scope &rest skip)
  13277. "Call FUNC at each headline selected by MATCH in SCOPE.
  13278. FUNC is a function or a lisp form. The function will be called without
  13279. arguments, with the cursor positioned at the beginning of the headline.
  13280. The return values of all calls to the function will be collected and
  13281. returned as a list.
  13282. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13283. does not need to preserve point. After evaluation, the cursor will be
  13284. moved to the end of the line (presumably of the headline of the
  13285. processed entry) and search continues from there. Under some
  13286. circumstances, this may not produce the wanted results. For example,
  13287. if you have removed (e.g. archived) the current (sub)tree it could
  13288. mean that the next entry will be skipped entirely. In such cases, you
  13289. can specify the position from where search should continue by making
  13290. FUNC set the variable `org-map-continue-from' to the desired buffer
  13291. position.
  13292. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13293. Only headlines that are matched by this query will be considered during
  13294. the iteration. When MATCH is nil or t, all headlines will be
  13295. visited by the iteration.
  13296. SCOPE determines the scope of this command. It can be any of:
  13297. nil The current buffer, respecting the restriction if any
  13298. tree The subtree started with the entry at point
  13299. region The entries within the active region, if any
  13300. region-start-level
  13301. The entries within the active region, but only those at
  13302. the same level than the first one.
  13303. file The current buffer, without restriction
  13304. file-with-archives
  13305. The current buffer, and any archives associated with it
  13306. agenda All agenda files
  13307. agenda-with-archives
  13308. All agenda files with any archive files associated with them
  13309. \(file1 file2 ...)
  13310. If this is a list, all files in the list will be scanned
  13311. The remaining args are treated as settings for the skipping facilities of
  13312. the scanner. The following items can be given here:
  13313. archive skip trees with the archive tag
  13314. comment skip trees with the COMMENT keyword
  13315. function or Emacs Lisp form:
  13316. will be used as value for `org-agenda-skip-function', so
  13317. whenever the function returns a position, FUNC will not be
  13318. called for that entry and search will continue from the
  13319. position returned
  13320. If your function needs to retrieve the tags including inherited tags
  13321. at the *current* entry, you can use the value of the variable
  13322. `org-scanner-tags' which will be much faster than getting the value
  13323. with `org-get-tags-at'. If your function gets properties with
  13324. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13325. to t around the call to `org-entry-properties' to get the same speedup.
  13326. Note that if your function moves around to retrieve tags and properties at
  13327. a *different* entry, you cannot use these techniques."
  13328. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13329. (not (org-region-active-p)))
  13330. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13331. (org-agenda-skip-archived-trees (memq 'archive skip))
  13332. (org-agenda-skip-comment-trees (memq 'comment skip))
  13333. (org-agenda-skip-function
  13334. (car (org-delete-all '(comment archive) skip)))
  13335. (org-tags-match-list-sublevels t)
  13336. (start-level (eq scope 'region-start-level))
  13337. matcher file res
  13338. org-todo-keywords-for-agenda
  13339. org-done-keywords-for-agenda
  13340. org-todo-keyword-alist-for-agenda
  13341. org-drawers-for-agenda
  13342. org-tag-alist-for-agenda
  13343. todo-only)
  13344. (cond
  13345. ((eq match t) (setq matcher t))
  13346. ((eq match nil) (setq matcher t))
  13347. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13348. (save-excursion
  13349. (save-restriction
  13350. (cond ((eq scope 'tree)
  13351. (org-back-to-heading t)
  13352. (org-narrow-to-subtree)
  13353. (setq scope nil))
  13354. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13355. (org-region-active-p))
  13356. ;; If needed, set start-level to a string like "2"
  13357. (when start-level
  13358. (save-excursion
  13359. (goto-char (region-beginning))
  13360. (unless (org-at-heading-p) (outline-next-heading))
  13361. (setq start-level (org-current-level))))
  13362. (narrow-to-region (region-beginning)
  13363. (save-excursion
  13364. (goto-char (region-end))
  13365. (unless (and (bolp) (org-at-heading-p))
  13366. (outline-next-heading))
  13367. (point)))
  13368. (setq scope nil)))
  13369. (if (not scope)
  13370. (progn
  13371. (org-agenda-prepare-buffers
  13372. (list (buffer-file-name (current-buffer))))
  13373. (setq res (org-scan-tags func matcher todo-only start-level)))
  13374. ;; Get the right scope
  13375. (cond
  13376. ((and scope (listp scope) (symbolp (car scope)))
  13377. (setq scope (eval scope)))
  13378. ((eq scope 'agenda)
  13379. (setq scope (org-agenda-files t)))
  13380. ((eq scope 'agenda-with-archives)
  13381. (setq scope (org-agenda-files t))
  13382. (setq scope (org-add-archive-files scope)))
  13383. ((eq scope 'file)
  13384. (setq scope (list (buffer-file-name))))
  13385. ((eq scope 'file-with-archives)
  13386. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13387. (org-agenda-prepare-buffers scope)
  13388. (while (setq file (pop scope))
  13389. (with-current-buffer (org-find-base-buffer-visiting file)
  13390. (save-excursion
  13391. (save-restriction
  13392. (widen)
  13393. (goto-char (point-min))
  13394. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13395. res)))
  13396. ;;;; Properties
  13397. ;;; Setting and retrieving properties
  13398. (defconst org-special-properties
  13399. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13400. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13401. "The special properties valid in Org-mode.
  13402. These are properties that are not defined in the property drawer,
  13403. but in some other way.")
  13404. (defconst org-default-properties
  13405. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13406. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13407. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13408. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13409. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13410. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13411. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13412. "Some properties that are used by Org-mode for various purposes.
  13413. Being in this list makes sure that they are offered for completion.")
  13414. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13415. "Regular expression matching the first line of a property drawer.")
  13416. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13417. "Regular expression matching the last line of a property drawer.")
  13418. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13419. "Regular expression matching the first line of a property drawer.")
  13420. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13421. "Regular expression matching the first line of a property drawer.")
  13422. (defconst org-property-drawer-re
  13423. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13424. org-property-end-re "\\)\n?")
  13425. "Matches an entire property drawer.")
  13426. (defconst org-clock-drawer-re
  13427. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13428. org-property-end-re "\\)\n?")
  13429. "Matches an entire clock drawer.")
  13430. (defsubst org-re-property (property)
  13431. "Return a regexp matching a PROPERTY line.
  13432. Match group 1 will be set to the value."
  13433. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13434. (defsubst org-re-property-keyword (property)
  13435. "Return a regexp matching a PROPERTY line, possibly with no
  13436. value for the property."
  13437. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13438. (defun org-property-action ()
  13439. "Do an action on properties."
  13440. (interactive)
  13441. (let (c)
  13442. (org-at-property-p)
  13443. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13444. (setq c (read-char-exclusive))
  13445. (cond
  13446. ((equal c ?s)
  13447. (call-interactively 'org-set-property))
  13448. ((equal c ?d)
  13449. (call-interactively 'org-delete-property))
  13450. ((equal c ?D)
  13451. (call-interactively 'org-delete-property-globally))
  13452. ((equal c ?c)
  13453. (call-interactively 'org-compute-property-at-point))
  13454. (t (error "No such property action %c" c)))))
  13455. (defun org-inc-effort ()
  13456. "Increment the value of the effort property in the current entry."
  13457. (interactive)
  13458. (org-set-effort nil t))
  13459. (defvar org-clock-effort) ;; Defined in org-clock.el
  13460. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13461. (defun org-set-effort (&optional value increment)
  13462. "Set the effort property of the current entry.
  13463. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13464. 10th allowed value.
  13465. When INCREMENT is non-nil, set the property to the next allowed value."
  13466. (interactive "P")
  13467. (if (equal value 0) (setq value 10))
  13468. (let* ((completion-ignore-case t)
  13469. (prop org-effort-property)
  13470. (cur (org-entry-get nil prop))
  13471. (allowed (org-property-get-allowed-values nil prop 'table))
  13472. (existing (mapcar 'list (org-property-values prop)))
  13473. (heading (nth 4 (org-heading-components)))
  13474. rpl
  13475. (val (cond
  13476. ((stringp value) value)
  13477. ((and allowed (integerp value))
  13478. (or (car (nth (1- value) allowed))
  13479. (car (org-last allowed))))
  13480. ((and allowed increment)
  13481. (or (caadr (member (list cur) allowed))
  13482. (error "Allowed effort values are not set")))
  13483. (allowed
  13484. (message "Select 1-9,0, [RET%s]: %s"
  13485. (if cur (concat "=" cur) "")
  13486. (mapconcat 'car allowed " "))
  13487. (setq rpl (read-char-exclusive))
  13488. (if (equal rpl ?\r)
  13489. cur
  13490. (setq rpl (- rpl ?0))
  13491. (if (equal rpl 0) (setq rpl 10))
  13492. (if (and (> rpl 0) (<= rpl (length allowed)))
  13493. (car (nth (1- rpl) allowed))
  13494. (org-completing-read "Effort: " allowed nil))))
  13495. (t
  13496. (let (org-completion-use-ido org-completion-use-iswitchb)
  13497. (org-completing-read
  13498. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13499. (concat "[" cur "]") "")
  13500. ": ")
  13501. existing nil nil "" nil cur))))))
  13502. (unless (equal (org-entry-get nil prop) val)
  13503. (org-entry-put nil prop val))
  13504. (save-excursion
  13505. (org-back-to-heading t)
  13506. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13507. (when (string= heading org-clock-current-task)
  13508. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13509. (org-clock-update-mode-line))
  13510. (message "%s is now %s" prop val)))
  13511. (defun org-at-property-p ()
  13512. "Is cursor inside a property drawer?"
  13513. (save-excursion
  13514. (beginning-of-line 1)
  13515. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13516. (save-match-data ;; Used by calling procedures
  13517. (let ((p (point))
  13518. (range (unless (org-before-first-heading-p)
  13519. (org-get-property-block))))
  13520. (and range (<= (car range) p) (< p (cdr range))))))))
  13521. (defun org-get-property-block (&optional beg end force)
  13522. "Return the (beg . end) range of the body of the property drawer.
  13523. BEG and END are the beginning and end of the current subtree, or of
  13524. the part before the first headline. If they are not given, they will
  13525. be found. If the drawer does not exist and FORCE is non-nil, create
  13526. the drawer."
  13527. (catch 'exit
  13528. (save-excursion
  13529. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13530. (progn (org-back-to-heading t) (point))))
  13531. (end (or end (and (not (outline-next-heading)) (point-max))
  13532. (point))))
  13533. (goto-char beg)
  13534. (if (re-search-forward org-property-start-re end t)
  13535. (setq beg (1+ (match-end 0)))
  13536. (if force
  13537. (save-excursion
  13538. (org-insert-property-drawer)
  13539. (setq end (progn (outline-next-heading) (point))))
  13540. (throw 'exit nil))
  13541. (goto-char beg)
  13542. (if (re-search-forward org-property-start-re end t)
  13543. (setq beg (1+ (match-end 0)))))
  13544. (if (re-search-forward org-property-end-re end t)
  13545. (setq end (match-beginning 0))
  13546. (or force (throw 'exit nil))
  13547. (goto-char beg)
  13548. (setq end beg)
  13549. (org-indent-line)
  13550. (insert ":END:\n"))
  13551. (cons beg end)))))
  13552. (defun org-entry-properties (&optional pom which specific)
  13553. "Get all properties of the entry at point-or-marker POM.
  13554. This includes the TODO keyword, the tags, time strings for deadline,
  13555. scheduled, and clocking, and any additional properties defined in the
  13556. entry. The return value is an alist, keys may occur multiple times
  13557. if the property key was used several times.
  13558. POM may also be nil, in which case the current entry is used.
  13559. If WHICH is nil or `all', get all properties. If WHICH is
  13560. `special' or `standard', only get that subclass. If WHICH
  13561. is a string only get exactly this property. SPECIFIC can be a string, the
  13562. specific property we are interested in. Specifying it can speed
  13563. things up because then unnecessary parsing is avoided."
  13564. (setq which (or which 'all))
  13565. (org-with-point-at pom
  13566. (let ((clockstr (substring org-clock-string 0 -1))
  13567. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13568. (case-fold-search nil)
  13569. beg end range props sum-props key key1 value string clocksum clocksumt)
  13570. (save-excursion
  13571. (when (condition-case nil
  13572. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13573. (error nil))
  13574. (setq beg (point))
  13575. (setq sum-props (get-text-property (point) 'org-summaries))
  13576. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13577. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13578. (outline-next-heading)
  13579. (setq end (point))
  13580. (when (memq which '(all special))
  13581. ;; Get the special properties, like TODO and tags
  13582. (goto-char beg)
  13583. (when (and (or (not specific) (string= specific "TODO"))
  13584. (looking-at org-todo-line-regexp) (match-end 2))
  13585. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13586. (when (and (or (not specific) (string= specific "PRIORITY"))
  13587. (looking-at org-priority-regexp))
  13588. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13589. (when (or (not specific) (string= specific "FILE"))
  13590. (push (cons "FILE" buffer-file-name) props))
  13591. (when (and (or (not specific) (string= specific "TAGS"))
  13592. (setq value (org-get-tags-string))
  13593. (string-match "\\S-" value))
  13594. (push (cons "TAGS" value) props))
  13595. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13596. (setq value (org-get-tags-at)))
  13597. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13598. ":"))
  13599. props))
  13600. (when (or (not specific) (string= specific "BLOCKED"))
  13601. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13602. (when (or (not specific)
  13603. (member specific
  13604. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13605. "TIMESTAMP" "TIMESTAMP_IA")))
  13606. (catch 'match
  13607. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13608. (setq key (if (match-end 1)
  13609. (substring (org-match-string-no-properties 1)
  13610. 0 -1))
  13611. string (if (equal key clockstr)
  13612. (org-trim
  13613. (buffer-substring-no-properties
  13614. (match-beginning 3) (goto-char
  13615. (point-at-eol))))
  13616. (substring (org-match-string-no-properties 3)
  13617. 1 -1)))
  13618. ;; Get the correct property name from the key. This is
  13619. ;; necessary if the user has configured time keywords.
  13620. (setq key1 (concat key ":"))
  13621. (cond
  13622. ((not key)
  13623. (setq key
  13624. (if (= (char-after (match-beginning 3)) ?\[)
  13625. "TIMESTAMP_IA" "TIMESTAMP")))
  13626. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13627. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13628. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13629. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13630. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13631. (progn
  13632. (push (cons key string) props)
  13633. ;; no need to search further if match is found
  13634. (throw 'match t))
  13635. (when (or (equal key "CLOCK") (not (assoc key props)))
  13636. (push (cons key string) props)))))))
  13637. (when (memq which '(all standard))
  13638. ;; Get the standard properties, like :PROP: ...
  13639. (setq range (org-get-property-block beg end))
  13640. (when range
  13641. (goto-char (car range))
  13642. (while (re-search-forward
  13643. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13644. (cdr range) t)
  13645. (setq key (org-match-string-no-properties 1)
  13646. value (org-trim (or (org-match-string-no-properties 2) "")))
  13647. (unless (member key excluded)
  13648. (push (cons key (or value "")) props)))))
  13649. (if clocksum
  13650. (push (cons "CLOCKSUM"
  13651. (org-columns-number-to-string (/ (float clocksum) 60.)
  13652. 'add_times))
  13653. props))
  13654. (if clocksumt
  13655. (push (cons "CLOCKSUM_T"
  13656. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13657. 'add_times))
  13658. props))
  13659. (unless (assoc "CATEGORY" props)
  13660. (push (cons "CATEGORY" (org-get-category)) props))
  13661. (append sum-props (nreverse props)))))))
  13662. (defun org-entry-get (pom property &optional inherit literal-nil)
  13663. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13664. If INHERIT is non-nil and the entry does not have the property,
  13665. then also check higher levels of the hierarchy.
  13666. If INHERIT is the symbol `selective', use inheritance only if the setting
  13667. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13668. If the property is present but empty, the return value is the empty string.
  13669. If the property is not present at all, nil is returned.
  13670. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13671. do not interpret it as the list atom nil. This is used for inheritance
  13672. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13673. (org-with-point-at pom
  13674. (if (and inherit (if (eq inherit 'selective)
  13675. (org-property-inherit-p property)
  13676. t))
  13677. (org-entry-get-with-inheritance property literal-nil)
  13678. (if (member property org-special-properties)
  13679. ;; We need a special property. Use `org-entry-properties' to
  13680. ;; retrieve it, but specify the wanted property
  13681. (cdr (assoc property (org-entry-properties nil 'special property)))
  13682. (let ((range (org-get-property-block)))
  13683. (when (and range (not (eq (car range) (cdr range))))
  13684. (let* ((props (list (or (assoc property org-file-properties)
  13685. (assoc property org-global-properties)
  13686. (assoc property org-global-properties-fixed))))
  13687. (ap (lambda (key)
  13688. (when (re-search-forward
  13689. (org-re-property key) (cdr range) t)
  13690. (setq props
  13691. (org-update-property-plist
  13692. key
  13693. (if (match-end 1)
  13694. (org-match-string-no-properties 1) "")
  13695. props)))))
  13696. val)
  13697. (goto-char (car range))
  13698. (funcall ap property)
  13699. (goto-char (car range))
  13700. (while (funcall ap (concat property "+")))
  13701. (setq val (cdr (assoc property props)))
  13702. (when val (if literal-nil val (org-not-nil val))))))))))
  13703. (defun org-property-or-variable-value (var &optional inherit)
  13704. "Check if there is a property fixing the value of VAR.
  13705. If yes, return this value. If not, return the current value of the variable."
  13706. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13707. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13708. (read prop)
  13709. (symbol-value var))))
  13710. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13711. "Delete the property PROPERTY from entry at point-or-marker POM.
  13712. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13713. an empty drawer to delete."
  13714. (org-with-point-at pom
  13715. (if (member property org-special-properties)
  13716. nil ; cannot delete these properties.
  13717. (let ((range (org-get-property-block)))
  13718. (if (and range
  13719. (goto-char (car range))
  13720. (re-search-forward
  13721. (org-re-property property)
  13722. (cdr range) t))
  13723. (progn
  13724. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13725. (and delete-empty-drawer
  13726. (org-remove-empty-drawer-at
  13727. delete-empty-drawer (car range)))
  13728. t)
  13729. nil)))))
  13730. ;; Multi-values properties are properties that contain multiple values
  13731. ;; These values are assumed to be single words, separated by whitespace.
  13732. (defun org-entry-add-to-multivalued-property (pom property value)
  13733. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13734. (let* ((old (org-entry-get pom property))
  13735. (values (and old (org-split-string old "[ \t]"))))
  13736. (setq value (org-entry-protect-space value))
  13737. (unless (member value values)
  13738. (setq values (cons value values))
  13739. (org-entry-put pom property
  13740. (mapconcat 'identity values " ")))))
  13741. (defun org-entry-remove-from-multivalued-property (pom property value)
  13742. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13743. (let* ((old (org-entry-get pom property))
  13744. (values (and old (org-split-string old "[ \t]"))))
  13745. (setq value (org-entry-protect-space value))
  13746. (when (member value values)
  13747. (setq values (delete value values))
  13748. (org-entry-put pom property
  13749. (mapconcat 'identity values " ")))))
  13750. (defun org-entry-member-in-multivalued-property (pom property value)
  13751. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13752. (let* ((old (org-entry-get pom property))
  13753. (values (and old (org-split-string old "[ \t]"))))
  13754. (setq value (org-entry-protect-space value))
  13755. (member value values)))
  13756. (defun org-entry-get-multivalued-property (pom property)
  13757. "Return a list of values in a multivalued property."
  13758. (let* ((value (org-entry-get pom property))
  13759. (values (and value (org-split-string value "[ \t]"))))
  13760. (mapcar 'org-entry-restore-space values)))
  13761. (defun org-entry-put-multivalued-property (pom property &rest values)
  13762. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13763. VALUES should be a list of strings. Spaces will be protected."
  13764. (org-entry-put pom property
  13765. (mapconcat 'org-entry-protect-space values " "))
  13766. (let* ((value (org-entry-get pom property))
  13767. (values (and value (org-split-string value "[ \t]"))))
  13768. (mapcar 'org-entry-restore-space values)))
  13769. (defun org-entry-protect-space (s)
  13770. "Protect spaces and newline in string S."
  13771. (while (string-match " " s)
  13772. (setq s (replace-match "%20" t t s)))
  13773. (while (string-match "\n" s)
  13774. (setq s (replace-match "%0A" t t s)))
  13775. s)
  13776. (defun org-entry-restore-space (s)
  13777. "Restore spaces and newline in string S."
  13778. (while (string-match "%20" s)
  13779. (setq s (replace-match " " t t s)))
  13780. (while (string-match "%0A" s)
  13781. (setq s (replace-match "\n" t t s)))
  13782. s)
  13783. (defvar org-entry-property-inherited-from (make-marker)
  13784. "Marker pointing to the entry from where a property was inherited.
  13785. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13786. location of the entry where the inheritance search matched. If there was
  13787. no match, the marker will point nowhere.
  13788. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13789. is set.")
  13790. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13791. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13792. The search will stop at the first ancestor which has the property defined.
  13793. If the value found is \"nil\", return nil to show that the property
  13794. should be considered as undefined (this is the meaning of nil here).
  13795. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13796. (move-marker org-entry-property-inherited-from nil)
  13797. (let (tmp)
  13798. (save-excursion
  13799. (save-restriction
  13800. (widen)
  13801. (catch 'ex
  13802. (while t
  13803. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13804. (or (ignore-errors (org-back-to-heading t))
  13805. (goto-char (point-min)))
  13806. (move-marker org-entry-property-inherited-from (point))
  13807. (throw 'ex tmp))
  13808. (or (ignore-errors (org-up-heading-safe))
  13809. (throw 'ex nil))))))
  13810. (setq tmp (or tmp
  13811. (cdr (assoc property org-file-properties))
  13812. (cdr (assoc property org-global-properties))
  13813. (cdr (assoc property org-global-properties-fixed))))
  13814. (if literal-nil tmp (org-not-nil tmp))))
  13815. (defvar org-property-changed-functions nil
  13816. "Hook called when the value of a property has changed.
  13817. Each hook function should accept two arguments, the name of the property
  13818. and the new value.")
  13819. (defun org-entry-put (pom property value)
  13820. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13821. (org-with-point-at pom
  13822. (org-back-to-heading t)
  13823. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13824. range)
  13825. (cond
  13826. ((equal property "TODO")
  13827. (when (and (stringp value) (string-match "\\S-" value)
  13828. (not (member value org-todo-keywords-1)))
  13829. (error "\"%s\" is not a valid TODO state" value))
  13830. (if (or (not value)
  13831. (not (string-match "\\S-" value)))
  13832. (setq value 'none))
  13833. (org-todo value)
  13834. (org-set-tags nil 'align))
  13835. ((equal property "PRIORITY")
  13836. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13837. (string-to-char value) ?\ ))
  13838. (org-set-tags nil 'align))
  13839. ((equal property "CLOCKSUM")
  13840. (if (not (re-search-forward
  13841. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13842. (error "Cannot find a clock log")
  13843. (goto-char (- (match-end 1) 2))
  13844. (cond
  13845. ((eq value 'earlier) (org-timestamp-down))
  13846. ((eq value 'later) (org-timestamp-up)))
  13847. (org-clock-sum-current-item)))
  13848. ((equal property "SCHEDULED")
  13849. (if (re-search-forward org-scheduled-time-regexp end t)
  13850. (cond
  13851. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13852. ((eq value 'later) (org-timestamp-change 1 'day))
  13853. (t (call-interactively 'org-schedule)))
  13854. (call-interactively 'org-schedule)))
  13855. ((equal property "DEADLINE")
  13856. (if (re-search-forward org-deadline-time-regexp end t)
  13857. (cond
  13858. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13859. ((eq value 'later) (org-timestamp-change 1 'day))
  13860. (t (call-interactively 'org-deadline)))
  13861. (call-interactively 'org-deadline)))
  13862. ((member property org-special-properties)
  13863. (error "The %s property can not yet be set with `org-entry-put'"
  13864. property))
  13865. (t ; a non-special property
  13866. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13867. (setq range (org-get-property-block beg end 'force))
  13868. (goto-char (car range))
  13869. (if (re-search-forward
  13870. (org-re-property-keyword property) (cdr range) t)
  13871. (progn
  13872. (delete-region (match-beginning 0) (match-end 0))
  13873. (goto-char (match-beginning 0)))
  13874. (goto-char (cdr range))
  13875. (insert "\n")
  13876. (backward-char 1)
  13877. (org-indent-line))
  13878. (insert ":" property ":")
  13879. (and value (insert " " value))
  13880. (org-indent-line)))))
  13881. (run-hook-with-args 'org-property-changed-functions property value)))
  13882. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13883. "Get all property keys in the current buffer.
  13884. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13885. like tags and TODO state.
  13886. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13887. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13888. and others.
  13889. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13890. formats in the current buffer."
  13891. (let (rtn range cfmt s p)
  13892. (save-excursion
  13893. (save-restriction
  13894. (widen)
  13895. (goto-char (point-min))
  13896. (while (re-search-forward org-property-start-re nil t)
  13897. (setq range (org-get-property-block))
  13898. (goto-char (car range))
  13899. (while (re-search-forward
  13900. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13901. (cdr range) t)
  13902. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13903. (outline-next-heading))))
  13904. (when include-specials
  13905. (setq rtn (append org-special-properties rtn)))
  13906. (when include-defaults
  13907. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13908. (add-to-list 'rtn org-effort-property))
  13909. (when include-columns
  13910. (save-excursion
  13911. (save-restriction
  13912. (widen)
  13913. (goto-char (point-min))
  13914. (while (re-search-forward
  13915. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13916. nil t)
  13917. (setq cfmt (match-string 2) s 0)
  13918. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13919. cfmt s)
  13920. (setq s (match-end 0)
  13921. p (match-string 1 cfmt))
  13922. (unless (or (equal p "ITEM")
  13923. (member p org-special-properties))
  13924. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13925. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13926. (defun org-property-values (key)
  13927. "Return a list of all values of property KEY in the current buffer."
  13928. (save-excursion
  13929. (save-restriction
  13930. (widen)
  13931. (goto-char (point-min))
  13932. (let ((re (org-re-property key))
  13933. values)
  13934. (while (re-search-forward re nil t)
  13935. (add-to-list 'values (org-trim (match-string 1))))
  13936. (delete "" values)))))
  13937. (defun org-insert-property-drawer ()
  13938. "Insert a property drawer into the current entry."
  13939. (org-back-to-heading t)
  13940. (looking-at org-outline-regexp)
  13941. (let ((indent (if org-adapt-indentation
  13942. (- (match-end 0) (match-beginning 0))
  13943. 0))
  13944. (beg (point))
  13945. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13946. end hiddenp)
  13947. (outline-next-heading)
  13948. (setq end (point))
  13949. (goto-char beg)
  13950. (while (re-search-forward re end t))
  13951. (setq hiddenp (outline-invisible-p))
  13952. (end-of-line 1)
  13953. (and (equal (char-after) ?\n) (forward-char 1))
  13954. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13955. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13956. ;; just skip this line
  13957. (beginning-of-line 2)
  13958. ;; Drawer start, find the end
  13959. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13960. (beginning-of-line 1)))
  13961. (org-skip-over-state-notes)
  13962. (skip-chars-backward " \t\n\r")
  13963. (if (eq (char-before) ?*) (forward-char 1))
  13964. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13965. (beginning-of-line 0)
  13966. (org-indent-to-column indent)
  13967. (beginning-of-line 2)
  13968. (org-indent-to-column indent)
  13969. (beginning-of-line 0)
  13970. (if hiddenp
  13971. (save-excursion
  13972. (org-back-to-heading t)
  13973. (hide-entry))
  13974. (org-flag-drawer t))))
  13975. (defun org-insert-drawer (&optional arg drawer)
  13976. "Insert a drawer at point.
  13977. Optional argument DRAWER, when non-nil, is a string representing
  13978. drawer's name. Otherwise, the user is prompted for a name.
  13979. If a region is active, insert the drawer around that region
  13980. instead.
  13981. Point is left between drawer's boundaries."
  13982. (interactive "P")
  13983. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13984. "LOGBOOK"))
  13985. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13986. ;; internally by Org. They are meant to be inserted
  13987. ;; automatically.
  13988. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13989. ;; Remove system drawers from list. Note: For some reason,
  13990. ;; `org-completing-read' ignores the predicate while
  13991. ;; `completing-read' handles it fine.
  13992. (drawer (if arg "PROPERTIES"
  13993. (or drawer
  13994. (completing-read
  13995. "Drawer: " org-drawers
  13996. (lambda (d) (not (member d system-drawers))))))))
  13997. (cond
  13998. ;; With C-u, fall back on `org-insert-property-drawer'
  13999. (arg (org-insert-property-drawer))
  14000. ;; With an active region, insert a drawer at point.
  14001. ((not (org-region-active-p))
  14002. (progn
  14003. (unless (bolp) (insert "\n"))
  14004. (insert (format ":%s:\n\n:END:\n" drawer))
  14005. (forward-line -2)))
  14006. ;; Otherwise, insert the drawer at point
  14007. (t
  14008. (let ((rbeg (region-beginning))
  14009. (rend (copy-marker (region-end))))
  14010. (unwind-protect
  14011. (progn
  14012. (goto-char rbeg)
  14013. (beginning-of-line)
  14014. (when (save-excursion
  14015. (re-search-forward org-outline-regexp-bol rend t))
  14016. (error "Drawers cannot contain headlines"))
  14017. ;; Position point at the beginning of the first
  14018. ;; non-blank line in region. Insert drawer's opening
  14019. ;; there, then indent it.
  14020. (org-skip-whitespace)
  14021. (beginning-of-line)
  14022. (insert ":" drawer ":\n")
  14023. (forward-line -1)
  14024. (indent-for-tab-command)
  14025. ;; Move point to the beginning of the first blank line
  14026. ;; after the last non-blank line in region. Insert
  14027. ;; drawer's closing, then indent it.
  14028. (goto-char rend)
  14029. (skip-chars-backward " \r\t\n")
  14030. (insert "\n:END:")
  14031. (deactivate-mark t)
  14032. (indent-for-tab-command)
  14033. (unless (eolp) (insert "\n")))
  14034. ;; Clear marker, whatever the outcome of insertion is.
  14035. (set-marker rend nil)))))))
  14036. (defvar org-property-set-functions-alist nil
  14037. "Property set function alist.
  14038. Each entry should have the following format:
  14039. (PROPERTY . READ-FUNCTION)
  14040. The read function will be called with the same argument as
  14041. `org-completing-read'.")
  14042. (defun org-set-property-function (property)
  14043. "Get the function that should be used to set PROPERTY.
  14044. This is computed according to `org-property-set-functions-alist'."
  14045. (or (cdr (assoc property org-property-set-functions-alist))
  14046. 'org-completing-read))
  14047. (defun org-read-property-value (property)
  14048. "Read PROPERTY value from user."
  14049. (let* ((completion-ignore-case t)
  14050. (allowed (org-property-get-allowed-values nil property 'table))
  14051. (cur (org-entry-get nil property))
  14052. (prompt (concat property " value"
  14053. (if (and cur (string-match "\\S-" cur))
  14054. (concat " [" cur "]") "") ": "))
  14055. (set-function (org-set-property-function property))
  14056. (val (if allowed
  14057. (funcall set-function prompt allowed nil
  14058. (not (get-text-property 0 'org-unrestricted
  14059. (caar allowed))))
  14060. (let (org-completion-use-ido org-completion-use-iswitchb)
  14061. (funcall set-function prompt
  14062. (mapcar 'list (org-property-values property))
  14063. nil nil "" nil cur)))))
  14064. (if (equal val "")
  14065. cur
  14066. val)))
  14067. (defvar org-last-set-property nil)
  14068. (defvar org-last-set-property-value nil)
  14069. (defun org-read-property-name ()
  14070. "Read a property name."
  14071. (let* ((completion-ignore-case t)
  14072. (keys (org-buffer-property-keys nil t t))
  14073. (default-prop (or (save-excursion
  14074. (save-match-data
  14075. (beginning-of-line)
  14076. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14077. (null (string= (match-string 1) "END"))
  14078. (match-string 1))))
  14079. org-last-set-property))
  14080. (property (org-icompleting-read
  14081. (concat "Property"
  14082. (if default-prop (concat " [" default-prop "]") "")
  14083. ": ")
  14084. (mapcar 'list keys)
  14085. nil nil nil nil
  14086. default-prop)))
  14087. (if (member property keys)
  14088. property
  14089. (or (cdr (assoc (downcase property)
  14090. (mapcar (lambda (x) (cons (downcase x) x))
  14091. keys)))
  14092. property))))
  14093. (defun org-set-property-and-value (use-last)
  14094. "Allow to set [PROPERTY]: [value] direction from prompt.
  14095. When use-default, don't even ask, just use the last
  14096. \"[PROPERTY]: [value]\" string from the history."
  14097. (interactive "P")
  14098. (let* ((completion-ignore-case t)
  14099. (pv (or (and use-last org-last-set-property-value)
  14100. (org-completing-read
  14101. "Enter a \"[Property]: [value]\" pair: "
  14102. nil nil nil nil nil
  14103. org-last-set-property-value)))
  14104. prop val)
  14105. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14106. (setq prop (match-string 1 pv)
  14107. val (match-string 2 pv))
  14108. (org-set-property prop val))))
  14109. (defun org-set-property (property value)
  14110. "In the current entry, set PROPERTY to VALUE.
  14111. When called interactively, this will prompt for a property name, offering
  14112. completion on existing and default properties. And then it will prompt
  14113. for a value, offering completion either on allowed values (via an inherited
  14114. xxx_ALL property) or on existing values in other instances of this property
  14115. in the current file."
  14116. (interactive (list nil nil))
  14117. (let* ((property (or property (org-read-property-name)))
  14118. (value (or value (org-read-property-value property)))
  14119. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14120. (setq org-last-set-property property)
  14121. (setq org-last-set-property-value (concat property ": " value))
  14122. ;; Possibly postprocess the inserted value:
  14123. (when fn (setq value (funcall fn value)))
  14124. (unless (equal (org-entry-get nil property) value)
  14125. (org-entry-put nil property value))))
  14126. (defun org-delete-property (property &optional delete-empty-drawer)
  14127. "In the current entry, delete PROPERTY.
  14128. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14129. an empty drawer to delete."
  14130. (interactive
  14131. (let* ((completion-ignore-case t)
  14132. (prop (org-icompleting-read "Property: "
  14133. (org-entry-properties nil 'standard))))
  14134. (list prop)))
  14135. (message "Property %s %s" property
  14136. (if (org-entry-delete nil property delete-empty-drawer)
  14137. "deleted"
  14138. "was not present in the entry")))
  14139. (defun org-delete-property-globally (property)
  14140. "Remove PROPERTY globally, from all entries."
  14141. (interactive
  14142. (let* ((completion-ignore-case t)
  14143. (prop (org-icompleting-read
  14144. "Globally remove property: "
  14145. (mapcar 'list (org-buffer-property-keys)))))
  14146. (list prop)))
  14147. (save-excursion
  14148. (save-restriction
  14149. (widen)
  14150. (goto-char (point-min))
  14151. (let ((cnt 0))
  14152. (while (re-search-forward
  14153. (org-re-property property)
  14154. nil t)
  14155. (setq cnt (1+ cnt))
  14156. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14157. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14158. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14159. (defun org-compute-property-at-point ()
  14160. "Compute the property at point.
  14161. This looks for an enclosing column format, extracts the operator and
  14162. then applies it to the property in the column format's scope."
  14163. (interactive)
  14164. (unless (org-at-property-p)
  14165. (error "Not at a property"))
  14166. (let ((prop (org-match-string-no-properties 2)))
  14167. (org-columns-get-format-and-top-level)
  14168. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14169. (error "No operator defined for property %s" prop))
  14170. (org-columns-compute prop)))
  14171. (defvar org-property-allowed-value-functions nil
  14172. "Hook for functions supplying allowed values for a specific property.
  14173. The functions must take a single argument, the name of the property, and
  14174. return a flat list of allowed values. If \":ETC\" is one of
  14175. the values, this means that these values are intended as defaults for
  14176. completion, but that other values should be allowed too.
  14177. The functions must return nil if they are not responsible for this
  14178. property.")
  14179. (defun org-property-get-allowed-values (pom property &optional table)
  14180. "Get allowed values for the property PROPERTY.
  14181. When TABLE is non-nil, return an alist that can directly be used for
  14182. completion."
  14183. (let (vals)
  14184. (cond
  14185. ((equal property "TODO")
  14186. (setq vals (org-with-point-at pom
  14187. (append org-todo-keywords-1 '("")))))
  14188. ((equal property "PRIORITY")
  14189. (let ((n org-lowest-priority))
  14190. (while (>= n org-highest-priority)
  14191. (push (char-to-string n) vals)
  14192. (setq n (1- n)))))
  14193. ((member property org-special-properties))
  14194. ((setq vals (run-hook-with-args-until-success
  14195. 'org-property-allowed-value-functions property)))
  14196. (t
  14197. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14198. (when (and vals (string-match "\\S-" vals))
  14199. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14200. (setq vals (mapcar (lambda (x)
  14201. (cond ((stringp x) x)
  14202. ((numberp x) (number-to-string x))
  14203. ((symbolp x) (symbol-name x))
  14204. (t "???")))
  14205. vals)))))
  14206. (when (member ":ETC" vals)
  14207. (setq vals (remove ":ETC" vals))
  14208. (org-add-props (car vals) '(org-unrestricted t)))
  14209. (if table (mapcar 'list vals) vals)))
  14210. (defun org-property-previous-allowed-value (&optional previous)
  14211. "Switch to the next allowed value for this property."
  14212. (interactive)
  14213. (org-property-next-allowed-value t))
  14214. (defun org-property-next-allowed-value (&optional previous)
  14215. "Switch to the next allowed value for this property."
  14216. (interactive)
  14217. (unless (org-at-property-p)
  14218. (error "Not at a property"))
  14219. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14220. (key (match-string 2))
  14221. (value (match-string 3))
  14222. (allowed (or (org-property-get-allowed-values (point) key)
  14223. (and (member value '("[ ]" "[-]" "[X]"))
  14224. '("[ ]" "[X]"))))
  14225. (heading (save-match-data (nth 4 (org-heading-components))))
  14226. nval)
  14227. (unless allowed
  14228. (error "Allowed values for this property have not been defined"))
  14229. (if previous (setq allowed (reverse allowed)))
  14230. (if (member value allowed)
  14231. (setq nval (car (cdr (member value allowed)))))
  14232. (setq nval (or nval (car allowed)))
  14233. (if (equal nval value)
  14234. (error "Only one allowed value for this property"))
  14235. (org-at-property-p)
  14236. (replace-match (concat " :" key ": " nval) t t)
  14237. (org-indent-line)
  14238. (beginning-of-line 1)
  14239. (skip-chars-forward " \t")
  14240. (when (equal prop org-effort-property)
  14241. (save-excursion
  14242. (org-back-to-heading t)
  14243. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14244. (when (string= org-clock-current-task heading)
  14245. (setq org-clock-effort nval)
  14246. (org-clock-update-mode-line)))
  14247. (run-hook-with-args 'org-property-changed-functions key nval)))
  14248. (defun org-find-olp (path &optional this-buffer)
  14249. "Return a marker pointing to the entry at outline path OLP.
  14250. If anything goes wrong, throw an error.
  14251. You can wrap this call to catch the error like this:
  14252. (condition-case msg
  14253. (org-mobile-locate-entry (match-string 4))
  14254. (error (nth 1 msg)))
  14255. The return value will then be either a string with the error message,
  14256. or a marker if everything is OK.
  14257. If THIS-BUFFER is set, the outline path does not contain a file,
  14258. only headings."
  14259. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14260. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14261. (level 1)
  14262. (lmin 1)
  14263. (lmax 1)
  14264. limit re end found pos heading cnt flevel)
  14265. (unless buffer (error "File not found :%s" file))
  14266. (with-current-buffer buffer
  14267. (save-excursion
  14268. (save-restriction
  14269. (widen)
  14270. (setq limit (point-max))
  14271. (goto-char (point-min))
  14272. (while (setq heading (pop path))
  14273. (setq re (format org-complex-heading-regexp-format
  14274. (regexp-quote heading)))
  14275. (setq cnt 0 pos (point))
  14276. (while (re-search-forward re end t)
  14277. (setq level (- (match-end 1) (match-beginning 1)))
  14278. (if (and (>= level lmin) (<= level lmax))
  14279. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14280. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14281. lmax heading))
  14282. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14283. lmax heading))
  14284. (goto-char found)
  14285. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14286. (setq end (save-excursion (org-end-of-subtree t t))))
  14287. (when (org-at-heading-p)
  14288. (point-marker)))))))
  14289. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14290. "Find node HEADING in BUFFER.
  14291. Return a marker to the heading if it was found, or nil if not.
  14292. If POS-ONLY is set, return just the position instead of a marker.
  14293. The heading text must match exact, but it may have a TODO keyword,
  14294. a priority cookie and tags in the standard locations."
  14295. (with-current-buffer (or buffer (current-buffer))
  14296. (save-excursion
  14297. (save-restriction
  14298. (widen)
  14299. (goto-char (point-min))
  14300. (let (case-fold-search)
  14301. (if (re-search-forward
  14302. (format org-complex-heading-regexp-format
  14303. (regexp-quote heading)) nil t)
  14304. (if pos-only
  14305. (match-beginning 0)
  14306. (move-marker (make-marker) (match-beginning 0)))))))))
  14307. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14308. "Find Org node headline HEADING in all .org files in directory DIR.
  14309. When the target headline is found, return a marker to this location."
  14310. (let ((files (directory-files (or dir default-directory)
  14311. nil "\\`[^.#].*\\.org\\'"))
  14312. file visiting m buffer)
  14313. (catch 'found
  14314. (while (setq file (pop files))
  14315. (message "trying %s" file)
  14316. (setq visiting (org-find-base-buffer-visiting file))
  14317. (setq buffer (or visiting (find-file-noselect file)))
  14318. (setq m (org-find-exact-headline-in-buffer
  14319. heading buffer))
  14320. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14321. (and m (throw 'found m))))))
  14322. (defun org-find-entry-with-id (ident)
  14323. "Locate the entry that contains the ID property with exact value IDENT.
  14324. IDENT can be a string, a symbol or a number, this function will search for
  14325. the string representation of it.
  14326. Return the position where this entry starts, or nil if there is no such entry."
  14327. (interactive "sID: ")
  14328. (let ((id (cond
  14329. ((stringp ident) ident)
  14330. ((symbol-name ident) (symbol-name ident))
  14331. ((numberp ident) (number-to-string ident))
  14332. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14333. (case-fold-search nil))
  14334. (save-excursion
  14335. (save-restriction
  14336. (widen)
  14337. (goto-char (point-min))
  14338. (when (re-search-forward
  14339. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14340. nil t)
  14341. (org-back-to-heading t)
  14342. (point))))))
  14343. ;;;; Timestamps
  14344. (defvar org-last-changed-timestamp nil)
  14345. (defvar org-last-inserted-timestamp nil
  14346. "The last time stamp inserted with `org-insert-time-stamp'.")
  14347. (defvar org-time-was-given) ; dynamically scoped parameter
  14348. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14349. (defvar org-ts-what) ; dynamically scoped parameter
  14350. (defun org-time-stamp (arg &optional inactive)
  14351. "Prompt for a date/time and insert a time stamp.
  14352. If the user specifies a time like HH:MM or if this command is
  14353. called with at least one prefix argument, the time stamp contains
  14354. the date and the time. Otherwise, only the date is be included.
  14355. All parts of a date not specified by the user is filled in from
  14356. the current date/time. So if you just press return without
  14357. typing anything, the time stamp will represent the current
  14358. date/time.
  14359. If there is already a timestamp at the cursor, it will be
  14360. modified.
  14361. With two universal prefix arguments, insert an active timestamp
  14362. with the current time without prompting the user."
  14363. (interactive "P")
  14364. (let* ((ts nil)
  14365. (default-time
  14366. ;; Default time is either today, or, when entering a range,
  14367. ;; the range start.
  14368. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14369. (save-excursion
  14370. (re-search-backward
  14371. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14372. (- (point) 20) t)))
  14373. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14374. (current-time)))
  14375. (default-input (and ts (org-get-compact-tod ts)))
  14376. (repeater (save-excursion
  14377. (save-match-data
  14378. (beginning-of-line)
  14379. (when (re-search-forward
  14380. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14381. (save-excursion (progn (end-of-line) (point))) t)
  14382. (match-string 0)))))
  14383. org-time-was-given org-end-time-was-given time)
  14384. (cond
  14385. ((and (org-at-timestamp-p t)
  14386. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14387. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14388. (insert "--")
  14389. (setq time (let ((this-command this-command))
  14390. (org-read-date arg 'totime nil nil
  14391. default-time default-input inactive)))
  14392. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14393. ((org-at-timestamp-p t)
  14394. (setq time (let ((this-command this-command))
  14395. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14396. (when (org-at-timestamp-p t) ; just to get the match data
  14397. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14398. (replace-match "")
  14399. (setq org-last-changed-timestamp
  14400. (org-insert-time-stamp
  14401. time (or org-time-was-given arg)
  14402. inactive nil nil (list org-end-time-was-given)))
  14403. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14404. (setq org-last-changed-timestamp
  14405. (concat (substring org-last-inserted-timestamp 0 -1)
  14406. " " repeater ">"))))
  14407. (message "Timestamp updated"))
  14408. ((equal arg '(16))
  14409. (org-insert-time-stamp (current-time) t))
  14410. (t
  14411. (setq time (let ((this-command this-command))
  14412. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14413. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14414. nil nil (list org-end-time-was-given))))))
  14415. ;; FIXME: can we use this for something else, like computing time differences?
  14416. (defun org-get-compact-tod (s)
  14417. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14418. (let* ((t1 (match-string 1 s))
  14419. (h1 (string-to-number (match-string 2 s)))
  14420. (m1 (string-to-number (match-string 3 s)))
  14421. (t2 (and (match-end 4) (match-string 5 s)))
  14422. (h2 (and t2 (string-to-number (match-string 6 s))))
  14423. (m2 (and t2 (string-to-number (match-string 7 s))))
  14424. dh dm)
  14425. (if (not t2)
  14426. t1
  14427. (setq dh (- h2 h1) dm (- m2 m1))
  14428. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14429. (concat t1 "+" (number-to-string dh)
  14430. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14431. (defun org-time-stamp-inactive (&optional arg)
  14432. "Insert an inactive time stamp.
  14433. An inactive time stamp is enclosed in square brackets instead of angle
  14434. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14435. does not link to the calendar and cannot be changed with the S-cursor keys.
  14436. So these are more for recording a certain time/date."
  14437. (interactive "P")
  14438. (org-time-stamp arg 'inactive))
  14439. (defvar org-date-ovl (make-overlay 1 1))
  14440. (overlay-put org-date-ovl 'face 'org-date-selected)
  14441. (org-detach-overlay org-date-ovl)
  14442. (defvar org-ans1) ; dynamically scoped parameter
  14443. (defvar org-ans2) ; dynamically scoped parameter
  14444. (defvar org-plain-time-of-day-regexp) ; defined below
  14445. (defvar org-overriding-default-time nil) ; dynamically scoped
  14446. (defvar org-read-date-overlay nil)
  14447. (defvar org-dcst nil) ; dynamically scoped
  14448. (defvar org-read-date-history nil)
  14449. (defvar org-read-date-final-answer nil)
  14450. (defvar org-read-date-analyze-futurep nil)
  14451. (defvar org-read-date-analyze-forced-year nil)
  14452. (defvar org-read-date-inactive)
  14453. (defvar org-read-date-minibuffer-local-map
  14454. (let ((map (make-sparse-keymap)))
  14455. (set-keymap-parent map minibuffer-local-map)
  14456. (org-defkey map (kbd ".")
  14457. (lambda () (interactive)
  14458. (org-eval-in-calendar '(calendar-goto-today))))
  14459. (org-defkey map [(meta shift left)]
  14460. (lambda () (interactive)
  14461. (org-eval-in-calendar '(calendar-backward-month 1))))
  14462. (org-defkey map [(meta shift right)]
  14463. (lambda () (interactive)
  14464. (org-eval-in-calendar '(calendar-forward-month 1))))
  14465. (org-defkey map [(meta shift up)]
  14466. (lambda () (interactive)
  14467. (org-eval-in-calendar '(calendar-backward-year 1))))
  14468. (org-defkey map [(meta shift down)]
  14469. (lambda () (interactive)
  14470. (org-eval-in-calendar '(calendar-forward-year 1))))
  14471. (org-defkey map [?\e (shift left)]
  14472. (lambda () (interactive)
  14473. (org-eval-in-calendar '(calendar-backward-month 1))))
  14474. (org-defkey map [?\e (shift right)]
  14475. (lambda () (interactive)
  14476. (org-eval-in-calendar '(calendar-forward-month 1))))
  14477. (org-defkey map [?\e (shift up)]
  14478. (lambda () (interactive)
  14479. (org-eval-in-calendar '(calendar-backward-year 1))))
  14480. (org-defkey map [?\e (shift down)]
  14481. (lambda () (interactive)
  14482. (org-eval-in-calendar '(calendar-forward-year 1))))
  14483. (org-defkey map [(shift up)]
  14484. (lambda () (interactive)
  14485. (org-eval-in-calendar '(calendar-backward-week 1))))
  14486. (org-defkey map [(shift down)]
  14487. (lambda () (interactive)
  14488. (org-eval-in-calendar '(calendar-forward-week 1))))
  14489. (org-defkey map [(shift left)]
  14490. (lambda () (interactive)
  14491. (org-eval-in-calendar '(calendar-backward-day 1))))
  14492. (org-defkey map [(shift right)]
  14493. (lambda () (interactive)
  14494. (org-eval-in-calendar '(calendar-forward-day 1))))
  14495. (org-defkey map "!"
  14496. (lambda () (interactive)
  14497. (org-eval-in-calendar '(diary-view-entries))
  14498. (message "")))
  14499. (org-defkey map ">"
  14500. (lambda () (interactive)
  14501. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14502. (org-defkey map "<"
  14503. (lambda () (interactive)
  14504. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14505. (org-defkey map "\C-v"
  14506. (lambda () (interactive)
  14507. (org-eval-in-calendar
  14508. '(calendar-scroll-left-three-months 1))))
  14509. (org-defkey map "\M-v"
  14510. (lambda () (interactive)
  14511. (org-eval-in-calendar
  14512. '(calendar-scroll-right-three-months 1))))
  14513. map)
  14514. "Keymap for minibuffer commands when using `org-read-date'.")
  14515. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14516. default-time default-input inactive)
  14517. "Read a date, possibly a time, and make things smooth for the user.
  14518. The prompt will suggest to enter an ISO date, but you can also enter anything
  14519. which will at least partially be understood by `parse-time-string'.
  14520. Unrecognized parts of the date will default to the current day, month, year,
  14521. hour and minute. If this command is called to replace a timestamp at point,
  14522. or to enter the second timestamp of a range, the default time is taken
  14523. from the existing stamp. Furthermore, the command prefers the future,
  14524. so if you are giving a date where the year is not given, and the day-month
  14525. combination is already past in the current year, it will assume you
  14526. mean next year. For details, see the manual. A few examples:
  14527. 3-2-5 --> 2003-02-05
  14528. feb 15 --> currentyear-02-15
  14529. 2/15 --> currentyear-02-15
  14530. sep 12 9 --> 2009-09-12
  14531. 12:45 --> today 12:45
  14532. 22 sept 0:34 --> currentyear-09-22 0:34
  14533. 12 --> currentyear-currentmonth-12
  14534. Fri --> nearest Friday (today or later)
  14535. etc.
  14536. Furthermore you can specify a relative date by giving, as the *first* thing
  14537. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14538. change in days weeks, months, years.
  14539. With a single plus or minus, the date is relative to today. With a double
  14540. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14541. +4d --> four days from today
  14542. +4 --> same as above
  14543. +2w --> two weeks from today
  14544. ++5 --> five days from default date
  14545. The function understands only English month and weekday abbreviations.
  14546. While prompting, a calendar is popped up - you can also select the
  14547. date with the mouse (button 1). The calendar shows a period of three
  14548. months. To scroll it to other months, use the keys `>' and `<'.
  14549. If you don't like the calendar, turn it off with
  14550. \(setq org-read-date-popup-calendar nil)
  14551. With optional argument TO-TIME, the date will immediately be converted
  14552. to an internal time.
  14553. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14554. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14555. still enter a time, and this function will inform the calling routine
  14556. about this change. The calling routine may then choose to change the
  14557. format used to insert the time stamp into the buffer to include the time.
  14558. With optional argument FROM-STRING, read from this string instead from
  14559. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14560. the time/date that is used for everything that is not specified by the
  14561. user."
  14562. (require 'parse-time)
  14563. (let* ((org-time-stamp-rounding-minutes
  14564. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14565. (org-dcst org-display-custom-times)
  14566. (ct (org-current-time))
  14567. (org-def (or org-overriding-default-time default-time ct))
  14568. (org-defdecode (decode-time org-def))
  14569. (dummy (progn
  14570. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14571. (setcar (nthcdr 2 org-defdecode) -1)
  14572. (setcar (nthcdr 1 org-defdecode) 59)
  14573. (setq org-def (apply 'encode-time org-defdecode)
  14574. org-defdecode (decode-time org-def)))))
  14575. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14576. (calendar-frame-setup nil)
  14577. (calendar-setup nil)
  14578. (calendar-move-hook nil)
  14579. (calendar-view-diary-initially-flag nil)
  14580. (calendar-view-holidays-initially-flag nil)
  14581. (timestr (format-time-string
  14582. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14583. (prompt (concat (if prompt (concat prompt " ") "")
  14584. (format "Date+time [%s]: " timestr)))
  14585. ans (org-ans0 "") org-ans1 org-ans2 final)
  14586. (cond
  14587. (from-string (setq ans from-string))
  14588. (org-read-date-popup-calendar
  14589. (save-excursion
  14590. (save-window-excursion
  14591. (calendar)
  14592. (org-eval-in-calendar '(setq cursor-type nil) t)
  14593. (unwind-protect
  14594. (progn
  14595. (calendar-forward-day (- (time-to-days org-def)
  14596. (calendar-absolute-from-gregorian
  14597. (calendar-current-date))))
  14598. (org-eval-in-calendar nil t)
  14599. (let* ((old-map (current-local-map))
  14600. (map (copy-keymap calendar-mode-map))
  14601. (minibuffer-local-map
  14602. (copy-keymap org-read-date-minibuffer-local-map)))
  14603. (org-defkey map (kbd "RET") 'org-calendar-select)
  14604. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14605. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14606. (unwind-protect
  14607. (progn
  14608. (use-local-map map)
  14609. (setq org-read-date-inactive inactive)
  14610. (add-hook 'post-command-hook 'org-read-date-display)
  14611. (setq org-ans0 (read-string prompt default-input
  14612. 'org-read-date-history nil))
  14613. ;; org-ans0: from prompt
  14614. ;; org-ans1: from mouse click
  14615. ;; org-ans2: from calendar motion
  14616. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14617. (remove-hook 'post-command-hook 'org-read-date-display)
  14618. (use-local-map old-map)
  14619. (when org-read-date-overlay
  14620. (delete-overlay org-read-date-overlay)
  14621. (setq org-read-date-overlay nil)))))
  14622. (bury-buffer "*Calendar*")))))
  14623. (t ; Naked prompt only
  14624. (unwind-protect
  14625. (setq ans (read-string prompt default-input
  14626. 'org-read-date-history timestr))
  14627. (when org-read-date-overlay
  14628. (delete-overlay org-read-date-overlay)
  14629. (setq org-read-date-overlay nil)))))
  14630. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14631. (when org-read-date-analyze-forced-year
  14632. (message "Year was forced into %s"
  14633. (if org-read-date-force-compatible-dates
  14634. "compatible range (1970-2037)"
  14635. "range representable on this machine"))
  14636. (ding))
  14637. ;; One round trip to get rid of 34th of August and stuff like that....
  14638. (setq final (decode-time (apply 'encode-time final)))
  14639. (setq org-read-date-final-answer ans)
  14640. (if to-time
  14641. (apply 'encode-time final)
  14642. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14643. (format "%04d-%02d-%02d %02d:%02d"
  14644. (nth 5 final) (nth 4 final) (nth 3 final)
  14645. (nth 2 final) (nth 1 final))
  14646. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14647. (defvar org-def)
  14648. (defvar org-defdecode)
  14649. (defvar org-with-time)
  14650. (defun org-read-date-display ()
  14651. "Display the current date prompt interpretation in the minibuffer."
  14652. (when org-read-date-display-live
  14653. (when org-read-date-overlay
  14654. (delete-overlay org-read-date-overlay))
  14655. (when (minibufferp (current-buffer))
  14656. (save-excursion
  14657. (end-of-line 1)
  14658. (while (not (equal (buffer-substring
  14659. (max (point-min) (- (point) 4)) (point))
  14660. " "))
  14661. (insert " ")))
  14662. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14663. " " (or org-ans1 org-ans2)))
  14664. (org-end-time-was-given nil)
  14665. (f (org-read-date-analyze ans org-def org-defdecode))
  14666. (fmts (if org-dcst
  14667. org-time-stamp-custom-formats
  14668. org-time-stamp-formats))
  14669. (fmt (if (or org-with-time
  14670. (and (boundp 'org-time-was-given) org-time-was-given))
  14671. (cdr fmts)
  14672. (car fmts)))
  14673. (txt (format-time-string fmt (apply 'encode-time f)))
  14674. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14675. (txt (concat "=> " txt)))
  14676. (when (and org-end-time-was-given
  14677. (string-match org-plain-time-of-day-regexp txt))
  14678. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14679. org-end-time-was-given
  14680. (substring txt (match-end 0)))))
  14681. (when org-read-date-analyze-futurep
  14682. (setq txt (concat txt " (=>F)")))
  14683. (setq org-read-date-overlay
  14684. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14685. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14686. (defun org-read-date-analyze (ans org-def org-defdecode)
  14687. "Analyze the combined answer of the date prompt."
  14688. ;; FIXME: cleanup and comment
  14689. (let ((nowdecode (decode-time (current-time)))
  14690. delta deltan deltaw deltadef year month day
  14691. hour minute second wday pm h2 m2 tl wday1
  14692. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14693. (setq org-read-date-analyze-futurep nil
  14694. org-read-date-analyze-forced-year nil)
  14695. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14696. (setq ans "+0"))
  14697. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14698. (setq ans (replace-match "" t t ans)
  14699. deltan (car delta)
  14700. deltaw (nth 1 delta)
  14701. deltadef (nth 2 delta)))
  14702. ;; Check if there is an iso week date in there. If yes, store the
  14703. ;; info and postpone interpreting it until the rest of the parsing
  14704. ;; is done.
  14705. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14706. (setq iso-year (if (match-end 1)
  14707. (org-small-year-to-year
  14708. (string-to-number (match-string 1 ans))))
  14709. iso-weekday (if (match-end 3)
  14710. (string-to-number (match-string 3 ans)))
  14711. iso-week (string-to-number (match-string 2 ans)))
  14712. (setq ans (replace-match "" t t ans)))
  14713. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14714. (when (string-match
  14715. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14716. (setq year (if (match-end 2)
  14717. (string-to-number (match-string 2 ans))
  14718. (progn (setq kill-year t)
  14719. (string-to-number (format-time-string "%Y"))))
  14720. month (string-to-number (match-string 3 ans))
  14721. day (string-to-number (match-string 4 ans)))
  14722. (if (< year 100) (setq year (+ 2000 year)))
  14723. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14724. t nil ans)))
  14725. ;; Help matching dotted european dates
  14726. (when (string-match
  14727. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14728. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14729. (setq kill-year t)
  14730. (string-to-number (format-time-string "%Y")))
  14731. day (string-to-number (match-string 1 ans))
  14732. month (string-to-number (match-string 2 ans))
  14733. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14734. t nil ans)))
  14735. ;; Help matching american dates, like 5/30 or 5/30/7
  14736. (when (string-match
  14737. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14738. (setq year (if (match-end 4)
  14739. (string-to-number (match-string 4 ans))
  14740. (progn (setq kill-year t)
  14741. (string-to-number (format-time-string "%Y"))))
  14742. month (string-to-number (match-string 1 ans))
  14743. day (string-to-number (match-string 2 ans)))
  14744. (if (< year 100) (setq year (+ 2000 year)))
  14745. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14746. t nil ans)))
  14747. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14748. ;; If there is a time with am/pm, and *no* time without it, we convert
  14749. ;; so that matching will be successful.
  14750. (loop for i from 1 to 2 do ; twice, for end time as well
  14751. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14752. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14753. (setq hour (string-to-number (match-string 1 ans))
  14754. minute (if (match-end 3)
  14755. (string-to-number (match-string 3 ans))
  14756. 0)
  14757. pm (equal ?p
  14758. (string-to-char (downcase (match-string 4 ans)))))
  14759. (if (and (= hour 12) (not pm))
  14760. (setq hour 0)
  14761. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14762. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14763. t t ans))))
  14764. ;; Check if a time range is given as a duration
  14765. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14766. (setq hour (string-to-number (match-string 1 ans))
  14767. h2 (+ hour (string-to-number (match-string 3 ans)))
  14768. minute (string-to-number (match-string 2 ans))
  14769. m2 (+ minute (if (match-end 5) (string-to-number
  14770. (match-string 5 ans))0)))
  14771. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14772. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14773. t t ans)))
  14774. ;; Check if there is a time range
  14775. (when (boundp 'org-end-time-was-given)
  14776. (setq org-time-was-given nil)
  14777. (when (and (string-match org-plain-time-of-day-regexp ans)
  14778. (match-end 8))
  14779. (setq org-end-time-was-given (match-string 8 ans))
  14780. (setq ans (concat (substring ans 0 (match-beginning 7))
  14781. (substring ans (match-end 7))))))
  14782. (setq tl (parse-time-string ans)
  14783. day (or (nth 3 tl) (nth 3 org-defdecode))
  14784. month (or (nth 4 tl)
  14785. (if (and org-read-date-prefer-future
  14786. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14787. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14788. (nth 4 org-defdecode)))
  14789. year (or (and (not kill-year) (nth 5 tl))
  14790. (if (and org-read-date-prefer-future
  14791. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14792. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14793. (nth 5 org-defdecode)))
  14794. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14795. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14796. second (or (nth 0 tl) 0)
  14797. wday (nth 6 tl))
  14798. (when (and (eq org-read-date-prefer-future 'time)
  14799. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14800. (equal day (nth 3 nowdecode))
  14801. (equal month (nth 4 nowdecode))
  14802. (equal year (nth 5 nowdecode))
  14803. (nth 2 tl)
  14804. (or (< (nth 2 tl) (nth 2 nowdecode))
  14805. (and (= (nth 2 tl) (nth 2 nowdecode))
  14806. (nth 1 tl)
  14807. (< (nth 1 tl) (nth 1 nowdecode)))))
  14808. (setq day (1+ day)
  14809. futurep t))
  14810. ;; Special date definitions below
  14811. (cond
  14812. (iso-week
  14813. ;; There was an iso week
  14814. (require 'cal-iso)
  14815. (setq futurep nil)
  14816. (setq year (or iso-year year)
  14817. day (or iso-weekday wday 1)
  14818. wday nil ; to make sure that the trigger below does not match
  14819. iso-date (calendar-gregorian-from-absolute
  14820. (calendar-absolute-from-iso
  14821. (list iso-week day year))))
  14822. ; FIXME: Should we also push ISO weeks into the future?
  14823. ; (when (and org-read-date-prefer-future
  14824. ; (not iso-year)
  14825. ; (< (calendar-absolute-from-gregorian iso-date)
  14826. ; (time-to-days (current-time))))
  14827. ; (setq year (1+ year)
  14828. ; iso-date (calendar-gregorian-from-absolute
  14829. ; (calendar-absolute-from-iso
  14830. ; (list iso-week day year)))))
  14831. (setq month (car iso-date)
  14832. year (nth 2 iso-date)
  14833. day (nth 1 iso-date)))
  14834. (deltan
  14835. (setq futurep nil)
  14836. (unless deltadef
  14837. (let ((now (decode-time (current-time))))
  14838. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14839. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14840. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14841. ((equal deltaw "m") (setq month (+ month deltan)))
  14842. ((equal deltaw "y") (setq year (+ year deltan)))))
  14843. ((and wday (not (nth 3 tl)))
  14844. ;; Weekday was given, but no day, so pick that day in the week
  14845. ;; on or after the derived date.
  14846. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14847. (unless (equal wday wday1)
  14848. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14849. (if (and (boundp 'org-time-was-given)
  14850. (nth 2 tl))
  14851. (setq org-time-was-given t))
  14852. (if (< year 100) (setq year (+ 2000 year)))
  14853. ;; Check of the date is representable
  14854. (if org-read-date-force-compatible-dates
  14855. (progn
  14856. (if (< year 1970)
  14857. (setq year 1970 org-read-date-analyze-forced-year t))
  14858. (if (> year 2037)
  14859. (setq year 2037 org-read-date-analyze-forced-year t)))
  14860. (condition-case nil
  14861. (ignore (encode-time second minute hour day month year))
  14862. (error
  14863. (setq year (nth 5 org-defdecode))
  14864. (setq org-read-date-analyze-forced-year t))))
  14865. (setq org-read-date-analyze-futurep futurep)
  14866. (list second minute hour day month year)))
  14867. (defvar parse-time-weekdays)
  14868. (defun org-read-date-get-relative (s today default)
  14869. "Check string S for special relative date string.
  14870. TODAY and DEFAULT are internal times, for today and for a default.
  14871. Return shift list (N what def-flag)
  14872. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14873. N is the number of WHATs to shift.
  14874. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14875. the DEFAULT date rather than TODAY."
  14876. (require 'parse-time)
  14877. (when (and
  14878. (string-match
  14879. (concat
  14880. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14881. "\\([0-9]+\\)?"
  14882. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14883. "\\([ \t]\\|$\\)") s)
  14884. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14885. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14886. (string-to-char (substring (match-string 1 s) -1))
  14887. ?+))
  14888. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14889. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14890. (what (if (match-end 3) (match-string 3 s) "d"))
  14891. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14892. (date (if rel default today))
  14893. (wday (nth 6 (decode-time date)))
  14894. delta)
  14895. (if wday1
  14896. (progn
  14897. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14898. (if (= dir ?-) (setq delta (- delta 7)))
  14899. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14900. (list delta "d" rel))
  14901. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14902. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14903. "Turn a user-specified date into the internal representation.
  14904. The internal representation needed by the calendar is (month day year).
  14905. This is a wrapper to handle the brain-dead convention in calendar that
  14906. user function argument order change dependent on argument order."
  14907. (if (boundp 'calendar-date-style)
  14908. (cond
  14909. ((eq calendar-date-style 'american)
  14910. (list arg1 arg2 arg3))
  14911. ((eq calendar-date-style 'european)
  14912. (list arg2 arg1 arg3))
  14913. ((eq calendar-date-style 'iso)
  14914. (list arg2 arg3 arg1)))
  14915. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14916. (if (org-bound-and-true-p european-calendar-style)
  14917. (list arg2 arg1 arg3)
  14918. (list arg1 arg2 arg3)))))
  14919. (defun org-eval-in-calendar (form &optional keepdate)
  14920. "Eval FORM in the calendar window and return to current window.
  14921. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14922. otherwise stick to the current value of `org-ans2'."
  14923. (let ((sf (selected-frame))
  14924. (sw (selected-window)))
  14925. (select-window (get-buffer-window "*Calendar*" t))
  14926. (eval form)
  14927. (when (and (not keepdate) (calendar-cursor-to-date))
  14928. (let* ((date (calendar-cursor-to-date))
  14929. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14930. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14931. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14932. (select-window sw)
  14933. (org-select-frame-set-input-focus sf)))
  14934. (defun org-calendar-select ()
  14935. "Return to `org-read-date' with the date currently selected.
  14936. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14937. (interactive)
  14938. (when (calendar-cursor-to-date)
  14939. (let* ((date (calendar-cursor-to-date))
  14940. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14941. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14942. (if (active-minibuffer-window) (exit-minibuffer))))
  14943. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14944. "Insert a date stamp for the date given by the internal TIME.
  14945. WITH-HM means use the stamp format that includes the time of the day.
  14946. INACTIVE means use square brackets instead of angular ones, so that the
  14947. stamp will not contribute to the agenda.
  14948. PRE and POST are optional strings to be inserted before and after the
  14949. stamp.
  14950. The command returns the inserted time stamp."
  14951. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14952. stamp)
  14953. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14954. (insert-before-markers (or pre ""))
  14955. (when (listp extra)
  14956. (setq extra (car extra))
  14957. (if (and (stringp extra)
  14958. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14959. (setq extra (format "-%02d:%02d"
  14960. (string-to-number (match-string 1 extra))
  14961. (string-to-number (match-string 2 extra))))
  14962. (setq extra nil)))
  14963. (when extra
  14964. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14965. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14966. (insert-before-markers (or post ""))
  14967. (setq org-last-inserted-timestamp stamp)))
  14968. (defun org-toggle-time-stamp-overlays ()
  14969. "Toggle the use of custom time stamp formats."
  14970. (interactive)
  14971. (setq org-display-custom-times (not org-display-custom-times))
  14972. (unless org-display-custom-times
  14973. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14974. (while (setq p (next-single-property-change p 'display))
  14975. (if (and (get-text-property p 'display)
  14976. (eq (get-text-property p 'face) 'org-date))
  14977. (remove-text-properties
  14978. p (setq p (next-single-property-change p 'display))
  14979. '(display t))))
  14980. (set-buffer-modified-p bmp)))
  14981. (if (featurep 'xemacs)
  14982. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14983. (org-restart-font-lock)
  14984. (setq org-table-may-need-update t)
  14985. (if org-display-custom-times
  14986. (message "Time stamps are overlaid with custom format")
  14987. (message "Time stamp overlays removed")))
  14988. (defun org-display-custom-time (beg end)
  14989. "Overlay modified time stamp format over timestamp between BEG and END."
  14990. (let* ((ts (buffer-substring beg end))
  14991. t1 w1 with-hm tf time str w2 (off 0))
  14992. (save-match-data
  14993. (setq t1 (org-parse-time-string ts t))
  14994. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14995. (setq off (- (match-end 0) (match-beginning 0)))))
  14996. (setq end (- end off))
  14997. (setq w1 (- end beg)
  14998. with-hm (and (nth 1 t1) (nth 2 t1))
  14999. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15000. time (org-fix-decoded-time t1)
  15001. str (org-add-props
  15002. (format-time-string
  15003. (substring tf 1 -1) (apply 'encode-time time))
  15004. nil 'mouse-face 'highlight)
  15005. w2 (length str))
  15006. (if (not (= w2 w1))
  15007. (add-text-properties (1+ beg) (+ 2 beg)
  15008. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15009. (if (featurep 'xemacs)
  15010. (progn
  15011. (put-text-property beg end 'invisible t)
  15012. (put-text-property beg end 'end-glyph (make-glyph str)))
  15013. (put-text-property beg end 'display str))))
  15014. (defun org-translate-time (string)
  15015. "Translate all timestamps in STRING to custom format.
  15016. But do this only if the variable `org-display-custom-times' is set."
  15017. (when org-display-custom-times
  15018. (save-match-data
  15019. (let* ((start 0)
  15020. (re org-ts-regexp-both)
  15021. t1 with-hm inactive tf time str beg end)
  15022. (while (setq start (string-match re string start))
  15023. (setq beg (match-beginning 0)
  15024. end (match-end 0)
  15025. t1 (save-match-data
  15026. (org-parse-time-string (substring string beg end) t))
  15027. with-hm (and (nth 1 t1) (nth 2 t1))
  15028. inactive (equal (substring string beg (1+ beg)) "[")
  15029. tf (funcall (if with-hm 'cdr 'car)
  15030. org-time-stamp-custom-formats)
  15031. time (org-fix-decoded-time t1)
  15032. str (format-time-string
  15033. (concat
  15034. (if inactive "[" "<") (substring tf 1 -1)
  15035. (if inactive "]" ">"))
  15036. (apply 'encode-time time))
  15037. string (replace-match str t t string)
  15038. start (+ start (length str)))))))
  15039. string)
  15040. (defun org-fix-decoded-time (time)
  15041. "Set 0 instead of nil for the first 6 elements of time.
  15042. Don't touch the rest."
  15043. (let ((n 0))
  15044. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15045. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15046. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15047. "Difference between TIMESTAMP-STRING and now in days.
  15048. If SECONDS is non-nil, return the difference in seconds."
  15049. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15050. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15051. (funcall fdiff (current-time)))))
  15052. (defun org-deadline-close (timestamp-string &optional ndays)
  15053. "Is the time in TIMESTAMP-STRING close to the current date?"
  15054. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15055. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15056. (not (org-entry-is-done-p))))
  15057. (defun org-get-wdays (ts &optional delay zero-delay)
  15058. "Get the deadline lead time appropriate for timestring TS.
  15059. When DELAY is non-nil, get the delay time for scheduled items
  15060. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15061. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15062. don't try to find the delay cookie in the scheduled timestamp."
  15063. (let ((tv (if delay org-scheduled-delay-days
  15064. org-deadline-warning-days)))
  15065. (cond
  15066. ((or (and delay (< tv 0))
  15067. (and delay zero-delay (<= tv 0))
  15068. (and (not delay) (<= tv 0)))
  15069. ;; Enforce this value no matter what
  15070. (- tv))
  15071. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15072. ;; lead time is specified.
  15073. (floor (* (string-to-number (match-string 1 ts))
  15074. (cdr (assoc (match-string 2 ts)
  15075. '(("d" . 1) ("w" . 7)
  15076. ("m" . 30.4) ("y" . 365.25)
  15077. ("h" . 0.041667)))))))
  15078. ;; go for the default.
  15079. (t tv))))
  15080. (defun org-calendar-select-mouse (ev)
  15081. "Return to `org-read-date' with the date currently selected.
  15082. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15083. (interactive "e")
  15084. (mouse-set-point ev)
  15085. (when (calendar-cursor-to-date)
  15086. (let* ((date (calendar-cursor-to-date))
  15087. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15088. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15089. (if (active-minibuffer-window) (exit-minibuffer))))
  15090. (defun org-check-deadlines (ndays)
  15091. "Check if there are any deadlines due or past due.
  15092. A deadline is considered due if it happens within `org-deadline-warning-days'
  15093. days from today's date. If the deadline appears in an entry marked DONE,
  15094. it is not shown. The prefix arg NDAYS can be used to test that many
  15095. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15096. (interactive "P")
  15097. (let* ((org-warn-days
  15098. (cond
  15099. ((equal ndays '(4)) 100000)
  15100. (ndays (prefix-numeric-value ndays))
  15101. (t (abs org-deadline-warning-days))))
  15102. (case-fold-search nil)
  15103. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15104. (callback
  15105. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15106. (message "%d deadlines past-due or due within %d days"
  15107. (org-occur regexp nil callback)
  15108. org-warn-days)))
  15109. (defsubst org-re-timestamp (type)
  15110. "Return a regexp for timestamp TYPE.
  15111. Allowed values for TYPE are:
  15112. all: all timestamps
  15113. active: only active timestamps (<...>)
  15114. inactive: only inactive timestamps ([...])
  15115. scheduled: only scheduled timestamps
  15116. deadline: only deadline timestamps
  15117. When TYPE is nil, fall back on returning a regexp that matches
  15118. both scheduled and deadline timestamps."
  15119. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15120. ((eq type 'active) org-ts-regexp)
  15121. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15122. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15123. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15124. ((eq type 'scheduled-or-deadline)
  15125. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15126. (defun org-check-before-date (date)
  15127. "Check if there are deadlines or scheduled entries before DATE."
  15128. (interactive (list (org-read-date)))
  15129. (let ((case-fold-search nil)
  15130. (regexp (org-re-timestamp org-ts-type))
  15131. (callback
  15132. (lambda () (time-less-p
  15133. (org-time-string-to-time (match-string 1))
  15134. (org-time-string-to-time date)))))
  15135. (message "%d entries before %s"
  15136. (org-occur regexp nil callback) date)))
  15137. (defun org-check-after-date (date)
  15138. "Check if there are deadlines or scheduled entries after DATE."
  15139. (interactive (list (org-read-date)))
  15140. (let ((case-fold-search nil)
  15141. (regexp (org-re-timestamp org-ts-type))
  15142. (callback
  15143. (lambda () (not
  15144. (time-less-p
  15145. (org-time-string-to-time (match-string 1))
  15146. (org-time-string-to-time date))))))
  15147. (message "%d entries after %s"
  15148. (org-occur regexp nil callback) date)))
  15149. (defun org-check-dates-range (start-date end-date)
  15150. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15151. (interactive (list (org-read-date nil nil nil "Range starts")
  15152. (org-read-date nil nil nil "Range end")))
  15153. (let ((case-fold-search nil)
  15154. (regexp (org-re-timestamp org-ts-type))
  15155. (callback
  15156. (lambda ()
  15157. (let ((match (match-string 1)))
  15158. (and
  15159. (not (time-less-p
  15160. (org-time-string-to-time match)
  15161. (org-time-string-to-time start-date)))
  15162. (time-less-p
  15163. (org-time-string-to-time match)
  15164. (org-time-string-to-time end-date)))))))
  15165. (message "%d entries between %s and %s"
  15166. (org-occur regexp nil callback) start-date end-date)))
  15167. (defun org-evaluate-time-range (&optional to-buffer)
  15168. "Evaluate a time range by computing the difference between start and end.
  15169. Normally the result is just printed in the echo area, but with prefix arg
  15170. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15171. If the time range is actually in a table, the result is inserted into the
  15172. next column.
  15173. For time difference computation, a year is assumed to be exactly 365
  15174. days in order to avoid rounding problems."
  15175. (interactive "P")
  15176. (or
  15177. (org-clock-update-time-maybe)
  15178. (save-excursion
  15179. (unless (org-at-date-range-p t)
  15180. (goto-char (point-at-bol))
  15181. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15182. (if (not (org-at-date-range-p t))
  15183. (error "Not at a time-stamp range, and none found in current line")))
  15184. (let* ((ts1 (match-string 1))
  15185. (ts2 (match-string 2))
  15186. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15187. (match-end (match-end 0))
  15188. (time1 (org-time-string-to-time ts1))
  15189. (time2 (org-time-string-to-time ts2))
  15190. (t1 (org-float-time time1))
  15191. (t2 (org-float-time time2))
  15192. (diff (abs (- t2 t1)))
  15193. (negative (< (- t2 t1) 0))
  15194. ;; (ys (floor (* 365 24 60 60)))
  15195. (ds (* 24 60 60))
  15196. (hs (* 60 60))
  15197. (fy "%dy %dd %02d:%02d")
  15198. (fy1 "%dy %dd")
  15199. (fd "%dd %02d:%02d")
  15200. (fd1 "%dd")
  15201. (fh "%02d:%02d")
  15202. y d h m align)
  15203. (if havetime
  15204. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15205. y 0
  15206. d (floor (/ diff ds)) diff (mod diff ds)
  15207. h (floor (/ diff hs)) diff (mod diff hs)
  15208. m (floor (/ diff 60)))
  15209. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15210. y 0
  15211. d (floor (+ (/ diff ds) 0.5))
  15212. h 0 m 0))
  15213. (if (not to-buffer)
  15214. (message "%s" (org-make-tdiff-string y d h m))
  15215. (if (org-at-table-p)
  15216. (progn
  15217. (goto-char match-end)
  15218. (setq align t)
  15219. (and (looking-at " *|") (goto-char (match-end 0))))
  15220. (goto-char match-end))
  15221. (if (looking-at
  15222. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15223. (replace-match ""))
  15224. (if negative (insert " -"))
  15225. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15226. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15227. (insert " " (format fh h m))))
  15228. (if align (org-table-align))
  15229. (message "Time difference inserted")))))
  15230. (defun org-make-tdiff-string (y d h m)
  15231. (let ((fmt "")
  15232. (l nil))
  15233. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15234. l (push y l)))
  15235. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15236. l (push d l)))
  15237. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15238. l (push h l)))
  15239. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15240. l (push m l)))
  15241. (apply 'format fmt (nreverse l))))
  15242. (defun org-time-string-to-time (s &optional buffer pos)
  15243. "Convert a timestamp string into internal time."
  15244. (condition-case errdata
  15245. (apply 'encode-time (org-parse-time-string s))
  15246. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15247. s (if (not (and buffer pos))
  15248. ""
  15249. (format " at %d in buffer `%s'" pos buffer))
  15250. (cdr errdata)))))
  15251. (defun org-time-string-to-seconds (s)
  15252. "Convert a timestamp string to a number of seconds."
  15253. (org-float-time (org-time-string-to-time s)))
  15254. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15255. "Convert a time stamp to an absolute day number.
  15256. If there is a specifier for a cyclic time stamp, get the closest
  15257. date to DAYNR.
  15258. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15259. The variable date is bound by the calendar when this is called."
  15260. (cond
  15261. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15262. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15263. daynr
  15264. (+ daynr 1000)))
  15265. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15266. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15267. (time-to-days (current-time))) (match-string 0 s)
  15268. prefer show-all))
  15269. (t (time-to-days
  15270. (condition-case errdata
  15271. (apply 'encode-time (org-parse-time-string s))
  15272. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15273. s (if (not (and buffer pos))
  15274. ""
  15275. (format " at %d in buffer `%s'" pos buffer))
  15276. (cdr errdata))))))))
  15277. (defun org-days-to-iso-week (days)
  15278. "Return the iso week number."
  15279. (require 'cal-iso)
  15280. (car (calendar-iso-from-absolute days)))
  15281. (defun org-small-year-to-year (year)
  15282. "Convert 2-digit years into 4-digit years.
  15283. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15284. The year 2000 cannot be abbreviated. Any year larger than 99
  15285. is returned unchanged."
  15286. (if (< year 38)
  15287. (setq year (+ 2000 year))
  15288. (if (< year 100)
  15289. (setq year (+ 1900 year))))
  15290. year)
  15291. (defun org-time-from-absolute (d)
  15292. "Return the time corresponding to date D.
  15293. D may be an absolute day number, or a calendar-type list (month day year)."
  15294. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15295. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15296. (defun org-calendar-holiday ()
  15297. "List of holidays, for Diary display in Org-mode."
  15298. (require 'holidays)
  15299. (let ((hl (funcall
  15300. (if (fboundp 'calendar-check-holidays)
  15301. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15302. (if hl (mapconcat 'identity hl "; "))))
  15303. (defun org-diary-sexp-entry (sexp entry date)
  15304. "Process a SEXP diary ENTRY for DATE."
  15305. (require 'diary-lib)
  15306. (let ((result (if calendar-debug-sexp
  15307. (let ((stack-trace-on-error t))
  15308. (eval (car (read-from-string sexp))))
  15309. (condition-case nil
  15310. (eval (car (read-from-string sexp)))
  15311. (error
  15312. (beep)
  15313. (message "Bad sexp at line %d in %s: %s"
  15314. (org-current-line)
  15315. (buffer-file-name) sexp)
  15316. (sleep-for 2))))))
  15317. (cond ((stringp result) (split-string result "; "))
  15318. ((and (consp result)
  15319. (not (consp (cdr result)))
  15320. (stringp (cdr result))) (cdr result))
  15321. ((and (consp result)
  15322. (stringp (car result))) result)
  15323. (result entry))))
  15324. (defun org-diary-to-ical-string (frombuf)
  15325. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15326. This uses the icalendar.el library."
  15327. (let* ((tmpdir (if (featurep 'xemacs)
  15328. (temp-directory)
  15329. temporary-file-directory))
  15330. (tmpfile (make-temp-name
  15331. (expand-file-name "orgics" tmpdir)))
  15332. buf rtn b e)
  15333. (with-current-buffer frombuf
  15334. (icalendar-export-region (point-min) (point-max) tmpfile)
  15335. (setq buf (find-buffer-visiting tmpfile))
  15336. (set-buffer buf)
  15337. (goto-char (point-min))
  15338. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15339. (setq b (match-beginning 0)))
  15340. (goto-char (point-max))
  15341. (if (re-search-backward "^END:VEVENT" nil t)
  15342. (setq e (match-end 0)))
  15343. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15344. (kill-buffer buf)
  15345. (delete-file tmpfile)
  15346. rtn))
  15347. (defun org-closest-date (start current change prefer show-all)
  15348. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15349. When PREFER is `past', return a date that is either CURRENT or past.
  15350. When PREFER is `future', return a date that is either CURRENT or future.
  15351. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15352. ;; Make the proper lists from the dates
  15353. (catch 'exit
  15354. (let ((a1 '(("h" . hour)
  15355. ("d" . day)
  15356. ("w" . week)
  15357. ("m" . month)
  15358. ("y" . year)))
  15359. (shour (nth 2 (org-parse-time-string start)))
  15360. dn dw sday cday n1 n2 n0
  15361. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15362. (setq start (org-date-to-gregorian start)
  15363. current (org-date-to-gregorian
  15364. (if show-all
  15365. current
  15366. (time-to-days (current-time))))
  15367. sday (calendar-absolute-from-gregorian start)
  15368. cday (calendar-absolute-from-gregorian current))
  15369. (if (<= cday sday) (throw 'exit sday))
  15370. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15371. (setq dn (string-to-number (match-string 1 change))
  15372. dw (cdr (assoc (match-string 2 change) a1)))
  15373. (error "Invalid change specifier: %s" change))
  15374. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15375. (cond
  15376. ((eq dw 'hour)
  15377. (let ((missing-hours
  15378. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15379. dn)))
  15380. (setq n1 (if (zerop missing-hours) cday
  15381. (- cday (1+ (floor (/ missing-hours 24)))))
  15382. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15383. ((eq dw 'day)
  15384. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15385. n2 (+ n1 dn)))
  15386. ((eq dw 'year)
  15387. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15388. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15389. (setq date1 (list m d y1)
  15390. n1 (calendar-absolute-from-gregorian date1)
  15391. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15392. n2 (calendar-absolute-from-gregorian date2)))
  15393. ((eq dw 'month)
  15394. ;; approx number of month between the two dates
  15395. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15396. ;; How often does dn fit in there?
  15397. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15398. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15399. m (+ m nm)
  15400. ny (floor (/ m 12))
  15401. y (+ y ny)
  15402. m (- m (* ny 12)))
  15403. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15404. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15405. (setq m2 (+ m dn) y2 y)
  15406. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15407. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15408. (while (<= n2 cday)
  15409. (setq n1 n2 m m2 y y2)
  15410. (setq m2 (+ m dn) y2 y)
  15411. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15412. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15413. ;; Make sure n1 is the earlier date
  15414. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15415. (if show-all
  15416. (cond
  15417. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15418. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15419. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15420. (cond
  15421. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15422. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15423. (t (if (= cday n1) n1 n2)))))))
  15424. (defun org-date-to-gregorian (date)
  15425. "Turn any specification of DATE into a Gregorian date for the calendar."
  15426. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15427. ((and (listp date) (= (length date) 3)) date)
  15428. ((stringp date)
  15429. (setq date (org-parse-time-string date))
  15430. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15431. ((listp date)
  15432. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15433. (defun org-parse-time-string (s &optional nodefault)
  15434. "Parse the standard Org-mode time string.
  15435. This should be a lot faster than the normal `parse-time-string'.
  15436. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15437. hour and minute fields will be nil if not given."
  15438. (cond ((string-match org-ts-regexp0 s)
  15439. (list 0
  15440. (if (or (match-beginning 8) (not nodefault))
  15441. (string-to-number (or (match-string 8 s) "0")))
  15442. (if (or (match-beginning 7) (not nodefault))
  15443. (string-to-number (or (match-string 7 s) "0")))
  15444. (string-to-number (match-string 4 s))
  15445. (string-to-number (match-string 3 s))
  15446. (string-to-number (match-string 2 s))
  15447. nil nil nil))
  15448. ((string-match "^<[^>]+>$" s)
  15449. (decode-time (seconds-to-time (org-matcher-time s))))
  15450. (t (error "Not a standard Org-mode time string: %s" s))))
  15451. (defun org-timestamp-up (&optional arg)
  15452. "Increase the date item at the cursor by one.
  15453. If the cursor is on the year, change the year. If it is on the month,
  15454. the day or the time, change that.
  15455. With prefix ARG, change by that many units."
  15456. (interactive "p")
  15457. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15458. (defun org-timestamp-down (&optional arg)
  15459. "Decrease the date item at the cursor by one.
  15460. If the cursor is on the year, change the year. If it is on the month,
  15461. the day or the time, change that.
  15462. With prefix ARG, change by that many units."
  15463. (interactive "p")
  15464. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15465. (defun org-timestamp-up-day (&optional arg)
  15466. "Increase the date in the time stamp by one day.
  15467. With prefix ARG, change that many days."
  15468. (interactive "p")
  15469. (if (and (not (org-at-timestamp-p t))
  15470. (org-at-heading-p))
  15471. (org-todo 'up)
  15472. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15473. (defun org-timestamp-down-day (&optional arg)
  15474. "Decrease the date in the time stamp by one day.
  15475. With prefix ARG, change that many days."
  15476. (interactive "p")
  15477. (if (and (not (org-at-timestamp-p t))
  15478. (org-at-heading-p))
  15479. (org-todo 'down)
  15480. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15481. (defun org-at-timestamp-p (&optional inactive-ok)
  15482. "Determine if the cursor is in or at a timestamp."
  15483. (interactive)
  15484. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15485. (pos (point))
  15486. (ans (or (looking-at tsr)
  15487. (save-excursion
  15488. (skip-chars-backward "^[<\n\r\t")
  15489. (if (> (point) (point-min)) (backward-char 1))
  15490. (and (looking-at tsr)
  15491. (> (- (match-end 0) pos) -1))))))
  15492. (and ans
  15493. (boundp 'org-ts-what)
  15494. (setq org-ts-what
  15495. (cond
  15496. ((= pos (match-beginning 0)) 'bracket)
  15497. ;; Point is considered to be "on the bracket" whether
  15498. ;; it's really on it or right after it.
  15499. ((= pos (1- (match-end 0))) 'bracket)
  15500. ((= pos (match-end 0)) 'after)
  15501. ((org-pos-in-match-range pos 2) 'year)
  15502. ((org-pos-in-match-range pos 3) 'month)
  15503. ((org-pos-in-match-range pos 7) 'hour)
  15504. ((org-pos-in-match-range pos 8) 'minute)
  15505. ((or (org-pos-in-match-range pos 4)
  15506. (org-pos-in-match-range pos 5)) 'day)
  15507. ((and (> pos (or (match-end 8) (match-end 5)))
  15508. (< pos (match-end 0)))
  15509. (- pos (or (match-end 8) (match-end 5))))
  15510. (t 'day))))
  15511. ans))
  15512. (defun org-toggle-timestamp-type ()
  15513. "Toggle the type (<active> or [inactive]) of a time stamp."
  15514. (interactive)
  15515. (when (org-at-timestamp-p t)
  15516. (let ((beg (match-beginning 0)) (end (match-end 0))
  15517. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15518. (save-excursion
  15519. (goto-char beg)
  15520. (while (re-search-forward "[][<>]" end t)
  15521. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15522. t t)))
  15523. (message "Timestamp is now %sactive"
  15524. (if (equal (char-after beg) ?<) "" "in")))))
  15525. (defun org-at-clock-log-p nil
  15526. "Is the cursor on the clock log line?"
  15527. (save-excursion
  15528. (move-beginning-of-line 1)
  15529. (looking-at "^[ \t]*CLOCK:")))
  15530. (defvar org-clock-history) ; defined in org-clock.el
  15531. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15532. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15533. "Change the date in the time stamp at point.
  15534. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15535. `year', `minute', `second'. If WHAT is not given, the cursor position
  15536. in the timestamp determines what will be changed.
  15537. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15538. (let ((origin (point)) origin-cat
  15539. with-hm inactive
  15540. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15541. org-ts-what
  15542. extra rem
  15543. ts time time0 fixnext clrgx)
  15544. (if (not (org-at-timestamp-p t))
  15545. (error "Not at a timestamp"))
  15546. (if (and (not what) (eq org-ts-what 'bracket))
  15547. (org-toggle-timestamp-type)
  15548. ;; Point isn't on brackets. Remember the part of the time-stamp
  15549. ;; the point was in. Indeed, size of time-stamps may change,
  15550. ;; but point must be kept in the same category nonetheless.
  15551. (setq origin-cat org-ts-what)
  15552. (if (and (not what) (not (eq org-ts-what 'day))
  15553. org-display-custom-times
  15554. (get-text-property (point) 'display)
  15555. (not (get-text-property (1- (point)) 'display)))
  15556. (setq org-ts-what 'day))
  15557. (setq org-ts-what (or what org-ts-what)
  15558. inactive (= (char-after (match-beginning 0)) ?\[)
  15559. ts (match-string 0))
  15560. (replace-match "")
  15561. (when (string-match
  15562. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15563. ts)
  15564. (setq extra (match-string 1 ts))
  15565. (if suppress-tmp-delay
  15566. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15567. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15568. (setq with-hm t))
  15569. (setq time0 (org-parse-time-string ts))
  15570. (when (and updown
  15571. (eq org-ts-what 'minute)
  15572. (not current-prefix-arg))
  15573. ;; This looks like s-up and s-down. Change by one rounding step.
  15574. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15575. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15576. (setcar (cdr time0) (+ (nth 1 time0)
  15577. (if (> n 0) (- rem) (- dm rem))))))
  15578. (setq time
  15579. (encode-time (or (car time0) 0)
  15580. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15581. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15582. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15583. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15584. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15585. (nthcdr 6 time0)))
  15586. (when (and (member org-ts-what '(hour minute))
  15587. extra
  15588. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15589. (setq extra (org-modify-ts-extra
  15590. extra
  15591. (if (eq org-ts-what 'hour) 2 5)
  15592. n dm)))
  15593. (when (integerp org-ts-what)
  15594. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15595. (if (eq what 'calendar)
  15596. (let ((cal-date (org-get-date-from-calendar)))
  15597. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15598. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15599. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15600. (setcar time0 (or (car time0) 0))
  15601. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15602. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15603. (setq time (apply 'encode-time time0))))
  15604. ;; Insert the new time-stamp, and ensure point stays in the same
  15605. ;; category as before (i.e. not after the last position in that
  15606. ;; category).
  15607. (let ((pos (point)))
  15608. ;; Stay before inserted string. `save-excursion' is of no use.
  15609. (setq org-last-changed-timestamp
  15610. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15611. (goto-char pos))
  15612. (save-match-data
  15613. (looking-at org-ts-regexp3)
  15614. (goto-char (cond
  15615. ;; `day' category ends before `hour' if any, or at
  15616. ;; the end of the day name.
  15617. ((eq origin-cat 'day)
  15618. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15619. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15620. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15621. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15622. ;; `year' and `month' have both fixed size: point
  15623. ;; couldn't have moved into another part.
  15624. (t origin))))
  15625. ;; Update clock if on a CLOCK line.
  15626. (org-clock-update-time-maybe)
  15627. ;; Maybe adjust the closest clock in `org-clock-history'
  15628. (when org-clock-adjust-closest
  15629. (if (not (and (org-at-clock-log-p)
  15630. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15631. org-clock-history))))))
  15632. (message "No clock to adjust")
  15633. (cond ((save-excursion ; fix previous clock?
  15634. (re-search-backward org-ts-regexp0 nil t)
  15635. (org-looking-back (concat org-clock-string " \\[")))
  15636. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15637. ((save-excursion ; fix next clock?
  15638. (re-search-backward org-ts-regexp0 nil t)
  15639. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15640. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15641. (save-window-excursion
  15642. ;; Find closest clock to point, adjust the previous/next one in history
  15643. (let* ((p (save-excursion (org-back-to-heading t)))
  15644. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15645. (clfixnth
  15646. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15647. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15648. (if (not clfixpos)
  15649. (message "No clock to adjust")
  15650. (save-excursion
  15651. (org-goto-marker-or-bmk clfixpos)
  15652. (org-show-subtree)
  15653. (when (re-search-forward clrgx nil t)
  15654. (goto-char (match-beginning 1))
  15655. (let (org-clock-adjust-closest)
  15656. (org-timestamp-change n org-ts-what updown))
  15657. (message "Clock adjusted in %s for heading: %s"
  15658. (file-name-nondirectory (buffer-file-name))
  15659. (org-get-heading t t)))))))))
  15660. ;; Try to recenter the calendar window, if any.
  15661. (if (and org-calendar-follow-timestamp-change
  15662. (get-buffer-window "*Calendar*" t)
  15663. (memq org-ts-what '(day month year)))
  15664. (org-recenter-calendar (time-to-days time))))))
  15665. (defun org-modify-ts-extra (s pos n dm)
  15666. "Change the different parts of the lead-time and repeat fields in timestamp."
  15667. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15668. ng h m new rem)
  15669. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15670. (cond
  15671. ((or (org-pos-in-match-range pos 2)
  15672. (org-pos-in-match-range pos 3))
  15673. (setq m (string-to-number (match-string 3 s))
  15674. h (string-to-number (match-string 2 s)))
  15675. (if (org-pos-in-match-range pos 2)
  15676. (setq h (+ h n))
  15677. (setq n (* dm (org-no-warnings (signum n))))
  15678. (when (not (= 0 (setq rem (% m dm))))
  15679. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15680. (setq m (+ m n)))
  15681. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15682. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15683. (setq h (min 24 (max 0 h)))
  15684. (setq ng 1 new (format "-%02d:%02d" h m)))
  15685. ((org-pos-in-match-range pos 6)
  15686. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15687. ((org-pos-in-match-range pos 5)
  15688. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15689. ((org-pos-in-match-range pos 9)
  15690. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15691. ((org-pos-in-match-range pos 8)
  15692. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15693. (when ng
  15694. (setq s (concat
  15695. (substring s 0 (match-beginning ng))
  15696. new
  15697. (substring s (match-end ng))))))
  15698. s))
  15699. (defun org-recenter-calendar (date)
  15700. "If the calendar is visible, recenter it to DATE."
  15701. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15702. (when cwin
  15703. (let ((calendar-move-hook nil))
  15704. (with-selected-window cwin
  15705. (calendar-goto-date (if (listp date) date
  15706. (calendar-gregorian-from-absolute date))))))))
  15707. (defun org-goto-calendar (&optional arg)
  15708. "Go to the Emacs calendar at the current date.
  15709. If there is a time stamp in the current line, go to that date.
  15710. A prefix ARG can be used to force the current date."
  15711. (interactive "P")
  15712. (let ((tsr org-ts-regexp) diff
  15713. (calendar-move-hook nil)
  15714. (calendar-view-holidays-initially-flag nil)
  15715. (calendar-view-diary-initially-flag nil))
  15716. (if (or (org-at-timestamp-p)
  15717. (save-excursion
  15718. (beginning-of-line 1)
  15719. (looking-at (concat ".*" tsr))))
  15720. (let ((d1 (time-to-days (current-time)))
  15721. (d2 (time-to-days
  15722. (org-time-string-to-time (match-string 1)))))
  15723. (setq diff (- d2 d1))))
  15724. (calendar)
  15725. (calendar-goto-today)
  15726. (if (and diff (not arg)) (calendar-forward-day diff))))
  15727. (defun org-get-date-from-calendar ()
  15728. "Return a list (month day year) of date at point in calendar."
  15729. (with-current-buffer "*Calendar*"
  15730. (save-match-data
  15731. (calendar-cursor-to-date))))
  15732. (defun org-date-from-calendar ()
  15733. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15734. If there is already a time stamp at the cursor position, update it."
  15735. (interactive)
  15736. (if (org-at-timestamp-p t)
  15737. (org-timestamp-change 0 'calendar)
  15738. (let ((cal-date (org-get-date-from-calendar)))
  15739. (org-insert-time-stamp
  15740. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15741. (defcustom org-effort-durations
  15742. `(("h" . 60)
  15743. ("d" . ,(* 60 8))
  15744. ("w" . ,(* 60 8 5))
  15745. ("m" . ,(* 60 8 5 4))
  15746. ("y" . ,(* 60 8 5 40)))
  15747. "Conversion factor to minutes for an effort modifier.
  15748. Each entry has the form (MODIFIER . MINUTES).
  15749. In an effort string, a number followed by MODIFIER is multiplied
  15750. by the specified number of MINUTES to obtain an effort in
  15751. minutes.
  15752. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15753. effort string \"2hours\" is equivalent to 120 minutes."
  15754. :group 'org-agenda
  15755. :version "24.1"
  15756. :type '(alist :key-type (string :tag "Modifier")
  15757. :value-type (number :tag "Minutes")))
  15758. (defun org-minutes-to-clocksum-string (m)
  15759. "Format number of minutes as a clocksum string.
  15760. The format is determined by `org-time-clocksum-format',
  15761. `org-time-clocksum-use-fractional' and
  15762. `org-time-clocksum-fractional-format' and
  15763. `org-time-clocksum-use-effort-durations'."
  15764. (let ((clocksum "") h d w mo y fmt n)
  15765. (setq h (if org-time-clocksum-use-effort-durations
  15766. (cdr (assoc "h" org-effort-durations)) 60)
  15767. d (if org-time-clocksum-use-effort-durations
  15768. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15769. w (if org-time-clocksum-use-effort-durations
  15770. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15771. mo (if org-time-clocksum-use-effort-durations
  15772. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15773. y (if org-time-clocksum-use-effort-durations
  15774. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15775. ;; fractional format
  15776. (if org-time-clocksum-use-fractional
  15777. (cond
  15778. ;; single format string
  15779. ((stringp org-time-clocksum-fractional-format)
  15780. (format org-time-clocksum-fractional-format (/ m (float h))))
  15781. ;; choice of fractional formats for different time units
  15782. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15783. (> (/ (truncate m) (* y d h)) 0))
  15784. (format fmt (/ m (* y d (float h)))))
  15785. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15786. (> (/ (truncate m) (* mo d h)) 0))
  15787. (format fmt (/ m (* mo d (float h)))))
  15788. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15789. (> (/ (truncate m) (* w d h)) 0))
  15790. (format fmt (/ m (* w d (float h)))))
  15791. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15792. (> (/ (truncate m) (* d h)) 0))
  15793. (format fmt (/ m (* d (float h)))))
  15794. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15795. (> (/ (truncate m) h) 0))
  15796. (format fmt (/ m (float h))))
  15797. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15798. (format fmt m))
  15799. ;; fall back to smallest time unit with a format
  15800. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15801. (format fmt (/ m (float h))))
  15802. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15803. (format fmt (/ m (* d (float h)))))
  15804. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15805. (format fmt (/ m (* w d (float h)))))
  15806. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15807. (format fmt (/ m (* mo d (float h)))))
  15808. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15809. (format fmt (/ m (* y d (float h))))))
  15810. ;; standard (non-fractional) format, with single format string
  15811. (if (stringp org-time-clocksum-format)
  15812. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15813. ;; separate formats components
  15814. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15815. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15816. (plist-get org-time-clocksum-format :require-years))
  15817. (setq clocksum (concat clocksum (format fmt n))
  15818. m (- m (* n y d h))))
  15819. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15820. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15821. (plist-get org-time-clocksum-format :require-months))
  15822. (setq clocksum (concat clocksum (format fmt n))
  15823. m (- m (* n mo d h))))
  15824. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15825. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15826. (plist-get org-time-clocksum-format :require-weeks))
  15827. (setq clocksum (concat clocksum (format fmt n))
  15828. m (- m (* n w d h))))
  15829. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15830. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15831. (plist-get org-time-clocksum-format :require-days))
  15832. (setq clocksum (concat clocksum (format fmt n))
  15833. m (- m (* n d h))))
  15834. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15835. (or (> (setq n (/ (truncate m) h)) 0)
  15836. (plist-get org-time-clocksum-format :require-hours))
  15837. (setq clocksum (concat clocksum (format fmt n))
  15838. m (- m (* n h))))
  15839. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15840. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15841. (setq clocksum (concat clocksum (format fmt m))))
  15842. ;; return formatted time duration
  15843. clocksum))))
  15844. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15845. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15846. "Org mode version 8.0")
  15847. (defun org-hours-to-clocksum-string (n)
  15848. (org-minutes-to-clocksum-string (* n 60)))
  15849. (defun org-hh:mm-string-to-minutes (s)
  15850. "Convert a string H:MM to a number of minutes.
  15851. If the string is just a number, interpret it as minutes.
  15852. In fact, the first hh:mm or number in the string will be taken,
  15853. there can be extra stuff in the string.
  15854. If no number is found, the return value is 0."
  15855. (cond
  15856. ((integerp s) s)
  15857. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15858. (+ (* (string-to-number (match-string 1 s)) 60)
  15859. (string-to-number (match-string 2 s))))
  15860. ((string-match "\\([0-9]+\\)" s)
  15861. (string-to-number (match-string 1 s)))
  15862. (t 0)))
  15863. (defcustom org-image-actual-width t
  15864. "Should we use the actual width of images when inlining them?
  15865. When set to `t', always use the image width.
  15866. When set to a number, use imagemagick (when available) to set
  15867. the image's width to this value.
  15868. When set to a number in a list, try to get the width from the
  15869. #+ATTR.* keyword if it matches a width specification like
  15870. width=\"[0-9]+\", and fall back on that number if none is found.
  15871. When set to nil, try to get the width from an #+ATTR.* keyword
  15872. and fall back on the original width if none is found.
  15873. This requires Emacs >= 24.1, build with imagemagick support."
  15874. :group 'org-appearance
  15875. :version "24.4"
  15876. :package-version '(Org . "8.0")
  15877. :type '(choice
  15878. (const :tag "Use the image width" t)
  15879. (integer :tag "Use a number of pixels")
  15880. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15881. (const :tag "Use #+ATTR* or don't resize" nil)))
  15882. (defcustom org-agenda-inhibit-startup nil
  15883. "Inhibit startup when preparing agenda buffers.
  15884. When this variable is `t' (the default), the initialization of
  15885. the Org agenda buffers is inhibited: e.g. the visibility state
  15886. is not set, the tables are not re-aligned, etc."
  15887. :type 'boolean
  15888. :version "24.3"
  15889. :group 'org-agenda)
  15890. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15891. "Convert a duration string S to minutes.
  15892. A bare number is interpreted as minutes, modifiers can be set by
  15893. customizing `org-effort-durations' (which see).
  15894. Entries containing a colon are interpreted as H:MM by
  15895. `org-hh:mm-string-to-minutes'."
  15896. (let ((result 0)
  15897. (re (concat "\\([0-9.]+\\) *\\("
  15898. (regexp-opt (mapcar 'car org-effort-durations))
  15899. "\\)")))
  15900. (while (string-match re s)
  15901. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15902. (string-to-number (match-string 1 s))))
  15903. (setq s (replace-match "" nil t s)))
  15904. (setq result (floor result))
  15905. (incf result (org-hh:mm-string-to-minutes s))
  15906. (if output-to-string (number-to-string result) result)))
  15907. ;;;; Files
  15908. (defun org-save-all-org-buffers ()
  15909. "Save all Org-mode buffers without user confirmation."
  15910. (interactive)
  15911. (message "Saving all Org-mode buffers...")
  15912. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15913. (when (featurep 'org-id) (org-id-locations-save))
  15914. (message "Saving all Org-mode buffers... done"))
  15915. (defun org-revert-all-org-buffers ()
  15916. "Revert all Org-mode buffers.
  15917. Prompt for confirmation when there are unsaved changes.
  15918. Be sure you know what you are doing before letting this function
  15919. overwrite your changes.
  15920. This function is useful in a setup where one tracks org files
  15921. with a version control system, to revert on one machine after pulling
  15922. changes from another. I believe the procedure must be like this:
  15923. 1. M-x org-save-all-org-buffers
  15924. 2. Pull changes from the other machine, resolve conflicts
  15925. 3. M-x org-revert-all-org-buffers"
  15926. (interactive)
  15927. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15928. (error "Abort"))
  15929. (save-excursion
  15930. (save-window-excursion
  15931. (mapc
  15932. (lambda (b)
  15933. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15934. (with-current-buffer b buffer-file-name))
  15935. (org-pop-to-buffer-same-window b)
  15936. (revert-buffer t 'no-confirm)))
  15937. (buffer-list))
  15938. (when (and (featurep 'org-id) org-id-track-globally)
  15939. (org-id-locations-load)))))
  15940. ;;;; Agenda files
  15941. ;;;###autoload
  15942. (defun org-switchb (&optional arg)
  15943. "Switch between Org buffers.
  15944. With one prefix argument, restrict available buffers to files.
  15945. With two prefix arguments, restrict available buffers to agenda files.
  15946. Defaults to `iswitchb' for buffer name completion.
  15947. Set `org-completion-use-ido' to make it use ido instead."
  15948. (interactive "P")
  15949. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15950. ((equal arg '(16)) (org-buffer-list 'agenda))
  15951. (t (org-buffer-list))))
  15952. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15953. (org-completion-use-ido org-completion-use-ido))
  15954. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15955. (setq org-completion-use-iswitchb t))
  15956. (org-pop-to-buffer-same-window
  15957. (org-icompleting-read "Org buffer: "
  15958. (mapcar 'list (mapcar 'buffer-name blist))
  15959. nil t))))
  15960. ;;; Define some older names previously used for this functionality
  15961. ;;;###autoload
  15962. (defalias 'org-ido-switchb 'org-switchb)
  15963. ;;;###autoload
  15964. (defalias 'org-iswitchb 'org-switchb)
  15965. (defun org-buffer-list (&optional predicate exclude-tmp)
  15966. "Return a list of Org buffers.
  15967. PREDICATE can be `export', `files' or `agenda'.
  15968. export restrict the list to Export buffers.
  15969. files restrict the list to buffers visiting Org files.
  15970. agenda restrict the list to buffers visiting agenda files.
  15971. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15972. (let* ((bfn nil)
  15973. (agenda-files (and (eq predicate 'agenda)
  15974. (mapcar 'file-truename (org-agenda-files t))))
  15975. (filter
  15976. (cond
  15977. ((eq predicate 'files)
  15978. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15979. ((eq predicate 'export)
  15980. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15981. ((eq predicate 'agenda)
  15982. (lambda (b)
  15983. (with-current-buffer b
  15984. (and (derived-mode-p 'org-mode)
  15985. (setq bfn (buffer-file-name b))
  15986. (member (file-truename bfn) agenda-files)))))
  15987. (t (lambda (b) (with-current-buffer b
  15988. (or (derived-mode-p 'org-mode)
  15989. (string-match "\*Org .*Export"
  15990. (buffer-name b)))))))))
  15991. (delq nil
  15992. (mapcar
  15993. (lambda(b)
  15994. (if (and (funcall filter b)
  15995. (or (not exclude-tmp)
  15996. (not (string-match "tmp" (buffer-name b)))))
  15997. b
  15998. nil))
  15999. (buffer-list)))))
  16000. (defun org-agenda-files (&optional unrestricted archives)
  16001. "Get the list of agenda files.
  16002. Optional UNRESTRICTED means return the full list even if a restriction
  16003. is currently in place.
  16004. When ARCHIVES is t, include all archive files that are really being
  16005. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16006. `org-agenda-archives-mode' is t."
  16007. (let ((files
  16008. (cond
  16009. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16010. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16011. ((listp org-agenda-files) org-agenda-files)
  16012. (t (error "Invalid value of `org-agenda-files'")))))
  16013. (setq files (apply 'append
  16014. (mapcar (lambda (f)
  16015. (if (file-directory-p f)
  16016. (directory-files
  16017. f t org-agenda-file-regexp)
  16018. (list f)))
  16019. files)))
  16020. (when org-agenda-skip-unavailable-files
  16021. (setq files (delq nil
  16022. (mapcar (function
  16023. (lambda (file)
  16024. (and (file-readable-p file) file)))
  16025. files))))
  16026. (when (or (eq archives t)
  16027. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16028. (setq files (org-add-archive-files files)))
  16029. files))
  16030. (defun org-agenda-file-p (&optional file)
  16031. "Return non-nil, if FILE is an agenda file.
  16032. If FILE is omitted, use the file associated with the current
  16033. buffer."
  16034. (member (or file (buffer-file-name))
  16035. (org-agenda-files t)))
  16036. (defun org-edit-agenda-file-list ()
  16037. "Edit the list of agenda files.
  16038. Depending on setup, this either uses customize to edit the variable
  16039. `org-agenda-files', or it visits the file that is holding the list. In the
  16040. latter case, the buffer is set up in a way that saving it automatically kills
  16041. the buffer and restores the previous window configuration."
  16042. (interactive)
  16043. (if (stringp org-agenda-files)
  16044. (let ((cw (current-window-configuration)))
  16045. (find-file org-agenda-files)
  16046. (org-set-local 'org-window-configuration cw)
  16047. (org-add-hook 'after-save-hook
  16048. (lambda ()
  16049. (set-window-configuration
  16050. (prog1 org-window-configuration
  16051. (kill-buffer (current-buffer))))
  16052. (org-install-agenda-files-menu)
  16053. (message "New agenda file list installed"))
  16054. nil 'local)
  16055. (message "%s" (substitute-command-keys
  16056. "Edit list and finish with \\[save-buffer]")))
  16057. (customize-variable 'org-agenda-files)))
  16058. (defun org-store-new-agenda-file-list (list)
  16059. "Set new value for the agenda file list and save it correctly."
  16060. (if (stringp org-agenda-files)
  16061. (let ((fe (org-read-agenda-file-list t)) b u)
  16062. (while (setq b (find-buffer-visiting org-agenda-files))
  16063. (kill-buffer b))
  16064. (with-temp-file org-agenda-files
  16065. (insert
  16066. (mapconcat
  16067. (lambda (f) ;; Keep un-expanded entries.
  16068. (if (setq u (assoc f fe))
  16069. (cdr u)
  16070. f))
  16071. list "\n")
  16072. "\n")))
  16073. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16074. (org-insert-mode-line-in-empty-file nil))
  16075. (setq org-agenda-files list)
  16076. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16077. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16078. "Read the list of agenda files from a file.
  16079. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16080. filenames, used by `org-store-new-agenda-file-list' to write back
  16081. un-expanded file names."
  16082. (when (file-directory-p org-agenda-files)
  16083. (error "`org-agenda-files' cannot be a single directory"))
  16084. (when (stringp org-agenda-files)
  16085. (with-temp-buffer
  16086. (insert-file-contents org-agenda-files)
  16087. (mapcar
  16088. (lambda (f)
  16089. (let ((e (expand-file-name (substitute-in-file-name f)
  16090. org-directory)))
  16091. (if pair-with-expansion
  16092. (cons e f)
  16093. e)))
  16094. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16095. ;;;###autoload
  16096. (defun org-cycle-agenda-files ()
  16097. "Cycle through the files in `org-agenda-files'.
  16098. If the current buffer visits an agenda file, find the next one in the list.
  16099. If the current buffer does not, find the first agenda file."
  16100. (interactive)
  16101. (let* ((fs (org-agenda-files t))
  16102. (files (append fs (list (car fs))))
  16103. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16104. file)
  16105. (unless files (error "No agenda files"))
  16106. (catch 'exit
  16107. (while (setq file (pop files))
  16108. (if (equal (file-truename file) tcf)
  16109. (when (car files)
  16110. (find-file (car files))
  16111. (throw 'exit t))))
  16112. (find-file (car fs)))
  16113. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16114. (defun org-agenda-file-to-front (&optional to-end)
  16115. "Move/add the current file to the top of the agenda file list.
  16116. If the file is not present in the list, it is added to the front. If it is
  16117. present, it is moved there. With optional argument TO-END, add/move to the
  16118. end of the list."
  16119. (interactive "P")
  16120. (let ((org-agenda-skip-unavailable-files nil)
  16121. (file-alist (mapcar (lambda (x)
  16122. (cons (file-truename x) x))
  16123. (org-agenda-files t)))
  16124. (ctf (file-truename
  16125. (or buffer-file-name
  16126. (error "Please save the current buffer to a file"))))
  16127. x had)
  16128. (setq x (assoc ctf file-alist) had x)
  16129. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16130. (if to-end
  16131. (setq file-alist (append (delq x file-alist) (list x)))
  16132. (setq file-alist (cons x (delq x file-alist))))
  16133. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16134. (org-install-agenda-files-menu)
  16135. (message "File %s to %s of agenda file list"
  16136. (if had "moved" "added") (if to-end "end" "front"))))
  16137. (defun org-remove-file (&optional file)
  16138. "Remove current file from the list of files in variable `org-agenda-files'.
  16139. These are the files which are being checked for agenda entries.
  16140. Optional argument FILE means use this file instead of the current."
  16141. (interactive)
  16142. (let* ((org-agenda-skip-unavailable-files nil)
  16143. (file (or file buffer-file-name
  16144. (error "Current buffer does not visit a file")))
  16145. (true-file (file-truename file))
  16146. (afile (abbreviate-file-name file))
  16147. (files (delq nil (mapcar
  16148. (lambda (x)
  16149. (if (equal true-file
  16150. (file-truename x))
  16151. nil x))
  16152. (org-agenda-files t)))))
  16153. (if (not (= (length files) (length (org-agenda-files t))))
  16154. (progn
  16155. (org-store-new-agenda-file-list files)
  16156. (org-install-agenda-files-menu)
  16157. (message "Removed file: %s" afile))
  16158. (message "File was not in list: %s (not removed)" afile))))
  16159. (defun org-file-menu-entry (file)
  16160. (vector file (list 'find-file file) t))
  16161. (defun org-check-agenda-file (file)
  16162. "Make sure FILE exists. If not, ask user what to do."
  16163. (when (not (file-exists-p file))
  16164. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16165. (abbreviate-file-name file))
  16166. (let ((r (downcase (read-char-exclusive))))
  16167. (cond
  16168. ((equal r ?r)
  16169. (org-remove-file file)
  16170. (throw 'nextfile t))
  16171. (t (error "Abort"))))))
  16172. (defun org-get-agenda-file-buffer (file)
  16173. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16174. it to the list of buffers which might be released later."
  16175. (let ((buf (org-find-base-buffer-visiting file)))
  16176. (if buf
  16177. buf ; just return it
  16178. ;; Make a new buffer and remember it
  16179. (setq buf (find-file-noselect file))
  16180. (if buf (push buf org-agenda-new-buffers))
  16181. buf)))
  16182. (defun org-release-buffers (blist)
  16183. "Release all buffers in list, asking the user for confirmation when needed.
  16184. When a buffer is unmodified, it is just killed. When modified, it is saved
  16185. \(if the user agrees) and then killed."
  16186. (let (buf file)
  16187. (while (setq buf (pop blist))
  16188. (setq file (buffer-file-name buf))
  16189. (when (and (buffer-modified-p buf)
  16190. file
  16191. (y-or-n-p (format "Save file %s? " file)))
  16192. (with-current-buffer buf (save-buffer)))
  16193. (kill-buffer buf))))
  16194. (defun org-agenda-prepare-buffers (files)
  16195. "Create buffers for all agenda files, protect archived trees and comments."
  16196. (interactive)
  16197. (let ((pa '(:org-archived t))
  16198. (pc '(:org-comment t))
  16199. (pall '(:org-archived t :org-comment t))
  16200. (inhibit-read-only t)
  16201. (org-inhibit-startup org-agenda-inhibit-startup)
  16202. (rea (concat ":" org-archive-tag ":"))
  16203. file re)
  16204. (save-excursion
  16205. (save-restriction
  16206. (while (setq file (pop files))
  16207. (catch 'nextfile
  16208. (if (bufferp file)
  16209. (set-buffer file)
  16210. (org-check-agenda-file file)
  16211. (set-buffer (org-get-agenda-file-buffer file)))
  16212. (widen)
  16213. (org-refresh-category-properties)
  16214. (org-refresh-properties org-effort-property 'org-effort)
  16215. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16216. (setq org-todo-keywords-for-agenda
  16217. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16218. (setq org-done-keywords-for-agenda
  16219. (append org-done-keywords-for-agenda org-done-keywords))
  16220. (setq org-todo-keyword-alist-for-agenda
  16221. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16222. (setq org-drawers-for-agenda
  16223. (append org-drawers-for-agenda org-drawers))
  16224. (setq org-tag-alist-for-agenda
  16225. (append org-tag-alist-for-agenda org-tag-alist))
  16226. (org-with-silent-modifications
  16227. (save-excursion
  16228. (remove-text-properties (point-min) (point-max) pall)
  16229. (when org-agenda-skip-archived-trees
  16230. (goto-char (point-min))
  16231. (while (re-search-forward rea nil t)
  16232. (if (org-at-heading-p t)
  16233. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16234. (goto-char (point-min))
  16235. (setq re (format org-heading-keyword-regexp-format
  16236. org-comment-string))
  16237. (while (re-search-forward re nil t)
  16238. (add-text-properties
  16239. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16240. (setq org-todo-keywords-for-agenda
  16241. (org-uniquify org-todo-keywords-for-agenda))
  16242. (setq org-todo-keyword-alist-for-agenda
  16243. (org-uniquify org-todo-keyword-alist-for-agenda)
  16244. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16245. ;;;; CDLaTeX minor mode
  16246. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16247. "Keymap for the minor `org-cdlatex-mode'.")
  16248. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16249. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16250. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16251. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16252. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16253. (defvar org-cdlatex-texmathp-advice-is-done nil
  16254. "Flag remembering if we have applied the advice to texmathp already.")
  16255. (define-minor-mode org-cdlatex-mode
  16256. "Toggle the minor `org-cdlatex-mode'.
  16257. This mode supports entering LaTeX environment and math in LaTeX fragments
  16258. in Org-mode.
  16259. \\{org-cdlatex-mode-map}"
  16260. nil " OCDL" nil
  16261. (when org-cdlatex-mode
  16262. (require 'cdlatex)
  16263. (run-hooks 'cdlatex-mode-hook)
  16264. (cdlatex-compute-tables))
  16265. (unless org-cdlatex-texmathp-advice-is-done
  16266. (setq org-cdlatex-texmathp-advice-is-done t)
  16267. (defadvice texmathp (around org-math-always-on activate)
  16268. "Always return t in org-mode buffers.
  16269. This is because we want to insert math symbols without dollars even outside
  16270. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16271. an embedded LaTeX fragment, let texmathp do its job.
  16272. \\[org-cdlatex-mode-map]"
  16273. (interactive)
  16274. (let (p)
  16275. (cond
  16276. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16277. ((eq this-command 'cdlatex-math-symbol)
  16278. (setq ad-return-value t
  16279. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16280. (t
  16281. (let ((p (org-inside-LaTeX-fragment-p)))
  16282. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16283. (setq ad-return-value t
  16284. texmathp-why '("Org-mode embedded math" . 0))
  16285. (if p ad-do-it)))))))))
  16286. (defun turn-on-org-cdlatex ()
  16287. "Unconditionally turn on `org-cdlatex-mode'."
  16288. (org-cdlatex-mode 1))
  16289. (defun org-try-cdlatex-tab ()
  16290. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16291. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16292. - inside a LaTeX fragment, or
  16293. - after the first word in a line, where an abbreviation expansion could
  16294. insert a LaTeX environment."
  16295. (when org-cdlatex-mode
  16296. (cond
  16297. ;; Before any word on the line: No expansion possible.
  16298. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16299. ;; Just after first word on the line: Expand it. Make sure it
  16300. ;; cannot happen on headlines, though.
  16301. ((save-excursion
  16302. (skip-chars-backward "a-zA-Z0-9*")
  16303. (skip-chars-backward " \t")
  16304. (and (bolp) (not (org-at-heading-p))))
  16305. (cdlatex-tab) t)
  16306. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16307. (defun org-cdlatex-underscore-caret (&optional arg)
  16308. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16309. Revert to the normal definition outside of these fragments."
  16310. (interactive "P")
  16311. (if (org-inside-LaTeX-fragment-p)
  16312. (call-interactively 'cdlatex-sub-superscript)
  16313. (let (org-cdlatex-mode)
  16314. (call-interactively (key-binding (vector last-input-event))))))
  16315. (defun org-cdlatex-math-modify (&optional arg)
  16316. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16317. Revert to the normal definition outside of these fragments."
  16318. (interactive "P")
  16319. (if (org-inside-LaTeX-fragment-p)
  16320. (call-interactively 'cdlatex-math-modify)
  16321. (let (org-cdlatex-mode)
  16322. (call-interactively (key-binding (vector last-input-event))))))
  16323. ;;;; LaTeX fragments
  16324. (defvar org-latex-regexps
  16325. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16326. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16327. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16328. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16329. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16330. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16331. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16332. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16333. "Regular expressions for matching embedded LaTeX.")
  16334. (defun org-inside-LaTeX-fragment-p ()
  16335. "Test if point is inside a LaTeX fragment.
  16336. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16337. sequence appearing also before point.
  16338. Even though the matchers for math are configurable, this function assumes
  16339. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16340. delimiters are skipped when they have been removed by customization.
  16341. The return value is nil, or a cons cell with the delimiter and the
  16342. position of this delimiter.
  16343. This function does a reasonably good job, but can locally be fooled by
  16344. for example currency specifications. For example it will assume being in
  16345. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16346. fragments that are properly closed, but during editing, we have to live
  16347. with the uncertainty caused by missing closing delimiters. This function
  16348. looks only before point, not after."
  16349. (catch 'exit
  16350. (let ((pos (point))
  16351. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16352. (lim (progn
  16353. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16354. (point)))
  16355. dd-on str (start 0) m re)
  16356. (goto-char pos)
  16357. (when dodollar
  16358. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16359. re (nth 1 (assoc "$" org-latex-regexps)))
  16360. (while (string-match re str start)
  16361. (cond
  16362. ((= (match-end 0) (length str))
  16363. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16364. ((= (match-end 0) (- (length str) 5))
  16365. (throw 'exit nil))
  16366. (t (setq start (match-end 0))))))
  16367. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16368. (goto-char pos)
  16369. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16370. (and (match-beginning 2) (throw 'exit nil))
  16371. ;; count $$
  16372. (while (re-search-backward "\\$\\$" lim t)
  16373. (setq dd-on (not dd-on)))
  16374. (goto-char pos)
  16375. (if dd-on (cons "$$" m))))))
  16376. (defun org-inside-latex-macro-p ()
  16377. "Is point inside a LaTeX macro or its arguments?"
  16378. (save-match-data
  16379. (org-in-regexp
  16380. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16381. (defvar org-latex-fragment-image-overlays nil
  16382. "List of overlays carrying the images of latex fragments.")
  16383. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16384. (defun org-remove-latex-fragment-image-overlays ()
  16385. "Remove all overlays with LaTeX fragment images in current buffer."
  16386. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16387. (setq org-latex-fragment-image-overlays nil))
  16388. (defun org-preview-latex-fragment (&optional subtree)
  16389. "Preview the LaTeX fragment at point, or all locally or globally.
  16390. If the cursor is in a LaTeX fragment, create the image and overlay
  16391. it over the source code. If there is no fragment at point, display
  16392. all fragments in the current text, from one headline to the next. With
  16393. prefix SUBTREE, display all fragments in the current subtree. With a
  16394. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16395. the cursor is before the first headline,
  16396. display all fragments in the buffer.
  16397. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16398. (interactive "P")
  16399. (unless buffer-file-name
  16400. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16401. (org-remove-latex-fragment-image-overlays)
  16402. (save-excursion
  16403. (save-restriction
  16404. (let (beg end at msg)
  16405. (cond
  16406. ((or (equal subtree '(16))
  16407. (not (save-excursion
  16408. (re-search-backward org-outline-regexp-bol nil t))))
  16409. (setq beg (point-min) end (point-max)
  16410. msg "Creating images for buffer...%s"))
  16411. ((equal subtree '(4))
  16412. (org-back-to-heading)
  16413. (setq beg (point) end (org-end-of-subtree t)
  16414. msg "Creating images for subtree...%s"))
  16415. (t
  16416. (if (setq at (org-inside-LaTeX-fragment-p))
  16417. (goto-char (max (point-min) (- (cdr at) 2)))
  16418. (org-back-to-heading))
  16419. (setq beg (point) end (progn (outline-next-heading) (point))
  16420. msg (if at "Creating image...%s"
  16421. "Creating images for entry...%s"))))
  16422. (message msg "")
  16423. (narrow-to-region beg end)
  16424. (goto-char beg)
  16425. (org-format-latex
  16426. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16427. (file-name-nondirectory
  16428. buffer-file-name)))
  16429. default-directory 'overlays msg at 'forbuffer
  16430. org-latex-create-formula-image-program)
  16431. (message msg "done. Use `C-c C-c' to remove images.")))))
  16432. (defun org-format-latex (prefix &optional dir overlays msg at
  16433. forbuffer processing-type)
  16434. "Replace LaTeX fragments with links to an image, and produce images.
  16435. Some of the options can be changed using the variable
  16436. `org-format-latex-options'."
  16437. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16438. (let* ((prefixnodir (file-name-nondirectory prefix))
  16439. (absprefix (expand-file-name prefix dir))
  16440. (todir (file-name-directory absprefix))
  16441. (opt org-format-latex-options)
  16442. (optnew org-format-latex-options)
  16443. (matchers (plist-get opt :matchers))
  16444. (re-list org-latex-regexps)
  16445. (cnt 0) txt hash link beg end re e checkdir
  16446. string
  16447. m n block-type block linkfile movefile ov)
  16448. ;; Check the different regular expressions
  16449. (while (setq e (pop re-list))
  16450. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16451. block (if block-type "\n\n" ""))
  16452. (when (member m matchers)
  16453. (goto-char (point-min))
  16454. (while (re-search-forward re nil t)
  16455. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16456. (or (not overlays)
  16457. (not (eq (get-char-property (match-beginning n)
  16458. 'org-overlay-type)
  16459. 'org-latex-overlay))))
  16460. (cond
  16461. ((eq processing-type 'verbatim))
  16462. ((eq processing-type 'mathjax)
  16463. ;; Prepare for MathJax processing.
  16464. (setq string (match-string n))
  16465. (when (member m '("$" "$1"))
  16466. (save-excursion
  16467. (delete-region (match-beginning n) (match-end n))
  16468. (goto-char (match-beginning n))
  16469. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16470. ((or (eq processing-type 'dvipng)
  16471. (eq processing-type 'imagemagick))
  16472. ;; Process to an image.
  16473. (setq txt (match-string n)
  16474. beg (match-beginning n) end (match-end n)
  16475. cnt (1+ cnt))
  16476. (let ((face (face-at-point))
  16477. (fg (plist-get opt :foreground))
  16478. (bg (plist-get opt :background))
  16479. ;; Ensure full list is printed.
  16480. print-length print-level)
  16481. (when forbuffer
  16482. ;; Get the colors from the face at point.
  16483. (goto-char beg)
  16484. (when (eq fg 'auto)
  16485. (setq fg (face-attribute face :foreground nil 'default)))
  16486. (when (eq bg 'auto)
  16487. (setq bg (face-attribute face :background nil 'default)))
  16488. (setq optnew (copy-sequence opt))
  16489. (plist-put optnew :foreground fg)
  16490. (plist-put optnew :background bg))
  16491. (setq hash (sha1 (prin1-to-string
  16492. (list org-format-latex-header
  16493. org-latex-default-packages-alist
  16494. org-latex-packages-alist
  16495. org-format-latex-options
  16496. forbuffer txt fg bg)))
  16497. linkfile (format "%s_%s.png" prefix hash)
  16498. movefile (format "%s_%s.png" absprefix hash)))
  16499. (setq link (concat block "[[file:" linkfile "]]" block))
  16500. (if msg (message msg cnt))
  16501. (goto-char beg)
  16502. (unless checkdir ; Ensure the directory exists.
  16503. (setq checkdir t)
  16504. (or (file-directory-p todir) (make-directory todir t)))
  16505. (unless (file-exists-p movefile)
  16506. (org-create-formula-image
  16507. txt movefile optnew forbuffer processing-type))
  16508. (if overlays
  16509. (progn
  16510. (mapc (lambda (o)
  16511. (if (eq (overlay-get o 'org-overlay-type)
  16512. 'org-latex-overlay)
  16513. (delete-overlay o)))
  16514. (overlays-in beg end))
  16515. (setq ov (make-overlay beg end))
  16516. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16517. (if (featurep 'xemacs)
  16518. (progn
  16519. (overlay-put ov 'invisible t)
  16520. (overlay-put
  16521. ov 'end-glyph
  16522. (make-glyph (vector 'png :file movefile))))
  16523. (overlay-put
  16524. ov 'display
  16525. (list 'image :type 'png :file movefile :ascent 'center)))
  16526. (push ov org-latex-fragment-image-overlays)
  16527. (goto-char end))
  16528. (delete-region beg end)
  16529. (insert (org-add-props link
  16530. (list 'org-latex-src
  16531. (replace-regexp-in-string
  16532. "\"" "" txt)
  16533. 'org-latex-src-embed-type
  16534. (if block-type 'paragraph 'character))))))
  16535. ((eq processing-type 'mathml)
  16536. ;; Process to MathML
  16537. (unless (save-match-data (org-format-latex-mathml-available-p))
  16538. (error "LaTeX to MathML converter not configured"))
  16539. (setq txt (match-string n)
  16540. beg (match-beginning n) end (match-end n)
  16541. cnt (1+ cnt))
  16542. (if msg (message msg cnt))
  16543. (goto-char beg)
  16544. (delete-region beg end)
  16545. (insert (org-format-latex-as-mathml
  16546. txt block-type prefix dir)))
  16547. (t
  16548. (error "Unknown conversion type %s for latex fragments"
  16549. processing-type)))))))))
  16550. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16551. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16552. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16553. sucessful, return the portion between \"<math...> </math>\"
  16554. elements otherwise return nil. When MATHML-FILE is specified,
  16555. write the results in to that file. When invoked as an
  16556. interactive command, prompt for LATEX-FRAG, with initial value
  16557. set to the current active region and echo the results for user
  16558. inspection."
  16559. (interactive (list (let ((frag (when (org-region-active-p)
  16560. (buffer-substring-no-properties
  16561. (region-beginning) (region-end)))))
  16562. (read-string "LaTeX Fragment: " frag nil frag))))
  16563. (unless latex-frag (error "Invalid latex-frag"))
  16564. (let* ((tmp-in-file (file-relative-name
  16565. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16566. (ignore (write-region latex-frag nil tmp-in-file))
  16567. (tmp-out-file (file-relative-name
  16568. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16569. (cmd (format-spec
  16570. org-latex-to-mathml-convert-command
  16571. `((?j . ,(shell-quote-argument
  16572. (expand-file-name org-latex-to-mathml-jar-file)))
  16573. (?I . ,(shell-quote-argument tmp-in-file))
  16574. (?o . ,(shell-quote-argument tmp-out-file)))))
  16575. mathml shell-command-output)
  16576. (when (org-called-interactively-p 'any)
  16577. (unless (org-format-latex-mathml-available-p)
  16578. (error "LaTeX to MathML converter not configured")))
  16579. (message "Running %s" cmd)
  16580. (setq shell-command-output (shell-command-to-string cmd))
  16581. (setq mathml
  16582. (when (file-readable-p tmp-out-file)
  16583. (with-current-buffer (find-file-noselect tmp-out-file t)
  16584. (goto-char (point-min))
  16585. (when (re-search-forward
  16586. (concat
  16587. (regexp-quote
  16588. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16589. "\\(.\\|\n\\)*"
  16590. (regexp-quote "</math>")) nil t)
  16591. (prog1 (match-string 0) (kill-buffer))))))
  16592. (cond
  16593. (mathml
  16594. (setq mathml
  16595. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16596. (when mathml-file
  16597. (write-region mathml nil mathml-file))
  16598. (when (org-called-interactively-p 'any)
  16599. (message mathml)))
  16600. ((message "LaTeX to MathML conversion failed")
  16601. (message shell-command-output)))
  16602. (delete-file tmp-in-file)
  16603. (when (file-exists-p tmp-out-file)
  16604. (delete-file tmp-out-file))
  16605. mathml))
  16606. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16607. prefix &optional dir)
  16608. "Use `org-create-math-formula' but check local cache first."
  16609. (let* ((absprefix (expand-file-name prefix dir))
  16610. (print-length nil) (print-level nil)
  16611. (formula-id (concat
  16612. "formula-"
  16613. (sha1
  16614. (prin1-to-string
  16615. (list latex-frag
  16616. org-latex-to-mathml-convert-command)))))
  16617. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16618. (formula-cache-dir (file-name-directory formula-cache)))
  16619. (unless (file-directory-p formula-cache-dir)
  16620. (make-directory formula-cache-dir t))
  16621. (unless (file-exists-p formula-cache)
  16622. (org-create-math-formula latex-frag formula-cache))
  16623. (if (file-exists-p formula-cache)
  16624. ;; Successful conversion. Return the link to MathML file.
  16625. (org-add-props
  16626. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16627. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16628. 'org-latex-src-embed-type (if latex-frag-type
  16629. 'paragraph 'character)))
  16630. ;; Failed conversion. Return the LaTeX fragment verbatim
  16631. latex-frag)))
  16632. (defun org-create-formula-image (string tofile options buffer &optional type)
  16633. "Create an image from LaTeX source using dvipng or convert.
  16634. This function calls either `org-create-formula-image-with-dvipng'
  16635. or `org-create-formula-image-with-imagemagick' depending on the
  16636. value of `org-latex-create-formula-image-program' or on the value
  16637. of the optional TYPE variable.
  16638. Note: ultimately these two function should be combined as they
  16639. share a good deal of logic."
  16640. (org-check-external-command
  16641. "latex" "needed to convert LaTeX fragments to images")
  16642. (funcall
  16643. (case (or type org-latex-create-formula-image-program)
  16644. ('dvipng
  16645. (org-check-external-command
  16646. "dvipng" "needed to convert LaTeX fragments to images")
  16647. #'org-create-formula-image-with-dvipng)
  16648. ('imagemagick
  16649. (org-check-external-command
  16650. "convert" "you need to install imagemagick")
  16651. #'org-create-formula-image-with-imagemagick)
  16652. (t (error
  16653. "invalid value of `org-latex-create-formula-image-program'")))
  16654. string tofile options buffer))
  16655. (declare-function org-export--get-global-options "ox" (&optional backend))
  16656. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16657. (defun org-create-formula--latex-header ()
  16658. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16659. (org-latex-guess-inputenc
  16660. (org-splice-latex-header
  16661. org-format-latex-header
  16662. org-latex-default-packages-alist
  16663. org-latex-packages-alist t
  16664. (plist-get
  16665. (org-combine-plists
  16666. (org-export--get-global-options 'latex)
  16667. (org-export--get-inbuffer-options 'latex))
  16668. :latex-header-extra))))
  16669. ;; This function borrows from Ganesh Swami's latex2png.el
  16670. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16671. "This calls dvipng."
  16672. (require 'ox-latex)
  16673. (let* ((tmpdir (if (featurep 'xemacs)
  16674. (temp-directory)
  16675. temporary-file-directory))
  16676. (texfilebase (make-temp-name
  16677. (expand-file-name "orgtex" tmpdir)))
  16678. (texfile (concat texfilebase ".tex"))
  16679. (dvifile (concat texfilebase ".dvi"))
  16680. (pngfile (concat texfilebase ".png"))
  16681. (fnh (if (featurep 'xemacs)
  16682. (font-height (face-font 'default))
  16683. (face-attribute 'default :height nil)))
  16684. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16685. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16686. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16687. "Black"))
  16688. (bg (or (plist-get options (if buffer :background :html-background))
  16689. "Transparent")))
  16690. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16691. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16692. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16693. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16694. (let ((latex-header (org-create-formula--latex-header)))
  16695. (with-temp-file texfile
  16696. (insert latex-header)
  16697. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16698. (let ((dir default-directory))
  16699. (condition-case nil
  16700. (progn
  16701. (cd tmpdir)
  16702. (call-process "latex" nil nil nil texfile))
  16703. (error nil))
  16704. (cd dir))
  16705. (if (not (file-exists-p dvifile))
  16706. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16707. (condition-case nil
  16708. (if (featurep 'xemacs)
  16709. (call-process "dvipng" nil nil nil
  16710. "-fg" fg "-bg" bg
  16711. "-T" "tight"
  16712. "-o" pngfile
  16713. dvifile)
  16714. (call-process "dvipng" nil nil nil
  16715. "-fg" fg "-bg" bg
  16716. "-D" dpi
  16717. ;;"-x" scale "-y" scale
  16718. "-T" "tight"
  16719. "-o" pngfile
  16720. dvifile))
  16721. (error nil))
  16722. (if (not (file-exists-p pngfile))
  16723. (if org-format-latex-signal-error
  16724. (error "Failed to create png file from %s" texfile)
  16725. (message "Failed to create png file from %s" texfile)
  16726. nil)
  16727. ;; Use the requested file name and clean up
  16728. (copy-file pngfile tofile 'replace)
  16729. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16730. (if (file-exists-p (concat texfilebase e))
  16731. (delete-file (concat texfilebase e))))
  16732. pngfile))))
  16733. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  16734. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16735. "This calls convert, which is included into imagemagick."
  16736. (require 'ox-latex)
  16737. (let* ((tmpdir (if (featurep 'xemacs)
  16738. (temp-directory)
  16739. temporary-file-directory))
  16740. (texfilebase (make-temp-name
  16741. (expand-file-name "orgtex" tmpdir)))
  16742. (texfile (concat texfilebase ".tex"))
  16743. (pdffile (concat texfilebase ".pdf"))
  16744. (pngfile (concat texfilebase ".png"))
  16745. (fnh (if (featurep 'xemacs)
  16746. (font-height (face-font 'default))
  16747. (face-attribute 'default :height nil)))
  16748. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16749. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16750. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16751. "black"))
  16752. (bg (or (plist-get options (if buffer :background :html-background))
  16753. "white")))
  16754. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16755. (setq fg (org-latex-color-format fg)))
  16756. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16757. (setq bg (org-latex-color-format
  16758. (if (string= bg "Transparent") "white" bg))))
  16759. (let ((latex-header (org-create-formula--latex-header)))
  16760. (with-temp-file texfile
  16761. (insert latex-header)
  16762. (insert "\n\\begin{document}\n"
  16763. "\\definecolor{fg}{rgb}{" fg "}\n"
  16764. "\\definecolor{bg}{rgb}{" bg "}\n"
  16765. "\n\\pagecolor{bg}\n"
  16766. "\n{\\color{fg}\n"
  16767. string
  16768. "\n}\n"
  16769. "\n\\end{document}\n")))
  16770. (org-latex-compile texfile t)
  16771. (if (not (file-exists-p pdffile))
  16772. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16773. (condition-case nil
  16774. (if (featurep 'xemacs)
  16775. (call-process "convert" nil nil nil
  16776. "-density" "96"
  16777. "-trim"
  16778. "-antialias"
  16779. pdffile
  16780. "-quality" "100"
  16781. ;; "-sharpen" "0x1.0"
  16782. pngfile)
  16783. (call-process "convert" nil nil nil
  16784. "-density" dpi
  16785. "-trim"
  16786. "-antialias"
  16787. pdffile
  16788. "-quality" "100"
  16789. ;; "-sharpen" "0x1.0"
  16790. pngfile))
  16791. (error nil))
  16792. (if (not (file-exists-p pngfile))
  16793. (if org-format-latex-signal-error
  16794. (error "Failed to create png file from %s" texfile)
  16795. (message "Failed to create png file from %s" texfile)
  16796. nil)
  16797. ;; Use the requested file name and clean up
  16798. (copy-file pngfile tofile 'replace)
  16799. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16800. (if (file-exists-p (concat texfilebase e))
  16801. (delete-file (concat texfilebase e))))
  16802. pngfile))))
  16803. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16804. "Fill a LaTeX header template TPL.
  16805. In the template, the following place holders will be recognized:
  16806. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16807. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16808. [PACKAGES] \\usepackage statements for PKG
  16809. [NO-PACKAGES] do not include PKG
  16810. [EXTRA] the string EXTRA
  16811. [NO-EXTRA] do not include EXTRA
  16812. For backward compatibility, if both the positive and the negative place
  16813. holder is missing, the positive one (without the \"NO-\") will be
  16814. assumed to be present at the end of the template.
  16815. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16816. EXTRA is a string.
  16817. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16818. (let (rpl (end ""))
  16819. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16820. (setq rpl (if (or (match-end 1) (not def-pkg))
  16821. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16822. tpl (replace-match rpl t t tpl))
  16823. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16824. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16825. (setq rpl (if (or (match-end 1) (not pkg))
  16826. "" (org-latex-packages-to-string pkg snippets-p t))
  16827. tpl (replace-match rpl t t tpl))
  16828. (if pkg (setq end
  16829. (concat end "\n"
  16830. (org-latex-packages-to-string pkg snippets-p)))))
  16831. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16832. (setq rpl (if (or (match-end 1) (not extra))
  16833. "" (concat extra "\n"))
  16834. tpl (replace-match rpl t t tpl))
  16835. (if (and extra (string-match "\\S-" extra))
  16836. (setq end (concat end "\n" extra))))
  16837. (if (string-match "\\S-" end)
  16838. (concat tpl "\n" end)
  16839. tpl)))
  16840. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16841. "Turn an alist of packages into a string with the \\usepackage macros."
  16842. (setq pkg (mapconcat (lambda(p)
  16843. (cond
  16844. ((stringp p) p)
  16845. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16846. (format "%% Package %s omitted" (cadr p)))
  16847. ((equal "" (car p))
  16848. (format "\\usepackage{%s}" (cadr p)))
  16849. (t
  16850. (format "\\usepackage[%s]{%s}"
  16851. (car p) (cadr p)))))
  16852. pkg
  16853. "\n"))
  16854. (if newline (concat pkg "\n") pkg))
  16855. (defun org-dvipng-color (attr)
  16856. "Return a RGB color specification for dvipng."
  16857. (apply 'format "rgb %s %s %s"
  16858. (mapcar 'org-normalize-color
  16859. (if (featurep 'xemacs)
  16860. (color-rgb-components
  16861. (face-property 'default
  16862. (cond ((eq attr :foreground) 'foreground)
  16863. ((eq attr :background) 'background))))
  16864. (color-values (face-attribute 'default attr nil))))))
  16865. (defun org-dvipng-color-format (color-name)
  16866. "Convert COLOR-NAME to a RGB color value for dvipng."
  16867. (apply 'format "rgb %s %s %s"
  16868. (mapcar 'org-normalize-color
  16869. (color-values color-name))))
  16870. (defun org-latex-color (attr)
  16871. "Return a RGB color for the LaTeX color package."
  16872. (apply 'format "%s,%s,%s"
  16873. (mapcar 'org-normalize-color
  16874. (if (featurep 'xemacs)
  16875. (color-rgb-components
  16876. (face-property 'default
  16877. (cond ((eq attr :foreground) 'foreground)
  16878. ((eq attr :background) 'background))))
  16879. (color-values (face-attribute 'default attr nil))))))
  16880. (defun org-latex-color-format (color-name)
  16881. "Convert COLOR-NAME to a RGB color value."
  16882. (apply 'format "%s,%s,%s"
  16883. (mapcar 'org-normalize-color
  16884. (color-values color-name))))
  16885. (defun org-normalize-color (value)
  16886. "Return string to be used as color value for an RGB component."
  16887. (format "%g" (/ value 65535.0)))
  16888. ;; Image display
  16889. (defvar org-inline-image-overlays nil)
  16890. (make-variable-buffer-local 'org-inline-image-overlays)
  16891. (defun org-toggle-inline-images (&optional include-linked)
  16892. "Toggle the display of inline images.
  16893. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16894. (interactive "P")
  16895. (if org-inline-image-overlays
  16896. (progn
  16897. (org-remove-inline-images)
  16898. (message "Inline image display turned off"))
  16899. (org-display-inline-images include-linked)
  16900. (if (and (org-called-interactively-p)
  16901. org-inline-image-overlays)
  16902. (message "%d images displayed inline"
  16903. (length org-inline-image-overlays))
  16904. (message "No images to display inline"))))
  16905. (defun org-redisplay-inline-images ()
  16906. "Refresh the display of inline images."
  16907. (interactive)
  16908. (if (not org-inline-image-overlays)
  16909. (org-toggle-inline-images)
  16910. (org-toggle-inline-images)
  16911. (org-toggle-inline-images)))
  16912. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16913. "Display inline images.
  16914. Normally only links without a description part are inlined, because this
  16915. is how it will work for export. When INCLUDE-LINKED is set, also links
  16916. with a description part will be inlined. This can be nice for a quick
  16917. look at those images, but it does not reflect what exported files will look
  16918. like.
  16919. When REFRESH is set, refresh existing images between BEG and END.
  16920. This will create new image displays only if necessary.
  16921. BEG and END default to the buffer boundaries."
  16922. (interactive "P")
  16923. (unless refresh
  16924. (org-remove-inline-images)
  16925. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16926. (save-excursion
  16927. (save-restriction
  16928. (widen)
  16929. (setq beg (or beg (point-min)) end (or end (point-max)))
  16930. (goto-char beg)
  16931. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16932. (substring (org-image-file-name-regexp) 0 -2)
  16933. "\\)\\]" (if include-linked "" "\\]")))
  16934. old file ov img type attrwidth width)
  16935. (while (re-search-forward re end t)
  16936. (setq old (get-char-property-and-overlay (match-beginning 1)
  16937. 'org-image-overlay)
  16938. file (expand-file-name
  16939. (concat (or (match-string 3) "") (match-string 4))))
  16940. (when (image-type-available-p 'imagemagick)
  16941. (setq attrwidth (if (or (listp org-image-actual-width)
  16942. (null org-image-actual-width))
  16943. (save-excursion
  16944. (save-match-data
  16945. (when (re-search-backward
  16946. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16947. (save-excursion
  16948. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16949. (string-to-number (match-string 1))))))
  16950. width (cond ((eq org-image-actual-width t) nil)
  16951. ((null org-image-actual-width) attrwidth)
  16952. ((numberp org-image-actual-width)
  16953. org-image-actual-width)
  16954. ((listp org-image-actual-width)
  16955. (or attrwidth (car org-image-actual-width))))
  16956. type (if width 'imagemagick)))
  16957. (when (file-exists-p file)
  16958. (if (and (car-safe old) refresh)
  16959. (image-refresh (overlay-get (cdr old) 'display))
  16960. (setq img (save-match-data (create-image file type nil :width width)))
  16961. (when img
  16962. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16963. (overlay-put ov 'display img)
  16964. (overlay-put ov 'face 'default)
  16965. (overlay-put ov 'org-image-overlay t)
  16966. (overlay-put ov 'modification-hooks
  16967. (list 'org-display-inline-remove-overlay))
  16968. (push ov org-inline-image-overlays)))))))))
  16969. (define-obsolete-function-alias
  16970. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16971. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16972. "Remove inline-display overlay if a corresponding region is modified."
  16973. (let ((inhibit-modification-hooks t))
  16974. (when (and ov after)
  16975. (delete ov org-inline-image-overlays)
  16976. (delete-overlay ov))))
  16977. (defun org-remove-inline-images ()
  16978. "Remove inline display of images."
  16979. (interactive)
  16980. (mapc 'delete-overlay org-inline-image-overlays)
  16981. (setq org-inline-image-overlays nil))
  16982. ;;;; Key bindings
  16983. ;; Outline functions from `outline-mode-prefix-map'
  16984. ;; that can be remapped in Org:
  16985. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16986. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16987. (define-key org-mode-map [remap outline-forward-same-level]
  16988. 'org-forward-heading-same-level)
  16989. (define-key org-mode-map [remap outline-backward-same-level]
  16990. 'org-backward-heading-same-level)
  16991. (define-key org-mode-map [remap show-branches]
  16992. 'org-kill-note-or-show-branches)
  16993. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16994. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16995. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16996. ;; Outline functions from `outline-mode-prefix-map' that can not
  16997. ;; be remapped in Org:
  16998. ;;
  16999. ;; - the column "key binding" shows whether the Outline function is still
  17000. ;; available in Org mode on the same key that it has been bound to in
  17001. ;; Outline mode:
  17002. ;; - "overridden": key used for a different functionality in Org mode
  17003. ;; - else: key still bound to the same Outline function in Org mode
  17004. ;;
  17005. ;; | Outline function | key binding | Org replacement |
  17006. ;; |------------------------------------+-------------+-----------------------|
  17007. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17008. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17009. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17010. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17011. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17012. ;; | `show-entry' | overridden | no replacement |
  17013. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17014. ;; | `show-branches' | `C-c C-k' | still same function |
  17015. ;; | `show-subtree' | overridden | visibility cycling |
  17016. ;; | `show-all' | overridden | no replacement |
  17017. ;; | `hide-subtree' | overridden | visibility cycling |
  17018. ;; | `hide-body' | overridden | no replacement |
  17019. ;; | `hide-entry' | overridden | visibility cycling |
  17020. ;; | `hide-leaves' | overridden | no replacement |
  17021. ;; | `hide-sublevels' | overridden | no replacement |
  17022. ;; | `hide-other' | overridden | no replacement |
  17023. ;; Make `C-c C-x' a prefix key
  17024. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17025. ;; TAB key with modifiers
  17026. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17027. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17028. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17029. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17030. ;; The following line is necessary under Suse GNU/Linux
  17031. (unless (featurep 'xemacs)
  17032. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17033. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17034. (define-key org-mode-map [backtab] 'org-shifttab)
  17035. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17036. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17037. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17038. ;; Cursor keys with modifiers
  17039. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17040. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17041. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17042. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17043. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17044. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17045. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17046. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17047. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17048. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17049. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17050. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17051. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17052. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17053. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17054. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17055. ;; Babel keys
  17056. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17057. (mapc (lambda (pair)
  17058. (define-key org-babel-map (car pair) (cdr pair)))
  17059. org-babel-key-bindings)
  17060. ;;; Extra keys for tty access.
  17061. ;; We only set them when really needed because otherwise the
  17062. ;; menus don't show the simple keys
  17063. (when (or org-use-extra-keys
  17064. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17065. (not window-system))
  17066. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17067. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17068. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17069. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17070. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17071. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17072. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17073. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17074. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17075. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17076. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17077. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17078. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17079. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17080. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17081. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17082. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17083. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17084. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17085. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17086. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17087. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17088. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17089. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17090. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17091. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17092. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17093. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17094. ;; All the other keys
  17095. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17096. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17097. (if (boundp 'narrow-map)
  17098. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17099. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17100. (if (boundp 'narrow-map)
  17101. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17102. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17103. (if (boundp 'narrow-map)
  17104. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17105. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17106. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17107. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17108. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17109. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17110. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17111. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17112. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17113. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17114. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17115. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17116. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17117. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17118. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17119. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17120. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17121. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17122. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17123. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17124. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17125. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17126. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17127. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17128. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17129. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17130. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17131. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17132. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17133. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17134. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17135. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17136. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17137. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17138. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17139. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17140. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17141. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17142. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17143. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17144. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17145. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17146. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17147. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17148. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17149. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17150. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17151. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17152. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17153. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17154. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17155. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17156. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17157. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17158. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17159. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17160. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17161. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17162. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17163. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17164. (org-defkey org-mode-map "\C-m" 'org-return)
  17165. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17166. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17167. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17168. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17169. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17170. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17171. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17172. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17173. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17174. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17175. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17176. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17177. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17178. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17179. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17180. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17181. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17182. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17183. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17184. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17185. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17186. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17187. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17188. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17189. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17190. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17191. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17192. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17193. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17194. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17195. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17196. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17197. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17198. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17199. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17200. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17201. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17202. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17203. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17204. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17205. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17206. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17207. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17208. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17209. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17210. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17211. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17212. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17213. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17214. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17215. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17216. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17217. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17218. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17219. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17220. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17221. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17222. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17223. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17224. (when (featurep 'xemacs)
  17225. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17226. (defconst org-speed-commands-default
  17227. '(
  17228. ("Outline Navigation")
  17229. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17230. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17231. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17232. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17233. ("F" . org-next-block)
  17234. ("B" . org-previous-block)
  17235. ("u" . (org-speed-move-safe 'outline-up-heading))
  17236. ("j" . org-goto)
  17237. ("g" . (org-refile t))
  17238. ("Outline Visibility")
  17239. ("c" . org-cycle)
  17240. ("C" . org-shifttab)
  17241. (" " . org-display-outline-path)
  17242. ("=" . org-columns)
  17243. ("Outline Structure Editing")
  17244. ("U" . org-shiftmetaup)
  17245. ("D" . org-shiftmetadown)
  17246. ("r" . org-metaright)
  17247. ("l" . org-metaleft)
  17248. ("R" . org-shiftmetaright)
  17249. ("L" . org-shiftmetaleft)
  17250. ("i" . (progn (forward-char 1) (call-interactively
  17251. 'org-insert-heading-respect-content)))
  17252. ("^" . org-sort)
  17253. ("w" . org-refile)
  17254. ("a" . org-archive-subtree-default-with-confirmation)
  17255. ("@" . org-mark-subtree)
  17256. ("#" . org-toggle-comment)
  17257. ("Clock Commands")
  17258. ("I" . org-clock-in)
  17259. ("O" . org-clock-out)
  17260. ("Meta Data Editing")
  17261. ("t" . org-todo)
  17262. ("," . (org-priority))
  17263. ("0" . (org-priority ?\ ))
  17264. ("1" . (org-priority ?A))
  17265. ("2" . (org-priority ?B))
  17266. ("3" . (org-priority ?C))
  17267. (":" . org-set-tags-command)
  17268. ("e" . org-set-effort)
  17269. ("E" . org-inc-effort)
  17270. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17271. (org-entry-put (point) "APPT_WARNTIME" m)))
  17272. ("Agenda Views etc")
  17273. ("v" . org-agenda)
  17274. ("/" . org-sparse-tree)
  17275. ("Misc")
  17276. ("o" . org-open-at-point)
  17277. ("?" . org-speed-command-help)
  17278. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17279. (">" . (org-agenda-remove-restriction-lock))
  17280. )
  17281. "The default speed commands.")
  17282. (defun org-print-speed-command (e)
  17283. (if (> (length (car e)) 1)
  17284. (progn
  17285. (princ "\n")
  17286. (princ (car e))
  17287. (princ "\n")
  17288. (princ (make-string (length (car e)) ?-))
  17289. (princ "\n"))
  17290. (princ (car e))
  17291. (princ " ")
  17292. (if (symbolp (cdr e))
  17293. (princ (symbol-name (cdr e)))
  17294. (prin1 (cdr e)))
  17295. (princ "\n")))
  17296. (defun org-speed-command-help ()
  17297. "Show the available speed commands."
  17298. (interactive)
  17299. (if (not org-use-speed-commands)
  17300. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17301. (with-output-to-temp-buffer "*Help*"
  17302. (princ "User-defined Speed commands\n===========================\n")
  17303. (mapc 'org-print-speed-command org-speed-commands-user)
  17304. (princ "\n")
  17305. (princ "Built-in Speed commands\n=======================\n")
  17306. (mapc 'org-print-speed-command org-speed-commands-default))
  17307. (with-current-buffer "*Help*"
  17308. (setq truncate-lines t))))
  17309. (defun org-speed-move-safe (cmd)
  17310. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17311. If not, return to the original position and throw an error."
  17312. (interactive)
  17313. (let ((pos (point)))
  17314. (call-interactively cmd)
  17315. (unless (and (bolp) (org-at-heading-p))
  17316. (goto-char pos)
  17317. (error "Boundary reached while executing %s" cmd))))
  17318. (defvar org-self-insert-command-undo-counter 0)
  17319. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17320. (defvar org-speed-command nil)
  17321. (define-obsolete-function-alias
  17322. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17323. (defun org-speed-command-activate (keys)
  17324. "Hook for activating single-letter speed commands.
  17325. `org-speed-commands-default' specifies a minimal command set.
  17326. Use `org-speed-commands-user' for further customization."
  17327. (when (or (and (bolp) (looking-at org-outline-regexp))
  17328. (and (functionp org-use-speed-commands)
  17329. (funcall org-use-speed-commands)))
  17330. (cdr (assoc keys (append org-speed-commands-user
  17331. org-speed-commands-default)))))
  17332. (define-obsolete-function-alias
  17333. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17334. (defun org-babel-speed-command-activate (keys)
  17335. "Hook for activating single-letter code block commands."
  17336. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17337. (cdr (assoc keys org-babel-key-bindings))))
  17338. (defcustom org-speed-command-hook
  17339. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17340. "Hook for activating speed commands at strategic locations.
  17341. Hook functions are called in sequence until a valid handler is
  17342. found.
  17343. Each hook takes a single argument, a user-pressed command key
  17344. which is also a `self-insert-command' from the global map.
  17345. Within the hook, examine the cursor position and the command key
  17346. and return nil or a valid handler as appropriate. Handler could
  17347. be one of an interactive command, a function, or a form.
  17348. Set `org-use-speed-commands' to non-nil value to enable this
  17349. hook. The default setting is `org-speed-command-activate'."
  17350. :group 'org-structure
  17351. :version "24.1"
  17352. :type 'hook)
  17353. (defun org-self-insert-command (N)
  17354. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17355. If the cursor is in a table looking at whitespace, the whitespace is
  17356. overwritten, and the table is not marked as requiring realignment."
  17357. (interactive "p")
  17358. (org-check-before-invisible-edit 'insert)
  17359. (cond
  17360. ((and org-use-speed-commands
  17361. (setq org-speed-command
  17362. (run-hook-with-args-until-success
  17363. 'org-speed-command-hook (this-command-keys))))
  17364. (cond
  17365. ((commandp org-speed-command)
  17366. (setq this-command org-speed-command)
  17367. (call-interactively org-speed-command))
  17368. ((functionp org-speed-command)
  17369. (funcall org-speed-command))
  17370. ((and org-speed-command (listp org-speed-command))
  17371. (eval org-speed-command))
  17372. (t (let (org-use-speed-commands)
  17373. (call-interactively 'org-self-insert-command)))))
  17374. ((and
  17375. (org-table-p)
  17376. (progn
  17377. ;; check if we blank the field, and if that triggers align
  17378. (and (featurep 'org-table) org-table-auto-blank-field
  17379. (member last-command
  17380. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17381. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17382. ;; got extra space, this field does not determine column width
  17383. (let (org-table-may-need-update) (org-table-blank-field))
  17384. ;; no extra space, this field may determine column width
  17385. (org-table-blank-field)))
  17386. t)
  17387. (eq N 1)
  17388. (looking-at "[^|\n]* |"))
  17389. (let (org-table-may-need-update)
  17390. (goto-char (1- (match-end 0)))
  17391. (backward-delete-char 1)
  17392. (goto-char (match-beginning 0))
  17393. (self-insert-command N)))
  17394. (t
  17395. (setq org-table-may-need-update t)
  17396. (self-insert-command N)
  17397. (org-fix-tags-on-the-fly)
  17398. (if org-self-insert-cluster-for-undo
  17399. (if (not (eq last-command 'org-self-insert-command))
  17400. (setq org-self-insert-command-undo-counter 1)
  17401. (if (>= org-self-insert-command-undo-counter 20)
  17402. (setq org-self-insert-command-undo-counter 1)
  17403. (and (> org-self-insert-command-undo-counter 0)
  17404. buffer-undo-list (listp buffer-undo-list)
  17405. (not (cadr buffer-undo-list)) ; remove nil entry
  17406. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17407. (setq org-self-insert-command-undo-counter
  17408. (1+ org-self-insert-command-undo-counter))))))))
  17409. (defun org-check-before-invisible-edit (kind)
  17410. "Check is editing if kind KIND would be dangerous with invisible text around.
  17411. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17412. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17413. (if (and org-catch-invisible-edits
  17414. (or (not (boundp 'visible-mode)) (not visible-mode))
  17415. (or (get-char-property (point) 'invisible)
  17416. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17417. ;; OK, we need to take a closer look
  17418. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17419. (invisible-before-point (if (bobp) nil (get-char-property
  17420. (1- (point)) 'invisible)))
  17421. (border-and-ok-direction
  17422. (or
  17423. ;; Check if we are acting predictably before invisible text
  17424. (and invisible-at-point (not invisible-before-point)
  17425. (memq kind '(insert delete-backward)))
  17426. ;; Check if we are acting predictably after invisible text
  17427. ;; This works not well, and I have turned it off. It seems
  17428. ;; better to always show and stop after invisible text.
  17429. ;; (and (not invisible-at-point) invisible-before-point
  17430. ;; (memq kind '(insert delete)))
  17431. )))
  17432. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17433. (memq invisible-before-point '(outline org-hide-block t)))
  17434. (if (eq org-catch-invisible-edits 'error)
  17435. (error "Editing in invisible areas is prohibited - make visible first"))
  17436. (if (and org-custom-properties-overlays
  17437. (y-or-n-p "Display invisible properties in this buffer? "))
  17438. (org-toggle-custom-properties-visibility)
  17439. ;; Make the area visible
  17440. (save-excursion
  17441. (if invisible-before-point
  17442. (goto-char (previous-single-char-property-change
  17443. (point) 'invisible)))
  17444. (org-cycle))
  17445. (cond
  17446. ((eq org-catch-invisible-edits 'show)
  17447. ;; That's it, we do the edit after showing
  17448. (message
  17449. "Unfolding invisible region around point before editing")
  17450. (sit-for 1))
  17451. ((and (eq org-catch-invisible-edits 'smart)
  17452. border-and-ok-direction)
  17453. (message "Unfolding invisible region around point before editing"))
  17454. (t
  17455. ;; Don't do the edit, make the user repeat it in full visibility
  17456. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17457. (defun org-fix-tags-on-the-fly ()
  17458. (when (and (equal (char-after (point-at-bol)) ?*)
  17459. (org-at-heading-p))
  17460. (org-align-tags-here org-tags-column)))
  17461. (defun org-delete-backward-char (N)
  17462. "Like `delete-backward-char', insert whitespace at field end in tables.
  17463. When deleting backwards, in tables this function will insert whitespace in
  17464. front of the next \"|\" separator, to keep the table aligned. The table will
  17465. still be marked for re-alignment if the field did fill the entire column,
  17466. because, in this case the deletion might narrow the column."
  17467. (interactive "p")
  17468. (save-match-data
  17469. (org-check-before-invisible-edit 'delete-backward)
  17470. (if (and (org-table-p)
  17471. (eq N 1)
  17472. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17473. (looking-at ".*?|"))
  17474. (let ((pos (point))
  17475. (noalign (looking-at "[^|\n\r]* |"))
  17476. (c org-table-may-need-update))
  17477. (backward-delete-char N)
  17478. (if (not overwrite-mode)
  17479. (progn
  17480. (skip-chars-forward "^|")
  17481. (insert " ")
  17482. (goto-char (1- pos))))
  17483. ;; noalign: if there were two spaces at the end, this field
  17484. ;; does not determine the width of the column.
  17485. (if noalign (setq org-table-may-need-update c)))
  17486. (backward-delete-char N)
  17487. (org-fix-tags-on-the-fly))))
  17488. (defun org-delete-char (N)
  17489. "Like `delete-char', but insert whitespace at field end in tables.
  17490. When deleting characters, in tables this function will insert whitespace in
  17491. front of the next \"|\" separator, to keep the table aligned. The table will
  17492. still be marked for re-alignment if the field did fill the entire column,
  17493. because, in this case the deletion might narrow the column."
  17494. (interactive "p")
  17495. (save-match-data
  17496. (org-check-before-invisible-edit 'delete)
  17497. (if (and (org-table-p)
  17498. (not (bolp))
  17499. (not (= (char-after) ?|))
  17500. (eq N 1))
  17501. (if (looking-at ".*?|")
  17502. (let ((pos (point))
  17503. (noalign (looking-at "[^|\n\r]* |"))
  17504. (c org-table-may-need-update))
  17505. (replace-match (concat
  17506. (substring (match-string 0) 1 -1)
  17507. " |"))
  17508. (goto-char pos)
  17509. ;; noalign: if there were two spaces at the end, this field
  17510. ;; does not determine the width of the column.
  17511. (if noalign (setq org-table-may-need-update c)))
  17512. (delete-char N))
  17513. (delete-char N)
  17514. (org-fix-tags-on-the-fly))))
  17515. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17516. (put 'org-self-insert-command 'delete-selection t)
  17517. (put 'orgtbl-self-insert-command 'delete-selection t)
  17518. (put 'org-delete-char 'delete-selection 'supersede)
  17519. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17520. (put 'org-yank 'delete-selection 'yank)
  17521. ;; Make `flyspell-mode' delay after some commands
  17522. (put 'org-self-insert-command 'flyspell-delayed t)
  17523. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17524. (put 'org-delete-char 'flyspell-delayed t)
  17525. (put 'org-delete-backward-char 'flyspell-delayed t)
  17526. ;; Make pabbrev-mode expand after org-mode commands
  17527. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17528. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17529. ;; How to do this: Measure non-white length of current string
  17530. ;; If equal to column width, we should realign.
  17531. (defun org-remap (map &rest commands)
  17532. "In MAP, remap the functions given in COMMANDS.
  17533. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17534. (let (new old)
  17535. (while commands
  17536. (setq old (pop commands) new (pop commands))
  17537. (if (fboundp 'command-remapping)
  17538. (org-defkey map (vector 'remap old) new)
  17539. (substitute-key-definition old new map global-map)))))
  17540. (defun org-transpose-words ()
  17541. "Transpose words for Org.
  17542. This uses the `org-mode-transpose-word-syntax-table' syntax
  17543. table, which interprets characters in `org-emphasis-alist' as
  17544. word constituants."
  17545. (interactive)
  17546. (with-syntax-table org-mode-transpose-word-syntax-table
  17547. (call-interactively 'transpose-words)))
  17548. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17549. (when (eq org-enable-table-editor 'optimized)
  17550. ;; If the user wants maximum table support, we need to hijack
  17551. ;; some standard editing functions
  17552. (org-remap org-mode-map
  17553. 'self-insert-command 'org-self-insert-command
  17554. 'delete-char 'org-delete-char
  17555. 'delete-backward-char 'org-delete-backward-char)
  17556. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17557. (defvar org-ctrl-c-ctrl-c-hook nil
  17558. "Hook for functions attaching themselves to `C-c C-c'.
  17559. This can be used to add additional functionality to the C-c C-c
  17560. key which executes context-dependent commands. This hook is run
  17561. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17562. run after the last test.
  17563. Each function will be called with no arguments. The function
  17564. must check if the context is appropriate for it to act. If yes,
  17565. it should do its thing and then return a non-nil value. If the
  17566. context is wrong, just do nothing and return nil.")
  17567. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17568. "Hook for functions attaching themselves to `C-c C-c'.
  17569. This can be used to add additional functionality to the C-c C-c
  17570. key which executes context-dependent commands. This hook is run
  17571. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17572. before the first test.
  17573. Each function will be called with no arguments. The function
  17574. must check if the context is appropriate for it to act. If yes,
  17575. it should do its thing and then return a non-nil value. If the
  17576. context is wrong, just do nothing and return nil.")
  17577. (defvar org-tab-first-hook nil
  17578. "Hook for functions to attach themselves to TAB.
  17579. See `org-ctrl-c-ctrl-c-hook' for more information.
  17580. This hook runs as the first action when TAB is pressed, even before
  17581. `org-cycle' messes around with the `outline-regexp' to cater for
  17582. inline tasks and plain list item folding.
  17583. If any function in this hook returns t, any other actions that
  17584. would have been caused by TAB (such as table field motion or visibility
  17585. cycling) will not occur.")
  17586. (defvar org-tab-after-check-for-table-hook nil
  17587. "Hook for functions to attach themselves to TAB.
  17588. See `org-ctrl-c-ctrl-c-hook' for more information.
  17589. This hook runs after it has been established that the cursor is not in a
  17590. table, but before checking if the cursor is in a headline or if global cycling
  17591. should be done.
  17592. If any function in this hook returns t, not other actions like visibility
  17593. cycling will be done.")
  17594. (defvar org-tab-after-check-for-cycling-hook nil
  17595. "Hook for functions to attach themselves to TAB.
  17596. See `org-ctrl-c-ctrl-c-hook' for more information.
  17597. This hook runs after it has been established that not table field motion and
  17598. not visibility should be done because of current context. This is probably
  17599. the place where a package like yasnippets can hook in.")
  17600. (defvar org-tab-before-tab-emulation-hook nil
  17601. "Hook for functions to attach themselves to TAB.
  17602. See `org-ctrl-c-ctrl-c-hook' for more information.
  17603. This hook runs after every other options for TAB have been exhausted, but
  17604. before indentation and \t insertion takes place.")
  17605. (defvar org-metaleft-hook nil
  17606. "Hook for functions attaching themselves to `M-left'.
  17607. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17608. (defvar org-metaright-hook nil
  17609. "Hook for functions attaching themselves to `M-right'.
  17610. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17611. (defvar org-metaup-hook nil
  17612. "Hook for functions attaching themselves to `M-up'.
  17613. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17614. (defvar org-metadown-hook nil
  17615. "Hook for functions attaching themselves to `M-down'.
  17616. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17617. (defvar org-shiftmetaleft-hook nil
  17618. "Hook for functions attaching themselves to `M-S-left'.
  17619. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17620. (defvar org-shiftmetaright-hook nil
  17621. "Hook for functions attaching themselves to `M-S-right'.
  17622. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17623. (defvar org-shiftmetaup-hook nil
  17624. "Hook for functions attaching themselves to `M-S-up'.
  17625. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17626. (defvar org-shiftmetadown-hook nil
  17627. "Hook for functions attaching themselves to `M-S-down'.
  17628. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17629. (defvar org-metareturn-hook nil
  17630. "Hook for functions attaching themselves to `M-RET'.
  17631. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17632. (defvar org-shiftup-hook nil
  17633. "Hook for functions attaching themselves to `S-up'.
  17634. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17635. (defvar org-shiftup-final-hook nil
  17636. "Hook for functions attaching themselves to `S-up'.
  17637. This one runs after all other options except shift-select have been excluded.
  17638. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17639. (defvar org-shiftdown-hook nil
  17640. "Hook for functions attaching themselves to `S-down'.
  17641. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17642. (defvar org-shiftdown-final-hook nil
  17643. "Hook for functions attaching themselves to `S-down'.
  17644. This one runs after all other options except shift-select have been excluded.
  17645. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17646. (defvar org-shiftleft-hook nil
  17647. "Hook for functions attaching themselves to `S-left'.
  17648. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17649. (defvar org-shiftleft-final-hook nil
  17650. "Hook for functions attaching themselves to `S-left'.
  17651. This one runs after all other options except shift-select have been excluded.
  17652. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17653. (defvar org-shiftright-hook nil
  17654. "Hook for functions attaching themselves to `S-right'.
  17655. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17656. (defvar org-shiftright-final-hook nil
  17657. "Hook for functions attaching themselves to `S-right'.
  17658. This one runs after all other options except shift-select have been excluded.
  17659. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17660. (defun org-modifier-cursor-error ()
  17661. "Throw an error, a modified cursor command was applied in wrong context."
  17662. (error "This command is active in special context like tables, headlines or items"))
  17663. (defun org-shiftselect-error ()
  17664. "Throw an error because Shift-Cursor command was applied in wrong context."
  17665. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17666. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17667. (error "This command works only in special context like headlines or timestamps")))
  17668. (defun org-call-for-shift-select (cmd)
  17669. (let ((this-command-keys-shift-translated t))
  17670. (call-interactively cmd)))
  17671. (defun org-shifttab (&optional arg)
  17672. "Global visibility cycling or move to previous table field.
  17673. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17674. on context.
  17675. See the individual commands for more information."
  17676. (interactive "P")
  17677. (cond
  17678. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17679. ((integerp arg)
  17680. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17681. (message "Content view to level: %d" arg)
  17682. (org-content (prefix-numeric-value arg2))
  17683. (setq org-cycle-global-status 'overview)))
  17684. (t (call-interactively 'org-global-cycle))))
  17685. (defun org-shiftmetaleft ()
  17686. "Promote subtree or delete table column.
  17687. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17688. `org-table-delete-column', depending on context. See the
  17689. individual commands for more information."
  17690. (interactive)
  17691. (cond
  17692. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17693. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17694. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17695. ((if (not (org-region-active-p)) (org-at-item-p)
  17696. (save-excursion (goto-char (region-beginning))
  17697. (org-at-item-p)))
  17698. (call-interactively 'org-outdent-item-tree))
  17699. (t (org-modifier-cursor-error))))
  17700. (defun org-shiftmetaright ()
  17701. "Demote subtree or insert table column.
  17702. Calls `org-demote-subtree', `org-indent-item-tree', or
  17703. `org-table-insert-column', depending on context. See the
  17704. individual commands for more information."
  17705. (interactive)
  17706. (cond
  17707. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17708. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17709. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17710. ((if (not (org-region-active-p)) (org-at-item-p)
  17711. (save-excursion (goto-char (region-beginning))
  17712. (org-at-item-p)))
  17713. (call-interactively 'org-indent-item-tree))
  17714. (t (org-modifier-cursor-error))))
  17715. (defun org-shiftmetaup (&optional arg)
  17716. "Move subtree up or kill table row.
  17717. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17718. `org-move-item-up' or `org-timestamp-up', depending on context.
  17719. See the individual commands for more information."
  17720. (interactive "P")
  17721. (cond
  17722. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17723. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17724. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17725. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17726. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17727. (call-interactively 'org-timestamp-up)))
  17728. (t (call-interactively 'org-drag-line-backward))))
  17729. (defun org-shiftmetadown (&optional arg)
  17730. "Move subtree down or insert table row.
  17731. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17732. `org-move-item-down' or `org-timestamp-up', depending on context.
  17733. See the individual commands for more information."
  17734. (interactive "P")
  17735. (cond
  17736. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17737. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17738. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17739. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17740. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17741. (call-interactively 'org-timestamp-down)))
  17742. (t (call-interactively 'org-drag-line-forward))))
  17743. (defsubst org-hidden-tree-error ()
  17744. (error
  17745. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17746. (defun org-metaleft (&optional arg)
  17747. "Promote heading or move table column to left.
  17748. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17749. With no specific context, calls the Emacs default `backward-word'.
  17750. See the individual commands for more information."
  17751. (interactive "P")
  17752. (cond
  17753. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17754. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17755. ((org-with-limited-levels
  17756. (or (org-at-heading-p)
  17757. (and (org-region-active-p)
  17758. (save-excursion
  17759. (goto-char (region-beginning))
  17760. (org-at-heading-p)))))
  17761. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17762. (call-interactively 'org-do-promote))
  17763. ;; At an inline task.
  17764. ((org-at-heading-p)
  17765. (call-interactively 'org-inlinetask-promote))
  17766. ((or (org-at-item-p)
  17767. (and (org-region-active-p)
  17768. (save-excursion
  17769. (goto-char (region-beginning))
  17770. (org-at-item-p))))
  17771. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17772. (call-interactively 'org-outdent-item))
  17773. (t (call-interactively 'backward-word))))
  17774. (defun org-metaright (&optional arg)
  17775. "Demote a subtree, a list item or move table column to right.
  17776. In front of a drawer or a block keyword, indent it correctly.
  17777. With no specific context, calls the Emacs default `forward-word'.
  17778. See the individual commands for more information."
  17779. (interactive "P")
  17780. (cond
  17781. ((run-hook-with-args-until-success 'org-metaright-hook))
  17782. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17783. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17784. ((org-at-block-p) (call-interactively 'org-indent-block))
  17785. ((org-with-limited-levels
  17786. (or (org-at-heading-p)
  17787. (and (org-region-active-p)
  17788. (save-excursion
  17789. (goto-char (region-beginning))
  17790. (org-at-heading-p)))))
  17791. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17792. (call-interactively 'org-do-demote))
  17793. ;; At an inline task.
  17794. ((org-at-heading-p)
  17795. (call-interactively 'org-inlinetask-demote))
  17796. ((or (org-at-item-p)
  17797. (and (org-region-active-p)
  17798. (save-excursion
  17799. (goto-char (region-beginning))
  17800. (org-at-item-p))))
  17801. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17802. (call-interactively 'org-indent-item))
  17803. (t (call-interactively 'forward-word))))
  17804. (defun org-check-for-hidden (what)
  17805. "Check if there are hidden headlines/items in the current visual line.
  17806. WHAT can be either `headlines' or `items'. If the current line is
  17807. an outline or item heading and it has a folded subtree below it,
  17808. this function returns t, nil otherwise."
  17809. (let ((re (cond
  17810. ((eq what 'headlines) org-outline-regexp-bol)
  17811. ((eq what 'items) (org-item-beginning-re))
  17812. (t (error "This should not happen"))))
  17813. beg end)
  17814. (save-excursion
  17815. (catch 'exit
  17816. (unless (org-region-active-p)
  17817. (setq beg (point-at-bol))
  17818. (beginning-of-line 2)
  17819. (while (and (not (eobp)) ;; this is like `next-line'
  17820. (get-char-property (1- (point)) 'invisible))
  17821. (beginning-of-line 2))
  17822. (setq end (point))
  17823. (goto-char beg)
  17824. (goto-char (point-at-eol))
  17825. (setq end (max end (point)))
  17826. (while (re-search-forward re end t)
  17827. (if (get-char-property (match-beginning 0) 'invisible)
  17828. (throw 'exit t))))
  17829. nil))))
  17830. (defun org-metaup (&optional arg)
  17831. "Move subtree up or move table row up.
  17832. Calls `org-move-subtree-up' or `org-table-move-row' or
  17833. `org-move-item-up', depending on context. See the individual commands
  17834. for more information."
  17835. (interactive "P")
  17836. (cond
  17837. ((run-hook-with-args-until-success 'org-metaup-hook))
  17838. ((org-region-active-p)
  17839. (let* ((a (min (region-beginning) (region-end)))
  17840. (b (1- (max (region-beginning) (region-end))))
  17841. (c (save-excursion (goto-char a)
  17842. (move-beginning-of-line 0)))
  17843. (d (save-excursion (goto-char a)
  17844. (move-end-of-line 0) (point))))
  17845. (transpose-regions a b c d)
  17846. (goto-char c)))
  17847. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17848. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17849. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17850. (t (org-drag-element-backward))))
  17851. (defun org-metadown (&optional arg)
  17852. "Move subtree down or move table row down.
  17853. Calls `org-move-subtree-down' or `org-table-move-row' or
  17854. `org-move-item-down', depending on context. See the individual
  17855. commands for more information."
  17856. (interactive "P")
  17857. (cond
  17858. ((run-hook-with-args-until-success 'org-metadown-hook))
  17859. ((org-region-active-p)
  17860. (let* ((a (min (region-beginning) (region-end)))
  17861. (b (max (region-beginning) (region-end)))
  17862. (c (save-excursion (goto-char b)
  17863. (move-beginning-of-line 1)))
  17864. (d (save-excursion (goto-char b)
  17865. (move-end-of-line 1) (1+ (point)))))
  17866. (transpose-regions a b c d)
  17867. (goto-char d)))
  17868. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17869. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17870. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17871. (t (org-drag-element-forward))))
  17872. (defun org-shiftup (&optional arg)
  17873. "Increase item in timestamp or increase priority of current headline.
  17874. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17875. depending on context. See the individual commands for more information."
  17876. (interactive "P")
  17877. (cond
  17878. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17879. ((and org-support-shift-select (org-region-active-p))
  17880. (org-call-for-shift-select 'previous-line))
  17881. ((org-at-timestamp-p t)
  17882. (call-interactively (if org-edit-timestamp-down-means-later
  17883. 'org-timestamp-down 'org-timestamp-up)))
  17884. ((and (not (eq org-support-shift-select 'always))
  17885. org-enable-priority-commands
  17886. (org-at-heading-p))
  17887. (call-interactively 'org-priority-up))
  17888. ((and (not org-support-shift-select) (org-at-item-p))
  17889. (call-interactively 'org-previous-item))
  17890. ((org-clocktable-try-shift 'up arg))
  17891. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17892. (org-support-shift-select
  17893. (org-call-for-shift-select 'previous-line))
  17894. (t (org-shiftselect-error))))
  17895. (defun org-shiftdown (&optional arg)
  17896. "Decrease item in timestamp or decrease priority of current headline.
  17897. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17898. depending on context. See the individual commands for more information."
  17899. (interactive "P")
  17900. (cond
  17901. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17902. ((and org-support-shift-select (org-region-active-p))
  17903. (org-call-for-shift-select 'next-line))
  17904. ((org-at-timestamp-p t)
  17905. (call-interactively (if org-edit-timestamp-down-means-later
  17906. 'org-timestamp-up 'org-timestamp-down)))
  17907. ((and (not (eq org-support-shift-select 'always))
  17908. org-enable-priority-commands
  17909. (org-at-heading-p))
  17910. (call-interactively 'org-priority-down))
  17911. ((and (not org-support-shift-select) (org-at-item-p))
  17912. (call-interactively 'org-next-item))
  17913. ((org-clocktable-try-shift 'down arg))
  17914. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17915. (org-support-shift-select
  17916. (org-call-for-shift-select 'next-line))
  17917. (t (org-shiftselect-error))))
  17918. (defun org-shiftright (&optional arg)
  17919. "Cycle the thing at point or in the current line, depending on context.
  17920. Depending on context, this does one of the following:
  17921. - switch a timestamp at point one day into the future
  17922. - on a headline, switch to the next TODO keyword.
  17923. - on an item, switch entire list to the next bullet type
  17924. - on a property line, switch to the next allowed value
  17925. - on a clocktable definition line, move time block into the future"
  17926. (interactive "P")
  17927. (cond
  17928. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17929. ((and org-support-shift-select (org-region-active-p))
  17930. (org-call-for-shift-select 'forward-char))
  17931. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17932. ((and (not (eq org-support-shift-select 'always))
  17933. (org-at-heading-p))
  17934. (let ((org-inhibit-logging
  17935. (not org-treat-S-cursor-todo-selection-as-state-change))
  17936. (org-inhibit-blocking
  17937. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17938. (org-call-with-arg 'org-todo 'right)))
  17939. ((or (and org-support-shift-select
  17940. (not (eq org-support-shift-select 'always))
  17941. (org-at-item-bullet-p))
  17942. (and (not org-support-shift-select) (org-at-item-p)))
  17943. (org-call-with-arg 'org-cycle-list-bullet nil))
  17944. ((and (not (eq org-support-shift-select 'always))
  17945. (org-at-property-p))
  17946. (call-interactively 'org-property-next-allowed-value))
  17947. ((org-clocktable-try-shift 'right arg))
  17948. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17949. (org-support-shift-select
  17950. (org-call-for-shift-select 'forward-char))
  17951. (t (org-shiftselect-error))))
  17952. (defun org-shiftleft (&optional arg)
  17953. "Cycle the thing at point or in the current line, depending on context.
  17954. Depending on context, this does one of the following:
  17955. - switch a timestamp at point one day into the past
  17956. - on a headline, switch to the previous TODO keyword.
  17957. - on an item, switch entire list to the previous bullet type
  17958. - on a property line, switch to the previous allowed value
  17959. - on a clocktable definition line, move time block into the past"
  17960. (interactive "P")
  17961. (cond
  17962. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17963. ((and org-support-shift-select (org-region-active-p))
  17964. (org-call-for-shift-select 'backward-char))
  17965. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17966. ((and (not (eq org-support-shift-select 'always))
  17967. (org-at-heading-p))
  17968. (let ((org-inhibit-logging
  17969. (not org-treat-S-cursor-todo-selection-as-state-change))
  17970. (org-inhibit-blocking
  17971. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17972. (org-call-with-arg 'org-todo 'left)))
  17973. ((or (and org-support-shift-select
  17974. (not (eq org-support-shift-select 'always))
  17975. (org-at-item-bullet-p))
  17976. (and (not org-support-shift-select) (org-at-item-p)))
  17977. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17978. ((and (not (eq org-support-shift-select 'always))
  17979. (org-at-property-p))
  17980. (call-interactively 'org-property-previous-allowed-value))
  17981. ((org-clocktable-try-shift 'left arg))
  17982. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17983. (org-support-shift-select
  17984. (org-call-for-shift-select 'backward-char))
  17985. (t (org-shiftselect-error))))
  17986. (defun org-shiftcontrolright ()
  17987. "Switch to next TODO set."
  17988. (interactive)
  17989. (cond
  17990. ((and org-support-shift-select (org-region-active-p))
  17991. (org-call-for-shift-select 'forward-word))
  17992. ((and (not (eq org-support-shift-select 'always))
  17993. (org-at-heading-p))
  17994. (org-call-with-arg 'org-todo 'nextset))
  17995. (org-support-shift-select
  17996. (org-call-for-shift-select 'forward-word))
  17997. (t (org-shiftselect-error))))
  17998. (defun org-shiftcontrolleft ()
  17999. "Switch to previous TODO set."
  18000. (interactive)
  18001. (cond
  18002. ((and org-support-shift-select (org-region-active-p))
  18003. (org-call-for-shift-select 'backward-word))
  18004. ((and (not (eq org-support-shift-select 'always))
  18005. (org-at-heading-p))
  18006. (org-call-with-arg 'org-todo 'previousset))
  18007. (org-support-shift-select
  18008. (org-call-for-shift-select 'backward-word))
  18009. (t (org-shiftselect-error))))
  18010. (defun org-shiftcontrolup (&optional n)
  18011. "Change timestamps synchronously up in CLOCK log lines.
  18012. Optional argument N tells to change by that many units."
  18013. (interactive "P")
  18014. (cond ((and (not org-support-shift-select)
  18015. (org-at-clock-log-p)
  18016. (org-at-timestamp-p t))
  18017. (org-clock-timestamps-up n))
  18018. (t (org-shiftselect-error))))
  18019. (defun org-shiftcontroldown (&optional n)
  18020. "Change timestamps synchronously down in CLOCK log lines.
  18021. Optional argument N tells to change by that many units."
  18022. (interactive "P")
  18023. (cond ((and (not org-support-shift-select)
  18024. (org-at-clock-log-p)
  18025. (org-at-timestamp-p t))
  18026. (org-clock-timestamps-down n))
  18027. (t (org-shiftselect-error))))
  18028. (defun org-ctrl-c-ret ()
  18029. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18030. (interactive)
  18031. (cond
  18032. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18033. (t (call-interactively 'org-insert-heading))))
  18034. (defun org-find-visible ()
  18035. (let ((s (point)))
  18036. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18037. (get-char-property s 'invisible)))
  18038. s))
  18039. (defun org-find-invisible ()
  18040. (let ((s (point)))
  18041. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18042. (not (get-char-property s 'invisible))))
  18043. s))
  18044. (defun org-copy-visible (beg end)
  18045. "Copy the visible parts of the region."
  18046. (interactive "r")
  18047. (let (snippets s)
  18048. (save-excursion
  18049. (save-restriction
  18050. (narrow-to-region beg end)
  18051. (setq s (goto-char (point-min)))
  18052. (while (not (= (point) (point-max)))
  18053. (goto-char (org-find-invisible))
  18054. (push (buffer-substring s (point)) snippets)
  18055. (setq s (goto-char (org-find-visible))))))
  18056. (kill-new (apply 'concat (nreverse snippets)))))
  18057. (defun org-copy-special ()
  18058. "Copy region in table or copy current subtree.
  18059. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18060. See the individual commands for more information."
  18061. (interactive)
  18062. (call-interactively
  18063. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18064. (defun org-cut-special ()
  18065. "Cut region in table or cut current subtree.
  18066. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18067. See the individual commands for more information."
  18068. (interactive)
  18069. (call-interactively
  18070. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18071. (defun org-paste-special (arg)
  18072. "Paste rectangular region into table, or past subtree relative to level.
  18073. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18074. See the individual commands for more information."
  18075. (interactive "P")
  18076. (if (org-at-table-p)
  18077. (org-table-paste-rectangle)
  18078. (org-paste-subtree arg)))
  18079. (defsubst org-in-fixed-width-region-p ()
  18080. "Is point in a fixed-width region?"
  18081. (save-match-data
  18082. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18083. (defun org-edit-special (&optional arg)
  18084. "Call a special editor for the element at point.
  18085. When at a table, call the formula editor with `org-table-edit-formulas'.
  18086. When in a source code block, call `org-edit-src-code'.
  18087. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18088. When at an #+INCLUDE keyword, visit the included file.
  18089. On a link, call `ffap' to visit the link at point.
  18090. Otherwise, return a user error."
  18091. (interactive)
  18092. (let ((element (org-element-at-point)))
  18093. (assert (not buffer-read-only) nil
  18094. "Buffer is read-only: %s" (buffer-name))
  18095. (case (org-element-type element)
  18096. (src-block
  18097. (if (not arg) (org-edit-src-code)
  18098. (let* ((info (org-babel-get-src-block-info))
  18099. (lang (nth 0 info))
  18100. (params (nth 2 info))
  18101. (session (cdr (assq :session params))))
  18102. (if (not session) (org-edit-src-code)
  18103. ;; At a src-block with a session and function called with
  18104. ;; an ARG: switch to the buffer related to the inferior
  18105. ;; process.
  18106. (funcall (intern (concat "org-babel-prep-session:" lang))
  18107. session params)))))
  18108. (keyword
  18109. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18110. (find-file
  18111. (org-remove-double-quotes
  18112. (car (org-split-string (org-element-property :value element)))))
  18113. (user-error "No special environment to edit here")))
  18114. (table
  18115. (if (eq (org-element-property :type element) 'table.el)
  18116. (org-edit-src-code)
  18117. (call-interactively 'org-table-edit-formulas)))
  18118. ;; Only Org tables contain `table-row' type elements.
  18119. (table-row (call-interactively 'org-table-edit-formulas))
  18120. ((example-block export-block) (org-edit-src-code))
  18121. (fixed-width (org-edit-fixed-width-region))
  18122. (otherwise
  18123. ;; No notable element at point. Though, we may be at a link,
  18124. ;; which is an object. Thus, scan deeper.
  18125. (if (eq (org-element-type (org-element-context element)) 'link)
  18126. (call-interactively 'ffap)
  18127. (user-error "No special environment to edit here"))))))
  18128. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18129. (defun org-ctrl-c-ctrl-c (&optional arg)
  18130. "Set tags in headline, or update according to changed information at point.
  18131. This command does many different things, depending on context:
  18132. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18133. this is what we do.
  18134. - If the cursor is on a statistics cookie, update it.
  18135. - If the cursor is in a headline, prompt for tags and insert them
  18136. into the current line, aligned to `org-tags-column'. When called
  18137. with prefix arg, realign all tags in the current buffer.
  18138. - If the cursor is in one of the special #+KEYWORD lines, this
  18139. triggers scanning the buffer for these lines and updating the
  18140. information.
  18141. - If the cursor is inside a table, realign the table. This command
  18142. works even if the automatic table editor has been turned off.
  18143. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18144. the entire table.
  18145. - If the cursor is at a footnote reference or definition, jump to
  18146. the corresponding definition or references, respectively.
  18147. - If the cursor is a the beginning of a dynamic block, update it.
  18148. - If the current buffer is a capture buffer, close note and file it.
  18149. - If the cursor is on a <<<target>>>, update radio targets and
  18150. corresponding links in this buffer.
  18151. - If the cursor is on a numbered item in a plain list, renumber the
  18152. ordered list.
  18153. - If the cursor is on a checkbox, toggle it.
  18154. - If the cursor is on a code block, evaluate it. The variable
  18155. `org-confirm-babel-evaluate' can be used to control prompting
  18156. before code block evaluation, by default every code block
  18157. evaluation requires confirmation. Code block evaluation can be
  18158. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18159. (interactive "P")
  18160. (cond
  18161. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18162. org-occur-highlights
  18163. org-latex-fragment-image-overlays)
  18164. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18165. (org-remove-occur-highlights)
  18166. (org-remove-latex-fragment-image-overlays)
  18167. (message "Temporary highlights/overlays removed from current buffer"))
  18168. ((and (local-variable-p 'org-finish-function (current-buffer))
  18169. (fboundp org-finish-function))
  18170. (funcall org-finish-function))
  18171. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18172. (t
  18173. (let* ((context (org-element-context)) (type (org-element-type context)))
  18174. ;; Test if point is within a blank line.
  18175. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18176. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18177. (user-error "C-c C-c can do nothing useful at this location"))
  18178. ;; For convenience: at the first line of a paragraph on the
  18179. ;; same line as an item, apply function on that item instead.
  18180. (when (eq type 'paragraph)
  18181. (let ((parent (org-element-property :parent context)))
  18182. (when (and (eq (org-element-type parent) 'item)
  18183. (= (point-at-bol) (org-element-property :begin parent)))
  18184. (setq context parent type 'item))))
  18185. ;; Act according to type of element or object at point.
  18186. (case type
  18187. (clock (org-clock-update-time-maybe))
  18188. (dynamic-block
  18189. (save-excursion
  18190. (goto-char (org-element-property :post-affiliated context))
  18191. (org-update-dblock)))
  18192. (footnote-definition
  18193. (goto-char (org-element-property :post-affiliated context))
  18194. (call-interactively 'org-footnote-action))
  18195. (footnote-reference (call-interactively 'org-footnote-action))
  18196. ((headline inlinetask)
  18197. (save-excursion (goto-char (org-element-property :begin context))
  18198. (call-interactively 'org-set-tags)))
  18199. (item
  18200. ;; At an item: a double C-u set checkbox to "[-]"
  18201. ;; unconditionally, whereas a single one will toggle its
  18202. ;; presence. Without an universal argument, if the item
  18203. ;; has a checkbox, toggle it. Otherwise repair the list.
  18204. (let* ((box (org-element-property :checkbox context))
  18205. (struct (org-element-property :structure context))
  18206. (old-struct (copy-tree struct))
  18207. (parents (org-list-parents-alist struct))
  18208. (prevs (org-list-prevs-alist struct))
  18209. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18210. (org-list-set-checkbox
  18211. (org-element-property :begin context) struct
  18212. (cond ((equal arg '(16)) "[-]")
  18213. ((and (not box) (equal arg '(4))) "[ ]")
  18214. ((or (not box) (equal arg '(4))) nil)
  18215. ((eq box 'on) "[ ]")
  18216. (t "[X]")))
  18217. ;; Mimic `org-list-write-struct' but with grabbing
  18218. ;; a return value from `org-list-struct-fix-box'.
  18219. (org-list-struct-fix-ind struct parents 2)
  18220. (org-list-struct-fix-item-end struct)
  18221. (org-list-struct-fix-bul struct prevs)
  18222. (org-list-struct-fix-ind struct parents)
  18223. (let ((block-item
  18224. (org-list-struct-fix-box struct parents prevs orderedp)))
  18225. (if (and box (equal struct old-struct))
  18226. (if (equal arg '(16))
  18227. (message "Checkboxes already reset")
  18228. (user-error "Cannot toggle this checkbox: %s"
  18229. (if (eq box 'on)
  18230. "all subitems checked"
  18231. "unchecked subitems")))
  18232. (org-list-struct-apply-struct struct old-struct)
  18233. (org-update-checkbox-count-maybe))
  18234. (when block-item
  18235. (message "Checkboxes were removed due to empty box at line %d"
  18236. (org-current-line block-item))))))
  18237. (keyword
  18238. (let ((org-inhibit-startup-visibility-stuff t)
  18239. (org-startup-align-all-tables nil))
  18240. (when (boundp 'org-table-coordinate-overlays)
  18241. (mapc 'delete-overlay org-table-coordinate-overlays)
  18242. (setq org-table-coordinate-overlays nil))
  18243. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18244. (message "Local setup has been refreshed"))
  18245. (plain-list
  18246. ;; At a plain list, with a double C-u argument, set
  18247. ;; checkboxes of each item to "[-]", whereas a single one
  18248. ;; will toggle their presence according to the state of the
  18249. ;; first item in the list. Without an argument, repair the
  18250. ;; list.
  18251. (let* ((begin (org-element-property :contents-begin context))
  18252. (struct (org-element-property :structure context))
  18253. (old-struct (copy-tree struct))
  18254. (first-box (save-excursion
  18255. (goto-char begin)
  18256. (looking-at org-list-full-item-re)
  18257. (match-string-no-properties 3)))
  18258. (new-box (cond ((equal arg '(16)) "[-]")
  18259. ((equal arg '(4)) (unless first-box "[ ]"))
  18260. ((equal first-box "[X]") "[ ]")
  18261. (t "[X]"))))
  18262. (cond
  18263. (arg
  18264. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18265. (org-list-get-all-items
  18266. begin struct (org-list-prevs-alist struct))))
  18267. ((and first-box (eq (point) begin))
  18268. ;; For convenience, when point is at bol on the first
  18269. ;; item of the list and no argument is provided, simply
  18270. ;; toggle checkbox of that item, if any.
  18271. (org-list-set-checkbox begin struct new-box)))
  18272. (org-list-write-struct
  18273. struct (org-list-parents-alist struct) old-struct)
  18274. (org-update-checkbox-count-maybe)
  18275. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18276. ((property-drawer node-property)
  18277. (call-interactively 'org-property-action))
  18278. ((radio-target target)
  18279. (call-interactively 'org-update-radio-target-regexp))
  18280. (statistics-cookie
  18281. (call-interactively 'org-update-statistics-cookies))
  18282. ((table table-cell table-row)
  18283. ;; At a table, recalculate every field and align it. Also
  18284. ;; send the table if necessary. If the table has
  18285. ;; a `table.el' type, just give up. At a table row or
  18286. ;; cell, maybe recalculate line but always align table.
  18287. (if (eq (org-element-property :type context) 'table.el)
  18288. (message "Use C-c ' to edit table.el tables")
  18289. (let ((org-enable-table-editor t))
  18290. (if (or (eq type 'table)
  18291. ;; Check if point is at a TBLFM line.
  18292. (and (eq type 'table-row)
  18293. (= (point) (org-element-property :end context))))
  18294. (save-excursion
  18295. (goto-char (org-element-property :contents-begin context))
  18296. (org-call-with-arg 'org-table-recalculate (or arg t))
  18297. (orgtbl-send-table 'maybe))
  18298. (org-table-maybe-eval-formula)
  18299. (cond (arg (call-interactively 'org-table-recalculate))
  18300. ((org-table-maybe-recalculate-line))
  18301. (t (org-table-align)))))))
  18302. (timestamp (org-timestamp-change 0 'day))
  18303. (otherwise
  18304. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18305. (user-error
  18306. "C-c C-c can do nothing useful at this location")))))))))
  18307. (defun org-mode-restart ()
  18308. "Restart Org-mode, to scan again for special lines.
  18309. Also updates the keyword regular expressions."
  18310. (interactive)
  18311. (org-mode)
  18312. (message "Org-mode restarted"))
  18313. (defun org-kill-note-or-show-branches ()
  18314. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18315. (interactive)
  18316. (if (not org-finish-function)
  18317. (progn
  18318. (hide-subtree)
  18319. (call-interactively 'show-branches))
  18320. (let ((org-note-abort t))
  18321. (funcall org-finish-function))))
  18322. (defun org-return (&optional indent)
  18323. "Goto next table row or insert a newline.
  18324. Calls `org-table-next-row' or `newline', depending on context.
  18325. See the individual commands for more information."
  18326. (interactive)
  18327. (let (org-ts-what)
  18328. (cond
  18329. ((or (bobp) (org-in-src-block-p))
  18330. (if indent (newline-and-indent) (newline)))
  18331. ((org-at-table-p)
  18332. (org-table-justify-field-maybe)
  18333. (call-interactively 'org-table-next-row))
  18334. ;; when `newline-and-indent' is called within a list, make sure
  18335. ;; text moved stays inside the item.
  18336. ((and (org-in-item-p) indent)
  18337. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18338. (progn
  18339. (save-match-data (newline))
  18340. (org-indent-line-to (length (match-string 0))))
  18341. (let ((ind (org-get-indentation)))
  18342. (newline)
  18343. (if (org-looking-back org-list-end-re)
  18344. (org-indent-line)
  18345. (org-indent-line-to ind)))))
  18346. ((and org-return-follows-link
  18347. (org-at-timestamp-p t)
  18348. (not (eq org-ts-what 'after)))
  18349. (org-follow-timestamp-link))
  18350. ((and org-return-follows-link
  18351. (let ((tprop (get-text-property (point) 'face)))
  18352. (or (eq tprop 'org-link)
  18353. (and (listp tprop) (memq 'org-link tprop)))))
  18354. (call-interactively 'org-open-at-point))
  18355. ((and (org-at-heading-p)
  18356. (looking-at
  18357. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18358. (org-show-entry)
  18359. (end-of-line 1)
  18360. (newline))
  18361. (t (if indent (newline-and-indent) (newline))))))
  18362. (defun org-return-indent ()
  18363. "Goto next table row or insert a newline and indent.
  18364. Calls `org-table-next-row' or `newline-and-indent', depending on
  18365. context. See the individual commands for more information."
  18366. (interactive)
  18367. (org-return t))
  18368. (defun org-ctrl-c-star ()
  18369. "Compute table, or change heading status of lines.
  18370. Calls `org-table-recalculate' or `org-toggle-heading',
  18371. depending on context."
  18372. (interactive)
  18373. (cond
  18374. ((org-at-table-p)
  18375. (call-interactively 'org-table-recalculate))
  18376. (t
  18377. ;; Convert all lines in region to list items
  18378. (call-interactively 'org-toggle-heading))))
  18379. (defun org-ctrl-c-minus ()
  18380. "Insert separator line in table or modify bullet status of line.
  18381. Also turns a plain line or a region of lines into list items.
  18382. Calls `org-table-insert-hline', `org-toggle-item', or
  18383. `org-cycle-list-bullet', depending on context."
  18384. (interactive)
  18385. (cond
  18386. ((org-at-table-p)
  18387. (call-interactively 'org-table-insert-hline))
  18388. ((org-region-active-p)
  18389. (call-interactively 'org-toggle-item))
  18390. ((org-in-item-p)
  18391. (call-interactively 'org-cycle-list-bullet))
  18392. (t
  18393. (call-interactively 'org-toggle-item))))
  18394. (defun org-toggle-item (arg)
  18395. "Convert headings or normal lines to items, items to normal lines.
  18396. If there is no active region, only the current line is considered.
  18397. If the first non blank line in the region is a headline, convert
  18398. all headlines to items, shifting text accordingly.
  18399. If it is an item, convert all items to normal lines.
  18400. If it is normal text, change region into an item. With a prefix
  18401. argument ARG, change each line in region into an item."
  18402. (interactive "P")
  18403. (let ((shift-text
  18404. (function
  18405. ;; Shift text in current section to IND, from point to END.
  18406. ;; The function leaves point to END line.
  18407. (lambda (ind end)
  18408. (let ((min-i 1000) (end (copy-marker end)))
  18409. ;; First determine the minimum indentation (MIN-I) of
  18410. ;; the text.
  18411. (save-excursion
  18412. (catch 'exit
  18413. (while (< (point) end)
  18414. (let ((i (org-get-indentation)))
  18415. (cond
  18416. ;; Skip blank lines and inline tasks.
  18417. ((looking-at "^[ \t]*$"))
  18418. ((looking-at org-outline-regexp-bol))
  18419. ;; We can't find less than 0 indentation.
  18420. ((zerop i) (throw 'exit (setq min-i 0)))
  18421. ((< i min-i) (setq min-i i))))
  18422. (forward-line))))
  18423. ;; Then indent each line so that a line indented to
  18424. ;; MIN-I becomes indented to IND. Ignore blank lines
  18425. ;; and inline tasks in the process.
  18426. (let ((delta (- ind min-i)))
  18427. (while (< (point) end)
  18428. (unless (or (looking-at "^[ \t]*$")
  18429. (looking-at org-outline-regexp-bol))
  18430. (org-indent-line-to (+ (org-get-indentation) delta)))
  18431. (forward-line)))))))
  18432. (skip-blanks
  18433. (function
  18434. ;; Return beginning of first non-blank line, starting from
  18435. ;; line at POS.
  18436. (lambda (pos)
  18437. (save-excursion
  18438. (goto-char pos)
  18439. (skip-chars-forward " \r\t\n")
  18440. (point-at-bol)))))
  18441. beg end)
  18442. ;; Determine boundaries of changes.
  18443. (if (org-region-active-p)
  18444. (setq beg (funcall skip-blanks (region-beginning))
  18445. end (copy-marker (region-end)))
  18446. (setq beg (funcall skip-blanks (point-at-bol))
  18447. end (copy-marker (point-at-eol))))
  18448. ;; Depending on the starting line, choose an action on the text
  18449. ;; between BEG and END.
  18450. (org-with-limited-levels
  18451. (save-excursion
  18452. (goto-char beg)
  18453. (cond
  18454. ;; Case 1. Start at an item: de-itemize. Note that it only
  18455. ;; happens when a region is active: `org-ctrl-c-minus'
  18456. ;; would call `org-cycle-list-bullet' otherwise.
  18457. ((org-at-item-p)
  18458. (while (< (point) end)
  18459. (when (org-at-item-p)
  18460. (skip-chars-forward " \t")
  18461. (delete-region (point) (match-end 0)))
  18462. (forward-line)))
  18463. ;; Case 2. Start at an heading: convert to items.
  18464. ((org-at-heading-p)
  18465. (let* ((bul (org-list-bullet-string "-"))
  18466. (bul-len (length bul))
  18467. ;; Indentation of the first heading. It should be
  18468. ;; relative to the indentation of its parent, if any.
  18469. (start-ind (save-excursion
  18470. (cond
  18471. ((not org-adapt-indentation) 0)
  18472. ((not (outline-previous-heading)) 0)
  18473. (t (length (match-string 0))))))
  18474. ;; Level of first heading. Further headings will be
  18475. ;; compared to it to determine hierarchy in the list.
  18476. (ref-level (org-reduced-level (org-outline-level))))
  18477. (while (< (point) end)
  18478. (let* ((level (org-reduced-level (org-outline-level)))
  18479. (delta (max 0 (- level ref-level))))
  18480. ;; If current headline is less indented than the first
  18481. ;; one, set it as reference, in order to preserve
  18482. ;; subtrees.
  18483. (when (< level ref-level) (setq ref-level level))
  18484. (replace-match bul t t)
  18485. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18486. ;; Ensure all text down to END (or SECTION-END) belongs
  18487. ;; to the newly created item.
  18488. (let ((section-end (save-excursion
  18489. (or (outline-next-heading) (point)))))
  18490. (forward-line)
  18491. (funcall shift-text
  18492. (+ start-ind (* (1+ delta) bul-len))
  18493. (min end section-end)))))))
  18494. ;; Case 3. Normal line with ARG: turn each non-item line into
  18495. ;; an item.
  18496. (arg
  18497. (while (< (point) end)
  18498. (unless (or (org-at-heading-p) (org-at-item-p))
  18499. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18500. (replace-match
  18501. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18502. (forward-line)))
  18503. ;; Case 4. Normal line without ARG: make the first line of
  18504. ;; region an item, and shift indentation of others
  18505. ;; lines to set them as item's body.
  18506. (t (let* ((bul (org-list-bullet-string "-"))
  18507. (bul-len (length bul))
  18508. (ref-ind (org-get-indentation)))
  18509. (skip-chars-forward " \t")
  18510. (insert bul)
  18511. (forward-line)
  18512. (while (< (point) end)
  18513. ;; Ensure that lines less indented than first one
  18514. ;; still get included in item body.
  18515. (funcall shift-text
  18516. (+ ref-ind bul-len)
  18517. (min end (save-excursion (or (outline-next-heading)
  18518. (point)))))
  18519. (forward-line)))))))))
  18520. (defun org-toggle-heading (&optional nstars)
  18521. "Convert headings to normal text, or items or text to headings.
  18522. If there is no active region, only the current line is considered.
  18523. With a \\[universal-argument] prefix, convert the whole list at
  18524. point into heading.
  18525. In a region:
  18526. - If the first non blank line is a headline, remove the stars
  18527. from all headlines in the region.
  18528. - If it is a normal line turn each and every normal line (i.e. not an
  18529. heading or an item) in the region into a heading.
  18530. - If it is a plain list item, turn all plain list items into headings.
  18531. When converting a line into a heading, the number of stars is chosen
  18532. such that the lines become children of the current entry. However,
  18533. when a prefix argument is given, its value determines the number of
  18534. stars to add."
  18535. (interactive "P")
  18536. (let ((skip-blanks
  18537. (function
  18538. ;; Return beginning of first non-blank line, starting from
  18539. ;; line at POS.
  18540. (lambda (pos)
  18541. (save-excursion
  18542. (goto-char pos)
  18543. (while (org-at-comment-p) (forward-line))
  18544. (skip-chars-forward " \r\t\n")
  18545. (point-at-bol)))))
  18546. beg end toggled)
  18547. ;; Determine boundaries of changes. If a universal prefix has
  18548. ;; been given, put the list in a region. If region ends at a bol,
  18549. ;; do not consider the last line to be in the region.
  18550. (when (and current-prefix-arg (org-at-item-p))
  18551. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18552. (org-mark-element))
  18553. (if (org-region-active-p)
  18554. (setq beg (funcall skip-blanks (region-beginning))
  18555. end (copy-marker (save-excursion
  18556. (goto-char (region-end))
  18557. (if (bolp) (point) (point-at-eol)))))
  18558. (setq beg (funcall skip-blanks (point-at-bol))
  18559. end (copy-marker (point-at-eol))))
  18560. ;; Ensure inline tasks don't count as headings.
  18561. (org-with-limited-levels
  18562. (save-excursion
  18563. (goto-char beg)
  18564. (cond
  18565. ;; Case 1. Started at an heading: de-star headings.
  18566. ((org-at-heading-p)
  18567. (while (< (point) end)
  18568. (when (org-at-heading-p t)
  18569. (looking-at org-outline-regexp) (replace-match "")
  18570. (setq toggled t))
  18571. (forward-line)))
  18572. ;; Case 2. Started at an item: change items into headlines.
  18573. ;; One star will be added by `org-list-to-subtree'.
  18574. ((org-at-item-p)
  18575. (let* ((stars (make-string
  18576. (if nstars
  18577. ;; subtract the star that will be added again by
  18578. ;; `org-list-to-subtree'
  18579. (1- (prefix-numeric-value current-prefix-arg))
  18580. (or (org-current-level) 0))
  18581. ?*))
  18582. (add-stars
  18583. (cond (nstars "") ; stars from prefix only
  18584. ((equal stars "") "") ; before first heading
  18585. (org-odd-levels-only "*") ; inside heading, odd
  18586. (t "")))) ; inside heading, oddeven
  18587. (while (< (point) end)
  18588. (when (org-at-item-p)
  18589. ;; Pay attention to cases when region ends before list.
  18590. (let* ((struct (org-list-struct))
  18591. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18592. (save-restriction
  18593. (narrow-to-region (point) list-end)
  18594. (insert
  18595. (org-list-to-subtree
  18596. (org-list-parse-list t)
  18597. '(:istart (concat stars add-stars (funcall get-stars depth))
  18598. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18599. (setq toggled t))
  18600. (forward-line))))
  18601. ;; Case 3. Started at normal text: make every line an heading,
  18602. ;; skipping headlines and items.
  18603. (t (let* ((stars (make-string
  18604. (if nstars
  18605. (prefix-numeric-value current-prefix-arg)
  18606. (or (org-current-level) 0))
  18607. ?*))
  18608. (add-stars
  18609. (cond (nstars "") ; stars from prefix only
  18610. ((equal stars "") "*") ; before first heading
  18611. (org-odd-levels-only "**") ; inside heading, odd
  18612. (t "*"))) ; inside heading, oddeven
  18613. (rpl (concat stars add-stars " ")))
  18614. (while (< (point) end)
  18615. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18616. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18617. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18618. (forward-line)))))))
  18619. (unless toggled (message "Cannot toggle heading from here"))))
  18620. (defun org-meta-return (&optional arg)
  18621. "Insert a new heading or wrap a region in a table.
  18622. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18623. See the individual commands for more information."
  18624. (interactive "P")
  18625. (cond
  18626. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18627. ((or (org-at-drawer-p) (org-at-property-p))
  18628. (newline-and-indent))
  18629. ((org-at-table-p)
  18630. (call-interactively 'org-table-wrap-region))
  18631. (t (call-interactively 'org-insert-heading))))
  18632. ;;; Menu entries
  18633. (defsubst org-in-subtree-not-table-p ()
  18634. "Are we in a subtree and not in a table?"
  18635. (and (not (org-before-first-heading-p))
  18636. (not (org-at-table-p))))
  18637. ;; Define the Org-mode menus
  18638. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18639. '("Tbl"
  18640. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18641. ["Next Field" org-cycle (org-at-table-p)]
  18642. ["Previous Field" org-shifttab (org-at-table-p)]
  18643. ["Next Row" org-return (org-at-table-p)]
  18644. "--"
  18645. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18646. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18647. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18648. "--"
  18649. ("Column"
  18650. ["Move Column Left" org-metaleft (org-at-table-p)]
  18651. ["Move Column Right" org-metaright (org-at-table-p)]
  18652. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18653. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18654. ("Row"
  18655. ["Move Row Up" org-metaup (org-at-table-p)]
  18656. ["Move Row Down" org-metadown (org-at-table-p)]
  18657. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18658. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18659. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18660. "--"
  18661. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18662. ("Rectangle"
  18663. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18664. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18665. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18666. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18667. "--"
  18668. ("Calculate"
  18669. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18670. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18671. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18672. "--"
  18673. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18674. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18675. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18676. "--"
  18677. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18678. "--"
  18679. ["Sum Column/Rectangle" org-table-sum
  18680. (or (org-at-table-p) (org-region-active-p))]
  18681. ["Which Column?" org-table-current-column (org-at-table-p)])
  18682. ["Debug Formulas"
  18683. org-table-toggle-formula-debugger
  18684. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18685. ["Show Col/Row Numbers"
  18686. org-table-toggle-coordinate-overlays
  18687. :style toggle
  18688. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18689. "--"
  18690. ["Create" org-table-create (and (not (org-at-table-p))
  18691. org-enable-table-editor)]
  18692. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18693. ["Import from File" org-table-import (not (org-at-table-p))]
  18694. ["Export to File" org-table-export (org-at-table-p)]
  18695. "--"
  18696. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18697. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18698. '("Org"
  18699. ("Show/Hide"
  18700. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18701. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18702. ["Sparse Tree..." org-sparse-tree t]
  18703. ["Reveal Context" org-reveal t]
  18704. ["Show All" show-all t]
  18705. "--"
  18706. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18707. "--"
  18708. ["New Heading" org-insert-heading t]
  18709. ("Navigate Headings"
  18710. ["Up" outline-up-heading t]
  18711. ["Next" outline-next-visible-heading t]
  18712. ["Previous" outline-previous-visible-heading t]
  18713. ["Next Same Level" outline-forward-same-level t]
  18714. ["Previous Same Level" outline-backward-same-level t]
  18715. "--"
  18716. ["Jump" org-goto t])
  18717. ("Edit Structure"
  18718. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18719. "--"
  18720. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18721. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18722. "--"
  18723. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18724. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18725. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18726. "--"
  18727. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18728. "--"
  18729. ["Copy visible text" org-copy-visible t]
  18730. "--"
  18731. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18732. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18733. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18734. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18735. "--"
  18736. ["Sort Region/Children" org-sort t]
  18737. "--"
  18738. ["Convert to odd levels" org-convert-to-odd-levels t]
  18739. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18740. ("Editing"
  18741. ["Emphasis..." org-emphasize t]
  18742. ["Edit Source Example" org-edit-special t]
  18743. "--"
  18744. ["Footnote new/jump" org-footnote-action t]
  18745. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18746. ("Archive"
  18747. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18748. "--"
  18749. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18750. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18751. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18752. )
  18753. "--"
  18754. ("Hyperlinks"
  18755. ["Store Link (Global)" org-store-link t]
  18756. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18757. ["Insert Link" org-insert-link t]
  18758. ["Follow Link" org-open-at-point t]
  18759. "--"
  18760. ["Next link" org-next-link t]
  18761. ["Previous link" org-previous-link t]
  18762. "--"
  18763. ["Descriptive Links"
  18764. org-toggle-link-display
  18765. :style radio
  18766. :selected org-descriptive-links
  18767. ]
  18768. ["Literal Links"
  18769. org-toggle-link-display
  18770. :style radio
  18771. :selected (not org-descriptive-links)])
  18772. "--"
  18773. ("TODO Lists"
  18774. ["TODO/DONE/-" org-todo t]
  18775. ("Select keyword"
  18776. ["Next keyword" org-shiftright (org-at-heading-p)]
  18777. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18778. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18779. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18780. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18781. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18782. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18783. "--"
  18784. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18785. :selected org-enforce-todo-dependencies :style toggle :active t]
  18786. "Settings for tree at point"
  18787. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18788. :selected (org-entry-get nil "ORDERED")
  18789. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18790. ["Do Children parallel" org-toggle-ordered-property :style radio
  18791. :selected (not (org-entry-get nil "ORDERED"))
  18792. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18793. "--"
  18794. ["Set Priority" org-priority t]
  18795. ["Priority Up" org-shiftup t]
  18796. ["Priority Down" org-shiftdown t]
  18797. "--"
  18798. ["Get news from all feeds" org-feed-update-all t]
  18799. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18800. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18801. ("TAGS and Properties"
  18802. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18803. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18804. "--"
  18805. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18806. ["Column view of properties" org-columns t]
  18807. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18808. ("Dates and Scheduling"
  18809. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18810. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18811. ("Change Date"
  18812. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18813. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18814. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18815. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18816. ["Compute Time Range" org-evaluate-time-range t]
  18817. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18818. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18819. "--"
  18820. ["Custom time format" org-toggle-time-stamp-overlays
  18821. :style radio :selected org-display-custom-times]
  18822. "--"
  18823. ["Goto Calendar" org-goto-calendar t]
  18824. ["Date from Calendar" org-date-from-calendar t]
  18825. "--"
  18826. ["Start/Restart Timer" org-timer-start t]
  18827. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18828. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18829. ["Insert Timer String" org-timer t]
  18830. ["Insert Timer Item" org-timer-item t])
  18831. ("Logging work"
  18832. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18833. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18834. ["Clock out" org-clock-out t]
  18835. ["Clock cancel" org-clock-cancel t]
  18836. "--"
  18837. ["Mark as default task" org-clock-mark-default-task t]
  18838. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18839. ["Goto running clock" org-clock-goto t]
  18840. "--"
  18841. ["Display times" org-clock-display t]
  18842. ["Create clock table" org-clock-report t]
  18843. "--"
  18844. ["Record DONE time"
  18845. (progn (setq org-log-done (not org-log-done))
  18846. (message "Switching to %s will %s record a timestamp"
  18847. (car org-done-keywords)
  18848. (if org-log-done "automatically" "not")))
  18849. :style toggle :selected org-log-done])
  18850. "--"
  18851. ["Agenda Command..." org-agenda t]
  18852. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18853. ("File List for Agenda")
  18854. ("Special views current file"
  18855. ["TODO Tree" org-show-todo-tree t]
  18856. ["Check Deadlines" org-check-deadlines t]
  18857. ["Timeline" org-timeline t]
  18858. ["Tags/Property tree" org-match-sparse-tree t])
  18859. "--"
  18860. ["Export/Publish..." org-export-dispatch t]
  18861. ("LaTeX"
  18862. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18863. :selected org-cdlatex-mode]
  18864. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18865. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18866. ["Modify math symbol" org-cdlatex-math-modify
  18867. (org-inside-LaTeX-fragment-p)]
  18868. ["Insert citation" org-reftex-citation t]
  18869. "--"
  18870. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18871. "--"
  18872. ("MobileOrg"
  18873. ["Push Files and Views" org-mobile-push t]
  18874. ["Get Captured and Flagged" org-mobile-pull t]
  18875. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18876. "--"
  18877. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18878. "--"
  18879. ("Documentation"
  18880. ["Show Version" org-version t]
  18881. ["Info Documentation" org-info t])
  18882. ("Customize"
  18883. ["Browse Org Group" org-customize t]
  18884. "--"
  18885. ["Expand This Menu" org-create-customize-menu
  18886. (fboundp 'customize-menu-create)])
  18887. ["Send bug report" org-submit-bug-report t]
  18888. "--"
  18889. ("Refresh/Reload"
  18890. ["Refresh setup current buffer" org-mode-restart t]
  18891. ["Reload Org (after update)" org-reload t]
  18892. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18893. ))
  18894. (defun org-info (&optional node)
  18895. "Read documentation for Org-mode in the info system.
  18896. With optional NODE, go directly to that node."
  18897. (interactive)
  18898. (info (format "(org)%s" (or node ""))))
  18899. ;;;###autoload
  18900. (defun org-submit-bug-report ()
  18901. "Submit a bug report on Org-mode via mail.
  18902. Don't hesitate to report any problems or inaccurate documentation.
  18903. If you don't have setup sending mail from (X)Emacs, please copy the
  18904. output buffer into your mail program, as it gives us important
  18905. information about your Org-mode version and configuration."
  18906. (interactive)
  18907. (require 'reporter)
  18908. (org-load-modules-maybe)
  18909. (org-require-autoloaded-modules)
  18910. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18911. (reporter-submit-bug-report
  18912. "emacs-orgmode@gnu.org"
  18913. (org-version nil 'full)
  18914. (let (list)
  18915. (save-window-excursion
  18916. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18917. (delete-other-windows)
  18918. (erase-buffer)
  18919. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18920. We would like to add your full Org-mode and Outline configuration to the
  18921. bug report. This greatly simplifies the work of the maintainer and
  18922. other experts on the mailing list.
  18923. HOWEVER, some variables you have customized may contain private
  18924. information. The names of customers, colleagues, or friends, might
  18925. appear in the form of file names, tags, todo states, or search strings.
  18926. If you answer yes to the prompt, you might want to check and remove
  18927. such private information before sending the email.")
  18928. (add-text-properties (point-min) (point-max) '(face org-warning))
  18929. (when (yes-or-no-p "Include your Org-mode configuration ")
  18930. (mapatoms
  18931. (lambda (v)
  18932. (and (boundp v)
  18933. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18934. (or (and (symbol-value v)
  18935. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18936. (and
  18937. (get v 'custom-type) (get v 'standard-value)
  18938. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18939. (push v list)))))
  18940. (kill-buffer (get-buffer "*Warn about privacy*"))
  18941. list))
  18942. nil nil
  18943. "Remember to cover the basics, that is, what you expected to happen and
  18944. what in fact did happen. You don't know how to make a good report? See
  18945. http://orgmode.org/manual/Feedback.html#Feedback
  18946. Your bug report will be posted to the Org-mode mailing list.
  18947. ------------------------------------------------------------------------")
  18948. (save-excursion
  18949. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18950. (replace-match "\\1Bug: \\3 [\\2]")))))
  18951. (defun org-install-agenda-files-menu ()
  18952. (let ((bl (buffer-list)))
  18953. (save-excursion
  18954. (while bl
  18955. (set-buffer (pop bl))
  18956. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18957. (when (derived-mode-p 'org-mode)
  18958. (easy-menu-change
  18959. '("Org") "File List for Agenda"
  18960. (append
  18961. (list
  18962. ["Edit File List" (org-edit-agenda-file-list) t]
  18963. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18964. ["Remove Current File from List" org-remove-file t]
  18965. ["Cycle through agenda files" org-cycle-agenda-files t]
  18966. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18967. "--")
  18968. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18969. ;;;; Documentation
  18970. (defun org-require-autoloaded-modules ()
  18971. (interactive)
  18972. (mapc 'require
  18973. '(org-agenda org-archive org-attach org-clock org-colview org-id
  18974. org-remember org-table org-timer)))
  18975. ;;;###autoload
  18976. (defun org-reload (&optional uncompiled)
  18977. "Reload all org lisp files.
  18978. With prefix arg UNCOMPILED, load the uncompiled versions."
  18979. (interactive "P")
  18980. (require 'loadhist)
  18981. (let* ((org-dir (org-find-library-dir "org"))
  18982. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18983. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  18984. (remove-re (mapconcat 'identity
  18985. (mapcar (lambda (f) (concat "^" f "$"))
  18986. (list (if (featurep 'xemacs)
  18987. "org-colview"
  18988. "org-colview-xemacs")
  18989. "org" "org-loaddefs" "org-version"))
  18990. "\\|"))
  18991. (feats (delete-dups
  18992. (mapcar 'file-name-sans-extension
  18993. (mapcar 'file-name-nondirectory
  18994. (delq nil
  18995. (mapcar 'feature-file
  18996. features))))))
  18997. (lfeat (append
  18998. (sort
  18999. (setq feats
  19000. (delq nil (mapcar
  19001. (lambda (f)
  19002. (if (and (string-match feature-re f)
  19003. (not (string-match remove-re f)))
  19004. f nil))
  19005. feats)))
  19006. 'string-lessp)
  19007. (list "org-version" "org")))
  19008. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19009. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19010. load-uncore load-misses)
  19011. (setq load-misses
  19012. (delq 't
  19013. (mapcar (lambda (f)
  19014. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19015. (and (string= org-dir contrib-dir)
  19016. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19017. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19018. (add-to-list 'load-uncore f 'append)
  19019. 't)
  19020. f))
  19021. lfeat)))
  19022. (if load-uncore
  19023. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19024. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19025. (if load-misses
  19026. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19027. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19028. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19029. ;;;###autoload
  19030. (defun org-customize ()
  19031. "Call the customize function with org as argument."
  19032. (interactive)
  19033. (org-load-modules-maybe)
  19034. (org-require-autoloaded-modules)
  19035. (customize-browse 'org))
  19036. (defun org-create-customize-menu ()
  19037. "Create a full customization menu for Org-mode, insert it into the menu."
  19038. (interactive)
  19039. (org-load-modules-maybe)
  19040. (org-require-autoloaded-modules)
  19041. (if (fboundp 'customize-menu-create)
  19042. (progn
  19043. (easy-menu-change
  19044. '("Org") "Customize"
  19045. `(["Browse Org group" org-customize t]
  19046. "--"
  19047. ,(customize-menu-create 'org)
  19048. ["Set" Custom-set t]
  19049. ["Save" Custom-save t]
  19050. ["Reset to Current" Custom-reset-current t]
  19051. ["Reset to Saved" Custom-reset-saved t]
  19052. ["Reset to Standard Settings" Custom-reset-standard t]))
  19053. (message "\"Org\"-menu now contains full customization menu"))
  19054. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19055. ;;;; Miscellaneous stuff
  19056. ;;; Generally useful functions
  19057. (defun org-get-at-bol (property)
  19058. "Get text property PROPERTY at beginning of line."
  19059. (get-text-property (point-at-bol) property))
  19060. (defun org-find-text-property-in-string (prop s)
  19061. "Return the first non-nil value of property PROP in string S."
  19062. (or (get-text-property 0 prop s)
  19063. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19064. prop s)))
  19065. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19066. "Display the given MESSAGE as a warning."
  19067. (if (fboundp 'display-warning)
  19068. (display-warning 'org message
  19069. (if (featurep 'xemacs) 'warning :warning))
  19070. (let ((buf (get-buffer-create "*Org warnings*")))
  19071. (with-current-buffer buf
  19072. (goto-char (point-max))
  19073. (insert "Warning (Org): " message)
  19074. (unless (bolp)
  19075. (newline)))
  19076. (display-buffer buf)
  19077. (sit-for 0))))
  19078. (defun org-eval (form)
  19079. "Eval FORM and return result."
  19080. (condition-case error
  19081. (eval form)
  19082. (error (format "%%![Error: %s]" error))))
  19083. (defun org-in-clocktable-p ()
  19084. "Check if the cursor is in a clocktable."
  19085. (let ((pos (point)) start)
  19086. (save-excursion
  19087. (end-of-line 1)
  19088. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19089. (setq start (match-beginning 0))
  19090. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19091. (>= (match-end 0) pos)
  19092. start))))
  19093. (defun org-in-commented-line ()
  19094. "Is point in a line starting with `#'?"
  19095. (equal (char-after (point-at-bol)) ?#))
  19096. (defun org-in-indented-comment-line ()
  19097. "Is point in a line starting with `#' after some white space?"
  19098. (save-excursion
  19099. (save-match-data
  19100. (goto-char (point-at-bol))
  19101. (looking-at "[ \t]*#"))))
  19102. (defun org-in-verbatim-emphasis ()
  19103. (save-match-data
  19104. (and (org-in-regexp org-emph-re 2)
  19105. (>= (point) (match-beginning 3))
  19106. (<= (point) (match-end 4))
  19107. (member (match-string 3) '("=" "~")))))
  19108. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19109. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19110. (if (and marker (marker-buffer marker)
  19111. (buffer-live-p (marker-buffer marker)))
  19112. (progn
  19113. (org-pop-to-buffer-same-window (marker-buffer marker))
  19114. (if (or (> marker (point-max)) (< marker (point-min)))
  19115. (widen))
  19116. (goto-char marker)
  19117. (org-show-context 'org-goto))
  19118. (if bookmark
  19119. (bookmark-jump bookmark)
  19120. (error "Cannot find location"))))
  19121. (defun org-quote-csv-field (s)
  19122. "Quote field for inclusion in CSV material."
  19123. (if (string-match "[\",]" s)
  19124. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19125. s))
  19126. (defun org-force-self-insert (N)
  19127. "Needed to enforce self-insert under remapping."
  19128. (interactive "p")
  19129. (self-insert-command N))
  19130. (defun org-string-width (s)
  19131. "Compute width of string, ignoring invisible characters.
  19132. This ignores character with invisibility property `org-link', and also
  19133. characters with property `org-cwidth', because these will become invisible
  19134. upon the next fontification round."
  19135. (let (b l)
  19136. (when (or (eq t buffer-invisibility-spec)
  19137. (assq 'org-link buffer-invisibility-spec))
  19138. (while (setq b (text-property-any 0 (length s)
  19139. 'invisible 'org-link s))
  19140. (setq s (concat (substring s 0 b)
  19141. (substring s (or (next-single-property-change
  19142. b 'invisible s) (length s)))))))
  19143. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19144. (setq s (concat (substring s 0 b)
  19145. (substring s (or (next-single-property-change
  19146. b 'org-cwidth s) (length s))))))
  19147. (setq l (string-width s) b -1)
  19148. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19149. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19150. l))
  19151. (defun org-shorten-string (s maxlength)
  19152. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19153. If the string is shorter or has length MAXLENGTH, just return the
  19154. original string. If it is longer, the functions finds a space in the
  19155. string, breaks this string off at that locations and adds three dots
  19156. as ellipsis. Including the ellipsis, the string will not be longer
  19157. than MAXLENGTH. If finding a good breaking point in the string does
  19158. not work, the string is just chopped off in the middle of a word
  19159. if necessary."
  19160. (if (<= (length s) maxlength)
  19161. s
  19162. (let* ((n (max (- maxlength 4) 1))
  19163. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19164. (if (string-match re s)
  19165. (concat (match-string 1 s) "...")
  19166. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19167. (defun org-get-indentation (&optional line)
  19168. "Get the indentation of the current line, interpreting tabs.
  19169. When LINE is given, assume it represents a line and compute its indentation."
  19170. (if line
  19171. (if (string-match "^ *" (org-remove-tabs line))
  19172. (match-end 0))
  19173. (save-excursion
  19174. (beginning-of-line 1)
  19175. (skip-chars-forward " \t")
  19176. (current-column))))
  19177. (defun org-get-string-indentation (s)
  19178. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19179. (let ((n -1) (i 0) (w tab-width) c)
  19180. (catch 'exit
  19181. (while (< (setq n (1+ n)) (length s))
  19182. (setq c (aref s n))
  19183. (cond ((= c ?\ ) (setq i (1+ i)))
  19184. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19185. (t (throw 'exit t)))))
  19186. i))
  19187. (defun org-remove-tabs (s &optional width)
  19188. "Replace tabulators in S with spaces.
  19189. Assumes that s is a single line, starting in column 0."
  19190. (setq width (or width tab-width))
  19191. (while (string-match "\t" s)
  19192. (setq s (replace-match
  19193. (make-string
  19194. (- (* width (/ (+ (match-beginning 0) width) width))
  19195. (match-beginning 0)) ?\ )
  19196. t t s)))
  19197. s)
  19198. (defun org-fix-indentation (line ind)
  19199. "Fix indentation in LINE.
  19200. IND is a cons cell with target and minimum indentation.
  19201. If the current indentation in LINE is smaller than the minimum,
  19202. leave it alone. If it is larger than ind, set it to the target."
  19203. (let* ((l (org-remove-tabs line))
  19204. (i (org-get-indentation l))
  19205. (i1 (car ind)) (i2 (cdr ind)))
  19206. (if (>= i i2) (setq l (substring line i2)))
  19207. (if (> i1 0)
  19208. (concat (make-string i1 ?\ ) l)
  19209. l)))
  19210. (defun org-remove-indentation (code &optional n)
  19211. "Remove the maximum common indentation from the lines in CODE.
  19212. N may optionally be the number of spaces to remove."
  19213. (with-temp-buffer
  19214. (insert code)
  19215. (org-do-remove-indentation n)
  19216. (buffer-string)))
  19217. (defun org-do-remove-indentation (&optional n)
  19218. "Remove the maximum common indentation from the buffer."
  19219. (untabify (point-min) (point-max))
  19220. (let ((min 10000) re)
  19221. (if n
  19222. (setq min n)
  19223. (goto-char (point-min))
  19224. (while (re-search-forward "^ *[^ \n]" nil t)
  19225. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19226. (unless (or (= min 0) (= min 10000))
  19227. (setq re (format "^ \\{%d\\}" min))
  19228. (goto-char (point-min))
  19229. (while (re-search-forward re nil t)
  19230. (replace-match "")
  19231. (end-of-line 1))
  19232. min)))
  19233. (defun org-fill-template (template alist)
  19234. "Find each %key of ALIST in TEMPLATE and replace it."
  19235. (let ((case-fold-search nil)
  19236. entry key value)
  19237. (setq alist (sort (copy-sequence alist)
  19238. (lambda (a b) (< (length (car a)) (length (car b))))))
  19239. (while (setq entry (pop alist))
  19240. (setq template
  19241. (replace-regexp-in-string
  19242. (concat "%" (regexp-quote (car entry)))
  19243. (or (cdr entry) "") template t t)))
  19244. template))
  19245. (defun org-base-buffer (buffer)
  19246. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19247. (if (not buffer)
  19248. buffer
  19249. (or (buffer-base-buffer buffer)
  19250. buffer)))
  19251. (defun org-trim (s)
  19252. "Remove whitespace at beginning and end of string."
  19253. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19254. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19255. s)
  19256. (defun org-wrap (string &optional width lines)
  19257. "Wrap string to either a number of lines, or a width in characters.
  19258. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19259. that costs. If there is a word longer than WIDTH, the text is actually
  19260. wrapped to the length of that word.
  19261. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19262. many lines, whatever width that takes.
  19263. The return value is a list of lines, without newlines at the end."
  19264. (let* ((words (org-split-string string "[ \t\n]+"))
  19265. (maxword (apply 'max (mapcar 'org-string-width words)))
  19266. w ll)
  19267. (cond (width
  19268. (org-do-wrap words (max maxword width)))
  19269. (lines
  19270. (setq w maxword)
  19271. (setq ll (org-do-wrap words maxword))
  19272. (if (<= (length ll) lines)
  19273. ll
  19274. (setq ll words)
  19275. (while (> (length ll) lines)
  19276. (setq w (1+ w))
  19277. (setq ll (org-do-wrap words w)))
  19278. ll))
  19279. (t (error "Cannot wrap this")))))
  19280. (defun org-do-wrap (words width)
  19281. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19282. (let (lines line)
  19283. (while words
  19284. (setq line (pop words))
  19285. (while (and words (< (+ (length line) (length (car words))) width))
  19286. (setq line (concat line " " (pop words))))
  19287. (setq lines (push line lines)))
  19288. (nreverse lines)))
  19289. (defun org-split-string (string &optional separators)
  19290. "Splits STRING into substrings at SEPARATORS.
  19291. No empty strings are returned if there are matches at the beginning
  19292. and end of string."
  19293. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19294. (start 0)
  19295. notfirst
  19296. (list nil))
  19297. (while (and (string-match rexp string
  19298. (if (and notfirst
  19299. (= start (match-beginning 0))
  19300. (< start (length string)))
  19301. (1+ start) start))
  19302. (< (match-beginning 0) (length string)))
  19303. (setq notfirst t)
  19304. (or (eq (match-beginning 0) 0)
  19305. (and (eq (match-beginning 0) (match-end 0))
  19306. (eq (match-beginning 0) start))
  19307. (setq list
  19308. (cons (substring string start (match-beginning 0))
  19309. list)))
  19310. (setq start (match-end 0)))
  19311. (or (eq start (length string))
  19312. (setq list
  19313. (cons (substring string start)
  19314. list)))
  19315. (nreverse list)))
  19316. (defun org-quote-vert (s)
  19317. "Replace \"|\" with \"\\vert\"."
  19318. (while (string-match "|" s)
  19319. (setq s (replace-match "\\vert" t t s)))
  19320. s)
  19321. (defun org-uuidgen-p (s)
  19322. "Is S an ID created by UUIDGEN?"
  19323. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19324. (defun org-in-src-block-p (&optional inside)
  19325. "Whether point is in a code source block.
  19326. When INSIDE is non-nil, don't consider we are within a src block
  19327. when point is at #+BEGIN_SRC or #+END_SRC."
  19328. (let ((case-fold-search t) ov)
  19329. (or (and (setq ov (overlays-at (point)))
  19330. (memq 'org-block-background
  19331. (overlay-properties (car ov))))
  19332. (and (not inside)
  19333. (save-match-data
  19334. (save-excursion
  19335. (beginning-of-line)
  19336. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19337. (defun org-context ()
  19338. "Return a list of contexts of the current cursor position.
  19339. If several contexts apply, all are returned.
  19340. Each context entry is a list with a symbol naming the context, and
  19341. two positions indicating start and end of the context. Possible
  19342. contexts are:
  19343. :headline anywhere in a headline
  19344. :headline-stars on the leading stars in a headline
  19345. :todo-keyword on a TODO keyword (including DONE) in a headline
  19346. :tags on the TAGS in a headline
  19347. :priority on the priority cookie in a headline
  19348. :item on the first line of a plain list item
  19349. :item-bullet on the bullet/number of a plain list item
  19350. :checkbox on the checkbox in a plain list item
  19351. :table in an org-mode table
  19352. :table-special on a special filed in a table
  19353. :table-table in a table.el table
  19354. :clocktable in a clocktable
  19355. :src-block in a source block
  19356. :link on a hyperlink
  19357. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19358. :target on a <<target>>
  19359. :radio-target on a <<<radio-target>>>
  19360. :latex-fragment on a LaTeX fragment
  19361. :latex-preview on a LaTeX fragment with overlaid preview image
  19362. This function expects the position to be visible because it uses font-lock
  19363. faces as a help to recognize the following contexts: :table-special, :link,
  19364. and :keyword."
  19365. (let* ((f (get-text-property (point) 'face))
  19366. (faces (if (listp f) f (list f)))
  19367. (case-fold-search t)
  19368. (p (point)) clist o)
  19369. ;; First the large context
  19370. (cond
  19371. ((org-at-heading-p t)
  19372. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19373. (when (progn
  19374. (beginning-of-line 1)
  19375. (looking-at org-todo-line-tags-regexp))
  19376. (push (org-point-in-group p 1 :headline-stars) clist)
  19377. (push (org-point-in-group p 2 :todo-keyword) clist)
  19378. (push (org-point-in-group p 4 :tags) clist))
  19379. (goto-char p)
  19380. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19381. (if (looking-at "\\[#[A-Z0-9]\\]")
  19382. (push (org-point-in-group p 0 :priority) clist)))
  19383. ((org-at-item-p)
  19384. (push (org-point-in-group p 2 :item-bullet) clist)
  19385. (push (list :item (point-at-bol)
  19386. (save-excursion (org-end-of-item) (point)))
  19387. clist)
  19388. (and (org-at-item-checkbox-p)
  19389. (push (org-point-in-group p 0 :checkbox) clist)))
  19390. ((org-at-table-p)
  19391. (push (list :table (org-table-begin) (org-table-end)) clist)
  19392. (if (memq 'org-formula faces)
  19393. (push (list :table-special
  19394. (previous-single-property-change p 'face)
  19395. (next-single-property-change p 'face)) clist)))
  19396. ((org-at-table-p 'any)
  19397. (push (list :table-table) clist)))
  19398. (goto-char p)
  19399. (let ((case-fold-search t))
  19400. ;; New the "medium" contexts: clocktables, source blocks
  19401. (cond ((org-in-clocktable-p)
  19402. (push (list :clocktable
  19403. (and (or (looking-at "#\\+BEGIN: clocktable")
  19404. (search-backward "#+BEGIN: clocktable" nil t))
  19405. (match-beginning 0))
  19406. (and (re-search-forward "#\\+END:?" nil t)
  19407. (match-end 0))) clist))
  19408. ((org-in-src-block-p)
  19409. (push (list :src-block
  19410. (and (or (looking-at "#\\+BEGIN_SRC")
  19411. (search-backward "#+BEGIN_SRC" nil t))
  19412. (match-beginning 0))
  19413. (and (search-forward "#+END_SRC" nil t)
  19414. (match-beginning 0))) clist))))
  19415. (goto-char p)
  19416. ;; Now the small context
  19417. (cond
  19418. ((org-at-timestamp-p)
  19419. (push (org-point-in-group p 0 :timestamp) clist))
  19420. ((memq 'org-link faces)
  19421. (push (list :link
  19422. (previous-single-property-change p 'face)
  19423. (next-single-property-change p 'face)) clist))
  19424. ((memq 'org-special-keyword faces)
  19425. (push (list :keyword
  19426. (previous-single-property-change p 'face)
  19427. (next-single-property-change p 'face)) clist))
  19428. ((org-at-target-p)
  19429. (push (org-point-in-group p 0 :target) clist)
  19430. (goto-char (1- (match-beginning 0)))
  19431. (if (looking-at org-radio-target-regexp)
  19432. (push (org-point-in-group p 0 :radio-target) clist))
  19433. (goto-char p))
  19434. ((setq o (car (delq nil
  19435. (mapcar
  19436. (lambda (x)
  19437. (if (memq x org-latex-fragment-image-overlays) x))
  19438. (overlays-at (point))))))
  19439. (push (list :latex-fragment
  19440. (overlay-start o) (overlay-end o)) clist)
  19441. (push (list :latex-preview
  19442. (overlay-start o) (overlay-end o)) clist))
  19443. ((org-inside-LaTeX-fragment-p)
  19444. ;; FIXME: positions wrong.
  19445. (push (list :latex-fragment (point) (point)) clist)))
  19446. (setq clist (nreverse (delq nil clist)))
  19447. clist))
  19448. ;; FIXME: Compare with at-regexp-p Do we need both?
  19449. (defun org-in-regexp (re &optional nlines visually)
  19450. "Check if point is inside a match of regexp.
  19451. Normally only the current line is checked, but you can include NLINES extra
  19452. lines both before and after point into the search.
  19453. If VISUALLY is set, require that the cursor is not after the match but
  19454. really on, so that the block visually is on the match."
  19455. (catch 'exit
  19456. (let ((pos (point))
  19457. (eol (point-at-eol (+ 1 (or nlines 0))))
  19458. (inc (if visually 1 0)))
  19459. (save-excursion
  19460. (beginning-of-line (- 1 (or nlines 0)))
  19461. (while (re-search-forward re eol t)
  19462. (if (and (<= (match-beginning 0) pos)
  19463. (>= (+ inc (match-end 0)) pos))
  19464. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19465. (defun org-at-regexp-p (regexp)
  19466. "Is point inside a match of REGEXP in the current line?"
  19467. (catch 'exit
  19468. (save-excursion
  19469. (let ((pos (point)) (end (point-at-eol)))
  19470. (beginning-of-line 1)
  19471. (while (re-search-forward regexp end t)
  19472. (if (and (<= (match-beginning 0) pos)
  19473. (>= (match-end 0) pos))
  19474. (throw 'exit t)))
  19475. nil))))
  19476. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19477. "Non-nil when point is between matches of START-RE and END-RE.
  19478. Also return a non-nil value when point is on one of the matches.
  19479. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19480. buffer positions. Default values are the positions of headlines
  19481. surrounding the point.
  19482. The functions returns a cons cell whose car (resp. cdr) is the
  19483. position before START-RE (resp. after END-RE)."
  19484. (save-match-data
  19485. (let ((pos (point))
  19486. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19487. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19488. beg end)
  19489. (save-excursion
  19490. ;; Point is on a block when on START-RE or if START-RE can be
  19491. ;; found before it...
  19492. (and (or (org-at-regexp-p start-re)
  19493. (re-search-backward start-re limit-up t))
  19494. (setq beg (match-beginning 0))
  19495. ;; ... and END-RE after it...
  19496. (goto-char (match-end 0))
  19497. (re-search-forward end-re limit-down t)
  19498. (> (setq end (match-end 0)) pos)
  19499. ;; ... without another START-RE in-between.
  19500. (goto-char (match-beginning 0))
  19501. (not (re-search-backward start-re (1+ beg) t))
  19502. ;; Return value.
  19503. (cons beg end))))))
  19504. (defun org-in-block-p (names)
  19505. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19506. NAMES is a list of strings containing names of blocks.
  19507. Return first block name matched, or nil. Beware that in case of
  19508. nested blocks, the returned name may not belong to the closest
  19509. block from point."
  19510. (save-match-data
  19511. (catch 'exit
  19512. (let ((case-fold-search t)
  19513. (lim-up (save-excursion (outline-previous-heading)))
  19514. (lim-down (save-excursion (outline-next-heading))))
  19515. (mapc (lambda (name)
  19516. (let ((n (regexp-quote name)))
  19517. (when (org-between-regexps-p
  19518. (concat "^[ \t]*#\\+begin_" n)
  19519. (concat "^[ \t]*#\\+end_" n)
  19520. lim-up lim-down)
  19521. (throw 'exit n))))
  19522. names))
  19523. nil)))
  19524. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19525. "Call `multi-occur' with buffers for all agenda files."
  19526. (interactive "sOrg-files matching: \np")
  19527. (let* ((files (org-agenda-files))
  19528. (tnames (mapcar 'file-truename files))
  19529. (extra org-agenda-text-search-extra-files)
  19530. f)
  19531. (when (eq (car extra) 'agenda-archives)
  19532. (setq extra (cdr extra))
  19533. (setq files (org-add-archive-files files)))
  19534. (while (setq f (pop extra))
  19535. (unless (member (file-truename f) tnames)
  19536. (add-to-list 'files f 'append)
  19537. (add-to-list 'tnames (file-truename f) 'append)))
  19538. (multi-occur
  19539. (mapcar (lambda (x)
  19540. (with-current-buffer
  19541. (or (get-file-buffer x) (find-file-noselect x))
  19542. (widen)
  19543. (current-buffer)))
  19544. files)
  19545. regexp)))
  19546. (if (boundp 'occur-mode-find-occurrence-hook)
  19547. ;; Emacs 23
  19548. (add-hook 'occur-mode-find-occurrence-hook
  19549. (lambda ()
  19550. (when (derived-mode-p 'org-mode)
  19551. (org-reveal))))
  19552. ;; Emacs 22
  19553. (defadvice occur-mode-goto-occurrence
  19554. (after org-occur-reveal activate)
  19555. (and (derived-mode-p 'org-mode) (org-reveal)))
  19556. (defadvice occur-mode-goto-occurrence-other-window
  19557. (after org-occur-reveal activate)
  19558. (and (derived-mode-p 'org-mode) (org-reveal)))
  19559. (defadvice occur-mode-display-occurrence
  19560. (after org-occur-reveal activate)
  19561. (when (derived-mode-p 'org-mode)
  19562. (let ((pos (occur-mode-find-occurrence)))
  19563. (with-current-buffer (marker-buffer pos)
  19564. (save-excursion
  19565. (goto-char pos)
  19566. (org-reveal)))))))
  19567. (defun org-occur-link-in-agenda-files ()
  19568. "Create a link and search for it in the agendas.
  19569. The link is not stored in `org-stored-links', it is just created
  19570. for the search purpose."
  19571. (interactive)
  19572. (let ((link (condition-case nil
  19573. (org-store-link nil)
  19574. (error "Unable to create a link to here"))))
  19575. (org-occur-in-agenda-files (regexp-quote link))))
  19576. (defun org-reverse-string (string)
  19577. "Return the reverse of STRING."
  19578. (apply 'string (reverse (string-to-list string))))
  19579. (defun org-uniquify (list)
  19580. "Remove duplicate elements from LIST."
  19581. (let (res)
  19582. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19583. res))
  19584. (defun org-delete-all (elts list)
  19585. "Remove all elements in ELTS from LIST."
  19586. (while elts
  19587. (setq list (delete (pop elts) list)))
  19588. list)
  19589. (defun org-count (cl-item cl-seq)
  19590. "Count the number of occurrences of ITEM in SEQ.
  19591. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19592. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19593. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19594. (while (< cl-start cl-end)
  19595. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19596. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19597. (setq cl-start (1+ cl-start)))
  19598. cl-count))
  19599. (defun org-remove-if (predicate seq)
  19600. "Remove everything from SEQ that fulfills PREDICATE."
  19601. (let (res e)
  19602. (while seq
  19603. (setq e (pop seq))
  19604. (if (not (funcall predicate e)) (push e res)))
  19605. (nreverse res)))
  19606. (defun org-remove-if-not (predicate seq)
  19607. "Remove everything from SEQ that does not fulfill PREDICATE."
  19608. (let (res e)
  19609. (while seq
  19610. (setq e (pop seq))
  19611. (if (funcall predicate e) (push e res)))
  19612. (nreverse res)))
  19613. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19614. "Reduce two-argument FUNCTION across SEQ.
  19615. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19616. \":initial-value\" removed."
  19617. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19618. (cadr (memq :initial-value cl-keys)))
  19619. (cl-seq (pop cl-seq))
  19620. (t (funcall cl-func)))))
  19621. (while cl-seq
  19622. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19623. cl-accum))
  19624. (defun org-back-over-empty-lines ()
  19625. "Move backwards over whitespace, to the beginning of the first empty line.
  19626. Returns the number of empty lines passed."
  19627. (let ((pos (point)))
  19628. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19629. (skip-chars-backward " \t\n\r")
  19630. (unless (eobp)
  19631. (forward-line -1)))
  19632. (beginning-of-line 2)
  19633. (goto-char (min (point) pos))
  19634. (count-lines (point) pos)))
  19635. (defun org-skip-whitespace ()
  19636. (skip-chars-forward " \t\n\r"))
  19637. (defun org-point-in-group (point group &optional context)
  19638. "Check if POINT is in match-group GROUP.
  19639. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19640. match. If the match group does not exist or point is not inside it,
  19641. return nil."
  19642. (and (match-beginning group)
  19643. (>= point (match-beginning group))
  19644. (<= point (match-end group))
  19645. (if context
  19646. (list context (match-beginning group) (match-end group))
  19647. t)))
  19648. (defun org-switch-to-buffer-other-window (&rest args)
  19649. "Switch to buffer in a second window on the current frame.
  19650. In particular, do not allow pop-up frames.
  19651. Returns the newly created buffer."
  19652. (org-no-popups
  19653. (apply 'switch-to-buffer-other-window args)))
  19654. (defun org-combine-plists (&rest plists)
  19655. "Create a single property list from all plists in PLISTS.
  19656. The process starts by copying the first list, and then setting properties
  19657. from the other lists. Settings in the last list are the most significant
  19658. ones and overrule settings in the other lists."
  19659. (let ((rtn (copy-sequence (pop plists)))
  19660. p v ls)
  19661. (while plists
  19662. (setq ls (pop plists))
  19663. (while ls
  19664. (setq p (pop ls) v (pop ls))
  19665. (setq rtn (plist-put rtn p v))))
  19666. rtn))
  19667. (defun org-replace-escapes (string table)
  19668. "Replace %-escapes in STRING with values in TABLE.
  19669. TABLE is an association list with keys like \"%a\" and string values.
  19670. The sequences in STRING may contain normal field width and padding information,
  19671. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19672. so values can contain further %-escapes if they are define later in TABLE."
  19673. (let ((tbl (copy-alist table))
  19674. (case-fold-search nil)
  19675. (pchg 0)
  19676. e re rpl)
  19677. (while (setq e (pop tbl))
  19678. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19679. (when (and (cdr e) (string-match re (cdr e)))
  19680. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19681. (safe "SREF"))
  19682. (add-text-properties 0 3 (list 'sref sref) safe)
  19683. (setcdr e (replace-match safe t t (cdr e)))))
  19684. (while (string-match re string)
  19685. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19686. (cdr e)))
  19687. (setq string (replace-match rpl t t string))))
  19688. (while (setq pchg (next-property-change pchg string))
  19689. (let ((sref (get-text-property pchg 'sref string)))
  19690. (when (and sref (string-match "SREF" string pchg))
  19691. (setq string (replace-match sref t t string)))))
  19692. string))
  19693. (defun org-sublist (list start end)
  19694. "Return a section of LIST, from START to END.
  19695. Counting starts at 1."
  19696. (let (rtn (c start))
  19697. (setq list (nthcdr (1- start) list))
  19698. (while (and list (<= c end))
  19699. (push (pop list) rtn)
  19700. (setq c (1+ c)))
  19701. (nreverse rtn)))
  19702. (defun org-find-base-buffer-visiting (file)
  19703. "Like `find-buffer-visiting' but always return the base buffer and
  19704. not an indirect buffer."
  19705. (let ((buf (or (get-file-buffer file)
  19706. (find-buffer-visiting file))))
  19707. (if buf
  19708. (or (buffer-base-buffer buf) buf)
  19709. nil)))
  19710. (defun org-image-file-name-regexp (&optional extensions)
  19711. "Return regexp matching the file names of images.
  19712. If EXTENSIONS is given, only match these."
  19713. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19714. (image-file-name-regexp)
  19715. (let ((image-file-name-extensions
  19716. (or extensions
  19717. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19718. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19719. (concat "\\."
  19720. (regexp-opt (nconc (mapcar 'upcase
  19721. image-file-name-extensions)
  19722. image-file-name-extensions)
  19723. t)
  19724. "\\'"))))
  19725. (defun org-file-image-p (file &optional extensions)
  19726. "Return non-nil if FILE is an image."
  19727. (save-match-data
  19728. (string-match (org-image-file-name-regexp extensions) file)))
  19729. (defun org-get-cursor-date (&optional with-time)
  19730. "Return the date at cursor in as a time.
  19731. This works in the calendar and in the agenda, anywhere else it just
  19732. returns the current time.
  19733. If WITH-TIME is non-nil, returns the time of the event at point (in
  19734. the agenda) or the current time of the day."
  19735. (let (date day defd tp tm hod mod)
  19736. (when with-time
  19737. (setq tp (get-text-property (point) 'time))
  19738. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19739. (setq hod (string-to-number (match-string 1 tp))
  19740. mod (string-to-number (match-string 2 tp))))
  19741. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19742. mod (nth 1 (decode-time (current-time))))))
  19743. (cond
  19744. ((eq major-mode 'calendar-mode)
  19745. (setq date (calendar-cursor-to-date)
  19746. defd (encode-time 0 (or mod 0) (or hod 0)
  19747. (nth 1 date) (nth 0 date) (nth 2 date))))
  19748. ((eq major-mode 'org-agenda-mode)
  19749. (setq day (get-text-property (point) 'day))
  19750. (if day
  19751. (setq date (calendar-gregorian-from-absolute day)
  19752. defd (encode-time 0 (or mod 0) (or hod 0)
  19753. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19754. (or defd (current-time))))
  19755. (defun org-mark-subtree (&optional up)
  19756. "Mark the current subtree.
  19757. This puts point at the start of the current subtree, and mark at
  19758. the end. If a numeric prefix UP is given, move up into the
  19759. hierarchy of headlines by UP levels before marking the subtree."
  19760. (interactive "P")
  19761. (org-with-limited-levels
  19762. (cond ((org-at-heading-p) (beginning-of-line))
  19763. ((org-before-first-heading-p) (error "Not in a subtree"))
  19764. (t (outline-previous-visible-heading 1))))
  19765. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19766. (if (org-called-interactively-p 'any)
  19767. (call-interactively 'org-mark-element)
  19768. (org-mark-element)))
  19769. ;;; Indentation
  19770. (defun org-indent-line ()
  19771. "Indent line depending on context."
  19772. (interactive)
  19773. (let* ((pos (point))
  19774. (itemp (org-at-item-p))
  19775. (case-fold-search t)
  19776. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19777. (inline-task-p (and (featurep 'org-inlinetask)
  19778. (org-inlinetask-in-task-p)))
  19779. (inline-re (and inline-task-p
  19780. (org-inlinetask-outline-regexp)))
  19781. column)
  19782. (if (and orgstruct-is-++ (eq pos (point)))
  19783. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19784. (indent-according-to-mode))
  19785. (beginning-of-line 1)
  19786. (cond
  19787. ;; Headings
  19788. ((looking-at org-outline-regexp) (setq column 0))
  19789. ;; Footnote definition
  19790. ((looking-at org-footnote-definition-re) (setq column 0))
  19791. ;; Literal examples
  19792. ((looking-at "[ \t]*:\\( \\|$\\)")
  19793. (setq column (org-get-indentation))) ; do nothing
  19794. ;; Lists
  19795. ((ignore-errors (goto-char (org-in-item-p)))
  19796. (setq column (if itemp
  19797. (org-get-indentation)
  19798. (org-list-item-body-column (point))))
  19799. (goto-char pos))
  19800. ;; Drawers
  19801. ((and (looking-at "[ \t]*:END:")
  19802. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19803. (save-excursion
  19804. (goto-char (1- (match-beginning 1)))
  19805. (setq column (current-column))))
  19806. ;; Special blocks
  19807. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19808. (save-excursion
  19809. (re-search-backward
  19810. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19811. (setq column (org-get-indentation (match-string 0))))
  19812. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19813. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19814. (save-excursion
  19815. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19816. (setq column
  19817. (cond ((equal (downcase (match-string 1)) "src")
  19818. ;; src blocks: let `org-edit-src-exit' handle them
  19819. (org-get-indentation))
  19820. ((equal (downcase (match-string 1)) "example")
  19821. (max (org-get-indentation)
  19822. (org-get-indentation (match-string 0))))
  19823. (t
  19824. (org-get-indentation (match-string 0))))))
  19825. ;; This line has nothing special, look at the previous relevant
  19826. ;; line to compute indentation
  19827. (t
  19828. (beginning-of-line 0)
  19829. (while (and (not (bobp))
  19830. (not (looking-at org-table-line-regexp))
  19831. (not (looking-at org-drawer-regexp))
  19832. ;; When point started in an inline task, do not move
  19833. ;; above task starting line.
  19834. (not (and inline-task-p (looking-at inline-re)))
  19835. ;; Skip drawers, blocks, empty lines, verbatim,
  19836. ;; comments, tables, footnotes definitions, lists,
  19837. ;; inline tasks.
  19838. (or (and (looking-at "[ \t]*:END:")
  19839. (re-search-backward org-drawer-regexp nil t))
  19840. (and (looking-at "[ \t]*#\\+end_")
  19841. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19842. (looking-at "[ \t]*[\n:#|]")
  19843. (looking-at org-footnote-definition-re)
  19844. (and (ignore-errors (goto-char (org-in-item-p)))
  19845. (goto-char
  19846. (org-list-get-top-point (org-list-struct))))
  19847. (and (not inline-task-p)
  19848. (featurep 'org-inlinetask)
  19849. (org-inlinetask-in-task-p)
  19850. (or (org-inlinetask-goto-beginning) t))))
  19851. (beginning-of-line 0))
  19852. (cond
  19853. ;; There was an heading above.
  19854. ((looking-at "\\*+[ \t]+")
  19855. (if (not org-adapt-indentation)
  19856. (setq column 0)
  19857. (goto-char (match-end 0))
  19858. (setq column (current-column))))
  19859. ;; A drawer had started and is unfinished
  19860. ((looking-at org-drawer-regexp)
  19861. (goto-char (1- (match-beginning 1)))
  19862. (setq column (current-column)))
  19863. ;; Else, nothing noticeable found: get indentation and go on.
  19864. (t (setq column (org-get-indentation))))))
  19865. ;; Now apply indentation and move cursor accordingly
  19866. (goto-char pos)
  19867. (if (<= (current-column) (current-indentation))
  19868. (org-indent-line-to column)
  19869. (save-excursion (org-indent-line-to column)))
  19870. ;; Special polishing for properties, see `org-property-format'
  19871. (setq column (current-column))
  19872. (beginning-of-line 1)
  19873. (if (looking-at
  19874. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19875. (replace-match (concat (match-string 1)
  19876. (format org-property-format
  19877. (match-string 2) (match-string 3)))
  19878. t t))
  19879. (org-move-to-column column))))
  19880. (defun org-indent-drawer ()
  19881. "Indent the drawer at point."
  19882. (interactive)
  19883. (let ((p (point))
  19884. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19885. (match-end 0)))
  19886. (folded
  19887. (save-excursion
  19888. (end-of-line)
  19889. (when (overlays-at (point))
  19890. (member 'invisible (overlay-properties
  19891. (car (overlays-at (point)))))))))
  19892. (when folded (org-cycle))
  19893. (indent-for-tab-command)
  19894. (while (and (move-beginning-of-line 2) (< (point) e))
  19895. (indent-for-tab-command))
  19896. (goto-char p)
  19897. (when folded (org-cycle)))
  19898. (message "Drawer at point indented"))
  19899. (defun org-indent-block ()
  19900. "Indent the block at point."
  19901. (interactive)
  19902. (let ((p (point))
  19903. (case-fold-search t)
  19904. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19905. (match-end 0)))
  19906. (folded
  19907. (save-excursion
  19908. (end-of-line)
  19909. (when (overlays-at (point))
  19910. (member 'invisible (overlay-properties
  19911. (car (overlays-at (point)))))))))
  19912. (when folded (org-cycle))
  19913. (indent-for-tab-command)
  19914. (while (and (move-beginning-of-line 2) (< (point) e))
  19915. (indent-for-tab-command))
  19916. (goto-char p)
  19917. (when folded (org-cycle)))
  19918. (message "Block at point indented"))
  19919. (defun org-indent-region (start end)
  19920. "Indent region."
  19921. (interactive "r")
  19922. (save-excursion
  19923. (let ((line-end (org-current-line end)))
  19924. (goto-char start)
  19925. (while (< (org-current-line) line-end)
  19926. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19927. (t (call-interactively 'org-indent-line)))
  19928. (move-beginning-of-line 2)))))
  19929. ;;; Filling
  19930. ;; We use our own fill-paragraph and auto-fill functions.
  19931. ;; `org-fill-paragraph' relies on adaptive filling and context
  19932. ;; checking. Appropriate `fill-prefix' is computed with
  19933. ;; `org-adaptive-fill-function'.
  19934. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19935. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19936. ;; `org-adaptive-fill-function' value. Internally,
  19937. ;; `org-comment-line-break-function' breaks the line.
  19938. ;; `org-setup-filling' installs filling and auto-filling related
  19939. ;; variables during `org-mode' initialization.
  19940. (defun org-setup-filling ()
  19941. (interactive)
  19942. ;; Prevent auto-fill from inserting unwanted new items.
  19943. (when (boundp 'fill-nobreak-predicate)
  19944. (org-set-local
  19945. 'fill-nobreak-predicate
  19946. (org-uniquify
  19947. (append fill-nobreak-predicate
  19948. '(org-fill-paragraph-separate-nobreak-p
  19949. org-fill-line-break-nobreak-p
  19950. org-fill-paragraph-with-timestamp-nobreak-p)))))
  19951. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19952. (org-set-local 'auto-fill-inhibit-regexp nil)
  19953. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19954. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19955. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19956. (defvar org-element-paragraph-separate) ; org-element.el
  19957. (defun org-fill-paragraph-separate-nobreak-p ()
  19958. "Non-nil when a line break at point would insert a new item."
  19959. (looking-at (substring org-element-paragraph-separate 1)))
  19960. (defun org-fill-line-break-nobreak-p ()
  19961. "Non-nil when a line break at point would create an Org line break."
  19962. (save-excursion
  19963. (skip-chars-backward "[ \t]")
  19964. (skip-chars-backward "\\\\")
  19965. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19966. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  19967. "Non-nil when a line break at point would insert a new item."
  19968. (and (org-at-timestamp-p t)
  19969. (not (looking-at org-ts-regexp-both))))
  19970. (declare-function message-in-body-p "message" ())
  19971. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19972. (defun org-adaptive-fill-function ()
  19973. "Compute a fill prefix for the current line.
  19974. Return fill prefix, as a string, or nil if current line isn't
  19975. meant to be filled."
  19976. (let (prefix)
  19977. (catch 'exit
  19978. (when (derived-mode-p 'message-mode)
  19979. (save-excursion
  19980. (beginning-of-line)
  19981. (cond ((or (not (message-in-body-p))
  19982. (looking-at orgtbl-line-start-regexp))
  19983. (throw 'exit nil))
  19984. ((looking-at message-cite-prefix-regexp)
  19985. (throw 'exit (match-string-no-properties 0)))
  19986. ((looking-at org-outline-regexp)
  19987. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19988. (org-with-wide-buffer
  19989. (let* ((p (line-beginning-position))
  19990. (element (save-excursion (beginning-of-line)
  19991. (org-element-at-point)))
  19992. (type (org-element-type element))
  19993. (post-affiliated (org-element-property :post-affiliated element)))
  19994. (unless (and post-affiliated (< p post-affiliated))
  19995. (case type
  19996. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19997. (footnote-definition "")
  19998. ((item plain-list)
  19999. (make-string (org-list-item-body-column
  20000. (or post-affiliated
  20001. (org-element-property :begin element)))
  20002. ? ))
  20003. (paragraph
  20004. ;; Fill prefix is usually the same as the current line,
  20005. ;; except if the paragraph is at the beginning of an item.
  20006. (let ((parent (org-element-property :parent element)))
  20007. (cond ((eq (org-element-type parent) 'item)
  20008. (make-string (org-list-item-body-column
  20009. (org-element-property :begin parent))
  20010. ? ))
  20011. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20012. (match-string 0))
  20013. (t ""))))
  20014. (comment-block
  20015. ;; Only fill contents if P is within block boundaries.
  20016. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20017. (forward-line)
  20018. (point)))
  20019. (cend (save-excursion
  20020. (goto-char (org-element-property :end element))
  20021. (skip-chars-backward " \r\t\n")
  20022. (line-beginning-position))))
  20023. (when (and (>= p cbeg) (< p cend))
  20024. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20025. (match-string 0)
  20026. "")))))))))))
  20027. (declare-function message-goto-body "message" ())
  20028. (defvar message-cite-prefix-regexp) ; From message.el
  20029. (defun org-fill-paragraph (&optional justify)
  20030. "Fill element at point, when applicable.
  20031. This function only applies to comment blocks, comments, example
  20032. blocks and paragraphs. Also, as a special case, re-align table
  20033. when point is at one.
  20034. If JUSTIFY is non-nil (interactively, with prefix argument),
  20035. justify as well. If `sentence-end-double-space' is non-nil, then
  20036. period followed by one space does not end a sentence, so don't
  20037. break a line there. The variable `fill-column' controls the
  20038. width for filling.
  20039. For convenience, when point is at a plain list, an item or
  20040. a footnote definition, try to fill the first paragraph within."
  20041. (interactive)
  20042. (if (and (derived-mode-p 'message-mode)
  20043. (or (not (message-in-body-p))
  20044. (save-excursion (move-beginning-of-line 1)
  20045. (looking-at message-cite-prefix-regexp))))
  20046. ;; First ensure filling is correct in message-mode.
  20047. (let ((fill-paragraph-function
  20048. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20049. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20050. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20051. (paragraph-separate
  20052. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20053. (fill-paragraph nil))
  20054. (with-syntax-table org-mode-transpose-word-syntax-table
  20055. ;; Move to end of line in order to get the first paragraph
  20056. ;; within a plain list or a footnote definition.
  20057. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20058. ;; First check if point is in a blank line at the beginning of
  20059. ;; the buffer. In that case, ignore filling.
  20060. (case (org-element-type element)
  20061. ;; Use major mode filling function is src blocks.
  20062. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20063. ;; Align Org tables, leave table.el tables as-is.
  20064. (table-row (org-table-align) t)
  20065. (table
  20066. (when (eq (org-element-property :type element) 'org)
  20067. (org-table-align))
  20068. t)
  20069. (paragraph
  20070. ;; Paragraphs may contain `line-break' type objects.
  20071. (let ((beg (max (point-min)
  20072. (org-element-property :contents-begin element)))
  20073. (end (min (point-max)
  20074. (org-element-property :contents-end element))))
  20075. ;; Do nothing if point is at an affiliated keyword.
  20076. (if (< (line-end-position) beg) t
  20077. (when (derived-mode-p 'message-mode)
  20078. ;; In `message-mode', do not fill following citation
  20079. ;; in current paragraph nor text before message body.
  20080. (let ((body-start (save-excursion (message-goto-body))))
  20081. (when body-start (setq beg (max body-start beg))))
  20082. (when (save-excursion
  20083. (re-search-forward
  20084. (concat "^" message-cite-prefix-regexp) end t))
  20085. (setq end (match-beginning 0))))
  20086. ;; Fill paragraph, taking line breaks into account.
  20087. ;; For that, slice the paragraph using line breaks as
  20088. ;; separators, and fill the parts in reverse order to
  20089. ;; avoid messing with markers.
  20090. (save-excursion
  20091. (goto-char end)
  20092. (mapc
  20093. (lambda (pos)
  20094. (fill-region-as-paragraph pos (point) justify)
  20095. (goto-char pos))
  20096. ;; Find the list of ending positions for line breaks
  20097. ;; in the current paragraph. Add paragraph
  20098. ;; beginning to include first slice.
  20099. (nreverse
  20100. (cons beg
  20101. (org-element-map
  20102. (org-element--parse-objects
  20103. beg end nil (org-element-restriction 'paragraph))
  20104. 'line-break
  20105. (lambda (lb) (org-element-property :end lb)))))))
  20106. t)))
  20107. ;; Contents of `comment-block' type elements should be
  20108. ;; filled as plain text, but only if point is within block
  20109. ;; markers.
  20110. (comment-block
  20111. (let* ((case-fold-search t)
  20112. (beg (save-excursion
  20113. (goto-char (org-element-property :begin element))
  20114. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20115. (forward-line)
  20116. (point)))
  20117. (end (save-excursion
  20118. (goto-char (org-element-property :end element))
  20119. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20120. (line-beginning-position))))
  20121. (when (and (>= (point) beg) (< (point) end))
  20122. (fill-region-as-paragraph
  20123. (save-excursion
  20124. (end-of-line)
  20125. (re-search-backward "^[ \t]*$" beg 'move)
  20126. (line-beginning-position))
  20127. (save-excursion
  20128. (beginning-of-line)
  20129. (re-search-forward "^[ \t]*$" end 'move)
  20130. (line-beginning-position))
  20131. justify)))
  20132. t)
  20133. ;; Fill comments.
  20134. (comment (fill-comment-paragraph justify))
  20135. ;; Ignore every other element.
  20136. (otherwise t))))))
  20137. (defun org-auto-fill-function ()
  20138. "Auto-fill function."
  20139. ;; Check if auto-filling is meaningful.
  20140. (let ((fc (current-fill-column)))
  20141. (when (and fc (> (current-column) fc))
  20142. (let* ((fill-prefix (org-adaptive-fill-function))
  20143. ;; Enforce empty fill prefix, if required. Otherwise, it
  20144. ;; will be computed again.
  20145. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20146. (when fill-prefix (do-auto-fill))))))
  20147. (defun org-comment-line-break-function (&optional soft)
  20148. "Break line at point and indent, continuing comment if within one.
  20149. The inserted newline is marked hard if variable
  20150. `use-hard-newlines' is true, unless optional argument SOFT is
  20151. non-nil."
  20152. (if soft (insert-and-inherit ?\n) (newline 1))
  20153. (save-excursion (forward-char -1) (delete-horizontal-space))
  20154. (delete-horizontal-space)
  20155. (indent-to-left-margin)
  20156. (insert-before-markers-and-inherit fill-prefix))
  20157. ;;; Comments
  20158. ;; Org comments syntax is quite complex. It requires the entire line
  20159. ;; to be just a comment. Also, even with the right syntax at the
  20160. ;; beginning of line, some some elements (i.e. verse-block or
  20161. ;; example-block) don't accept comments. Usual Emacs comment commands
  20162. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20163. ;; Org still relies on `comment-dwim', but cannot trust
  20164. ;; `comment-only-p'. So, `comment-region-function' and
  20165. ;; `uncomment-region-function' both point
  20166. ;; to`org-comment-or-uncomment-region'. Eventually,
  20167. ;; `org-insert-comment' takes care of insertion of comments at the
  20168. ;; beginning of line.
  20169. ;; `org-setup-comments-handling' install comments related variables
  20170. ;; during `org-mode' initialization.
  20171. (defun org-setup-comments-handling ()
  20172. (interactive)
  20173. (org-set-local 'comment-use-syntax nil)
  20174. (org-set-local 'comment-start "# ")
  20175. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20176. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20177. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20178. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20179. (defun org-insert-comment ()
  20180. "Insert an empty comment above current line.
  20181. If the line is empty, insert comment at its beginning."
  20182. (beginning-of-line)
  20183. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20184. (org-indent-line)
  20185. (insert "# "))
  20186. (defvar comment-empty-lines) ; From newcomment.el.
  20187. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20188. "Comment or uncomment each non-blank line in the region.
  20189. Uncomment each non-blank line between BEG and END if it only
  20190. contains commented lines. Otherwise, comment them."
  20191. (save-restriction
  20192. ;; Restrict region
  20193. (narrow-to-region (save-excursion (goto-char beg)
  20194. (skip-chars-forward " \r\t\n" end)
  20195. (line-beginning-position))
  20196. (save-excursion (goto-char end)
  20197. (skip-chars-backward " \r\t\n" beg)
  20198. (line-end-position)))
  20199. (let ((uncommentp
  20200. ;; UNCOMMENTP is non-nil when every non blank line between
  20201. ;; BEG and END is a comment.
  20202. (save-excursion
  20203. (goto-char (point-min))
  20204. (while (and (not (eobp))
  20205. (let ((element (org-element-at-point)))
  20206. (and (eq (org-element-type element) 'comment)
  20207. (goto-char (min (point-max)
  20208. (org-element-property
  20209. :end element)))))))
  20210. (eobp))))
  20211. (if uncommentp
  20212. ;; Only blank lines and comments in region: uncomment it.
  20213. (save-excursion
  20214. (goto-char (point-min))
  20215. (while (not (eobp))
  20216. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20217. (replace-match "" nil nil nil 1))
  20218. (forward-line)))
  20219. ;; Comment each line in region.
  20220. (let ((min-indent (point-max)))
  20221. ;; First find the minimum indentation across all lines.
  20222. (save-excursion
  20223. (goto-char (point-min))
  20224. (while (and (not (eobp)) (not (zerop min-indent)))
  20225. (unless (looking-at "[ \t]*$")
  20226. (setq min-indent (min min-indent (current-indentation))))
  20227. (forward-line)))
  20228. ;; Then loop over all lines.
  20229. (save-excursion
  20230. (goto-char (point-min))
  20231. (while (not (eobp))
  20232. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20233. (org-move-to-column min-indent t)
  20234. (insert comment-start))
  20235. (forward-line))))))))
  20236. ;;; Planning
  20237. ;; This section contains tools to operate on timestamp objects, as
  20238. ;; returned by, e.g. `org-element-context'.
  20239. (defun org-timestamp-has-time-p (timestamp)
  20240. "Non-nil when TIMESTAMP has a time specified."
  20241. (org-element-property :hour-start timestamp))
  20242. (defun org-timestamp-format (timestamp format &optional end utc)
  20243. "Format a TIMESTAMP element into a string.
  20244. FORMAT is a format specifier to be passed to
  20245. `format-time-string'.
  20246. When optional argument END is non-nil, use end of date-range or
  20247. time-range, if possible.
  20248. When optional argument UTC is non-nil, time will be expressed as
  20249. Universal Time."
  20250. (format-time-string
  20251. format
  20252. (apply 'encode-time
  20253. (cons 0
  20254. (mapcar
  20255. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20256. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20257. '(:minute-start :hour-start :day-start :month-start
  20258. :year-start)))))
  20259. utc))
  20260. (defun org-timestamp-split-range (timestamp &optional end)
  20261. "Extract a timestamp object from a date or time range.
  20262. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20263. the end of the range. Otherwise, extract its start.
  20264. Return a new timestamp object sharing the same parent as
  20265. TIMESTAMP."
  20266. (let ((type (org-element-property :type timestamp)))
  20267. (if (memq type '(active inactive diary)) timestamp
  20268. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20269. ;; Set new type.
  20270. (org-element-put-property
  20271. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20272. ;; Copy start properties over end properties if END is
  20273. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20274. (let ((p-alist '((:minute-start . :minute-end)
  20275. (:hour-start . :hour-end)
  20276. (:day-start . :day-end)
  20277. (:month-start . :month-end)
  20278. (:year-start . :year-end))))
  20279. (dolist (p-cell p-alist)
  20280. (org-element-put-property
  20281. split-ts
  20282. (funcall (if end 'car 'cdr) p-cell)
  20283. (org-element-property
  20284. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20285. ;; Eventually refresh `:raw-value'.
  20286. (org-element-put-property split-ts :raw-value nil)
  20287. (org-element-put-property
  20288. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20289. (defun org-timestamp-translate (timestamp &optional boundary)
  20290. "Apply `org-translate-time' on a TIMESTAMP object.
  20291. When optional argument BOUNDARY is non-nil, it is either the
  20292. symbol `start' or `end'. In this case, only translate the
  20293. starting or ending part of TIMESTAMP if it is a date or time
  20294. range. Otherwise, translate both parts."
  20295. (if (and (not boundary)
  20296. (memq (org-element-property :type timestamp)
  20297. '(active-range inactive-range)))
  20298. (concat
  20299. (org-translate-time
  20300. (org-element-property :raw-value
  20301. (org-timestamp-split-range timestamp)))
  20302. "--"
  20303. (org-translate-time
  20304. (org-element-property :raw-value
  20305. (org-timestamp-split-range timestamp t))))
  20306. (org-translate-time
  20307. (org-element-property
  20308. :raw-value
  20309. (if (not boundary) timestamp
  20310. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20311. ;;; Other stuff.
  20312. (defun org-toggle-fixed-width-section (arg)
  20313. "Toggle the fixed-width export.
  20314. If there is no active region, the QUOTE keyword at the current headline is
  20315. inserted or removed. When present, it causes the text between this headline
  20316. and the next to be exported as fixed-width text, and unmodified.
  20317. If there is an active region, this command adds or removes a colon as the
  20318. first character of this line. If the first character of a line is a colon,
  20319. this line is also exported in fixed-width font."
  20320. (interactive "P")
  20321. (let* ((cc 0)
  20322. (regionp (org-region-active-p))
  20323. (beg (if regionp (region-beginning) (point)))
  20324. (end (if regionp (region-end)))
  20325. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20326. (case-fold-search nil)
  20327. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20328. off)
  20329. (if regionp
  20330. (save-excursion
  20331. (goto-char beg)
  20332. (setq cc (current-column))
  20333. (beginning-of-line 1)
  20334. (setq off (looking-at re))
  20335. (while (> nlines 0)
  20336. (setq nlines (1- nlines))
  20337. (beginning-of-line 1)
  20338. (cond
  20339. (arg
  20340. (org-move-to-column cc t)
  20341. (insert ": \n")
  20342. (forward-line -1))
  20343. ((and off (looking-at re))
  20344. (replace-match "" t t nil 1))
  20345. ((not off) (org-move-to-column cc t) (insert ": ")))
  20346. (forward-line 1)))
  20347. (save-excursion
  20348. (org-back-to-heading)
  20349. (cond
  20350. ((looking-at (format org-heading-keyword-regexp-format
  20351. org-quote-string))
  20352. (goto-char (match-end 1))
  20353. (looking-at (concat " +" org-quote-string))
  20354. (replace-match "" t t)
  20355. (when (eolp) (insert " ")))
  20356. ((looking-at org-outline-regexp)
  20357. (goto-char (match-end 0))
  20358. (insert org-quote-string " ")))))))
  20359. (defun org-reftex-citation ()
  20360. "Use reftex-citation to insert a citation into the buffer.
  20361. This looks for a line like
  20362. #+BIBLIOGRAPHY: foo plain option:-d
  20363. and derives from it that foo.bib is the bibliography file relevant
  20364. for this document. It then installs the necessary environment for RefTeX
  20365. to work in this buffer and calls `reftex-citation' to insert a citation
  20366. into the buffer.
  20367. Export of such citations to both LaTeX and HTML is handled by the contributed
  20368. package org-exp-bibtex by Taru Karttunen."
  20369. (interactive)
  20370. (let ((reftex-docstruct-symbol 'rds)
  20371. (reftex-cite-format "\\cite{%l}")
  20372. rds bib)
  20373. (save-excursion
  20374. (save-restriction
  20375. (widen)
  20376. (let ((case-fold-search t)
  20377. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20378. (if (not (save-excursion
  20379. (or (re-search-forward re nil t)
  20380. (re-search-backward re nil t))))
  20381. (error "No bibliography defined in file")
  20382. (setq bib (concat (match-string 1) ".bib")
  20383. rds (list (list 'bib bib)))))))
  20384. (call-interactively 'reftex-citation)))
  20385. ;;;; Functions extending outline functionality
  20386. (defun org-beginning-of-line (&optional arg)
  20387. "Go to the beginning of the current line. If that is invisible, continue
  20388. to a visible line beginning. This makes the function of C-a more intuitive.
  20389. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20390. first attempt, and only move to after the tags when the cursor is already
  20391. beyond the end of the headline."
  20392. (interactive "P")
  20393. (let ((pos (point))
  20394. (special (if (consp org-special-ctrl-a/e)
  20395. (car org-special-ctrl-a/e)
  20396. org-special-ctrl-a/e))
  20397. refpos)
  20398. (if (org-bound-and-true-p visual-line-mode)
  20399. (beginning-of-visual-line 1)
  20400. (beginning-of-line 1))
  20401. (if (and arg (fboundp 'move-beginning-of-line))
  20402. (call-interactively 'move-beginning-of-line)
  20403. (if (bobp)
  20404. nil
  20405. (backward-char 1)
  20406. (if (org-truely-invisible-p)
  20407. (while (and (not (bobp)) (org-truely-invisible-p))
  20408. (backward-char 1)
  20409. (beginning-of-line 1))
  20410. (forward-char 1))))
  20411. (when special
  20412. (cond
  20413. ((and (looking-at org-complex-heading-regexp)
  20414. (= (char-after (match-end 1)) ?\ ))
  20415. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20416. (point-at-eol)))
  20417. (goto-char
  20418. (if (eq special t)
  20419. (cond ((> pos refpos) refpos)
  20420. ((= pos (point)) refpos)
  20421. (t (point)))
  20422. (cond ((> pos (point)) (point))
  20423. ((not (eq last-command this-command)) (point))
  20424. (t refpos)))))
  20425. ((org-at-item-p)
  20426. ;; Being at an item and not looking at an the item means point
  20427. ;; was previously moved to beginning of a visual line, which
  20428. ;; doesn't contain the item. Therefore, do nothing special,
  20429. ;; just stay here.
  20430. (when (looking-at org-list-full-item-re)
  20431. ;; Set special position at first white space character after
  20432. ;; bullet, and check-box, if any.
  20433. (let ((after-bullet
  20434. (let ((box (match-end 3)))
  20435. (if (not box) (match-end 1)
  20436. (let ((after (char-after box)))
  20437. (if (and after (= after ? )) (1+ box) box))))))
  20438. ;; Special case: Move point to special position when
  20439. ;; currently after it or at beginning of line.
  20440. (if (eq special t)
  20441. (when (or (> pos after-bullet) (= (point) pos))
  20442. (goto-char after-bullet))
  20443. ;; Reversed case: Move point to special position when
  20444. ;; point was already at beginning of line and command is
  20445. ;; repeated.
  20446. (when (and (= (point) pos) (eq last-command this-command))
  20447. (goto-char after-bullet))))))))
  20448. (org-no-warnings
  20449. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20450. (defun org-end-of-line (&optional arg)
  20451. "Go to the end of the line.
  20452. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20453. tags on the first attempt, and only move to after the tags when
  20454. the cursor is already beyond the end of the headline."
  20455. (interactive "P")
  20456. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20457. org-special-ctrl-a/e))
  20458. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20459. 'end-of-visual-line)
  20460. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20461. (t 'end-of-line))))
  20462. (if (or (not special) arg) (call-interactively move-fun)
  20463. (let* ((element (save-excursion (beginning-of-line)
  20464. (org-element-at-point)))
  20465. (type (org-element-type element)))
  20466. (cond
  20467. ((memq type '(headline inlinetask))
  20468. (let ((pos (point)))
  20469. (beginning-of-line 1)
  20470. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20471. (if (eq special t)
  20472. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20473. (goto-char (match-beginning 1))
  20474. (goto-char (match-end 0)))
  20475. (if (or (< pos (match-end 0))
  20476. (not (eq this-command last-command)))
  20477. (goto-char (match-end 0))
  20478. (goto-char (match-beginning 1))))
  20479. (call-interactively move-fun))))
  20480. ((org-element-property :hiddenp element)
  20481. ;; If element is hidden, `move-end-of-line' would put point
  20482. ;; after it. Use `end-of-line' to stay on current line.
  20483. (call-interactively 'end-of-line))
  20484. (t (call-interactively move-fun)))))
  20485. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20486. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20487. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20488. (defun org-backward-sentence (&optional arg)
  20489. "Go to beginning of sentence, or beginning of table field.
  20490. This will call `backward-sentence' or `org-table-beginning-of-field',
  20491. depending on context."
  20492. (interactive "P")
  20493. (cond
  20494. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20495. (t (call-interactively 'backward-sentence))))
  20496. (defun org-forward-sentence (&optional arg)
  20497. "Go to end of sentence, or end of table field.
  20498. This will call `forward-sentence' or `org-table-end-of-field',
  20499. depending on context."
  20500. (interactive "P")
  20501. (cond
  20502. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20503. (t (call-interactively 'forward-sentence))))
  20504. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20505. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20506. (defun org-kill-line (&optional arg)
  20507. "Kill line, to tags or end of line."
  20508. (interactive "P")
  20509. (cond
  20510. ((or (not org-special-ctrl-k)
  20511. (bolp)
  20512. (not (org-at-heading-p)))
  20513. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20514. org-ctrl-k-protect-subtree)
  20515. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20516. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20517. (error "C-k aborted - would kill hidden subtree")))
  20518. (call-interactively
  20519. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20520. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20521. (kill-region (point) (match-beginning 1))
  20522. (org-set-tags nil t))
  20523. (t (kill-region (point) (point-at-eol)))))
  20524. (define-key org-mode-map "\C-k" 'org-kill-line)
  20525. (defun org-yank (&optional arg)
  20526. "Yank. If the kill is a subtree, treat it specially.
  20527. This command will look at the current kill and check if is a single
  20528. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20529. cursor is at the beginning of a line or after the stars of a currently
  20530. empty headline, then the yank is handled specially. How exactly depends
  20531. on the value of the following variables, both set by default.
  20532. org-yank-folded-subtrees
  20533. When set, the subtree(s) will be folded after insertion, but only
  20534. if doing so would now swallow text after the yanked text.
  20535. org-yank-adjusted-subtrees
  20536. When set, the subtree will be promoted or demoted in order to
  20537. fit into the local outline tree structure, which means that the level
  20538. will be adjusted so that it becomes the smaller one of the two
  20539. *visible* surrounding headings.
  20540. Any prefix to this command will cause `yank' to be called directly with
  20541. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20542. will just
  20543. plainly yank the text as it is.
  20544. \[1] The test checks if the first non-white line is a heading
  20545. and if there are no other headings with fewer stars."
  20546. (interactive "P")
  20547. (org-yank-generic 'yank arg))
  20548. (defun org-yank-generic (command arg)
  20549. "Perform some yank-like command.
  20550. This function implements the behavior described in the `org-yank'
  20551. documentation. However, it has been generalized to work for any
  20552. interactive command with similar behavior."
  20553. ;; pretend to be command COMMAND
  20554. (setq this-command command)
  20555. (if arg
  20556. (call-interactively command)
  20557. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20558. (and (org-kill-is-subtree-p)
  20559. (or (bolp)
  20560. (and (looking-at "[ \t]*$")
  20561. (string-match
  20562. "\\`\\*+\\'"
  20563. (buffer-substring (point-at-bol) (point)))))))
  20564. swallowp)
  20565. (cond
  20566. ((and subtreep org-yank-folded-subtrees)
  20567. (let ((beg (point))
  20568. end)
  20569. (if (and subtreep org-yank-adjusted-subtrees)
  20570. (org-paste-subtree nil nil 'for-yank)
  20571. (call-interactively command))
  20572. (setq end (point))
  20573. (goto-char beg)
  20574. (when (and (bolp) subtreep
  20575. (not (setq swallowp
  20576. (org-yank-folding-would-swallow-text beg end))))
  20577. (org-with-limited-levels
  20578. (or (looking-at org-outline-regexp)
  20579. (re-search-forward org-outline-regexp-bol end t))
  20580. (while (and (< (point) end) (looking-at org-outline-regexp))
  20581. (hide-subtree)
  20582. (org-cycle-show-empty-lines 'folded)
  20583. (condition-case nil
  20584. (outline-forward-same-level 1)
  20585. (error (goto-char end))))))
  20586. (when swallowp
  20587. (message
  20588. "Inserted text not folded because that would swallow text"))
  20589. (goto-char end)
  20590. (skip-chars-forward " \t\n\r")
  20591. (beginning-of-line 1)
  20592. (push-mark beg 'nomsg)))
  20593. ((and subtreep org-yank-adjusted-subtrees)
  20594. (let ((beg (point-at-bol)))
  20595. (org-paste-subtree nil nil 'for-yank)
  20596. (push-mark beg 'nomsg)))
  20597. (t
  20598. (call-interactively command))))))
  20599. (defun org-yank-folding-would-swallow-text (beg end)
  20600. "Would hide-subtree at BEG swallow any text after END?"
  20601. (let (level)
  20602. (org-with-limited-levels
  20603. (save-excursion
  20604. (goto-char beg)
  20605. (when (or (looking-at org-outline-regexp)
  20606. (re-search-forward org-outline-regexp-bol end t))
  20607. (setq level (org-outline-level)))
  20608. (goto-char end)
  20609. (skip-chars-forward " \t\r\n\v\f")
  20610. (if (or (eobp)
  20611. (and (bolp) (looking-at org-outline-regexp)
  20612. (<= (org-outline-level) level)))
  20613. nil ; Nothing would be swallowed
  20614. t))))) ; something would swallow
  20615. (define-key org-mode-map "\C-y" 'org-yank)
  20616. (defun org-truely-invisible-p ()
  20617. "Check if point is at a character currently not visible.
  20618. This version does not only check the character property, but also
  20619. `visible-mode'."
  20620. ;; Early versions of noutline don't have `outline-invisible-p'.
  20621. (if (org-bound-and-true-p visible-mode)
  20622. nil
  20623. (outline-invisible-p)))
  20624. (defun org-invisible-p2 ()
  20625. "Check if point is at a character currently not visible."
  20626. (save-excursion
  20627. (if (and (eolp) (not (bobp))) (backward-char 1))
  20628. ;; Early versions of noutline don't have `outline-invisible-p'.
  20629. (outline-invisible-p)))
  20630. (defun org-back-to-heading (&optional invisible-ok)
  20631. "Call `outline-back-to-heading', but provide a better error message."
  20632. (condition-case nil
  20633. (outline-back-to-heading invisible-ok)
  20634. (error (error "Before first headline at position %d in buffer %s"
  20635. (point) (current-buffer)))))
  20636. (defun org-before-first-heading-p ()
  20637. "Before first heading?"
  20638. (save-excursion
  20639. (end-of-line)
  20640. (null (re-search-backward org-outline-regexp-bol nil t))))
  20641. (defun org-at-heading-p (&optional ignored)
  20642. (outline-on-heading-p t))
  20643. ;; Compatibility alias with Org versions < 7.8.03
  20644. (defalias 'org-on-heading-p 'org-at-heading-p)
  20645. (defun org-at-comment-p nil
  20646. "Is cursor in a line starting with a # character?"
  20647. (save-excursion
  20648. (beginning-of-line)
  20649. (looking-at "^#")))
  20650. (defun org-at-drawer-p nil
  20651. "Is cursor at a drawer keyword?"
  20652. (save-excursion
  20653. (move-beginning-of-line 1)
  20654. (looking-at org-drawer-regexp)))
  20655. (defun org-at-block-p nil
  20656. "Is cursor at a block keyword?"
  20657. (save-excursion
  20658. (move-beginning-of-line 1)
  20659. (looking-at org-block-regexp)))
  20660. (defun org-point-at-end-of-empty-headline ()
  20661. "If point is at the end of an empty headline, return t, else nil.
  20662. If the heading only contains a TODO keyword, it is still still considered
  20663. empty."
  20664. (and (looking-at "[ \t]*$")
  20665. (when org-todo-line-regexp
  20666. (save-excursion
  20667. (beginning-of-line 1)
  20668. (let ((case-fold-search nil))
  20669. (looking-at org-todo-line-regexp)
  20670. (string= (match-string 3) ""))))))
  20671. (defun org-at-heading-or-item-p ()
  20672. (or (org-at-heading-p) (org-at-item-p)))
  20673. (defun org-at-target-p ()
  20674. (or (org-in-regexp org-radio-target-regexp)
  20675. (org-in-regexp org-target-regexp)))
  20676. ;; Compatibility alias with Org versions < 7.8.03
  20677. (defalias 'org-on-target-p 'org-at-target-p)
  20678. (defun org-up-heading-all (arg)
  20679. "Move to the heading line of which the present line is a subheading.
  20680. This function considers both visible and invisible heading lines.
  20681. With argument, move up ARG levels."
  20682. (if (fboundp 'outline-up-heading-all)
  20683. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20684. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20685. (defun org-up-heading-safe ()
  20686. "Move to the heading line of which the present line is a subheading.
  20687. This version will not throw an error. It will return the level of the
  20688. headline found, or nil if no higher level is found.
  20689. Also, this function will be a lot faster than `outline-up-heading',
  20690. because it relies on stars being the outline starters. This can really
  20691. make a significant difference in outlines with very many siblings."
  20692. (let (start-level re)
  20693. (org-back-to-heading t)
  20694. (setq start-level (funcall outline-level))
  20695. (if (equal start-level 1)
  20696. nil
  20697. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20698. (if (re-search-backward re nil t)
  20699. (funcall outline-level)))))
  20700. (defun org-first-sibling-p ()
  20701. "Is this heading the first child of its parents?"
  20702. (interactive)
  20703. (let ((re org-outline-regexp-bol)
  20704. level l)
  20705. (unless (org-at-heading-p t)
  20706. (error "Not at a heading"))
  20707. (setq level (funcall outline-level))
  20708. (save-excursion
  20709. (if (not (re-search-backward re nil t))
  20710. t
  20711. (setq l (funcall outline-level))
  20712. (< l level)))))
  20713. (defun org-goto-sibling (&optional previous)
  20714. "Goto the next sibling, even if it is invisible.
  20715. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20716. when a sibling was found. When none is found, return nil and don't
  20717. move point."
  20718. (let ((fun (if previous 're-search-backward 're-search-forward))
  20719. (pos (point))
  20720. (re org-outline-regexp-bol)
  20721. level l)
  20722. (when (condition-case nil (org-back-to-heading t) (error nil))
  20723. (setq level (funcall outline-level))
  20724. (catch 'exit
  20725. (or previous (forward-char 1))
  20726. (while (funcall fun re nil t)
  20727. (setq l (funcall outline-level))
  20728. (when (< l level) (goto-char pos) (throw 'exit nil))
  20729. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20730. (goto-char pos)
  20731. nil))))
  20732. (defun org-show-siblings ()
  20733. "Show all siblings of the current headline."
  20734. (save-excursion
  20735. (while (org-goto-sibling) (org-flag-heading nil)))
  20736. (save-excursion
  20737. (while (org-goto-sibling 'previous)
  20738. (org-flag-heading nil))))
  20739. (defun org-goto-first-child ()
  20740. "Goto the first child, even if it is invisible.
  20741. Return t when a child was found. Otherwise don't move point and
  20742. return nil."
  20743. (let (level (pos (point)) (re org-outline-regexp-bol))
  20744. (when (condition-case nil (org-back-to-heading t) (error nil))
  20745. (setq level (outline-level))
  20746. (forward-char 1)
  20747. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20748. (progn (goto-char (match-beginning 0)) t)
  20749. (goto-char pos) nil))))
  20750. (defun org-show-hidden-entry ()
  20751. "Show an entry where even the heading is hidden."
  20752. (save-excursion
  20753. (org-show-entry)))
  20754. (defun org-flag-heading (flag &optional entry)
  20755. "Flag the current heading. FLAG non-nil means make invisible.
  20756. When ENTRY is non-nil, show the entire entry."
  20757. (save-excursion
  20758. (org-back-to-heading t)
  20759. ;; Check if we should show the entire entry
  20760. (if entry
  20761. (progn
  20762. (org-show-entry)
  20763. (save-excursion
  20764. (and (outline-next-heading)
  20765. (org-flag-heading nil))))
  20766. (outline-flag-region (max (point-min) (1- (point)))
  20767. (save-excursion (outline-end-of-heading) (point))
  20768. flag))))
  20769. (defun org-get-next-sibling ()
  20770. "Move to next heading of the same level, and return point.
  20771. If there is no such heading, return nil.
  20772. This is like outline-next-sibling, but invisible headings are ok."
  20773. (let ((level (funcall outline-level)))
  20774. (outline-next-heading)
  20775. (while (and (not (eobp)) (> (funcall outline-level) level))
  20776. (outline-next-heading))
  20777. (if (or (eobp) (< (funcall outline-level) level))
  20778. nil
  20779. (point))))
  20780. (defun org-get-last-sibling ()
  20781. "Move to previous heading of the same level, and return point.
  20782. If there is no such heading, return nil."
  20783. (let ((opoint (point))
  20784. (level (funcall outline-level)))
  20785. (outline-previous-heading)
  20786. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20787. (while (and (> (funcall outline-level) level)
  20788. (not (bobp)))
  20789. (outline-previous-heading))
  20790. (if (< (funcall outline-level) level)
  20791. nil
  20792. (point)))))
  20793. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20794. "Goto to the end of a subtree."
  20795. ;; This contains an exact copy of the original function, but it uses
  20796. ;; `org-back-to-heading', to make it work also in invisible
  20797. ;; trees. And is uses an invisible-ok argument.
  20798. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20799. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20800. ;; with many children and grandchildren etc, this can be much faster
  20801. ;; than the outline version.
  20802. (org-back-to-heading invisible-ok)
  20803. (let ((first t)
  20804. (level (funcall outline-level)))
  20805. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20806. ;; A true heading (not a plain list item), in Org-mode
  20807. ;; This means we can easily find the end by looking
  20808. ;; only for the right number of stars. Using a regexp to do
  20809. ;; this is so much faster than using a Lisp loop.
  20810. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20811. (forward-char 1)
  20812. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20813. ;; something else, do it the slow way
  20814. (while (and (not (eobp))
  20815. (or first (> (funcall outline-level) level)))
  20816. (setq first nil)
  20817. (outline-next-heading)))
  20818. (unless to-heading
  20819. (if (memq (preceding-char) '(?\n ?\^M))
  20820. (progn
  20821. ;; Go to end of line before heading
  20822. (forward-char -1)
  20823. (if (memq (preceding-char) '(?\n ?\^M))
  20824. ;; leave blank line before heading
  20825. (forward-char -1))))))
  20826. (point))
  20827. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20828. "Use Org version in org-mode, for dramatic speed-up."
  20829. (if (derived-mode-p 'org-mode)
  20830. (progn
  20831. (org-end-of-subtree nil t)
  20832. (unless (eobp) (backward-char 1)))
  20833. ad-do-it))
  20834. (defun org-end-of-meta-data-and-drawers ()
  20835. "Jump to the first text after meta data and drawers in the current entry.
  20836. This will move over empty lines, lines with planning time stamps,
  20837. clocking lines, and drawers."
  20838. (org-back-to-heading t)
  20839. (let ((end (save-excursion (outline-next-heading) (point)))
  20840. (re (concat "\\(" org-drawer-regexp "\\)"
  20841. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20842. (forward-line 1)
  20843. (while (re-search-forward re end t)
  20844. (if (not (match-end 1))
  20845. ;; empty or planning line
  20846. (forward-line 1)
  20847. ;; a drawer, find the end
  20848. (re-search-forward "^[ \t]*:END:" end 'move)
  20849. (forward-line 1)))
  20850. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20851. (point)))
  20852. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20853. "Move forward to the ARG'th subheading at same level as this one.
  20854. Stop at the first and last subheadings of a superior heading.
  20855. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20856. non-nil it will also look at invisible ones."
  20857. (interactive "p")
  20858. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20859. (if (and arg (< arg 0))
  20860. (goto-char (point-min))
  20861. (outline-next-heading))
  20862. (org-at-heading-p)
  20863. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20864. (f (if (and arg (< arg 0))
  20865. 're-search-backward
  20866. 're-search-forward))
  20867. (count (if arg (abs arg) 1))
  20868. (result (point)))
  20869. (forward-char (if (and arg (< arg 0)) -1 1))
  20870. (while (and (> count 0)
  20871. (funcall f org-outline-regexp-bol nil 'move))
  20872. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20873. (cond ((< l level) (setq count 0))
  20874. ((and (= l level)
  20875. (or invisible-ok
  20876. (progn
  20877. (goto-char (line-beginning-position))
  20878. (not (outline-invisible-p)))))
  20879. (setq count (1- count))
  20880. (when (eq l level)
  20881. (setq result (point)))))))
  20882. (goto-char result))
  20883. (beginning-of-line 1)))
  20884. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20885. "Move backward to the ARG'th subheading at same level as this one.
  20886. Stop at the first and last subheadings of a superior heading."
  20887. (interactive "p")
  20888. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20889. (defun org-next-block (arg &optional backward block-regexp)
  20890. "Jump to the next block.
  20891. With a prefix argument ARG, jump forward ARG many source blocks.
  20892. When BACKWARD is non-nil, jump to the previous block.
  20893. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20894. (interactive "p")
  20895. (let ((re (or block-regexp org-block-regexp))
  20896. (re-search-fn (or (and backward 're-search-backward)
  20897. 're-search-forward)))
  20898. (if (looking-at re) (forward-char 1))
  20899. (condition-case nil
  20900. (funcall re-search-fn re nil nil arg)
  20901. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20902. (goto-char (match-beginning 0)) (org-show-context)))
  20903. (defun org-previous-block (arg &optional block-regexp)
  20904. "Jump to the previous block.
  20905. With a prefix argument ARG, jump backward ARG many source blocks.
  20906. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20907. (interactive "p")
  20908. (org-next-block arg t block-regexp))
  20909. (defun org-forward-element ()
  20910. "Move forward by one element.
  20911. Move to the next element at the same level, when possible."
  20912. (interactive)
  20913. (cond ((eobp) (user-error "Cannot move further down"))
  20914. ((org-with-limited-levels (org-at-heading-p))
  20915. (let ((origin (point)))
  20916. (goto-char (org-end-of-subtree nil t))
  20917. (unless (org-with-limited-levels (org-at-heading-p))
  20918. (goto-char origin)
  20919. (user-error "Cannot move further down"))))
  20920. (t
  20921. (let* ((elem (org-element-at-point))
  20922. (end (org-element-property :end elem))
  20923. (parent (org-element-property :parent elem)))
  20924. (if (and parent (= (org-element-property :contents-end parent) end))
  20925. (goto-char (org-element-property :end parent))
  20926. (goto-char end))))))
  20927. (defun org-backward-element ()
  20928. "Move backward by one element.
  20929. Move to the previous element at the same level, when possible."
  20930. (interactive)
  20931. (cond ((bobp) (user-error "Cannot move further up"))
  20932. ((org-with-limited-levels (org-at-heading-p))
  20933. ;; At a headline, move to the previous one, if any, or stay
  20934. ;; here.
  20935. (let ((origin (point)))
  20936. (org-with-limited-levels (org-backward-heading-same-level 1))
  20937. ;; When current headline has no sibling above, move to its
  20938. ;; parent.
  20939. (when (= (point) origin)
  20940. (or (org-with-limited-levels (org-up-heading-safe))
  20941. (progn (goto-char origin)
  20942. (user-error "Cannot move further up"))))))
  20943. (t
  20944. (let* ((trail (org-element-at-point 'keep-trail))
  20945. (elem (car trail))
  20946. (prev-elem (nth 1 trail))
  20947. (beg (org-element-property :begin elem)))
  20948. (cond
  20949. ;; Move to beginning of current element if point isn't
  20950. ;; there already.
  20951. ((/= (point) beg) (goto-char beg))
  20952. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20953. ((org-before-first-heading-p) (goto-char (point-min)))
  20954. (t (org-back-to-heading)))))))
  20955. (defun org-up-element ()
  20956. "Move to upper element."
  20957. (interactive)
  20958. (if (org-with-limited-levels (org-at-heading-p))
  20959. (unless (org-up-heading-safe) (error "No surrounding element"))
  20960. (let* ((elem (org-element-at-point))
  20961. (parent (org-element-property :parent elem)))
  20962. (if parent (goto-char (org-element-property :begin parent))
  20963. (if (org-with-limited-levels (org-before-first-heading-p))
  20964. (error "No surrounding element")
  20965. (org-with-limited-levels (org-back-to-heading)))))))
  20966. (defvar org-element-greater-elements)
  20967. (defun org-down-element ()
  20968. "Move to inner element."
  20969. (interactive)
  20970. (let ((element (org-element-at-point)))
  20971. (cond
  20972. ((memq (org-element-type element) '(plain-list table))
  20973. (goto-char (org-element-property :contents-begin element))
  20974. (forward-char))
  20975. ((memq (org-element-type element) org-element-greater-elements)
  20976. ;; If contents are hidden, first disclose them.
  20977. (when (org-element-property :hiddenp element) (org-cycle))
  20978. (goto-char (or (org-element-property :contents-begin element)
  20979. (error "No content for this element"))))
  20980. (t (error "No inner element")))))
  20981. (defun org-drag-element-backward ()
  20982. "Move backward element at point."
  20983. (interactive)
  20984. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20985. (let* ((trail (org-element-at-point 'keep-trail))
  20986. (elem (car trail))
  20987. (prev-elem (nth 1 trail)))
  20988. ;; Error out if no previous element or previous element is
  20989. ;; a parent of the current one.
  20990. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20991. (error "Cannot drag element backward")
  20992. (let ((pos (point)))
  20993. (org-element-swap-A-B prev-elem elem)
  20994. (goto-char (+ (org-element-property :begin prev-elem)
  20995. (- pos (org-element-property :begin elem)))))))))
  20996. (defun org-drag-element-forward ()
  20997. "Move forward element at point."
  20998. (interactive)
  20999. (let* ((pos (point))
  21000. (elem (org-element-at-point)))
  21001. (when (= (point-max) (org-element-property :end elem))
  21002. (error "Cannot drag element forward"))
  21003. (goto-char (org-element-property :end elem))
  21004. (let ((next-elem (org-element-at-point)))
  21005. (when (or (org-element-nested-p elem next-elem)
  21006. (and (eq (org-element-type next-elem) 'headline)
  21007. (not (eq (org-element-type elem) 'headline))))
  21008. (goto-char pos)
  21009. (error "Cannot drag element forward"))
  21010. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21011. ;; body's length (without final blanks) and by the length of
  21012. ;; blanks between ELEM and NEXT-ELEM.
  21013. (let ((size-next (- (save-excursion
  21014. (goto-char (org-element-property :end next-elem))
  21015. (skip-chars-backward " \r\t\n")
  21016. (forward-line)
  21017. ;; Small correction if buffer doesn't end
  21018. ;; with a newline character.
  21019. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21020. (org-element-property :begin next-elem)))
  21021. (size-blank (- (org-element-property :end elem)
  21022. (save-excursion
  21023. (goto-char (org-element-property :end elem))
  21024. (skip-chars-backward " \r\t\n")
  21025. (forward-line)
  21026. (point)))))
  21027. (org-element-swap-A-B elem next-elem)
  21028. (goto-char (+ pos size-next size-blank))))))
  21029. (defun org-drag-line-forward (arg)
  21030. "Drag the line at point ARG lines forward."
  21031. (interactive "p")
  21032. (dotimes (n (abs arg))
  21033. (let ((c (current-column)))
  21034. (if (< 0 arg)
  21035. (progn
  21036. (beginning-of-line 2)
  21037. (transpose-lines 1)
  21038. (beginning-of-line 0))
  21039. (transpose-lines 1)
  21040. (beginning-of-line -1))
  21041. (org-move-to-column c))))
  21042. (defun org-drag-line-backward (arg)
  21043. "Drag the line at point ARG lines backward."
  21044. (interactive "p")
  21045. (org-drag-line-forward (- arg)))
  21046. (defun org-mark-element ()
  21047. "Put point at beginning of this element, mark at end.
  21048. Interactively, if this command is repeated or (in Transient Mark
  21049. mode) if the mark is active, it marks the next element after the
  21050. ones already marked."
  21051. (interactive)
  21052. (let (deactivate-mark)
  21053. (if (and (org-called-interactively-p 'any)
  21054. (or (and (eq last-command this-command) (mark t))
  21055. (and transient-mark-mode mark-active)))
  21056. (set-mark
  21057. (save-excursion
  21058. (goto-char (mark))
  21059. (goto-char (org-element-property :end (org-element-at-point)))))
  21060. (let ((element (org-element-at-point)))
  21061. (end-of-line)
  21062. (push-mark (org-element-property :end element) t t)
  21063. (goto-char (org-element-property :begin element))))))
  21064. (defun org-narrow-to-element ()
  21065. "Narrow buffer to current element."
  21066. (interactive)
  21067. (let ((elem (org-element-at-point)))
  21068. (cond
  21069. ((eq (car elem) 'headline)
  21070. (narrow-to-region
  21071. (org-element-property :begin elem)
  21072. (org-element-property :end elem)))
  21073. ((memq (car elem) org-element-greater-elements)
  21074. (narrow-to-region
  21075. (org-element-property :contents-begin elem)
  21076. (org-element-property :contents-end elem)))
  21077. (t
  21078. (narrow-to-region
  21079. (org-element-property :begin elem)
  21080. (org-element-property :end elem))))))
  21081. (defun org-transpose-element ()
  21082. "Transpose current and previous elements, keeping blank lines between.
  21083. Point is moved after both elements."
  21084. (interactive)
  21085. (org-skip-whitespace)
  21086. (let ((end (org-element-property :end (org-element-at-point))))
  21087. (org-drag-element-backward)
  21088. (goto-char end)))
  21089. (defun org-unindent-buffer ()
  21090. "Un-indent the visible part of the buffer.
  21091. Relative indentation (between items, inside blocks, etc.) isn't
  21092. modified."
  21093. (interactive)
  21094. (unless (eq major-mode 'org-mode)
  21095. (error "Cannot un-indent a buffer not in Org mode"))
  21096. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21097. unindent-tree ; For byte-compiler.
  21098. (unindent-tree
  21099. (function
  21100. (lambda (contents)
  21101. (mapc
  21102. (lambda (element)
  21103. (if (memq (org-element-type element) '(headline section))
  21104. (funcall unindent-tree (org-element-contents element))
  21105. (save-excursion
  21106. (save-restriction
  21107. (narrow-to-region
  21108. (org-element-property :begin element)
  21109. (org-element-property :end element))
  21110. (org-do-remove-indentation)))))
  21111. (reverse contents))))))
  21112. (funcall unindent-tree (org-element-contents parse-tree))))
  21113. (defun org-show-subtree ()
  21114. "Show everything after this heading at deeper levels."
  21115. (interactive)
  21116. (outline-flag-region
  21117. (point)
  21118. (save-excursion
  21119. (org-end-of-subtree t t))
  21120. nil))
  21121. (defun org-show-entry ()
  21122. "Show the body directly following this heading.
  21123. Show the heading too, if it is currently invisible."
  21124. (interactive)
  21125. (save-excursion
  21126. (condition-case nil
  21127. (progn
  21128. (org-back-to-heading t)
  21129. (outline-flag-region
  21130. (max (point-min) (1- (point)))
  21131. (save-excursion
  21132. (if (re-search-forward
  21133. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21134. (match-beginning 1)
  21135. (point-max)))
  21136. nil)
  21137. (org-cycle-hide-drawers 'children))
  21138. (error nil))))
  21139. (defun org-make-options-regexp (kwds &optional extra)
  21140. "Make a regular expression for keyword lines."
  21141. (concat
  21142. "^#\\+\\("
  21143. (mapconcat 'regexp-quote kwds "\\|")
  21144. (if extra (concat "\\|" extra))
  21145. "\\):[ \t]*\\(.*\\)"))
  21146. ;; Make isearch reveal the necessary context
  21147. (defun org-isearch-end ()
  21148. "Reveal context after isearch exits."
  21149. (when isearch-success ; only if search was successful
  21150. (if (featurep 'xemacs)
  21151. ;; Under XEmacs, the hook is run in the correct place,
  21152. ;; we directly show the context.
  21153. (org-show-context 'isearch)
  21154. ;; In Emacs the hook runs *before* restoring the overlays.
  21155. ;; So we have to use a one-time post-command-hook to do this.
  21156. ;; (Emacs 22 has a special variable, see function `org-mode')
  21157. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21158. isearch-mode-end-hook-quit)
  21159. ;; Only when the isearch was not quitted.
  21160. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21161. 'append 'local)))
  21162. (org-fix-ellipsis-at-bol)))
  21163. (defun org-isearch-post-command ()
  21164. "Remove self from hook, and show context."
  21165. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21166. (org-show-context 'isearch))
  21167. ;;;; Integration with and fixes for other packages
  21168. ;;; Imenu support
  21169. (defvar org-imenu-markers nil
  21170. "All markers currently used by Imenu.")
  21171. (make-variable-buffer-local 'org-imenu-markers)
  21172. (defun org-imenu-new-marker (&optional pos)
  21173. "Return a new marker for use by Imenu, and remember the marker."
  21174. (let ((m (make-marker)))
  21175. (move-marker m (or pos (point)))
  21176. (push m org-imenu-markers)
  21177. m))
  21178. (defun org-imenu-get-tree ()
  21179. "Produce the index for Imenu."
  21180. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21181. (setq org-imenu-markers nil)
  21182. (let* ((n org-imenu-depth)
  21183. (re (concat "^" (org-get-limited-outline-regexp)))
  21184. (subs (make-vector (1+ n) nil))
  21185. (last-level 0)
  21186. m level head0 head)
  21187. (save-excursion
  21188. (save-restriction
  21189. (widen)
  21190. (goto-char (point-max))
  21191. (while (re-search-backward re nil t)
  21192. (setq level (org-reduced-level (funcall outline-level)))
  21193. (when (and (<= level n)
  21194. (looking-at org-complex-heading-regexp)
  21195. (setq head0 (org-match-string-no-properties 4)))
  21196. (setq head (org-link-display-format head0)
  21197. m (org-imenu-new-marker))
  21198. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21199. (if (>= level last-level)
  21200. (push (cons head m) (aref subs level))
  21201. (push (cons head (aref subs (1+ level))) (aref subs level))
  21202. (loop for i from (1+ level) to n do (aset subs i nil)))
  21203. (setq last-level level)))))
  21204. (aref subs 1)))
  21205. (eval-after-load "imenu"
  21206. '(progn
  21207. (add-hook 'imenu-after-jump-hook
  21208. (lambda ()
  21209. (if (derived-mode-p 'org-mode)
  21210. (org-show-context 'org-goto))))))
  21211. (defun org-link-display-format (link)
  21212. "Replace a link with either the description, or the link target
  21213. if no description is present"
  21214. (save-match-data
  21215. (if (string-match org-bracket-link-analytic-regexp link)
  21216. (replace-match (if (match-end 5)
  21217. (match-string 5 link)
  21218. (concat (match-string 1 link)
  21219. (match-string 3 link)))
  21220. nil t link)
  21221. link)))
  21222. (defun org-toggle-link-display ()
  21223. "Toggle the literal or descriptive display of links."
  21224. (interactive)
  21225. (if org-descriptive-links
  21226. (progn (org-remove-from-invisibility-spec '(org-link))
  21227. (org-restart-font-lock)
  21228. (setq org-descriptive-links nil))
  21229. (progn (add-to-invisibility-spec '(org-link))
  21230. (org-restart-font-lock)
  21231. (setq org-descriptive-links t))))
  21232. ;; Speedbar support
  21233. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21234. "Overlay marking the agenda restriction line in speedbar.")
  21235. (overlay-put org-speedbar-restriction-lock-overlay
  21236. 'face 'org-agenda-restriction-lock)
  21237. (overlay-put org-speedbar-restriction-lock-overlay
  21238. 'help-echo "Agendas are currently limited to this item.")
  21239. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21240. (defun org-speedbar-set-agenda-restriction ()
  21241. "Restrict future agenda commands to the location at point in speedbar.
  21242. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21243. (interactive)
  21244. (require 'org-agenda)
  21245. (let (p m tp np dir txt)
  21246. (cond
  21247. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21248. 'org-imenu t))
  21249. (setq m (get-text-property p 'org-imenu-marker))
  21250. (with-current-buffer (marker-buffer m)
  21251. (goto-char m)
  21252. (org-agenda-set-restriction-lock 'subtree)))
  21253. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21254. 'speedbar-function 'speedbar-find-file))
  21255. (setq tp (previous-single-property-change
  21256. (1+ p) 'speedbar-function)
  21257. np (next-single-property-change
  21258. tp 'speedbar-function)
  21259. dir (speedbar-line-directory)
  21260. txt (buffer-substring-no-properties (or tp (point-min))
  21261. (or np (point-max))))
  21262. (with-current-buffer (find-file-noselect
  21263. (let ((default-directory dir))
  21264. (expand-file-name txt)))
  21265. (unless (derived-mode-p 'org-mode)
  21266. (error "Cannot restrict to non-Org-mode file"))
  21267. (org-agenda-set-restriction-lock 'file)))
  21268. (t (error "Don't know how to restrict Org-mode's agenda")))
  21269. (move-overlay org-speedbar-restriction-lock-overlay
  21270. (point-at-bol) (point-at-eol))
  21271. (setq current-prefix-arg nil)
  21272. (org-agenda-maybe-redo)))
  21273. (eval-after-load "speedbar"
  21274. '(progn
  21275. (speedbar-add-supported-extension ".org")
  21276. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21277. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21278. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21279. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21280. (add-hook 'speedbar-visiting-tag-hook
  21281. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21282. ;;; Fixes and Hacks for problems with other packages
  21283. ;; Make flyspell not check words in links, to not mess up our keymap
  21284. (defvar org-element-affiliated-keywords) ; From org-element.el
  21285. (defvar org-element-block-name-alist) ; From org-element.el
  21286. (defun org-mode-flyspell-verify ()
  21287. "Don't let flyspell put overlays at active buttons, or on
  21288. {todo,all-time,additional-option-like}-keywords."
  21289. (let ((pos (max (1- (point)) (point-min)))
  21290. (word (thing-at-point 'word)))
  21291. (and (not (get-text-property pos 'keymap))
  21292. (not (get-text-property pos 'org-no-flyspell))
  21293. (not (member word org-todo-keywords-1))
  21294. (not (member word org-all-time-keywords))
  21295. (not (member word org-options-keywords))
  21296. (not (member word (mapcar 'car org-startup-options)))
  21297. (not (member-ignore-case word org-element-affiliated-keywords))
  21298. (not (member-ignore-case word (org-get-export-keywords)))
  21299. (not (member-ignore-case
  21300. word (mapcar 'car org-element-block-name-alist)))
  21301. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21302. (defun org-remove-flyspell-overlays-in (beg end)
  21303. "Remove flyspell overlays in region."
  21304. (and (org-bound-and-true-p flyspell-mode)
  21305. (fboundp 'flyspell-delete-region-overlays)
  21306. (flyspell-delete-region-overlays beg end))
  21307. (add-text-properties beg end '(org-no-flyspell t)))
  21308. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21309. (eval-after-load "bookmark"
  21310. '(if (boundp 'bookmark-after-jump-hook)
  21311. ;; We can use the hook
  21312. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21313. ;; Hook not available, use advice
  21314. (defadvice bookmark-jump (after org-make-visible activate)
  21315. "Make the position visible."
  21316. (org-bookmark-jump-unhide))))
  21317. ;; Make sure saveplace shows the location if it was hidden
  21318. (eval-after-load "saveplace"
  21319. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21320. "Make the position visible."
  21321. (org-bookmark-jump-unhide)))
  21322. ;; Make sure ecb shows the location if it was hidden
  21323. (eval-after-load "ecb"
  21324. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21325. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21326. (if (derived-mode-p 'org-mode)
  21327. (org-show-context))))
  21328. (defun org-bookmark-jump-unhide ()
  21329. "Unhide the current position, to show the bookmark location."
  21330. (and (derived-mode-p 'org-mode)
  21331. (or (outline-invisible-p)
  21332. (save-excursion (goto-char (max (point-min) (1- (point))))
  21333. (outline-invisible-p)))
  21334. (org-show-context 'bookmark-jump)))
  21335. ;; Make session.el ignore our circular variable
  21336. (eval-after-load "session"
  21337. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21338. ;;;; Experimental code
  21339. (defun org-closed-in-range ()
  21340. "Sparse tree of items closed in a certain time range.
  21341. Still experimental, may disappear in the future."
  21342. (interactive)
  21343. ;; Get the time interval from the user.
  21344. (let* ((time1 (org-float-time
  21345. (org-read-date nil 'to-time nil "Starting date: ")))
  21346. (time2 (org-float-time
  21347. (org-read-date nil 'to-time nil "End date:")))
  21348. ;; callback function
  21349. (callback (lambda ()
  21350. (let ((time
  21351. (org-float-time
  21352. (apply 'encode-time
  21353. (org-parse-time-string
  21354. (match-string 1))))))
  21355. ;; check if time in interval
  21356. (and (>= time time1) (<= time time2))))))
  21357. ;; make tree, check each match with the callback
  21358. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21359. ;;;; Finish up
  21360. (provide 'org)
  21361. (run-hooks 'org-load-hook)
  21362. ;;; org.el ends here