org.el 953 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode 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. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  85. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  86. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  87. ;;
  88. ;; In Org buffers, the value of `outline-regexp' is that of
  89. ;; `org-outline-regexp'. The only function still directly relying on
  90. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  91. ;; job when `orgstruct-mode' is active.
  92. (defvar org-outline-regexp "\\*+ "
  93. "Regexp to match Org headlines.")
  94. (defvar org-outline-regexp-bol "^\\*+ "
  95. "Regexp to match Org headlines.
  96. This is similar to `org-outline-regexp' but additionally makes
  97. sure that we are at the beginning of the line.")
  98. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  99. "Matches a headline, putting stars and text into groups.
  100. Stars are put in group 1 and the trimmed body in group 2.")
  101. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  102. (unless (boundp 'calendar-view-holidays-initially-flag)
  103. (org-defvaralias 'calendar-view-holidays-initially-flag
  104. 'view-calendar-holidays-initially))
  105. (unless (boundp 'calendar-view-diary-initially-flag)
  106. (org-defvaralias 'calendar-view-diary-initially-flag
  107. 'view-diary-entries-initially))
  108. (unless (boundp 'diary-fancy-buffer)
  109. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  112. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  113. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  114. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  115. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  116. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  117. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  118. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  119. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  120. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  121. (declare-function org-clock-update-time-maybe "org-clock" ())
  122. (declare-function org-clocktable-shift "org-clock" (dir n))
  123. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  124. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  125. (declare-function orgtbl-mode "org-table" (&optional arg))
  126. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  127. (declare-function org-beamer-mode "ox-beamer" ())
  128. (declare-function org-table-blank-field "org-table" ())
  129. (declare-function org-table-edit-field "org-table" (arg))
  130. (declare-function org-table-insert-row "org-table" (&optional arg))
  131. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  132. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  133. (declare-function org-id-get-create "org-id" (&optional force))
  134. (declare-function org-add-archive-files "org-archive" (files))
  135. (declare-function org-id-find-id-file "org-id" (id))
  136. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  137. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  138. (declare-function org-agenda-redo "org-agenda" (&optional all))
  139. (declare-function org-table-align "org-table" ())
  140. (declare-function org-table-begin "org-table" (&optional table-type))
  141. (declare-function org-table-blank-field "org-table" ())
  142. (declare-function org-table-end "org-table" (&optional table-type))
  143. (declare-function org-table-end-of-field "org-table" (&optional n))
  144. (declare-function org-table-insert-row "org-table" (&optional arg))
  145. (declare-function org-table-paste-rectangle "org-table" ())
  146. (declare-function org-table-maybe-eval-formula "org-table" ())
  147. (declare-function org-table-maybe-recalculate-line "org-table" ())
  148. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  149. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  150. (declare-function org-element-at-point "org-element" ())
  151. (declare-function org-element-cache-reset "org-element" (&optional all))
  152. (declare-function org-element-cache-refresh "org-element" (pos))
  153. (declare-function org-element-contents "org-element" (element))
  154. (declare-function org-element-context "org-element" (&optional element))
  155. (declare-function org-element-interpret-data "org-element"
  156. (data &optional parent))
  157. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  158. (declare-function org-element-parse-buffer "org-element"
  159. (&optional granularity visible-only))
  160. (declare-function org-element-property "org-element" (property element))
  161. (declare-function org-element-put-property "org-element"
  162. (element property value))
  163. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  164. (declare-function org-element-parse-buffer "org-element"
  165. (&optional granularity visible-only))
  166. (declare-function org-element-type "org-element" (element))
  167. (declare-function org-element-update-syntax "org-element" ())
  168. (defsubst org-uniquify (list)
  169. "Non-destructively remove duplicate elements from LIST."
  170. (let ((res (copy-sequence list))) (delete-dups res)))
  171. (defsubst org-get-at-bol (property)
  172. "Get text property PROPERTY at the beginning of line."
  173. (get-text-property (point-at-bol) property))
  174. (defsubst org-trim (s)
  175. "Remove whitespace at the beginning and the end of string S."
  176. (replace-regexp-in-string
  177. "\\`[ \t\n\r]+" ""
  178. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  179. ;; load languages based on value of `org-babel-load-languages'
  180. (defvar org-babel-load-languages)
  181. ;;;###autoload
  182. (defun org-babel-do-load-languages (sym value)
  183. "Load the languages defined in `org-babel-load-languages'."
  184. (set-default sym value)
  185. (mapc (lambda (pair)
  186. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  187. (if active
  188. (progn
  189. (require (intern (concat "ob-" lang))))
  190. (progn
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-execute:" lang)))
  193. (funcall 'fmakunbound
  194. (intern (concat "org-babel-expand-body:" lang)))))))
  195. org-babel-load-languages))
  196. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  197. ;;;###autoload
  198. (defun org-babel-load-file (file &optional compile)
  199. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  200. This function exports the source code using `org-babel-tangle'
  201. and then loads the resulting file using `load-file'. With prefix
  202. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  203. file to byte-code before it is loaded."
  204. (interactive "fFile to load: \nP")
  205. (let* ((age (lambda (file)
  206. (float-time
  207. (time-subtract (current-time)
  208. (nth 5 (or (file-attributes (file-truename file))
  209. (file-attributes file)))))))
  210. (base-name (file-name-sans-extension file))
  211. (exported-file (concat base-name ".el")))
  212. ;; tangle if the org-mode file is newer than the elisp file
  213. (unless (and (file-exists-p exported-file)
  214. (> (funcall age file) (funcall age exported-file)))
  215. ;; Tangle-file traversal returns reversed list of tangled files
  216. ;; and we want to evaluate the first target.
  217. (setq exported-file
  218. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  219. (message "%s %s"
  220. (if compile
  221. (progn (byte-compile-file exported-file 'load)
  222. "Compiled and loaded")
  223. (progn (load-file exported-file) "Loaded"))
  224. exported-file)))
  225. (defcustom org-babel-load-languages '((emacs-lisp . t))
  226. "Languages which can be evaluated in Org-mode buffers.
  227. This list can be used to load support for any of the languages
  228. below, note that each language will depend on a different set of
  229. system executables and/or Emacs modes. When a language is
  230. \"loaded\", then code blocks in that language can be evaluated
  231. with `org-babel-execute-src-block' bound by default to C-c
  232. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  233. be set to remove code block evaluation from the C-c C-c
  234. keybinding. By default only Emacs Lisp (which has no
  235. requirements) is loaded."
  236. :group 'org-babel
  237. :set 'org-babel-do-load-languages
  238. :version "24.1"
  239. :type '(alist :tag "Babel Languages"
  240. :key-type
  241. (choice
  242. (const :tag "Awk" awk)
  243. (const :tag "C" C)
  244. (const :tag "R" R)
  245. (const :tag "Asymptote" asymptote)
  246. (const :tag "Calc" calc)
  247. (const :tag "Clojure" clojure)
  248. (const :tag "CSS" css)
  249. (const :tag "Ditaa" ditaa)
  250. (const :tag "Dot" dot)
  251. (const :tag "Emacs Lisp" emacs-lisp)
  252. (const :tag "Forth" forth)
  253. (const :tag "Fortran" fortran)
  254. (const :tag "Gnuplot" gnuplot)
  255. (const :tag "Haskell" haskell)
  256. (const :tag "IO" io)
  257. (const :tag "J" J)
  258. (const :tag "Java" java)
  259. (const :tag "Javascript" js)
  260. (const :tag "LaTeX" latex)
  261. (const :tag "Ledger" ledger)
  262. (const :tag "Lilypond" lilypond)
  263. (const :tag "Lisp" lisp)
  264. (const :tag "Makefile" makefile)
  265. (const :tag "Maxima" maxima)
  266. (const :tag "Matlab" matlab)
  267. (const :tag "Mscgen" mscgen)
  268. (const :tag "Ocaml" ocaml)
  269. (const :tag "Octave" octave)
  270. (const :tag "Org" org)
  271. (const :tag "Perl" perl)
  272. (const :tag "Pico Lisp" picolisp)
  273. (const :tag "PlantUML" plantuml)
  274. (const :tag "Python" python)
  275. (const :tag "Ruby" ruby)
  276. (const :tag "Sass" sass)
  277. (const :tag "Scala" scala)
  278. (const :tag "Scheme" scheme)
  279. (const :tag "Screen" screen)
  280. (const :tag "Shell Script" shell)
  281. (const :tag "Shen" shen)
  282. (const :tag "Sql" sql)
  283. (const :tag "Sqlite" sqlite)
  284. (const :tag "ebnf2ps" ebnf2ps))
  285. :value-type (boolean :tag "Activate" :value t)))
  286. ;;;; Customization variables
  287. (defcustom org-clone-delete-id nil
  288. "Remove ID property of clones of a subtree.
  289. When non-nil, clones of a subtree don't inherit the ID property.
  290. Otherwise they inherit the ID property with a new unique
  291. identifier."
  292. :type 'boolean
  293. :version "24.1"
  294. :group 'org-id)
  295. ;;; Version
  296. (org-check-version)
  297. ;;;###autoload
  298. (defun org-version (&optional here full message)
  299. "Show the org-mode version.
  300. Interactively, or when MESSAGE is non-nil, show it in echo area.
  301. With prefix argument, or when HERE is non-nil, insert it at point.
  302. In non-interactive uses, a reduced version string is output unless
  303. FULL is given."
  304. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  305. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  306. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  307. (load-suffixes (list ".el"))
  308. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  309. (org-trash (or
  310. (and (fboundp 'org-release) (fboundp 'org-git-version))
  311. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  312. (load-suffixes save-load-suffixes)
  313. (org-version (org-release))
  314. (git-version (org-git-version))
  315. (version (format "Org-mode version %s (%s @ %s)"
  316. org-version
  317. git-version
  318. (if org-install-dir
  319. (if (string= org-dir org-install-dir)
  320. org-install-dir
  321. (concat "mixed installation! " org-install-dir " and " org-dir))
  322. "org-loaddefs.el can not be found!")))
  323. (version1 (if full version org-version)))
  324. (when here (insert version1))
  325. (when message (message "%s" version1))
  326. version1))
  327. (defconst org-version (org-version))
  328. ;;; Syntax Constants
  329. ;;;; Block
  330. (defconst org-block-regexp
  331. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  332. "Regular expression for hiding blocks.")
  333. (defconst org-dblock-start-re
  334. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  335. "Matches the start line of a dynamic block, with parameters.")
  336. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  337. "Matches the end of a dynamic block.")
  338. ;;;; Clock and Planning
  339. (defconst org-clock-string "CLOCK:"
  340. "String used as prefix for timestamps clocking work hours on an item.")
  341. (defvar org-closed-string "CLOSED:"
  342. "String used as the prefix for timestamps logging closing a TODO entry.")
  343. (defvar org-deadline-string "DEADLINE:"
  344. "String to mark deadline entries.
  345. A deadline is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-deadline].")
  348. (defvar org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].")
  353. (defconst org-ds-keyword-length
  354. (+ 2
  355. (apply #'max
  356. (mapcar #'length
  357. (list org-deadline-string org-scheduled-string
  358. org-clock-string org-closed-string))))
  359. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  360. (defconst org-planning-line-re
  361. (concat "^[ \t]*"
  362. (regexp-opt
  363. (list org-closed-string org-deadline-string org-scheduled-string)
  364. t))
  365. "Matches a line with planning info.
  366. Matched keyword is in group 1.")
  367. (defconst org-clock-line-re
  368. (concat "^[ \t]*" org-clock-string)
  369. "Matches a line with clock info.")
  370. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  371. "Matches the DEADLINE keyword.")
  372. (defconst org-deadline-time-regexp
  373. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  374. "Matches the DEADLINE keyword together with a time stamp.")
  375. (defconst org-deadline-time-hour-regexp
  376. (concat "\\<" org-deadline-string
  377. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  378. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  379. (defconst org-deadline-line-regexp
  380. (concat "\\<\\(" org-deadline-string "\\).*")
  381. "Matches the DEADLINE keyword and the rest of the line.")
  382. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  383. "Matches the SCHEDULED keyword.")
  384. (defconst org-scheduled-time-regexp
  385. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  386. "Matches the SCHEDULED keyword together with a time stamp.")
  387. (defconst org-scheduled-time-hour-regexp
  388. (concat "\\<" org-scheduled-string
  389. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  390. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  391. (defconst org-closed-time-regexp
  392. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  393. "Matches the CLOSED keyword together with a time stamp.")
  394. (defconst org-keyword-time-regexp
  395. (concat "\\<"
  396. (regexp-opt
  397. (list org-scheduled-string org-deadline-string org-closed-string
  398. org-clock-string)
  399. t)
  400. " *[[<]\\([^]>]+\\)[]>]")
  401. "Matches any of the 4 keywords, together with the time stamp.")
  402. (defconst org-keyword-time-not-clock-regexp
  403. (concat
  404. "\\<"
  405. (regexp-opt
  406. (list org-scheduled-string org-deadline-string org-closed-string) t)
  407. " *[[<]\\([^]>]+\\)[]>]")
  408. "Matches any of the 3 keywords, together with the time stamp.")
  409. (defconst org-maybe-keyword-time-regexp
  410. (concat "\\(\\<"
  411. (regexp-opt
  412. (list org-scheduled-string org-deadline-string org-closed-string
  413. org-clock-string)
  414. t)
  415. "\\)?"
  416. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  417. "\\|"
  418. "<%%([^\r\n>]*>\\)")
  419. "Matches a timestamp, possibly preceded by a keyword.")
  420. (defconst org-all-time-keywords
  421. (mapcar (lambda (w) (substring w 0 -1))
  422. (list org-scheduled-string org-deadline-string
  423. org-clock-string org-closed-string))
  424. "List of time keywords.")
  425. ;;;; Drawer
  426. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  427. "Matches first or last line of a hidden block.
  428. Group 1 contains drawer's name or \"END\".")
  429. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  430. "Regular expression matching the first line of a property drawer.")
  431. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  432. "Regular expression matching the last line of a property drawer.")
  433. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  434. "Regular expression matching the first line of a clock drawer.")
  435. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  436. "Regular expression matching the last line of a clock drawer.")
  437. (defconst org-property-drawer-re
  438. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  439. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  440. "[ \t]*:END:[ \t]*$")
  441. "Matches an entire property drawer.")
  442. (defconst org-clock-drawer-re
  443. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  444. org-clock-drawer-end-re "\\)\n?")
  445. "Matches an entire clock drawer.")
  446. ;;;; Headline
  447. (defconst org-heading-keyword-regexp-format
  448. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  449. "Printf format for a regexp matching a headline with some keyword.
  450. This regexp will match the headline of any node which has the
  451. exact keyword that is put into the format. The keyword isn't in
  452. any group by default, but the stars and the body are.")
  453. (defconst org-heading-keyword-maybe-regexp-format
  454. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  455. "Printf format for a regexp matching a headline, possibly with some keyword.
  456. This regexp can match any headline with the specified keyword, or
  457. without a keyword. The keyword isn't in any group by default,
  458. but the stars and the body are.")
  459. (defconst org-archive-tag "ARCHIVE"
  460. "The tag that marks a subtree as archived.
  461. An archived subtree does not open during visibility cycling, and does
  462. not contribute to the agenda listings.")
  463. (defconst org-comment-string "COMMENT"
  464. "Entries starting with this keyword will never be exported.
  465. An entry can be toggled between COMMENT and normal with
  466. \\[org-toggle-comment].")
  467. ;;;; LaTeX Environments and Fragments
  468. (defconst org-latex-regexps
  469. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  470. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  471. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  472. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  473. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  474. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  475. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  476. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  477. "Regular expressions for matching embedded LaTeX.")
  478. ;;;; Node Property
  479. (defconst org-effort-property "Effort"
  480. "The property that is being used to keep track of effort estimates.
  481. Effort estimates given in this property need to have the format H:MM.")
  482. ;;;; Table
  483. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  484. "Detect an org-type or table-type table.")
  485. (defconst org-table-line-regexp "^[ \t]*|"
  486. "Detect an org-type table line.")
  487. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  488. "Detect an org-type table line.")
  489. (defconst org-table-hline-regexp "^[ \t]*|-"
  490. "Detect an org-type table hline.")
  491. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  492. "Detect a table-type table hline.")
  493. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  494. "Detect the first line outside a table when searching from within it.
  495. This works for both table types.")
  496. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  497. "Detect a #+TBLFM line.")
  498. ;;;; Timestamp
  499. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  500. "Regular expression for fast time stamp matching.")
  501. (defconst org-ts-regexp-inactive
  502. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  503. "Regular expression for fast inactive time stamp matching.")
  504. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  505. "Regular expression for fast time stamp matching.")
  506. (defconst org-ts-regexp0
  507. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  508. "Regular expression matching time strings for analysis.
  509. This one does not require the space after the date, so it can be used
  510. on a string that terminates immediately after the date.")
  511. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  512. "Regular expression matching time strings for analysis.")
  513. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  514. "Regular expression matching time stamps, with groups.")
  515. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  516. "Regular expression matching time stamps (also [..]), with groups.")
  517. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  518. "Regular expression matching a time stamp range.")
  519. (defconst org-tr-regexp-both
  520. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  521. "Regular expression matching a time stamp range.")
  522. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  523. org-ts-regexp "\\)?")
  524. "Regular expression matching a time stamp or time stamp range.")
  525. (defconst org-tsr-regexp-both
  526. (concat org-ts-regexp-both "\\(--?-?"
  527. org-ts-regexp-both "\\)?")
  528. "Regular expression matching a time stamp or time stamp range.
  529. The time stamps may be either active or inactive.")
  530. (defconst org-repeat-re
  531. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  532. "Regular expression for specifying repeated events.
  533. After a match, group 1 contains the repeat expression.")
  534. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  535. "Formats for `format-time-string' which are used for time stamps.")
  536. ;;; The custom variables
  537. (defgroup org nil
  538. "Outline-based notes management and organizer."
  539. :tag "Org"
  540. :group 'outlines
  541. :group 'calendar)
  542. (defcustom org-mode-hook nil
  543. "Mode hook for Org-mode, run after the mode was turned on."
  544. :group 'org
  545. :type 'hook)
  546. (defcustom org-load-hook nil
  547. "Hook that is run after org.el has been loaded."
  548. :group 'org
  549. :type 'hook)
  550. (defcustom org-log-buffer-setup-hook nil
  551. "Hook that is run after an Org log buffer is created."
  552. :group 'org
  553. :version "24.1"
  554. :type 'hook)
  555. (defvar org-modules) ; defined below
  556. (defvar org-modules-loaded nil
  557. "Have the modules been loaded already?")
  558. (defun org-load-modules-maybe (&optional force)
  559. "Load all extensions listed in `org-modules'."
  560. (when (or force (not org-modules-loaded))
  561. (mapc (lambda (ext)
  562. (condition-case nil (require ext)
  563. (error (message "Problems while trying to load feature `%s'" ext))))
  564. org-modules)
  565. (setq org-modules-loaded t)))
  566. (defun org-set-modules (var value)
  567. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  568. (set var value)
  569. (when (featurep 'org)
  570. (org-load-modules-maybe 'force)
  571. (org-element-cache-reset 'all)))
  572. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  573. "Modules that should always be loaded together with org.el.
  574. If a description starts with <C>, the file is not part of Emacs
  575. and loading it will require that you have downloaded and properly
  576. installed the Org mode distribution.
  577. You can also use this system to load external packages (i.e. neither Org
  578. core modules, nor modules from the CONTRIB directory). Just add symbols
  579. to the end of the list. If the package is called org-xyz.el, then you need
  580. to add the symbol `xyz', and the package must have a call to:
  581. \(provide 'org-xyz)
  582. For export specific modules, see also `org-export-backends'."
  583. :group 'org
  584. :set 'org-set-modules
  585. :version "24.4"
  586. :package-version '(Org . "8.0")
  587. :type
  588. '(set :greedy t
  589. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  590. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  591. (const :tag " crypt: Encryption of subtrees" org-crypt)
  592. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  593. (const :tag " docview: Links to doc-view buffers" org-docview)
  594. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  595. (const :tag " habit: Track your consistency with habits" org-habit)
  596. (const :tag " id: Global IDs for identifying entries" org-id)
  597. (const :tag " info: Links to Info nodes" org-info)
  598. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  599. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  600. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  601. (const :tag " mouse: Additional mouse support" org-mouse)
  602. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  603. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  604. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  605. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  606. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  607. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  608. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  609. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  610. (const :tag "C collector: Collect properties into tables" org-collector)
  611. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  612. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  613. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  614. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  615. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  616. (const :tag "C eval: Include command output as text" org-eval)
  617. (const :tag "C eww: Store link to url of eww" org-eww)
  618. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  619. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  620. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  621. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  622. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  623. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  624. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  625. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  626. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  627. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  628. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  629. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  630. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  631. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  632. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  633. (const :tag "C registry: A registry for Org-mode links" org-registry)
  634. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  635. (const :tag "C secretary: Team management with org-mode" org-secretary)
  636. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  637. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  638. (const :tag "C track: Keep up with Org-mode development" org-track)
  639. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  640. (const :tag "C vm: Links to VM folders/messages" org-vm)
  641. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  642. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  643. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  644. (defvar org-export-registered-backends) ; From ox.el.
  645. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  646. (declare-function org-export-backend-name "ox" (backend))
  647. (defcustom org-export-backends '(ascii html icalendar latex)
  648. "List of export back-ends that should be always available.
  649. If a description starts with <C>, the file is not part of Emacs
  650. and loading it will require that you have downloaded and properly
  651. installed the Org mode distribution.
  652. Unlike to `org-modules', libraries in this list will not be
  653. loaded along with Org, but only once the export framework is
  654. needed.
  655. This variable needs to be set before org.el is loaded. If you
  656. need to make a change while Emacs is running, use the customize
  657. interface or run the following code, where VAL stands for the new
  658. value of the variable, after updating it:
  659. \(progn
  660. \(setq org-export-registered-backends
  661. \(org-remove-if-not
  662. \(lambda (backend)
  663. \(let ((name (org-export-backend-name backend)))
  664. \(or (memq name val)
  665. \(catch 'parentp
  666. \(dolist (b val)
  667. \(and (org-export-derived-backend-p b name)
  668. \(throw 'parentp t)))))))
  669. org-export-registered-backends))
  670. \(let ((new-list (mapcar #'org-export-backend-name
  671. org-export-registered-backends)))
  672. \(dolist (backend val)
  673. \(cond
  674. \((not (load (format \"ox-%s\" backend) t t))
  675. \(message \"Problems while trying to load export back-end `%s'\"
  676. backend))
  677. \((not (memq backend new-list)) (push backend new-list))))
  678. \(set-default 'org-export-backends new-list)))
  679. Adding a back-end to this list will also pull the back-end it
  680. depends on, if any."
  681. :group 'org
  682. :group 'org-export
  683. :version "24.4"
  684. :package-version '(Org . "8.0")
  685. :initialize 'custom-initialize-set
  686. :set (lambda (var val)
  687. (if (not (featurep 'ox)) (set-default var val)
  688. ;; Any back-end not required anymore (not present in VAL and not
  689. ;; a parent of any back-end in the new value) is removed from the
  690. ;; list of registered back-ends.
  691. (setq org-export-registered-backends
  692. (org-remove-if-not
  693. (lambda (backend)
  694. (let ((name (org-export-backend-name backend)))
  695. (or (memq name val)
  696. (catch 'parentp
  697. (dolist (b val)
  698. (and (org-export-derived-backend-p b name)
  699. (throw 'parentp t)))))))
  700. org-export-registered-backends))
  701. ;; Now build NEW-LIST of both new back-ends and required
  702. ;; parents.
  703. (let ((new-list (mapcar #'org-export-backend-name
  704. org-export-registered-backends)))
  705. (dolist (backend val)
  706. (cond
  707. ((not (load (format "ox-%s" backend) t t))
  708. (message "Problems while trying to load export back-end `%s'"
  709. backend))
  710. ((not (memq backend new-list)) (push backend new-list))))
  711. ;; Set VAR to that list with fixed dependencies.
  712. (set-default var new-list))))
  713. :type '(set :greedy t
  714. (const :tag " ascii Export buffer to ASCII format" ascii)
  715. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  716. (const :tag " html Export buffer to HTML format" html)
  717. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  718. (const :tag " latex Export buffer to LaTeX format" latex)
  719. (const :tag " man Export buffer to MAN format" man)
  720. (const :tag " md Export buffer to Markdown format" md)
  721. (const :tag " odt Export buffer to ODT format" odt)
  722. (const :tag " org Export buffer to Org format" org)
  723. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  724. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  725. (const :tag "C deck Export buffer to deck.js presentations" deck)
  726. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  727. (const :tag "C groff Export buffer to Groff format" groff)
  728. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  729. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  730. (const :tag "C s5 Export buffer to s5 presentations" s5)
  731. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  732. (eval-after-load 'ox
  733. '(mapc
  734. (lambda (backend)
  735. (condition-case nil (require (intern (format "ox-%s" backend)))
  736. (error (message "Problems while trying to load export back-end `%s'"
  737. backend))))
  738. org-export-backends))
  739. (defcustom org-support-shift-select nil
  740. "Non-nil means make shift-cursor commands select text when possible.
  741. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  742. start selecting a region, or enlarge regions started in this way.
  743. In Org-mode, in special contexts, these same keys are used for
  744. other purposes, important enough to compete with shift selection.
  745. Org tries to balance these needs by supporting `shift-select-mode'
  746. outside these special contexts, under control of this variable.
  747. The default of this variable is nil, to avoid confusing behavior. Shifted
  748. cursor keys will then execute Org commands in the following contexts:
  749. - on a headline, changing TODO state (left/right) and priority (up/down)
  750. - on a time stamp, changing the time
  751. - in a plain list item, changing the bullet type
  752. - in a property definition line, switching between allowed values
  753. - in the BEGIN line of a clock table (changing the time block).
  754. Outside these contexts, the commands will throw an error.
  755. When this variable is t and the cursor is not in a special
  756. context, Org-mode will support shift-selection for making and
  757. enlarging regions. To make this more effective, the bullet
  758. cycling will no longer happen anywhere in an item line, but only
  759. if the cursor is exactly on the bullet.
  760. If you set this variable to the symbol `always', then the keys
  761. will not be special in headlines, property lines, and item lines,
  762. to make shift selection work there as well. If this is what you
  763. want, you can use the following alternative commands: `C-c C-t'
  764. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  765. can be used to switch TODO sets, `C-c -' to cycle item bullet
  766. types, and properties can be edited by hand or in column view.
  767. However, when the cursor is on a timestamp, shift-cursor commands
  768. will still edit the time stamp - this is just too good to give up.
  769. XEmacs user should have this variable set to nil, because
  770. `shift-select-mode' is in Emacs 23 or later only."
  771. :group 'org
  772. :type '(choice
  773. (const :tag "Never" nil)
  774. (const :tag "When outside special context" t)
  775. (const :tag "Everywhere except timestamps" always)))
  776. (defcustom org-loop-over-headlines-in-active-region nil
  777. "Shall some commands act upon headlines in the active region?
  778. When set to `t', some commands will be performed in all headlines
  779. within the active region.
  780. When set to `start-level', some commands will be performed in all
  781. headlines within the active region, provided that these headlines
  782. are of the same level than the first one.
  783. When set to a string, those commands will be performed on the
  784. matching headlines within the active region. Such string must be
  785. a tags/property/todo match as it is used in the agenda tags view.
  786. The list of commands is: `org-schedule', `org-deadline',
  787. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  788. `org-archive-to-archive-sibling'. The archiving commands skip
  789. already archived entries."
  790. :type '(choice (const :tag "Don't loop" nil)
  791. (const :tag "All headlines in active region" t)
  792. (const :tag "In active region, headlines at the same level than the first one" start-level)
  793. (string :tag "Tags/Property/Todo matcher"))
  794. :version "24.1"
  795. :group 'org-todo
  796. :group 'org-archive)
  797. (defgroup org-startup nil
  798. "Options concerning startup of Org-mode."
  799. :tag "Org Startup"
  800. :group 'org)
  801. (defcustom org-startup-folded t
  802. "Non-nil means entering Org-mode will switch to OVERVIEW.
  803. This can also be configured on a per-file basis by adding one of
  804. the following lines anywhere in the buffer:
  805. #+STARTUP: fold (or `overview', this is equivalent)
  806. #+STARTUP: nofold (or `showall', this is equivalent)
  807. #+STARTUP: content
  808. #+STARTUP: showeverything
  809. By default, this option is ignored when Org opens agenda files
  810. for the first time. If you want the agenda to honor the startup
  811. option, set `org-agenda-inhibit-startup' to nil."
  812. :group 'org-startup
  813. :type '(choice
  814. (const :tag "nofold: show all" nil)
  815. (const :tag "fold: overview" t)
  816. (const :tag "content: all headlines" content)
  817. (const :tag "show everything, even drawers" showeverything)))
  818. (defcustom org-startup-truncated t
  819. "Non-nil means entering Org-mode will set `truncate-lines'.
  820. This is useful since some lines containing links can be very long and
  821. uninteresting. Also tables look terrible when wrapped."
  822. :group 'org-startup
  823. :type 'boolean)
  824. (defcustom org-startup-indented nil
  825. "Non-nil means turn on `org-indent-mode' on startup.
  826. This can also be configured on a per-file basis by adding one of
  827. the following lines anywhere in the buffer:
  828. #+STARTUP: indent
  829. #+STARTUP: noindent"
  830. :group 'org-structure
  831. :type '(choice
  832. (const :tag "Not" nil)
  833. (const :tag "Globally (slow on startup in large files)" t)))
  834. (defcustom org-use-sub-superscripts t
  835. "Non-nil means interpret \"_\" and \"^\" for display.
  836. If you want to control how Org exports those characters, see
  837. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  838. used to be an alias for `org-export-with-sub-superscripts' in
  839. Org <8.0, it is not anymore.
  840. When this option is turned on, you can use TeX-like syntax for
  841. sub- and superscripts within the buffer. Several characters after
  842. \"_\" or \"^\" will be considered as a single item - so grouping
  843. with {} is normally not needed. For example, the following things
  844. will be parsed as single sub- or superscripts:
  845. 10^24 or 10^tau several digits will be considered 1 item.
  846. 10^-12 or 10^-tau a leading sign with digits or a word
  847. x^2-y^3 will be read as x^2 - y^3, because items are
  848. terminated by almost any nonword/nondigit char.
  849. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  850. Still, ambiguity is possible. So when in doubt, use {} to enclose
  851. the sub/superscript. If you set this variable to the symbol `{}',
  852. the braces are *required* in order to trigger interpretations as
  853. sub/superscript. This can be helpful in documents that need \"_\"
  854. frequently in plain text."
  855. :group 'org-startup
  856. :version "24.4"
  857. :package-version '(Org . "8.0")
  858. :type '(choice
  859. (const :tag "Always interpret" t)
  860. (const :tag "Only with braces" {})
  861. (const :tag "Never interpret" nil)))
  862. (defcustom org-startup-with-beamer-mode nil
  863. "Non-nil means turn on `org-beamer-mode' on startup.
  864. This can also be configured on a per-file basis by adding one of
  865. the following lines anywhere in the buffer:
  866. #+STARTUP: beamer"
  867. :group 'org-startup
  868. :version "24.1"
  869. :type 'boolean)
  870. (defcustom org-startup-align-all-tables nil
  871. "Non-nil means align all tables when visiting a file.
  872. This is useful when the column width in tables is forced with <N> cookies
  873. in table fields. Such tables will look correct only after the first re-align.
  874. This can also be configured on a per-file basis by adding one of
  875. the following lines anywhere in the buffer:
  876. #+STARTUP: align
  877. #+STARTUP: noalign"
  878. :group 'org-startup
  879. :type 'boolean)
  880. (defcustom org-startup-with-inline-images nil
  881. "Non-nil means show inline images when loading a new Org file.
  882. This can also be configured on a per-file basis by adding one of
  883. the following lines anywhere in the buffer:
  884. #+STARTUP: inlineimages
  885. #+STARTUP: noinlineimages"
  886. :group 'org-startup
  887. :version "24.1"
  888. :type 'boolean)
  889. (defcustom org-startup-with-latex-preview nil
  890. "Non-nil means preview LaTeX fragments when loading a new Org file.
  891. This can also be configured on a per-file basis by adding one of
  892. the following lines anywhere in the buffer:
  893. #+STARTUP: latexpreview
  894. #+STARTUP: nolatexpreview"
  895. :group 'org-startup
  896. :version "24.4"
  897. :package-version '(Org . "8.0")
  898. :type 'boolean)
  899. (defcustom org-insert-mode-line-in-empty-file nil
  900. "Non-nil means insert the first line setting Org-mode in empty files.
  901. When the function `org-mode' is called interactively in an empty file, this
  902. normally means that the file name does not automatically trigger Org-mode.
  903. To ensure that the file will always be in Org-mode in the future, a
  904. line enforcing Org-mode will be inserted into the buffer, if this option
  905. has been set."
  906. :group 'org-startup
  907. :type 'boolean)
  908. (defcustom org-replace-disputed-keys nil
  909. "Non-nil means use alternative key bindings for some keys.
  910. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  911. These keys are also used by other packages like shift-selection-mode'
  912. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  913. If you want to use Org-mode together with one of these other modes,
  914. or more generally if you would like to move some Org-mode commands to
  915. other keys, set this variable and configure the keys with the variable
  916. `org-disputed-keys'.
  917. This option is only relevant at load-time of Org-mode, and must be set
  918. *before* org.el is loaded. Changing it requires a restart of Emacs to
  919. become effective."
  920. :group 'org-startup
  921. :type 'boolean)
  922. (defcustom org-use-extra-keys nil
  923. "Non-nil means use extra key sequence definitions for certain commands.
  924. This happens automatically if you run XEmacs or if `window-system'
  925. is nil. This variable lets you do the same manually. You must
  926. set it before loading org.
  927. Example: on Carbon Emacs 22 running graphically, with an external
  928. keyboard on a Powerbook, the default way of setting M-left might
  929. not work for either Alt or ESC. Setting this variable will make
  930. it work for ESC."
  931. :group 'org-startup
  932. :type 'boolean)
  933. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  934. (defcustom org-disputed-keys
  935. '(([(shift up)] . [(meta p)])
  936. ([(shift down)] . [(meta n)])
  937. ([(shift left)] . [(meta -)])
  938. ([(shift right)] . [(meta +)])
  939. ([(control shift right)] . [(meta shift +)])
  940. ([(control shift left)] . [(meta shift -)]))
  941. "Keys for which Org-mode and other modes compete.
  942. This is an alist, cars are the default keys, second element specifies
  943. the alternative to use when `org-replace-disputed-keys' is t.
  944. Keys can be specified in any syntax supported by `define-key'.
  945. The value of this option takes effect only at Org-mode's startup,
  946. therefore you'll have to restart Emacs to apply it after changing."
  947. :group 'org-startup
  948. :type 'alist)
  949. (defun org-key (key)
  950. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  951. Or return the original if not disputed.
  952. Also apply the translations defined in `org-xemacs-key-equivalents'."
  953. (when org-replace-disputed-keys
  954. (let* ((nkey (key-description key))
  955. (x (org-find-if (lambda (x)
  956. (equal (key-description (car x)) nkey))
  957. org-disputed-keys)))
  958. (setq key (if x (cdr x) key))))
  959. (when (featurep 'xemacs)
  960. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  961. key)
  962. (defun org-find-if (predicate seq)
  963. (catch 'exit
  964. (while seq
  965. (if (funcall predicate (car seq))
  966. (throw 'exit (car seq))
  967. (pop seq)))))
  968. (defun org-defkey (keymap key def)
  969. "Define a key, possibly translated, as returned by `org-key'."
  970. (define-key keymap (org-key key) def))
  971. (defcustom org-ellipsis nil
  972. "The ellipsis to use in the Org-mode outline.
  973. When nil, just use the standard three dots.
  974. When a string, use that string instead.
  975. When a face, use the standard 3 dots, but with the specified face.
  976. The change affects only Org-mode (which will then use its own display table).
  977. Changing this requires executing \\[org-mode] in a buffer to become
  978. effective."
  979. :group 'org-startup
  980. :type '(choice (const :tag "Default" nil)
  981. (face :tag "Face" :value org-warning)
  982. (string :tag "String" :value "...#")))
  983. (defvar org-display-table nil
  984. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  985. (defgroup org-keywords nil
  986. "Keywords in Org-mode."
  987. :tag "Org Keywords"
  988. :group 'org)
  989. (defcustom org-closed-keep-when-no-todo nil
  990. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  991. :group 'org-todo
  992. :group 'org-keywords
  993. :version "24.4"
  994. :package-version '(Org . "8.0")
  995. :type 'boolean)
  996. (defgroup org-structure nil
  997. "Options concerning the general structure of Org-mode files."
  998. :tag "Org Structure"
  999. :group 'org)
  1000. (defgroup org-reveal-location nil
  1001. "Options about how to make context of a location visible."
  1002. :tag "Org Reveal Location"
  1003. :group 'org-structure)
  1004. (defcustom org-show-context-detail '((isearch . lineage)
  1005. (bookmark-jump . lineage)
  1006. (default . ancestors))
  1007. "Alist between context and visibility span when revealing a location.
  1008. \\<org-mode-map>Some actions may move point into invisible
  1009. locations. As a consequence, Org always expose a neighborhood
  1010. around point. How much is shown depends on the initial action,
  1011. or context. Valid contexts are
  1012. agenda when exposing an entry from the agenda
  1013. org-goto when using the command `org-goto' (\\[org-goto])
  1014. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1015. tags-tree when constructing a sparse tree based on tags matches
  1016. link-search when exposing search matches associated with a link
  1017. mark-goto when exposing the jump goal of a mark
  1018. bookmark-jump when exposing a bookmark location
  1019. isearch when exiting from an incremental search
  1020. default default for all contexts not set explicitly
  1021. Allowed visibility spans are
  1022. minimal show current headline; if point is not on headline,
  1023. also show entry
  1024. local show current headline, entry and next headline
  1025. ancestors show current headline and its direct ancestors; if
  1026. point is not on headline, also show entry
  1027. lineage show current headline, its direct ancestors and all
  1028. their children; if point is not on headline, also show
  1029. entry and first child
  1030. tree show current headline, its direct ancestors and all
  1031. their children; if point is not on headline, also show
  1032. entry and all children
  1033. canonical show current headline, its direct ancestors along with
  1034. their entries and children; if point is not located on
  1035. the headline, also show current entry and all children
  1036. As special cases, a nil or t value means show all contexts in
  1037. `minimal' or `canonical' view, respectively.
  1038. Some views can make displayed information very compact, but also
  1039. make it harder to edit the location of the match. In such
  1040. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1041. more context."
  1042. :group 'org-reveal-location
  1043. :version "25.1"
  1044. :package-version '(Org . "8.3")
  1045. :type '(choice
  1046. (const :tag "Canonical" t)
  1047. (const :tag "Minimal" nil)
  1048. (repeat :greedy t :tag "Individual contexts"
  1049. (cons
  1050. (choice :tag "Context"
  1051. (const agenda)
  1052. (const org-goto)
  1053. (const occur-tree)
  1054. (const tags-tree)
  1055. (const link-search)
  1056. (const mark-goto)
  1057. (const bookmark-jump)
  1058. (const isearch)
  1059. (const default))
  1060. (choice :tag "Detail level"
  1061. (const minimal)
  1062. (const local)
  1063. (const ancestors)
  1064. (const lineage)
  1065. (const tree)
  1066. (const canonical))))))
  1067. (defcustom org-indirect-buffer-display 'other-window
  1068. "How should indirect tree buffers be displayed?
  1069. This applies to indirect buffers created with the commands
  1070. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1071. Valid values are:
  1072. current-window Display in the current window
  1073. other-window Just display in another window.
  1074. dedicated-frame Create one new frame, and re-use it each time.
  1075. new-frame Make a new frame each time. Note that in this case
  1076. previously-made indirect buffers are kept, and you need to
  1077. kill these buffers yourself."
  1078. :group 'org-structure
  1079. :group 'org-agenda-windows
  1080. :type '(choice
  1081. (const :tag "In current window" current-window)
  1082. (const :tag "In current frame, other window" other-window)
  1083. (const :tag "Each time a new frame" new-frame)
  1084. (const :tag "One dedicated frame" dedicated-frame)))
  1085. (defcustom org-use-speed-commands nil
  1086. "Non-nil means activate single letter commands at beginning of a headline.
  1087. This may also be a function to test for appropriate locations where speed
  1088. commands should be active.
  1089. For example, to activate speed commands when the point is on any
  1090. star at the beginning of the headline, you can do this:
  1091. (setq org-use-speed-commands
  1092. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1093. :group 'org-structure
  1094. :type '(choice
  1095. (const :tag "Never" nil)
  1096. (const :tag "At beginning of headline stars" t)
  1097. (function)))
  1098. (defcustom org-speed-commands-user nil
  1099. "Alist of additional speed commands.
  1100. This list will be checked before `org-speed-commands-default'
  1101. when the variable `org-use-speed-commands' is non-nil
  1102. and when the cursor is at the beginning of a headline.
  1103. The car if each entry is a string with a single letter, which must
  1104. be assigned to `self-insert-command' in the global map.
  1105. The cdr is either a command to be called interactively, a function
  1106. to be called, or a form to be evaluated.
  1107. An entry that is just a list with a single string will be interpreted
  1108. as a descriptive headline that will be added when listing the speed
  1109. commands in the Help buffer using the `?' speed command."
  1110. :group 'org-structure
  1111. :type '(repeat :value ("k" . ignore)
  1112. (choice :value ("k" . ignore)
  1113. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1114. (cons :tag "Letter and Command"
  1115. (string :tag "Command letter")
  1116. (choice
  1117. (function)
  1118. (sexp))))))
  1119. (defcustom org-bookmark-names-plist
  1120. '(:last-capture "org-capture-last-stored"
  1121. :last-refile "org-refile-last-stored"
  1122. :last-capture-marker "org-capture-last-stored-marker")
  1123. "Names for bookmarks automatically set by some Org commands.
  1124. This can provide strings as names for a number of bookmarks Org sets
  1125. automatically. The following keys are currently implemented:
  1126. :last-capture
  1127. :last-capture-marker
  1128. :last-refile
  1129. When a key does not show up in the property list, the corresponding bookmark
  1130. is not set."
  1131. :group 'org-structure
  1132. :type 'plist)
  1133. (defgroup org-cycle nil
  1134. "Options concerning visibility cycling in Org-mode."
  1135. :tag "Org Cycle"
  1136. :group 'org-structure)
  1137. (defcustom org-cycle-skip-children-state-if-no-children t
  1138. "Non-nil means skip CHILDREN state in entries that don't have any."
  1139. :group 'org-cycle
  1140. :type 'boolean)
  1141. (defcustom org-cycle-max-level nil
  1142. "Maximum level which should still be subject to visibility cycling.
  1143. Levels higher than this will, for cycling, be treated as text, not a headline.
  1144. When `org-odd-levels-only' is set, a value of N in this variable actually
  1145. means 2N-1 stars as the limiting headline.
  1146. When nil, cycle all levels.
  1147. Note that the limiting level of cycling is also influenced by
  1148. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1149. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1150. than its value."
  1151. :group 'org-cycle
  1152. :type '(choice
  1153. (const :tag "No limit" nil)
  1154. (integer :tag "Maximum level")))
  1155. (defcustom org-hide-block-startup nil
  1156. "Non-nil means entering Org-mode will fold all blocks.
  1157. This can also be set in on a per-file basis with
  1158. #+STARTUP: hideblocks
  1159. #+STARTUP: showblocks"
  1160. :group 'org-startup
  1161. :group 'org-cycle
  1162. :type 'boolean)
  1163. (defcustom org-cycle-global-at-bob nil
  1164. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1165. This makes it possible to do global cycling without having to use S-TAB or
  1166. \\[universal-argument] TAB. For this special case to work, the first line
  1167. of the buffer must not be a headline -- it may be empty or some other text.
  1168. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1169. sure the cursor stays at the beginning of the buffer. When this option is
  1170. nil, don't do anything special at the beginning of the buffer."
  1171. :group 'org-cycle
  1172. :type 'boolean)
  1173. (defcustom org-cycle-level-after-item/entry-creation t
  1174. "Non-nil means cycle entry level or item indentation in new empty entries.
  1175. When the cursor is at the end of an empty headline, i.e., with only stars
  1176. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1177. and then all possible ancestor states, before returning to the original state.
  1178. This makes data entry extremely fast: M-RET to create a new headline,
  1179. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1180. When the cursor is at the end of an empty plain list item, one TAB will
  1181. make it a subitem, two or more tabs will back up to make this an item
  1182. higher up in the item hierarchy."
  1183. :group 'org-cycle
  1184. :type 'boolean)
  1185. (defcustom org-cycle-emulate-tab t
  1186. "Where should `org-cycle' emulate TAB.
  1187. nil Never
  1188. white Only in completely white lines
  1189. whitestart Only at the beginning of lines, before the first non-white char
  1190. t Everywhere except in headlines
  1191. exc-hl-bol Everywhere except at the start of a headline
  1192. If TAB is used in a place where it does not emulate TAB, the current subtree
  1193. visibility is cycled."
  1194. :group 'org-cycle
  1195. :type '(choice (const :tag "Never" nil)
  1196. (const :tag "Only in completely white lines" white)
  1197. (const :tag "Before first char in a line" whitestart)
  1198. (const :tag "Everywhere except in headlines" t)
  1199. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1200. (defcustom org-cycle-separator-lines 2
  1201. "Number of empty lines needed to keep an empty line between collapsed trees.
  1202. If you leave an empty line between the end of a subtree and the following
  1203. headline, this empty line is hidden when the subtree is folded.
  1204. Org-mode will leave (exactly) one empty line visible if the number of
  1205. empty lines is equal or larger to the number given in this variable.
  1206. So the default 2 means at least 2 empty lines after the end of a subtree
  1207. are needed to produce free space between a collapsed subtree and the
  1208. following headline.
  1209. If the number is negative, and the number of empty lines is at least -N,
  1210. all empty lines are shown.
  1211. Special case: when 0, never leave empty lines in collapsed view."
  1212. :group 'org-cycle
  1213. :type 'integer)
  1214. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1215. (defcustom org-pre-cycle-hook nil
  1216. "Hook that is run before visibility cycling is happening.
  1217. The function(s) in this hook must accept a single argument which indicates
  1218. the new state that will be set right after running this hook. The
  1219. argument is a symbol. Before a global state change, it can have the values
  1220. `overview', `content', or `all'. Before a local state change, it can have
  1221. the values `folded', `children', or `subtree'."
  1222. :group 'org-cycle
  1223. :type 'hook)
  1224. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1225. org-cycle-hide-drawers
  1226. org-cycle-show-empty-lines
  1227. org-optimize-window-after-visibility-change)
  1228. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1229. The function(s) in this hook must accept a single argument which indicates
  1230. the new state that was set by the most recent `org-cycle' command. The
  1231. argument is a symbol. After a global state change, it can have the values
  1232. `overview', `contents', or `all'. After a local state change, it can have
  1233. the values `folded', `children', or `subtree'."
  1234. :group 'org-cycle
  1235. :type 'hook
  1236. :version "25.1"
  1237. :package-version '(Org . "8.3"))
  1238. (defgroup org-edit-structure nil
  1239. "Options concerning structure editing in Org-mode."
  1240. :tag "Org Edit Structure"
  1241. :group 'org-structure)
  1242. (defcustom org-odd-levels-only nil
  1243. "Non-nil means skip even levels and only use odd levels for the outline.
  1244. This has the effect that two stars are being added/taken away in
  1245. promotion/demotion commands. It also influences how levels are
  1246. handled by the exporters.
  1247. Changing it requires restart of `font-lock-mode' to become effective
  1248. for fontification also in regions already fontified.
  1249. You may also set this on a per-file basis by adding one of the following
  1250. lines to the buffer:
  1251. #+STARTUP: odd
  1252. #+STARTUP: oddeven"
  1253. :group 'org-edit-structure
  1254. :group 'org-appearance
  1255. :type 'boolean)
  1256. (defcustom org-adapt-indentation t
  1257. "Non-nil means adapt indentation to outline node level.
  1258. When this variable is set, Org assumes that you write outlines by
  1259. indenting text in each node to align with the headline (after the
  1260. stars). The following issues are influenced by this variable:
  1261. - The indentation is increased by one space in a demotion
  1262. command, and decreased by one in a promotion command. However,
  1263. in the latter case, if shifting some line in the entry body
  1264. would alter document structure (e.g., insert a new headline),
  1265. indentation is not changed at all.
  1266. - Property drawers and planning information is inserted indented
  1267. when this variable is set. When nil, they will not be indented.
  1268. - TAB indents a line relative to current level. The lines below
  1269. a headline will be indented when this variable is set.
  1270. Note that this is all about true indentation, by adding and
  1271. removing space characters. See also `org-indent.el' which does
  1272. level-dependent indentation in a virtual way, i.e. at display
  1273. time in Emacs."
  1274. :group 'org-edit-structure
  1275. :type 'boolean)
  1276. (defcustom org-special-ctrl-a/e nil
  1277. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1278. When t, `C-a' will bring back the cursor to the beginning of the
  1279. headline text, i.e. after the stars and after a possible TODO
  1280. keyword. In an item, this will be the position after bullet and
  1281. check-box, if any. When the cursor is already at that position,
  1282. another `C-a' will bring it to the beginning of the line.
  1283. `C-e' will jump to the end of the headline, ignoring the presence
  1284. of tags in the headline. A second `C-e' will then jump to the
  1285. true end of the line, after any tags. This also means that, when
  1286. this variable is non-nil, `C-e' also will never jump beyond the
  1287. end of the heading of a folded section, i.e. not after the
  1288. ellipses.
  1289. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1290. normally, going to the true line boundary first. Only a directly
  1291. following, identical keypress will bring the cursor to the
  1292. special positions.
  1293. This may also be a cons cell where the behavior for `C-a' and
  1294. `C-e' is set separately."
  1295. :group 'org-edit-structure
  1296. :type '(choice
  1297. (const :tag "off" nil)
  1298. (const :tag "on: after stars/bullet and before tags first" t)
  1299. (const :tag "reversed: true line boundary first" reversed)
  1300. (cons :tag "Set C-a and C-e separately"
  1301. (choice :tag "Special C-a"
  1302. (const :tag "off" nil)
  1303. (const :tag "on: after stars/bullet first" t)
  1304. (const :tag "reversed: before stars/bullet first" reversed))
  1305. (choice :tag "Special C-e"
  1306. (const :tag "off" nil)
  1307. (const :tag "on: before tags first" t)
  1308. (const :tag "reversed: after tags first" reversed)))))
  1309. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1310. (defcustom org-special-ctrl-k nil
  1311. "Non-nil means `C-k' will behave specially in headlines.
  1312. When nil, `C-k' will call the default `kill-line' command.
  1313. When t, the following will happen while the cursor is in the headline:
  1314. - When the cursor is at the beginning of a headline, kill the entire
  1315. line and possible the folded subtree below the line.
  1316. - When in the middle of the headline text, kill the headline up to the tags.
  1317. - When after the headline text, kill the tags."
  1318. :group 'org-edit-structure
  1319. :type 'boolean)
  1320. (defcustom org-ctrl-k-protect-subtree nil
  1321. "Non-nil means, do not delete a hidden subtree with C-k.
  1322. When set to the symbol `error', simply throw an error when C-k is
  1323. used to kill (part-of) a headline that has hidden text behind it.
  1324. Any other non-nil value will result in a query to the user, if it is
  1325. OK to kill that hidden subtree. When nil, kill without remorse."
  1326. :group 'org-edit-structure
  1327. :version "24.1"
  1328. :type '(choice
  1329. (const :tag "Do not protect hidden subtrees" nil)
  1330. (const :tag "Protect hidden subtrees with a security query" t)
  1331. (const :tag "Never kill a hidden subtree with C-k" error)))
  1332. (defcustom org-special-ctrl-o t
  1333. "Non-nil means, make `C-o' insert a row in tables."
  1334. :group 'org-edit-structure
  1335. :type 'boolean)
  1336. (defcustom org-catch-invisible-edits nil
  1337. "Check if in invisible region before inserting or deleting a character.
  1338. Valid values are:
  1339. nil Do not check, so just do invisible edits.
  1340. error Throw an error and do nothing.
  1341. show Make point visible, and do the requested edit.
  1342. show-and-error Make point visible, then throw an error and abort the edit.
  1343. smart Make point visible, and do insertion/deletion if it is
  1344. adjacent to visible text and the change feels predictable.
  1345. Never delete a previously invisible character or add in the
  1346. middle or right after an invisible region. Basically, this
  1347. allows insertion and backward-delete right before ellipses.
  1348. FIXME: maybe in this case we should not even show?"
  1349. :group 'org-edit-structure
  1350. :version "24.1"
  1351. :type '(choice
  1352. (const :tag "Do not check" nil)
  1353. (const :tag "Throw error when trying to edit" error)
  1354. (const :tag "Unhide, but do not do the edit" show-and-error)
  1355. (const :tag "Show invisible part and do the edit" show)
  1356. (const :tag "Be smart and do the right thing" smart)))
  1357. (defcustom org-yank-folded-subtrees t
  1358. "Non-nil means when yanking subtrees, fold them.
  1359. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1360. it starts with a heading and all other headings in it are either children
  1361. or siblings, then fold all the subtrees. However, do this only if no
  1362. text after the yank would be swallowed into a folded tree by this action."
  1363. :group 'org-edit-structure
  1364. :type 'boolean)
  1365. (defcustom org-yank-adjusted-subtrees nil
  1366. "Non-nil means when yanking subtrees, adjust the level.
  1367. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1368. this function for details."
  1369. :group 'org-edit-structure
  1370. :type 'boolean)
  1371. (defcustom org-M-RET-may-split-line '((default . t))
  1372. "Non-nil means M-RET will split the line at the cursor position.
  1373. When nil, it will go to the end of the line before making a
  1374. new line.
  1375. You may also set this option in a different way for different
  1376. contexts. Valid contexts are:
  1377. headline when creating a new headline
  1378. item when creating a new item
  1379. table in a table field
  1380. default the value to be used for all contexts not explicitly
  1381. customized"
  1382. :group 'org-structure
  1383. :group 'org-table
  1384. :type '(choice
  1385. (const :tag "Always" t)
  1386. (const :tag "Never" nil)
  1387. (repeat :greedy t :tag "Individual contexts"
  1388. (cons
  1389. (choice :tag "Context"
  1390. (const headline)
  1391. (const item)
  1392. (const table)
  1393. (const default))
  1394. (boolean)))))
  1395. (defcustom org-insert-heading-respect-content nil
  1396. "Non-nil means insert new headings after the current subtree.
  1397. When nil, the new heading is created directly after the current line.
  1398. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1399. this variable on for the duration of the command."
  1400. :group 'org-structure
  1401. :type 'boolean)
  1402. (defcustom org-blank-before-new-entry '((heading . auto)
  1403. (plain-list-item . auto))
  1404. "Should `org-insert-heading' leave a blank line before new heading/item?
  1405. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1406. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1407. which case Org will look at the surrounding headings/items and try to
  1408. make an intelligent decision whether to insert a blank line or not.
  1409. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1410. the setting here is ignored and no empty line is inserted to avoid breaking
  1411. the list structure."
  1412. :group 'org-edit-structure
  1413. :type '(list
  1414. (cons (const heading)
  1415. (choice (const :tag "Never" nil)
  1416. (const :tag "Always" t)
  1417. (const :tag "Auto" auto)))
  1418. (cons (const plain-list-item)
  1419. (choice (const :tag "Never" nil)
  1420. (const :tag "Always" t)
  1421. (const :tag "Auto" auto)))))
  1422. (defcustom org-insert-heading-hook nil
  1423. "Hook being run after inserting a new heading."
  1424. :group 'org-edit-structure
  1425. :type 'hook)
  1426. (defcustom org-enable-fixed-width-editor t
  1427. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1428. This currently only means they are never auto-wrapped.
  1429. When nil, such lines will be treated like ordinary lines."
  1430. :group 'org-edit-structure
  1431. :type 'boolean)
  1432. (defcustom org-goto-auto-isearch t
  1433. "Non-nil means typing characters in `org-goto' starts incremental search.
  1434. When nil, you can use these keybindings to navigate the buffer:
  1435. q Quit the org-goto interface
  1436. n Go to the next visible heading
  1437. p Go to the previous visible heading
  1438. f Go one heading forward on same level
  1439. b Go one heading backward on same level
  1440. u Go one heading up"
  1441. :group 'org-edit-structure
  1442. :type 'boolean)
  1443. (defgroup org-sparse-trees nil
  1444. "Options concerning sparse trees in Org-mode."
  1445. :tag "Org Sparse Trees"
  1446. :group 'org-structure)
  1447. (defcustom org-highlight-sparse-tree-matches t
  1448. "Non-nil means highlight all matches that define a sparse tree.
  1449. The highlights will automatically disappear the next time the buffer is
  1450. changed by an edit command."
  1451. :group 'org-sparse-trees
  1452. :type 'boolean)
  1453. (defcustom org-remove-highlights-with-change t
  1454. "Non-nil means any change to the buffer will remove temporary highlights.
  1455. Such highlights are created by `org-occur' and `org-clock-display'.
  1456. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1457. The highlights created by `org-toggle-latex-fragment' always need
  1458. `C-c C-x C-l' to be removed."
  1459. :group 'org-sparse-trees
  1460. :group 'org-time
  1461. :type 'boolean)
  1462. (defcustom org-occur-hook '(org-first-headline-recenter)
  1463. "Hook that is run after `org-occur' has constructed a sparse tree.
  1464. This can be used to recenter the window to show as much of the structure
  1465. as possible."
  1466. :group 'org-sparse-trees
  1467. :type 'hook)
  1468. (defgroup org-imenu-and-speedbar nil
  1469. "Options concerning imenu and speedbar in Org-mode."
  1470. :tag "Org Imenu and Speedbar"
  1471. :group 'org-structure)
  1472. (defcustom org-imenu-depth 2
  1473. "The maximum level for Imenu access to Org-mode headlines.
  1474. This also applied for speedbar access."
  1475. :group 'org-imenu-and-speedbar
  1476. :type 'integer)
  1477. (defgroup org-table nil
  1478. "Options concerning tables in Org-mode."
  1479. :tag "Org Table"
  1480. :group 'org)
  1481. (defcustom org-enable-table-editor 'optimized
  1482. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1483. When nil, such lines will be treated like ordinary lines.
  1484. When equal to the symbol `optimized', the table editor will be optimized to
  1485. do the following:
  1486. - Automatic overwrite mode in front of whitespace in table fields.
  1487. This makes the structure of the table stay in tact as long as the edited
  1488. field does not exceed the column width.
  1489. - Minimize the number of realigns. Normally, the table is aligned each time
  1490. TAB or RET are pressed to move to another field. With optimization this
  1491. happens only if changes to a field might have changed the column width.
  1492. Optimization requires replacing the functions `self-insert-command',
  1493. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1494. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1495. very good at guessing when a re-align will be necessary, but you can always
  1496. force one with \\[org-ctrl-c-ctrl-c].
  1497. If you would like to use the optimized version in Org-mode, but the
  1498. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1499. This variable can be used to turn on and off the table editor during a session,
  1500. but in order to toggle optimization, a restart is required.
  1501. See also the variable `org-table-auto-blank-field'."
  1502. :group 'org-table
  1503. :type '(choice
  1504. (const :tag "off" nil)
  1505. (const :tag "on" t)
  1506. (const :tag "on, optimized" optimized)))
  1507. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1508. (version<= emacs-version "24.1"))
  1509. "Non-nil means cluster self-insert commands for undo when possible.
  1510. If this is set, then, like in the Emacs command loop, 20 consecutive
  1511. characters will be undone together.
  1512. This is configurable, because there is some impact on typing performance."
  1513. :group 'org-table
  1514. :type 'boolean)
  1515. (defcustom org-table-tab-recognizes-table.el t
  1516. "Non-nil means TAB will automatically notice a table.el table.
  1517. When it sees such a table, it moves point into it and - if necessary -
  1518. calls `table-recognize-table'."
  1519. :group 'org-table-editing
  1520. :type 'boolean)
  1521. (defgroup org-link nil
  1522. "Options concerning links in Org-mode."
  1523. :tag "Org Link"
  1524. :group 'org)
  1525. (defvar org-link-abbrev-alist-local nil
  1526. "Buffer-local version of `org-link-abbrev-alist', which see.
  1527. The value of this is taken from the #+LINK lines.")
  1528. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1529. (defcustom org-link-abbrev-alist nil
  1530. "Alist of link abbreviations.
  1531. The car of each element is a string, to be replaced at the start of a link.
  1532. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1533. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1534. [[linkkey:tag][description]]
  1535. The 'linkkey' must be a word word, starting with a letter, followed
  1536. by letters, numbers, '-' or '_'.
  1537. If REPLACE is a string, the tag will simply be appended to create the link.
  1538. If the string contains \"%s\", the tag will be inserted there. If the string
  1539. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1540. at that point (see the function `url-hexify-string'). If the string contains
  1541. the specifier \"%(my-function)\", then the custom function `my-function' will
  1542. be invoked: this function takes the tag as its only argument and must return
  1543. a string.
  1544. REPLACE may also be a function that will be called with the tag as the
  1545. only argument to create the link, which should be returned as a string.
  1546. See the manual for examples."
  1547. :group 'org-link
  1548. :type '(repeat
  1549. (cons
  1550. (string :tag "Protocol")
  1551. (choice
  1552. (string :tag "Format")
  1553. (function)))))
  1554. (defcustom org-descriptive-links t
  1555. "Non-nil means Org will display descriptive links.
  1556. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1557. \"Org Website\", hiding the link itself and just displaying its
  1558. description. When set to `nil', Org will display the full links
  1559. literally.
  1560. You can interactively set the value of this variable by calling
  1561. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1562. :group 'org-link
  1563. :type 'boolean)
  1564. (defcustom org-link-file-path-type 'adaptive
  1565. "How the path name in file links should be stored.
  1566. Valid values are:
  1567. relative Relative to the current directory, i.e. the directory of the file
  1568. into which the link is being inserted.
  1569. absolute Absolute path, if possible with ~ for home directory.
  1570. noabbrev Absolute path, no abbreviation of home directory.
  1571. adaptive Use relative path for files in the current directory and sub-
  1572. directories of it. For other files, use an absolute path."
  1573. :group 'org-link
  1574. :type '(choice
  1575. (const relative)
  1576. (const absolute)
  1577. (const noabbrev)
  1578. (const adaptive)))
  1579. (defvaralias 'org-activate-links 'org-highlight-links)
  1580. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1581. "Types of links that should be highlighted in Org-mode files.
  1582. This is a list of symbols, each one of them leading to the
  1583. highlighting of a certain link type.
  1584. You can still open links that are not highlighted.
  1585. In principle, it does not hurt to turn on highlighting for all
  1586. link types. There may be a small gain when turning off unused
  1587. link types. The types are:
  1588. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1589. angle Links in angular brackets that may contain whitespace like
  1590. <bbdb:Carsten Dominik>.
  1591. plain Plain links in normal text, no whitespace, like http://google.com.
  1592. radio Text that is matched by a radio target, see manual for details.
  1593. tag Tag settings in a headline (link to tag search).
  1594. date Time stamps (link to calendar).
  1595. footnote Footnote labels.
  1596. If you set this variable during an Emacs session, use `org-mode-restart'
  1597. in the Org buffer so that the change takes effect."
  1598. :group 'org-link
  1599. :group 'org-appearance
  1600. :type '(set :greedy t
  1601. (const :tag "Double bracket links" bracket)
  1602. (const :tag "Angular bracket links" angle)
  1603. (const :tag "Plain text links" plain)
  1604. (const :tag "Radio target matches" radio)
  1605. (const :tag "Tags" tag)
  1606. (const :tag "Timestamps" date)
  1607. (const :tag "Footnotes" footnote)))
  1608. (defcustom org-make-link-description-function nil
  1609. "Function to use for generating link descriptions from links.
  1610. When nil, the link location will be used. This function must take
  1611. two parameters: the first one is the link, the second one is the
  1612. description generated by `org-insert-link'. The function should
  1613. return the description to use."
  1614. :group 'org-link
  1615. :type '(choice (const nil) (function)))
  1616. (defgroup org-link-store nil
  1617. "Options concerning storing links in Org-mode."
  1618. :tag "Org Store Link"
  1619. :group 'org-link)
  1620. (defcustom org-url-hexify-p t
  1621. "When non-nil, hexify URL when creating a link."
  1622. :type 'boolean
  1623. :version "24.3"
  1624. :group 'org-link-store)
  1625. (defcustom org-email-link-description-format "Email %c: %.30s"
  1626. "Format of the description part of a link to an email or usenet message.
  1627. The following %-escapes will be replaced by corresponding information:
  1628. %F full \"From\" field
  1629. %f name, taken from \"From\" field, address if no name
  1630. %T full \"To\" field
  1631. %t first name in \"To\" field, address if no name
  1632. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1633. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1634. %s subject
  1635. %d date
  1636. %m message-id.
  1637. You may use normal field width specification between the % and the letter.
  1638. This is for example useful to limit the length of the subject.
  1639. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1640. :group 'org-link-store
  1641. :type 'string)
  1642. (defcustom org-from-is-user-regexp
  1643. (let (r1 r2)
  1644. (when (and user-mail-address (not (string= user-mail-address "")))
  1645. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1646. (when (and user-full-name (not (string= user-full-name "")))
  1647. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1648. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1649. "Regexp matched against the \"From:\" header of an email or usenet message.
  1650. It should match if the message is from the user him/herself."
  1651. :group 'org-link-store
  1652. :type 'regexp)
  1653. (defcustom org-context-in-file-links t
  1654. "Non-nil means file links from `org-store-link' contain context.
  1655. A search string will be added to the file name with :: as separator and
  1656. used to find the context when the link is activated by the command
  1657. `org-open-at-point'. When this option is t, the entire active region
  1658. will be placed in the search string of the file link. If set to a
  1659. positive integer, only the first n lines of context will be stored.
  1660. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1661. negates this setting for the duration of the command."
  1662. :group 'org-link-store
  1663. :type '(choice boolean integer))
  1664. (defcustom org-keep-stored-link-after-insertion nil
  1665. "Non-nil means keep link in list for entire session.
  1666. The command `org-store-link' adds a link pointing to the current
  1667. location to an internal list. These links accumulate during a session.
  1668. The command `org-insert-link' can be used to insert links into any
  1669. Org-mode file (offering completion for all stored links). When this
  1670. option is nil, every link which has been inserted once using \\[org-insert-link]
  1671. will be removed from the list, to make completing the unused links
  1672. more efficient."
  1673. :group 'org-link-store
  1674. :type 'boolean)
  1675. (defgroup org-link-follow nil
  1676. "Options concerning following links in Org-mode."
  1677. :tag "Org Follow Link"
  1678. :group 'org-link)
  1679. (defcustom org-link-translation-function nil
  1680. "Function to translate links with different syntax to Org syntax.
  1681. This can be used to translate links created for example by the Planner
  1682. or emacs-wiki packages to Org syntax.
  1683. The function must accept two parameters, a TYPE containing the link
  1684. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1685. which is everything after the link protocol. It should return a cons
  1686. with possibly modified values of type and path.
  1687. Org contains a function for this, so if you set this variable to
  1688. `org-translate-link-from-planner', you should be able follow many
  1689. links created by planner."
  1690. :group 'org-link-follow
  1691. :type '(choice (const nil) (function)))
  1692. (defcustom org-follow-link-hook nil
  1693. "Hook that is run after a link has been followed."
  1694. :group 'org-link-follow
  1695. :type 'hook)
  1696. (defcustom org-tab-follows-link nil
  1697. "Non-nil means on links TAB will follow the link.
  1698. Needs to be set before org.el is loaded.
  1699. This really should not be used, it does not make sense, and the
  1700. implementation is bad."
  1701. :group 'org-link-follow
  1702. :type 'boolean)
  1703. (defcustom org-return-follows-link nil
  1704. "Non-nil means on links RET will follow the link.
  1705. In tables, the special behavior of RET has precedence."
  1706. :group 'org-link-follow
  1707. :type 'boolean)
  1708. (defcustom org-mouse-1-follows-link
  1709. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1710. "Non-nil means mouse-1 on a link will follow the link.
  1711. A longer mouse click will still set point. Does not work on XEmacs.
  1712. Needs to be set before org.el is loaded."
  1713. :group 'org-link-follow
  1714. :version "24.4"
  1715. :package-version '(Org . "8.3")
  1716. :type '(choice
  1717. (const :tag "A double click follows the link" double)
  1718. (const :tag "Unconditionally follow the link with mouse-1" t)
  1719. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1720. (defcustom org-mark-ring-length 4
  1721. "Number of different positions to be recorded in the ring.
  1722. Changing this requires a restart of Emacs to work correctly."
  1723. :group 'org-link-follow
  1724. :type 'integer)
  1725. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1726. "Non-nil means internal links in Org files must exactly match a headline.
  1727. When nil, the link search tries to match a phrase with all words
  1728. in the search text."
  1729. :group 'org-link-follow
  1730. :version "24.1"
  1731. :type '(choice
  1732. (const :tag "Use fuzzy text search" nil)
  1733. (const :tag "Match only exact headline" t)
  1734. (const :tag "Match exact headline or query to create it"
  1735. query-to-create)))
  1736. (defcustom org-link-frame-setup
  1737. '((vm . vm-visit-folder-other-frame)
  1738. (vm-imap . vm-visit-imap-folder-other-frame)
  1739. (gnus . org-gnus-no-new-news)
  1740. (file . find-file-other-window)
  1741. (wl . wl-other-frame))
  1742. "Setup the frame configuration for following links.
  1743. When following a link with Emacs, it may often be useful to display
  1744. this link in another window or frame. This variable can be used to
  1745. set this up for the different types of links.
  1746. For VM, use any of
  1747. `vm-visit-folder'
  1748. `vm-visit-folder-other-window'
  1749. `vm-visit-folder-other-frame'
  1750. For Gnus, use any of
  1751. `gnus'
  1752. `gnus-other-frame'
  1753. `org-gnus-no-new-news'
  1754. For FILE, use any of
  1755. `find-file'
  1756. `find-file-other-window'
  1757. `find-file-other-frame'
  1758. For Wanderlust use any of
  1759. `wl'
  1760. `wl-other-frame'
  1761. For the calendar, use the variable `calendar-setup'.
  1762. For BBDB, it is currently only possible to display the matches in
  1763. another window."
  1764. :group 'org-link-follow
  1765. :type '(list
  1766. (cons (const vm)
  1767. (choice
  1768. (const vm-visit-folder)
  1769. (const vm-visit-folder-other-window)
  1770. (const vm-visit-folder-other-frame)))
  1771. (cons (const vm-imap)
  1772. (choice
  1773. (const vm-visit-imap-folder)
  1774. (const vm-visit-imap-folder-other-window)
  1775. (const vm-visit-imap-folder-other-frame)))
  1776. (cons (const gnus)
  1777. (choice
  1778. (const gnus)
  1779. (const gnus-other-frame)
  1780. (const org-gnus-no-new-news)))
  1781. (cons (const file)
  1782. (choice
  1783. (const find-file)
  1784. (const find-file-other-window)
  1785. (const find-file-other-frame)))
  1786. (cons (const wl)
  1787. (choice
  1788. (const wl)
  1789. (const wl-other-frame)))))
  1790. (defcustom org-display-internal-link-with-indirect-buffer nil
  1791. "Non-nil means use indirect buffer to display infile links.
  1792. Activating internal links (from one location in a file to another location
  1793. in the same file) normally just jumps to the location. When the link is
  1794. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1795. is displayed in
  1796. another window. When this option is set, the other window actually displays
  1797. an indirect buffer clone of the current buffer, to avoid any visibility
  1798. changes to the current buffer."
  1799. :group 'org-link-follow
  1800. :type 'boolean)
  1801. (defcustom org-open-non-existing-files nil
  1802. "Non-nil means `org-open-file' will open non-existing files.
  1803. When nil, an error will be generated.
  1804. This variable applies only to external applications because they
  1805. might choke on non-existing files. If the link is to a file that
  1806. will be opened in Emacs, the variable is ignored."
  1807. :group 'org-link-follow
  1808. :type 'boolean)
  1809. (defcustom org-open-directory-means-index-dot-org nil
  1810. "Non-nil means a link to a directory really means to index.org.
  1811. When nil, following a directory link will run dired or open a finder/explorer
  1812. window on that directory."
  1813. :group 'org-link-follow
  1814. :type 'boolean)
  1815. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1816. "Non-nil means ask for confirmation before executing shell links.
  1817. Shell links can be dangerous: just think about a link
  1818. [[shell:rm -rf ~/*][Google Search]]
  1819. This link would show up in your Org-mode document as \"Google Search\",
  1820. but really it would remove your entire home directory.
  1821. Therefore we advise against setting this variable to nil.
  1822. Just change it to `y-or-n-p' if you want to confirm with a
  1823. single keystroke rather than having to type \"yes\"."
  1824. :group 'org-link-follow
  1825. :type '(choice
  1826. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1827. (const :tag "with y-or-n (faster)" y-or-n-p)
  1828. (const :tag "no confirmation (dangerous)" nil)))
  1829. (put 'org-confirm-shell-link-function
  1830. 'safe-local-variable
  1831. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1832. (defcustom org-confirm-shell-link-not-regexp ""
  1833. "A regexp to skip confirmation for shell links."
  1834. :group 'org-link-follow
  1835. :version "24.1"
  1836. :type 'regexp)
  1837. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1838. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1839. Elisp links can be dangerous: just think about a link
  1840. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1841. This link would show up in your Org-mode document as \"Google Search\",
  1842. but really it would remove your entire home directory.
  1843. Therefore we advise against setting this variable to nil.
  1844. Just change it to `y-or-n-p' if you want to confirm with a
  1845. single keystroke rather than having to type \"yes\"."
  1846. :group 'org-link-follow
  1847. :type '(choice
  1848. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1849. (const :tag "with y-or-n (faster)" y-or-n-p)
  1850. (const :tag "no confirmation (dangerous)" nil)))
  1851. (put 'org-confirm-shell-link-function
  1852. 'safe-local-variable
  1853. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1854. (defcustom org-confirm-elisp-link-not-regexp ""
  1855. "A regexp to skip confirmation for Elisp links."
  1856. :group 'org-link-follow
  1857. :version "24.1"
  1858. :type 'regexp)
  1859. (defconst org-file-apps-defaults-gnu
  1860. '((remote . emacs)
  1861. (system . mailcap)
  1862. (t . mailcap))
  1863. "Default file applications on a UNIX or GNU/Linux system.
  1864. See `org-file-apps'.")
  1865. (defconst org-file-apps-defaults-macosx
  1866. '((remote . emacs)
  1867. (t . "open %s")
  1868. (system . "open %s")
  1869. ("ps.gz" . "gv %s")
  1870. ("eps.gz" . "gv %s")
  1871. ("dvi" . "xdvi %s")
  1872. ("fig" . "xfig %s"))
  1873. "Default file applications on a MacOS X system.
  1874. The system \"open\" is known as a default, but we use X11 applications
  1875. for some files for which the OS does not have a good default.
  1876. See `org-file-apps'.")
  1877. (defconst org-file-apps-defaults-windowsnt
  1878. (list
  1879. '(remote . emacs)
  1880. (cons t
  1881. (list (if (featurep 'xemacs)
  1882. 'mswindows-shell-execute
  1883. 'w32-shell-execute)
  1884. "open" 'file))
  1885. (cons 'system
  1886. (list (if (featurep 'xemacs)
  1887. 'mswindows-shell-execute
  1888. 'w32-shell-execute)
  1889. "open" 'file)))
  1890. "Default file applications on a Windows NT system.
  1891. The system \"open\" is used for most files.
  1892. See `org-file-apps'.")
  1893. (defcustom org-file-apps
  1894. '((auto-mode . emacs)
  1895. ("\\.mm\\'" . default)
  1896. ("\\.x?html?\\'" . default)
  1897. ("\\.pdf\\'" . default))
  1898. "External applications for opening `file:path' items in a document.
  1899. Org-mode uses system defaults for different file types, but
  1900. you can use this variable to set the application for a given file
  1901. extension. The entries in this list are cons cells where the car identifies
  1902. files and the cdr the corresponding command. Possible values for the
  1903. file identifier are
  1904. \"string\" A string as a file identifier can be interpreted in different
  1905. ways, depending on its contents:
  1906. - Alphanumeric characters only:
  1907. Match links with this file extension.
  1908. Example: (\"pdf\" . \"evince %s\")
  1909. to open PDFs with evince.
  1910. - Regular expression: Match links where the
  1911. filename matches the regexp. If you want to
  1912. use groups here, use shy groups.
  1913. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1914. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1915. to open *.html and *.xhtml with firefox.
  1916. - Regular expression which contains (non-shy) groups:
  1917. Match links where the whole link, including \"::\", and
  1918. anything after that, matches the regexp.
  1919. In a custom command string, %1, %2, etc. are replaced with
  1920. the parts of the link that were matched by the groups.
  1921. For backwards compatibility, if a command string is given
  1922. that does not use any of the group matches, this case is
  1923. handled identically to the second one (i.e. match against
  1924. file name only).
  1925. In a custom lisp form, you can access the group matches with
  1926. (match-string n link).
  1927. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1928. to open [[file:document.pdf::5]] with evince at page 5.
  1929. `directory' Matches a directory
  1930. `remote' Matches a remote file, accessible through tramp or efs.
  1931. Remote files most likely should be visited through Emacs
  1932. because external applications cannot handle such paths.
  1933. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1934. so all files Emacs knows how to handle. Using this with
  1935. command `emacs' will open most files in Emacs. Beware that this
  1936. will also open html files inside Emacs, unless you add
  1937. (\"html\" . default) to the list as well.
  1938. t Default for files not matched by any of the other options.
  1939. `system' The system command to open files, like `open' on Windows
  1940. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1941. that will be selected if you call `C-c C-o' with a double
  1942. \\[universal-argument] \\[universal-argument] prefix.
  1943. Possible values for the command are:
  1944. `emacs' The file will be visited by the current Emacs process.
  1945. `default' Use the default application for this file type, which is the
  1946. association for t in the list, most likely in the system-specific
  1947. part.
  1948. This can be used to overrule an unwanted setting in the
  1949. system-specific variable.
  1950. `system' Use the system command for opening files, like \"open\".
  1951. This command is specified by the entry whose car is `system'.
  1952. Most likely, the system-specific version of this variable
  1953. does define this command, but you can overrule/replace it
  1954. here.
  1955. string A command to be executed by a shell; %s will be replaced
  1956. by the path to the file.
  1957. sexp A Lisp form which will be evaluated. The file path will
  1958. be available in the Lisp variable `file'.
  1959. For more examples, see the system specific constants
  1960. `org-file-apps-defaults-macosx'
  1961. `org-file-apps-defaults-windowsnt'
  1962. `org-file-apps-defaults-gnu'."
  1963. :group 'org-link-follow
  1964. :type '(repeat
  1965. (cons (choice :value ""
  1966. (string :tag "Extension")
  1967. (const :tag "System command to open files" system)
  1968. (const :tag "Default for unrecognized files" t)
  1969. (const :tag "Remote file" remote)
  1970. (const :tag "Links to a directory" directory)
  1971. (const :tag "Any files that have Emacs modes"
  1972. auto-mode))
  1973. (choice :value ""
  1974. (const :tag "Visit with Emacs" emacs)
  1975. (const :tag "Use default" default)
  1976. (const :tag "Use the system command" system)
  1977. (string :tag "Command")
  1978. (sexp :tag "Lisp form")))))
  1979. (defcustom org-doi-server-url "http://dx.doi.org/"
  1980. "The URL of the DOI server."
  1981. :type 'string
  1982. :version "24.3"
  1983. :group 'org-link-follow)
  1984. (defgroup org-refile nil
  1985. "Options concerning refiling entries in Org-mode."
  1986. :tag "Org Refile"
  1987. :group 'org)
  1988. (defcustom org-directory "~/org"
  1989. "Directory with org files.
  1990. This is just a default location to look for Org files. There is no need
  1991. at all to put your files into this directory. It is only used in the
  1992. following situations:
  1993. 1. When a capture template specifies a target file that is not an
  1994. absolute path. The path will then be interpreted relative to
  1995. `org-directory'
  1996. 2. When a capture note is filed away in an interactive way (when exiting the
  1997. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1998. with `org-directory' as the default path."
  1999. :group 'org-refile
  2000. :group 'org-capture
  2001. :type 'directory)
  2002. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2003. "Default target for storing notes.
  2004. Used as a fall back file for org-capture.el, for templates that
  2005. do not specify a target file."
  2006. :group 'org-refile
  2007. :group 'org-capture
  2008. :type 'file)
  2009. (defcustom org-goto-interface 'outline
  2010. "The default interface to be used for `org-goto'.
  2011. Allowed values are:
  2012. outline The interface shows an outline of the relevant file
  2013. and the correct heading is found by moving through
  2014. the outline or by searching with incremental search.
  2015. outline-path-completion Headlines in the current buffer are offered via
  2016. completion. This is the interface also used by
  2017. the refile command."
  2018. :group 'org-refile
  2019. :type '(choice
  2020. (const :tag "Outline" outline)
  2021. (const :tag "Outline-path-completion" outline-path-completion)))
  2022. (defcustom org-goto-max-level 5
  2023. "Maximum target level when running `org-goto' with refile interface."
  2024. :group 'org-refile
  2025. :type 'integer)
  2026. (defcustom org-reverse-note-order nil
  2027. "Non-nil means store new notes at the beginning of a file or entry.
  2028. When nil, new notes will be filed to the end of a file or entry.
  2029. This can also be a list with cons cells of regular expressions that
  2030. are matched against file names, and values."
  2031. :group 'org-capture
  2032. :group 'org-refile
  2033. :type '(choice
  2034. (const :tag "Reverse always" t)
  2035. (const :tag "Reverse never" nil)
  2036. (repeat :tag "By file name regexp"
  2037. (cons regexp boolean))))
  2038. (defcustom org-log-refile nil
  2039. "Information to record when a task is refiled.
  2040. Possible values are:
  2041. nil Don't add anything
  2042. time Add a time stamp to the task
  2043. note Prompt for a note and add it with template `org-log-note-headings'
  2044. This option can also be set with on a per-file-basis with
  2045. #+STARTUP: nologrefile
  2046. #+STARTUP: logrefile
  2047. #+STARTUP: lognoterefile
  2048. You can have local logging settings for a subtree by setting the LOGGING
  2049. property to one or more of these keywords.
  2050. When bulk-refiling from the agenda, the value `note' is forbidden and
  2051. will temporarily be changed to `time'."
  2052. :group 'org-refile
  2053. :group 'org-progress
  2054. :version "24.1"
  2055. :type '(choice
  2056. (const :tag "No logging" nil)
  2057. (const :tag "Record timestamp" time)
  2058. (const :tag "Record timestamp with note." note)))
  2059. (defcustom org-refile-targets nil
  2060. "Targets for refiling entries with \\[org-refile].
  2061. This is a list of cons cells. Each cell contains:
  2062. - a specification of the files to be considered, either a list of files,
  2063. or a symbol whose function or variable value will be used to retrieve
  2064. a file name or a list of file names. If you use `org-agenda-files' for
  2065. that, all agenda files will be scanned for targets. Nil means consider
  2066. headings in the current buffer.
  2067. - A specification of how to find candidate refile targets. This may be
  2068. any of:
  2069. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2070. This tag has to be present in all target headlines, inheritance will
  2071. not be considered.
  2072. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2073. todo keyword.
  2074. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2075. headlines that are refiling targets.
  2076. - a cons cell (:level . N). Any headline of level N is considered a target.
  2077. Note that, when `org-odd-levels-only' is set, level corresponds to
  2078. order in hierarchy, not to the number of stars.
  2079. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2080. Note that, when `org-odd-levels-only' is set, level corresponds to
  2081. order in hierarchy, not to the number of stars.
  2082. Each element of this list generates a set of possible targets.
  2083. The union of these sets is presented (with completion) to
  2084. the user by `org-refile'.
  2085. You can set the variable `org-refile-target-verify-function' to a function
  2086. to verify each headline found by the simple criteria above.
  2087. When this variable is nil, all top-level headlines in the current buffer
  2088. are used, equivalent to the value `((nil . (:level . 1))'."
  2089. :group 'org-refile
  2090. :type '(repeat
  2091. (cons
  2092. (choice :value org-agenda-files
  2093. (const :tag "All agenda files" org-agenda-files)
  2094. (const :tag "Current buffer" nil)
  2095. (function) (variable) (file))
  2096. (choice :tag "Identify target headline by"
  2097. (cons :tag "Specific tag" (const :value :tag) (string))
  2098. (cons :tag "TODO keyword" (const :value :todo) (string))
  2099. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2100. (cons :tag "Level number" (const :value :level) (integer))
  2101. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2102. (defcustom org-refile-target-verify-function nil
  2103. "Function to verify if the headline at point should be a refile target.
  2104. The function will be called without arguments, with point at the
  2105. beginning of the headline. It should return t and leave point
  2106. where it is if the headline is a valid target for refiling.
  2107. If the target should not be selected, the function must return nil.
  2108. In addition to this, it may move point to a place from where the search
  2109. should be continued. For example, the function may decide that the entire
  2110. subtree of the current entry should be excluded and move point to the end
  2111. of the subtree."
  2112. :group 'org-refile
  2113. :type '(choice
  2114. (const nil)
  2115. (function)))
  2116. (defcustom org-refile-use-cache nil
  2117. "Non-nil means cache refile targets to speed up the process.
  2118. The cache for a particular file will be updated automatically when
  2119. the buffer has been killed, or when any of the marker used for flagging
  2120. refile targets no longer points at a live buffer.
  2121. If you have added new entries to a buffer that might themselves be targets,
  2122. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2123. find that easier, `C-u C-u C-u C-c C-w'."
  2124. :group 'org-refile
  2125. :version "24.1"
  2126. :type 'boolean)
  2127. (defcustom org-refile-use-outline-path nil
  2128. "Non-nil means provide refile targets as paths.
  2129. So a level 3 headline will be available as level1/level2/level3.
  2130. When the value is `file', also include the file name (without directory)
  2131. into the path. In this case, you can also stop the completion after
  2132. the file name, to get entries inserted as top level in the file.
  2133. When `full-file-path', include the full file path."
  2134. :group 'org-refile
  2135. :type '(choice
  2136. (const :tag "Not" nil)
  2137. (const :tag "Yes" t)
  2138. (const :tag "Start with file name" file)
  2139. (const :tag "Start with full file path" full-file-path)))
  2140. (defcustom org-outline-path-complete-in-steps t
  2141. "Non-nil means complete the outline path in hierarchical steps.
  2142. When Org-mode uses the refile interface to select an outline path
  2143. \(see variable `org-refile-use-outline-path'), the completion of
  2144. the path can be done in a single go, or it can be done in steps down
  2145. the headline hierarchy. Going in steps is probably the best if you
  2146. do not use a special completion package like `ido' or `icicles'.
  2147. However, when using these packages, going in one step can be very
  2148. fast, while still showing the whole path to the entry."
  2149. :group 'org-refile
  2150. :type 'boolean)
  2151. (defcustom org-refile-allow-creating-parent-nodes nil
  2152. "Non-nil means allow to create new nodes as refile targets.
  2153. New nodes are then created by adding \"/new node name\" to the completion
  2154. of an existing node. When the value of this variable is `confirm',
  2155. new node creation must be confirmed by the user (recommended).
  2156. When nil, the completion must match an existing entry.
  2157. Note that, if the new heading is not seen by the criteria
  2158. listed in `org-refile-targets', multiple instances of the same
  2159. heading would be created by trying again to file under the new
  2160. heading."
  2161. :group 'org-refile
  2162. :type '(choice
  2163. (const :tag "Never" nil)
  2164. (const :tag "Always" t)
  2165. (const :tag "Prompt for confirmation" confirm)))
  2166. (defcustom org-refile-active-region-within-subtree nil
  2167. "Non-nil means also refile active region within a subtree.
  2168. By default `org-refile' doesn't allow refiling regions if they
  2169. don't contain a set of subtrees, but it might be convenient to
  2170. do so sometimes: in that case, the first line of the region is
  2171. converted to a headline before refiling."
  2172. :group 'org-refile
  2173. :version "24.1"
  2174. :type 'boolean)
  2175. (defgroup org-todo nil
  2176. "Options concerning TODO items in Org-mode."
  2177. :tag "Org TODO"
  2178. :group 'org)
  2179. (defgroup org-progress nil
  2180. "Options concerning Progress logging in Org-mode."
  2181. :tag "Org Progress"
  2182. :group 'org-time)
  2183. (defvar org-todo-interpretation-widgets
  2184. '((:tag "Sequence (cycling hits every state)" sequence)
  2185. (:tag "Type (cycling directly to DONE)" type))
  2186. "The available interpretation symbols for customizing `org-todo-keywords'.
  2187. Interested libraries should add to this list.")
  2188. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2189. "List of TODO entry keyword sequences and their interpretation.
  2190. \\<org-mode-map>This is a list of sequences.
  2191. Each sequence starts with a symbol, either `sequence' or `type',
  2192. indicating if the keywords should be interpreted as a sequence of
  2193. action steps, or as different types of TODO items. The first
  2194. keywords are states requiring action - these states will select a headline
  2195. for inclusion into the global TODO list Org-mode produces. If one of
  2196. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2197. signify that no further action is necessary. If \"|\" is not found,
  2198. the last keyword is treated as the only DONE state of the sequence.
  2199. The command \\[org-todo] cycles an entry through these states, and one
  2200. additional state where no keyword is present. For details about this
  2201. cycling, see the manual.
  2202. TODO keywords and interpretation can also be set on a per-file basis with
  2203. the special #+SEQ_TODO and #+TYP_TODO lines.
  2204. Each keyword can optionally specify a character for fast state selection
  2205. \(in combination with the variable `org-use-fast-todo-selection')
  2206. and specifiers for state change logging, using the same syntax that
  2207. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2208. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2209. indicates to record a time stamp each time this state is selected.
  2210. Each keyword may also specify if a timestamp or a note should be
  2211. recorded when entering or leaving the state, by adding additional
  2212. characters in the parenthesis after the keyword. This looks like this:
  2213. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2214. record only the time of the state change. With X and Y being either
  2215. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2216. Y when leaving the state if and only if the *target* state does not
  2217. define X. You may omit any of the fast-selection key or X or /Y,
  2218. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2219. For backward compatibility, this variable may also be just a list
  2220. of keywords. In this case the interpretation (sequence or type) will be
  2221. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2222. :group 'org-todo
  2223. :group 'org-keywords
  2224. :type '(choice
  2225. (repeat :tag "Old syntax, just keywords"
  2226. (string :tag "Keyword"))
  2227. (repeat :tag "New syntax"
  2228. (cons
  2229. (choice
  2230. :tag "Interpretation"
  2231. ;;Quick and dirty way to see
  2232. ;;`org-todo-interpretations'. This takes the
  2233. ;;place of item arguments
  2234. :convert-widget
  2235. (lambda (widget)
  2236. (widget-put widget
  2237. :args (mapcar
  2238. (lambda (x)
  2239. (widget-convert
  2240. (cons 'const x)))
  2241. org-todo-interpretation-widgets))
  2242. widget))
  2243. (repeat
  2244. (string :tag "Keyword"))))))
  2245. (defvar org-todo-keywords-1 nil
  2246. "All TODO and DONE keywords active in a buffer.")
  2247. (make-variable-buffer-local 'org-todo-keywords-1)
  2248. (defvar org-todo-keywords-for-agenda nil)
  2249. (defvar org-done-keywords-for-agenda nil)
  2250. (defvar org-todo-keyword-alist-for-agenda nil)
  2251. (defvar org-tag-alist-for-agenda nil
  2252. "Alist of all tags from all agenda files.")
  2253. (defvar org-tag-groups-alist-for-agenda nil
  2254. "Alist of all groups tags from all current agenda files.")
  2255. (defvar org-tag-groups-alist nil)
  2256. (make-variable-buffer-local 'org-tag-groups-alist)
  2257. (defvar org-agenda-contributing-files nil)
  2258. (defvar org-not-done-keywords nil)
  2259. (make-variable-buffer-local 'org-not-done-keywords)
  2260. (defvar org-done-keywords nil)
  2261. (make-variable-buffer-local 'org-done-keywords)
  2262. (defvar org-todo-heads nil)
  2263. (make-variable-buffer-local 'org-todo-heads)
  2264. (defvar org-todo-sets nil)
  2265. (make-variable-buffer-local 'org-todo-sets)
  2266. (defvar org-todo-log-states nil)
  2267. (make-variable-buffer-local 'org-todo-log-states)
  2268. (defvar org-todo-kwd-alist nil)
  2269. (make-variable-buffer-local 'org-todo-kwd-alist)
  2270. (defvar org-todo-key-alist nil)
  2271. (make-variable-buffer-local 'org-todo-key-alist)
  2272. (defvar org-todo-key-trigger nil)
  2273. (make-variable-buffer-local 'org-todo-key-trigger)
  2274. (defcustom org-todo-interpretation 'sequence
  2275. "Controls how TODO keywords are interpreted.
  2276. This variable is in principle obsolete and is only used for
  2277. backward compatibility, if the interpretation of todo keywords is
  2278. not given already in `org-todo-keywords'. See that variable for
  2279. more information."
  2280. :group 'org-todo
  2281. :group 'org-keywords
  2282. :type '(choice (const sequence)
  2283. (const type)))
  2284. (defcustom org-use-fast-todo-selection t
  2285. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2286. This variable describes if and under what circumstances the cycling
  2287. mechanism for TODO keywords will be replaced by a single-key, direct
  2288. selection scheme.
  2289. When nil, fast selection is never used.
  2290. When the symbol `prefix', it will be used when `org-todo' is called
  2291. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2292. `C-u t' in an agenda buffer.
  2293. When t, fast selection is used by default. In this case, the prefix
  2294. argument forces cycling instead.
  2295. In all cases, the special interface is only used if access keys have
  2296. actually been assigned by the user, i.e. if keywords in the configuration
  2297. are followed by a letter in parenthesis, like TODO(t)."
  2298. :group 'org-todo
  2299. :type '(choice
  2300. (const :tag "Never" nil)
  2301. (const :tag "By default" t)
  2302. (const :tag "Only with C-u C-c C-t" prefix)))
  2303. (defcustom org-provide-todo-statistics t
  2304. "Non-nil means update todo statistics after insert and toggle.
  2305. ALL-HEADLINES means update todo statistics by including headlines
  2306. with no TODO keyword as well, counting them as not done.
  2307. A list of TODO keywords means the same, but skip keywords that are
  2308. not in this list.
  2309. When set to a list of two lists, the first list contains keywords
  2310. to consider as TODO keywords, the second list contains keywords
  2311. to consider as DONE keywords.
  2312. When this is set, todo statistics is updated in the parent of the
  2313. current entry each time a todo state is changed."
  2314. :group 'org-todo
  2315. :type '(choice
  2316. (const :tag "Yes, only for TODO entries" t)
  2317. (const :tag "Yes, including all entries" all-headlines)
  2318. (repeat :tag "Yes, for TODOs in this list"
  2319. (string :tag "TODO keyword"))
  2320. (list :tag "Yes, for TODOs and DONEs in these lists"
  2321. (repeat (string :tag "TODO keyword"))
  2322. (repeat (string :tag "DONE keyword")))
  2323. (other :tag "No TODO statistics" nil)))
  2324. (defcustom org-hierarchical-todo-statistics t
  2325. "Non-nil means TODO statistics covers just direct children.
  2326. When nil, all entries in the subtree are considered.
  2327. This has only an effect if `org-provide-todo-statistics' is set.
  2328. To set this to nil for only a single subtree, use a COOKIE_DATA
  2329. property and include the word \"recursive\" into the value."
  2330. :group 'org-todo
  2331. :type 'boolean)
  2332. (defcustom org-after-todo-state-change-hook nil
  2333. "Hook which is run after the state of a TODO item was changed.
  2334. The new state (a string with a TODO keyword, or nil) is available in the
  2335. Lisp variable `org-state'."
  2336. :group 'org-todo
  2337. :type 'hook)
  2338. (defvar org-blocker-hook nil
  2339. "Hook for functions that are allowed to block a state change.
  2340. Functions in this hook should not modify the buffer.
  2341. Each function gets as its single argument a property list,
  2342. see `org-trigger-hook' for more information about this list.
  2343. If any of the functions in this hook returns nil, the state change
  2344. is blocked.")
  2345. (defvar org-trigger-hook nil
  2346. "Hook for functions that are triggered by a state change.
  2347. Each function gets as its single argument a property list with at
  2348. least the following elements:
  2349. (:type type-of-change :position pos-at-entry-start
  2350. :from old-state :to new-state)
  2351. Depending on the type, more properties may be present.
  2352. This mechanism is currently implemented for:
  2353. TODO state changes
  2354. ------------------
  2355. :type todo-state-change
  2356. :from previous state (keyword as a string), or nil, or a symbol
  2357. 'todo' or 'done', to indicate the general type of state.
  2358. :to new state, like in :from")
  2359. (defcustom org-enforce-todo-dependencies nil
  2360. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2361. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2362. be blocked if any prior sibling is not yet done.
  2363. Finally, if the parent is blocked because of ordered siblings of its own,
  2364. the child will also be blocked."
  2365. :set (lambda (var val)
  2366. (set var val)
  2367. (if val
  2368. (add-hook 'org-blocker-hook
  2369. 'org-block-todo-from-children-or-siblings-or-parent)
  2370. (remove-hook 'org-blocker-hook
  2371. 'org-block-todo-from-children-or-siblings-or-parent)))
  2372. :group 'org-todo
  2373. :type 'boolean)
  2374. (defcustom org-enforce-todo-checkbox-dependencies nil
  2375. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2376. When this is nil, checkboxes have no influence on switching TODO states.
  2377. When non-nil, you first need to check off all check boxes before the TODO
  2378. entry can be switched to DONE.
  2379. This variable needs to be set before org.el is loaded, and you need to
  2380. restart Emacs after a change to make the change effective. The only way
  2381. to change is while Emacs is running is through the customize interface."
  2382. :set (lambda (var val)
  2383. (set var val)
  2384. (if val
  2385. (add-hook 'org-blocker-hook
  2386. 'org-block-todo-from-checkboxes)
  2387. (remove-hook 'org-blocker-hook
  2388. 'org-block-todo-from-checkboxes)))
  2389. :group 'org-todo
  2390. :type 'boolean)
  2391. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2392. "Non-nil means inserting a TODO heading is treated as state change.
  2393. So when the command \\[org-insert-todo-heading] is used, state change
  2394. logging will apply if appropriate. When nil, the new TODO item will
  2395. be inserted directly, and no logging will take place."
  2396. :group 'org-todo
  2397. :type 'boolean)
  2398. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2399. "Non-nil means switching TODO states with S-cursor counts as state change.
  2400. This is the default behavior. However, setting this to nil allows a
  2401. convenient way to select a TODO state and bypass any logging associated
  2402. with that."
  2403. :group 'org-todo
  2404. :type 'boolean)
  2405. (defcustom org-todo-state-tags-triggers nil
  2406. "Tag changes that should be triggered by TODO state changes.
  2407. This is a list. Each entry is
  2408. (state-change (tag . flag) .......)
  2409. State-change can be a string with a state, and empty string to indicate the
  2410. state that has no TODO keyword, or it can be one of the symbols `todo'
  2411. or `done', meaning any not-done or done state, respectively."
  2412. :group 'org-todo
  2413. :group 'org-tags
  2414. :type '(repeat
  2415. (cons (choice :tag "When changing to"
  2416. (const :tag "Not-done state" todo)
  2417. (const :tag "Done state" done)
  2418. (string :tag "State"))
  2419. (repeat
  2420. (cons :tag "Tag action"
  2421. (string :tag "Tag")
  2422. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2423. (defcustom org-log-done nil
  2424. "Information to record when a task moves to the DONE state.
  2425. Possible values are:
  2426. nil Don't add anything, just change the keyword
  2427. time Add a time stamp to the task
  2428. note Prompt for a note and add it with template `org-log-note-headings'
  2429. This option can also be set with on a per-file-basis with
  2430. #+STARTUP: nologdone
  2431. #+STARTUP: logdone
  2432. #+STARTUP: lognotedone
  2433. You can have local logging settings for a subtree by setting the LOGGING
  2434. property to one or more of these keywords."
  2435. :group 'org-todo
  2436. :group 'org-progress
  2437. :type '(choice
  2438. (const :tag "No logging" nil)
  2439. (const :tag "Record CLOSED timestamp" time)
  2440. (const :tag "Record CLOSED timestamp with note." note)))
  2441. ;; Normalize old uses of org-log-done.
  2442. (cond
  2443. ((eq org-log-done t) (setq org-log-done 'time))
  2444. ((and (listp org-log-done) (memq 'done org-log-done))
  2445. (setq org-log-done 'note)))
  2446. (defcustom org-log-reschedule nil
  2447. "Information to record when the scheduling date of a tasks is modified.
  2448. Possible values are:
  2449. nil Don't add anything, just change the date
  2450. time Add a time stamp to the task
  2451. note Prompt for a note and add it with template `org-log-note-headings'
  2452. This option can also be set with on a per-file-basis with
  2453. #+STARTUP: nologreschedule
  2454. #+STARTUP: logreschedule
  2455. #+STARTUP: lognotereschedule"
  2456. :group 'org-todo
  2457. :group 'org-progress
  2458. :type '(choice
  2459. (const :tag "No logging" nil)
  2460. (const :tag "Record timestamp" time)
  2461. (const :tag "Record timestamp with note." note)))
  2462. (defcustom org-log-redeadline nil
  2463. "Information to record when the deadline date of a tasks is modified.
  2464. Possible values are:
  2465. nil Don't add anything, just change the date
  2466. time Add a time stamp to the task
  2467. note Prompt for a note and add it with template `org-log-note-headings'
  2468. This option can also be set with on a per-file-basis with
  2469. #+STARTUP: nologredeadline
  2470. #+STARTUP: logredeadline
  2471. #+STARTUP: lognoteredeadline
  2472. You can have local logging settings for a subtree by setting the LOGGING
  2473. property to one or more of these keywords."
  2474. :group 'org-todo
  2475. :group 'org-progress
  2476. :type '(choice
  2477. (const :tag "No logging" nil)
  2478. (const :tag "Record timestamp" time)
  2479. (const :tag "Record timestamp with note." note)))
  2480. (defcustom org-log-note-clock-out nil
  2481. "Non-nil means record a note when clocking out of an item.
  2482. This can also be configured on a per-file basis by adding one of
  2483. the following lines anywhere in the buffer:
  2484. #+STARTUP: lognoteclock-out
  2485. #+STARTUP: nolognoteclock-out"
  2486. :group 'org-todo
  2487. :group 'org-progress
  2488. :type 'boolean)
  2489. (defcustom org-log-done-with-time t
  2490. "Non-nil means the CLOSED time stamp will contain date and time.
  2491. When nil, only the date will be recorded."
  2492. :group 'org-progress
  2493. :type 'boolean)
  2494. (defcustom org-log-note-headings
  2495. '((done . "CLOSING NOTE %t")
  2496. (state . "State %-12s from %-12S %t")
  2497. (note . "Note taken on %t")
  2498. (reschedule . "Rescheduled from %S on %t")
  2499. (delschedule . "Not scheduled, was %S on %t")
  2500. (redeadline . "New deadline from %S on %t")
  2501. (deldeadline . "Removed deadline, was %S on %t")
  2502. (refile . "Refiled on %t")
  2503. (clock-out . ""))
  2504. "Headings for notes added to entries.
  2505. The value is an alist, with the car being a symbol indicating the note
  2506. context, and the cdr is the heading to be used. The heading may also be the
  2507. empty string.
  2508. %t in the heading will be replaced by a time stamp.
  2509. %T will be an active time stamp instead the default inactive one
  2510. %d will be replaced by a short-format time stamp.
  2511. %D will be replaced by an active short-format time stamp.
  2512. %s will be replaced by the new TODO state, in double quotes.
  2513. %S will be replaced by the old TODO state, in double quotes.
  2514. %u will be replaced by the user name.
  2515. %U will be replaced by the full user name.
  2516. In fact, it is not a good idea to change the `state' entry, because
  2517. agenda log mode depends on the format of these entries."
  2518. :group 'org-todo
  2519. :group 'org-progress
  2520. :type '(list :greedy t
  2521. (cons (const :tag "Heading when closing an item" done) string)
  2522. (cons (const :tag
  2523. "Heading when changing todo state (todo sequence only)"
  2524. state) string)
  2525. (cons (const :tag "Heading when just taking a note" note) string)
  2526. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2527. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2528. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2529. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2530. (cons (const :tag "Heading when refiling" refile) string)
  2531. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2532. (unless (assq 'note org-log-note-headings)
  2533. (push '(note . "%t") org-log-note-headings))
  2534. (defcustom org-log-into-drawer nil
  2535. "Non-nil means insert state change notes and time stamps into a drawer.
  2536. When nil, state changes notes will be inserted after the headline and
  2537. any scheduling and clock lines, but not inside a drawer.
  2538. The value of this variable should be the name of the drawer to use.
  2539. LOGBOOK is proposed as the default drawer for this purpose, you can
  2540. also set this to a string to define the drawer of your choice.
  2541. A value of t is also allowed, representing \"LOGBOOK\".
  2542. A value of t or nil can also be set with on a per-file-basis with
  2543. #+STARTUP: logdrawer
  2544. #+STARTUP: nologdrawer
  2545. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2546. will be ignored.
  2547. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2548. a subtree.
  2549. Do not check directly this variable in a Lisp program. Call
  2550. function `org-log-into-drawer' instead."
  2551. :group 'org-todo
  2552. :group 'org-progress
  2553. :type '(choice
  2554. (const :tag "Not into a drawer" nil)
  2555. (const :tag "LOGBOOK" t)
  2556. (string :tag "Other")))
  2557. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2558. (defun org-log-into-drawer ()
  2559. "Name of the log drawer, as a string, or nil.
  2560. This is the value of `org-log-into-drawer'. However, if the
  2561. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2562. be used instead of the default value."
  2563. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2564. (cond ((equal p "nil") nil)
  2565. ((equal p "t") "LOGBOOK")
  2566. ((stringp p) p)
  2567. (p "LOGBOOK")
  2568. ((stringp org-log-into-drawer) org-log-into-drawer)
  2569. (org-log-into-drawer "LOGBOOK"))))
  2570. (defcustom org-log-state-notes-insert-after-drawers nil
  2571. "Non-nil means insert state change notes after any drawers in entry.
  2572. Only the drawers that *immediately* follow the headline and the
  2573. deadline/scheduled line are skipped.
  2574. When nil, insert notes right after the heading and perhaps the line
  2575. with deadline/scheduling if present.
  2576. This variable will have no effect if `org-log-into-drawer' is
  2577. set."
  2578. :group 'org-todo
  2579. :group 'org-progress
  2580. :type 'boolean)
  2581. (defcustom org-log-states-order-reversed t
  2582. "Non-nil means the latest state note will be directly after heading.
  2583. When nil, the state change notes will be ordered according to time.
  2584. This option can also be set with on a per-file-basis with
  2585. #+STARTUP: logstatesreversed
  2586. #+STARTUP: nologstatesreversed"
  2587. :group 'org-todo
  2588. :group 'org-progress
  2589. :type 'boolean)
  2590. (defcustom org-todo-repeat-to-state nil
  2591. "The TODO state to which a repeater should return the repeating task.
  2592. By default this is the first task in a TODO sequence, or the previous state
  2593. in a TODO_TYP set. But you can specify another task here.
  2594. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2595. :group 'org-todo
  2596. :version "24.1"
  2597. :type '(choice (const :tag "Head of sequence" nil)
  2598. (string :tag "Specific state")))
  2599. (defcustom org-log-repeat 'time
  2600. "Non-nil means record moving through the DONE state when triggering repeat.
  2601. An auto-repeating task is immediately switched back to TODO when
  2602. marked DONE. If you are not logging state changes (by adding \"@\"
  2603. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2604. record a closing note, there will be no record of the task moving
  2605. through DONE. This variable forces taking a note anyway.
  2606. nil Don't force a record
  2607. time Record a time stamp
  2608. note Prompt for a note and add it with template `org-log-note-headings'
  2609. This option can also be set with on a per-file-basis with
  2610. #+STARTUP: nologrepeat
  2611. #+STARTUP: logrepeat
  2612. #+STARTUP: lognoterepeat
  2613. You can have local logging settings for a subtree by setting the LOGGING
  2614. property to one or more of these keywords."
  2615. :group 'org-todo
  2616. :group 'org-progress
  2617. :type '(choice
  2618. (const :tag "Don't force a record" nil)
  2619. (const :tag "Force recording the DONE state" time)
  2620. (const :tag "Force recording a note with the DONE state" note)))
  2621. (defgroup org-priorities nil
  2622. "Priorities in Org-mode."
  2623. :tag "Org Priorities"
  2624. :group 'org-todo)
  2625. (defcustom org-enable-priority-commands t
  2626. "Non-nil means priority commands are active.
  2627. When nil, these commands will be disabled, so that you never accidentally
  2628. set a priority."
  2629. :group 'org-priorities
  2630. :type 'boolean)
  2631. (defcustom org-highest-priority ?A
  2632. "The highest priority of TODO items. A character like ?A, ?B etc.
  2633. Must have a smaller ASCII number than `org-lowest-priority'."
  2634. :group 'org-priorities
  2635. :type 'character)
  2636. (defcustom org-lowest-priority ?C
  2637. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2638. Must have a larger ASCII number than `org-highest-priority'."
  2639. :group 'org-priorities
  2640. :type 'character)
  2641. (defcustom org-default-priority ?B
  2642. "The default priority of TODO items.
  2643. This is the priority an item gets if no explicit priority is given.
  2644. When starting to cycle on an empty priority the first step in the cycle
  2645. depends on `org-priority-start-cycle-with-default'. The resulting first
  2646. step priority must not exceed the range from `org-highest-priority' to
  2647. `org-lowest-priority' which means that `org-default-priority' has to be
  2648. in this range exclusive or inclusive the range boundaries. Else the
  2649. first step refuses to set the default and the second will fall back
  2650. to (depending on the command used) the highest or lowest priority."
  2651. :group 'org-priorities
  2652. :type 'character)
  2653. (defcustom org-priority-start-cycle-with-default t
  2654. "Non-nil means start with default priority when starting to cycle.
  2655. When this is nil, the first step in the cycle will be (depending on the
  2656. command used) one higher or lower than the default priority.
  2657. See also `org-default-priority'."
  2658. :group 'org-priorities
  2659. :type 'boolean)
  2660. (defcustom org-get-priority-function nil
  2661. "Function to extract the priority from a string.
  2662. The string is normally the headline. If this is nil Org computes the
  2663. priority from the priority cookie like [#A] in the headline. It returns
  2664. an integer, increasing by 1000 for each priority level.
  2665. The user can set a different function here, which should take a string
  2666. as an argument and return the numeric priority."
  2667. :group 'org-priorities
  2668. :version "24.1"
  2669. :type '(choice
  2670. (const nil)
  2671. (function)))
  2672. (defgroup org-time nil
  2673. "Options concerning time stamps and deadlines in Org-mode."
  2674. :tag "Org Time"
  2675. :group 'org)
  2676. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2677. "Number of minutes to round time stamps to.
  2678. These are two values, the first applies when first creating a time stamp.
  2679. The second applies when changing it with the commands `S-up' and `S-down'.
  2680. When changing the time stamp, this means that it will change in steps
  2681. of N minutes, as given by the second value.
  2682. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2683. numbers should be factors of 60, so for example 5, 10, 15.
  2684. When this is larger than 1, you can still force an exact time stamp by using
  2685. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2686. and by using a prefix arg to `S-up/down' to specify the exact number
  2687. of minutes to shift."
  2688. :group 'org-time
  2689. :get (lambda (var) ; Make sure both elements are there
  2690. (if (integerp (default-value var))
  2691. (list (default-value var) 5)
  2692. (default-value var)))
  2693. :type '(list
  2694. (integer :tag "when inserting times")
  2695. (integer :tag "when modifying times")))
  2696. ;; Normalize old customizations of this variable.
  2697. (when (integerp org-time-stamp-rounding-minutes)
  2698. (setq org-time-stamp-rounding-minutes
  2699. (list org-time-stamp-rounding-minutes
  2700. org-time-stamp-rounding-minutes)))
  2701. (defcustom org-display-custom-times nil
  2702. "Non-nil means overlay custom formats over all time stamps.
  2703. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2704. To turn this on on a per-file basis, insert anywhere in the file:
  2705. #+STARTUP: customtime"
  2706. :group 'org-time
  2707. :set 'set-default
  2708. :type 'sexp)
  2709. (make-variable-buffer-local 'org-display-custom-times)
  2710. (defcustom org-time-stamp-custom-formats
  2711. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2712. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2713. These are overlaid over the default ISO format if the variable
  2714. `org-display-custom-times' is set. Time like %H:%M should be at the
  2715. end of the second format. The custom formats are also honored by export
  2716. commands, if custom time display is turned on at the time of export."
  2717. :group 'org-time
  2718. :type 'sexp)
  2719. (defun org-time-stamp-format (&optional long inactive)
  2720. "Get the right format for a time string."
  2721. (let ((f (if long (cdr org-time-stamp-formats)
  2722. (car org-time-stamp-formats))))
  2723. (if inactive
  2724. (concat "[" (substring f 1 -1) "]")
  2725. f)))
  2726. (defcustom org-time-clocksum-format
  2727. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2728. "The format string used when creating CLOCKSUM lines.
  2729. This is also used when Org mode generates a time duration.
  2730. The value can be a single format string containing two
  2731. %-sequences, which will be filled with the number of hours and
  2732. minutes in that order.
  2733. Alternatively, the value can be a plist associating any of the
  2734. keys :years, :months, :weeks, :days, :hours or :minutes with
  2735. format strings. The time duration is formatted using only the
  2736. time components that are needed and concatenating the results.
  2737. If a time unit in absent, it falls back to the next smallest
  2738. unit.
  2739. The keys :require-years, :require-months, :require-days,
  2740. :require-weeks, :require-hours, :require-minutes are also
  2741. meaningful. A non-nil value for these keys indicates that the
  2742. corresponding time component should always be included, even if
  2743. its value is 0.
  2744. For example,
  2745. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2746. :require-minutes t)
  2747. means durations longer than a day will be expressed in days,
  2748. hours and minutes, and durations less than a day will always be
  2749. expressed in hours and minutes (even for durations less than an
  2750. hour).
  2751. The value
  2752. \(:days \"%dd\" :minutes \"%dm\")
  2753. means durations longer than a day will be expressed in days and
  2754. minutes, and durations less than a day will be expressed entirely
  2755. in minutes (even for durations longer than an hour)."
  2756. :group 'org-time
  2757. :group 'org-clock
  2758. :version "24.4"
  2759. :package-version '(Org . "8.0")
  2760. :type '(choice (string :tag "Format string")
  2761. (set :tag "Plist"
  2762. (group :inline t (const :tag "Years" :years)
  2763. (string :tag "Format string"))
  2764. (group :inline t
  2765. (const :tag "Always show years" :require-years)
  2766. (const t))
  2767. (group :inline t (const :tag "Months" :months)
  2768. (string :tag "Format string"))
  2769. (group :inline t
  2770. (const :tag "Always show months" :require-months)
  2771. (const t))
  2772. (group :inline t (const :tag "Weeks" :weeks)
  2773. (string :tag "Format string"))
  2774. (group :inline t
  2775. (const :tag "Always show weeks" :require-weeks)
  2776. (const t))
  2777. (group :inline t (const :tag "Days" :days)
  2778. (string :tag "Format string"))
  2779. (group :inline t
  2780. (const :tag "Always show days" :require-days)
  2781. (const t))
  2782. (group :inline t (const :tag "Hours" :hours)
  2783. (string :tag "Format string"))
  2784. (group :inline t
  2785. (const :tag "Always show hours" :require-hours)
  2786. (const t))
  2787. (group :inline t (const :tag "Minutes" :minutes)
  2788. (string :tag "Format string"))
  2789. (group :inline t
  2790. (const :tag "Always show minutes" :require-minutes)
  2791. (const t)))))
  2792. (defcustom org-time-clocksum-use-fractional nil
  2793. "When non-nil, \\[org-clock-display] uses fractional times.
  2794. See `org-time-clocksum-format' for more on time clock formats."
  2795. :group 'org-time
  2796. :group 'org-clock
  2797. :version "24.3"
  2798. :type 'boolean)
  2799. (defcustom org-time-clocksum-use-effort-durations nil
  2800. "When non-nil, \\[org-clock-display] uses effort durations.
  2801. E.g. by default, one day is considered to be a 8 hours effort,
  2802. so a task that has been clocked for 16 hours will be displayed
  2803. as during 2 days in the clock display or in the clocktable.
  2804. See `org-effort-durations' on how to set effort durations
  2805. and `org-time-clocksum-format' for more on time clock formats."
  2806. :group 'org-time
  2807. :group 'org-clock
  2808. :version "24.4"
  2809. :package-version '(Org . "8.0")
  2810. :type 'boolean)
  2811. (defcustom org-time-clocksum-fractional-format "%.2f"
  2812. "The format string used when creating CLOCKSUM lines,
  2813. or when Org mode generates a time duration, if
  2814. `org-time-clocksum-use-fractional' is enabled.
  2815. The value can be a single format string containing one
  2816. %-sequence, which will be filled with the number of hours as
  2817. a float.
  2818. Alternatively, the value can be a plist associating any of the
  2819. keys :years, :months, :weeks, :days, :hours or :minutes with
  2820. a format string. The time duration is formatted using the
  2821. largest time unit which gives a non-zero integer part. If all
  2822. specified formats have zero integer part, the smallest time unit
  2823. is used."
  2824. :group 'org-time
  2825. :type '(choice (string :tag "Format string")
  2826. (set (group :inline t (const :tag "Years" :years)
  2827. (string :tag "Format string"))
  2828. (group :inline t (const :tag "Months" :months)
  2829. (string :tag "Format string"))
  2830. (group :inline t (const :tag "Weeks" :weeks)
  2831. (string :tag "Format string"))
  2832. (group :inline t (const :tag "Days" :days)
  2833. (string :tag "Format string"))
  2834. (group :inline t (const :tag "Hours" :hours)
  2835. (string :tag "Format string"))
  2836. (group :inline t (const :tag "Minutes" :minutes)
  2837. (string :tag "Format string")))))
  2838. (defcustom org-deadline-warning-days 14
  2839. "Number of days before expiration during which a deadline becomes active.
  2840. This variable governs the display in sparse trees and in the agenda.
  2841. When 0 or negative, it means use this number (the absolute value of it)
  2842. even if a deadline has a different individual lead time specified.
  2843. Custom commands can set this variable in the options section."
  2844. :group 'org-time
  2845. :group 'org-agenda-daily/weekly
  2846. :type 'integer)
  2847. (defcustom org-scheduled-delay-days 0
  2848. "Number of days before a scheduled item becomes active.
  2849. This variable governs the display in sparse trees and in the agenda.
  2850. The default value (i.e. 0) means: don't delay scheduled item.
  2851. When negative, it means use this number (the absolute value of it)
  2852. even if a scheduled item has a different individual delay time
  2853. specified.
  2854. Custom commands can set this variable in the options section."
  2855. :group 'org-time
  2856. :group 'org-agenda-daily/weekly
  2857. :version "24.4"
  2858. :package-version '(Org . "8.0")
  2859. :type 'integer)
  2860. (defcustom org-read-date-prefer-future t
  2861. "Non-nil means assume future for incomplete date input from user.
  2862. This affects the following situations:
  2863. 1. The user gives a month but not a year.
  2864. For example, if it is April and you enter \"feb 2\", this will be read
  2865. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2866. 2. The user gives a day, but no month.
  2867. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2868. read this as the third of *next* month. However, if you enter \"17\",
  2869. it will be considered as *this* month.
  2870. If you set this variable to the symbol `time', then also the following
  2871. will work:
  2872. 3. If the user gives a time.
  2873. If the time is before now, it will be interpreted as tomorrow.
  2874. Currently none of this works for ISO week specifications.
  2875. When this option is nil, the current day, month and year will always be
  2876. used as defaults.
  2877. See also `org-agenda-jump-prefer-future'."
  2878. :group 'org-time
  2879. :type '(choice
  2880. (const :tag "Never" nil)
  2881. (const :tag "Check month and day" t)
  2882. (const :tag "Check month, day, and time" time)))
  2883. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2884. "Should the agenda jump command prefer the future for incomplete dates?
  2885. The default is to do the same as configured in `org-read-date-prefer-future'.
  2886. But you can also set a deviating value here.
  2887. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2888. :group 'org-agenda
  2889. :group 'org-time
  2890. :version "24.1"
  2891. :type '(choice
  2892. (const :tag "Use org-read-date-prefer-future"
  2893. org-read-date-prefer-future)
  2894. (const :tag "Never" nil)
  2895. (const :tag "Always" t)))
  2896. (defcustom org-read-date-force-compatible-dates t
  2897. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2898. Depending on the system Emacs is running on, certain dates cannot
  2899. be represented with the type used internally to represent time.
  2900. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2901. correctly. Some systems allow for earlier dates, some for later,
  2902. some for both. One way to find out it to insert any date into an
  2903. Org buffer, putting the cursor on the year and hitting S-up and
  2904. S-down to test the range.
  2905. When this variable is set to t, the date/time prompt will not let
  2906. you specify dates outside the 1970-2037 range, so it is certain that
  2907. these dates will work in whatever version of Emacs you are
  2908. running, and also that you can move a file from one Emacs implementation
  2909. to another. WHenever Org is forcing the year for you, it will display
  2910. a message and beep.
  2911. When this variable is nil, Org will check if the date is
  2912. representable in the specific Emacs implementation you are using.
  2913. If not, it will force a year, usually the current year, and beep
  2914. to remind you. Currently this setting is not recommended because
  2915. the likelihood that you will open your Org files in an Emacs that
  2916. has limited date range is not negligible.
  2917. A workaround for this problem is to use diary sexp dates for time
  2918. stamps outside of this range."
  2919. :group 'org-time
  2920. :version "24.1"
  2921. :type 'boolean)
  2922. (defcustom org-read-date-display-live t
  2923. "Non-nil means display current interpretation of date prompt live.
  2924. This display will be in an overlay, in the minibuffer."
  2925. :group 'org-time
  2926. :type 'boolean)
  2927. (defcustom org-read-date-popup-calendar t
  2928. "Non-nil means pop up a calendar when prompting for a date.
  2929. In the calendar, the date can be selected with mouse-1. However, the
  2930. minibuffer will also be active, and you can simply enter the date as well.
  2931. When nil, only the minibuffer will be available."
  2932. :group 'org-time
  2933. :type 'boolean)
  2934. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2935. 'org-read-date-popup-calendar)
  2936. (make-obsolete-variable
  2937. 'org-read-date-minibuffer-setup-hook
  2938. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2939. (defcustom org-read-date-minibuffer-setup-hook nil
  2940. "Hook to be used to set up keys for the date/time interface.
  2941. Add key definitions to `minibuffer-local-map', which will be a
  2942. temporary copy.
  2943. WARNING: This option is obsolete, you should use
  2944. `org-read-date-minibuffer-local-map' to set up keys."
  2945. :group 'org-time
  2946. :type 'hook)
  2947. (defcustom org-extend-today-until 0
  2948. "The hour when your day really ends. Must be an integer.
  2949. This has influence for the following applications:
  2950. - When switching the agenda to \"today\". It it is still earlier than
  2951. the time given here, the day recognized as TODAY is actually yesterday.
  2952. - When a date is read from the user and it is still before the time given
  2953. here, the current date and time will be assumed to be yesterday, 23:59.
  2954. Also, timestamps inserted in capture templates follow this rule.
  2955. IMPORTANT: This is a feature whose implementation is and likely will
  2956. remain incomplete. Really, it is only here because past midnight seems to
  2957. be the favorite working time of John Wiegley :-)"
  2958. :group 'org-time
  2959. :type 'integer)
  2960. (defcustom org-use-effective-time nil
  2961. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2962. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2963. \"effective time\" of any timestamps between midnight and 8am will be
  2964. 23:59 of the previous day."
  2965. :group 'org-time
  2966. :version "24.1"
  2967. :type 'boolean)
  2968. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2969. "When non-nil, use the last clock out time for `org-todo'.
  2970. Note that this option has precedence over the combined use of
  2971. `org-use-effective-time' and `org-extend-today-until'."
  2972. :group 'org-time
  2973. :version "24.4"
  2974. :package-version '(Org . "8.0")
  2975. :type 'boolean)
  2976. (defcustom org-edit-timestamp-down-means-later nil
  2977. "Non-nil means S-down will increase the time in a time stamp.
  2978. When nil, S-up will increase."
  2979. :group 'org-time
  2980. :type 'boolean)
  2981. (defcustom org-calendar-follow-timestamp-change t
  2982. "Non-nil means make the calendar window follow timestamp changes.
  2983. When a timestamp is modified and the calendar window is visible, it will be
  2984. moved to the new date."
  2985. :group 'org-time
  2986. :type 'boolean)
  2987. (defgroup org-tags nil
  2988. "Options concerning tags in Org-mode."
  2989. :tag "Org Tags"
  2990. :group 'org)
  2991. (defcustom org-tag-alist nil
  2992. "List of tags allowed in Org-mode files.
  2993. When this list is nil, Org-mode will base TAG input on what is already in the
  2994. buffer.
  2995. The value of this variable is an alist, the car of each entry must be a
  2996. keyword as a string, the cdr may be a character that is used to select
  2997. that tag through the fast-tag-selection interface.
  2998. See the manual for details."
  2999. :group 'org-tags
  3000. :type '(repeat
  3001. (choice
  3002. (cons (string :tag "Tag name")
  3003. (character :tag "Access char"))
  3004. (list :tag "Start radio group"
  3005. (const :startgroup)
  3006. (option (string :tag "Group description")))
  3007. (list :tag "Start tag group, non distinct"
  3008. (const :startgrouptag)
  3009. (option (string :tag "Group description")))
  3010. (list :tag "Group tags delimiter"
  3011. (const :grouptags))
  3012. (list :tag "End radio group"
  3013. (const :endgroup)
  3014. (option (string :tag "Group description")))
  3015. (list :tag "End tag group, non distinct"
  3016. (const :endgrouptag)
  3017. (option (string :tag "Group description")))
  3018. (const :tag "New line" (:newline)))))
  3019. (defcustom org-tag-persistent-alist nil
  3020. "List of tags that will always appear in all Org-mode files.
  3021. This is in addition to any in buffer settings or customizations
  3022. of `org-tag-alist'.
  3023. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3024. The value of this variable is an alist, the car of each entry must be a
  3025. keyword as a string, the cdr may be a character that is used to select
  3026. that tag through the fast-tag-selection interface.
  3027. See the manual for details.
  3028. To disable these tags on a per-file basis, insert anywhere in the file:
  3029. #+STARTUP: noptag"
  3030. :group 'org-tags
  3031. :type '(repeat
  3032. (choice
  3033. (cons (string :tag "Tag name")
  3034. (character :tag "Access char"))
  3035. (const :tag "Start radio group" (:startgroup))
  3036. (const :tag "Group tags delimiter" (:grouptags))
  3037. (const :tag "End radio group" (:endgroup))
  3038. (const :tag "New line" (:newline)))))
  3039. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3040. "If non-nil, always offer completion for all tags of all agenda files.
  3041. Instead of customizing this variable directly, you might want to
  3042. set it locally for capture buffers, because there no list of
  3043. tags in that file can be created dynamically (there are none).
  3044. (add-hook 'org-capture-mode-hook
  3045. (lambda ()
  3046. (set (make-local-variable
  3047. 'org-complete-tags-always-offer-all-agenda-tags)
  3048. t)))"
  3049. :group 'org-tags
  3050. :version "24.1"
  3051. :type 'boolean)
  3052. (defvar org-file-tags nil
  3053. "List of tags that can be inherited by all entries in the file.
  3054. The tags will be inherited if the variable `org-use-tag-inheritance'
  3055. says they should be.
  3056. This variable is populated from #+FILETAGS lines.")
  3057. (defcustom org-use-fast-tag-selection 'auto
  3058. "Non-nil means use fast tag selection scheme.
  3059. This is a special interface to select and deselect tags with single keys.
  3060. When nil, fast selection is never used.
  3061. When the symbol `auto', fast selection is used if and only if selection
  3062. characters for tags have been configured, either through the variable
  3063. `org-tag-alist' or through a #+TAGS line in the buffer.
  3064. When t, fast selection is always used and selection keys are assigned
  3065. automatically if necessary."
  3066. :group 'org-tags
  3067. :type '(choice
  3068. (const :tag "Always" t)
  3069. (const :tag "Never" nil)
  3070. (const :tag "When selection characters are configured" auto)))
  3071. (defcustom org-fast-tag-selection-single-key nil
  3072. "Non-nil means fast tag selection exits after first change.
  3073. When nil, you have to press RET to exit it.
  3074. During fast tag selection, you can toggle this flag with `C-c'.
  3075. This variable can also have the value `expert'. In this case, the window
  3076. displaying the tags menu is not even shown, until you press C-c again."
  3077. :group 'org-tags
  3078. :type '(choice
  3079. (const :tag "No" nil)
  3080. (const :tag "Yes" t)
  3081. (const :tag "Expert" expert)))
  3082. (defvar org-fast-tag-selection-include-todo nil
  3083. "Non-nil means fast tags selection interface will also offer TODO states.
  3084. This is an undocumented feature, you should not rely on it.")
  3085. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3086. "The column to which tags should be indented in a headline.
  3087. If this number is positive, it specifies the column. If it is negative,
  3088. it means that the tags should be flushright to that column. For example,
  3089. -80 works well for a normal 80 character screen.
  3090. When 0, place tags directly after headline text, with only one space in
  3091. between."
  3092. :group 'org-tags
  3093. :type 'integer)
  3094. (defcustom org-auto-align-tags t
  3095. "Non-nil keeps tags aligned when modifying headlines.
  3096. Some operations (i.e. demoting) change the length of a headline and
  3097. therefore shift the tags around. With this option turned on, after
  3098. each such operation the tags are again aligned to `org-tags-column'."
  3099. :group 'org-tags
  3100. :type 'boolean)
  3101. (defcustom org-use-tag-inheritance t
  3102. "Non-nil means tags in levels apply also for sublevels.
  3103. When nil, only the tags directly given in a specific line apply there.
  3104. This may also be a list of tags that should be inherited, or a regexp that
  3105. matches tags that should be inherited. Additional control is possible
  3106. with the variable `org-tags-exclude-from-inheritance' which gives an
  3107. explicit list of tags to be excluded from inheritance, even if the value of
  3108. `org-use-tag-inheritance' would select it for inheritance.
  3109. If this option is t, a match early-on in a tree can lead to a large
  3110. number of matches in the subtree when constructing the agenda or creating
  3111. a sparse tree. If you only want to see the first match in a tree during
  3112. a search, check out the variable `org-tags-match-list-sublevels'."
  3113. :group 'org-tags
  3114. :type '(choice
  3115. (const :tag "Not" nil)
  3116. (const :tag "Always" t)
  3117. (repeat :tag "Specific tags" (string :tag "Tag"))
  3118. (regexp :tag "Tags matched by regexp")))
  3119. (defcustom org-tags-exclude-from-inheritance nil
  3120. "List of tags that should never be inherited.
  3121. This is a way to exclude a few tags from inheritance. For way to do
  3122. the opposite, to actively allow inheritance for selected tags,
  3123. see the variable `org-use-tag-inheritance'."
  3124. :group 'org-tags
  3125. :type '(repeat (string :tag "Tag")))
  3126. (defun org-tag-inherit-p (tag)
  3127. "Check if TAG is one that should be inherited."
  3128. (cond
  3129. ((member tag org-tags-exclude-from-inheritance) nil)
  3130. ((eq org-use-tag-inheritance t) t)
  3131. ((not org-use-tag-inheritance) nil)
  3132. ((stringp org-use-tag-inheritance)
  3133. (string-match org-use-tag-inheritance tag))
  3134. ((listp org-use-tag-inheritance)
  3135. (member tag org-use-tag-inheritance))
  3136. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3137. (defcustom org-tags-match-list-sublevels t
  3138. "Non-nil means list also sublevels of headlines matching a search.
  3139. This variable applies to tags/property searches, and also to stuck
  3140. projects because this search is based on a tags match as well.
  3141. When set to the symbol `indented', sublevels are indented with
  3142. leading dots.
  3143. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3144. the sublevels of a headline matching a tag search often also match
  3145. the same search. Listing all of them can create very long lists.
  3146. Setting this variable to nil causes subtrees of a match to be skipped.
  3147. This variable is semi-obsolete and probably should always be true. It
  3148. is better to limit inheritance to certain tags using the variables
  3149. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3150. :group 'org-tags
  3151. :type '(choice
  3152. (const :tag "No, don't list them" nil)
  3153. (const :tag "Yes, do list them" t)
  3154. (const :tag "List them, indented with leading dots" indented)))
  3155. (defcustom org-tags-sort-function nil
  3156. "When set, tags are sorted using this function as a comparator."
  3157. :group 'org-tags
  3158. :type '(choice
  3159. (const :tag "No sorting" nil)
  3160. (const :tag "Alphabetical" string<)
  3161. (const :tag "Reverse alphabetical" string>)
  3162. (function :tag "Custom function" nil)))
  3163. (defvar org-tags-history nil
  3164. "History of minibuffer reads for tags.")
  3165. (defvar org-last-tags-completion-table nil
  3166. "The last used completion table for tags.")
  3167. (defvar org-after-tags-change-hook nil
  3168. "Hook that is run after the tags in a line have changed.")
  3169. (defgroup org-properties nil
  3170. "Options concerning properties in Org-mode."
  3171. :tag "Org Properties"
  3172. :group 'org)
  3173. (defcustom org-property-format "%-10s %s"
  3174. "How property key/value pairs should be formatted by `indent-line'.
  3175. When `indent-line' hits a property definition, it will format the line
  3176. according to this format, mainly to make sure that the values are
  3177. lined-up with respect to each other."
  3178. :group 'org-properties
  3179. :type 'string)
  3180. (defcustom org-properties-postprocess-alist nil
  3181. "Alist of properties and functions to adjust inserted values.
  3182. Elements of this alist must be of the form
  3183. ([string] [function])
  3184. where [string] must be a property name and [function] must be a
  3185. lambda expression: this lambda expression must take one argument,
  3186. the value to adjust, and return the new value as a string.
  3187. For example, this element will allow the property \"Remaining\"
  3188. to be updated wrt the relation between the \"Effort\" property
  3189. and the clock summary:
  3190. ((\"Remaining\" (lambda(value)
  3191. (let ((clocksum (org-clock-sum-current-item))
  3192. (effort (org-duration-string-to-minutes
  3193. (org-entry-get (point) \"Effort\"))))
  3194. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3195. :group 'org-properties
  3196. :version "24.1"
  3197. :type '(alist :key-type (string :tag "Property")
  3198. :value-type (function :tag "Function")))
  3199. (defcustom org-use-property-inheritance nil
  3200. "Non-nil means properties apply also for sublevels.
  3201. This setting is chiefly used during property searches. Turning it on can
  3202. cause significant overhead when doing a search, which is why it is not
  3203. on by default.
  3204. When nil, only the properties directly given in the current entry count.
  3205. When t, every property is inherited. The value may also be a list of
  3206. properties that should have inheritance, or a regular expression matching
  3207. properties that should be inherited.
  3208. However, note that some special properties use inheritance under special
  3209. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3210. and the properties ending in \"_ALL\" when they are used as descriptor
  3211. for valid values of a property.
  3212. Note for programmers:
  3213. When querying an entry with `org-entry-get', you can control if inheritance
  3214. should be used. By default, `org-entry-get' looks only at the local
  3215. properties. You can request inheritance by setting the inherit argument
  3216. to t (to force inheritance) or to `selective' (to respect the setting
  3217. in this variable)."
  3218. :group 'org-properties
  3219. :type '(choice
  3220. (const :tag "Not" nil)
  3221. (const :tag "Always" t)
  3222. (repeat :tag "Specific properties" (string :tag "Property"))
  3223. (regexp :tag "Properties matched by regexp")))
  3224. (defun org-property-inherit-p (property)
  3225. "Check if PROPERTY is one that should be inherited."
  3226. (cond
  3227. ((eq org-use-property-inheritance t) t)
  3228. ((not org-use-property-inheritance) nil)
  3229. ((stringp org-use-property-inheritance)
  3230. (string-match org-use-property-inheritance property))
  3231. ((listp org-use-property-inheritance)
  3232. (member property org-use-property-inheritance))
  3233. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3234. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3235. "The default column format, if no other format has been defined.
  3236. This variable can be set on the per-file basis by inserting a line
  3237. #+COLUMNS: %25ITEM ....."
  3238. :group 'org-properties
  3239. :type 'string)
  3240. (defcustom org-columns-ellipses ".."
  3241. "The ellipses to be used when a field in column view is truncated.
  3242. When this is the empty string, as many characters as possible are shown,
  3243. but then there will be no visual indication that the field has been truncated.
  3244. When this is a string of length N, the last N characters of a truncated
  3245. field are replaced by this string. If the column is narrower than the
  3246. ellipses string, only part of the ellipses string will be shown."
  3247. :group 'org-properties
  3248. :type 'string)
  3249. (defcustom org-columns-modify-value-for-display-function nil
  3250. "Function that modifies values for display in column view.
  3251. For example, it can be used to cut out a certain part from a time stamp.
  3252. The function must take 2 arguments:
  3253. column-title The title of the column (*not* the property name)
  3254. value The value that should be modified.
  3255. The function should return the value that should be displayed,
  3256. or nil if the normal value should be used."
  3257. :group 'org-properties
  3258. :type '(choice (const nil) (function)))
  3259. (defconst org-global-properties-fixed
  3260. '(("VISIBILITY_ALL" . "folded children content all")
  3261. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3262. "List of property/value pairs that can be inherited by any entry.
  3263. These are fixed values, for the preset properties. The user variable
  3264. that can be used to add to this list is `org-global-properties'.
  3265. The entries in this list are cons cells where the car is a property
  3266. name and cdr is a string with the value. If the value represents
  3267. multiple items like an \"_ALL\" property, separate the items by
  3268. spaces.")
  3269. (defcustom org-global-properties nil
  3270. "List of property/value pairs that can be inherited by any entry.
  3271. This list will be combined with the constant `org-global-properties-fixed'.
  3272. The entries in this list are cons cells where the car is a property
  3273. name and cdr is a string with the value.
  3274. You can set buffer-local values for the same purpose in the variable
  3275. `org-file-properties' this by adding lines like
  3276. #+PROPERTY: NAME VALUE"
  3277. :group 'org-properties
  3278. :type '(repeat
  3279. (cons (string :tag "Property")
  3280. (string :tag "Value"))))
  3281. (defvar org-file-properties nil
  3282. "List of property/value pairs that can be inherited by any entry.
  3283. Valid for the current buffer.
  3284. This variable is populated from #+PROPERTY lines.")
  3285. (make-variable-buffer-local 'org-file-properties)
  3286. (defgroup org-agenda nil
  3287. "Options concerning agenda views in Org-mode."
  3288. :tag "Org Agenda"
  3289. :group 'org)
  3290. (defvar org-category nil
  3291. "Variable used by org files to set a category for agenda display.
  3292. Such files should use a file variable to set it, for example
  3293. # -*- mode: org; org-category: \"ELisp\"
  3294. or contain a special line
  3295. #+CATEGORY: ELisp
  3296. If the file does not specify a category, then file's base name
  3297. is used instead.")
  3298. (make-variable-buffer-local 'org-category)
  3299. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3300. (defcustom org-agenda-files nil
  3301. "The files to be used for agenda display.
  3302. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3303. \\[org-remove-file]. You can also use customize to edit the list.
  3304. If an entry is a directory, all files in that directory that are matched by
  3305. `org-agenda-file-regexp' will be part of the file list.
  3306. If the value of the variable is not a list but a single file name, then
  3307. the list of agenda files is actually stored and maintained in that file, one
  3308. agenda file per line. In this file paths can be given relative to
  3309. `org-directory'. Tilde expansion and environment variable substitution
  3310. are also made."
  3311. :group 'org-agenda
  3312. :type '(choice
  3313. (repeat :tag "List of files and directories" file)
  3314. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3315. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3316. "Regular expression to match files for `org-agenda-files'.
  3317. If any element in the list in that variable contains a directory instead
  3318. of a normal file, all files in that directory that are matched by this
  3319. regular expression will be included."
  3320. :group 'org-agenda
  3321. :type 'regexp)
  3322. (defcustom org-agenda-text-search-extra-files nil
  3323. "List of extra files to be searched by text search commands.
  3324. These files will be searched in addition to the agenda files by the
  3325. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3326. Note that these files will only be searched for text search commands,
  3327. not for the other agenda views like todo lists, tag searches or the weekly
  3328. agenda. This variable is intended to list notes and possibly archive files
  3329. that should also be searched by these two commands.
  3330. In fact, if the first element in the list is the symbol `agenda-archives',
  3331. then all archive files of all agenda files will be added to the search
  3332. scope."
  3333. :group 'org-agenda
  3334. :type '(set :greedy t
  3335. (const :tag "Agenda Archives" agenda-archives)
  3336. (repeat :inline t (file))))
  3337. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3338. 'org-agenda-text-search-extra-files)
  3339. (defcustom org-agenda-skip-unavailable-files nil
  3340. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3341. A nil value means to remove them, after a query, from the list."
  3342. :group 'org-agenda
  3343. :type 'boolean)
  3344. (defcustom org-calendar-to-agenda-key [?c]
  3345. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3346. The command `org-calendar-goto-agenda' will be bound to this key. The
  3347. default is the character `c' because then `c' can be used to switch back and
  3348. forth between agenda and calendar."
  3349. :group 'org-agenda
  3350. :type 'sexp)
  3351. (defcustom org-calendar-insert-diary-entry-key [?i]
  3352. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3353. This option is irrelevant until `org-agenda-diary-file' has been configured
  3354. to point to an Org-mode file. When that is the case, the command
  3355. `org-agenda-diary-entry' will be bound to the key given here, by default
  3356. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3357. if you want to continue doing this, you need to change this to a different
  3358. key."
  3359. :group 'org-agenda
  3360. :type 'sexp)
  3361. (defcustom org-agenda-diary-file 'diary-file
  3362. "File to which to add new entries with the `i' key in agenda and calendar.
  3363. When this is the symbol `diary-file', the functionality in the Emacs
  3364. calendar will be used to add entries to the `diary-file'. But when this
  3365. points to a file, `org-agenda-diary-entry' will be used instead."
  3366. :group 'org-agenda
  3367. :type '(choice
  3368. (const :tag "The standard Emacs diary file" diary-file)
  3369. (file :tag "Special Org file diary entries")))
  3370. (eval-after-load "calendar"
  3371. '(progn
  3372. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3373. 'org-calendar-goto-agenda)
  3374. (add-hook 'calendar-mode-hook
  3375. (lambda ()
  3376. (unless (eq org-agenda-diary-file 'diary-file)
  3377. (define-key calendar-mode-map
  3378. org-calendar-insert-diary-entry-key
  3379. 'org-agenda-diary-entry))))))
  3380. (defgroup org-latex nil
  3381. "Options for embedding LaTeX code into Org-mode."
  3382. :tag "Org LaTeX"
  3383. :group 'org)
  3384. (defcustom org-format-latex-options
  3385. '(:foreground default :background default :scale 1.0
  3386. :html-foreground "Black" :html-background "Transparent"
  3387. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3388. "Options for creating images from LaTeX fragments.
  3389. This is a property list with the following properties:
  3390. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3391. `default' means use the foreground of the default face.
  3392. `auto' means use the foreground from the text face.
  3393. :background the background color, or \"Transparent\".
  3394. `default' means use the background of the default face.
  3395. `auto' means use the background from the text face.
  3396. :scale a scaling factor for the size of the images, to get more pixels
  3397. :html-foreground, :html-background, :html-scale
  3398. the same numbers for HTML export.
  3399. :matchers a list indicating which matchers should be used to
  3400. find LaTeX fragments. Valid members of this list are:
  3401. \"begin\" find environments
  3402. \"$1\" find single characters surrounded by $.$
  3403. \"$\" find math expressions surrounded by $...$
  3404. \"$$\" find math expressions surrounded by $$....$$
  3405. \"\\(\" find math expressions surrounded by \\(...\\)
  3406. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3407. :group 'org-latex
  3408. :type 'plist)
  3409. (defcustom org-format-latex-signal-error t
  3410. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3411. When nil, just push out a message."
  3412. :group 'org-latex
  3413. :version "24.1"
  3414. :type 'boolean)
  3415. (defcustom org-latex-to-mathml-jar-file nil
  3416. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3417. Use this to specify additional executable file say a jar file.
  3418. When using MathToWeb as the converter, specify the full-path to
  3419. your mathtoweb.jar file."
  3420. :group 'org-latex
  3421. :version "24.1"
  3422. :type '(choice
  3423. (const :tag "None" nil)
  3424. (file :tag "JAR file" :must-match t)))
  3425. (defcustom org-latex-to-mathml-convert-command nil
  3426. "Command to convert LaTeX fragments to MathML.
  3427. Replace format-specifiers in the command as noted below and use
  3428. `shell-command' to convert LaTeX to MathML.
  3429. %j: Executable file in fully expanded form as specified by
  3430. `org-latex-to-mathml-jar-file'.
  3431. %I: Input LaTeX file in fully expanded form.
  3432. %i: The latex fragment to be converted.
  3433. %o: Output MathML file.
  3434. This command is used by `org-create-math-formula'.
  3435. When using MathToWeb as the converter, set this to
  3436. \"java -jar %j -unicode -force -df %o %I\".
  3437. When using LaTeXML set this to
  3438. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3439. :group 'org-latex
  3440. :version "24.1"
  3441. :type '(choice
  3442. (const :tag "None" nil)
  3443. (string :tag "\nShell command")))
  3444. (defcustom org-latex-create-formula-image-program 'dvipng
  3445. "Program to convert LaTeX fragments with.
  3446. dvipng Process the LaTeX fragments to dvi file, then convert
  3447. dvi files to png files using dvipng.
  3448. This will also include processing of non-math environments.
  3449. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3450. to convert pdf files to png files"
  3451. :group 'org-latex
  3452. :version "24.1"
  3453. :type '(choice
  3454. (const :tag "dvipng" dvipng)
  3455. (const :tag "imagemagick" imagemagick)))
  3456. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3457. "Path to store latex preview images.
  3458. A relative path here creates many directories relative to the
  3459. processed org files paths. An absolute path puts all preview
  3460. images at the same place."
  3461. :group 'org-latex
  3462. :version "24.3"
  3463. :type 'string)
  3464. (defun org-format-latex-mathml-available-p ()
  3465. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3466. (save-match-data
  3467. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3468. org-latex-to-mathml-convert-command)
  3469. (let ((executable (car (split-string
  3470. org-latex-to-mathml-convert-command))))
  3471. (when (executable-find executable)
  3472. (if (string-match
  3473. "%j" org-latex-to-mathml-convert-command)
  3474. (file-readable-p org-latex-to-mathml-jar-file)
  3475. t))))))
  3476. (defcustom org-format-latex-header "\\documentclass{article}
  3477. \\usepackage[usenames]{color}
  3478. \[PACKAGES]
  3479. \[DEFAULT-PACKAGES]
  3480. \\pagestyle{empty} % do not remove
  3481. % The settings below are copied from fullpage.sty
  3482. \\setlength{\\textwidth}{\\paperwidth}
  3483. \\addtolength{\\textwidth}{-3cm}
  3484. \\setlength{\\oddsidemargin}{1.5cm}
  3485. \\addtolength{\\oddsidemargin}{-2.54cm}
  3486. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3487. \\setlength{\\textheight}{\\paperheight}
  3488. \\addtolength{\\textheight}{-\\headheight}
  3489. \\addtolength{\\textheight}{-\\headsep}
  3490. \\addtolength{\\textheight}{-\\footskip}
  3491. \\addtolength{\\textheight}{-3cm}
  3492. \\setlength{\\topmargin}{1.5cm}
  3493. \\addtolength{\\topmargin}{-2.54cm}"
  3494. "The document header used for processing LaTeX fragments.
  3495. It is imperative that this header make sure that no page number
  3496. appears on the page. The package defined in the variables
  3497. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3498. will either replace the placeholder \"[PACKAGES]\" in this
  3499. header, or they will be appended."
  3500. :group 'org-latex
  3501. :type 'string)
  3502. (defun org-set-packages-alist (var val)
  3503. "Set the packages alist and make sure it has 3 elements per entry."
  3504. (set var (mapcar (lambda (x)
  3505. (if (and (consp x) (= (length x) 2))
  3506. (list (car x) (nth 1 x) t)
  3507. x))
  3508. val)))
  3509. (defun org-get-packages-alist (var)
  3510. "Get the packages alist and make sure it has 3 elements per entry."
  3511. (mapcar (lambda (x)
  3512. (if (and (consp x) (= (length x) 2))
  3513. (list (car x) (nth 1 x) t)
  3514. x))
  3515. (default-value var)))
  3516. (defcustom org-latex-default-packages-alist
  3517. '(("AUTO" "inputenc" t)
  3518. ("T1" "fontenc" t)
  3519. ("" "fixltx2e" nil)
  3520. ("" "graphicx" t)
  3521. ("" "longtable" nil)
  3522. ("" "float" nil)
  3523. ("" "wrapfig" nil)
  3524. ("" "rotating" nil)
  3525. ("normalem" "ulem" t)
  3526. ("" "amsmath" t)
  3527. ("" "textcomp" t)
  3528. ("" "marvosym" t)
  3529. ("" "wasysym" t)
  3530. ("" "amssymb" t)
  3531. ("" "capt-of" nil)
  3532. ("" "hyperref" nil)
  3533. "\\tolerance=1000")
  3534. "Alist of default packages to be inserted in the header.
  3535. Change this only if one of the packages here causes an
  3536. incompatibility with another package you are using.
  3537. The packages in this list are needed by one part or another of
  3538. Org mode to function properly:
  3539. - inputenc, fontenc: for basic font and character selection
  3540. - fixltx2e: Important patches of LaTeX itself
  3541. - graphicx: for including images
  3542. - longtable: For multipage tables
  3543. - float, wrapfig: for figure placement
  3544. - rotating: for sideways figures and tables
  3545. - ulem: for underline and strike-through
  3546. - amsmath: for subscript and superscript and math environments
  3547. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3548. for interpreting the entities in `org-entities'. You can skip
  3549. some of these packages if you don't use any of their symbols.
  3550. - capt-of: for captions outside of floats
  3551. - hyperref: for cross references
  3552. Therefore you should not modify this variable unless you know
  3553. what you are doing. The one reason to change it anyway is that
  3554. you might be loading some other package that conflicts with one
  3555. of the default packages. Each element is either a cell or
  3556. a string.
  3557. A cell is of the format:
  3558. \( \"options\" \"package\" SNIPPET-FLAG).
  3559. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3560. when compiling LaTeX snippets into images for inclusion into
  3561. non-LaTeX output.
  3562. A string will be inserted as-is in the header of the document."
  3563. :group 'org-latex
  3564. :group 'org-export-latex
  3565. :set 'org-set-packages-alist
  3566. :get 'org-get-packages-alist
  3567. :version "24.1"
  3568. :type '(repeat
  3569. (choice
  3570. (list :tag "options/package pair"
  3571. (string :tag "options")
  3572. (string :tag "package")
  3573. (boolean :tag "Snippet"))
  3574. (string :tag "A line of LaTeX"))))
  3575. (defcustom org-latex-packages-alist nil
  3576. "Alist of packages to be inserted in every LaTeX header.
  3577. These will be inserted after `org-latex-default-packages-alist'.
  3578. Each element is either a cell or a string.
  3579. A cell is of the format:
  3580. \(\"options\" \"package\" SNIPPET-FLAG)
  3581. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3582. needed when turning LaTeX snippets into images for inclusion into
  3583. non-LaTeX output.
  3584. A string will be inserted as-is in the header of the document.
  3585. Make sure that you only list packages here which:
  3586. - you want in every file;
  3587. - do not conflict with the setup in `org-format-latex-header';
  3588. - do not conflict with the default packages in
  3589. `org-latex-default-packages-alist'."
  3590. :group 'org-latex
  3591. :group 'org-export-latex
  3592. :set 'org-set-packages-alist
  3593. :get 'org-get-packages-alist
  3594. :type '(repeat
  3595. (choice
  3596. (list :tag "options/package pair"
  3597. (string :tag "options")
  3598. (string :tag "package")
  3599. (boolean :tag "Snippet"))
  3600. (string :tag "A line of LaTeX"))))
  3601. (defgroup org-appearance nil
  3602. "Settings for Org-mode appearance."
  3603. :tag "Org Appearance"
  3604. :group 'org)
  3605. (defcustom org-level-color-stars-only nil
  3606. "Non-nil means fontify only the stars in each headline.
  3607. When nil, the entire headline is fontified.
  3608. Changing it requires restart of `font-lock-mode' to become effective
  3609. also in regions already fontified."
  3610. :group 'org-appearance
  3611. :type 'boolean)
  3612. (defcustom org-hide-leading-stars nil
  3613. "Non-nil means hide the first N-1 stars in a headline.
  3614. This works by using the face `org-hide' for these stars. This
  3615. face is white for a light background, and black for a dark
  3616. background. You may have to customize the face `org-hide' to
  3617. make this work.
  3618. Changing it requires restart of `font-lock-mode' to become effective
  3619. also in regions already fontified.
  3620. You may also set this on a per-file basis by adding one of the following
  3621. lines to the buffer:
  3622. #+STARTUP: hidestars
  3623. #+STARTUP: showstars"
  3624. :group 'org-appearance
  3625. :type 'boolean)
  3626. (defcustom org-hidden-keywords nil
  3627. "List of symbols corresponding to keywords to be hidden the org buffer.
  3628. For example, a value '(title) for this list will make the document's title
  3629. appear in the buffer without the initial #+TITLE: keyword."
  3630. :group 'org-appearance
  3631. :version "24.1"
  3632. :type '(set (const :tag "#+AUTHOR" author)
  3633. (const :tag "#+DATE" date)
  3634. (const :tag "#+EMAIL" email)
  3635. (const :tag "#+TITLE" title)))
  3636. (defcustom org-custom-properties nil
  3637. "List of properties (as strings) with a special meaning.
  3638. The default use of these custom properties is to let the user
  3639. hide them with `org-toggle-custom-properties-visibility'."
  3640. :group 'org-properties
  3641. :group 'org-appearance
  3642. :version "24.3"
  3643. :type '(repeat (string :tag "Property Name")))
  3644. (defcustom org-fontify-done-headline nil
  3645. "Non-nil means change the face of a headline if it is marked DONE.
  3646. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3647. When this is non-nil, the headline after the keyword is set to the
  3648. `org-headline-done' as an additional indication."
  3649. :group 'org-appearance
  3650. :type 'boolean)
  3651. (defcustom org-fontify-emphasized-text t
  3652. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3653. Changing this variable requires a restart of Emacs to take effect."
  3654. :group 'org-appearance
  3655. :type 'boolean)
  3656. (defcustom org-fontify-whole-heading-line nil
  3657. "Non-nil means fontify the whole line for headings.
  3658. This is useful when setting a background color for the
  3659. org-level-* faces."
  3660. :group 'org-appearance
  3661. :type 'boolean)
  3662. (defcustom org-highlight-latex-and-related nil
  3663. "Non-nil means highlight LaTeX related syntax in the buffer.
  3664. When non nil, the value should be a list containing any of the
  3665. following symbols:
  3666. `latex' Highlight LaTeX snippets and environments.
  3667. `script' Highlight subscript and superscript.
  3668. `entities' Highlight entities."
  3669. :group 'org-appearance
  3670. :version "24.4"
  3671. :package-version '(Org . "8.0")
  3672. :type '(choice
  3673. (const :tag "No highlighting" nil)
  3674. (set :greedy t :tag "Highlight"
  3675. (const :tag "LaTeX snippets and environments" latex)
  3676. (const :tag "Subscript and superscript" script)
  3677. (const :tag "Entities" entities))))
  3678. (defcustom org-hide-emphasis-markers nil
  3679. "Non-nil mean font-lock should hide the emphasis marker characters."
  3680. :group 'org-appearance
  3681. :type 'boolean)
  3682. (defcustom org-hide-macro-markers nil
  3683. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3684. :group 'org-appearance
  3685. :type 'boolean)
  3686. (defcustom org-pretty-entities nil
  3687. "Non-nil means show entities as UTF8 characters.
  3688. When nil, the \\name form remains in the buffer."
  3689. :group 'org-appearance
  3690. :version "24.1"
  3691. :type 'boolean)
  3692. (defcustom org-pretty-entities-include-sub-superscripts t
  3693. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3694. :group 'org-appearance
  3695. :version "24.1"
  3696. :type 'boolean)
  3697. (defvar org-emph-re nil
  3698. "Regular expression for matching emphasis.
  3699. After a match, the match groups contain these elements:
  3700. 0 The match of the full regular expression, including the characters
  3701. before and after the proper match
  3702. 1 The character before the proper match, or empty at beginning of line
  3703. 2 The proper match, including the leading and trailing markers
  3704. 3 The leading marker like * or /, indicating the type of highlighting
  3705. 4 The text between the emphasis markers, not including the markers
  3706. 5 The character after the match, empty at the end of a line")
  3707. (defvar org-verbatim-re nil
  3708. "Regular expression for matching verbatim text.")
  3709. (defvar org-emphasis-regexp-components) ; defined just below
  3710. (defvar org-emphasis-alist) ; defined just below
  3711. (defun org-set-emph-re (var val)
  3712. "Set variable and compute the emphasis regular expression."
  3713. (set var val)
  3714. (when (and (boundp 'org-emphasis-alist)
  3715. (boundp 'org-emphasis-regexp-components)
  3716. org-emphasis-alist org-emphasis-regexp-components)
  3717. (let* ((e org-emphasis-regexp-components)
  3718. (pre (car e))
  3719. (post (nth 1 e))
  3720. (border (nth 2 e))
  3721. (body (nth 3 e))
  3722. (nl (nth 4 e))
  3723. (body1 (concat body "*?"))
  3724. (markers (mapconcat 'car org-emphasis-alist ""))
  3725. (vmarkers (mapconcat
  3726. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3727. org-emphasis-alist "")))
  3728. ;; make sure special characters appear at the right position in the class
  3729. (if (string-match "\\^" markers)
  3730. (setq markers (concat (replace-match "" t t markers) "^")))
  3731. (if (string-match "-" markers)
  3732. (setq markers (concat (replace-match "" t t markers) "-")))
  3733. (if (string-match "\\^" vmarkers)
  3734. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3735. (if (string-match "-" vmarkers)
  3736. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3737. (if (> nl 0)
  3738. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3739. (int-to-string nl) "\\}")))
  3740. ;; Make the regexp
  3741. (setq org-emph-re
  3742. (concat "\\([" pre "]\\|^\\)"
  3743. "\\("
  3744. "\\([" markers "]\\)"
  3745. "\\("
  3746. "[^" border "]\\|"
  3747. "[^" border "]"
  3748. body1
  3749. "[^" border "]"
  3750. "\\)"
  3751. "\\3\\)"
  3752. "\\([" post "]\\|$\\)"))
  3753. (setq org-verbatim-re
  3754. (concat "\\([" pre "]\\|^\\)"
  3755. "\\("
  3756. "\\([" vmarkers "]\\)"
  3757. "\\("
  3758. "[^" border "]\\|"
  3759. "[^" border "]"
  3760. body1
  3761. "[^" border "]"
  3762. "\\)"
  3763. "\\3\\)"
  3764. "\\([" post "]\\|$\\)")))))
  3765. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3766. ;; set this option proved cumbersome. See this message/thread:
  3767. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3768. (defvar org-emphasis-regexp-components
  3769. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3770. "Components used to build the regular expression for emphasis.
  3771. This is a list with five entries. Terminology: In an emphasis string
  3772. like \" *strong word* \", we call the initial space PREMATCH, the final
  3773. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3774. and \"trong wor\" is the body. The different components in this variable
  3775. specify what is allowed/forbidden in each part:
  3776. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3777. post Chars allowed as postmatch. End of line will be allowed too.
  3778. border The chars *forbidden* as border characters.
  3779. body-regexp A regexp like \".\" to match a body character. Don't use
  3780. non-shy groups here, and don't allow newline here.
  3781. newline The maximum number of newlines allowed in an emphasis exp.
  3782. You need to reload Org or to restart Emacs after customizing this.")
  3783. (defcustom org-emphasis-alist
  3784. `(("*" bold)
  3785. ("/" italic)
  3786. ("_" underline)
  3787. ("=" org-verbatim verbatim)
  3788. ("~" org-code verbatim)
  3789. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3790. "Alist of characters and faces to emphasize text.
  3791. Text starting and ending with a special character will be emphasized,
  3792. for example *bold*, _underlined_ and /italic/. This variable sets the
  3793. marker characters and the face to be used by font-lock for highlighting
  3794. in Org-mode Emacs buffers.
  3795. You need to reload Org or to restart Emacs after customizing this."
  3796. :group 'org-appearance
  3797. :set 'org-set-emph-re
  3798. :version "24.4"
  3799. :package-version '(Org . "8.0")
  3800. :type '(repeat
  3801. (list
  3802. (string :tag "Marker character")
  3803. (choice
  3804. (face :tag "Font-lock-face")
  3805. (plist :tag "Face property list"))
  3806. (option (const verbatim)))))
  3807. (defvar org-protecting-blocks
  3808. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3809. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3810. This is needed for font-lock setup.")
  3811. ;;; Miscellaneous options
  3812. (defgroup org-completion nil
  3813. "Completion in Org-mode."
  3814. :tag "Org Completion"
  3815. :group 'org)
  3816. (defcustom org-completion-use-ido nil
  3817. "Non-nil means use ido completion wherever possible.
  3818. Note that `ido-mode' must be active for this variable to be relevant.
  3819. If you decide to turn this variable on, you might well want to turn off
  3820. `org-outline-path-complete-in-steps'.
  3821. See also `org-completion-use-iswitchb'."
  3822. :group 'org-completion
  3823. :type 'boolean)
  3824. (defcustom org-completion-use-iswitchb nil
  3825. "Non-nil means use iswitchb completion wherever possible.
  3826. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3827. If you decide to turn this variable on, you might well want to turn off
  3828. `org-outline-path-complete-in-steps'.
  3829. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3830. :group 'org-completion
  3831. :type 'boolean)
  3832. (defcustom org-completion-fallback-command 'hippie-expand
  3833. "The expansion command called by \\[pcomplete] in normal context.
  3834. Normal means, no org-mode-specific context."
  3835. :group 'org-completion
  3836. :type 'function)
  3837. ;;; Functions and variables from their packages
  3838. ;; Declared here to avoid compiler warnings
  3839. ;; XEmacs only
  3840. (defvar outline-mode-menu-heading)
  3841. (defvar outline-mode-menu-show)
  3842. (defvar outline-mode-menu-hide)
  3843. (defvar zmacs-regions) ; XEmacs regions
  3844. ;; Emacs only
  3845. (defvar mark-active)
  3846. ;; Various packages
  3847. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3848. (declare-function calendar-forward-day "cal-move" (arg))
  3849. (declare-function calendar-goto-date "cal-move" (date))
  3850. (declare-function calendar-goto-today "cal-move" ())
  3851. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3852. (defvar calc-embedded-close-formula)
  3853. (defvar calc-embedded-open-formula)
  3854. (declare-function cdlatex-tab "ext:cdlatex" ())
  3855. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3856. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3857. (defvar font-lock-unfontify-region-function)
  3858. (declare-function iswitchb-read-buffer "iswitchb"
  3859. (prompt &optional default require-match start matches-set))
  3860. (defvar iswitchb-temp-buflist)
  3861. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3862. (defvar org-agenda-tags-todo-honor-ignore-options)
  3863. (declare-function org-agenda-skip "org-agenda" ())
  3864. (declare-function
  3865. org-agenda-format-item "org-agenda"
  3866. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3867. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3868. (declare-function org-agenda-change-all-lines "org-agenda"
  3869. (newhead hdmarker &optional fixface just-this))
  3870. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3871. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3872. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3873. (beg end))
  3874. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3875. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3876. "org-agenda" (&optional end))
  3877. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3878. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3879. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3880. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3881. (declare-function org-indent-mode "org-indent" (&optional arg))
  3882. (declare-function parse-time-string "parse-time" (string))
  3883. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3884. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3885. (defvar remember-data-file)
  3886. (defvar texmathp-why)
  3887. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3888. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3889. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3890. ;;;###autoload
  3891. (defun turn-on-orgtbl ()
  3892. "Unconditionally turn on `orgtbl-mode'."
  3893. (require 'org-table)
  3894. (orgtbl-mode 1))
  3895. (defun org-at-table-p (&optional table-type)
  3896. "Return t if the cursor is inside an org-type table.
  3897. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3898. (if org-enable-table-editor
  3899. (save-excursion
  3900. (beginning-of-line 1)
  3901. (looking-at (if table-type org-table-any-line-regexp
  3902. org-table-line-regexp)))
  3903. nil))
  3904. (defsubst org-table-p () (org-at-table-p))
  3905. (defun org-at-table.el-p ()
  3906. "Return t if and only if we are at a table.el table."
  3907. (and (org-at-table-p 'any)
  3908. (save-excursion
  3909. (goto-char (org-table-begin 'any))
  3910. (looking-at org-table1-hline-regexp))))
  3911. (defun org-table-recognize-table.el ()
  3912. "If there is a table.el table nearby, recognize it and move into it."
  3913. (if org-table-tab-recognizes-table.el
  3914. (if (org-at-table.el-p)
  3915. (progn
  3916. (beginning-of-line 1)
  3917. (if (looking-at org-table-dataline-regexp)
  3918. nil
  3919. (if (looking-at org-table1-hline-regexp)
  3920. (progn
  3921. (beginning-of-line 2)
  3922. (if (looking-at org-table-any-border-regexp)
  3923. (beginning-of-line -1)))))
  3924. (if (re-search-forward "|" (org-table-end t) t)
  3925. (progn
  3926. (require 'table)
  3927. (if (table--at-cell-p (point))
  3928. t
  3929. (message "recognizing table.el table...")
  3930. (table-recognize-table)
  3931. (message "recognizing table.el table...done")))
  3932. (error "This should not happen"))
  3933. t)
  3934. nil)
  3935. nil))
  3936. (defun org-at-table-hline-p ()
  3937. "Return t if the cursor is inside a hline in a table."
  3938. (if org-enable-table-editor
  3939. (save-excursion
  3940. (beginning-of-line 1)
  3941. (looking-at org-table-hline-regexp))
  3942. nil))
  3943. (defun org-table-map-tables (function &optional quietly)
  3944. "Apply FUNCTION to the start of all tables in the buffer."
  3945. (save-excursion
  3946. (save-restriction
  3947. (widen)
  3948. (goto-char (point-min))
  3949. (while (re-search-forward org-table-any-line-regexp nil t)
  3950. (unless quietly
  3951. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3952. (beginning-of-line 1)
  3953. (when (and (looking-at org-table-line-regexp)
  3954. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3955. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3956. (save-excursion (funcall function))
  3957. (or (looking-at org-table-line-regexp)
  3958. (forward-char 1)))
  3959. (re-search-forward org-table-any-border-regexp nil 1))))
  3960. (unless quietly (message "Mapping tables: done")))
  3961. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3962. (declare-function org-clock-update-mode-line "org-clock" ())
  3963. (declare-function org-resolve-clocks "org-clock"
  3964. (&optional also-non-dangling-p prompt last-valid))
  3965. (defun org-at-TBLFM-p (&optional pos)
  3966. "Non-nil when point (or POS) is in #+TBLFM line."
  3967. (save-excursion
  3968. (goto-char (or pos (point)))
  3969. (beginning-of-line)
  3970. (and (eq (org-element-type (org-element-at-point)) 'table)
  3971. (looking-at org-TBLFM-regexp))))
  3972. (defvar org-clock-start-time)
  3973. (defvar org-clock-marker (make-marker)
  3974. "Marker recording the last clock-in.")
  3975. (defvar org-clock-hd-marker (make-marker)
  3976. "Marker recording the last clock-in, but the headline position.")
  3977. (defvar org-clock-heading ""
  3978. "The heading of the current clock entry.")
  3979. (defun org-clock-is-active ()
  3980. "Return the buffer where the clock is currently running.
  3981. Return nil if no clock is running."
  3982. (marker-buffer org-clock-marker))
  3983. (defun org-check-running-clock ()
  3984. "Check if the current buffer contains the running clock.
  3985. If yes, offer to stop it and to save the buffer with the changes."
  3986. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3987. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3988. (buffer-name))))
  3989. (org-clock-out)
  3990. (when (y-or-n-p "Save changed buffer?")
  3991. (save-buffer))))
  3992. (defun org-clocktable-try-shift (dir n)
  3993. "Check if this line starts a clock table, if yes, shift the time block."
  3994. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3995. (org-clocktable-shift dir n)))
  3996. ;;;###autoload
  3997. (defun org-clock-persistence-insinuate ()
  3998. "Set up hooks for clock persistence."
  3999. (require 'org-clock)
  4000. (add-hook 'org-mode-hook 'org-clock-load)
  4001. (add-hook 'kill-emacs-hook 'org-clock-save))
  4002. (defgroup org-archive nil
  4003. "Options concerning archiving in Org-mode."
  4004. :tag "Org Archive"
  4005. :group 'org-structure)
  4006. (defcustom org-archive-location "%s_archive::"
  4007. "The location where subtrees should be archived.
  4008. The value of this variable is a string, consisting of two parts,
  4009. separated by a double-colon. The first part is a filename and
  4010. the second part is a headline.
  4011. When the filename is omitted, archiving happens in the same file.
  4012. %s in the filename will be replaced by the current file
  4013. name (without the directory part). Archiving to a different file
  4014. is useful to keep archived entries from contributing to the
  4015. Org-mode Agenda.
  4016. The archived entries will be filed as subtrees of the specified
  4017. headline. When the headline is omitted, the subtrees are simply
  4018. filed away at the end of the file, as top-level entries. Also in
  4019. the heading you can use %s to represent the file name, this can be
  4020. useful when using the same archive for a number of different files.
  4021. Here are a few examples:
  4022. \"%s_archive::\"
  4023. If the current file is Projects.org, archive in file
  4024. Projects.org_archive, as top-level trees. This is the default.
  4025. \"::* Archived Tasks\"
  4026. Archive in the current file, under the top-level headline
  4027. \"* Archived Tasks\".
  4028. \"~/org/archive.org::\"
  4029. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4030. \"~/org/archive.org::* From %s\"
  4031. Archive in file ~/org/archive.org (absolute path), under headlines
  4032. \"From FILENAME\" where file name is the current file name.
  4033. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4034. The \"datetree/\" string is special, signifying to archive
  4035. items to the datetree. Items are placed in either the CLOSED
  4036. date of the item, or the current date if there is no CLOSED date.
  4037. The heading will be a subentry to the current date. There doesn't
  4038. need to be a heading, but there always needs to be a slash after
  4039. datetree. For example, to store archived items directly in the
  4040. datetree, use \"~/org/datetree.org::datetree/\".
  4041. \"basement::** Finished Tasks\"
  4042. Archive in file ./basement (relative path), as level 3 trees
  4043. below the level 2 heading \"** Finished Tasks\".
  4044. You may set this option on a per-file basis by adding to the buffer a
  4045. line like
  4046. #+ARCHIVE: basement::** Finished Tasks
  4047. You may also define it locally for a subtree by setting an ARCHIVE property
  4048. in the entry. If such a property is found in an entry, or anywhere up
  4049. the hierarchy, it will be used."
  4050. :group 'org-archive
  4051. :type 'string)
  4052. (defcustom org-agenda-skip-archived-trees t
  4053. "Non-nil means the agenda will skip any items located in archived trees.
  4054. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4055. variable is no longer recommended, you should leave it at the value t.
  4056. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4057. :group 'org-archive
  4058. :group 'org-agenda-skip
  4059. :type 'boolean)
  4060. (defcustom org-columns-skip-archived-trees t
  4061. "Non-nil means ignore archived trees when creating column view."
  4062. :group 'org-archive
  4063. :group 'org-properties
  4064. :type 'boolean)
  4065. (defcustom org-cycle-open-archived-trees nil
  4066. "Non-nil means `org-cycle' will open archived trees.
  4067. An archived tree is a tree marked with the tag ARCHIVE.
  4068. When nil, archived trees will stay folded. You can still open them with
  4069. normal outline commands like `show-all', but not with the cycling commands."
  4070. :group 'org-archive
  4071. :group 'org-cycle
  4072. :type 'boolean)
  4073. (defcustom org-sparse-tree-open-archived-trees nil
  4074. "Non-nil means sparse tree construction shows matches in archived trees.
  4075. When nil, matches in these trees are highlighted, but the trees are kept in
  4076. collapsed state."
  4077. :group 'org-archive
  4078. :group 'org-sparse-trees
  4079. :type 'boolean)
  4080. (defcustom org-sparse-tree-default-date-type nil
  4081. "The default date type when building a sparse tree.
  4082. When this is nil, a date is a scheduled or a deadline timestamp.
  4083. Otherwise, these types are allowed:
  4084. all: all timestamps
  4085. active: only active timestamps (<...>)
  4086. inactive: only inactive timestamps ([...])
  4087. scheduled: only scheduled timestamps
  4088. deadline: only deadline timestamps"
  4089. :type '(choice (const :tag "Scheduled or deadline" nil)
  4090. (const :tag "All timestamps" all)
  4091. (const :tag "Only active timestamps" active)
  4092. (const :tag "Only inactive timestamps" inactive)
  4093. (const :tag "Only scheduled timestamps" scheduled)
  4094. (const :tag "Only deadline timestamps" deadline)
  4095. (const :tag "Only closed timestamps" closed))
  4096. :version "25.1"
  4097. :package-version '(Org . "8.3")
  4098. :group 'org-sparse-trees)
  4099. (defun org-cycle-hide-archived-subtrees (state)
  4100. "Re-hide all archived subtrees after a visibility state change."
  4101. (when (and (not org-cycle-open-archived-trees)
  4102. (not (memq state '(overview folded))))
  4103. (save-excursion
  4104. (let* ((globalp (memq state '(contents all)))
  4105. (beg (if globalp (point-min) (point)))
  4106. (end (if globalp (point-max) (org-end-of-subtree t))))
  4107. (org-hide-archived-subtrees beg end)
  4108. (goto-char beg)
  4109. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4110. (message "%s" (substitute-command-keys
  4111. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4112. (defun org-force-cycle-archived ()
  4113. "Cycle subtree even if it is archived."
  4114. (interactive)
  4115. (setq this-command 'org-cycle)
  4116. (let ((org-cycle-open-archived-trees t))
  4117. (call-interactively 'org-cycle)))
  4118. (defun org-hide-archived-subtrees (beg end)
  4119. "Re-hide all archived subtrees after a visibility state change."
  4120. (org-with-wide-buffer
  4121. (let ((case-fold-search nil)
  4122. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4123. (goto-char beg)
  4124. (while (and (< (point) end) (re-search-forward re end t))
  4125. (when (member org-archive-tag (org-get-tags))
  4126. (org-flag-subtree t)
  4127. (org-end-of-subtree t))))))
  4128. (declare-function outline-end-of-heading "outline" ())
  4129. (declare-function outline-flag-region "outline" (from to flag))
  4130. (defun org-flag-subtree (flag)
  4131. (save-excursion
  4132. (org-back-to-heading t)
  4133. (outline-end-of-heading)
  4134. (outline-flag-region (point)
  4135. (progn (org-end-of-subtree t) (point))
  4136. flag)))
  4137. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4138. ;; Declare Column View Code
  4139. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4140. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4141. (declare-function org-columns-compute "org-colview" (property))
  4142. ;; Declare ID code
  4143. (declare-function org-id-store-link "org-id")
  4144. (declare-function org-id-locations-load "org-id")
  4145. (declare-function org-id-locations-save "org-id")
  4146. (defvar org-id-track-globally)
  4147. ;;; Variables for pre-computed regular expressions, all buffer local
  4148. (defvar org-todo-regexp nil
  4149. "Matches any of the TODO state keywords.")
  4150. (make-variable-buffer-local 'org-todo-regexp)
  4151. (defvar org-not-done-regexp nil
  4152. "Matches any of the TODO state keywords except the last one.")
  4153. (make-variable-buffer-local 'org-not-done-regexp)
  4154. (defvar org-not-done-heading-regexp nil
  4155. "Matches a TODO headline that is not done.")
  4156. (make-variable-buffer-local 'org-not-done-regexp)
  4157. (defvar org-todo-line-regexp nil
  4158. "Matches a headline and puts TODO state into group 2 if present.")
  4159. (make-variable-buffer-local 'org-todo-line-regexp)
  4160. (defvar org-complex-heading-regexp nil
  4161. "Matches a headline and puts everything into groups:
  4162. group 1: the stars
  4163. group 2: The todo keyword, maybe
  4164. group 3: Priority cookie
  4165. group 4: True headline
  4166. group 5: Tags")
  4167. (make-variable-buffer-local 'org-complex-heading-regexp)
  4168. (defvar org-complex-heading-regexp-format nil
  4169. "Printf format to make regexp to match an exact headline.
  4170. This regexp will match the headline of any node which has the
  4171. exact headline text that is put into the format, but may have any
  4172. TODO state, priority and tags.")
  4173. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4174. (defvar org-todo-line-tags-regexp nil
  4175. "Matches a headline and puts TODO state into group 2 if present.
  4176. Also put tags into group 4 if tags are present.")
  4177. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4178. (defconst org-plain-time-of-day-regexp
  4179. (concat
  4180. "\\(\\<[012]?[0-9]"
  4181. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4182. "\\(--?"
  4183. "\\(\\<[012]?[0-9]"
  4184. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4185. "\\)?")
  4186. "Regular expression to match a plain time or time range.
  4187. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4188. groups carry important information:
  4189. 0 the full match
  4190. 1 the first time, range or not
  4191. 8 the second time, if it is a range.")
  4192. (defconst org-plain-time-extension-regexp
  4193. (concat
  4194. "\\(\\<[012]?[0-9]"
  4195. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4196. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4197. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4198. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4199. groups carry important information:
  4200. 0 the full match
  4201. 7 hours of duration
  4202. 9 minutes of duration")
  4203. (defconst org-stamp-time-of-day-regexp
  4204. (concat
  4205. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4206. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4207. "\\(--?"
  4208. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4209. "Regular expression to match a timestamp time or time range.
  4210. After a match, the following groups carry important information:
  4211. 0 the full match
  4212. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4213. 2 the first time, range or not
  4214. 4 the second time, if it is a range.")
  4215. (defconst org-startup-options
  4216. '(("fold" org-startup-folded t)
  4217. ("overview" org-startup-folded t)
  4218. ("nofold" org-startup-folded nil)
  4219. ("showall" org-startup-folded nil)
  4220. ("showeverything" org-startup-folded showeverything)
  4221. ("content" org-startup-folded content)
  4222. ("indent" org-startup-indented t)
  4223. ("noindent" org-startup-indented nil)
  4224. ("hidestars" org-hide-leading-stars t)
  4225. ("showstars" org-hide-leading-stars nil)
  4226. ("odd" org-odd-levels-only t)
  4227. ("oddeven" org-odd-levels-only nil)
  4228. ("align" org-startup-align-all-tables t)
  4229. ("noalign" org-startup-align-all-tables nil)
  4230. ("inlineimages" org-startup-with-inline-images t)
  4231. ("noinlineimages" org-startup-with-inline-images nil)
  4232. ("latexpreview" org-startup-with-latex-preview t)
  4233. ("nolatexpreview" org-startup-with-latex-preview nil)
  4234. ("customtime" org-display-custom-times t)
  4235. ("logdone" org-log-done time)
  4236. ("lognotedone" org-log-done note)
  4237. ("nologdone" org-log-done nil)
  4238. ("lognoteclock-out" org-log-note-clock-out t)
  4239. ("nolognoteclock-out" org-log-note-clock-out nil)
  4240. ("logrepeat" org-log-repeat state)
  4241. ("lognoterepeat" org-log-repeat note)
  4242. ("logdrawer" org-log-into-drawer t)
  4243. ("nologdrawer" org-log-into-drawer nil)
  4244. ("logstatesreversed" org-log-states-order-reversed t)
  4245. ("nologstatesreversed" org-log-states-order-reversed nil)
  4246. ("nologrepeat" org-log-repeat nil)
  4247. ("logreschedule" org-log-reschedule time)
  4248. ("lognotereschedule" org-log-reschedule note)
  4249. ("nologreschedule" org-log-reschedule nil)
  4250. ("logredeadline" org-log-redeadline time)
  4251. ("lognoteredeadline" org-log-redeadline note)
  4252. ("nologredeadline" org-log-redeadline nil)
  4253. ("logrefile" org-log-refile time)
  4254. ("lognoterefile" org-log-refile note)
  4255. ("nologrefile" org-log-refile nil)
  4256. ("fninline" org-footnote-define-inline t)
  4257. ("nofninline" org-footnote-define-inline nil)
  4258. ("fnlocal" org-footnote-section nil)
  4259. ("fnauto" org-footnote-auto-label t)
  4260. ("fnprompt" org-footnote-auto-label nil)
  4261. ("fnconfirm" org-footnote-auto-label confirm)
  4262. ("fnplain" org-footnote-auto-label plain)
  4263. ("fnadjust" org-footnote-auto-adjust t)
  4264. ("nofnadjust" org-footnote-auto-adjust nil)
  4265. ("constcgs" constants-unit-system cgs)
  4266. ("constSI" constants-unit-system SI)
  4267. ("noptag" org-tag-persistent-alist nil)
  4268. ("hideblocks" org-hide-block-startup t)
  4269. ("nohideblocks" org-hide-block-startup nil)
  4270. ("beamer" org-startup-with-beamer-mode t)
  4271. ("entitiespretty" org-pretty-entities t)
  4272. ("entitiesplain" org-pretty-entities nil))
  4273. "Variable associated with STARTUP options for org-mode.
  4274. Each element is a list of three items: the startup options (as written
  4275. in the #+STARTUP line), the corresponding variable, and the value to set
  4276. this variable to if the option is found. An optional forth element PUSH
  4277. means to push this value onto the list in the variable.")
  4278. (defcustom org-group-tags t
  4279. "When non-nil (the default), use group tags.
  4280. This can be turned on/off through `org-toggle-tags-groups'."
  4281. :group 'org-tags
  4282. :group 'org-startup
  4283. :type 'boolean)
  4284. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4285. (defun org-toggle-tags-groups ()
  4286. "Toggle support for group tags.
  4287. Support for group tags is controlled by the option
  4288. `org-group-tags', which is non-nil by default."
  4289. (interactive)
  4290. (setq org-group-tags (not org-group-tags))
  4291. (cond ((and (derived-mode-p 'org-agenda-mode)
  4292. org-group-tags)
  4293. (org-agenda-redo))
  4294. ((derived-mode-p 'org-mode)
  4295. (let ((org-inhibit-startup t)) (org-mode))))
  4296. (message "Groups tags support has been turned %s"
  4297. (if org-group-tags "on" "off")))
  4298. (defun org-set-regexps-and-options (&optional tags-only)
  4299. "Precompute regular expressions used in the current buffer.
  4300. When optional argument TAGS-ONLY is non-nil, only compute tags
  4301. related expressions."
  4302. (when (derived-mode-p 'org-mode)
  4303. (let ((alist (org--setup-collect-keywords
  4304. (org-make-options-regexp
  4305. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4306. (and (not tags-only)
  4307. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4308. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4309. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4310. (org--setup-process-tags
  4311. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4312. (unless tags-only
  4313. ;; File properties.
  4314. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4315. ;; Archive location.
  4316. (let ((archive (cdr (assq 'archive alist))))
  4317. (when archive (org-set-local 'org-archive-location archive)))
  4318. ;; Category.
  4319. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4320. (when cat
  4321. (org-set-local 'org-category (intern cat))
  4322. (org-set-local 'org-file-properties
  4323. (org--update-property-plist
  4324. "CATEGORY" cat org-file-properties))))
  4325. ;; Columns.
  4326. (let ((column (cdr (assq 'columns alist))))
  4327. (when column (org-set-local 'org-columns-default-format column)))
  4328. ;; Constants.
  4329. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4330. ;; Link abbreviations.
  4331. (let ((links (cdr (assq 'link alist))))
  4332. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4333. ;; Priorities.
  4334. (let ((priorities (cdr (assq 'priorities alist))))
  4335. (when priorities
  4336. (org-set-local 'org-highest-priority (nth 0 priorities))
  4337. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4338. (org-set-local 'org-default-priority (nth 2 priorities))))
  4339. ;; Scripts.
  4340. (let ((scripts (assq 'scripts alist)))
  4341. (when scripts
  4342. (org-set-local 'org-use-sub-superscripts (cdr scripts))))
  4343. ;; Startup options.
  4344. (let ((startup (cdr (assq 'startup alist))))
  4345. (dolist (option startup)
  4346. (let ((entry (assoc-string option org-startup-options t)))
  4347. (when entry
  4348. (let ((var (nth 1 entry))
  4349. (val (nth 2 entry)))
  4350. (if (not (nth 3 entry)) (org-set-local var val)
  4351. (unless (listp (symbol-value var))
  4352. (org-set-local var nil))
  4353. (add-to-list var val)))))))
  4354. ;; TODO keywords.
  4355. (org-set-local 'org-todo-kwd-alist nil)
  4356. (org-set-local 'org-todo-key-alist nil)
  4357. (org-set-local 'org-todo-key-trigger nil)
  4358. (org-set-local 'org-todo-keywords-1 nil)
  4359. (org-set-local 'org-done-keywords nil)
  4360. (org-set-local 'org-todo-heads nil)
  4361. (org-set-local 'org-todo-sets nil)
  4362. (org-set-local 'org-todo-log-states nil)
  4363. (let ((todo-sequences
  4364. (or (nreverse (cdr (assq 'todo alist)))
  4365. (let ((d (default-value 'org-todo-keywords)))
  4366. (if (not (stringp (car d))) d
  4367. ;; XXX: Backward compatibility code.
  4368. (list (cons org-todo-interpretation d)))))))
  4369. (dolist (sequence todo-sequences)
  4370. (let* ((sequence (or (run-hook-with-args-until-success
  4371. 'org-todo-setup-filter-hook sequence)
  4372. sequence))
  4373. (sequence-type (car sequence))
  4374. (keywords (cdr sequence))
  4375. (sep (member "|" keywords))
  4376. names alist)
  4377. (dolist (k (remove "|" keywords))
  4378. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4379. k)
  4380. (error "Invalid TODO keyword %s" k))
  4381. (let ((name (match-string 1 k))
  4382. (key (match-string 2 k))
  4383. (log (org-extract-log-state-settings k)))
  4384. (push name names)
  4385. (push (cons name (and key (string-to-char key))) alist)
  4386. (when log (push log org-todo-log-states))))
  4387. (let* ((names (nreverse names))
  4388. (done (if sep (org-remove-keyword-keys (cdr sep))
  4389. (last names)))
  4390. (head (car names))
  4391. (tail (list sequence-type head (car done) (org-last done))))
  4392. (add-to-list 'org-todo-heads head 'append)
  4393. (push names org-todo-sets)
  4394. (setq org-done-keywords (append org-done-keywords done nil))
  4395. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4396. (setq org-todo-key-alist
  4397. (append org-todo-key-alist
  4398. (and alist
  4399. (append '((:startgroup))
  4400. (nreverse alist)
  4401. '((:endgroup))))))
  4402. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4403. (setq org-todo-sets (nreverse org-todo-sets)
  4404. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4405. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4406. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4407. ;; Compute the regular expressions and other local variables.
  4408. ;; Using `org-outline-regexp-bol' would complicate them much,
  4409. ;; because of the fixed white space at the end of that string.
  4410. (if (not org-done-keywords)
  4411. (setq org-done-keywords
  4412. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4413. (setq org-not-done-keywords
  4414. (org-delete-all org-done-keywords
  4415. (copy-sequence org-todo-keywords-1))
  4416. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4417. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4418. org-not-done-heading-regexp
  4419. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4420. org-todo-line-regexp
  4421. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4422. org-complex-heading-regexp
  4423. (concat "^\\(\\*+\\)"
  4424. "\\(?: +" org-todo-regexp "\\)?"
  4425. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4426. "\\(?: +\\(.*?\\)\\)??"
  4427. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4428. "[ \t]*$")
  4429. org-complex-heading-regexp-format
  4430. (concat "^\\(\\*+\\)"
  4431. "\\(?: +" org-todo-regexp "\\)?"
  4432. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4433. "\\(?: +"
  4434. ;; Stats cookies can be stuck to body.
  4435. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4436. "\\(%s\\)"
  4437. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4438. "\\)"
  4439. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4440. "[ \t]*$")
  4441. org-todo-line-tags-regexp
  4442. (concat "^\\(\\*+\\)"
  4443. "\\(?: +" org-todo-regexp "\\)?"
  4444. "\\(?: +\\(.*?\\)\\)??"
  4445. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4446. "[ \t]*$"))
  4447. (org-compute-latex-and-related-regexp)))))
  4448. (defun org--setup-collect-keywords (regexp &optional files alist)
  4449. "Return setup keywords values as an alist.
  4450. REGEXP matches a subset of setup keywords. FILES is a list of
  4451. file names already visited. It is used to avoid circular setup
  4452. files. ALIST, when non-nil, is the alist computed so far.
  4453. Return value contains the following keys: `archive', `category',
  4454. `columns', `constants', `filetags', `link', `priorities',
  4455. `property', `scripts', `startup', `tags' and `todo'."
  4456. (org-with-wide-buffer
  4457. (goto-char (point-min))
  4458. (let ((case-fold-search t))
  4459. (while (re-search-forward regexp nil t)
  4460. (let ((element (org-element-at-point)))
  4461. (when (eq (org-element-type element) 'keyword)
  4462. (let ((key (org-element-property :key element))
  4463. (value (org-element-property :value element)))
  4464. (cond
  4465. ((equal key "ARCHIVE")
  4466. (when (org-string-nw-p value)
  4467. (push (cons 'archive value) alist)))
  4468. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4469. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4470. ((equal key "CONSTANTS")
  4471. (let* ((constants (assq 'constants alist))
  4472. (store (cdr constants)))
  4473. (dolist (pair (org-split-string value))
  4474. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4475. pair)
  4476. (let* ((name (match-string 1 pair))
  4477. (value (match-string 2 pair))
  4478. (old (assoc name store)))
  4479. (if old (setcdr old value)
  4480. (push (cons name value) store)))))
  4481. (if constants (setcdr constants store)
  4482. (push (cons 'constants store) alist))))
  4483. ((equal key "FILETAGS")
  4484. (when (org-string-nw-p value)
  4485. (let ((old (assq 'filetags alist))
  4486. (new (apply #'nconc
  4487. (mapcar (lambda (x) (org-split-string x ":"))
  4488. (org-split-string value)))))
  4489. (if old (setcdr old (append new (cdr old)))
  4490. (push (cons 'filetags new) alist)))))
  4491. ((equal key "LINK")
  4492. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4493. (let ((links (assq 'link alist))
  4494. (pair (cons (org-match-string-no-properties 1 value)
  4495. (org-match-string-no-properties 2 value))))
  4496. (if links (push pair (cdr links))
  4497. (push (list 'link pair) alist)))))
  4498. ((equal key "OPTIONS")
  4499. (when (and (org-string-nw-p value)
  4500. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4501. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4502. ((equal key "PRIORITIES")
  4503. (push (cons 'priorities
  4504. (let ((prio (org-split-string value)))
  4505. (if (< (length prio) 3) '(?A ?C ?B)
  4506. (mapcar #'string-to-char prio))))
  4507. alist))
  4508. ((equal key "PROPERTY")
  4509. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4510. (let* ((property (assq 'property alist))
  4511. (value (org--update-property-plist
  4512. (org-match-string-no-properties 1 value)
  4513. (org-match-string-no-properties 2 value)
  4514. (cdr property))))
  4515. (if property (setcdr property value)
  4516. (push (cons 'property value) alist)))))
  4517. ((equal key "STARTUP")
  4518. (let ((startup (assq 'startup alist)))
  4519. (if startup
  4520. (setcdr startup
  4521. (append (cdr startup) (org-split-string value)))
  4522. (push (cons 'startup (org-split-string value)) alist))))
  4523. ((equal key "TAGS")
  4524. (let ((tag-cell (assq 'tags alist)))
  4525. (if tag-cell
  4526. (setcdr tag-cell
  4527. (append (cdr tag-cell)
  4528. '("\\n")
  4529. (org-split-string value)))
  4530. (push (cons 'tags (org-split-string value)) alist))))
  4531. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4532. (let ((todo (assq 'todo alist))
  4533. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4534. (org-split-string value))))
  4535. (if todo (push value (cdr todo))
  4536. (push (list 'todo value) alist))))
  4537. ((equal key "SETUPFILE")
  4538. (unless buffer-read-only ; Do not check in Gnus messages.
  4539. (let ((f (and (org-string-nw-p value)
  4540. (expand-file-name
  4541. (org-remove-double-quotes value)))))
  4542. (when (and f (file-readable-p f) (not (member f files)))
  4543. (with-temp-buffer
  4544. (insert-file-contents f)
  4545. (setq alist
  4546. ;; Fake Org mode to benefit from cache
  4547. ;; without recurring needlessly.
  4548. (let ((major-mode 'org-mode))
  4549. (org--setup-collect-keywords
  4550. regexp (cons f files) alist)))))))))))))))
  4551. alist)
  4552. (defun org--setup-process-tags (tags filetags)
  4553. "Precompute variables used for tags.
  4554. TAGS is a list of tags and tag group symbols, as strings.
  4555. FILETAGS is a list of tags, as strings."
  4556. ;; Process the file tags.
  4557. (org-set-local 'org-file-tags
  4558. (mapcar #'org-add-prop-inherited filetags))
  4559. ;; Provide default tags if no local tags are found.
  4560. (when (and (not tags) org-tag-alist)
  4561. (setq tags
  4562. (mapcar (lambda (tag)
  4563. (case (car tag)
  4564. (:startgroup "{")
  4565. (:endgroup "}")
  4566. (:startgrouptag "[")
  4567. (:endgrouptag "]")
  4568. (:grouptags ":")
  4569. (:newline "\\n")
  4570. (otherwise (concat (car tag)
  4571. (and (characterp (cdr tag))
  4572. (format "(%c)" (cdr tag)))))))
  4573. org-tag-alist)))
  4574. ;; Process the tags.
  4575. (org-set-local 'org-tag-groups-alist nil)
  4576. (org-set-local 'org-tag-alist nil)
  4577. (let (group-flag)
  4578. (while tags
  4579. (let ((e (car tags)))
  4580. (setq tags (cdr tags))
  4581. (cond
  4582. ((equal e "{")
  4583. (push '(:startgroup) org-tag-alist)
  4584. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4585. ((equal e "}")
  4586. (push '(:endgroup) org-tag-alist)
  4587. (setq group-flag nil))
  4588. ((equal e "[")
  4589. (push '(:startgrouptag) org-tag-alist)
  4590. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4591. ((equal e "]")
  4592. (push '(:endgrouptag) org-tag-alist)
  4593. (setq group-flag nil))
  4594. ((equal e ":")
  4595. (push '(:grouptags) org-tag-alist)
  4596. (setq group-flag 'append))
  4597. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4598. ((string-match
  4599. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4600. "\\|{.+?}\\)" ; regular expression
  4601. "\\(?:(\\(.\\))\\)?\\'")) e)
  4602. (let ((tag (match-string 1 e))
  4603. (key (and (match-beginning 2)
  4604. (string-to-char (match-string 2 e)))))
  4605. (cond ((eq group-flag 'append)
  4606. (setcar org-tag-groups-alist
  4607. (append (car org-tag-groups-alist) (list tag))))
  4608. (group-flag (push (list tag) org-tag-groups-alist)))
  4609. ;; Push all tags in groups, no matter if they already exist.
  4610. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4611. (push (cons tag key) org-tag-alist))))))))
  4612. (setq org-tag-alist (nreverse org-tag-alist)))
  4613. (defun org-file-contents (file &optional noerror)
  4614. "Return the contents of FILE, as a string."
  4615. (if (and file (file-readable-p file))
  4616. (with-temp-buffer
  4617. (insert-file-contents file)
  4618. (buffer-string))
  4619. (funcall (if noerror 'message 'error)
  4620. "Cannot read file \"%s\"%s"
  4621. file
  4622. (let ((from (buffer-file-name (buffer-base-buffer))))
  4623. (if from (concat " (referenced in file \"" from "\")") "")))))
  4624. (defun org-extract-log-state-settings (x)
  4625. "Extract the log state setting from a TODO keyword string.
  4626. This will extract info from a string like \"WAIT(w@/!)\"."
  4627. (let (kw key log1 log2)
  4628. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4629. (setq kw (match-string 1 x)
  4630. key (and (match-end 2) (match-string 2 x))
  4631. log1 (and (match-end 3) (match-string 3 x))
  4632. log2 (and (match-end 4) (match-string 4 x)))
  4633. (and (or log1 log2)
  4634. (list kw
  4635. (and log1 (if (equal log1 "!") 'time 'note))
  4636. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4637. (defun org-remove-keyword-keys (list)
  4638. "Remove a pair of parenthesis at the end of each string in LIST."
  4639. (mapcar (lambda (x)
  4640. (if (string-match "(.*)$" x)
  4641. (substring x 0 (match-beginning 0))
  4642. x))
  4643. list))
  4644. (defun org-assign-fast-keys (alist)
  4645. "Assign fast keys to a keyword-key alist.
  4646. Respect keys that are already there."
  4647. (let (new e (alt ?0))
  4648. (while (setq e (pop alist))
  4649. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4650. (cdr e)) ;; Key already assigned.
  4651. (push e new)
  4652. (let ((clist (string-to-list (downcase (car e))))
  4653. (used (append new alist)))
  4654. (when (= (car clist) ?@)
  4655. (pop clist))
  4656. (while (and clist (rassoc (car clist) used))
  4657. (pop clist))
  4658. (unless clist
  4659. (while (rassoc alt used)
  4660. (incf alt)))
  4661. (push (cons (car e) (or (car clist) alt)) new))))
  4662. (nreverse new)))
  4663. ;;; Some variables used in various places
  4664. (defvar org-window-configuration nil
  4665. "Used in various places to store a window configuration.")
  4666. (defvar org-selected-window nil
  4667. "Used in various places to store a window configuration.")
  4668. (defvar org-finish-function nil
  4669. "Function to be called when `C-c C-c' is used.
  4670. This is for getting out of special buffers like capture.")
  4671. ;; FIXME: Occasionally check by commenting these, to make sure
  4672. ;; no other functions uses these, forgetting to let-bind them.
  4673. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4674. (defvar org-last-state)
  4675. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4676. ;; Defined somewhere in this file, but used before definition.
  4677. (defvar org-entities) ;; defined in org-entities.el
  4678. (defvar org-struct-menu)
  4679. (defvar org-org-menu)
  4680. (defvar org-tbl-menu)
  4681. ;;;; Define the Org-mode
  4682. ;; We use a before-change function to check if a table might need
  4683. ;; an update.
  4684. (defvar org-table-may-need-update t
  4685. "Indicates that a table might need an update.
  4686. This variable is set by `org-before-change-function'.
  4687. `org-table-align' sets it back to nil.")
  4688. (defun org-before-change-function (beg end)
  4689. "Every change indicates that a table might need an update."
  4690. (setq org-table-may-need-update t))
  4691. (defvar org-mode-map)
  4692. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4693. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4694. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4695. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4696. (defvar org-table-buffer-is-an nil)
  4697. (defvar bidi-paragraph-direction)
  4698. (defvar buffer-face-mode-face)
  4699. (require 'outline)
  4700. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4701. (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"))
  4702. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4703. ;; Other stuff we need.
  4704. (require 'time-date)
  4705. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4706. (require 'easymenu)
  4707. (require 'overlay)
  4708. ;; (require 'org-macs) moved higher up in the file before it is first used
  4709. (require 'org-entities)
  4710. ;; (require 'org-compat) moved higher up in the file before it is first used
  4711. (require 'org-faces)
  4712. (require 'org-list)
  4713. (require 'org-pcomplete)
  4714. (require 'org-src)
  4715. (require 'org-footnote)
  4716. (require 'org-macro)
  4717. ;; babel
  4718. (require 'ob)
  4719. ;;;###autoload
  4720. (define-derived-mode org-mode outline-mode "Org"
  4721. "Outline-based notes management and organizer, alias
  4722. \"Carsten's outline-mode for keeping track of everything.\"
  4723. Org-mode develops organizational tasks around a NOTES file which
  4724. contains information about projects as plain text. Org-mode is
  4725. implemented on top of outline-mode, which is ideal to keep the content
  4726. of large files well structured. It supports ToDo items, deadlines and
  4727. time stamps, which magically appear in the diary listing of the Emacs
  4728. calendar. Tables are easily created with a built-in table editor.
  4729. Plain text URL-like links connect to websites, emails (VM), Usenet
  4730. messages (Gnus), BBDB entries, and any files related to the project.
  4731. For printing and sharing of notes, an Org-mode file (or a part of it)
  4732. can be exported as a structured ASCII or HTML file.
  4733. The following commands are available:
  4734. \\{org-mode-map}"
  4735. ;; Get rid of Outline menus, they are not needed
  4736. ;; Need to do this here because define-derived-mode sets up
  4737. ;; the keymap so late. Still, it is a waste to call this each time
  4738. ;; we switch another buffer into org-mode.
  4739. (if (featurep 'xemacs)
  4740. (when (boundp 'outline-mode-menu-heading)
  4741. ;; Assume this is Greg's port, it uses easymenu
  4742. (easy-menu-remove outline-mode-menu-heading)
  4743. (easy-menu-remove outline-mode-menu-show)
  4744. (easy-menu-remove outline-mode-menu-hide))
  4745. (define-key org-mode-map [menu-bar headings] 'undefined)
  4746. (define-key org-mode-map [menu-bar hide] 'undefined)
  4747. (define-key org-mode-map [menu-bar show] 'undefined))
  4748. (org-load-modules-maybe)
  4749. (easy-menu-add org-org-menu)
  4750. (easy-menu-add org-tbl-menu)
  4751. (org-install-agenda-files-menu)
  4752. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4753. (add-to-invisibility-spec '(org-cwidth))
  4754. (add-to-invisibility-spec '(org-hide-block . t))
  4755. (when (featurep 'xemacs)
  4756. (org-set-local 'line-move-ignore-invisible t))
  4757. (org-set-local 'outline-regexp org-outline-regexp)
  4758. (org-set-local 'outline-level 'org-outline-level)
  4759. (setq bidi-paragraph-direction 'left-to-right)
  4760. (when (and org-ellipsis
  4761. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4762. (fboundp 'make-glyph-code))
  4763. (unless org-display-table
  4764. (setq org-display-table (make-display-table)))
  4765. (set-display-table-slot
  4766. org-display-table 4
  4767. (vconcat (mapcar
  4768. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4769. org-ellipsis)))
  4770. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4771. (setq buffer-display-table org-display-table))
  4772. (org-set-regexps-and-options)
  4773. (org-set-font-lock-defaults)
  4774. (when (and org-tag-faces (not org-tags-special-faces-re))
  4775. ;; tag faces set outside customize.... force initialization.
  4776. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4777. ;; Calc embedded
  4778. (org-set-local 'calc-embedded-open-mode "# ")
  4779. ;; Modify a few syntax entries
  4780. (modify-syntax-entry ?@ "w")
  4781. (modify-syntax-entry ?\" "\"")
  4782. (modify-syntax-entry ?\\ "_")
  4783. (modify-syntax-entry ?~ "_")
  4784. (if org-startup-truncated (setq truncate-lines t))
  4785. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4786. (org-set-local 'font-lock-unfontify-region-function
  4787. 'org-unfontify-region)
  4788. ;; Activate before-change-function
  4789. (org-set-local 'org-table-may-need-update t)
  4790. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4791. 'local)
  4792. ;; Check for running clock before killing a buffer
  4793. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4794. ;; Initialize macros templates.
  4795. (org-macro-initialize-templates)
  4796. ;; Initialize radio targets.
  4797. (org-update-radio-target-regexp)
  4798. ;; Indentation.
  4799. (org-set-local 'indent-line-function 'org-indent-line)
  4800. (org-set-local 'indent-region-function 'org-indent-region)
  4801. ;; Filling and auto-filling.
  4802. (org-setup-filling)
  4803. ;; Comments.
  4804. (org-setup-comments-handling)
  4805. ;; Initialize cache.
  4806. (org-element-cache-reset)
  4807. ;; Beginning/end of defun
  4808. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4809. (org-set-local 'end-of-defun-function
  4810. (lambda ()
  4811. (if (not (org-at-heading-p))
  4812. (org-forward-element)
  4813. (org-forward-element)
  4814. (forward-char -1))))
  4815. ;; Next error for sparse trees
  4816. (org-set-local 'next-error-function 'org-occur-next-match)
  4817. ;; Make sure dependence stuff works reliably, even for users who set it
  4818. ;; too late :-(
  4819. (if org-enforce-todo-dependencies
  4820. (add-hook 'org-blocker-hook
  4821. 'org-block-todo-from-children-or-siblings-or-parent)
  4822. (remove-hook 'org-blocker-hook
  4823. 'org-block-todo-from-children-or-siblings-or-parent))
  4824. (if org-enforce-todo-checkbox-dependencies
  4825. (add-hook 'org-blocker-hook
  4826. 'org-block-todo-from-checkboxes)
  4827. (remove-hook 'org-blocker-hook
  4828. 'org-block-todo-from-checkboxes))
  4829. ;; Align options lines
  4830. (org-set-local
  4831. 'align-mode-rules-list
  4832. '((org-in-buffer-settings
  4833. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4834. (modes . '(org-mode)))))
  4835. ;; Imenu
  4836. (org-set-local 'imenu-create-index-function
  4837. 'org-imenu-get-tree)
  4838. ;; Make isearch reveal context
  4839. (if (or (featurep 'xemacs)
  4840. (not (boundp 'outline-isearch-open-invisible-function)))
  4841. ;; Emacs 21 and XEmacs make use of the hook
  4842. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4843. ;; Emacs 22 deals with this through a special variable
  4844. (org-set-local 'outline-isearch-open-invisible-function
  4845. (lambda (&rest ignore) (org-show-context 'isearch))))
  4846. ;; Setup the pcomplete hooks
  4847. (set (make-local-variable 'pcomplete-command-completion-function)
  4848. 'org-pcomplete-initial)
  4849. (set (make-local-variable 'pcomplete-command-name-function)
  4850. 'org-command-at-point)
  4851. (set (make-local-variable 'pcomplete-default-completion-function)
  4852. 'ignore)
  4853. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4854. 'org-parse-arguments)
  4855. (set (make-local-variable 'pcomplete-termination-string) "")
  4856. (when (>= emacs-major-version 23)
  4857. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4858. ;; If empty file that did not turn on org-mode automatically, make it to.
  4859. (if (and org-insert-mode-line-in-empty-file
  4860. (org-called-interactively-p 'any)
  4861. (= (point-min) (point-max)))
  4862. (insert "# -*- mode: org -*-\n\n"))
  4863. (unless org-inhibit-startup
  4864. (org-unmodified
  4865. (and org-startup-with-beamer-mode (org-beamer-mode))
  4866. (when org-startup-align-all-tables
  4867. (org-table-map-tables 'org-table-align 'quietly))
  4868. (when org-startup-with-inline-images
  4869. (org-display-inline-images))
  4870. (when org-startup-with-latex-preview
  4871. (org-toggle-latex-fragment))
  4872. (unless org-inhibit-startup-visibility-stuff
  4873. (org-set-startup-visibility))
  4874. (org-refresh-effort-properties)))
  4875. ;; Try to set org-hide correctly
  4876. (let ((foreground (org-find-invisible-foreground)))
  4877. (if foreground
  4878. (set-face-foreground 'org-hide foreground))))
  4879. ;; Update `customize-package-emacs-version-alist'
  4880. (add-to-list 'customize-package-emacs-version-alist
  4881. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4882. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4883. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4884. (defvar org-mode-transpose-word-syntax-table
  4885. (let ((st (make-syntax-table text-mode-syntax-table)))
  4886. (mapc (lambda(c) (modify-syntax-entry
  4887. (string-to-char (car c)) "w p" st))
  4888. org-emphasis-alist)
  4889. st))
  4890. (when (fboundp 'abbrev-table-put)
  4891. (abbrev-table-put org-mode-abbrev-table
  4892. :parents (list text-mode-abbrev-table)))
  4893. (defun org-find-invisible-foreground ()
  4894. (let ((candidates (remove
  4895. "unspecified-bg"
  4896. (nconc
  4897. (list (face-background 'default)
  4898. (face-background 'org-default))
  4899. (mapcar
  4900. (lambda (alist)
  4901. (when (boundp alist)
  4902. (cdr (assoc 'background-color (symbol-value alist)))))
  4903. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4904. (list (face-foreground 'org-hide))))))
  4905. (car (remove nil candidates))))
  4906. (defun org-current-time (&optional rounding-minutes past)
  4907. "Current time, possibly rounded to ROUNDING-MINUTES.
  4908. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4909. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4910. the rounding returns a past time."
  4911. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4912. (car org-time-stamp-rounding-minutes)))
  4913. (time (decode-time)) res)
  4914. (if (< r 1)
  4915. (current-time)
  4916. (setq res
  4917. (apply 'encode-time
  4918. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4919. (nthcdr 2 time))))
  4920. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4921. (seconds-to-time (- (org-float-time res) (* r 60)))
  4922. res))))
  4923. (defun org-today ()
  4924. "Return today date, considering `org-extend-today-until'."
  4925. (time-to-days
  4926. (time-subtract (current-time)
  4927. (list 0 (* 3600 org-extend-today-until) 0))))
  4928. ;;;; Font-Lock stuff, including the activators
  4929. (defvar org-mouse-map (make-sparse-keymap))
  4930. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4931. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4932. (when org-mouse-1-follows-link
  4933. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4934. (when org-tab-follows-link
  4935. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4936. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4937. (require 'font-lock)
  4938. (defconst org-non-link-chars "]\t\n\r<>")
  4939. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4940. "file+sys" "news" "shell" "elisp" "doi" "message"
  4941. "help"))
  4942. (defvar org-link-types-re nil
  4943. "Matches a link that has a url-like prefix like \"http:\"")
  4944. (defvar org-link-re-with-space nil
  4945. "Matches a link with spaces, optional angular brackets around it.")
  4946. (defvar org-link-re-with-space2 nil
  4947. "Matches a link with spaces, optional angular brackets around it.")
  4948. (defvar org-link-re-with-space3 nil
  4949. "Matches a link with spaces, only for internal part in bracket links.")
  4950. (defvar org-angle-link-re nil
  4951. "Matches link with angular brackets, spaces are allowed.")
  4952. (defvar org-plain-link-re nil
  4953. "Matches plain link, without spaces.")
  4954. (defvar org-bracket-link-regexp nil
  4955. "Matches a link in double brackets.")
  4956. (defvar org-bracket-link-analytic-regexp nil
  4957. "Regular expression used to analyze links.
  4958. Here is what the match groups contain after a match:
  4959. 1: http:
  4960. 2: http
  4961. 3: path
  4962. 4: [desc]
  4963. 5: desc")
  4964. (defvar org-bracket-link-analytic-regexp++ nil
  4965. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4966. (defvar org-any-link-re nil
  4967. "Regular expression matching any link.")
  4968. (defconst org-match-sexp-depth 3
  4969. "Number of stacked braces for sub/superscript matching.")
  4970. (defun org-create-multibrace-regexp (left right n)
  4971. "Create a regular expression which will match a balanced sexp.
  4972. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4973. as single character strings.
  4974. The regexp returned will match the entire expression including the
  4975. delimiters. It will also define a single group which contains the
  4976. match except for the outermost delimiters. The maximum depth of
  4977. stacked delimiters is N. Escaping delimiters is not possible."
  4978. (let* ((nothing (concat "[^" left right "]*?"))
  4979. (or "\\|")
  4980. (re nothing)
  4981. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4982. (while (> n 1)
  4983. (setq n (1- n)
  4984. re (concat re or next)
  4985. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4986. (concat left "\\(" re "\\)" right)))
  4987. (defconst org-match-substring-regexp
  4988. (concat
  4989. "\\(\\S-\\)\\([_^]\\)\\("
  4990. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4991. "\\|"
  4992. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4993. "\\|"
  4994. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4995. "The regular expression matching a sub- or superscript.")
  4996. (defconst org-match-substring-with-braces-regexp
  4997. (concat
  4998. "\\(\\S-\\)\\([_^]\\)"
  4999. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  5000. "The regular expression matching a sub- or superscript, forcing braces.")
  5001. (defun org-make-link-regexps ()
  5002. "Update the link regular expressions.
  5003. This should be called after the variable `org-link-types' has changed."
  5004. (let ((types-re (regexp-opt org-link-types t)))
  5005. (setq org-link-types-re
  5006. (concat "\\`" types-re ":")
  5007. org-link-re-with-space
  5008. (concat "<?" types-re ":"
  5009. "\\([^" org-non-link-chars " ]"
  5010. "[^" org-non-link-chars "]*"
  5011. "[^" org-non-link-chars " ]\\)>?")
  5012. org-link-re-with-space2
  5013. (concat "<?" types-re ":"
  5014. "\\([^" org-non-link-chars " ]"
  5015. "[^\t\n\r]*"
  5016. "[^" org-non-link-chars " ]\\)>?")
  5017. org-link-re-with-space3
  5018. (concat "<?" types-re ":"
  5019. "\\([^" org-non-link-chars " ]"
  5020. "[^\t\n\r]*\\)")
  5021. org-angle-link-re
  5022. (concat "<" types-re ":"
  5023. "\\([^" org-non-link-chars " ]"
  5024. "[^" org-non-link-chars "]*"
  5025. "\\)>")
  5026. org-plain-link-re
  5027. (concat
  5028. "\\<" types-re ":"
  5029. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5030. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5031. org-bracket-link-regexp
  5032. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5033. org-bracket-link-analytic-regexp
  5034. (concat
  5035. "\\[\\["
  5036. "\\(" types-re ":\\)?"
  5037. "\\([^]]+\\)"
  5038. "\\]"
  5039. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5040. "\\]")
  5041. org-bracket-link-analytic-regexp++
  5042. (concat
  5043. "\\[\\["
  5044. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5045. "\\([^]]+\\)"
  5046. "\\]"
  5047. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5048. "\\]")
  5049. org-any-link-re
  5050. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5051. org-angle-link-re "\\)\\|\\("
  5052. org-plain-link-re "\\)"))))
  5053. (org-make-link-regexps)
  5054. (defvar org-emph-face nil)
  5055. (defun org-do-emphasis-faces (limit)
  5056. "Run through the buffer and emphasize strings."
  5057. (let (rtn a)
  5058. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5059. (let* ((border (char-after (match-beginning 3)))
  5060. (bre (regexp-quote (char-to-string border))))
  5061. (if (and (not (= border (char-after (match-beginning 4))))
  5062. (not (save-match-data
  5063. (string-match (concat bre ".*" bre)
  5064. (replace-regexp-in-string
  5065. "\n" " "
  5066. (substring (match-string 2) 1 -1))))))
  5067. (progn
  5068. (setq rtn t)
  5069. (setq a (assoc (match-string 3) org-emphasis-alist))
  5070. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5071. 'face
  5072. (nth 1 a))
  5073. (and (nth 2 a)
  5074. (org-remove-flyspell-overlays-in
  5075. (match-beginning 0) (match-end 0)))
  5076. (add-text-properties (match-beginning 2) (match-end 2)
  5077. '(font-lock-multiline t org-emphasis t))
  5078. (when org-hide-emphasis-markers
  5079. (add-text-properties (match-end 4) (match-beginning 5)
  5080. '(invisible org-link))
  5081. (add-text-properties (match-beginning 3) (match-end 3)
  5082. '(invisible org-link))))))
  5083. (goto-char (1+ (match-beginning 0))))
  5084. rtn))
  5085. (defun org-emphasize (&optional char)
  5086. "Insert or change an emphasis, i.e. a font like bold or italic.
  5087. If there is an active region, change that region to a new emphasis.
  5088. If there is no region, just insert the marker characters and position
  5089. the cursor between them.
  5090. CHAR should be the marker character. If it is a space, it means to
  5091. remove the emphasis of the selected region.
  5092. If CHAR is not given (for example in an interactive call) it will be
  5093. prompted for."
  5094. (interactive)
  5095. (let ((erc org-emphasis-regexp-components)
  5096. (prompt "")
  5097. (string "") beg end move c s)
  5098. (if (org-region-active-p)
  5099. (setq beg (region-beginning) end (region-end)
  5100. string (buffer-substring beg end))
  5101. (setq move t))
  5102. (unless char
  5103. (message "Emphasis marker or tag: [%s]"
  5104. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5105. (setq char (read-char-exclusive)))
  5106. (if (equal char ?\ )
  5107. (setq s "" move nil)
  5108. (unless (assoc (char-to-string char) org-emphasis-alist)
  5109. (user-error "No such emphasis marker: \"%c\"" char))
  5110. (setq s (char-to-string char)))
  5111. (while (and (> (length string) 1)
  5112. (equal (substring string 0 1) (substring string -1))
  5113. (assoc (substring string 0 1) org-emphasis-alist))
  5114. (setq string (substring string 1 -1)))
  5115. (setq string (concat s string s))
  5116. (if beg (delete-region beg end))
  5117. (unless (or (bolp)
  5118. (string-match (concat "[" (nth 0 erc) "\n]")
  5119. (char-to-string (char-before (point)))))
  5120. (insert " "))
  5121. (unless (or (eobp)
  5122. (string-match (concat "[" (nth 1 erc) "\n]")
  5123. (char-to-string (char-after (point)))))
  5124. (insert " ") (backward-char 1))
  5125. (insert string)
  5126. (and move (backward-char 1))))
  5127. (defconst org-nonsticky-props
  5128. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5129. (defsubst org-rear-nonsticky-at (pos)
  5130. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5131. (defun org-activate-plain-links (limit)
  5132. "Add link properties for plain links."
  5133. (let (f hl)
  5134. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5135. (not (member 'org-tag
  5136. (get-text-property (1- (match-beginning 0)) 'face)))
  5137. (not (org-in-src-block-p)))
  5138. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5139. (setq f (get-text-property (match-beginning 0) 'face))
  5140. (setq hl (org-match-string-no-properties 0))
  5141. (if (or (eq f 'org-tag)
  5142. (and (listp f) (memq 'org-tag f)))
  5143. nil
  5144. (add-text-properties (match-beginning 0) (match-end 0)
  5145. (list 'mouse-face 'highlight
  5146. 'face 'org-link
  5147. 'htmlize-link `(:uri ,hl)
  5148. 'keymap org-mouse-map))
  5149. (org-rear-nonsticky-at (match-end 0)))
  5150. t)))
  5151. (defun org-activate-code (limit)
  5152. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5153. (progn
  5154. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5155. (remove-text-properties (match-beginning 0) (match-end 0)
  5156. '(display t invisible t intangible t))
  5157. t)))
  5158. (defcustom org-src-fontify-natively t
  5159. "When non-nil, fontify code in code blocks."
  5160. :type 'boolean
  5161. :version "24.4"
  5162. :package-version '(Org . "8.3")
  5163. :group 'org-appearance
  5164. :group 'org-babel)
  5165. (defcustom org-allow-promoting-top-level-subtree nil
  5166. "When non-nil, allow promoting a top level subtree.
  5167. The leading star of the top level headline will be replaced
  5168. by a #."
  5169. :type 'boolean
  5170. :version "24.1"
  5171. :group 'org-appearance)
  5172. (defun org-fontify-meta-lines-and-blocks (limit)
  5173. (condition-case nil
  5174. (org-fontify-meta-lines-and-blocks-1 limit)
  5175. (error (message "org-mode fontification error"))))
  5176. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5177. "Fontify #+ lines and blocks."
  5178. (let ((case-fold-search t))
  5179. (if (re-search-forward
  5180. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5181. limit t)
  5182. (let ((beg (match-beginning 0))
  5183. (block-start (match-end 0))
  5184. (block-end nil)
  5185. (lang (match-string 7))
  5186. (beg1 (line-beginning-position 2))
  5187. (dc1 (downcase (match-string 2)))
  5188. (dc3 (downcase (match-string 3)))
  5189. end end1 quoting block-type ovl)
  5190. (cond
  5191. ((and (match-end 4) (equal dc3 "+begin"))
  5192. ;; Truly a block
  5193. (setq block-type (downcase (match-string 5))
  5194. quoting (member block-type org-protecting-blocks))
  5195. (when (re-search-forward
  5196. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5197. nil t) ;; on purpose, we look further than LIMIT
  5198. (setq end (min (point-max) (match-end 0))
  5199. end1 (min (point-max) (1- (match-beginning 0))))
  5200. (setq block-end (match-beginning 0))
  5201. (when quoting
  5202. (org-remove-flyspell-overlays-in beg1 end1)
  5203. (remove-text-properties beg end
  5204. '(display t invisible t intangible t)))
  5205. (add-text-properties
  5206. beg end '(font-lock-fontified t font-lock-multiline t))
  5207. (add-text-properties beg beg1 '(face org-meta-line))
  5208. (org-remove-flyspell-overlays-in beg beg1)
  5209. (add-text-properties ; For end_src
  5210. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5211. (org-remove-flyspell-overlays-in end1 end)
  5212. (cond
  5213. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5214. (org-src-font-lock-fontify-block lang block-start block-end)
  5215. (add-text-properties beg1 block-end '(src-block t)))
  5216. (quoting
  5217. (add-text-properties beg1 (min (point-max) (1+ end1))
  5218. '(face org-block))) ; end of source block
  5219. ((not org-fontify-quote-and-verse-blocks))
  5220. ((string= block-type "quote")
  5221. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5222. ((string= block-type "verse")
  5223. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5224. (add-text-properties beg beg1 '(face org-block-begin-line))
  5225. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5226. '(face org-block-end-line))
  5227. t))
  5228. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5229. (org-remove-flyspell-overlays-in
  5230. (match-beginning 0)
  5231. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5232. (add-text-properties
  5233. beg (match-end 3)
  5234. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5235. '(font-lock-fontified t invisible t)
  5236. '(font-lock-fontified t face org-document-info-keyword)))
  5237. (add-text-properties
  5238. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5239. (if (string-equal dc1 "+title:")
  5240. '(font-lock-fontified t face org-document-title)
  5241. '(font-lock-fontified t face org-document-info))))
  5242. ((equal dc1 "+caption:")
  5243. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5244. (remove-text-properties (match-beginning 0) (match-end 0)
  5245. '(display t invisible t intangible t))
  5246. (add-text-properties (match-beginning 1) (match-end 3)
  5247. '(font-lock-fontified t face org-meta-line))
  5248. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5249. '(font-lock-fontified t face org-block))
  5250. t)
  5251. ((member dc3 '(" " ""))
  5252. (org-remove-flyspell-overlays-in beg (match-end 0))
  5253. (add-text-properties
  5254. beg (match-end 0)
  5255. '(font-lock-fontified t face font-lock-comment-face)))
  5256. (t ;; just any other in-buffer setting, but not indented
  5257. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5258. (remove-text-properties (match-beginning 0) (match-end 0)
  5259. '(display t invisible t intangible t))
  5260. (add-text-properties beg (match-end 0)
  5261. '(font-lock-fontified t face org-meta-line))
  5262. t))))))
  5263. (defun org-fontify-drawers (limit)
  5264. "Fontify drawers."
  5265. (when (re-search-forward org-drawer-regexp limit t)
  5266. (add-text-properties
  5267. (match-beginning 0) (match-end 0)
  5268. '(font-lock-fontified t face org-special-keyword))
  5269. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5270. t))
  5271. (defun org-fontify-macros (limit)
  5272. "Fontify macros."
  5273. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5274. (add-text-properties
  5275. (match-beginning 0) (match-end 0)
  5276. '(font-lock-fontified t face org-macro))
  5277. (when org-hide-macro-markers
  5278. (add-text-properties (match-end 2) (match-beginning 2)
  5279. '(invisible t))
  5280. (add-text-properties (match-beginning 1) (match-end 1)
  5281. '(invisible t)))
  5282. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5283. t))
  5284. (defun org-activate-angle-links (limit)
  5285. "Add text properties for angle links."
  5286. (if (and (re-search-forward org-angle-link-re limit t)
  5287. (not (org-in-src-block-p)))
  5288. (progn
  5289. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5290. (add-text-properties (match-beginning 0) (match-end 0)
  5291. (list 'mouse-face 'highlight
  5292. 'keymap org-mouse-map))
  5293. (org-rear-nonsticky-at (match-end 0))
  5294. t)))
  5295. (defun org-activate-footnote-links (limit)
  5296. "Add text properties for footnotes."
  5297. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5298. (when fn
  5299. (let* ((beg (nth 1 fn))
  5300. (end (nth 2 fn))
  5301. (label (car fn))
  5302. (referencep (/= (line-beginning-position) beg)))
  5303. (when (and referencep (nth 3 fn))
  5304. (save-excursion
  5305. (goto-char beg)
  5306. (search-forward (or label "fn:"))
  5307. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5308. (add-text-properties beg end
  5309. (list 'mouse-face 'highlight
  5310. 'keymap org-mouse-map
  5311. 'help-echo
  5312. (if referencep "Footnote reference"
  5313. "Footnote definition")
  5314. 'font-lock-fontified t
  5315. 'font-lock-multiline t
  5316. 'face 'org-footnote))))))
  5317. (defun org-activate-bracket-links (limit)
  5318. "Add text properties for bracketed links."
  5319. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5320. (not (org-in-src-block-p)))
  5321. (let* ((hl (org-match-string-no-properties 1))
  5322. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5323. (ip (org-maybe-intangible
  5324. (list 'invisible 'org-link
  5325. 'keymap org-mouse-map 'mouse-face 'highlight
  5326. 'font-lock-multiline t 'help-echo help
  5327. 'htmlize-link `(:uri ,hl))))
  5328. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5329. 'font-lock-multiline t 'help-echo help
  5330. 'htmlize-link `(:uri ,hl))))
  5331. ;; We need to remove the invisible property here. Table narrowing
  5332. ;; may have made some of this invisible.
  5333. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5334. (remove-text-properties (match-beginning 0) (match-end 0)
  5335. '(invisible nil))
  5336. (if (match-end 3)
  5337. (progn
  5338. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5339. (org-rear-nonsticky-at (match-beginning 3))
  5340. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5341. (org-rear-nonsticky-at (match-end 3))
  5342. (add-text-properties (match-end 3) (match-end 0) ip)
  5343. (org-rear-nonsticky-at (match-end 0)))
  5344. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5345. (org-rear-nonsticky-at (match-beginning 1))
  5346. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5347. (org-rear-nonsticky-at (match-end 1))
  5348. (add-text-properties (match-end 1) (match-end 0) ip)
  5349. (org-rear-nonsticky-at (match-end 0)))
  5350. t)))
  5351. (defun org-activate-dates (limit)
  5352. "Add text properties for dates."
  5353. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5354. (not (equal (char-before (match-beginning 0)) 91)))
  5355. (progn
  5356. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5357. (add-text-properties (match-beginning 0) (match-end 0)
  5358. (list 'mouse-face 'highlight
  5359. 'keymap org-mouse-map))
  5360. (org-rear-nonsticky-at (match-end 0))
  5361. (when org-display-custom-times
  5362. (if (match-end 3)
  5363. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5364. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5365. t)))
  5366. (defvar org-target-link-regexp nil
  5367. "Regular expression matching radio targets in plain text.")
  5368. (make-variable-buffer-local 'org-target-link-regexp)
  5369. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5370. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5371. border border border))
  5372. "Regular expression matching a link target.")
  5373. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5374. "Regular expression matching a radio target.")
  5375. (defconst org-any-target-regexp
  5376. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5377. "Regular expression matching any target.")
  5378. (defun org-activate-target-links (limit)
  5379. "Add text properties for target matches."
  5380. (when org-target-link-regexp
  5381. (let ((case-fold-search t))
  5382. (if (re-search-forward org-target-link-regexp limit t)
  5383. (progn
  5384. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5385. (add-text-properties (match-beginning 1) (match-end 1)
  5386. (list 'mouse-face 'highlight
  5387. 'keymap org-mouse-map
  5388. 'help-echo "Radio target link"
  5389. 'org-linked-text t))
  5390. (org-rear-nonsticky-at (match-end 1))
  5391. t)))))
  5392. (defun org-update-radio-target-regexp ()
  5393. "Find all radio targets in this file and update the regular expression.
  5394. Also refresh fontification if needed."
  5395. (interactive)
  5396. (let ((old-regexp org-target-link-regexp)
  5397. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5398. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5399. (targets
  5400. (org-with-wide-buffer
  5401. (goto-char (point-min))
  5402. (let (rtn)
  5403. (while (re-search-forward org-radio-target-regexp nil t)
  5404. ;; Make sure point is really within the object.
  5405. (backward-char)
  5406. (let ((obj (org-element-context)))
  5407. (when (eq (org-element-type obj) 'radio-target)
  5408. (add-to-list 'rtn (org-element-property :value obj)))))
  5409. rtn))))
  5410. (setq org-target-link-regexp
  5411. (and targets
  5412. (concat before-re
  5413. (mapconcat
  5414. (lambda (x)
  5415. (replace-regexp-in-string
  5416. " +" "\\s-+" (regexp-quote x) t t))
  5417. targets
  5418. "\\|")
  5419. after-re)))
  5420. (unless (equal old-regexp org-target-link-regexp)
  5421. ;; Clean-up cache.
  5422. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5423. ((not org-target-link-regexp) old-regexp)
  5424. (t
  5425. (concat before-re
  5426. (mapconcat
  5427. (lambda (re)
  5428. (substring re (length before-re)
  5429. (- (length after-re))))
  5430. (list old-regexp org-target-link-regexp)
  5431. "\\|")
  5432. after-re)))))
  5433. (org-with-wide-buffer
  5434. (goto-char (point-min))
  5435. (while (re-search-forward regexp nil t)
  5436. (org-element-cache-refresh (match-beginning 1)))))
  5437. ;; Re fontify buffer.
  5438. (when (memq 'radio org-highlight-links)
  5439. (org-restart-font-lock)))))
  5440. (defun org-hide-wide-columns (limit)
  5441. (let (s e)
  5442. (setq s (text-property-any (point) (or limit (point-max))
  5443. 'org-cwidth t))
  5444. (when s
  5445. (setq e (next-single-property-change s 'org-cwidth))
  5446. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5447. (goto-char e)
  5448. t)))
  5449. (defvar org-latex-and-related-regexp nil
  5450. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5451. (defun org-compute-latex-and-related-regexp ()
  5452. "Compute regular expression for LaTeX, entities and sub/superscript.
  5453. Result depends on variable `org-highlight-latex-and-related'."
  5454. (org-set-local
  5455. 'org-latex-and-related-regexp
  5456. (let* ((re-sub
  5457. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5458. ((eq org-use-sub-superscripts '{})
  5459. (list org-match-substring-with-braces-regexp))
  5460. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5461. (re-latex
  5462. (when (memq 'latex org-highlight-latex-and-related)
  5463. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5464. (delq nil
  5465. (mapcar (lambda (x)
  5466. (and (member (car x) matchers) (nth 1 x)))
  5467. org-latex-regexps)))))
  5468. (re-entities
  5469. (when (memq 'entities org-highlight-latex-and-related)
  5470. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5471. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5472. (defun org-do-latex-and-related (limit)
  5473. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5474. LIMIT bounds the search for syntax to highlight. Stop at first
  5475. highlighted object, if any. Return t if some highlighting was
  5476. done, nil otherwise."
  5477. (when (org-string-nw-p org-latex-and-related-regexp)
  5478. (catch 'found
  5479. (while (re-search-forward org-latex-and-related-regexp limit t)
  5480. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5481. 'face))
  5482. '(org-code org-verbatim underline))
  5483. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5484. '(?_ ?^))
  5485. 1
  5486. 0)))
  5487. (font-lock-prepend-text-property
  5488. (+ offset (match-beginning 0)) (match-end 0)
  5489. 'face 'org-latex-and-related)
  5490. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5491. '(font-lock-multiline t)))
  5492. (throw 'found t)))
  5493. nil)))
  5494. (defun org-restart-font-lock ()
  5495. "Restart `font-lock-mode', to force refontification."
  5496. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5497. (font-lock-mode -1)
  5498. (font-lock-mode 1)))
  5499. (defun org-activate-tags (limit)
  5500. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5501. (progn
  5502. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5503. (add-text-properties (match-beginning 1) (match-end 1)
  5504. (list 'mouse-face 'highlight
  5505. 'keymap org-mouse-map))
  5506. (org-rear-nonsticky-at (match-end 1))
  5507. t)))
  5508. (defun org-outline-level ()
  5509. "Compute the outline level of the heading at point.
  5510. If this is called at a normal headline, the level is the number
  5511. of stars. Use `org-reduced-level' to remove the effect of
  5512. `org-odd-levels'. Unlike to `org-current-level', this function
  5513. takes into consideration inlinetasks."
  5514. (org-with-wide-buffer
  5515. (end-of-line)
  5516. (if (re-search-backward org-outline-regexp-bol nil t)
  5517. (1- (- (match-end 0) (match-beginning 0)))
  5518. 0)))
  5519. (defvar org-font-lock-keywords nil)
  5520. (defsubst org-re-property (property &optional literal allow-null value)
  5521. "Return a regexp matching a PROPERTY line.
  5522. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5523. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5524. non-nil, match properties even without a value.
  5525. Match group 3 is set to the value when it exists. If there is no
  5526. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5527. With optional argument VALUE, match only property lines with
  5528. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5529. unless LITERAL is non-nil."
  5530. (concat
  5531. "^\\(?4:[ \t]*\\)"
  5532. (format "\\(?1::\\(?2:%s\\):\\)"
  5533. (if literal property (regexp-quote property)))
  5534. (cond (value
  5535. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5536. (if literal value (regexp-quote value))))
  5537. (allow-null
  5538. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5539. (t
  5540. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5541. (defconst org-property-re
  5542. (org-re-property "\\S-+" 'literal t)
  5543. "Regular expression matching a property line.
  5544. There are four matching groups:
  5545. 1: :PROPKEY: including the leading and trailing colon,
  5546. 2: PROPKEY without the leading and trailing colon,
  5547. 3: PROPVAL without leading or trailing spaces,
  5548. 4: the indentation of the current line,
  5549. 5: trailing whitespace.")
  5550. (defvar org-font-lock-hook nil
  5551. "Functions to be called for special font lock stuff.")
  5552. (defvar org-font-lock-set-keywords-hook nil
  5553. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5554. This is called after `org-font-lock-extra-keywords' is defined, but before
  5555. it is installed to be used by font lock. This can be useful if something
  5556. needs to be inserted at a specific position in the font-lock sequence.")
  5557. (defun org-font-lock-hook (limit)
  5558. "Run `org-font-lock-hook' within LIMIT."
  5559. (run-hook-with-args 'org-font-lock-hook limit))
  5560. (defun org-set-font-lock-defaults ()
  5561. "Set font lock defaults for the current buffer."
  5562. (let* ((em org-fontify-emphasized-text)
  5563. (lk org-highlight-links)
  5564. (org-font-lock-extra-keywords
  5565. (list
  5566. ;; Call the hook
  5567. '(org-font-lock-hook)
  5568. ;; Headlines
  5569. `(,(if org-fontify-whole-heading-line
  5570. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5571. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5572. (1 (org-get-level-face 1))
  5573. (2 (org-get-level-face 2))
  5574. (3 (org-get-level-face 3)))
  5575. ;; Table lines
  5576. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5577. (1 'org-table t))
  5578. ;; Table internals
  5579. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5580. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5581. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5582. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5583. ;; Drawers
  5584. '(org-fontify-drawers)
  5585. ;; Properties
  5586. (list org-property-re
  5587. '(1 'org-special-keyword t)
  5588. '(3 'org-property-value t))
  5589. ;; Links
  5590. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5591. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5592. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5593. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5594. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5595. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5596. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5597. ;; Targets.
  5598. (list org-any-target-regexp '(0 'org-target t))
  5599. ;; Diary sexps.
  5600. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5601. ;; Macro
  5602. '(org-fontify-macros)
  5603. '(org-hide-wide-columns (0 nil append))
  5604. ;; TODO keyword
  5605. (list (format org-heading-keyword-regexp-format
  5606. org-todo-regexp)
  5607. '(2 (org-get-todo-face 2) t))
  5608. ;; DONE
  5609. (if org-fontify-done-headline
  5610. (list (format org-heading-keyword-regexp-format
  5611. (concat
  5612. "\\(?:"
  5613. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5614. "\\)"))
  5615. '(2 'org-headline-done t))
  5616. nil)
  5617. ;; Priorities
  5618. '(org-font-lock-add-priority-faces)
  5619. ;; Tags
  5620. '(org-font-lock-add-tag-faces)
  5621. ;; Tags groups
  5622. (if (and org-group-tags org-tag-groups-alist)
  5623. (list (concat org-outline-regexp-bol ".+\\(:"
  5624. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5625. ":\\).*$")
  5626. '(1 'org-tag-group prepend)))
  5627. ;; Special keywords
  5628. (list (concat "\\<" org-comment-string) '(0 'org-special-keyword t))
  5629. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5630. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5631. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5632. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5633. ;; Emphasis
  5634. (if em
  5635. (if (featurep 'xemacs)
  5636. '(org-do-emphasis-faces (0 nil append))
  5637. '(org-do-emphasis-faces)))
  5638. ;; Checkboxes
  5639. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5640. 1 'org-checkbox prepend)
  5641. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5642. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5643. (0 (org-get-checkbox-statistics-face) t)))
  5644. ;; Description list items
  5645. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5646. 1 'org-list-dt prepend)
  5647. ;; ARCHIVEd headings
  5648. (list (concat
  5649. org-outline-regexp-bol
  5650. "\\(.*:" org-archive-tag ":.*\\)")
  5651. '(1 'org-archived prepend))
  5652. ;; Specials
  5653. '(org-do-latex-and-related)
  5654. '(org-fontify-entities)
  5655. '(org-raise-scripts)
  5656. ;; Code
  5657. '(org-activate-code (1 'org-code t))
  5658. ;; COMMENT
  5659. (list (format
  5660. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5661. org-todo-regexp
  5662. org-comment-string)
  5663. '(9 'org-special-keyword t))
  5664. ;; Blocks and meta lines
  5665. '(org-fontify-meta-lines-and-blocks))))
  5666. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5667. (run-hooks 'org-font-lock-set-keywords-hook)
  5668. ;; Now set the full font-lock-keywords
  5669. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5670. (org-set-local 'font-lock-defaults
  5671. '(org-font-lock-keywords t nil nil backward-paragraph))
  5672. (kill-local-variable 'font-lock-keywords) nil))
  5673. (defun org-toggle-pretty-entities ()
  5674. "Toggle the composition display of entities as UTF8 characters."
  5675. (interactive)
  5676. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5677. (org-restart-font-lock)
  5678. (if org-pretty-entities
  5679. (message "Entities are now displayed as UTF8 characters")
  5680. (save-restriction
  5681. (widen)
  5682. (org-decompose-region (point-min) (point-max))
  5683. (message "Entities are now displayed as plain text"))))
  5684. (defvar org-custom-properties-overlays nil
  5685. "List of overlays used for custom properties.")
  5686. (make-variable-buffer-local 'org-custom-properties-overlays)
  5687. (defun org-toggle-custom-properties-visibility ()
  5688. "Display or hide properties in `org-custom-properties'."
  5689. (interactive)
  5690. (if org-custom-properties-overlays
  5691. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5692. (setq org-custom-properties-overlays nil))
  5693. (when org-custom-properties
  5694. (org-with-wide-buffer
  5695. (goto-char (point-min))
  5696. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5697. (while (re-search-forward regexp nil t)
  5698. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5699. (when (and end (< (point) end))
  5700. ;; Hide first custom property in current drawer.
  5701. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5702. (overlay-put o 'invisible t)
  5703. (overlay-put o 'org-custom-property t)
  5704. (push o org-custom-properties-overlays))
  5705. ;; Hide additional custom properties in the same drawer.
  5706. (while (re-search-forward regexp end t)
  5707. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5708. (overlay-put o 'invisible t)
  5709. (overlay-put o 'org-custom-property t)
  5710. (push o org-custom-properties-overlays)))))
  5711. ;; Each entry is limited to a single property drawer.
  5712. (outline-next-heading)))))))
  5713. (defun org-fontify-entities (limit)
  5714. "Find an entity to fontify."
  5715. (let (ee)
  5716. (when org-pretty-entities
  5717. (catch 'match
  5718. ;; "\_ "-family is left out on purpose. Only the first one,
  5719. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5720. ;; confusing when adding a second white space character.
  5721. (while (re-search-forward
  5722. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5723. limit t)
  5724. (if (and (not (org-at-comment-p))
  5725. (setq ee (org-entity-get (match-string 1)))
  5726. (= (length (nth 6 ee)) 1))
  5727. (let*
  5728. ((end (if (equal (match-string 2) "{}")
  5729. (match-end 2)
  5730. (match-end 1))))
  5731. (add-text-properties
  5732. (match-beginning 0) end
  5733. (list 'font-lock-fontified t))
  5734. (compose-region (match-beginning 0) end
  5735. (nth 6 ee) nil)
  5736. (backward-char 1)
  5737. (throw 'match t))))
  5738. nil))))
  5739. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5740. "Fontify string S like in Org-mode."
  5741. (with-temp-buffer
  5742. (insert s)
  5743. (let ((org-odd-levels-only odd-levels))
  5744. (org-mode)
  5745. (font-lock-ensure)
  5746. (buffer-string))))
  5747. (defvar org-m nil)
  5748. (defvar org-l nil)
  5749. (defvar org-f nil)
  5750. (defun org-get-level-face (n)
  5751. "Get the right face for match N in font-lock matching of headlines."
  5752. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5753. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5754. (if org-cycle-level-faces
  5755. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5756. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5757. (cond
  5758. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5759. ((eq n 2) org-f)
  5760. (t (if org-level-color-stars-only nil org-f))))
  5761. (defun org-get-todo-face (kwd)
  5762. "Get the right face for a TODO keyword KWD.
  5763. If KWD is a number, get the corresponding match group."
  5764. (if (numberp kwd) (setq kwd (match-string kwd)))
  5765. (or (org-face-from-face-or-color
  5766. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5767. (and (member kwd org-done-keywords) 'org-done)
  5768. 'org-todo))
  5769. (defun org-face-from-face-or-color (context inherit face-or-color)
  5770. "Create a face list that inherits INHERIT, but sets the foreground color.
  5771. When FACE-OR-COLOR is not a string, just return it."
  5772. (if (stringp face-or-color)
  5773. (list :inherit inherit
  5774. (cdr (assoc context org-faces-easy-properties))
  5775. face-or-color)
  5776. face-or-color))
  5777. (defun org-font-lock-add-tag-faces (limit)
  5778. "Add the special tag faces."
  5779. (when (and org-tag-faces org-tags-special-faces-re)
  5780. (while (re-search-forward org-tags-special-faces-re limit t)
  5781. (add-text-properties (match-beginning 1) (match-end 1)
  5782. (list 'face (org-get-tag-face 1)
  5783. 'font-lock-fontified t))
  5784. (backward-char 1))))
  5785. (defun org-font-lock-add-priority-faces (limit)
  5786. "Add the special priority faces."
  5787. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5788. (when (save-match-data (org-at-heading-p))
  5789. (add-text-properties
  5790. (match-beginning 0) (match-end 0)
  5791. (list 'face (or (org-face-from-face-or-color
  5792. 'priority 'org-priority
  5793. (cdr (assoc (char-after (match-beginning 1))
  5794. org-priority-faces)))
  5795. 'org-priority)
  5796. 'font-lock-fontified t)))))
  5797. (defun org-get-tag-face (kwd)
  5798. "Get the right face for a TODO keyword KWD.
  5799. If KWD is a number, get the corresponding match group."
  5800. (if (numberp kwd) (setq kwd (match-string kwd)))
  5801. (or (org-face-from-face-or-color
  5802. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5803. 'org-tag))
  5804. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5805. "Remove fontification and activation overlays from links."
  5806. (font-lock-default-unfontify-region beg end)
  5807. (let* ((buffer-undo-list t)
  5808. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5809. (inhibit-modification-hooks t)
  5810. deactivate-mark buffer-file-name buffer-file-truename)
  5811. (org-decompose-region beg end)
  5812. (remove-text-properties beg end
  5813. '(mouse-face t keymap t org-linked-text t
  5814. invisible t intangible t
  5815. org-emphasis t))
  5816. (org-remove-font-lock-display-properties beg end)))
  5817. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5818. ((raise 0.3) (height 0.7))
  5819. ((raise -0.5))
  5820. ((raise 0.5)))
  5821. "Display properties for showing superscripts and subscripts.")
  5822. (defun org-remove-font-lock-display-properties (beg end)
  5823. "Remove specific display properties that have been added by font lock.
  5824. The will remove the raise properties that are used to show superscripts
  5825. and subscripts."
  5826. (let (next prop)
  5827. (while (< beg end)
  5828. (setq next (next-single-property-change beg 'display nil end)
  5829. prop (get-text-property beg 'display))
  5830. (if (member prop org-script-display)
  5831. (put-text-property beg next 'display nil))
  5832. (setq beg next))))
  5833. (defun org-raise-scripts (limit)
  5834. "Add raise properties to sub/superscripts."
  5835. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5836. (if (re-search-forward
  5837. (if (eq org-use-sub-superscripts t)
  5838. org-match-substring-regexp
  5839. org-match-substring-with-braces-regexp)
  5840. limit t)
  5841. (let* ((pos (point)) table-p comment-p
  5842. (mpos (match-beginning 3))
  5843. (emph-p (get-text-property mpos 'org-emphasis))
  5844. (link-p (get-text-property mpos 'mouse-face))
  5845. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5846. (goto-char (point-at-bol))
  5847. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5848. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5849. (goto-char pos)
  5850. ;; Handle a_b^c
  5851. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5852. (if (or comment-p emph-p link-p keyw-p)
  5853. t
  5854. (put-text-property (match-beginning 3) (match-end 0)
  5855. 'display
  5856. (if (equal (char-after (match-beginning 2)) ?^)
  5857. (nth (if table-p 3 1) org-script-display)
  5858. (nth (if table-p 2 0) org-script-display)))
  5859. (add-text-properties (match-beginning 2) (match-end 2)
  5860. (list 'invisible t
  5861. 'org-dwidth t 'org-dwidth-n 1))
  5862. (if (and (eq (char-after (match-beginning 3)) ?{)
  5863. (eq (char-before (match-end 3)) ?}))
  5864. (progn
  5865. (add-text-properties
  5866. (match-beginning 3) (1+ (match-beginning 3))
  5867. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5868. (add-text-properties
  5869. (1- (match-end 3)) (match-end 3)
  5870. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5871. t)))))
  5872. ;;;; Visibility cycling, including org-goto and indirect buffer
  5873. ;;; Cycling
  5874. (defvar org-cycle-global-status nil)
  5875. (make-variable-buffer-local 'org-cycle-global-status)
  5876. (put 'org-cycle-global-status 'org-state t)
  5877. (defvar org-cycle-subtree-status nil)
  5878. (make-variable-buffer-local 'org-cycle-subtree-status)
  5879. (put 'org-cycle-subtree-status 'org-state t)
  5880. (defvar org-inlinetask-min-level)
  5881. (defun org-unlogged-message (&rest args)
  5882. "Display a message, but avoid logging it in the *Messages* buffer."
  5883. (let ((message-log-max nil))
  5884. (apply 'message args)))
  5885. ;;;###autoload
  5886. (defun org-cycle (&optional arg)
  5887. "TAB-action and visibility cycling for Org-mode.
  5888. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5889. is outline visibility cycling, but it also invokes other actions
  5890. in special contexts.
  5891. - When this function is called with a prefix argument, rotate the entire
  5892. buffer through 3 states (global cycling)
  5893. 1. OVERVIEW: Show only top-level headlines.
  5894. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5895. 3. SHOW ALL: Show everything.
  5896. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5897. determined by the variable `org-startup-folded', and by any VISIBILITY
  5898. properties in the buffer.
  5899. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5900. including any drawers.
  5901. - When inside a table, re-align the table and move to the next field.
  5902. - When point is at the beginning of a headline, rotate the subtree started
  5903. by this line through 3 different states (local cycling)
  5904. 1. FOLDED: Only the main headline is shown.
  5905. 2. CHILDREN: The main headline and the direct children are shown.
  5906. From this state, you can move to one of the children
  5907. and zoom in further.
  5908. 3. SUBTREE: Show the entire subtree, including body text.
  5909. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5910. - When point is at the beginning of an empty headline and the variable
  5911. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5912. of the headline by demoting and promoting it to likely levels. This
  5913. speeds up creation document structure by pressing TAB once or several
  5914. times right after creating a new headline.
  5915. - When there is a numeric prefix, go up to a heading with level ARG, do
  5916. a `show-subtree' and return to the previous cursor position. If ARG
  5917. is negative, go up that many levels.
  5918. - When point is not at the beginning of a headline, execute the global
  5919. binding for TAB, which is re-indenting the line. See the option
  5920. `org-cycle-emulate-tab' for details.
  5921. - Special case: if point is at the beginning of the buffer and there is
  5922. no headline in line 1, this function will act as if called with prefix arg
  5923. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5924. But only if also the variable `org-cycle-global-at-bob' is t."
  5925. (interactive "P")
  5926. (org-load-modules-maybe)
  5927. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5928. (and org-cycle-level-after-item/entry-creation
  5929. (or (org-cycle-level)
  5930. (org-cycle-item-indentation))))
  5931. (let* ((limit-level
  5932. (or org-cycle-max-level
  5933. (and (boundp 'org-inlinetask-min-level)
  5934. org-inlinetask-min-level
  5935. (1- org-inlinetask-min-level))))
  5936. (nstars (and limit-level
  5937. (if org-odd-levels-only
  5938. (and limit-level (1- (* limit-level 2)))
  5939. limit-level)))
  5940. (org-outline-regexp
  5941. (if (not (derived-mode-p 'org-mode))
  5942. outline-regexp
  5943. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5944. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5945. (not (looking-at org-outline-regexp))))
  5946. (org-cycle-hook
  5947. (if bob-special
  5948. (delq 'org-optimize-window-after-visibility-change
  5949. (copy-sequence org-cycle-hook))
  5950. org-cycle-hook))
  5951. (pos (point)))
  5952. (if (or bob-special (equal arg '(4)))
  5953. ;; special case: use global cycling
  5954. (setq arg t))
  5955. (cond
  5956. ((equal arg '(16))
  5957. (setq last-command 'dummy)
  5958. (org-set-startup-visibility)
  5959. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5960. ((equal arg '(64))
  5961. (show-all)
  5962. (org-unlogged-message "Entire buffer visible, including drawers"))
  5963. ;; Try cdlatex TAB completion
  5964. ((org-try-cdlatex-tab))
  5965. ;; Table: enter it or move to the next field.
  5966. ((org-at-table-p 'any)
  5967. (if (org-at-table.el-p)
  5968. (message "Use C-c ' to edit table.el tables")
  5969. (if arg (org-table-edit-field t)
  5970. (org-table-justify-field-maybe)
  5971. (call-interactively 'org-table-next-field))))
  5972. ((run-hook-with-args-until-success
  5973. 'org-tab-after-check-for-table-hook))
  5974. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5975. ((eq arg t) (org-cycle-internal-global))
  5976. ;; Drawers: delegate to `org-flag-drawer'.
  5977. ((save-excursion
  5978. (beginning-of-line 1)
  5979. (looking-at org-drawer-regexp))
  5980. (org-flag-drawer ; toggle block visibility
  5981. (not (get-char-property (match-end 0) 'invisible))))
  5982. ;; Show-subtree, ARG levels up from here.
  5983. ((integerp arg)
  5984. (save-excursion
  5985. (org-back-to-heading)
  5986. (outline-up-heading (if (< arg 0) (- arg)
  5987. (- (funcall outline-level) arg)))
  5988. (org-show-subtree)))
  5989. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5990. ((and (featurep 'org-inlinetask)
  5991. (org-inlinetask-at-task-p)
  5992. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5993. (org-inlinetask-toggle-visibility))
  5994. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5995. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5996. (save-excursion (move-beginning-of-line 1)
  5997. (looking-at org-outline-regexp)))
  5998. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5999. (org-cycle-internal-local))
  6000. ;; From there: TAB emulation and template completion.
  6001. (buffer-read-only (org-back-to-heading))
  6002. ((run-hook-with-args-until-success
  6003. 'org-tab-after-check-for-cycling-hook))
  6004. ((org-try-structure-completion))
  6005. ((run-hook-with-args-until-success
  6006. 'org-tab-before-tab-emulation-hook))
  6007. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6008. (or (not (bolp))
  6009. (not (looking-at org-outline-regexp))))
  6010. (call-interactively (global-key-binding "\t")))
  6011. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6012. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6013. (or (and (eq org-cycle-emulate-tab 'white)
  6014. (= (match-end 0) (point-at-eol)))
  6015. (and (eq org-cycle-emulate-tab 'whitestart)
  6016. (>= (match-end 0) pos))))
  6017. t
  6018. (eq org-cycle-emulate-tab t))
  6019. (call-interactively (global-key-binding "\t")))
  6020. (t (save-excursion
  6021. (org-back-to-heading)
  6022. (org-cycle)))))))
  6023. (defun org-cycle-internal-global ()
  6024. "Do the global cycling action."
  6025. ;; Hack to avoid display of messages for .org attachments in Gnus
  6026. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6027. (cond
  6028. ((and (eq last-command this-command)
  6029. (eq org-cycle-global-status 'overview))
  6030. ;; We just created the overview - now do table of contents
  6031. ;; This can be slow in very large buffers, so indicate action
  6032. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6033. (unless ga (org-unlogged-message "CONTENTS..."))
  6034. (org-content)
  6035. (unless ga (org-unlogged-message "CONTENTS...done"))
  6036. (setq org-cycle-global-status 'contents)
  6037. (run-hook-with-args 'org-cycle-hook 'contents))
  6038. ((and (eq last-command this-command)
  6039. (eq org-cycle-global-status 'contents))
  6040. ;; We just showed the table of contents - now show everything
  6041. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6042. (show-all)
  6043. (unless ga (org-unlogged-message "SHOW ALL"))
  6044. (setq org-cycle-global-status 'all)
  6045. (run-hook-with-args 'org-cycle-hook 'all))
  6046. (t
  6047. ;; Default action: go to overview
  6048. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6049. (org-overview)
  6050. (unless ga (org-unlogged-message "OVERVIEW"))
  6051. (setq org-cycle-global-status 'overview)
  6052. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6053. (defvar org-called-with-limited-levels nil
  6054. "Non-nil when `org-with-limited-levels' is currently active.")
  6055. (defun org-cycle-internal-local ()
  6056. "Do the local cycling action."
  6057. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6058. ;; First, determine end of headline (EOH), end of subtree or item
  6059. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6060. (save-excursion
  6061. (if (org-at-item-p)
  6062. (progn
  6063. (beginning-of-line)
  6064. (setq struct (org-list-struct))
  6065. (setq eoh (point-at-eol))
  6066. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6067. (setq has-children (org-list-has-child-p (point) struct)))
  6068. (org-back-to-heading)
  6069. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6070. (setq eos (save-excursion (org-end-of-subtree t t)
  6071. (when (bolp) (backward-char)) (point)))
  6072. (setq has-children
  6073. (or (save-excursion
  6074. (let ((level (funcall outline-level)))
  6075. (outline-next-heading)
  6076. (and (org-at-heading-p t)
  6077. (> (funcall outline-level) level))))
  6078. (save-excursion
  6079. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6080. ;; Determine end invisible part of buffer (EOL)
  6081. (beginning-of-line 2)
  6082. ;; XEmacs doesn't have `next-single-char-property-change'
  6083. (if (featurep 'xemacs)
  6084. (while (and (not (eobp)) ;; this is like `next-line'
  6085. (get-char-property (1- (point)) 'invisible))
  6086. (beginning-of-line 2))
  6087. (while (and (not (eobp)) ;; this is like `next-line'
  6088. (get-char-property (1- (point)) 'invisible))
  6089. (goto-char (next-single-char-property-change (point) 'invisible))
  6090. (and (eolp) (beginning-of-line 2))))
  6091. (setq eol (point)))
  6092. ;; Find out what to do next and set `this-command'
  6093. (cond
  6094. ((= eos eoh)
  6095. ;; Nothing is hidden behind this heading
  6096. (unless (org-before-first-heading-p)
  6097. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6098. (org-unlogged-message "EMPTY ENTRY")
  6099. (setq org-cycle-subtree-status nil)
  6100. (save-excursion
  6101. (goto-char eos)
  6102. (outline-next-heading)
  6103. (if (outline-invisible-p) (org-flag-heading nil))))
  6104. ((and (or (>= eol eos)
  6105. (not (string-match "\\S-" (buffer-substring eol eos))))
  6106. (or has-children
  6107. (not (setq children-skipped
  6108. org-cycle-skip-children-state-if-no-children))))
  6109. ;; Entire subtree is hidden in one line: children view
  6110. (unless (org-before-first-heading-p)
  6111. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6112. (if (org-at-item-p)
  6113. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6114. (org-show-entry)
  6115. (org-with-limited-levels (show-children))
  6116. ;; FIXME: This slows down the func way too much.
  6117. ;; How keep drawers hidden in subtree anyway?
  6118. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6119. ;; (org-cycle-hide-drawers 'subtree))
  6120. ;; Fold every list in subtree to top-level items.
  6121. (when (eq org-cycle-include-plain-lists 'integrate)
  6122. (save-excursion
  6123. (org-back-to-heading)
  6124. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6125. (beginning-of-line 1)
  6126. (let* ((struct (org-list-struct))
  6127. (prevs (org-list-prevs-alist struct))
  6128. (end (org-list-get-bottom-point struct)))
  6129. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6130. (org-list-get-all-items (point) struct prevs))
  6131. (goto-char (if (< end eos) end eos)))))))
  6132. (org-unlogged-message "CHILDREN")
  6133. (save-excursion
  6134. (goto-char eos)
  6135. (outline-next-heading)
  6136. (if (outline-invisible-p) (org-flag-heading nil)))
  6137. (setq org-cycle-subtree-status 'children)
  6138. (unless (org-before-first-heading-p)
  6139. (run-hook-with-args 'org-cycle-hook 'children)))
  6140. ((or children-skipped
  6141. (and (eq last-command this-command)
  6142. (eq org-cycle-subtree-status 'children)))
  6143. ;; We just showed the children, or no children are there,
  6144. ;; now show everything.
  6145. (unless (org-before-first-heading-p)
  6146. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6147. (outline-flag-region eoh eos nil)
  6148. (org-unlogged-message
  6149. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6150. (setq org-cycle-subtree-status 'subtree)
  6151. (unless (org-before-first-heading-p)
  6152. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6153. (t
  6154. ;; Default action: hide the subtree.
  6155. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6156. (outline-flag-region eoh eos t)
  6157. (org-unlogged-message "FOLDED")
  6158. (setq org-cycle-subtree-status 'folded)
  6159. (unless (org-before-first-heading-p)
  6160. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6161. ;;;###autoload
  6162. (defun org-global-cycle (&optional arg)
  6163. "Cycle the global visibility. For details see `org-cycle'.
  6164. With \\[universal-argument] prefix arg, switch to startup visibility.
  6165. With a numeric prefix, show all headlines up to that level."
  6166. (interactive "P")
  6167. (let ((org-cycle-include-plain-lists
  6168. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6169. (cond
  6170. ((integerp arg)
  6171. (show-all)
  6172. (hide-sublevels arg)
  6173. (setq org-cycle-global-status 'contents))
  6174. ((equal arg '(4))
  6175. (org-set-startup-visibility)
  6176. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6177. (t
  6178. (org-cycle '(4))))))
  6179. (defun org-set-startup-visibility ()
  6180. "Set the visibility required by startup options and properties."
  6181. (cond
  6182. ((eq org-startup-folded t)
  6183. (org-overview))
  6184. ((eq org-startup-folded 'content)
  6185. (org-content))
  6186. ((or (eq org-startup-folded 'showeverything)
  6187. (eq org-startup-folded nil))
  6188. (show-all)))
  6189. (unless (eq org-startup-folded 'showeverything)
  6190. (if org-hide-block-startup (org-hide-block-all))
  6191. (org-set-visibility-according-to-property 'no-cleanup)
  6192. (org-cycle-hide-archived-subtrees 'all)
  6193. (org-cycle-hide-drawers 'all)
  6194. (org-cycle-show-empty-lines t)))
  6195. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6196. "Switch subtree visibilities according to :VISIBILITY: property."
  6197. (interactive)
  6198. (let (org-show-entry-below)
  6199. (org-with-wide-buffer
  6200. (goto-char (point-min))
  6201. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6202. (if (not (org-at-property-p)) (outline-next-heading)
  6203. (let ((state (match-string 3)))
  6204. (save-excursion
  6205. (org-back-to-heading t)
  6206. (hide-subtree)
  6207. (org-reveal)
  6208. (cond
  6209. ((equal state "folded")
  6210. (hide-subtree))
  6211. ((equal state "children")
  6212. (org-show-hidden-entry)
  6213. (show-children))
  6214. ((equal state "content")
  6215. (save-excursion
  6216. (save-restriction
  6217. (org-narrow-to-subtree)
  6218. (org-content))))
  6219. ((member state '("all" "showall"))
  6220. (show-subtree)))))))
  6221. (unless no-cleanup
  6222. (org-cycle-hide-archived-subtrees 'all)
  6223. (org-cycle-hide-drawers 'all)
  6224. (org-cycle-show-empty-lines 'all)))))
  6225. ;; This function uses outline-regexp instead of the more fundamental
  6226. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6227. ;; buffers, where outline-regexp is needed.
  6228. (defun org-overview ()
  6229. "Switch to overview mode, showing only top-level headlines.
  6230. This shows all headlines with a level equal or greater than the level
  6231. of the first headline in the buffer. This is important, because if the
  6232. first headline is not level one, then (hide-sublevels 1) gives confusing
  6233. results."
  6234. (interactive)
  6235. (save-excursion
  6236. (let ((level
  6237. (save-excursion
  6238. (goto-char (point-min))
  6239. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6240. (progn
  6241. (goto-char (match-beginning 0))
  6242. (funcall outline-level))))))
  6243. (and level (hide-sublevels level)))))
  6244. (defun org-content (&optional arg)
  6245. "Show all headlines in the buffer, like a table of contents.
  6246. With numerical argument N, show content up to level N."
  6247. (interactive "P")
  6248. (org-overview)
  6249. (save-excursion
  6250. ;; Visit all headings and show their offspring
  6251. (and (integerp arg) (org-overview))
  6252. (goto-char (point-max))
  6253. (catch 'exit
  6254. (while (and (progn (condition-case nil
  6255. (outline-previous-visible-heading 1)
  6256. (error (goto-char (point-min))))
  6257. t)
  6258. (looking-at org-outline-regexp))
  6259. (if (integerp arg)
  6260. (show-children (1- arg))
  6261. (show-branches))
  6262. (if (bobp) (throw 'exit nil))))))
  6263. (defun org-optimize-window-after-visibility-change (state)
  6264. "Adjust the window after a change in outline visibility.
  6265. This function is the default value of the hook `org-cycle-hook'."
  6266. (when (get-buffer-window (current-buffer))
  6267. (cond
  6268. ((eq state 'content) nil)
  6269. ((eq state 'all) nil)
  6270. ((eq state 'folded) nil)
  6271. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6272. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6273. (defun org-remove-empty-overlays-at (pos)
  6274. "Remove outline overlays that do not contain non-white stuff."
  6275. (mapc
  6276. (lambda (o)
  6277. (and (eq 'outline (overlay-get o 'invisible))
  6278. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6279. (overlay-end o))))
  6280. (delete-overlay o)))
  6281. (overlays-at pos)))
  6282. (defun org-clean-visibility-after-subtree-move ()
  6283. "Fix visibility issues after moving a subtree."
  6284. ;; First, find a reasonable region to look at:
  6285. ;; Start two siblings above, end three below
  6286. (let* ((beg (save-excursion
  6287. (and (org-get-last-sibling)
  6288. (org-get-last-sibling))
  6289. (point)))
  6290. (end (save-excursion
  6291. (and (org-get-next-sibling)
  6292. (org-get-next-sibling)
  6293. (org-get-next-sibling))
  6294. (if (org-at-heading-p)
  6295. (point-at-eol)
  6296. (point))))
  6297. (level (looking-at "\\*+"))
  6298. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6299. (save-excursion
  6300. (save-restriction
  6301. (narrow-to-region beg end)
  6302. (when re
  6303. ;; Properly fold already folded siblings
  6304. (goto-char (point-min))
  6305. (while (re-search-forward re nil t)
  6306. (if (and (not (outline-invisible-p))
  6307. (save-excursion
  6308. (goto-char (point-at-eol)) (outline-invisible-p)))
  6309. (hide-entry))))
  6310. (org-cycle-show-empty-lines 'overview)
  6311. (org-cycle-hide-drawers 'overview)))))
  6312. (defun org-cycle-show-empty-lines (state)
  6313. "Show empty lines above all visible headlines.
  6314. The region to be covered depends on STATE when called through
  6315. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6316. entire buffer covered. Note that an empty line is only shown if there
  6317. are at least `org-cycle-separator-lines' empty lines before the headline."
  6318. (when (not (= org-cycle-separator-lines 0))
  6319. (save-excursion
  6320. (let* ((n (abs org-cycle-separator-lines))
  6321. (re (cond
  6322. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6323. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6324. (t (let ((ns (number-to-string (- n 2))))
  6325. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6326. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6327. beg end b e)
  6328. (cond
  6329. ((memq state '(overview contents t))
  6330. (setq beg (point-min) end (point-max)))
  6331. ((memq state '(children folded))
  6332. (setq beg (point) end (progn (org-end-of-subtree t t)
  6333. (beginning-of-line 2)
  6334. (point)))))
  6335. (when beg
  6336. (goto-char beg)
  6337. (while (re-search-forward re end t)
  6338. (unless (get-char-property (match-end 1) 'invisible)
  6339. (setq e (match-end 1))
  6340. (if (< org-cycle-separator-lines 0)
  6341. (setq b (save-excursion
  6342. (goto-char (match-beginning 0))
  6343. (org-back-over-empty-lines)
  6344. (if (save-excursion
  6345. (goto-char (max (point-min) (1- (point))))
  6346. (org-at-heading-p))
  6347. (1- (point))
  6348. (point))))
  6349. (setq b (match-beginning 1)))
  6350. (outline-flag-region b e nil)))))))
  6351. ;; Never hide empty lines at the end of the file.
  6352. (save-excursion
  6353. (goto-char (point-max))
  6354. (outline-previous-heading)
  6355. (outline-end-of-heading)
  6356. (if (and (looking-at "[ \t\n]+")
  6357. (= (match-end 0) (point-max)))
  6358. (outline-flag-region (point) (match-end 0) nil))))
  6359. (defun org-show-empty-lines-in-parent ()
  6360. "Move to the parent and re-show empty lines before visible headlines."
  6361. (save-excursion
  6362. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6363. (org-cycle-show-empty-lines context))))
  6364. (defun org-files-list ()
  6365. "Return `org-agenda-files' list, plus all open org-mode files.
  6366. This is useful for operations that need to scan all of a user's
  6367. open and agenda-wise Org files."
  6368. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6369. (dolist (buf (buffer-list))
  6370. (with-current-buffer buf
  6371. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6372. (let ((file (expand-file-name (buffer-file-name))))
  6373. (unless (member file files)
  6374. (push file files))))))
  6375. files))
  6376. (defsubst org-entry-beginning-position ()
  6377. "Return the beginning position of the current entry."
  6378. (save-excursion (outline-back-to-heading t) (point)))
  6379. (defsubst org-entry-end-position ()
  6380. "Return the end position of the current entry."
  6381. (save-excursion (outline-next-heading) (point)))
  6382. (defun org-cycle-hide-drawers (state &optional exceptions)
  6383. "Re-hide all drawers after a visibility state change.
  6384. When non-nil, optional argument EXCEPTIONS is a list of strings
  6385. specifying which drawers should not be hidden."
  6386. (when (and (derived-mode-p 'org-mode)
  6387. (not (memq state '(overview folded contents))))
  6388. (save-excursion
  6389. (let* ((globalp (memq state '(contents all)))
  6390. (beg (if globalp (point-min) (point)))
  6391. (end (if globalp (point-max)
  6392. (if (eq state 'children)
  6393. (save-excursion (outline-next-heading) (point))
  6394. (org-end-of-subtree t)))))
  6395. (goto-char beg)
  6396. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6397. (unless (member-ignore-case (match-string 1) exceptions)
  6398. (let ((drawer (org-element-at-point)))
  6399. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6400. (org-flag-drawer t drawer)
  6401. ;; Make sure to skip drawer entirely or we might flag
  6402. ;; it another time when matching its ending line with
  6403. ;; `org-drawer-regexp'.
  6404. (goto-char (org-element-property :end drawer))))))))))
  6405. (defun org-flag-drawer (flag &optional element)
  6406. "When FLAG is non-nil, hide the drawer we are at.
  6407. Otherwise make it visible. When optional argument ELEMENT is
  6408. a parsed drawer, as returned by `org-element-at-point', hide or
  6409. show that drawer instead."
  6410. (when (save-excursion
  6411. (beginning-of-line)
  6412. (org-looking-at-p org-drawer-regexp))
  6413. (let ((drawer (or element (org-element-at-point))))
  6414. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6415. (let ((post (org-element-property :post-affiliated drawer)))
  6416. (save-excursion
  6417. (outline-flag-region
  6418. (progn (goto-char post) (line-end-position))
  6419. (progn (goto-char (org-element-property :end drawer))
  6420. (skip-chars-backward " \r\t\n")
  6421. (line-end-position))
  6422. flag))
  6423. ;; When the drawer is hidden away, make sure point lies in
  6424. ;; a visible part of the buffer.
  6425. (when (and flag (> (line-beginning-position) post))
  6426. (goto-char post)))))))
  6427. (defun org-subtree-end-visible-p ()
  6428. "Is the end of the current subtree visible?"
  6429. (pos-visible-in-window-p
  6430. (save-excursion (org-end-of-subtree t) (point))))
  6431. (defun org-first-headline-recenter ()
  6432. "Move cursor to the first headline and recenter the headline."
  6433. (goto-char (point-min))
  6434. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6435. (set-window-start (selected-window) (point-at-bol))))
  6436. ;;; Saving and restoring visibility
  6437. (defun org-outline-overlay-data (&optional use-markers)
  6438. "Return a list of the locations of all outline overlays.
  6439. These are overlays with the `invisible' property value `outline'.
  6440. The return value is a list of cons cells, with start and stop
  6441. positions for each overlay.
  6442. If USE-MARKERS is set, return the positions as markers."
  6443. (let (beg end)
  6444. (save-excursion
  6445. (save-restriction
  6446. (widen)
  6447. (delq nil
  6448. (mapcar (lambda (o)
  6449. (when (eq (overlay-get o 'invisible) 'outline)
  6450. (setq beg (overlay-start o)
  6451. end (overlay-end o))
  6452. (and beg end (> end beg)
  6453. (if use-markers
  6454. (cons (copy-marker beg)
  6455. (copy-marker end t))
  6456. (cons beg end)))))
  6457. (overlays-in (point-min) (point-max))))))))
  6458. (defun org-set-outline-overlay-data (data)
  6459. "Create visibility overlays for all positions in DATA.
  6460. DATA should have been made by `org-outline-overlay-data'."
  6461. (let (o)
  6462. (save-excursion
  6463. (save-restriction
  6464. (widen)
  6465. (show-all)
  6466. (mapc (lambda (c)
  6467. (outline-flag-region (car c) (cdr c) t))
  6468. data)))))
  6469. ;;; Folding of blocks
  6470. (defvar org-hide-block-overlays nil
  6471. "Overlays hiding blocks.")
  6472. (make-variable-buffer-local 'org-hide-block-overlays)
  6473. (defun org-block-map (function &optional start end)
  6474. "Call FUNCTION at the head of all source blocks in the current buffer.
  6475. Optional arguments START and END can be used to limit the range."
  6476. (let ((start (or start (point-min)))
  6477. (end (or end (point-max))))
  6478. (save-excursion
  6479. (goto-char start)
  6480. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6481. (save-excursion
  6482. (save-match-data
  6483. (goto-char (match-beginning 0))
  6484. (funcall function)))))))
  6485. (defun org-hide-block-toggle-all ()
  6486. "Toggle the visibility of all blocks in the current buffer."
  6487. (org-block-map 'org-hide-block-toggle))
  6488. (defun org-hide-block-all ()
  6489. "Fold all blocks in the current buffer."
  6490. (interactive)
  6491. (org-show-block-all)
  6492. (org-block-map 'org-hide-block-toggle-maybe))
  6493. (defun org-show-block-all ()
  6494. "Unfold all blocks in the current buffer."
  6495. (interactive)
  6496. (mapc 'delete-overlay org-hide-block-overlays)
  6497. (setq org-hide-block-overlays nil))
  6498. (defun org-hide-block-toggle-maybe ()
  6499. "Toggle visibility of block at point.
  6500. Unlike to `org-hide-block-toggle', this function does not throw
  6501. an error. Return a non-nil value when toggling is successful."
  6502. (interactive)
  6503. (ignore-errors (org-hide-block-toggle)))
  6504. (defun org-hide-block-toggle (&optional force)
  6505. "Toggle the visibility of the current block.
  6506. When optional argument FORCE is `off', make block visible. If it
  6507. is non-nil, hide it unconditionally. Throw an error when not at
  6508. a block. Return a non-nil value when toggling is successful."
  6509. (interactive)
  6510. (let ((element (org-element-at-point)))
  6511. (unless (memq (org-element-type element)
  6512. '(center-block comment-block dynamic-block example-block
  6513. export-block quote-block special-block
  6514. src-block verse-block))
  6515. (user-error "Not at a block"))
  6516. (let* ((start (save-excursion
  6517. (goto-char (org-element-property :post-affiliated element))
  6518. (line-end-position)))
  6519. (end (save-excursion
  6520. (goto-char (org-element-property :end element))
  6521. (skip-chars-backward " \r\t\n")
  6522. (line-end-position)))
  6523. (overlays (overlays-at start)))
  6524. (cond
  6525. ;; Do nothing when not before or at the block opening line or
  6526. ;; at the block closing line.
  6527. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6528. ((and (not (eq force 'off))
  6529. (not (memq t (mapcar
  6530. (lambda (o)
  6531. (eq (overlay-get o 'invisible) 'org-hide-block))
  6532. overlays))))
  6533. (let ((ov (make-overlay start end)))
  6534. (overlay-put ov 'invisible 'org-hide-block)
  6535. ;; Make the block accessible to `isearch'.
  6536. (overlay-put
  6537. ov 'isearch-open-invisible
  6538. (lambda (ov)
  6539. (when (memq ov org-hide-block-overlays)
  6540. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6541. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6542. (delete-overlay ov))))
  6543. (push ov org-hide-block-overlays)
  6544. ;; When the block is hidden away, make sure point is left in
  6545. ;; a visible part of the buffer.
  6546. (when (> (line-beginning-position) start)
  6547. (goto-char start)
  6548. (beginning-of-line))
  6549. ;; Signal successful toggling.
  6550. t))
  6551. ((or (not force) (eq force 'off))
  6552. (dolist (ov overlays t)
  6553. (when (memq ov org-hide-block-overlays)
  6554. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6555. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6556. (delete-overlay ov))))))))
  6557. ;; org-tab-after-check-for-cycling-hook
  6558. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6559. ;; Remove overlays when changing major mode
  6560. (add-hook 'org-mode-hook
  6561. (lambda () (org-add-hook 'change-major-mode-hook
  6562. 'org-show-block-all 'append 'local)))
  6563. ;;; Org-goto
  6564. (defvar org-goto-window-configuration nil)
  6565. (defvar org-goto-marker nil)
  6566. (defvar org-goto-map)
  6567. (defun org-goto-map ()
  6568. "Set the keymap `org-goto'."
  6569. (setq org-goto-map
  6570. (let ((map (make-sparse-keymap)))
  6571. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6572. mouse-drag-region universal-argument org-occur))
  6573. cmd)
  6574. (while (setq cmd (pop cmds))
  6575. (substitute-key-definition cmd cmd map global-map)))
  6576. (suppress-keymap map)
  6577. (org-defkey map "\C-m" 'org-goto-ret)
  6578. (org-defkey map [(return)] 'org-goto-ret)
  6579. (org-defkey map [(left)] 'org-goto-left)
  6580. (org-defkey map [(right)] 'org-goto-right)
  6581. (org-defkey map [(control ?g)] 'org-goto-quit)
  6582. (org-defkey map "\C-i" 'org-cycle)
  6583. (org-defkey map [(tab)] 'org-cycle)
  6584. (org-defkey map [(down)] 'outline-next-visible-heading)
  6585. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6586. (if org-goto-auto-isearch
  6587. (if (fboundp 'define-key-after)
  6588. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6589. nil)
  6590. (org-defkey map "q" 'org-goto-quit)
  6591. (org-defkey map "n" 'outline-next-visible-heading)
  6592. (org-defkey map "p" 'outline-previous-visible-heading)
  6593. (org-defkey map "f" 'outline-forward-same-level)
  6594. (org-defkey map "b" 'outline-backward-same-level)
  6595. (org-defkey map "u" 'outline-up-heading))
  6596. (org-defkey map "/" 'org-occur)
  6597. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6598. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6599. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6600. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6601. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6602. map)))
  6603. (defconst org-goto-help
  6604. "Browse buffer copy, to find location or copy text.%s
  6605. RET=jump to location C-g=quit and return to previous location
  6606. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6607. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6608. (defun org-goto (&optional alternative-interface)
  6609. "Look up a different location in the current file, keeping current visibility.
  6610. When you want look-up or go to a different location in a
  6611. document, the fastest way is often to fold the entire buffer and
  6612. then dive into the tree. This method has the disadvantage, that
  6613. the previous location will be folded, which may not be what you
  6614. want.
  6615. This command works around this by showing a copy of the current
  6616. buffer in an indirect buffer, in overview mode. You can dive
  6617. into the tree in that copy, use org-occur and incremental search
  6618. to find a location. When pressing RET or `Q', the command
  6619. returns to the original buffer in which the visibility is still
  6620. unchanged. After RET it will also jump to the location selected
  6621. in the indirect buffer and expose the headline hierarchy above.
  6622. With a prefix argument, use the alternative interface: e.g. if
  6623. `org-goto-interface' is 'outline use 'outline-path-completion."
  6624. (interactive "P")
  6625. (org-goto-map)
  6626. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6627. (org-refile-use-outline-path t)
  6628. (org-refile-target-verify-function nil)
  6629. (interface
  6630. (if (not alternative-interface)
  6631. org-goto-interface
  6632. (if (eq org-goto-interface 'outline)
  6633. 'outline-path-completion
  6634. 'outline)))
  6635. (org-goto-start-pos (point))
  6636. (selected-point
  6637. (if (eq interface 'outline)
  6638. (car (org-get-location (current-buffer) org-goto-help))
  6639. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6640. (org-refile-check-position pa)
  6641. (nth 3 pa)))))
  6642. (if selected-point
  6643. (progn
  6644. (org-mark-ring-push org-goto-start-pos)
  6645. (goto-char selected-point)
  6646. (if (or (outline-invisible-p) (org-invisible-p2))
  6647. (org-show-context 'org-goto)))
  6648. (message "Quit"))))
  6649. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6650. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6651. (defvar org-goto-local-auto-isearch-map) ; defined below
  6652. (defun org-get-location (buf help)
  6653. "Let the user select a location in the Org-mode buffer BUF.
  6654. This function uses a recursive edit. It returns the selected position
  6655. or nil."
  6656. (org-no-popups
  6657. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6658. (isearch-hide-immediately nil)
  6659. (isearch-search-fun-function
  6660. (lambda () 'org-goto-local-search-headings))
  6661. (org-goto-selected-point org-goto-exit-command))
  6662. (save-excursion
  6663. (save-window-excursion
  6664. (delete-other-windows)
  6665. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6666. (org-pop-to-buffer-same-window
  6667. (condition-case nil
  6668. (make-indirect-buffer (current-buffer) "*org-goto*")
  6669. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6670. (with-output-to-temp-buffer "*Org Help*"
  6671. (princ (format help (if org-goto-auto-isearch
  6672. " Just type for auto-isearch."
  6673. " n/p/f/b/u to navigate, q to quit."))))
  6674. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6675. (setq buffer-read-only nil)
  6676. (let ((org-startup-truncated t)
  6677. (org-startup-folded nil)
  6678. (org-startup-align-all-tables nil))
  6679. (org-mode)
  6680. (org-overview))
  6681. (setq buffer-read-only t)
  6682. (if (and (boundp 'org-goto-start-pos)
  6683. (integer-or-marker-p org-goto-start-pos))
  6684. (progn (goto-char org-goto-start-pos)
  6685. (when (outline-invisible-p)
  6686. (org-show-set-visibility 'lineage)))
  6687. (goto-char (point-min)))
  6688. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6689. (message "Select location and press RET")
  6690. (use-local-map org-goto-map)
  6691. (recursive-edit)))
  6692. (kill-buffer "*org-goto*")
  6693. (cons org-goto-selected-point org-goto-exit-command))))
  6694. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6695. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6696. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6697. (if (fboundp 'isearch-other-control-char)
  6698. (progn
  6699. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6700. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6701. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6702. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6703. (define-key org-goto-local-auto-isearch-map [return] nil))
  6704. (defun org-goto-local-search-headings (string bound noerror)
  6705. "Search and make sure that any matches are in headlines."
  6706. (catch 'return
  6707. (while (if isearch-forward
  6708. (search-forward string bound noerror)
  6709. (search-backward string bound noerror))
  6710. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6711. (and (member :headline context)
  6712. (not (member :tags context))))
  6713. (throw 'return (point))))))
  6714. (defun org-goto-local-auto-isearch ()
  6715. "Start isearch."
  6716. (interactive)
  6717. (goto-char (point-min))
  6718. (let ((keys (this-command-keys)))
  6719. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6720. (isearch-mode t)
  6721. (isearch-process-search-char (string-to-char keys)))))
  6722. (defun org-goto-ret (&optional arg)
  6723. "Finish `org-goto' by going to the new location."
  6724. (interactive "P")
  6725. (setq org-goto-selected-point (point)
  6726. org-goto-exit-command 'return)
  6727. (throw 'exit nil))
  6728. (defun org-goto-left ()
  6729. "Finish `org-goto' by going to the new location."
  6730. (interactive)
  6731. (if (org-at-heading-p)
  6732. (progn
  6733. (beginning-of-line 1)
  6734. (setq org-goto-selected-point (point)
  6735. org-goto-exit-command 'left)
  6736. (throw 'exit nil))
  6737. (user-error "Not on a heading")))
  6738. (defun org-goto-right ()
  6739. "Finish `org-goto' by going to the new location."
  6740. (interactive)
  6741. (if (org-at-heading-p)
  6742. (progn
  6743. (setq org-goto-selected-point (point)
  6744. org-goto-exit-command 'right)
  6745. (throw 'exit nil))
  6746. (user-error "Not on a heading")))
  6747. (defun org-goto-quit ()
  6748. "Finish `org-goto' without cursor motion."
  6749. (interactive)
  6750. (setq org-goto-selected-point nil)
  6751. (setq org-goto-exit-command 'quit)
  6752. (throw 'exit nil))
  6753. ;;; Indirect buffer display of subtrees
  6754. (defvar org-indirect-dedicated-frame nil
  6755. "This is the frame being used for indirect tree display.")
  6756. (defvar org-last-indirect-buffer nil)
  6757. (defun org-tree-to-indirect-buffer (&optional arg)
  6758. "Create indirect buffer and narrow it to current subtree.
  6759. With a numerical prefix ARG, go up to this level and then take that tree.
  6760. If ARG is negative, go up that many levels.
  6761. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6762. indirect buffer previously made with this command, to avoid proliferation of
  6763. indirect buffers. However, when you call the command with a \
  6764. \\[universal-argument] prefix, or
  6765. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6766. is kept so that you can work with several indirect buffers at the same time.
  6767. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6768. \\[universal-argument] prefix also
  6769. requests that a new frame be made for the new buffer, so that the dedicated
  6770. frame is not changed."
  6771. (interactive "P")
  6772. (let ((cbuf (current-buffer))
  6773. (cwin (selected-window))
  6774. (pos (point))
  6775. beg end level heading ibuf)
  6776. (save-excursion
  6777. (org-back-to-heading t)
  6778. (when (numberp arg)
  6779. (setq level (org-outline-level))
  6780. (if (< arg 0) (setq arg (+ level arg)))
  6781. (while (> (setq level (org-outline-level)) arg)
  6782. (org-up-heading-safe)))
  6783. (setq beg (point)
  6784. heading (org-get-heading))
  6785. (org-end-of-subtree t t)
  6786. (if (org-at-heading-p) (backward-char 1))
  6787. (setq end (point)))
  6788. (if (and (buffer-live-p org-last-indirect-buffer)
  6789. (not (eq org-indirect-buffer-display 'new-frame))
  6790. (not arg))
  6791. (kill-buffer org-last-indirect-buffer))
  6792. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6793. org-last-indirect-buffer ibuf)
  6794. (cond
  6795. ((or (eq org-indirect-buffer-display 'new-frame)
  6796. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6797. (select-frame (make-frame))
  6798. (delete-other-windows)
  6799. (org-pop-to-buffer-same-window ibuf)
  6800. (org-set-frame-title heading))
  6801. ((eq org-indirect-buffer-display 'dedicated-frame)
  6802. (raise-frame
  6803. (select-frame (or (and org-indirect-dedicated-frame
  6804. (frame-live-p org-indirect-dedicated-frame)
  6805. org-indirect-dedicated-frame)
  6806. (setq org-indirect-dedicated-frame (make-frame)))))
  6807. (delete-other-windows)
  6808. (org-pop-to-buffer-same-window ibuf)
  6809. (org-set-frame-title (concat "Indirect: " heading)))
  6810. ((eq org-indirect-buffer-display 'current-window)
  6811. (org-pop-to-buffer-same-window ibuf))
  6812. ((eq org-indirect-buffer-display 'other-window)
  6813. (pop-to-buffer ibuf))
  6814. (t (error "Invalid value")))
  6815. (if (featurep 'xemacs)
  6816. (save-excursion (org-mode) (turn-on-font-lock)))
  6817. (narrow-to-region beg end)
  6818. (show-all)
  6819. (goto-char pos)
  6820. (run-hook-with-args 'org-cycle-hook 'all)
  6821. (and (window-live-p cwin) (select-window cwin))))
  6822. (defun org-get-indirect-buffer (&optional buffer heading)
  6823. (setq buffer (or buffer (current-buffer)))
  6824. (let ((n 1) (base (buffer-name buffer)) bname)
  6825. (while (buffer-live-p
  6826. (get-buffer
  6827. (setq bname
  6828. (concat base "-"
  6829. (if heading (concat heading "-" (number-to-string n))
  6830. (number-to-string n))))))
  6831. (setq n (1+ n)))
  6832. (condition-case nil
  6833. (make-indirect-buffer buffer bname 'clone)
  6834. (error (make-indirect-buffer buffer bname)))))
  6835. (defun org-set-frame-title (title)
  6836. "Set the title of the current frame to the string TITLE."
  6837. ;; FIXME: how to name a single frame in XEmacs???
  6838. (unless (featurep 'xemacs)
  6839. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6840. ;;;; Structure editing
  6841. ;;; Inserting headlines
  6842. (defun org-previous-line-empty-p (&optional next)
  6843. "Is the previous line a blank line?
  6844. When NEXT is non-nil, check the next line instead."
  6845. (save-excursion
  6846. (and (not (bobp))
  6847. (or (beginning-of-line (if next 2 0)) t)
  6848. (save-match-data
  6849. (looking-at "[ \t]*$")))))
  6850. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6851. "Insert a new heading or an item with the same depth at point.
  6852. If point is at the beginning of a heading or a list item, insert
  6853. a new heading or a new item above the current one. If point is
  6854. at the beginning of a normal line, turn the line into a heading.
  6855. If point is in the middle of a headline or a list item, split the
  6856. headline or the item and create a new headline/item with the text
  6857. in the current line after point \(see `org-M-RET-may-split-line'
  6858. on how to modify this behavior).
  6859. With one universal prefix argument, set the user option
  6860. `org-insert-heading-respect-content' to t for the duration of
  6861. the command. This modifies the behavior described above in this
  6862. ways: on list items and at the beginning of normal lines, force
  6863. the insertion of a heading after the current subtree.
  6864. With two universal prefix arguments, insert the heading at the
  6865. end of the grandparent subtree. For example, if point is within
  6866. a 2nd-level heading, then it will insert a 2nd-level heading at
  6867. the end of the 1st-level parent heading.
  6868. If point is at the beginning of a headline, insert a sibling
  6869. before the current headline. If point is not at the beginning,
  6870. split the line and create a new headline with the text in the
  6871. current line after point \(see `org-M-RET-may-split-line' on how
  6872. to modify this behavior).
  6873. If point is at the beginning of a normal line, turn this line
  6874. into a heading.
  6875. When INVISIBLE-OK is set, stop at invisible headlines when going
  6876. back. This is important for non-interactive uses of the
  6877. command.
  6878. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6879. heading, unconditionally."
  6880. (interactive "P")
  6881. (if (org-called-interactively-p 'any) (org-reveal))
  6882. (let ((itemp (and (not top-level) (org-in-item-p)))
  6883. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6884. (respect-content (or org-insert-heading-respect-content
  6885. (equal arg '(4))))
  6886. (initial-content ""))
  6887. (cond
  6888. ((or (= (buffer-size) 0)
  6889. (and (not (save-excursion
  6890. (and (ignore-errors (org-back-to-heading invisible-ok))
  6891. (org-at-heading-p))))
  6892. (or arg (not itemp))))
  6893. ;; At beginning of buffer or so high up that only a heading
  6894. ;; makes sense.
  6895. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6896. ((not respect-content)
  6897. (unless may-split (end-of-line))
  6898. (insert "\n* "))
  6899. ((re-search-forward org-outline-regexp-bol nil t)
  6900. (beginning-of-line)
  6901. (insert "* \n")
  6902. (backward-char))
  6903. (t (goto-char (point-max))
  6904. (insert "\n* ")))
  6905. (run-hooks 'org-insert-heading-hook))
  6906. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6907. (t
  6908. ;; Maybe move at the end of the subtree
  6909. (when (equal arg '(16))
  6910. (org-up-heading-safe)
  6911. (org-end-of-subtree t))
  6912. ;; Insert a heading
  6913. (save-restriction
  6914. (widen)
  6915. (let* ((level nil)
  6916. (on-heading (org-at-heading-p))
  6917. (empty-line-p (if on-heading
  6918. (org-previous-line-empty-p)
  6919. ;; We will decide later
  6920. nil))
  6921. ;; Get a level string to fall back on.
  6922. (fix-level
  6923. (if (org-before-first-heading-p) "*"
  6924. (save-excursion
  6925. (org-back-to-heading t)
  6926. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6927. (looking-at org-outline-regexp)
  6928. (make-string (1- (length (match-string 0))) ?*))))
  6929. (stars
  6930. (save-excursion
  6931. (condition-case nil
  6932. (if top-level "* "
  6933. (org-back-to-heading invisible-ok)
  6934. (when (and (not on-heading)
  6935. (featurep 'org-inlinetask)
  6936. (integerp org-inlinetask-min-level)
  6937. (>= (length (match-string 0))
  6938. org-inlinetask-min-level))
  6939. ;; Find a heading level before the inline
  6940. ;; task.
  6941. (while (and (setq level (org-up-heading-safe))
  6942. (>= level org-inlinetask-min-level)))
  6943. (if (org-at-heading-p)
  6944. (org-back-to-heading invisible-ok)
  6945. (error "This should not happen")))
  6946. (unless (and (save-excursion
  6947. (save-match-data
  6948. (org-backward-heading-same-level
  6949. 1 invisible-ok))
  6950. (= (point) (match-beginning 0)))
  6951. (not (org-previous-line-empty-p t)))
  6952. (setq empty-line-p (or empty-line-p
  6953. (org-previous-line-empty-p))))
  6954. (match-string 0))
  6955. (error (or fix-level "* ")))))
  6956. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6957. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6958. pos hide-previous previous-pos)
  6959. ;; If we insert after content, move there and clean up
  6960. ;; whitespace.
  6961. (when (and respect-content
  6962. (not (org-looking-at-p org-outline-regexp-bol)))
  6963. (if (not (org-before-first-heading-p))
  6964. (org-end-of-subtree nil t)
  6965. (re-search-forward org-outline-regexp-bol)
  6966. (beginning-of-line 0))
  6967. (skip-chars-backward " \r\t\n")
  6968. (and (not (looking-back "^\\*+"))
  6969. (looking-at "[ \t]+") (replace-match ""))
  6970. (unless (eobp) (forward-char 1))
  6971. (when (looking-at "^\\*")
  6972. (unless (bobp) (backward-char 1))
  6973. (insert "\n")))
  6974. ;; If we are splitting, grab the text that should be moved
  6975. ;; to the new headline.
  6976. (when may-split
  6977. (if (org-on-heading-p)
  6978. ;; This is a heading: split intelligently (keeping
  6979. ;; tags).
  6980. (let ((pos (point)))
  6981. (beginning-of-line)
  6982. (unless (looking-at org-complex-heading-regexp)
  6983. (error "This should not happen"))
  6984. (when (and (match-beginning 4)
  6985. (> pos (match-beginning 4))
  6986. (< pos (match-end 4)))
  6987. (setq initial-content (buffer-substring pos (match-end 4)))
  6988. (goto-char pos)
  6989. (delete-region (point) (match-end 4))
  6990. (if (looking-at "[ \t]*$")
  6991. (replace-match "")
  6992. (insert (make-string (length initial-content) ?\s)))
  6993. (setq initial-content (org-trim initial-content)))
  6994. (goto-char pos))
  6995. ;; A normal line.
  6996. (setq initial-content
  6997. (org-trim
  6998. (delete-and-extract-region (point) (line-end-position))))))
  6999. ;; If we are at the beginning of the line, insert before it.
  7000. ;; Otherwise, after it.
  7001. (cond
  7002. ((and (bolp) (looking-at "[ \t]*$")))
  7003. ((bolp) (save-excursion (insert "\n")))
  7004. (t (end-of-line)
  7005. (insert "\n")))
  7006. ;; Insert the new heading
  7007. (insert stars)
  7008. (just-one-space)
  7009. (insert initial-content)
  7010. (unless (and blank (org-previous-line-empty-p))
  7011. (org-N-empty-lines-before-current (if blank 1 0)))
  7012. ;; Adjust visibility, which may be messed up if we removed
  7013. ;; blank lines while previous entry was hidden.
  7014. (let ((bol (line-beginning-position)))
  7015. (dolist (o (overlays-at (1- bol)))
  7016. (when (and (eq (overlay-get o 'invisible) 'outline)
  7017. (eq (overlay-end o) bol))
  7018. (move-overlay o (overlay-start o) (1- bol)))))
  7019. (run-hooks 'org-insert-heading-hook)))))))
  7020. (defun org-N-empty-lines-before-current (N)
  7021. "Make the number of empty lines before current exactly N.
  7022. So this will delete or add empty lines."
  7023. (save-excursion
  7024. (beginning-of-line)
  7025. (let ((p (point)))
  7026. (skip-chars-backward " \r\t\n")
  7027. (unless (bolp) (forward-line))
  7028. (delete-region (point) p))
  7029. (when (> N 0) (insert (make-string N ?\n)))))
  7030. (defun org-get-heading (&optional no-tags no-todo)
  7031. "Return the heading of the current entry, without the stars.
  7032. When NO-TAGS is non-nil, don't include tags.
  7033. When NO-TODO is non-nil, don't include TODO keywords."
  7034. (save-excursion
  7035. (org-back-to-heading t)
  7036. (cond
  7037. ((and no-tags no-todo)
  7038. (looking-at org-complex-heading-regexp)
  7039. (match-string 4))
  7040. (no-tags
  7041. (looking-at (concat org-outline-regexp
  7042. "\\(.*?\\)"
  7043. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7044. (match-string 1))
  7045. (no-todo
  7046. (looking-at org-todo-line-regexp)
  7047. (match-string 3))
  7048. (t (looking-at org-heading-regexp)
  7049. (match-string 2)))))
  7050. (defvar orgstruct-mode) ; defined below
  7051. (defun org-heading-components ()
  7052. "Return the components of the current heading.
  7053. This is a list with the following elements:
  7054. - the level as an integer
  7055. - the reduced level, different if `org-odd-levels-only' is set.
  7056. - the TODO keyword, or nil
  7057. - the priority character, like ?A, or nil if no priority is given
  7058. - the headline text itself, or the tags string if no headline text
  7059. - the tags string, or nil."
  7060. (save-excursion
  7061. (org-back-to-heading t)
  7062. (if (let (case-fold-search)
  7063. (looking-at
  7064. (if orgstruct-mode
  7065. org-heading-regexp
  7066. org-complex-heading-regexp)))
  7067. (if orgstruct-mode
  7068. (list (length (match-string 1))
  7069. (org-reduced-level (length (match-string 1)))
  7070. nil
  7071. nil
  7072. (match-string 2)
  7073. nil)
  7074. (list (length (match-string 1))
  7075. (org-reduced-level (length (match-string 1)))
  7076. (org-match-string-no-properties 2)
  7077. (and (match-end 3) (aref (match-string 3) 2))
  7078. (org-match-string-no-properties 4)
  7079. (org-match-string-no-properties 5))))))
  7080. (defun org-get-entry ()
  7081. "Get the entry text, after heading, entire subtree."
  7082. (save-excursion
  7083. (org-back-to-heading t)
  7084. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7085. (defun org-insert-heading-after-current ()
  7086. "Insert a new heading with same level as current, after current subtree."
  7087. (interactive)
  7088. (org-back-to-heading)
  7089. (org-insert-heading)
  7090. (org-move-subtree-down)
  7091. (end-of-line 1))
  7092. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7093. "Insert heading with `org-insert-heading-respect-content' set to t."
  7094. (interactive)
  7095. (org-insert-heading '(4) invisible-ok))
  7096. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7097. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7098. (interactive)
  7099. (org-insert-todo-heading force-state '(4)))
  7100. (defun org-insert-todo-heading (arg &optional force-heading)
  7101. "Insert a new heading with the same level and TODO state as current heading.
  7102. If the heading has no TODO state, or if the state is DONE, use the first
  7103. state (TODO by default). Also with one prefix arg, force first state. With
  7104. two prefix args, force inserting at the end of the parent subtree."
  7105. (interactive "P")
  7106. (when (or force-heading (not (org-insert-item 'checkbox)))
  7107. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7108. force-heading))
  7109. (save-excursion
  7110. (org-back-to-heading)
  7111. (outline-previous-heading)
  7112. (looking-at org-todo-line-regexp))
  7113. (let*
  7114. ((new-mark-x
  7115. (if (or (equal arg '(4))
  7116. (not (match-beginning 2))
  7117. (member (match-string 2) org-done-keywords))
  7118. (car org-todo-keywords-1)
  7119. (match-string 2)))
  7120. (new-mark
  7121. (or
  7122. (run-hook-with-args-until-success
  7123. 'org-todo-get-default-hook new-mark-x nil)
  7124. new-mark-x)))
  7125. (beginning-of-line 1)
  7126. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7127. (if org-treat-insert-todo-heading-as-state-change
  7128. (org-todo new-mark)
  7129. (insert new-mark " "))))
  7130. (when org-provide-todo-statistics
  7131. (org-update-parent-todo-statistics))))
  7132. (defun org-insert-subheading (arg)
  7133. "Insert a new subheading and demote it.
  7134. Works for outline headings and for plain lists alike."
  7135. (interactive "P")
  7136. (org-insert-heading arg)
  7137. (cond
  7138. ((org-at-heading-p) (org-do-demote))
  7139. ((org-at-item-p) (org-indent-item))))
  7140. (defun org-insert-todo-subheading (arg)
  7141. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7142. Works for outline headings and for plain lists alike."
  7143. (interactive "P")
  7144. (org-insert-todo-heading arg)
  7145. (cond
  7146. ((org-at-heading-p) (org-do-demote))
  7147. ((org-at-item-p) (org-indent-item))))
  7148. ;;; Promotion and Demotion
  7149. (defvar org-after-demote-entry-hook nil
  7150. "Hook run after an entry has been demoted.
  7151. The cursor will be at the beginning of the entry.
  7152. When a subtree is being demoted, the hook will be called for each node.")
  7153. (defvar org-after-promote-entry-hook nil
  7154. "Hook run after an entry has been promoted.
  7155. The cursor will be at the beginning of the entry.
  7156. When a subtree is being promoted, the hook will be called for each node.")
  7157. (defun org-promote-subtree ()
  7158. "Promote the entire subtree.
  7159. See also `org-promote'."
  7160. (interactive)
  7161. (save-excursion
  7162. (org-with-limited-levels (org-map-tree 'org-promote)))
  7163. (org-fix-position-after-promote))
  7164. (defun org-demote-subtree ()
  7165. "Demote the entire subtree. See `org-demote'.
  7166. See also `org-promote'."
  7167. (interactive)
  7168. (save-excursion
  7169. (org-with-limited-levels (org-map-tree 'org-demote)))
  7170. (org-fix-position-after-promote))
  7171. (defun org-do-promote ()
  7172. "Promote the current heading higher up the tree.
  7173. If the region is active in `transient-mark-mode', promote all headings
  7174. in the region."
  7175. (interactive)
  7176. (save-excursion
  7177. (if (org-region-active-p)
  7178. (org-map-region 'org-promote (region-beginning) (region-end))
  7179. (org-promote)))
  7180. (org-fix-position-after-promote))
  7181. (defun org-do-demote ()
  7182. "Demote the current heading lower down the tree.
  7183. If the region is active in `transient-mark-mode', demote all headings
  7184. in the region."
  7185. (interactive)
  7186. (save-excursion
  7187. (if (org-region-active-p)
  7188. (org-map-region 'org-demote (region-beginning) (region-end))
  7189. (org-demote)))
  7190. (org-fix-position-after-promote))
  7191. (defun org-fix-position-after-promote ()
  7192. "Make sure that after pro/demotion cursor position is right."
  7193. (let ((pos (point)))
  7194. (when (save-excursion
  7195. (beginning-of-line 1)
  7196. (looking-at org-todo-line-regexp)
  7197. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7198. (cond ((eobp) (insert " "))
  7199. ((eolp) (insert " "))
  7200. ((equal (char-after) ?\ ) (forward-char 1))))))
  7201. (defun org-current-level ()
  7202. "Return the level of the current entry, or nil if before the first headline.
  7203. The level is the number of stars at the beginning of the
  7204. headline. Use `org-reduced-level' to remove the effect of
  7205. `org-odd-levels'. Unlike to `org-outline-level', this function
  7206. ignores inlinetasks."
  7207. (let ((level (org-with-limited-levels (org-outline-level))))
  7208. (and (> level 0) level)))
  7209. (defun org-get-previous-line-level ()
  7210. "Return the outline depth of the last headline before the current line.
  7211. Returns 0 for the first headline in the buffer, and nil if before the
  7212. first headline."
  7213. (and (org-current-level)
  7214. (or (and (/= (line-beginning-position) (point-min))
  7215. (save-excursion (beginning-of-line 0) (org-current-level)))
  7216. 0)))
  7217. (defun org-reduced-level (l)
  7218. "Compute the effective level of a heading.
  7219. This takes into account the setting of `org-odd-levels-only'."
  7220. (cond
  7221. ((zerop l) 0)
  7222. (org-odd-levels-only (1+ (floor (/ l 2))))
  7223. (t l)))
  7224. (defun org-level-increment ()
  7225. "Return the number of stars that will be added or removed at a
  7226. time to headlines when structure editing, based on the value of
  7227. `org-odd-levels-only'."
  7228. (if org-odd-levels-only 2 1))
  7229. (defun org-get-valid-level (level &optional change)
  7230. "Rectify a level change under the influence of `org-odd-levels-only'
  7231. LEVEL is a current level, CHANGE is by how much the level should be
  7232. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7233. even level numbers will become the next higher odd number."
  7234. (if org-odd-levels-only
  7235. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7236. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7237. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7238. (max 1 (+ level (or change 0)))))
  7239. (if (boundp 'define-obsolete-function-alias)
  7240. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7241. (define-obsolete-function-alias 'org-get-legal-level
  7242. 'org-get-valid-level)
  7243. (define-obsolete-function-alias 'org-get-legal-level
  7244. 'org-get-valid-level "23.1")))
  7245. (defun org-promote ()
  7246. "Promote the current heading higher up the tree."
  7247. (org-with-wide-buffer
  7248. (org-back-to-heading t)
  7249. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7250. after-change-functions))
  7251. (level (save-match-data (funcall outline-level)))
  7252. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7253. (diff (abs (- level (length up-head) -1))))
  7254. (cond
  7255. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7256. (replace-match "# " nil t))
  7257. ((= level 1)
  7258. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7259. (t (replace-match up-head nil t)))
  7260. (unless (= level 1)
  7261. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7262. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7263. (run-hooks 'org-after-promote-entry-hook))))
  7264. (defun org-demote ()
  7265. "Demote the current heading lower down the tree."
  7266. (org-with-wide-buffer
  7267. (org-back-to-heading t)
  7268. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7269. after-change-functions))
  7270. (level (save-match-data (funcall outline-level)))
  7271. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7272. (diff (abs (- level (length down-head) -1))))
  7273. (replace-match down-head nil t)
  7274. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7275. (when org-adapt-indentation (org-fixup-indentation diff))
  7276. (run-hooks 'org-after-demote-entry-hook))))
  7277. (defun org-cycle-level ()
  7278. "Cycle the level of an empty headline through possible states.
  7279. This goes first to child, then to parent, level, then up the hierarchy.
  7280. After top level, it switches back to sibling level."
  7281. (interactive)
  7282. (let ((org-adapt-indentation nil))
  7283. (when (org-point-at-end-of-empty-headline)
  7284. (setq this-command 'org-cycle-level) ; Only needed for caching
  7285. (let ((cur-level (org-current-level))
  7286. (prev-level (org-get-previous-line-level)))
  7287. (cond
  7288. ;; If first headline in file, promote to top-level.
  7289. ((= prev-level 0)
  7290. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7291. do (org-do-promote)))
  7292. ;; If same level as prev, demote one.
  7293. ((= prev-level cur-level)
  7294. (org-do-demote))
  7295. ;; If parent is top-level, promote to top level if not already.
  7296. ((= prev-level 1)
  7297. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7298. do (org-do-promote)))
  7299. ;; If top-level, return to prev-level.
  7300. ((= cur-level 1)
  7301. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7302. do (org-do-demote)))
  7303. ;; If less than prev-level, promote one.
  7304. ((< cur-level prev-level)
  7305. (org-do-promote))
  7306. ;; If deeper than prev-level, promote until higher than
  7307. ;; prev-level.
  7308. ((> cur-level prev-level)
  7309. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7310. do (org-do-promote))))
  7311. t))))
  7312. (defun org-map-tree (fun)
  7313. "Call FUN for every heading underneath the current one."
  7314. (org-back-to-heading)
  7315. (let ((level (funcall outline-level)))
  7316. (save-excursion
  7317. (funcall fun)
  7318. (while (and (progn
  7319. (outline-next-heading)
  7320. (> (funcall outline-level) level))
  7321. (not (eobp)))
  7322. (funcall fun)))))
  7323. (defun org-map-region (fun beg end)
  7324. "Call FUN for every heading between BEG and END."
  7325. (let ((org-ignore-region t))
  7326. (save-excursion
  7327. (setq end (copy-marker end))
  7328. (goto-char beg)
  7329. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7330. (< (point) end))
  7331. (funcall fun))
  7332. (while (and (progn
  7333. (outline-next-heading)
  7334. (< (point) end))
  7335. (not (eobp)))
  7336. (funcall fun)))))
  7337. (defun org-fixup-indentation (diff)
  7338. "Change the indentation in the current entry by DIFF.
  7339. DIFF is an integer. Indentation is done according to the
  7340. following rules:
  7341. - Planning information and property drawers are always indented
  7342. according to the new level of the headline;
  7343. - Footnote definitions and their contents are ignored;
  7344. - Inlinetasks' boundaries are not shifted;
  7345. - Empty lines are ignored;
  7346. - Other lines' indentation are shifted by DIFF columns, unless
  7347. it would introduce a structural change in the document, in
  7348. which case no shifting is done at all.
  7349. Assume point is at a heading or an inlinetask beginning."
  7350. (org-with-wide-buffer
  7351. (narrow-to-region (line-beginning-position)
  7352. (save-excursion
  7353. (if (org-with-limited-levels (org-at-heading-p))
  7354. (org-with-limited-levels (outline-next-heading))
  7355. (org-inlinetask-goto-end))
  7356. (point)))
  7357. (forward-line)
  7358. ;; Indent properly planning info and property drawer.
  7359. (when (org-looking-at-p org-planning-line-re)
  7360. (org-indent-line)
  7361. (forward-line))
  7362. (when (looking-at org-property-drawer-re)
  7363. (goto-char (match-end 0))
  7364. (forward-line)
  7365. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7366. (catch 'no-shift
  7367. (when (zerop diff) (throw 'no-shift nil))
  7368. ;; If DIFF is negative, first check if a shift is possible at all
  7369. ;; (e.g., it doesn't break structure). This can only happen if
  7370. ;; some contents are not properly indented.
  7371. (let ((case-fold-search t))
  7372. (when (< diff 0)
  7373. (let ((diff (- diff))
  7374. (forbidden-re (concat org-outline-regexp
  7375. "\\|"
  7376. (substring org-footnote-definition-re 1))))
  7377. (save-excursion
  7378. (while (not (eobp))
  7379. (cond
  7380. ((org-looking-at-p "[ \t]*$") (forward-line))
  7381. ((and (org-looking-at-p org-footnote-definition-re)
  7382. (let ((e (org-element-at-point)))
  7383. (and (eq (org-element-type e) 'footnote-definition)
  7384. (goto-char (org-element-property :end e))))))
  7385. ((org-looking-at-p org-outline-regexp) (forward-line))
  7386. ;; Give up if shifting would move before column 0 or
  7387. ;; if it would introduce a headline or a footnote
  7388. ;; definition.
  7389. (t
  7390. (skip-chars-forward " \t")
  7391. (let ((ind (current-column)))
  7392. (when (or (< ind diff)
  7393. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7394. (throw 'no-shift nil)))
  7395. ;; Ignore contents of example blocks and source
  7396. ;; blocks if their indentation is meant to be
  7397. ;; preserved. Jump to block's closing line.
  7398. (beginning-of-line)
  7399. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7400. (let ((e (org-element-at-point)))
  7401. (and (memq (org-element-type e)
  7402. '(example-block src-block))
  7403. (or org-src-preserve-indentation
  7404. (org-element-property :preserve-indent e))
  7405. (goto-char (org-element-property :end e))
  7406. (progn (skip-chars-backward " \r\t\n")
  7407. (beginning-of-line)
  7408. t))))
  7409. (forward-line))))))))
  7410. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7411. ;; by DIFF. Also skip contents of source or example blocks
  7412. ;; when indentation is meant to be preserved.
  7413. (while (not (eobp))
  7414. (cond
  7415. ((and (org-looking-at-p org-footnote-definition-re)
  7416. (let ((e (org-element-at-point)))
  7417. (and (eq (org-element-type e) 'footnote-definition)
  7418. (goto-char (org-element-property :end e))))))
  7419. ((org-looking-at-p org-outline-regexp) (forward-line))
  7420. ((org-looking-at-p "[ \t]*$") (forward-line))
  7421. (t
  7422. (org-indent-line-to (+ (org-get-indentation) diff))
  7423. (beginning-of-line)
  7424. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7425. (let ((e (org-element-at-point)))
  7426. (and (memq (org-element-type e)
  7427. '(example-block src-block))
  7428. (or org-src-preserve-indentation
  7429. (org-element-property :preserve-indent e))
  7430. (goto-char (org-element-property :end e))
  7431. (progn (skip-chars-backward " \r\t\n")
  7432. (beginning-of-line)
  7433. t))))
  7434. (forward-line)))))))))
  7435. (defun org-convert-to-odd-levels ()
  7436. "Convert an org-mode file with all levels allowed to one with odd levels.
  7437. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7438. level 5 etc."
  7439. (interactive)
  7440. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7441. (let ((outline-level 'org-outline-level)
  7442. (org-odd-levels-only nil) n)
  7443. (save-excursion
  7444. (goto-char (point-min))
  7445. (while (re-search-forward "^\\*\\*+ " nil t)
  7446. (setq n (- (length (match-string 0)) 2))
  7447. (while (>= (setq n (1- n)) 0)
  7448. (org-demote))
  7449. (end-of-line 1))))))
  7450. (defun org-convert-to-oddeven-levels ()
  7451. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7452. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7453. file contains a section with an even level, conversion would
  7454. destroy the structure of the file. An error is signaled in this
  7455. case."
  7456. (interactive)
  7457. (goto-char (point-min))
  7458. ;; First check if there are no even levels
  7459. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7460. (org-show-set-visibility 'canonical)
  7461. (error "Not all levels are odd in this file. Conversion not possible"))
  7462. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7463. (let ((outline-regexp org-outline-regexp)
  7464. (outline-level 'org-outline-level)
  7465. (org-odd-levels-only nil) n)
  7466. (save-excursion
  7467. (goto-char (point-min))
  7468. (while (re-search-forward "^\\*\\*+ " nil t)
  7469. (setq n (/ (1- (length (match-string 0))) 2))
  7470. (while (>= (setq n (1- n)) 0)
  7471. (org-promote))
  7472. (end-of-line 1))))))
  7473. (defun org-tr-level (n)
  7474. "Make N odd if required."
  7475. (if org-odd-levels-only (1+ (/ n 2)) n))
  7476. ;;; Vertical tree motion, cutting and pasting of subtrees
  7477. (defun org-move-subtree-up (&optional arg)
  7478. "Move the current subtree up past ARG headlines of the same level."
  7479. (interactive "p")
  7480. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7481. (defun org-move-subtree-down (&optional arg)
  7482. "Move the current subtree down past ARG headlines of the same level."
  7483. (interactive "p")
  7484. (setq arg (prefix-numeric-value arg))
  7485. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7486. 'org-get-last-sibling))
  7487. (ins-point (make-marker))
  7488. (cnt (abs arg))
  7489. (col (current-column))
  7490. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7491. ;; Select the tree
  7492. (org-back-to-heading)
  7493. (setq beg0 (point))
  7494. (save-excursion
  7495. (setq ne-beg (org-back-over-empty-lines))
  7496. (setq beg (point)))
  7497. (save-match-data
  7498. (save-excursion (outline-end-of-heading)
  7499. (setq folded (outline-invisible-p)))
  7500. (progn (org-end-of-subtree nil t)
  7501. (unless (eobp) (backward-char))))
  7502. (outline-next-heading)
  7503. (setq ne-end (org-back-over-empty-lines))
  7504. (setq end (point))
  7505. (goto-char beg0)
  7506. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7507. ;; include less whitespace
  7508. (save-excursion
  7509. (goto-char beg)
  7510. (forward-line (- ne-beg ne-end))
  7511. (setq beg (point))))
  7512. ;; Find insertion point, with error handling
  7513. (while (> cnt 0)
  7514. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7515. (progn (goto-char beg0)
  7516. (user-error "Cannot move past superior level or buffer limit")))
  7517. (setq cnt (1- cnt)))
  7518. (if (> arg 0)
  7519. ;; Moving forward - still need to move over subtree
  7520. (progn (org-end-of-subtree t t)
  7521. (save-excursion
  7522. (org-back-over-empty-lines)
  7523. (or (bolp) (newline)))))
  7524. (setq ne-ins (org-back-over-empty-lines))
  7525. (move-marker ins-point (point))
  7526. (setq txt (buffer-substring beg end))
  7527. (org-save-markers-in-region beg end)
  7528. (delete-region beg end)
  7529. (org-remove-empty-overlays-at beg)
  7530. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7531. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7532. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7533. (let ((bbb (point)))
  7534. (insert-before-markers txt)
  7535. (org-reinstall-markers-in-region bbb)
  7536. (move-marker ins-point bbb))
  7537. (or (bolp) (insert "\n"))
  7538. (setq ins-end (point))
  7539. (goto-char ins-point)
  7540. (org-skip-whitespace)
  7541. (when (and (< arg 0)
  7542. (org-first-sibling-p)
  7543. (> ne-ins ne-beg))
  7544. ;; Move whitespace back to beginning
  7545. (save-excursion
  7546. (goto-char ins-end)
  7547. (let ((kill-whole-line t))
  7548. (kill-line (- ne-ins ne-beg)) (point)))
  7549. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7550. (move-marker ins-point nil)
  7551. (if folded
  7552. (hide-subtree)
  7553. (org-show-entry)
  7554. (show-children)
  7555. (org-cycle-hide-drawers 'children))
  7556. (org-clean-visibility-after-subtree-move)
  7557. ;; move back to the initial column we were at
  7558. (move-to-column col)))
  7559. (defvar org-subtree-clip ""
  7560. "Clipboard for cut and paste of subtrees.
  7561. This is actually only a copy of the kill, because we use the normal kill
  7562. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7563. (defvar org-subtree-clip-folded nil
  7564. "Was the last copied subtree folded?
  7565. This is used to fold the tree back after pasting.")
  7566. (defun org-cut-subtree (&optional n)
  7567. "Cut the current subtree into the clipboard.
  7568. With prefix arg N, cut this many sequential subtrees.
  7569. This is a short-hand for marking the subtree and then cutting it."
  7570. (interactive "p")
  7571. (org-copy-subtree n 'cut))
  7572. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7573. "Copy the current subtree it in the clipboard.
  7574. With prefix arg N, copy this many sequential subtrees.
  7575. This is a short-hand for marking the subtree and then copying it.
  7576. If CUT is non-nil, actually cut the subtree.
  7577. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7578. of some markers in the region, even if CUT is non-nil. This is
  7579. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7580. (interactive "p")
  7581. (let (beg end folded (beg0 (point)))
  7582. (if (org-called-interactively-p 'any)
  7583. (org-back-to-heading nil) ; take what looks like a subtree
  7584. (org-back-to-heading t)) ; take what is really there
  7585. (setq beg (point))
  7586. (skip-chars-forward " \t\r\n")
  7587. (save-match-data
  7588. (if nosubtrees
  7589. (outline-next-heading)
  7590. (save-excursion (outline-end-of-heading)
  7591. (setq folded (outline-invisible-p)))
  7592. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7593. (org-end-of-subtree t t)))
  7594. ;; Include the end of an inlinetask
  7595. (when (and (featurep 'org-inlinetask)
  7596. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7597. "END[ \t]*$")))
  7598. (end-of-line))
  7599. (setq end (point))
  7600. (goto-char beg0)
  7601. (when (> end beg)
  7602. (setq org-subtree-clip-folded folded)
  7603. (when (or cut force-store-markers)
  7604. (org-save-markers-in-region beg end))
  7605. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7606. (setq org-subtree-clip (current-kill 0))
  7607. (message "%s: Subtree(s) with %d characters"
  7608. (if cut "Cut" "Copied")
  7609. (length org-subtree-clip)))))
  7610. (defun org-paste-subtree (&optional level tree for-yank remove)
  7611. "Paste the clipboard as a subtree, with modification of headline level.
  7612. The entire subtree is promoted or demoted in order to match a new headline
  7613. level.
  7614. If the cursor is at the beginning of a headline, the same level as
  7615. that headline is used to paste the tree.
  7616. If not, the new level is derived from the *visible* headings
  7617. before and after the insertion point, and taken to be the inferior headline
  7618. level of the two. So if the previous visible heading is level 3 and the
  7619. next is level 4 (or vice versa), level 4 will be used for insertion.
  7620. This makes sure that the subtree remains an independent subtree and does
  7621. not swallow low level entries.
  7622. You can also force a different level, either by using a numeric prefix
  7623. argument, or by inserting the heading marker by hand. For example, if the
  7624. cursor is after \"*****\", then the tree will be shifted to level 5.
  7625. If optional TREE is given, use this text instead of the kill ring.
  7626. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7627. move back over whitespace before inserting, and move point to the end of
  7628. the inserted text when done.
  7629. When REMOVE is non-nil, remove the subtree from the clipboard."
  7630. (interactive "P")
  7631. (setq tree (or tree (and kill-ring (current-kill 0))))
  7632. (unless (org-kill-is-subtree-p tree)
  7633. (user-error "%s"
  7634. (substitute-command-keys
  7635. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7636. (org-with-limited-levels
  7637. (let* ((visp (not (outline-invisible-p)))
  7638. (txt tree)
  7639. (^re_ "\\(\\*+\\)[ \t]*")
  7640. (old-level (if (string-match org-outline-regexp-bol txt)
  7641. (- (match-end 0) (match-beginning 0) 1)
  7642. -1))
  7643. (force-level (cond (level (prefix-numeric-value level))
  7644. ((and (looking-at "[ \t]*$")
  7645. (string-match
  7646. "^\\*+$" (buffer-substring
  7647. (point-at-bol) (point))))
  7648. (- (match-end 0) (match-beginning 0)))
  7649. ((and (bolp)
  7650. (looking-at org-outline-regexp))
  7651. (- (match-end 0) (point) 1))))
  7652. (previous-level (save-excursion
  7653. (condition-case nil
  7654. (progn
  7655. (outline-previous-visible-heading 1)
  7656. (if (looking-at ^re_)
  7657. (- (match-end 0) (match-beginning 0) 1)
  7658. 1))
  7659. (error 1))))
  7660. (next-level (save-excursion
  7661. (condition-case nil
  7662. (progn
  7663. (or (looking-at org-outline-regexp)
  7664. (outline-next-visible-heading 1))
  7665. (if (looking-at ^re_)
  7666. (- (match-end 0) (match-beginning 0) 1)
  7667. 1))
  7668. (error 1))))
  7669. (new-level (or force-level (max previous-level next-level)))
  7670. (shift (if (or (= old-level -1)
  7671. (= new-level -1)
  7672. (= old-level new-level))
  7673. 0
  7674. (- new-level old-level)))
  7675. (delta (if (> shift 0) -1 1))
  7676. (func (if (> shift 0) 'org-demote 'org-promote))
  7677. (org-odd-levels-only nil)
  7678. beg end newend)
  7679. ;; Remove the forced level indicator
  7680. (if force-level
  7681. (delete-region (point-at-bol) (point)))
  7682. ;; Paste
  7683. (beginning-of-line (if (bolp) 1 2))
  7684. (setq beg (point))
  7685. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7686. (insert-before-markers txt)
  7687. (unless (string-match "\n\\'" txt) (insert "\n"))
  7688. (setq newend (point))
  7689. (org-reinstall-markers-in-region beg)
  7690. (setq end (point))
  7691. (goto-char beg)
  7692. (skip-chars-forward " \t\n\r")
  7693. (setq beg (point))
  7694. (if (and (outline-invisible-p) visp)
  7695. (save-excursion (outline-show-heading)))
  7696. ;; Shift if necessary
  7697. (unless (= shift 0)
  7698. (save-restriction
  7699. (narrow-to-region beg end)
  7700. (while (not (= shift 0))
  7701. (org-map-region func (point-min) (point-max))
  7702. (setq shift (+ delta shift)))
  7703. (goto-char (point-min))
  7704. (setq newend (point-max))))
  7705. (when (or (org-called-interactively-p 'interactive) for-yank)
  7706. (message "Clipboard pasted as level %d subtree" new-level))
  7707. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7708. kill-ring
  7709. (eq org-subtree-clip (current-kill 0))
  7710. org-subtree-clip-folded)
  7711. ;; The tree was folded before it was killed/copied
  7712. (hide-subtree))
  7713. (and for-yank (goto-char newend))
  7714. (and remove (setq kill-ring (cdr kill-ring))))))
  7715. (defun org-kill-is-subtree-p (&optional txt)
  7716. "Check if the current kill is an outline subtree, or a set of trees.
  7717. Returns nil if kill does not start with a headline, or if the first
  7718. headline level is not the largest headline level in the tree.
  7719. So this will actually accept several entries of equal levels as well,
  7720. which is OK for `org-paste-subtree'.
  7721. If optional TXT is given, check this string instead of the current kill."
  7722. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7723. (re (org-get-limited-outline-regexp))
  7724. (^re (concat "^" re))
  7725. (start-level (and kill
  7726. (string-match
  7727. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7728. kill)
  7729. (- (match-end 2) (match-beginning 2) 1)))
  7730. (start (1+ (or (match-beginning 2) -1))))
  7731. (if (not start-level)
  7732. (progn
  7733. nil) ;; does not even start with a heading
  7734. (catch 'exit
  7735. (while (setq start (string-match ^re kill (1+ start)))
  7736. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7737. (throw 'exit nil)))
  7738. t))))
  7739. (defvar org-markers-to-move nil
  7740. "Markers that should be moved with a cut-and-paste operation.
  7741. Those markers are stored together with their positions relative to
  7742. the start of the region.")
  7743. (defun org-save-markers-in-region (beg end)
  7744. "Check markers in region.
  7745. If these markers are between BEG and END, record their position relative
  7746. to BEG, so that after moving the block of text, we can put the markers back
  7747. into place.
  7748. This function gets called just before an entry or tree gets cut from the
  7749. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7750. called immediately, to move the markers with the entries."
  7751. (setq org-markers-to-move nil)
  7752. (when (featurep 'org-clock)
  7753. (org-clock-save-markers-for-cut-and-paste beg end))
  7754. (when (featurep 'org-agenda)
  7755. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7756. (defun org-check-and-save-marker (marker beg end)
  7757. "Check if MARKER is between BEG and END.
  7758. If yes, remember the marker and the distance to BEG."
  7759. (when (and (marker-buffer marker)
  7760. (equal (marker-buffer marker) (current-buffer)))
  7761. (if (and (>= marker beg) (< marker end))
  7762. (push (cons marker (- marker beg)) org-markers-to-move))))
  7763. (defun org-reinstall-markers-in-region (beg)
  7764. "Move all remembered markers to their position relative to BEG."
  7765. (mapc (lambda (x)
  7766. (move-marker (car x) (+ beg (cdr x))))
  7767. org-markers-to-move)
  7768. (setq org-markers-to-move nil))
  7769. (defun org-narrow-to-subtree ()
  7770. "Narrow buffer to the current subtree."
  7771. (interactive)
  7772. (save-excursion
  7773. (save-match-data
  7774. (org-with-limited-levels
  7775. (narrow-to-region
  7776. (progn (org-back-to-heading t) (point))
  7777. (progn (org-end-of-subtree t t)
  7778. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7779. (point)))))))
  7780. (defun org-narrow-to-block ()
  7781. "Narrow buffer to the current block."
  7782. (interactive)
  7783. (let* ((case-fold-search t)
  7784. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7785. "^[ \t]*#\\+end_.*")))
  7786. (if blockp
  7787. (narrow-to-region (car blockp) (cdr blockp))
  7788. (user-error "Not in a block"))))
  7789. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7790. "Clone the task (subtree) at point N times.
  7791. The clones will be inserted as siblings.
  7792. In interactive use, the user will be prompted for the number of
  7793. clones to be produced. If the entry has a timestamp, the user
  7794. will also be prompted for a time shift, which may be a repeater
  7795. as used in time stamps, for example `+3d'. To disable this,
  7796. you can call the function with a universal prefix argument.
  7797. When a valid repeater is given and the entry contains any time
  7798. stamps, the clones will become a sequence in time, with time
  7799. stamps in the subtree shifted for each clone produced. If SHIFT
  7800. is nil or the empty string, time stamps will be left alone. The
  7801. ID property of the original subtree is removed.
  7802. If the original subtree did contain time stamps with a repeater,
  7803. the following will happen:
  7804. - the repeater will be removed in each clone
  7805. - an additional clone will be produced, with the current, unshifted
  7806. date(s) in the entry.
  7807. - the original entry will be placed *after* all the clones, with
  7808. repeater intact.
  7809. - the start days in the repeater in the original entry will be shifted
  7810. to past the last clone.
  7811. In this way you can spell out a number of instances of a repeating task,
  7812. and still retain the repeater to cover future instances of the task."
  7813. (interactive "nNumber of clones to produce: ")
  7814. (let ((shift
  7815. (or shift
  7816. (if (and (not (equal current-prefix-arg '(4)))
  7817. (save-excursion
  7818. (re-search-forward org-ts-regexp-both
  7819. (save-excursion
  7820. (org-end-of-subtree t)
  7821. (point)) t)))
  7822. (read-from-minibuffer
  7823. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7824. ""))) ;; No time shift
  7825. (n-no-remove -1)
  7826. (drawer-re org-drawer-regexp)
  7827. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7828. beg end template task idprop
  7829. shift-n shift-what doshift nmin nmax)
  7830. (if (not (and (integerp n) (> n 0)))
  7831. (user-error "Invalid number of replications %s" n))
  7832. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7833. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7834. shift)))
  7835. (user-error "Invalid shift specification %s" shift))
  7836. (when doshift
  7837. (setq shift-n (string-to-number (match-string 1 shift))
  7838. shift-what (cdr (assoc (match-string 2 shift)
  7839. '(("d" . day) ("w" . week)
  7840. ("m" . month) ("y" . year))))))
  7841. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7842. (setq nmin 1 nmax n)
  7843. (org-back-to-heading t)
  7844. (setq beg (point))
  7845. (setq idprop (org-entry-get nil "ID"))
  7846. (org-end-of-subtree t t)
  7847. (or (bolp) (insert "\n"))
  7848. (setq end (point))
  7849. (setq template (buffer-substring beg end))
  7850. (when (and doshift
  7851. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7852. (delete-region beg end)
  7853. (setq end beg)
  7854. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7855. (goto-char end)
  7856. (loop for n from nmin to nmax do
  7857. ;; prepare clone
  7858. (with-temp-buffer
  7859. (insert template)
  7860. (org-mode)
  7861. (goto-char (point-min))
  7862. (org-show-subtree)
  7863. (and idprop (if org-clone-delete-id
  7864. (org-entry-delete nil "ID")
  7865. (org-id-get-create t)))
  7866. (unless (= n 0)
  7867. (while (re-search-forward org-clock-re nil t)
  7868. (kill-whole-line))
  7869. (goto-char (point-min))
  7870. (while (re-search-forward drawer-re nil t)
  7871. (org-remove-empty-drawer-at (point))))
  7872. (goto-char (point-min))
  7873. (when doshift
  7874. (while (re-search-forward org-ts-regexp-both nil t)
  7875. (org-timestamp-change (* n shift-n) shift-what))
  7876. (unless (= n n-no-remove)
  7877. (goto-char (point-min))
  7878. (while (re-search-forward org-ts-regexp nil t)
  7879. (save-excursion
  7880. (goto-char (match-beginning 0))
  7881. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7882. (delete-region (match-beginning 1) (match-end 1)))))))
  7883. (setq task (buffer-string)))
  7884. (insert task))
  7885. (goto-char beg)))
  7886. ;;; Outline Sorting
  7887. (defun org-sort (with-case)
  7888. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7889. Optional argument WITH-CASE means sort case-sensitively."
  7890. (interactive "P")
  7891. (cond
  7892. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7893. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7894. (t
  7895. (org-call-with-arg 'org-sort-entries with-case))))
  7896. (defun org-sort-remove-invisible (s)
  7897. "Remove invisible links from string S."
  7898. (remove-text-properties 0 (length s) org-rm-props s)
  7899. (while (string-match org-bracket-link-regexp s)
  7900. (setq s (replace-match (if (match-end 2)
  7901. (match-string 3 s)
  7902. (match-string 1 s)) t t s)))
  7903. (let ((st (format " %s " s)))
  7904. (while (string-match org-emph-re st)
  7905. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7906. (setq s (substring st 1 -1)))
  7907. s)
  7908. (defvar org-priority-regexp) ; defined later in the file
  7909. (defvar org-after-sorting-entries-or-items-hook nil
  7910. "Hook that is run after a bunch of entries or items have been sorted.
  7911. When children are sorted, the cursor is in the parent line when this
  7912. hook gets called. When a region or a plain list is sorted, the cursor
  7913. will be in the first entry of the sorted region/list.")
  7914. (defun org-sort-entries
  7915. (&optional with-case sorting-type getkey-func compare-func property)
  7916. "Sort entries on a certain level of an outline tree.
  7917. If there is an active region, the entries in the region are sorted.
  7918. Else, if the cursor is before the first entry, sort the top-level items.
  7919. Else, the children of the entry at point are sorted.
  7920. Sorting can be alphabetically, numerically, by date/time as given by
  7921. a time stamp, by a property, by priority order, or by a custom function.
  7922. The command prompts for the sorting type unless it has been given to the
  7923. function through the SORTING-TYPE argument, which needs to be a character,
  7924. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7925. the precise meaning of each character:
  7926. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7927. c By creation time, which is assumed to be the first inactive time stamp
  7928. at the beginning of a line.
  7929. d By deadline date/time.
  7930. k By clocking time.
  7931. n Numerically, by converting the beginning of the entry/item to a number.
  7932. o By order of TODO keywords.
  7933. p By priority according to the cookie.
  7934. r By the value of a property.
  7935. s By scheduled date/time.
  7936. t By date/time, either the first active time stamp in the entry, or, if
  7937. none exist, by the first inactive one.
  7938. Capital letters will reverse the sort order.
  7939. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7940. called with point at the beginning of the record. It must return either
  7941. a string or a number that should serve as the sorting key for that record.
  7942. Comparing entries ignores case by default. However, with an optional argument
  7943. WITH-CASE, the sorting considers case as well.
  7944. Sorting is done against the visible part of the headlines, it ignores hidden
  7945. links.
  7946. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7947. (interactive "P")
  7948. (let ((case-func (if with-case 'identity 'downcase))
  7949. (cmstr
  7950. ;; The clock marker is lost when using `sort-subr', let's
  7951. ;; store the clocking string.
  7952. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7953. (save-excursion
  7954. (goto-char org-clock-marker)
  7955. (looking-back "^.*") (match-string-no-properties 0))))
  7956. start beg end stars re re2
  7957. txt what tmp)
  7958. ;; Find beginning and end of region to sort
  7959. (cond
  7960. ((org-region-active-p)
  7961. ;; we will sort the region
  7962. (setq end (region-end)
  7963. what "region")
  7964. (goto-char (region-beginning))
  7965. (if (not (org-at-heading-p)) (outline-next-heading))
  7966. (setq start (point)))
  7967. ((or (org-at-heading-p)
  7968. (ignore-errors (progn (org-back-to-heading) t)))
  7969. ;; we will sort the children of the current headline
  7970. (org-back-to-heading)
  7971. (setq start (point)
  7972. end (progn (org-end-of-subtree t t)
  7973. (or (bolp) (insert "\n"))
  7974. (when (>= (org-back-over-empty-lines) 1)
  7975. (forward-line 1))
  7976. (point))
  7977. what "children")
  7978. (goto-char start)
  7979. (show-subtree)
  7980. (outline-next-heading))
  7981. (t
  7982. ;; we will sort the top-level entries in this file
  7983. (goto-char (point-min))
  7984. (or (org-at-heading-p) (outline-next-heading))
  7985. (setq start (point))
  7986. (goto-char (point-max))
  7987. (beginning-of-line 1)
  7988. (when (looking-at ".*?\\S-")
  7989. ;; File ends in a non-white line
  7990. (end-of-line 1)
  7991. (insert "\n"))
  7992. (setq end (point-max))
  7993. (setq what "top-level")
  7994. (goto-char start)
  7995. (show-all)))
  7996. (setq beg (point))
  7997. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7998. (looking-at "\\(\\*+\\)")
  7999. (setq stars (match-string 1)
  8000. re (concat "^" (regexp-quote stars) " +")
  8001. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8002. txt (buffer-substring beg end))
  8003. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  8004. (if (and (not (equal stars "*")) (string-match re2 txt))
  8005. (user-error "Region to sort contains a level above the first entry"))
  8006. (unless sorting-type
  8007. (message
  8008. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8009. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8010. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8011. what)
  8012. (setq sorting-type (read-char-exclusive))
  8013. (unless getkey-func
  8014. (and (= (downcase sorting-type) ?f)
  8015. (setq getkey-func
  8016. (org-icompleting-read "Sort using function: "
  8017. obarray 'fboundp t nil nil))
  8018. (setq getkey-func (intern getkey-func))))
  8019. (and (= (downcase sorting-type) ?r)
  8020. (not property)
  8021. (setq property
  8022. (org-icompleting-read "Property: "
  8023. (mapcar 'list (org-buffer-property-keys t))
  8024. nil t))))
  8025. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8026. (message "Sorting entries...")
  8027. (save-restriction
  8028. (narrow-to-region start end)
  8029. (let ((dcst (downcase sorting-type))
  8030. (case-fold-search nil)
  8031. (now (current-time)))
  8032. (sort-subr
  8033. (/= dcst sorting-type)
  8034. ;; This function moves to the beginning character of the "record" to
  8035. ;; be sorted.
  8036. (lambda nil
  8037. (if (re-search-forward re nil t)
  8038. (goto-char (match-beginning 0))
  8039. (goto-char (point-max))))
  8040. ;; This function moves to the last character of the "record" being
  8041. ;; sorted.
  8042. (lambda nil
  8043. (save-match-data
  8044. (condition-case nil
  8045. (outline-forward-same-level 1)
  8046. (error
  8047. (goto-char (point-max))))))
  8048. ;; This function returns the value that gets sorted against.
  8049. (lambda nil
  8050. (cond
  8051. ((= dcst ?n)
  8052. (if (looking-at org-complex-heading-regexp)
  8053. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8054. nil))
  8055. ((= dcst ?a)
  8056. (if (looking-at org-complex-heading-regexp)
  8057. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8058. nil))
  8059. ((= dcst ?k)
  8060. (or (get-text-property (point) :org-clock-minutes) 0))
  8061. ((= dcst ?t)
  8062. (let ((end (save-excursion (outline-next-heading) (point))))
  8063. (if (or (re-search-forward org-ts-regexp end t)
  8064. (re-search-forward org-ts-regexp-both end t))
  8065. (org-time-string-to-seconds (match-string 0))
  8066. (org-float-time now))))
  8067. ((= dcst ?c)
  8068. (let ((end (save-excursion (outline-next-heading) (point))))
  8069. (if (re-search-forward
  8070. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8071. end t)
  8072. (org-time-string-to-seconds (match-string 0))
  8073. (org-float-time now))))
  8074. ((= dcst ?s)
  8075. (let ((end (save-excursion (outline-next-heading) (point))))
  8076. (if (re-search-forward org-scheduled-time-regexp end t)
  8077. (org-time-string-to-seconds (match-string 1))
  8078. (org-float-time now))))
  8079. ((= dcst ?d)
  8080. (let ((end (save-excursion (outline-next-heading) (point))))
  8081. (if (re-search-forward org-deadline-time-regexp end t)
  8082. (org-time-string-to-seconds (match-string 1))
  8083. (org-float-time now))))
  8084. ((= dcst ?p)
  8085. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8086. (string-to-char (match-string 2))
  8087. org-default-priority))
  8088. ((= dcst ?r)
  8089. (or (org-entry-get nil property) ""))
  8090. ((= dcst ?o)
  8091. (if (looking-at org-complex-heading-regexp)
  8092. (let* ((m (match-string 2))
  8093. (s (if (member m org-done-keywords) '- '+)))
  8094. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8095. ((= dcst ?f)
  8096. (if getkey-func
  8097. (progn
  8098. (setq tmp (funcall getkey-func))
  8099. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8100. tmp)
  8101. (error "Invalid key function `%s'" getkey-func)))
  8102. (t (error "Invalid sorting type `%c'" sorting-type))))
  8103. nil
  8104. (cond
  8105. ((= dcst ?a) 'string<)
  8106. ((= dcst ?f) compare-func)
  8107. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8108. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8109. ;; Reset the clock marker if needed
  8110. (when cmstr
  8111. (save-excursion
  8112. (goto-char start)
  8113. (search-forward cmstr nil t)
  8114. (move-marker org-clock-marker (point))))
  8115. (message "Sorting entries...done")))
  8116. ;;; The orgstruct minor mode
  8117. ;; Define a minor mode which can be used in other modes in order to
  8118. ;; integrate the org-mode structure editing commands.
  8119. ;; This is really a hack, because the org-mode structure commands use
  8120. ;; keys which normally belong to the major mode. Here is how it
  8121. ;; works: The minor mode defines all the keys necessary to operate the
  8122. ;; structure commands, but wraps the commands into a function which
  8123. ;; tests if the cursor is currently at a headline or a plain list
  8124. ;; item. If that is the case, the structure command is used,
  8125. ;; temporarily setting many Org-mode variables like regular
  8126. ;; expressions for filling etc. However, when any of those keys is
  8127. ;; used at a different location, function uses `key-binding' to look
  8128. ;; up if the key has an associated command in another currently active
  8129. ;; keymap (minor modes, major mode, global), and executes that
  8130. ;; command. There might be problems if any of the keys is otherwise
  8131. ;; used as a prefix key.
  8132. (defcustom orgstruct-heading-prefix-regexp ""
  8133. "Regexp that matches the custom prefix of Org headlines in
  8134. orgstruct(++)-mode."
  8135. :group 'org
  8136. :version "24.4"
  8137. :package-version '(Org . "8.3")
  8138. :type 'regexp)
  8139. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8140. (defcustom orgstruct-setup-hook nil
  8141. "Hook run after orgstruct-mode-map is filled."
  8142. :group 'org
  8143. :version "24.4"
  8144. :package-version '(Org . "8.0")
  8145. :type 'hook)
  8146. (defvar orgstruct-initialized nil)
  8147. (defvar org-local-vars nil
  8148. "List of local variables, for use by `orgstruct-mode'.")
  8149. ;;;###autoload
  8150. (define-minor-mode orgstruct-mode
  8151. "Toggle the minor mode `orgstruct-mode'.
  8152. This mode is for using Org-mode structure commands in other
  8153. modes. The following keys behave as if Org-mode were active, if
  8154. the cursor is on a headline, or on a plain list item (both as
  8155. defined by Org-mode)."
  8156. nil " OrgStruct" (make-sparse-keymap)
  8157. (funcall (if orgstruct-mode
  8158. 'add-to-invisibility-spec
  8159. 'remove-from-invisibility-spec)
  8160. '(outline . t))
  8161. (when orgstruct-mode
  8162. (org-load-modules-maybe)
  8163. (unless orgstruct-initialized
  8164. (orgstruct-setup)
  8165. (setq orgstruct-initialized t))))
  8166. ;;;###autoload
  8167. (defun turn-on-orgstruct ()
  8168. "Unconditionally turn on `orgstruct-mode'."
  8169. (orgstruct-mode 1))
  8170. (defvar org-fb-vars nil)
  8171. (make-variable-buffer-local 'org-fb-vars)
  8172. (defun orgstruct++-mode (&optional arg)
  8173. "Toggle `orgstruct-mode', the enhanced version of it.
  8174. In addition to setting orgstruct-mode, this also exports all
  8175. indentation and autofilling variables from org-mode into the
  8176. buffer. It will also recognize item context in multiline items."
  8177. (interactive "P")
  8178. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8179. (if (< arg 1)
  8180. (progn (orgstruct-mode -1)
  8181. (mapc (lambda(v)
  8182. (org-set-local (car v)
  8183. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8184. org-fb-vars))
  8185. (orgstruct-mode 1)
  8186. (setq org-fb-vars nil)
  8187. (unless org-local-vars
  8188. (setq org-local-vars (org-get-local-variables)))
  8189. (let (var val)
  8190. (mapc
  8191. (lambda (x)
  8192. (when (string-match
  8193. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8194. (symbol-name (car x)))
  8195. (setq var (car x) val (nth 1 x))
  8196. (push (list var `(quote ,(eval var))) org-fb-vars)
  8197. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8198. org-local-vars)
  8199. (org-set-local 'orgstruct-is-++ t))))
  8200. (defvar orgstruct-is-++ nil
  8201. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8202. (make-variable-buffer-local 'orgstruct-is-++)
  8203. ;;;###autoload
  8204. (defun turn-on-orgstruct++ ()
  8205. "Unconditionally turn on `orgstruct++-mode'."
  8206. (orgstruct++-mode 1))
  8207. (defun orgstruct-error ()
  8208. "Error when there is no default binding for a structure key."
  8209. (interactive)
  8210. (funcall (if (fboundp 'user-error)
  8211. 'user-error
  8212. 'error)
  8213. "This key has no function outside structure elements"))
  8214. (defun orgstruct-setup ()
  8215. "Setup orgstruct keymap."
  8216. (dolist (cell '((org-demote . t)
  8217. (org-metaleft . t)
  8218. (org-metaright . t)
  8219. (org-promote . t)
  8220. (org-shiftmetaleft . t)
  8221. (org-shiftmetaright . t)
  8222. org-backward-element
  8223. org-backward-heading-same-level
  8224. org-ctrl-c-ret
  8225. org-ctrl-c-minus
  8226. org-ctrl-c-star
  8227. org-cycle
  8228. org-forward-heading-same-level
  8229. org-insert-heading
  8230. org-insert-heading-respect-content
  8231. org-kill-note-or-show-branches
  8232. org-mark-subtree
  8233. org-meta-return
  8234. org-metadown
  8235. org-metaup
  8236. org-narrow-to-subtree
  8237. org-promote-subtree
  8238. org-reveal
  8239. org-shiftdown
  8240. org-shiftleft
  8241. org-shiftmetadown
  8242. org-shiftmetaup
  8243. org-shiftright
  8244. org-shifttab
  8245. org-shifttab
  8246. org-shiftup
  8247. org-show-subtree
  8248. org-sort
  8249. org-up-element
  8250. outline-demote
  8251. outline-next-visible-heading
  8252. outline-previous-visible-heading
  8253. outline-promote
  8254. outline-up-heading
  8255. show-children))
  8256. (let ((f (or (car-safe cell) cell))
  8257. (disable-when-heading-prefix (cdr-safe cell)))
  8258. (when (fboundp f)
  8259. (let ((new-bindings))
  8260. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8261. (where-is-internal f outline-mode-map)))
  8262. (push binding new-bindings)
  8263. ;; TODO use local-function-key-map
  8264. (dolist (rep '(("<tab>" . "TAB")
  8265. ("<return>" . "RET")
  8266. ("<escape>" . "ESC")
  8267. ("<delete>" . "DEL")))
  8268. (setq binding (read-kbd-macro
  8269. (let ((case-fold-search))
  8270. (replace-regexp-in-string
  8271. (regexp-quote (cdr rep))
  8272. (car rep)
  8273. (key-description binding)))))
  8274. (pushnew binding new-bindings :test 'equal)))
  8275. (dolist (binding new-bindings)
  8276. (let ((key (lookup-key orgstruct-mode-map binding)))
  8277. (when (or (not key) (numberp key))
  8278. (ignore-errors
  8279. (org-defkey orgstruct-mode-map
  8280. binding
  8281. (orgstruct-make-binding
  8282. f binding disable-when-heading-prefix))))))))))
  8283. (run-hooks 'orgstruct-setup-hook))
  8284. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8285. "Create a function for binding in the structure minor mode.
  8286. FUN is the command to call inside a table. KEY is the key that
  8287. should be checked in for a command to execute outside of tables.
  8288. Non-nil `disable-when-heading-prefix' means to disable the command
  8289. if `orgstruct-heading-prefix-regexp' is not empty."
  8290. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8291. (let ((nname name)
  8292. (i 0))
  8293. (while (fboundp (intern nname))
  8294. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8295. (setq name (intern nname)))
  8296. (eval
  8297. (let ((bindings '((org-heading-regexp
  8298. (concat "^"
  8299. orgstruct-heading-prefix-regexp
  8300. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8301. (org-outline-regexp
  8302. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8303. (org-outline-regexp-bol
  8304. (concat "^" org-outline-regexp))
  8305. (outline-regexp org-outline-regexp)
  8306. (outline-heading-end-regexp "\n")
  8307. (outline-level 'org-outline-level)
  8308. (outline-heading-alist))))
  8309. `(defun ,name (arg)
  8310. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8311. "Outside of structure, run the binding of `"
  8312. (key-description key) "'."
  8313. (when disable-when-heading-prefix
  8314. (concat
  8315. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8316. "back to the default binding due to limitations of Org's implementation of\n"
  8317. "`" (symbol-name fun) "'.")))
  8318. (interactive "p")
  8319. (let* ((disable
  8320. ,(and disable-when-heading-prefix
  8321. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8322. (fallback
  8323. (or disable
  8324. (not
  8325. (let* ,bindings
  8326. (org-context-p 'headline 'item
  8327. ,(when (memq fun
  8328. '(org-insert-heading
  8329. org-insert-heading-respect-content
  8330. org-meta-return))
  8331. '(when orgstruct-is-++
  8332. 'item-body))))))))
  8333. (if fallback
  8334. (let* ((orgstruct-mode)
  8335. (binding
  8336. (let ((key ,key))
  8337. (catch 'exit
  8338. (dolist
  8339. (rep
  8340. '(nil
  8341. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8342. ("<\\([^>]*\\)return>" . "\\1RET")
  8343. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8344. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8345. nil)
  8346. (when rep
  8347. (setq key (read-kbd-macro
  8348. (let ((case-fold-search))
  8349. (replace-regexp-in-string
  8350. (car rep)
  8351. (cdr rep)
  8352. (key-description key))))))
  8353. (when (key-binding key)
  8354. (throw 'exit (key-binding key))))))))
  8355. (if (keymapp binding)
  8356. (org-set-transient-map binding)
  8357. (let ((func (or binding
  8358. (unless disable
  8359. 'orgstruct-error))))
  8360. (when func
  8361. (call-interactively func)))))
  8362. (org-run-like-in-org-mode
  8363. (lambda ()
  8364. (interactive)
  8365. (let* ,bindings
  8366. (call-interactively ',fun)))))))))
  8367. name))
  8368. (defun org-contextualize-keys (alist contexts)
  8369. "Return valid elements in ALIST depending on CONTEXTS.
  8370. `org-agenda-custom-commands' or `org-capture-templates' are the
  8371. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8372. or `org-capture-templates-contexts' are the associated contexts
  8373. definitions."
  8374. (let ((contexts
  8375. ;; normalize contexts
  8376. (mapcar
  8377. (lambda(c) (cond ((listp (cadr c))
  8378. (list (car c) (car c) (cadr c)))
  8379. ((string= "" (cadr c))
  8380. (list (car c) (car c) (caddr c)))
  8381. (t c))) contexts))
  8382. (a alist) c r s)
  8383. ;; loop over all commands or templates
  8384. (while (setq c (pop a))
  8385. (let (vrules repl)
  8386. (cond
  8387. ((not (assoc (car c) contexts))
  8388. (push c r))
  8389. ((and (assoc (car c) contexts)
  8390. (setq vrules (org-contextualize-validate-key
  8391. (car c) contexts)))
  8392. (mapc (lambda (vr)
  8393. (when (not (equal (car vr) (cadr vr)))
  8394. (setq repl vr))) vrules)
  8395. (if (not repl) (push c r)
  8396. (push (cadr repl) s)
  8397. (push
  8398. (cons (car c)
  8399. (cdr (or (assoc (cadr repl) alist)
  8400. (error "Undefined key `%s' as contextual replacement for `%s'"
  8401. (cadr repl) (car c)))))
  8402. r))))))
  8403. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8404. (delq
  8405. nil
  8406. (delete-dups
  8407. (mapcar (lambda (x)
  8408. (let ((tpl (car x)))
  8409. (when (not (delq
  8410. nil
  8411. (mapcar (lambda(y)
  8412. (equal y tpl)) s))) x)))
  8413. (reverse r))))))
  8414. (defun org-contextualize-validate-key (key contexts)
  8415. "Check CONTEXTS for agenda or capture KEY."
  8416. (let (r rr res)
  8417. (while (setq r (pop contexts))
  8418. (mapc
  8419. (lambda (rr)
  8420. (when
  8421. (and (equal key (car r))
  8422. (if (functionp rr) (funcall rr)
  8423. (or (and (eq (car rr) 'in-file)
  8424. (buffer-file-name)
  8425. (string-match (cdr rr) (buffer-file-name)))
  8426. (and (eq (car rr) 'in-mode)
  8427. (string-match (cdr rr) (symbol-name major-mode)))
  8428. (and (eq (car rr) 'in-buffer)
  8429. (string-match (cdr rr) (buffer-name)))
  8430. (when (and (eq (car rr) 'not-in-file)
  8431. (buffer-file-name))
  8432. (not (string-match (cdr rr) (buffer-file-name))))
  8433. (when (eq (car rr) 'not-in-mode)
  8434. (not (string-match (cdr rr) (symbol-name major-mode))))
  8435. (when (eq (car rr) 'not-in-buffer)
  8436. (not (string-match (cdr rr) (buffer-name)))))))
  8437. (push r res)))
  8438. (car (last r))))
  8439. (delete-dups (delq nil res))))
  8440. (defun org-context-p (&rest contexts)
  8441. "Check if local context is any of CONTEXTS.
  8442. Possible values in the list of contexts are `table', `headline', and `item'."
  8443. (let ((pos (point)))
  8444. (goto-char (point-at-bol))
  8445. (prog1 (or (and (memq 'table contexts)
  8446. (looking-at "[ \t]*|"))
  8447. (and (memq 'headline contexts)
  8448. (looking-at org-outline-regexp))
  8449. (and (memq 'item contexts)
  8450. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8451. (and (memq 'item-body contexts)
  8452. (org-in-item-p)))
  8453. (goto-char pos))))
  8454. (defun org-get-local-variables ()
  8455. "Return a list of all local variables in an Org mode buffer."
  8456. (let (varlist)
  8457. (with-current-buffer (get-buffer-create "*Org tmp*")
  8458. (erase-buffer)
  8459. (org-mode)
  8460. (setq varlist (buffer-local-variables)))
  8461. (kill-buffer "*Org tmp*")
  8462. (delq nil
  8463. (mapcar
  8464. (lambda (x)
  8465. (setq x
  8466. (if (symbolp x)
  8467. (list x)
  8468. (list (car x) (cdr x))))
  8469. (if (and (not (get (car x) 'org-state))
  8470. (string-match
  8471. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8472. (symbol-name (car x))))
  8473. x nil))
  8474. varlist))))
  8475. (defun org-clone-local-variables (from-buffer &optional regexp)
  8476. "Clone local variables from FROM-BUFFER.
  8477. Optional argument REGEXP selects variables to clone."
  8478. (mapc
  8479. (lambda (pair)
  8480. (and (symbolp (car pair))
  8481. (or (null regexp)
  8482. (string-match regexp (symbol-name (car pair))))
  8483. (set (make-local-variable (car pair))
  8484. (cdr pair))))
  8485. (buffer-local-variables from-buffer)))
  8486. ;;;###autoload
  8487. (defun org-run-like-in-org-mode (cmd)
  8488. "Run a command, pretending that the current buffer is in Org-mode.
  8489. This will temporarily bind local variables that are typically bound in
  8490. Org-mode to the values they have in Org-mode, and then interactively
  8491. call CMD."
  8492. (org-load-modules-maybe)
  8493. (unless org-local-vars
  8494. (setq org-local-vars (org-get-local-variables)))
  8495. (let (binds)
  8496. (dolist (var org-local-vars)
  8497. (when (or (not (boundp (car var)))
  8498. (eq (symbol-value (car var))
  8499. (default-value (car var))))
  8500. (push (list (car var) `(quote ,(cadr var))) binds)))
  8501. (eval `(let ,binds
  8502. (call-interactively (quote ,cmd))))))
  8503. (defun org-get-category (&optional pos force-refresh)
  8504. "Get the category applying to position POS."
  8505. (save-match-data
  8506. (if force-refresh (org-refresh-category-properties))
  8507. (let ((pos (or pos (point))))
  8508. (or (get-text-property pos 'org-category)
  8509. (progn (org-refresh-category-properties)
  8510. (get-text-property pos 'org-category))))))
  8511. ;;; Refresh properties
  8512. (defun org-refresh-properties (dprop tprop)
  8513. "Refresh buffer text properties.
  8514. DPROP is the drawer property and TPROP is either the
  8515. corresponding text property to set, or an alist with each element
  8516. being a text property (as a symbol) and a function to apply to
  8517. the value of the drawer property."
  8518. (let ((case-fold-search t)
  8519. (inhibit-read-only t))
  8520. (org-with-silent-modifications
  8521. (save-excursion
  8522. (save-restriction
  8523. (widen)
  8524. (goto-char (point-min))
  8525. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8526. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8527. (defun org-refresh-property (tprop p)
  8528. "Refresh the buffer text property TPROP from the drawer property P.
  8529. The refresh happens only for the current tree (not subtree)."
  8530. (unless (org-before-first-heading-p)
  8531. (save-excursion
  8532. (org-back-to-heading t)
  8533. (if (symbolp tprop)
  8534. ;; TPROP is a text property symbol
  8535. (put-text-property
  8536. (point) (or (outline-next-heading) (point-max)) tprop p)
  8537. ;; TPROP is an alist with (properties . function) elements
  8538. (dolist (al tprop)
  8539. (save-excursion
  8540. (put-text-property
  8541. (line-beginning-position) (or (outline-next-heading) (point-max))
  8542. (car al)
  8543. (funcall (cdr al) p))))))))
  8544. (defun org-refresh-category-properties ()
  8545. "Refresh category text properties in the buffer."
  8546. (let ((case-fold-search t)
  8547. (inhibit-read-only t)
  8548. (default-category
  8549. (cond ((null org-category)
  8550. (if buffer-file-name
  8551. (file-name-sans-extension
  8552. (file-name-nondirectory buffer-file-name))
  8553. "???"))
  8554. ((symbolp org-category) (symbol-name org-category))
  8555. (t org-category))))
  8556. (org-with-silent-modifications
  8557. (org-with-wide-buffer
  8558. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8559. ;; This is the default category for the buffer. If none is
  8560. ;; found, fall-back to `org-category' or buffer file name.
  8561. (put-text-property
  8562. (point-min) (point-max)
  8563. 'org-category
  8564. (catch 'buffer-category
  8565. (goto-char (point-max))
  8566. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8567. (let ((element (org-element-at-point)))
  8568. (when (eq (org-element-type element) 'keyword)
  8569. (throw 'buffer-category
  8570. (org-element-property :value element)))))
  8571. default-category))
  8572. ;; Set sub-tree specific categories.
  8573. (goto-char (point-min))
  8574. (let ((regexp (org-re-property "CATEGORY")))
  8575. (while (re-search-forward regexp nil t)
  8576. (let ((value (org-match-string-no-properties 3)))
  8577. (when (org-at-property-p)
  8578. (put-text-property
  8579. (save-excursion (org-back-to-heading t) (point))
  8580. (save-excursion (org-end-of-subtree t t) (point))
  8581. 'org-category
  8582. value)))))))))
  8583. (defun org-refresh-stats-properties ()
  8584. "Refresh stats text properties in the buffer."
  8585. (let (stats)
  8586. (org-with-silent-modifications
  8587. (save-excursion
  8588. (save-restriction
  8589. (widen)
  8590. (goto-char (point-min))
  8591. (while (re-search-forward
  8592. (concat org-outline-regexp-bol ".*"
  8593. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8594. nil t)
  8595. (setq stats (cond ((equal (match-string 3) "0") 0)
  8596. ((match-string 2)
  8597. (/ (* (string-to-number (match-string 2)) 100)
  8598. (string-to-number (match-string 3))))
  8599. (t (string-to-number (match-string 1)))))
  8600. (org-back-to-heading t)
  8601. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8602. 'org-stats stats)))))))
  8603. (defun org-refresh-effort-properties ()
  8604. "Refresh effort properties"
  8605. (org-refresh-properties
  8606. org-effort-property
  8607. '((effort . identity)
  8608. (effort-minutes . org-duration-string-to-minutes))))
  8609. ;;;; Link Stuff
  8610. ;;; Link abbreviations
  8611. (defun org-link-expand-abbrev (link)
  8612. "Apply replacements as defined in `org-link-abbrev-alist'."
  8613. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8614. (let* ((key (match-string 1 link))
  8615. (as (or (assoc key org-link-abbrev-alist-local)
  8616. (assoc key org-link-abbrev-alist)))
  8617. (tag (and (match-end 2) (match-string 3 link)))
  8618. rpl)
  8619. (if (not as)
  8620. link
  8621. (setq rpl (cdr as))
  8622. (cond
  8623. ((symbolp rpl) (funcall rpl tag))
  8624. ((string-match "%(\\([^)]+\\))" rpl)
  8625. (replace-match
  8626. (save-match-data
  8627. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8628. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8629. ((string-match "%h" rpl)
  8630. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8631. (t (concat rpl tag)))))
  8632. link))
  8633. ;;; Storing and inserting links
  8634. (defvar org-insert-link-history nil
  8635. "Minibuffer history for links inserted with `org-insert-link'.")
  8636. (defvar org-stored-links nil
  8637. "Contains the links stored with `org-store-link'.")
  8638. (defvar org-store-link-plist nil
  8639. "Plist with info about the most recently link created with `org-store-link'.")
  8640. (defvar org-link-protocols nil
  8641. "Link protocols added to Org-mode using `org-add-link-type'.")
  8642. (defvar org-store-link-functions nil
  8643. "List of functions that are called to create and store a link.
  8644. Each function will be called in turn until one returns a non-nil
  8645. value. Each function should check if it is responsible for creating
  8646. this link (for example by looking at the major mode).
  8647. If not, it must exit and return nil.
  8648. If yes, it should return a non-nil value after a calling
  8649. `org-store-link-props' with a list of properties and values.
  8650. Special properties are:
  8651. :type The link prefix, like \"http\". This must be given.
  8652. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8653. This is obligatory as well.
  8654. :description Optional default description for the second pair
  8655. of brackets in an Org-mode link. The user can still change
  8656. this when inserting this link into an Org-mode buffer.
  8657. In addition to these, any additional properties can be specified
  8658. and then used in capture templates.")
  8659. (defun org-add-link-type (type &optional follow export)
  8660. "Add TYPE to the list of `org-link-types'.
  8661. Re-compute all regular expressions depending on `org-link-types'
  8662. FOLLOW and EXPORT are two functions.
  8663. FOLLOW should take the link path as the single argument and do whatever
  8664. is necessary to follow the link, for example find a file or display
  8665. a mail message.
  8666. EXPORT should format the link path for export to one of the export formats.
  8667. It should be a function accepting three arguments:
  8668. path the path of the link, the text after the prefix (like \"http:\")
  8669. desc the description of the link, if any
  8670. format the export format, a symbol like `html' or `latex' or `ascii'.
  8671. The function may use the FORMAT information to return different values
  8672. depending on the format. The return value will be put literally into
  8673. the exported file. If the return value is nil, this means Org should
  8674. do what it normally does with links which do not have EXPORT defined.
  8675. Org mode has a built-in default for exporting links. If you are happy with
  8676. this default, there is no need to define an export function for the link
  8677. type. For a simple example of an export function, see `org-bbdb.el'."
  8678. (add-to-list 'org-link-types type t)
  8679. (org-make-link-regexps)
  8680. (org-element-update-syntax)
  8681. (if (assoc type org-link-protocols)
  8682. (setcdr (assoc type org-link-protocols) (list follow export))
  8683. (push (list type follow export) org-link-protocols)))
  8684. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8685. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8686. ;;;###autoload
  8687. (defun org-store-link (arg)
  8688. "\\<org-mode-map>Store an org-link to the current location.
  8689. This link is added to `org-stored-links' and can later be inserted
  8690. into an org-buffer with \\[org-insert-link].
  8691. For some link types, a prefix arg is interpreted.
  8692. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8693. For file links, arg negates `org-context-in-file-links'.
  8694. A double prefix arg force skipping storing functions that are not
  8695. part of Org's core.
  8696. A triple prefix arg force storing a link for each line in the
  8697. active region."
  8698. (interactive "P")
  8699. (org-load-modules-maybe)
  8700. (if (and (equal arg '(64)) (org-region-active-p))
  8701. (save-excursion
  8702. (let ((end (region-end)))
  8703. (goto-char (region-beginning))
  8704. (set-mark (point))
  8705. (while (< (point-at-eol) end)
  8706. (move-end-of-line 1) (activate-mark)
  8707. (let (current-prefix-arg)
  8708. (call-interactively 'org-store-link))
  8709. (move-beginning-of-line 2)
  8710. (set-mark (point)))))
  8711. (org-with-limited-levels
  8712. (setq org-store-link-plist nil)
  8713. (let (link cpltxt desc description search
  8714. txt custom-id agenda-link sfuns sfunsn)
  8715. (cond
  8716. ;; Store a link using an external link type
  8717. ((and (not (equal arg '(16)))
  8718. (setq sfuns
  8719. (delq
  8720. nil (mapcar (lambda (f)
  8721. (let (fs) (if (funcall f) (push f fs))))
  8722. org-store-link-functions))
  8723. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8724. (or (and (cdr sfuns)
  8725. (funcall (intern
  8726. (completing-read
  8727. "Which function for creating the link? "
  8728. sfunsn nil t (car sfunsn)))))
  8729. (funcall (caar sfuns)))
  8730. (setq link (plist-get org-store-link-plist :link)
  8731. desc (or (plist-get org-store-link-plist
  8732. :description) link))))
  8733. ;; Store a link from a source code buffer
  8734. ((org-src-edit-buffer-p)
  8735. (let (label gc)
  8736. (while (or (not label)
  8737. (save-excursion
  8738. (save-restriction
  8739. (widen)
  8740. (goto-char (point-min))
  8741. (re-search-forward
  8742. (regexp-quote (format org-coderef-label-format label))
  8743. nil t))))
  8744. (when label (message "Label exists already") (sit-for 2))
  8745. (setq label (read-string "Code line label: " label)))
  8746. (end-of-line 1)
  8747. (setq link (format org-coderef-label-format label))
  8748. (setq gc (- 79 (length link)))
  8749. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8750. (insert link)
  8751. (setq link (concat "(" label ")") desc nil)))
  8752. ;; We are in the agenda, link to referenced location
  8753. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8754. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8755. (get-text-property (point) 'org-marker))))
  8756. (when m
  8757. (org-with-point-at m
  8758. (setq agenda-link
  8759. (if (org-called-interactively-p 'any)
  8760. (call-interactively 'org-store-link)
  8761. (org-store-link nil)))))))
  8762. ((eq major-mode 'calendar-mode)
  8763. (let ((cd (calendar-cursor-to-date)))
  8764. (setq link
  8765. (format-time-string
  8766. (car org-time-stamp-formats)
  8767. (apply 'encode-time
  8768. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8769. nil nil nil))))
  8770. (org-store-link-props :type "calendar" :date cd)))
  8771. ((eq major-mode 'help-mode)
  8772. (setq link (concat "help:" (save-excursion
  8773. (goto-char (point-min))
  8774. (looking-at "^[^ ]+")
  8775. (match-string 0))))
  8776. (org-store-link-props :type "help"))
  8777. ((eq major-mode 'w3-mode)
  8778. (setq cpltxt (if (and (buffer-name)
  8779. (not (string-match "Untitled" (buffer-name))))
  8780. (buffer-name)
  8781. (url-view-url t))
  8782. link (url-view-url t))
  8783. (org-store-link-props :type "w3" :url (url-view-url t)))
  8784. ((eq major-mode 'image-mode)
  8785. (setq cpltxt (concat "file:"
  8786. (abbreviate-file-name buffer-file-name))
  8787. link cpltxt)
  8788. (org-store-link-props :type "image" :file buffer-file-name))
  8789. ;; In dired, store a link to the file of the current line
  8790. ((derived-mode-p 'dired-mode)
  8791. (let ((file (dired-get-filename nil t)))
  8792. (setq file (if file
  8793. (abbreviate-file-name
  8794. (expand-file-name (dired-get-filename nil t)))
  8795. ;; otherwise, no file so use current directory.
  8796. default-directory))
  8797. (setq cpltxt (concat "file:" file)
  8798. link cpltxt)))
  8799. ((setq search (run-hook-with-args-until-success
  8800. 'org-create-file-search-functions))
  8801. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8802. "::" search))
  8803. (setq cpltxt (or description link)))
  8804. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8805. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8806. (cond
  8807. ;; Store a link using the target at point
  8808. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8809. (setq cpltxt
  8810. (concat "file:"
  8811. (abbreviate-file-name
  8812. (buffer-file-name (buffer-base-buffer)))
  8813. "::" (match-string 1))
  8814. link cpltxt))
  8815. ((and (featurep 'org-id)
  8816. (or (eq org-id-link-to-org-use-id t)
  8817. (and (org-called-interactively-p 'any)
  8818. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8819. (and (eq org-id-link-to-org-use-id
  8820. 'create-if-interactive-and-no-custom-id)
  8821. (not custom-id))))
  8822. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8823. ;; Store a link using the ID at point
  8824. (setq link (condition-case nil
  8825. (prog1 (org-id-store-link)
  8826. (setq desc (or (plist-get org-store-link-plist
  8827. :description)
  8828. "")))
  8829. (error
  8830. ;; Probably before first headline, link only to file
  8831. (concat "file:"
  8832. (abbreviate-file-name
  8833. (buffer-file-name (buffer-base-buffer))))))))
  8834. (t
  8835. ;; Just link to current headline
  8836. (setq cpltxt (concat "file:"
  8837. (abbreviate-file-name
  8838. (buffer-file-name (buffer-base-buffer)))))
  8839. ;; Add a context search string
  8840. (when (org-xor org-context-in-file-links arg)
  8841. (let* ((ee (org-element-at-point))
  8842. (et (org-element-type ee))
  8843. (ev (plist-get (cadr ee) :value))
  8844. (ek (plist-get (cadr ee) :key))
  8845. (eok (and (stringp ek) (string-match "name" ek))))
  8846. (setq txt (cond
  8847. ((org-at-heading-p) nil)
  8848. ((and (eq et 'keyword) eok) ev)
  8849. ((org-region-active-p)
  8850. (buffer-substring (region-beginning) (region-end)))))
  8851. (when (or (null txt) (string-match "\\S-" txt))
  8852. (setq cpltxt
  8853. (concat cpltxt "::"
  8854. (condition-case nil
  8855. (org-make-org-heading-search-string txt)
  8856. (error "")))
  8857. desc (or (and (eq et 'keyword) eok ev)
  8858. (nth 4 (ignore-errors (org-heading-components)))
  8859. "NONE")))))
  8860. (if (string-match "::\\'" cpltxt)
  8861. (setq cpltxt (substring cpltxt 0 -2)))
  8862. (setq link cpltxt))))
  8863. ((buffer-file-name (buffer-base-buffer))
  8864. ;; Just link to this file here.
  8865. (setq cpltxt (concat "file:"
  8866. (abbreviate-file-name
  8867. (buffer-file-name (buffer-base-buffer)))))
  8868. ;; Add a context string.
  8869. (when (org-xor org-context-in-file-links arg)
  8870. (setq txt (if (org-region-active-p)
  8871. (buffer-substring (region-beginning) (region-end))
  8872. (buffer-substring (point-at-bol) (point-at-eol))))
  8873. ;; Only use search option if there is some text.
  8874. (when (string-match "\\S-" txt)
  8875. (setq cpltxt
  8876. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8877. desc "NONE")))
  8878. (setq link cpltxt))
  8879. ((org-called-interactively-p 'interactive)
  8880. (user-error "No method for storing a link from this buffer"))
  8881. (t (setq link nil)))
  8882. ;; We're done setting link and desc, clean up
  8883. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8884. (setq link (or link cpltxt)
  8885. desc (or desc cpltxt))
  8886. (cond ((equal desc "NONE") (setq desc nil))
  8887. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8888. (let ((d0 (match-string 3 desc))
  8889. (p0 (match-string 5 desc)))
  8890. (setq desc
  8891. (replace-regexp-in-string
  8892. org-bracket-link-regexp
  8893. (concat (or p0 d0)
  8894. (if (equal (length (match-string 0 desc))
  8895. (length desc)) "*" "")) desc)))))
  8896. ;; Return the link
  8897. (if (not (and (or (org-called-interactively-p 'any)
  8898. executing-kbd-macro) link))
  8899. (or agenda-link (and link (org-make-link-string link desc)))
  8900. (push (list link desc) org-stored-links)
  8901. (message "Stored: %s" (or desc link))
  8902. (when custom-id
  8903. (setq link (concat "file:" (abbreviate-file-name
  8904. (buffer-file-name)) "::#" custom-id))
  8905. (push (list link desc) org-stored-links))
  8906. (car org-stored-links))))))
  8907. (defun org-store-link-props (&rest plist)
  8908. "Store link properties, extract names and addresses."
  8909. (let (x adr)
  8910. (when (setq x (plist-get plist :from))
  8911. (setq adr (mail-extract-address-components x))
  8912. (setq plist (plist-put plist :fromname (car adr)))
  8913. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8914. (when (setq x (plist-get plist :to))
  8915. (setq adr (mail-extract-address-components x))
  8916. (setq plist (plist-put plist :toname (car adr)))
  8917. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8918. (let ((from (plist-get plist :from))
  8919. (to (plist-get plist :to)))
  8920. (when (and from to org-from-is-user-regexp)
  8921. (setq plist
  8922. (plist-put plist :fromto
  8923. (if (string-match org-from-is-user-regexp from)
  8924. (concat "to %t")
  8925. (concat "from %f"))))))
  8926. (setq org-store-link-plist plist))
  8927. (defun org-add-link-props (&rest plist)
  8928. "Add these properties to the link property list."
  8929. (let (key value)
  8930. (while plist
  8931. (setq key (pop plist) value (pop plist))
  8932. (setq org-store-link-plist
  8933. (plist-put org-store-link-plist key value)))))
  8934. (defun org-email-link-description (&optional fmt)
  8935. "Return the description part of an email link.
  8936. This takes information from `org-store-link-plist' and formats it
  8937. according to FMT (default from `org-email-link-description-format')."
  8938. (setq fmt (or fmt org-email-link-description-format))
  8939. (let* ((p org-store-link-plist)
  8940. (to (plist-get p :toaddress))
  8941. (from (plist-get p :fromaddress))
  8942. (table
  8943. (list
  8944. (cons "%c" (plist-get p :fromto))
  8945. (cons "%F" (plist-get p :from))
  8946. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8947. (cons "%T" (plist-get p :to))
  8948. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8949. (cons "%s" (plist-get p :subject))
  8950. (cons "%d" (plist-get p :date))
  8951. (cons "%m" (plist-get p :message-id)))))
  8952. (when (string-match "%c" fmt)
  8953. ;; Check if the user wrote this message
  8954. (if (and org-from-is-user-regexp from to
  8955. (save-match-data (string-match org-from-is-user-regexp from)))
  8956. (setq fmt (replace-match "to %t" t t fmt))
  8957. (setq fmt (replace-match "from %f" t t fmt))))
  8958. (org-replace-escapes fmt table)))
  8959. (defun org-make-org-heading-search-string (&optional string)
  8960. "Make search string for the current headline or STRING."
  8961. (let ((s (or string
  8962. (and (derived-mode-p 'org-mode)
  8963. (save-excursion
  8964. (org-back-to-heading t)
  8965. (org-element-property :raw-value (org-element-at-point))))))
  8966. (lines org-context-in-file-links))
  8967. (or string (setq s (concat "*" s))) ; Add * for headlines
  8968. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8969. (when (and string (integerp lines) (> lines 0))
  8970. (let ((slines (org-split-string s "\n")))
  8971. (when (< lines (length slines))
  8972. (setq s (mapconcat
  8973. 'identity
  8974. (reverse (nthcdr (- (length slines) lines)
  8975. (reverse slines))) "\n")))))
  8976. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8977. (defun org-make-link-string (link &optional description)
  8978. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8979. (unless (string-match "\\S-" link)
  8980. (error "Empty link"))
  8981. (when (and description
  8982. (stringp description)
  8983. (not (string-match "\\S-" description)))
  8984. (setq description nil))
  8985. (when (stringp description)
  8986. ;; Remove brackets from the description, they are fatal.
  8987. (while (string-match "\\[" description)
  8988. (setq description (replace-match "{" t t description)))
  8989. (while (string-match "\\]" description)
  8990. (setq description (replace-match "}" t t description))))
  8991. (when (equal link description)
  8992. ;; No description needed, it is identical
  8993. (setq description nil))
  8994. (when (and (not description)
  8995. (not (string-match (org-image-file-name-regexp) link))
  8996. (not (equal link (org-link-escape link))))
  8997. (setq description (org-extract-attributes link)))
  8998. (setq link
  8999. (cond ((string-match (org-image-file-name-regexp) link) link)
  9000. ((string-match org-link-types-re link)
  9001. (concat (match-string 1 link)
  9002. (org-link-escape (substring link (match-end 1)))))
  9003. (t (org-link-escape link))))
  9004. (concat "[[" link "]"
  9005. (if description (concat "[" description "]") "")
  9006. "]"))
  9007. (defconst org-link-escape-chars
  9008. ;;%20 %5B %5D
  9009. '(?\ ?\[ ?\])
  9010. "List of characters that should be escaped in a link when stored to Org.
  9011. This is the list that is used for internal purposes.")
  9012. (defconst org-link-escape-chars-browser
  9013. ;;%20 %22
  9014. '(?\ ?\")
  9015. "List of characters to be escaped before handing over to the browser.
  9016. If you consider using this constant then you probably want to use
  9017. the function `org-link-escape-browser' instead. See there why
  9018. this constant is a candidate to be removed once Org drops support
  9019. for Emacs 24.1 and 24.2.")
  9020. (defun org-link-escape (text &optional table merge)
  9021. "Return percent escaped representation of TEXT.
  9022. TEXT is a string with the text to escape.
  9023. Optional argument TABLE is a list with characters that should be
  9024. escaped. When nil, `org-link-escape-chars' is used.
  9025. If optional argument MERGE is set, merge TABLE into
  9026. `org-link-escape-chars'."
  9027. ;; Don't escape chars in internal links
  9028. (if (string-match "^\\*[[:alnum:]]+" text)
  9029. text
  9030. (cond
  9031. ((and table merge)
  9032. (mapc (lambda (defchr)
  9033. (unless (member defchr table)
  9034. (setq table (cons defchr table))))
  9035. org-link-escape-chars))
  9036. ((null table)
  9037. (setq table org-link-escape-chars)))
  9038. (mapconcat
  9039. (lambda (char)
  9040. (if (or (member char table)
  9041. (and (or (< char 32) (= char ?\%) (> char 126))
  9042. org-url-hexify-p))
  9043. (mapconcat (lambda (sequence-element)
  9044. (format "%%%.2X" sequence-element))
  9045. (or (encode-coding-char char 'utf-8)
  9046. (error "Unable to percent escape character: %s"
  9047. (char-to-string char))) "")
  9048. (char-to-string char))) text "")))
  9049. (defun org-link-escape-browser (text)
  9050. "Escape some characters before handing over to the browser.
  9051. This function is a candidate to be removed together with the
  9052. constant `org-link-escape-chars-browser' once Org drops support
  9053. for Emacs 24.1 and 24.2. All calls to this function will have to
  9054. be replaced with `url-encode-url' which is available since Emacs
  9055. 24.3.1."
  9056. ;; Example with the Org link
  9057. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9058. ;; to open the browser with +subject:"Release 8.2" filled into the
  9059. ;; query field: In this case the variable TEXT contains the
  9060. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9061. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9062. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9063. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9064. (if (fboundp 'url-encode-url)
  9065. (url-encode-url text)
  9066. (if (org-string-match-p
  9067. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9068. text)
  9069. (org-link-escape text org-link-escape-chars-browser)
  9070. text)))
  9071. (defun org-link-unescape (str)
  9072. "Unhex hexified Unicode strings as returned from the JavaScript function
  9073. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  9074. (unless (and (null str) (string= "" str))
  9075. (let ((pos 0) (case-fold-search t) unhexed)
  9076. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  9077. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  9078. (setq str (replace-match unhexed t t str))
  9079. (setq pos (+ pos (length unhexed))))))
  9080. str)
  9081. (defun org-link-unescape-compound (hex)
  9082. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9083. Note: this function also decodes single byte encodings like
  9084. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9085. (save-match-data
  9086. (let* ((bytes (cdr (split-string hex "%")))
  9087. (ret "")
  9088. (eat 0)
  9089. (sum 0))
  9090. (while bytes
  9091. (let* ((val (string-to-number (pop bytes) 16))
  9092. (shift-xor
  9093. (if (= 0 eat)
  9094. (cond
  9095. ((>= val 252) (cons 6 252))
  9096. ((>= val 248) (cons 5 248))
  9097. ((>= val 240) (cons 4 240))
  9098. ((>= val 224) (cons 3 224))
  9099. ((>= val 192) (cons 2 192))
  9100. (t (cons 0 0)))
  9101. (cons 6 128))))
  9102. (if (>= val 192) (setq eat (car shift-xor)))
  9103. (setq val (logxor val (cdr shift-xor)))
  9104. (setq sum (+ (lsh sum (car shift-xor)) val))
  9105. (if (> eat 0) (setq eat (- eat 1)))
  9106. (cond
  9107. ((= 0 eat) ;multi byte
  9108. (setq ret (concat ret (org-char-to-string sum)))
  9109. (setq sum 0))
  9110. ((not bytes) ; single byte(s)
  9111. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9112. ret)))
  9113. (defun org-link-unescape-single-byte-sequence (hex)
  9114. "Unhexify hex-encoded single byte character sequences."
  9115. (mapconcat (lambda (byte)
  9116. (char-to-string (string-to-number byte 16)))
  9117. (cdr (split-string hex "%")) ""))
  9118. (defun org-xor (a b)
  9119. "Exclusive or."
  9120. (if a (not b) b))
  9121. (defun org-fixup-message-id-for-http (s)
  9122. "Replace special characters in a message id, so it can be used in an http query."
  9123. (when (string-match "%" s)
  9124. (setq s (mapconcat (lambda (c)
  9125. (if (eq c ?%)
  9126. "%25"
  9127. (char-to-string c)))
  9128. s "")))
  9129. (while (string-match "<" s)
  9130. (setq s (replace-match "%3C" t t s)))
  9131. (while (string-match ">" s)
  9132. (setq s (replace-match "%3E" t t s)))
  9133. (while (string-match "@" s)
  9134. (setq s (replace-match "%40" t t s)))
  9135. s)
  9136. (defun org-link-prettify (link)
  9137. "Return a human-readable representation of LINK.
  9138. The car of LINK must be a raw link.
  9139. The cdr of LINK must be either a link description or nil."
  9140. (let ((desc (or (cadr link) "<no description>")))
  9141. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9142. "<" (car link) ">")))
  9143. ;;;###autoload
  9144. (defun org-insert-link-global ()
  9145. "Insert a link like Org-mode does.
  9146. This command can be called in any mode to insert a link in Org-mode syntax."
  9147. (interactive)
  9148. (org-load-modules-maybe)
  9149. (org-run-like-in-org-mode 'org-insert-link))
  9150. (defun org-insert-all-links (arg &optional pre post)
  9151. "Insert all links in `org-stored-links'.
  9152. When a universal prefix, do not delete the links from `org-stored-links'.
  9153. When `ARG' is a number, insert the last N link(s).
  9154. `PRE' and `POST' are optional arguments to define a string to
  9155. prepend or to append."
  9156. (interactive "P")
  9157. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9158. (links (copy-seq org-stored-links))
  9159. (pr (or pre "- "))
  9160. (po (or post "\n"))
  9161. (cnt 1) l)
  9162. (if (null org-stored-links)
  9163. (message "No link to insert")
  9164. (while (and (or (listp arg) (>= arg cnt))
  9165. (setq l (if (listp arg)
  9166. (pop links)
  9167. (pop org-stored-links))))
  9168. (setq cnt (1+ cnt))
  9169. (insert pr)
  9170. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9171. (insert po)))))
  9172. (defun org-insert-last-stored-link (arg)
  9173. "Insert the last link stored in `org-stored-links'."
  9174. (interactive "p")
  9175. (org-insert-all-links arg "" "\n"))
  9176. (defun org-link-fontify-links-to-this-file ()
  9177. "Fontify links to the current file in `org-stored-links'."
  9178. (let ((f (buffer-file-name)) a b)
  9179. (setq a (mapcar (lambda(l)
  9180. (let ((ll (car l)))
  9181. (when (and (string-match "^file:\\(.+\\)::" ll)
  9182. (equal f (expand-file-name (match-string 1 ll))))
  9183. ll)))
  9184. org-stored-links))
  9185. (when (featurep 'org-id)
  9186. (setq b (mapcar (lambda(l)
  9187. (let ((ll (car l)))
  9188. (when (and (string-match "^id:\\(.+\\)$" ll)
  9189. (equal f (expand-file-name
  9190. (or (org-id-find-id-file
  9191. (match-string 1 ll)) ""))))
  9192. ll)))
  9193. org-stored-links)))
  9194. (mapcar (lambda(l)
  9195. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9196. (delq nil (append a b)))))
  9197. (defvar org-link-links-in-this-file nil)
  9198. (defun org-insert-link (&optional complete-file link-location default-description)
  9199. "Insert a link. At the prompt, enter the link.
  9200. Completion can be used to insert any of the link protocol prefixes like
  9201. http or ftp in use.
  9202. The history can be used to select a link previously stored with
  9203. `org-store-link'. When the empty string is entered (i.e. if you just
  9204. press RET at the prompt), the link defaults to the most recently
  9205. stored link. As SPC triggers completion in the minibuffer, you need to
  9206. use M-SPC or C-q SPC to force the insertion of a space character.
  9207. You will also be prompted for a description, and if one is given, it will
  9208. be displayed in the buffer instead of the link.
  9209. If there is already a link at point, this command will allow you to edit link
  9210. and description parts.
  9211. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9212. be selected using completion. The path to the file will be relative to the
  9213. current directory if the file is in the current directory or a subdirectory.
  9214. Otherwise, the link will be the absolute path as completed in the minibuffer
  9215. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9216. option `org-link-file-path-type'.
  9217. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9218. the current directory or below.
  9219. With three \\[universal-argument] prefixes, negate the meaning of
  9220. `org-keep-stored-link-after-insertion'.
  9221. If `org-make-link-description-function' is non-nil, this function will be
  9222. called with the link target, and the result will be the default
  9223. link description.
  9224. If the LINK-LOCATION parameter is non-nil, this value will be
  9225. used as the link location instead of reading one interactively.
  9226. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9227. be used as the default description."
  9228. (interactive "P")
  9229. (let* ((wcf (current-window-configuration))
  9230. (origbuf (current-buffer))
  9231. (region (if (org-region-active-p)
  9232. (buffer-substring (region-beginning) (region-end))))
  9233. (remove (and region (list (region-beginning) (region-end))))
  9234. (desc region)
  9235. tmphist ; byte-compile incorrectly complains about this
  9236. (link link-location)
  9237. (abbrevs org-link-abbrev-alist-local)
  9238. entry file all-prefixes auto-desc)
  9239. (cond
  9240. (link-location) ; specified by arg, just use it.
  9241. ((org-in-regexp org-bracket-link-regexp 1)
  9242. ;; We do have a link at point, and we are going to edit it.
  9243. (setq remove (list (match-beginning 0) (match-end 0)))
  9244. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9245. (setq link (read-string "Link: "
  9246. (org-link-unescape
  9247. (org-match-string-no-properties 1)))))
  9248. ((or (org-in-regexp org-angle-link-re)
  9249. (org-in-regexp org-plain-link-re))
  9250. ;; Convert to bracket link
  9251. (setq remove (list (match-beginning 0) (match-end 0))
  9252. link (read-string "Link: "
  9253. (org-remove-angle-brackets (match-string 0)))))
  9254. ((member complete-file '((4) (16)))
  9255. ;; Completing read for file names.
  9256. (setq link (org-file-complete-link complete-file)))
  9257. (t
  9258. ;; Read link, with completion for stored links.
  9259. (org-link-fontify-links-to-this-file)
  9260. (org-switch-to-buffer-other-window "*Org Links*")
  9261. (with-current-buffer "*Org Links*"
  9262. (erase-buffer)
  9263. (insert "Insert a link.
  9264. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9265. (when org-stored-links
  9266. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9267. (insert (mapconcat 'org-link-prettify
  9268. (reverse org-stored-links) "\n")))
  9269. (goto-char (point-min)))
  9270. (let ((cw (selected-window)))
  9271. (select-window (get-buffer-window "*Org Links*" 'visible))
  9272. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9273. (unless (pos-visible-in-window-p (point-max))
  9274. (org-fit-window-to-buffer))
  9275. (and (window-live-p cw) (select-window cw)))
  9276. ;; Fake a link history, containing the stored links.
  9277. (setq tmphist (append (mapcar 'car org-stored-links)
  9278. org-insert-link-history))
  9279. (setq all-prefixes (append (mapcar 'car abbrevs)
  9280. (mapcar 'car org-link-abbrev-alist)
  9281. org-link-types))
  9282. (unwind-protect
  9283. (progn
  9284. (setq link
  9285. (org-completing-read
  9286. "Link: "
  9287. (append
  9288. (mapcar (lambda (x) (concat x ":"))
  9289. all-prefixes)
  9290. (mapcar 'car org-stored-links))
  9291. nil nil nil
  9292. 'tmphist
  9293. (caar org-stored-links)))
  9294. (if (not (string-match "\\S-" link))
  9295. (user-error "No link selected"))
  9296. (mapc (lambda(l)
  9297. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9298. org-stored-links)
  9299. (if (or (member link all-prefixes)
  9300. (and (equal ":" (substring link -1))
  9301. (member (substring link 0 -1) all-prefixes)
  9302. (setq link (substring link 0 -1))))
  9303. (setq link (with-current-buffer origbuf
  9304. (org-link-try-special-completion link)))))
  9305. (set-window-configuration wcf)
  9306. (kill-buffer "*Org Links*"))
  9307. (setq entry (assoc link org-stored-links))
  9308. (or entry (push link org-insert-link-history))
  9309. (setq desc (or desc (nth 1 entry)))))
  9310. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9311. (not org-keep-stored-link-after-insertion))
  9312. (setq org-stored-links (delq (assoc link org-stored-links)
  9313. org-stored-links)))
  9314. (if (and (string-match org-plain-link-re link)
  9315. (not (string-match org-ts-regexp link)))
  9316. ;; URL-like link, normalize the use of angular brackets.
  9317. (setq link (org-remove-angle-brackets link)))
  9318. ;; Check if we are linking to the current file with a search
  9319. ;; option If yes, simplify the link by using only the search
  9320. ;; option.
  9321. (when (and buffer-file-name
  9322. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9323. (let* ((path (match-string 1 link))
  9324. (case-fold-search nil)
  9325. (search (match-string 2 link)))
  9326. (save-match-data
  9327. (if (equal (file-truename buffer-file-name) (file-truename path))
  9328. ;; We are linking to this same file, with a search option
  9329. (setq link search)))))
  9330. ;; Check if we can/should use a relative path. If yes, simplify the link
  9331. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9332. (let* ((type (match-string 1 link))
  9333. (path (match-string 2 link))
  9334. (origpath path)
  9335. (case-fold-search nil))
  9336. (cond
  9337. ((or (eq org-link-file-path-type 'absolute)
  9338. (equal complete-file '(16)))
  9339. (setq path (abbreviate-file-name (expand-file-name path))))
  9340. ((eq org-link-file-path-type 'noabbrev)
  9341. (setq path (expand-file-name path)))
  9342. ((eq org-link-file-path-type 'relative)
  9343. (setq path (file-relative-name path)))
  9344. (t
  9345. (save-match-data
  9346. (if (string-match (concat "^" (regexp-quote
  9347. (expand-file-name
  9348. (file-name-as-directory
  9349. default-directory))))
  9350. (expand-file-name path))
  9351. ;; We are linking a file with relative path name.
  9352. (setq path (substring (expand-file-name path)
  9353. (match-end 0)))
  9354. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9355. (setq link (concat type path))
  9356. (if (equal desc origpath)
  9357. (setq desc path))))
  9358. (if org-make-link-description-function
  9359. (setq desc
  9360. (or (condition-case nil
  9361. (funcall org-make-link-description-function link desc)
  9362. (error (progn (message "Can't get link description from `%s'"
  9363. (symbol-name org-make-link-description-function))
  9364. (sit-for 2) nil)))
  9365. (read-string "Description: " default-description)))
  9366. (if default-description (setq desc default-description)
  9367. (setq desc (or (and auto-desc desc)
  9368. (read-string "Description: " desc)))))
  9369. (unless (string-match "\\S-" desc) (setq desc nil))
  9370. (if remove (apply 'delete-region remove))
  9371. (insert (org-make-link-string link desc))))
  9372. (defun org-link-try-special-completion (type)
  9373. "If there is completion support for link type TYPE, offer it."
  9374. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9375. (if (functionp fun)
  9376. (funcall fun)
  9377. (read-string "Link (no completion support): " (concat type ":")))))
  9378. (defun org-file-complete-link (&optional arg)
  9379. "Create a file link using completion."
  9380. (let ((file (org-iread-file-name "File: "))
  9381. (pwd (file-name-as-directory (expand-file-name ".")))
  9382. (pwd1 (file-name-as-directory (abbreviate-file-name
  9383. (expand-file-name ".")))))
  9384. (cond ((equal arg '(16))
  9385. (concat "file:"
  9386. (abbreviate-file-name (expand-file-name file))))
  9387. ((string-match
  9388. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9389. (concat "file:" (match-string 1 file)))
  9390. ((string-match
  9391. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9392. (expand-file-name file))
  9393. (concat "file:"
  9394. (match-string 1 (expand-file-name file))))
  9395. (t (concat "file:" file)))))
  9396. (defun org-iread-file-name (&rest args)
  9397. "Read-file-name using `ido-mode' speedup if available.
  9398. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9399. See `read-file-name' for a description of parameters."
  9400. (org-without-partial-completion
  9401. (if (and org-completion-use-ido
  9402. (fboundp 'ido-read-file-name)
  9403. (org-bound-and-true-p ido-mode)
  9404. (listp (nth 1 args)))
  9405. (let ((ido-enter-matching-directory nil))
  9406. (apply #'ido-read-file-name args))
  9407. (apply #'read-file-name args))))
  9408. (defun org-completing-read (&rest args)
  9409. "Completing-read with SPACE being a normal character."
  9410. (let ((enable-recursive-minibuffers t)
  9411. (minibuffer-local-completion-map
  9412. (copy-keymap minibuffer-local-completion-map)))
  9413. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9414. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9415. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9416. (apply 'org-icompleting-read args)))
  9417. (defun org-completing-read-no-i (&rest args)
  9418. (let (org-completion-use-ido org-completion-use-iswitchb)
  9419. (apply 'org-completing-read args)))
  9420. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9421. "Use iswitch as a completing-read replacement to choose from choices.
  9422. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9423. from."
  9424. (let* ((iswitchb-use-virtual-buffers nil)
  9425. (iswitchb-make-buflist-hook
  9426. (lambda ()
  9427. (setq iswitchb-temp-buflist choices))))
  9428. (iswitchb-read-buffer prompt)))
  9429. (defun org-icompleting-read (&rest args)
  9430. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9431. Should be called like `completing-read'."
  9432. (org-without-partial-completion
  9433. (if (not (listp (nth 1 args)))
  9434. ;; Ido only supports lists as the COLLECTION argument. Use
  9435. ;; default completion function when second argument is not
  9436. ;; a list.
  9437. (apply #'completing-read args)
  9438. (let ((ido-enter-matching-directory nil))
  9439. (apply (cond ((and org-completion-use-ido
  9440. (fboundp 'ido-completing-read)
  9441. (org-bound-and-true-p ido-mode))
  9442. #'ido-completing-read)
  9443. ((and org-completion-use-iswitchb
  9444. (org-bound-and-true-p iswitchb-mode))
  9445. #'org-iswitchb-completing-read)
  9446. (t #'completing-read))
  9447. (pop args)
  9448. (if (org-some #'consp (car args))
  9449. (mapcar #'car (pop args))
  9450. (pop args))
  9451. args)))))
  9452. (defun org-extract-attributes (s)
  9453. "Extract the attributes cookie from a string and set as text property."
  9454. (let (a attr (start 0) key value)
  9455. (save-match-data
  9456. (when (string-match "{{\\([^}]+\\)}}$" s)
  9457. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9458. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9459. (setq key (match-string 1 a) value (match-string 2 a)
  9460. start (match-end 0)
  9461. attr (plist-put attr (intern key) value))))
  9462. (org-add-props s nil 'org-attr attr))
  9463. s))
  9464. ;;; Opening/following a link
  9465. (defvar org-link-search-failed nil)
  9466. (defvar org-open-link-functions nil
  9467. "Hook for functions finding a plain text link.
  9468. These functions must take a single argument, the link content.
  9469. They will be called for links that look like [[link text][description]]
  9470. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9471. like a filename (e.g. \"./blue.png\").
  9472. These functions will be called *before* Org attempts to resolve the
  9473. link by doing text searches in the current buffer - so if you want a
  9474. link \"[[target]]\" to still find \"<<target>>\", your function should
  9475. handle this as a special case.
  9476. When the function does handle the link, it must return a non-nil value.
  9477. If it decides that it is not responsible for this link, it must return
  9478. nil to indicate that that Org-mode can continue with other options
  9479. like exact and fuzzy text search.")
  9480. (defun org-next-link (&optional search-backward)
  9481. "Move forward to the next link.
  9482. If the link is in hidden text, expose it."
  9483. (interactive "P")
  9484. (when (and org-link-search-failed (eq this-command last-command))
  9485. (goto-char (point-min))
  9486. (message "Link search wrapped back to beginning of buffer"))
  9487. (setq org-link-search-failed nil)
  9488. (let* ((pos (point))
  9489. (ct (org-context))
  9490. (a (assoc :link ct))
  9491. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9492. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9493. ((looking-at org-any-link-re)
  9494. ;; Don't stay stuck at link without an org-link face
  9495. (forward-char (if search-backward -1 1))))
  9496. (if (funcall srch-fun org-any-link-re nil t)
  9497. (progn
  9498. (goto-char (match-beginning 0))
  9499. (if (outline-invisible-p) (org-show-context)))
  9500. (goto-char pos)
  9501. (setq org-link-search-failed t)
  9502. (message "No further link found"))))
  9503. (defun org-previous-link ()
  9504. "Move backward to the previous link.
  9505. If the link is in hidden text, expose it."
  9506. (interactive)
  9507. (funcall 'org-next-link t))
  9508. (defun org-translate-link (s)
  9509. "Translate a link string if a translation function has been defined."
  9510. (if (and org-link-translation-function
  9511. (fboundp org-link-translation-function)
  9512. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9513. (progn
  9514. (setq s (funcall org-link-translation-function
  9515. (match-string 1 s) (match-string 2 s)))
  9516. (concat (car s) ":" (cdr s)))
  9517. s))
  9518. (defun org-translate-link-from-planner (type path)
  9519. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9520. This is still an experimental function, your mileage may vary."
  9521. (cond
  9522. ((member type '("http" "https" "news" "ftp"))
  9523. ;; standard Internet links are the same.
  9524. nil)
  9525. ((and (equal type "irc") (string-match "^//" path))
  9526. ;; Planner has two / at the beginning of an irc link, we have 1.
  9527. ;; We should have zero, actually....
  9528. (setq path (substring path 1)))
  9529. ((and (equal type "lisp") (string-match "^/" path))
  9530. ;; Planner has a slash, we do not.
  9531. (setq type "elisp" path (substring path 1)))
  9532. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9533. ;; A typical message link. Planner has the id after the final slash,
  9534. ;; we separate it with a hash mark
  9535. (setq path (concat (match-string 1 path) "#"
  9536. (org-remove-angle-brackets (match-string 2 path))))))
  9537. (cons type path))
  9538. (defun org-find-file-at-mouse (ev)
  9539. "Open file link or URL at mouse."
  9540. (interactive "e")
  9541. (mouse-set-point ev)
  9542. (org-open-at-point 'in-emacs))
  9543. (defun org-open-at-mouse (ev)
  9544. "Open file link or URL at mouse.
  9545. See the docstring of `org-open-file' for details."
  9546. (interactive "e")
  9547. (mouse-set-point ev)
  9548. (if (eq major-mode 'org-agenda-mode)
  9549. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9550. (org-open-at-point))
  9551. (defvar org-window-config-before-follow-link nil
  9552. "The window configuration before following a link.
  9553. This is saved in case the need arises to restore it.")
  9554. (defvar org-open-link-marker (make-marker)
  9555. "Marker pointing to the location where `org-open-at-point' was called.")
  9556. ;;;###autoload
  9557. (defun org-open-at-point-global ()
  9558. "Follow a link like Org-mode does.
  9559. This command can be called in any mode to follow a link that has
  9560. Org-mode syntax."
  9561. (interactive)
  9562. (org-run-like-in-org-mode 'org-open-at-point))
  9563. ;;;###autoload
  9564. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9565. "Open a link in the string S, as if it was in Org-mode."
  9566. (interactive "sLink: \nP")
  9567. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9568. (with-temp-buffer
  9569. (let ((org-inhibit-startup (not reference-buffer)))
  9570. (org-mode)
  9571. (insert s)
  9572. (goto-char (point-min))
  9573. (when reference-buffer
  9574. (setq org-link-abbrev-alist-local
  9575. (with-current-buffer reference-buffer
  9576. org-link-abbrev-alist-local)))
  9577. (org-open-at-point arg reference-buffer)))))
  9578. (defvar org-open-at-point-functions nil
  9579. "Hook that is run when following a link at point.
  9580. Functions in this hook must return t if they identify and follow
  9581. a link at point. If they don't find anything interesting at point,
  9582. they must return nil.")
  9583. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9584. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9585. (defun org-open-at-point (&optional arg reference-buffer)
  9586. "Open link, timestamp, footnote or tags at point.
  9587. When point is on a link, follow it. Normally, files will be
  9588. opened by an appropriate application. If the optional prefix
  9589. argument ARG is non-nil, Emacs will visit the file. With
  9590. a double prefix argument, try to open outside of Emacs, in the
  9591. application the system uses for this file type.
  9592. When point is on a timestamp, open the agenda at the day
  9593. specified.
  9594. When point is a footnote definition, move to the first reference
  9595. found. If it is on a reference, move to the associated
  9596. definition.
  9597. When point is on a headline, display a list of every link in the
  9598. entry, so it is possible to pick one, or all, of them. If point
  9599. is on a tag, call `org-tags-view' instead.
  9600. When optional argument REFERENCE-BUFFER is non-nil, it should
  9601. specify a buffer from where the link search should happen. This
  9602. is used internally by `org-open-link-from-string'.
  9603. On top of syntactically correct links, this function will open
  9604. the link at point in comments or comment blocks and the first
  9605. link in a property drawer line."
  9606. (interactive "P")
  9607. ;; On a code block, open block's results.
  9608. (unless (call-interactively 'org-babel-open-src-block-result)
  9609. (org-load-modules-maybe)
  9610. (move-marker org-open-link-marker (point))
  9611. (setq org-window-config-before-follow-link (current-window-configuration))
  9612. (org-remove-occur-highlights nil nil t)
  9613. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9614. (let* ((context
  9615. ;; Only consider supported types, even if they are not
  9616. ;; the closest one.
  9617. (org-element-lineage
  9618. (org-element-context)
  9619. '(comment comment-block footnote-definition footnote-reference
  9620. headline inlinetask keyword link node-property
  9621. timestamp)
  9622. t))
  9623. (type (org-element-type context))
  9624. (value (org-element-property :value context)))
  9625. (cond
  9626. ((not context) (user-error "No link found"))
  9627. ;; Exception: open timestamps and links in properties
  9628. ;; drawers, keywords and comments.
  9629. ((memq type '(comment comment-block keyword node-property))
  9630. (cond ((org-in-regexp org-any-link-re)
  9631. (org-open-link-from-string (match-string-no-properties 0)))
  9632. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9633. (org-follow-timestamp-link))
  9634. (t (user-error "No link found"))))
  9635. ;; On a headline or an inlinetask, but not on a timestamp,
  9636. ;; a link, a footnote reference or on tags.
  9637. ((and (memq type '(headline inlinetask))
  9638. ;; Not on tags.
  9639. (progn (save-excursion (beginning-of-line)
  9640. (looking-at org-complex-heading-regexp))
  9641. (or (not (match-beginning 5))
  9642. (< (point) (match-beginning 5)))))
  9643. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9644. (links (car data))
  9645. (links-end (cdr data)))
  9646. (if links
  9647. (dolist (link (if (stringp links) (list links) links))
  9648. (search-forward link nil links-end)
  9649. (goto-char (match-beginning 0))
  9650. (org-open-at-point))
  9651. (require 'org-attach)
  9652. (org-attach-reveal 'if-exists))))
  9653. ;; Do nothing on white spaces after an object, unless point
  9654. ;; is right after it.
  9655. ((> (point)
  9656. (save-excursion
  9657. (goto-char (org-element-property :end context))
  9658. (skip-chars-backward " \t")
  9659. (point)))
  9660. (user-error "No link found"))
  9661. ((eq type 'timestamp) (org-follow-timestamp-link))
  9662. ;; On tags within a headline or an inlinetask.
  9663. ((and (memq type '(headline inlinetask))
  9664. (progn (save-excursion (beginning-of-line)
  9665. (looking-at org-complex-heading-regexp))
  9666. (and (match-beginning 5)
  9667. (>= (point) (match-beginning 5)))))
  9668. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9669. ((eq type 'link)
  9670. ;; When link is located within the description of another
  9671. ;; link (e.g., an inline image), always open the parent
  9672. ;; link.
  9673. (let*((link (let ((up (org-element-property :parent context)))
  9674. (if (eq (org-element-type up) 'link) up context)))
  9675. (type (org-element-property :type link))
  9676. (path (org-link-unescape (org-element-property :path link))))
  9677. ;; Switch back to REFERENCE-BUFFER needed when called in
  9678. ;; a temporary buffer through `org-open-link-from-string'.
  9679. (with-current-buffer (or reference-buffer (current-buffer))
  9680. (cond
  9681. ((equal type "file")
  9682. (if (string-match "[*?{]" (file-name-nondirectory path))
  9683. (dired path)
  9684. ;; Look into `org-link-protocols' in order to find
  9685. ;; a DEDICATED-FUNCTION to open file. The function
  9686. ;; will be applied on raw link instead of parsed
  9687. ;; link due to the limitation in `org-add-link-type'
  9688. ;; ("open" function called with a single argument).
  9689. ;; If no such function is found, fallback to
  9690. ;; `org-open-file'.
  9691. ;;
  9692. ;; Note : "file+emacs" and "file+sys" types are
  9693. ;; hard-coded in order to escape the previous
  9694. ;; limitation.
  9695. (let* ((option (org-element-property :search-option link))
  9696. (app (org-element-property :application link))
  9697. (dedicated-function
  9698. (nth 1 (assoc app org-link-protocols))))
  9699. (if dedicated-function
  9700. (funcall dedicated-function
  9701. (concat path
  9702. (and option (concat "::" option))))
  9703. (apply 'org-open-file
  9704. path
  9705. (cond (arg)
  9706. ((equal app "emacs") 'emacs)
  9707. ((equal app "sys") 'system))
  9708. (cond ((not option) nil)
  9709. ((org-string-match-p "\\`[0-9]+\\'" option)
  9710. (list (string-to-number option)))
  9711. (t (list nil
  9712. (org-link-unescape option)))))))))
  9713. ((assoc type org-link-protocols)
  9714. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9715. ((equal type "help")
  9716. (let ((f-or-v (intern path)))
  9717. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9718. ((boundp f-or-v) (describe-variable f-or-v))
  9719. (t (error "Not a known function or variable")))))
  9720. ((member type '("http" "https" "ftp" "mailto" "news"))
  9721. (browse-url (org-link-escape-browser (concat type ":" path))))
  9722. ((equal type "doi")
  9723. (browse-url
  9724. (org-link-escape-browser (concat org-doi-server-url path))))
  9725. ((equal type "message") (browse-url (concat type ":" path)))
  9726. ((equal type "shell")
  9727. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9728. (cmd path))
  9729. (if (or (and (org-string-nw-p
  9730. org-confirm-shell-link-not-regexp)
  9731. (string-match
  9732. org-confirm-shell-link-not-regexp cmd))
  9733. (not org-confirm-shell-link-function)
  9734. (funcall org-confirm-shell-link-function
  9735. (format "Execute \"%s\" in shell? "
  9736. (org-add-props cmd nil
  9737. 'face 'org-warning))))
  9738. (progn
  9739. (message "Executing %s" cmd)
  9740. (shell-command cmd buf)
  9741. (when (featurep 'midnight)
  9742. (setq clean-buffer-list-kill-buffer-names
  9743. (cons (buffer-name buf)
  9744. clean-buffer-list-kill-buffer-names))))
  9745. (user-error "Abort"))))
  9746. ((equal type "elisp")
  9747. (let ((cmd path))
  9748. (if (or (and (org-string-nw-p
  9749. org-confirm-elisp-link-not-regexp)
  9750. (org-string-match-p
  9751. org-confirm-elisp-link-not-regexp cmd))
  9752. (not org-confirm-elisp-link-function)
  9753. (funcall org-confirm-elisp-link-function
  9754. (format "Execute \"%s\" as elisp? "
  9755. (org-add-props cmd nil
  9756. 'face 'org-warning))))
  9757. (message "%s => %s" cmd
  9758. (if (eq (string-to-char cmd) ?\()
  9759. (eval (read cmd))
  9760. (call-interactively (read cmd))))
  9761. (user-error "Abort"))))
  9762. ((equal type "id")
  9763. (require 'ord-id)
  9764. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9765. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9766. (unless (run-hook-with-args-until-success
  9767. 'org-open-link-functions path)
  9768. (if (not arg) (org-mark-ring-push)
  9769. (switch-to-buffer-other-window
  9770. (org-get-buffer-for-internal-link (current-buffer))))
  9771. (let ((cmd `(org-link-search
  9772. ,(if (member type '("custom-id" "coderef"))
  9773. (org-element-property :raw-link link)
  9774. path)
  9775. ,(cond ((equal arg '(4)) 'occur)
  9776. ((equal arg '(16)) 'org-occur))
  9777. ,(org-element-property :begin link))))
  9778. (condition-case nil
  9779. (let ((org-link-search-inhibit-query t))
  9780. (eval cmd))
  9781. (error (progn (widen) (eval cmd)))))))
  9782. (t (browse-url-at-point))))))
  9783. ;; On a footnote reference or at a footnote definition's label.
  9784. ((or (eq type 'footnote-reference)
  9785. (and (eq type 'footnote-definition)
  9786. (save-excursion
  9787. ;; Do not validate action when point is on the
  9788. ;; spaces right after the footnote label, in
  9789. ;; order to be on par with behaviour on links.
  9790. (skip-chars-forward " \t")
  9791. (let ((begin
  9792. (org-element-property :contents-begin context)))
  9793. (if begin (< (point) begin)
  9794. (= (org-element-property :post-affiliated context)
  9795. (line-beginning-position)))))))
  9796. (org-footnote-action))
  9797. (t (user-error "No link found")))))
  9798. (move-marker org-open-link-marker nil)
  9799. (run-hook-with-args 'org-follow-link-hook)))
  9800. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9801. "Offer links in the current entry and return the selected link.
  9802. If there is only one link, return it.
  9803. If NTH is an integer, return the NTH link found.
  9804. If ZERO is a string, check also this string for a link, and if
  9805. there is one, return it."
  9806. (with-current-buffer buffer
  9807. (save-excursion
  9808. (save-restriction
  9809. (widen)
  9810. (goto-char marker)
  9811. (let ((cnt ?0)
  9812. (in-emacs (if (integerp nth) nil nth))
  9813. have-zero end links link c)
  9814. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9815. (push (match-string 0 zero) links)
  9816. (setq cnt (1- cnt) have-zero t))
  9817. (save-excursion
  9818. (org-back-to-heading t)
  9819. (setq end (save-excursion (outline-next-heading) (point)))
  9820. (while (re-search-forward org-any-link-re end t)
  9821. (push (match-string 0) links))
  9822. (setq links (org-uniquify (reverse links))))
  9823. (cond
  9824. ((null links)
  9825. (message "No links"))
  9826. ((equal (length links) 1)
  9827. (setq link (car links)))
  9828. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9829. (setq link (nth (if have-zero nth (1- nth)) links)))
  9830. (t ; we have to select a link
  9831. (save-excursion
  9832. (save-window-excursion
  9833. (delete-other-windows)
  9834. (with-output-to-temp-buffer "*Select Link*"
  9835. (mapc (lambda (l)
  9836. (if (not (string-match org-bracket-link-regexp l))
  9837. (princ (format "[%c] %s\n" (incf cnt)
  9838. (org-remove-angle-brackets l)))
  9839. (if (match-end 3)
  9840. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9841. (match-string 3 l) (match-string 1 l)))
  9842. (princ (format "[%c] %s\n" (incf cnt)
  9843. (match-string 1 l))))))
  9844. links))
  9845. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9846. (message "Select link to open, RET to open all:")
  9847. (setq c (read-char-exclusive))
  9848. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9849. (when (equal c ?q) (user-error "Abort"))
  9850. (if (equal c ?\C-m)
  9851. (setq link links)
  9852. (setq nth (- c ?0))
  9853. (if have-zero (setq nth (1+ nth)))
  9854. (unless (and (integerp nth) (>= (length links) nth))
  9855. (user-error "Invalid link selection"))
  9856. (setq link (nth (1- nth) links)))))
  9857. (cons link end))))))
  9858. ;; TODO: These functions are deprecated since `org-open-at-point'
  9859. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9860. (defun org-open-file-with-system (path)
  9861. "Open file at PATH using the system way of opening it."
  9862. (org-open-file path 'system))
  9863. (defun org-open-file-with-emacs (path)
  9864. "Open file at PATH in Emacs."
  9865. (org-open-file path 'emacs))
  9866. ;;; File search
  9867. (defvar org-create-file-search-functions nil
  9868. "List of functions to construct the right search string for a file link.
  9869. These functions are called in turn with point at the location to
  9870. which the link should point.
  9871. A function in the hook should first test if it would like to
  9872. handle this file type, for example by checking the `major-mode'
  9873. or the file extension. If it decides not to handle this file, it
  9874. should just return nil to give other functions a chance. If it
  9875. does handle the file, it must return the search string to be used
  9876. when following the link. The search string will be part of the
  9877. file link, given after a double colon, and `org-open-at-point'
  9878. will automatically search for it. If special measures must be
  9879. taken to make the search successful, another function should be
  9880. added to the companion hook `org-execute-file-search-functions',
  9881. which see.
  9882. A function in this hook may also use `setq' to set the variable
  9883. `description' to provide a suggestion for the descriptive text to
  9884. be used for this link when it gets inserted into an Org-mode
  9885. buffer with \\[org-insert-link].")
  9886. (defvar org-execute-file-search-functions nil
  9887. "List of functions to execute a file search triggered by a link.
  9888. Functions added to this hook must accept a single argument, the
  9889. search string that was part of the file link, the part after the
  9890. double colon. The function must first check if it would like to
  9891. handle this search, for example by checking the `major-mode' or
  9892. the file extension. If it decides not to handle this search, it
  9893. should just return nil to give other functions a chance. If it
  9894. does handle the search, it must return a non-nil value to keep
  9895. other functions from trying.
  9896. Each function can access the current prefix argument through the
  9897. variable `current-prefix-arg'. Note that a single prefix is used
  9898. to force opening a link in Emacs, so it may be good to only use a
  9899. numeric or double prefix to guide the search function.
  9900. In case this is needed, a function in this hook can also restore
  9901. the window configuration before `org-open-at-point' was called using:
  9902. (set-window-configuration org-window-config-before-follow-link)")
  9903. (defun org-link-search (s &optional type avoid-pos stealth)
  9904. "Search for a link search option.
  9905. If S is surrounded by forward slashes, it is interpreted as a
  9906. regular expression. In org-mode files, this will create an `org-occur'
  9907. sparse tree. In ordinary files, `occur' will be used to list matches.
  9908. If the current buffer is in `dired-mode', grep will be used to search
  9909. in all files. If AVOID-POS is given, ignore matches near that position.
  9910. When optional argument STEALTH is non-nil, do not modify
  9911. visibility around point, thus ignoring `org-show-context-detail'
  9912. variable."
  9913. (let ((case-fold-search t)
  9914. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9915. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9916. (append '(("") (" ") ("\t") ("\n"))
  9917. org-emphasis-alist)
  9918. "\\|") "\\)"))
  9919. (pos (point))
  9920. words re0 re2 re4_ re4 re5 re2a re2a_ reall)
  9921. (cond
  9922. ;; First check if there are any special search functions
  9923. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9924. ;; Now try the builtin stuff
  9925. ((and (eq (aref s0 0) ?#)
  9926. (> (length s0) 1)
  9927. (let ((match (org-find-property "CUSTOM_ID" (substring s0 1))))
  9928. (and match (goto-char match) (setf type 'dedicated)))))
  9929. ((save-excursion
  9930. (goto-char (point-min))
  9931. (and
  9932. (re-search-forward
  9933. (concat "<<" (regexp-quote s0) ">>") nil t)
  9934. (setq type 'dedicated
  9935. pos (match-beginning 0))))
  9936. ;; There is an exact target for this
  9937. (goto-char pos))
  9938. ((save-excursion
  9939. (goto-char (point-min))
  9940. (and
  9941. (re-search-forward
  9942. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9943. (setq type 'dedicated pos (match-beginning 0))))
  9944. ;; Found an element with a matching #+name affiliated keyword.
  9945. (goto-char pos))
  9946. ((and (string-match "^(\\(.*\\))$" s0)
  9947. (save-excursion
  9948. (goto-char (point-min))
  9949. (and
  9950. (re-search-forward
  9951. (concat "[^[]" (regexp-quote
  9952. (format org-coderef-label-format
  9953. (match-string 1 s0))))
  9954. nil t)
  9955. (setq type 'dedicated
  9956. pos (1+ (match-beginning 0))))))
  9957. ;; There is a coderef target for this
  9958. (goto-char pos))
  9959. ((string-match "^/\\(.*\\)/$" s)
  9960. ;; A regular expression
  9961. (cond
  9962. ((derived-mode-p 'org-mode)
  9963. (org-occur (match-string 1 s)))
  9964. (t (org-do-occur (match-string 1 s)))))
  9965. ((and (derived-mode-p 'org-mode)
  9966. (or (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9967. org-link-search-must-match-exact-headline))
  9968. ;; Headline search.
  9969. (goto-char (point-min))
  9970. (cond
  9971. ((let (case-fold-search)
  9972. (re-search-forward
  9973. (let* ((wspace "[ \t]")
  9974. (wspaceopt (concat wspace "*"))
  9975. (cookie (concat "\\(?:"
  9976. wspaceopt
  9977. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  9978. wspaceopt
  9979. "\\)"))
  9980. (sep (concat "\\(?:" wspace "+\\|" cookie "+\\)")))
  9981. (concat
  9982. org-outline-regexp-bol
  9983. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  9984. "\\(?:\\[#.\\][ \t]+\\)?"
  9985. "\\(?:" org-comment-string "[ \t]+\\)?"
  9986. sep "*"
  9987. (mapconcat #'identity
  9988. (org-split-string (regexp-quote s))
  9989. (concat sep "+"))
  9990. sep "*"
  9991. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  9992. "[ \t]*$"))
  9993. nil t))
  9994. ;; OK, found a match
  9995. (setq type 'dedicated)
  9996. (goto-char (match-beginning 0)))
  9997. ((and (not org-link-search-inhibit-query)
  9998. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9999. (y-or-n-p "No match - create this as a new heading? "))
  10000. (goto-char (point-max))
  10001. (unless (bolp) (newline))
  10002. (org-insert-heading nil t t)
  10003. (insert s "\n")
  10004. (beginning-of-line 0))
  10005. (t
  10006. (goto-char pos)
  10007. (error "No match"))))
  10008. (t
  10009. ;; A normal search string
  10010. (remove-text-properties
  10011. 0 (length s)
  10012. '(face nil mouse-face nil keymap nil fontified nil) s)
  10013. ;; Make a series of regular expressions to find a match
  10014. (setq words (org-split-string s "[ \n\r\t]+")
  10015. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10016. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10017. "\\)" markers)
  10018. re2a_ (concat "\\(" (mapconcat 'downcase words
  10019. "[ \t\r\n]+") "\\)[ \t\r\n]")
  10020. re2a (concat "[ \t\r\n]" re2a_)
  10021. re4_ (concat "\\(" (mapconcat 'downcase words
  10022. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10023. re4 (concat "[^a-zA-Z_]" re4_)
  10024. re5 (concat ".*" re4)
  10025. reall (concat "\\(" re0 "\\)\\|\\(" re2 "\\)\\|\\(" re4
  10026. "\\)\\|\\(" re5 "\\)"))
  10027. (cond
  10028. ((eq type 'org-occur) (org-occur reall))
  10029. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10030. (t (goto-char (point-min))
  10031. (setq type 'fuzzy)
  10032. (if (or (and (org-search-not-self 1 re0 nil t)
  10033. (setq type 'dedicated))
  10034. (org-search-not-self 1 re2 nil t)
  10035. (org-search-not-self 1 re2a nil t)
  10036. (org-search-not-self 1 re4 nil t)
  10037. (org-search-not-self 1 re5 nil t))
  10038. (goto-char (match-beginning 1))
  10039. (goto-char pos)
  10040. (error "No match"))))))
  10041. (and (derived-mode-p 'org-mode)
  10042. (not stealth)
  10043. (org-show-context 'link-search))
  10044. type))
  10045. (defun org-search-not-self (group &rest args)
  10046. "Execute `re-search-forward', but only accept matches that do not
  10047. enclose the position of `org-open-link-marker'."
  10048. (let ((m org-open-link-marker))
  10049. (catch 'exit
  10050. (while (apply 're-search-forward args)
  10051. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10052. (goto-char (match-end group))
  10053. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10054. (> (match-beginning 0) (marker-position m))
  10055. (< (match-end 0) (marker-position m)))
  10056. (save-match-data
  10057. (or (not (org-in-regexp
  10058. org-bracket-link-analytic-regexp 1))
  10059. (not (match-end 4)) ; no description
  10060. (and (<= (match-beginning 4) (point))
  10061. (>= (match-end 4) (point))))))
  10062. (throw 'exit (point))))))))
  10063. (defun org-get-buffer-for-internal-link (buffer)
  10064. "Return a buffer to be used for displaying the link target of internal links."
  10065. (cond
  10066. ((not org-display-internal-link-with-indirect-buffer)
  10067. buffer)
  10068. ((string-match "(Clone)$" (buffer-name buffer))
  10069. (message "Buffer is already a clone, not making another one")
  10070. ;; we also do not modify visibility in this case
  10071. buffer)
  10072. (t ; make a new indirect buffer for displaying the link
  10073. (let* ((bn (buffer-name buffer))
  10074. (ibn (concat bn "(Clone)"))
  10075. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10076. (with-current-buffer ib (org-overview))
  10077. ib))))
  10078. (defun org-do-occur (regexp &optional cleanup)
  10079. "Call the Emacs command `occur'.
  10080. If CLEANUP is non-nil, remove the printout of the regular expression
  10081. in the *Occur* buffer. This is useful if the regex is long and not useful
  10082. to read."
  10083. (occur regexp)
  10084. (when cleanup
  10085. (let ((cwin (selected-window)) win beg end)
  10086. (when (setq win (get-buffer-window "*Occur*"))
  10087. (select-window win))
  10088. (goto-char (point-min))
  10089. (when (re-search-forward "match[a-z]+" nil t)
  10090. (setq beg (match-end 0))
  10091. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10092. (setq end (1- (match-beginning 0)))))
  10093. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10094. (goto-char (point-min))
  10095. (select-window cwin))))
  10096. ;;; The mark ring for links jumps
  10097. (defvar org-mark-ring nil
  10098. "Mark ring for positions before jumps in Org-mode.")
  10099. (defvar org-mark-ring-last-goto nil
  10100. "Last position in the mark ring used to go back.")
  10101. ;; Fill and close the ring
  10102. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10103. (loop for i from 1 to org-mark-ring-length do
  10104. (push (make-marker) org-mark-ring))
  10105. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10106. org-mark-ring)
  10107. (defun org-mark-ring-push (&optional pos buffer)
  10108. "Put the current position or POS into the mark ring and rotate it."
  10109. (interactive)
  10110. (setq pos (or pos (point)))
  10111. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10112. (move-marker (car org-mark-ring)
  10113. (or pos (point))
  10114. (or buffer (current-buffer)))
  10115. (message "%s"
  10116. (substitute-command-keys
  10117. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10118. (defun org-mark-ring-goto (&optional n)
  10119. "Jump to the previous position in the mark ring.
  10120. With prefix arg N, jump back that many stored positions. When
  10121. called several times in succession, walk through the entire ring.
  10122. Org-mode commands jumping to a different position in the current file,
  10123. or to another Org-mode file, automatically push the old position
  10124. onto the ring."
  10125. (interactive "p")
  10126. (let (p m)
  10127. (if (eq last-command this-command)
  10128. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10129. (setq p org-mark-ring))
  10130. (setq org-mark-ring-last-goto p)
  10131. (setq m (car p))
  10132. (org-pop-to-buffer-same-window (marker-buffer m))
  10133. (goto-char m)
  10134. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10135. (defun org-remove-angle-brackets (s)
  10136. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10137. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10138. s)
  10139. (defun org-add-angle-brackets (s)
  10140. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10141. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10142. s)
  10143. (defun org-remove-double-quotes (s)
  10144. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10145. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10146. s)
  10147. ;;; Following specific links
  10148. (defun org-follow-timestamp-link ()
  10149. "Open an agenda view for the time-stamp date/range at point."
  10150. (cond
  10151. ((org-at-date-range-p t)
  10152. (let ((org-agenda-start-on-weekday)
  10153. (t1 (match-string 1))
  10154. (t2 (match-string 2)) tt1 tt2)
  10155. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10156. tt2 (time-to-days (org-time-string-to-time t2)))
  10157. (let ((org-agenda-buffer-tmp-name
  10158. (format "*Org Agenda(a:%s)"
  10159. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10160. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10161. ((org-at-timestamp-p t)
  10162. (let ((org-agenda-buffer-tmp-name
  10163. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10164. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10165. (substring (match-string 1) 0 10)))
  10166. 1)))
  10167. (t (error "This should not happen"))))
  10168. ;;; Following file links
  10169. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10170. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10171. (declare-function mailcap-mime-info
  10172. "mailcap" (string &optional request no-decode))
  10173. (defvar org-wait nil)
  10174. (defun org-open-file (path &optional in-emacs line search)
  10175. "Open the file at PATH.
  10176. First, this expands any special file name abbreviations. Then the
  10177. configuration variable `org-file-apps' is checked if it contains an
  10178. entry for this file type, and if yes, the corresponding command is launched.
  10179. If no application is found, Emacs simply visits the file.
  10180. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10181. With a double \\[universal-argument] \\[universal-argument] \
  10182. prefix arg, Org tries to avoid opening in Emacs
  10183. and to use an external application to visit the file.
  10184. Optional LINE specifies a line to go to, optional SEARCH a string
  10185. to search for. If LINE or SEARCH is given, the file will be
  10186. opened in Emacs, unless an entry from org-file-apps that makes
  10187. use of groups in a regexp matches.
  10188. If you want to change the way frames are used when following a
  10189. link, please customize `org-link-frame-setup'.
  10190. If the file does not exist, an error is thrown."
  10191. (let* ((file (if (equal path "")
  10192. buffer-file-name
  10193. (substitute-in-file-name (expand-file-name path))))
  10194. (file-apps (append org-file-apps (org-default-apps)))
  10195. (apps (org-remove-if
  10196. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10197. (apps-dlink (org-remove-if-not
  10198. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10199. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10200. (dirp (if remp nil (file-directory-p file)))
  10201. (file (if (and dirp org-open-directory-means-index-dot-org)
  10202. (concat (file-name-as-directory file) "index.org")
  10203. file))
  10204. (a-m-a-p (assq 'auto-mode apps))
  10205. (dfile (downcase file))
  10206. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10207. (link (cond ((and (eq line nil)
  10208. (eq search nil))
  10209. file)
  10210. (line
  10211. (concat file "::" (number-to-string line)))
  10212. (search
  10213. (concat file "::" search))))
  10214. (dlink (downcase link))
  10215. (old-buffer (current-buffer))
  10216. (old-pos (point))
  10217. (old-mode major-mode)
  10218. ext cmd link-match-data)
  10219. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10220. (setq ext (match-string 1 dfile))
  10221. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10222. (setq ext (match-string 1 dfile))))
  10223. (cond
  10224. ((member in-emacs '((16) system))
  10225. (setq cmd (cdr (assoc 'system apps))))
  10226. (in-emacs (setq cmd 'emacs))
  10227. (t
  10228. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10229. (and dirp (cdr (assoc 'directory apps)))
  10230. ; first, try matching against apps-dlink
  10231. ; if we get a match here, store the match data for later
  10232. (let ((match (assoc-default dlink apps-dlink
  10233. 'string-match)))
  10234. (if match
  10235. (progn (setq link-match-data (match-data))
  10236. match)
  10237. (progn (setq in-emacs (or in-emacs line search))
  10238. nil))) ; if we have no match in apps-dlink,
  10239. ; always open the file in emacs if line or search
  10240. ; is given (for backwards compatibility)
  10241. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10242. 'string-match)
  10243. (cdr (assoc ext apps))
  10244. (cdr (assoc t apps))))))
  10245. (when (eq cmd 'system)
  10246. (setq cmd (cdr (assoc 'system apps))))
  10247. (when (eq cmd 'default)
  10248. (setq cmd (cdr (assoc t apps))))
  10249. (when (eq cmd 'mailcap)
  10250. (require 'mailcap)
  10251. (mailcap-parse-mailcaps)
  10252. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10253. (command (mailcap-mime-info mime-type)))
  10254. (if (stringp command)
  10255. (setq cmd command)
  10256. (setq cmd 'emacs))))
  10257. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10258. (not (file-exists-p file))
  10259. (not org-open-non-existing-files))
  10260. (user-error "No such file: %s" file))
  10261. (cond
  10262. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10263. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10264. (while (string-match "['\"]%s['\"]" cmd)
  10265. (setq cmd (replace-match "%s" t t cmd)))
  10266. (while (string-match "%s" cmd)
  10267. (setq cmd (replace-match
  10268. (save-match-data
  10269. (shell-quote-argument
  10270. (convert-standard-filename file)))
  10271. t t cmd)))
  10272. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10273. (save-match-data
  10274. (let ((match-index 1)
  10275. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10276. (set-match-data link-match-data)
  10277. (while (<= match-index number-of-groups)
  10278. (let ((regex (concat "%" (number-to-string match-index)))
  10279. (replace-with (match-string match-index dlink)))
  10280. (while (string-match regex cmd)
  10281. (setq cmd (replace-match replace-with t t cmd))))
  10282. (setq match-index (+ match-index 1)))))
  10283. (save-window-excursion
  10284. (message "Running %s...done" cmd)
  10285. (start-process-shell-command cmd nil cmd)
  10286. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10287. ((or (stringp cmd)
  10288. (eq cmd 'emacs))
  10289. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10290. (widen)
  10291. (if line (progn (org-goto-line line)
  10292. (if (derived-mode-p 'org-mode)
  10293. (org-reveal)))
  10294. (if search (org-link-search search))))
  10295. ((consp cmd)
  10296. (let ((file (convert-standard-filename file)))
  10297. (save-match-data
  10298. (set-match-data link-match-data)
  10299. (eval cmd))))
  10300. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10301. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10302. (or (not (equal old-buffer (current-buffer)))
  10303. (not (equal old-pos (point))))
  10304. (org-mark-ring-push old-pos old-buffer))))
  10305. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10306. "This function returns non-nil if `entry' uses a regular
  10307. expression which should be matched against the whole link by
  10308. org-open-file.
  10309. It assumes that is the case when the entry uses a regular
  10310. expression which has at least one grouping construct and the
  10311. action is either a lisp form or a command string containing
  10312. '%1', i.e. using at least one subexpression match as a
  10313. parameter."
  10314. (let ((selector (car entry))
  10315. (action (cdr entry)))
  10316. (if (stringp selector)
  10317. (and (> (regexp-opt-depth selector) 0)
  10318. (or (and (stringp action)
  10319. (string-match "%[0-9]" action))
  10320. (consp action)))
  10321. nil)))
  10322. (defun org-default-apps ()
  10323. "Return the default applications for this operating system."
  10324. (cond
  10325. ((eq system-type 'darwin)
  10326. org-file-apps-defaults-macosx)
  10327. ((eq system-type 'windows-nt)
  10328. org-file-apps-defaults-windowsnt)
  10329. (t org-file-apps-defaults-gnu)))
  10330. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10331. "Convert extensions to regular expressions in the cars of LIST.
  10332. Also, weed out any non-string entries, because the return value is used
  10333. only for regexp matching.
  10334. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10335. point to the symbol `emacs', indicating that the file should
  10336. be opened in Emacs."
  10337. (append
  10338. (delq nil
  10339. (mapcar (lambda (x)
  10340. (if (not (stringp (car x)))
  10341. nil
  10342. (if (string-match "\\W" (car x))
  10343. x
  10344. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10345. list))
  10346. (if add-auto-mode
  10347. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10348. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10349. (defun org-file-remote-p (file)
  10350. "Test whether FILE specifies a location on a remote system.
  10351. Return non-nil if the location is indeed remote.
  10352. For example, the filename \"/user@host:/foo\" specifies a location
  10353. on the system \"/user@host:\"."
  10354. (cond ((fboundp 'file-remote-p)
  10355. (file-remote-p file))
  10356. ((fboundp 'tramp-handle-file-remote-p)
  10357. (tramp-handle-file-remote-p file))
  10358. ((and (boundp 'ange-ftp-name-format)
  10359. (string-match (car ange-ftp-name-format) file))
  10360. t)))
  10361. ;;;; Refiling
  10362. (defun org-get-org-file ()
  10363. "Read a filename, with default directory `org-directory'."
  10364. (let ((default (or org-default-notes-file remember-data-file)))
  10365. (read-file-name (format "File name [%s]: " default)
  10366. (file-name-as-directory org-directory)
  10367. default)))
  10368. (defun org-notes-order-reversed-p ()
  10369. "Check if the current file should receive notes in reversed order."
  10370. (cond
  10371. ((not org-reverse-note-order) nil)
  10372. ((eq t org-reverse-note-order) t)
  10373. ((not (listp org-reverse-note-order)) nil)
  10374. (t (catch 'exit
  10375. (let ((all org-reverse-note-order)
  10376. entry)
  10377. (while (setq entry (pop all))
  10378. (if (string-match (car entry) buffer-file-name)
  10379. (throw 'exit (cdr entry))))
  10380. nil)))))
  10381. (defvar org-refile-target-table nil
  10382. "The list of refile targets, created by `org-refile'.")
  10383. (defvar org-agenda-new-buffers nil
  10384. "Buffers created to visit agenda files.")
  10385. (defvar org-refile-cache nil
  10386. "Cache for refile targets.")
  10387. (defvar org-refile-markers nil
  10388. "All the markers used for caching refile locations.")
  10389. (defun org-refile-marker (pos)
  10390. "Get a new refile marker, but only if caching is in use."
  10391. (if (not org-refile-use-cache)
  10392. pos
  10393. (let ((m (make-marker)))
  10394. (move-marker m pos)
  10395. (push m org-refile-markers)
  10396. m)))
  10397. (defun org-refile-cache-clear ()
  10398. "Clear the refile cache and disable all the markers."
  10399. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10400. (setq org-refile-markers nil)
  10401. (setq org-refile-cache nil)
  10402. (message "Refile cache has been cleared"))
  10403. (defun org-refile-cache-check-set (set)
  10404. "Check if all the markers in the cache still have live buffers."
  10405. (let (marker)
  10406. (catch 'exit
  10407. (while (and set (setq marker (nth 3 (pop set))))
  10408. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10409. (when (and marker (null (marker-buffer marker)))
  10410. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10411. (sit-for 3)
  10412. (throw 'exit nil)))
  10413. t)))
  10414. (defun org-refile-cache-put (set &rest identifiers)
  10415. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10416. (let* ((key (sha1 (prin1-to-string identifiers)))
  10417. (entry (assoc key org-refile-cache)))
  10418. (if entry
  10419. (setcdr entry set)
  10420. (push (cons key set) org-refile-cache))))
  10421. (defun org-refile-cache-get (&rest identifiers)
  10422. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10423. (cond
  10424. ((not org-refile-cache) nil)
  10425. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10426. (t
  10427. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10428. org-refile-cache))))
  10429. (and set (org-refile-cache-check-set set) set)))))
  10430. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10431. "Produce a table with refile targets."
  10432. (let ((case-fold-search nil)
  10433. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10434. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10435. targets tgs txt re files f desc descre fast-path-p level pos0)
  10436. (message "Getting targets...")
  10437. (with-current-buffer (or default-buffer (current-buffer))
  10438. (while (setq entry (pop entries))
  10439. (setq files (car entry) desc (cdr entry))
  10440. (setq fast-path-p nil)
  10441. (cond
  10442. ((null files) (setq files (list (current-buffer))))
  10443. ((eq files 'org-agenda-files)
  10444. (setq files (org-agenda-files 'unrestricted)))
  10445. ((and (symbolp files) (fboundp files))
  10446. (setq files (funcall files)))
  10447. ((and (symbolp files) (boundp files))
  10448. (setq files (symbol-value files))))
  10449. (if (stringp files) (setq files (list files)))
  10450. (cond
  10451. ((eq (car desc) :tag)
  10452. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10453. ((eq (car desc) :todo)
  10454. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10455. ((eq (car desc) :regexp)
  10456. (setq descre (cdr desc)))
  10457. ((eq (car desc) :level)
  10458. (setq descre (concat "^\\*\\{" (number-to-string
  10459. (if org-odd-levels-only
  10460. (1- (* 2 (cdr desc)))
  10461. (cdr desc)))
  10462. "\\}[ \t]")))
  10463. ((eq (car desc) :maxlevel)
  10464. (setq fast-path-p t)
  10465. (setq descre (concat "^\\*\\{1," (number-to-string
  10466. (if org-odd-levels-only
  10467. (1- (* 2 (cdr desc)))
  10468. (cdr desc)))
  10469. "\\}[ \t]")))
  10470. (t (error "Bad refiling target description %s" desc)))
  10471. (while (setq f (pop files))
  10472. (with-current-buffer
  10473. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10474. (or
  10475. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10476. (progn
  10477. (if (bufferp f) (setq f (buffer-file-name
  10478. (buffer-base-buffer f))))
  10479. (setq f (and f (expand-file-name f)))
  10480. (if (eq org-refile-use-outline-path 'file)
  10481. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10482. (save-excursion
  10483. (save-restriction
  10484. (widen)
  10485. (goto-char (point-min))
  10486. (while (re-search-forward descre nil t)
  10487. (goto-char (setq pos0 (point-at-bol)))
  10488. (catch 'next
  10489. (when org-refile-target-verify-function
  10490. (save-match-data
  10491. (or (funcall org-refile-target-verify-function)
  10492. (throw 'next t))))
  10493. (when (and (looking-at org-complex-heading-regexp)
  10494. (not (member (match-string 4) excluded-entries))
  10495. (match-string 4))
  10496. (setq level (org-reduced-level
  10497. (- (match-end 1) (match-beginning 1)))
  10498. txt (org-link-display-format (match-string 4))
  10499. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10500. re (format org-complex-heading-regexp-format
  10501. (regexp-quote (match-string 4))))
  10502. (when org-refile-use-outline-path
  10503. (setq txt (mapconcat
  10504. 'org-protect-slash
  10505. (append
  10506. (if (eq org-refile-use-outline-path
  10507. 'file)
  10508. (list (file-name-nondirectory
  10509. (buffer-file-name
  10510. (buffer-base-buffer))))
  10511. (if (eq org-refile-use-outline-path
  10512. 'full-file-path)
  10513. (list (buffer-file-name
  10514. (buffer-base-buffer)))))
  10515. (org-get-outline-path fast-path-p
  10516. level txt)
  10517. (list txt))
  10518. "/")))
  10519. (push (list txt f re (org-refile-marker (point)))
  10520. tgs)))
  10521. (when (= (point) pos0)
  10522. ;; verification function has not moved point
  10523. (goto-char (point-at-eol))))))))
  10524. (when org-refile-use-cache
  10525. (org-refile-cache-put tgs (buffer-file-name) descre))
  10526. (setq targets (append tgs targets))))))
  10527. (message "Getting targets...done")
  10528. (nreverse targets)))
  10529. (defun org-protect-slash (s)
  10530. (while (string-match "/" s)
  10531. (setq s (replace-match "\\" t t s)))
  10532. s)
  10533. (defvar org-olpa (make-vector 20 nil))
  10534. (defun org-get-outline-path (&optional fastp level heading)
  10535. "Return the outline path to the current entry, as a list.
  10536. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10537. routine which makes outline path derivations for an entire file,
  10538. avoiding backtracing. Refile target collection makes use of that."
  10539. (if fastp
  10540. (progn
  10541. (if (> level 19)
  10542. (error "Outline path failure, more than 19 levels"))
  10543. (loop for i from level upto 19 do
  10544. (aset org-olpa i nil))
  10545. (prog1
  10546. (delq nil (append org-olpa nil))
  10547. (aset org-olpa level heading)))
  10548. (let (rtn case-fold-search)
  10549. (save-excursion
  10550. (save-restriction
  10551. (widen)
  10552. (while (org-up-heading-safe)
  10553. (when (looking-at org-complex-heading-regexp)
  10554. (push (org-trim
  10555. (replace-regexp-in-string
  10556. ;; Remove statistical/checkboxes cookies
  10557. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10558. (org-match-string-no-properties 4)))
  10559. rtn)))
  10560. rtn)))))
  10561. (defun org-format-outline-path (path &optional width prefix separator)
  10562. "Format the outline path PATH for display.
  10563. WIDTH is the maximum number of characters that is available.
  10564. PREFIX is a prefix to be included in the returned string,
  10565. such as the file name.
  10566. SEPARATOR is inserted between the different parts of the path,
  10567. the default is \"/\"."
  10568. (setq width (or width 79))
  10569. (if prefix (setq width (- width (length prefix))))
  10570. (if (not path)
  10571. (or prefix "")
  10572. (let* ((nsteps (length path))
  10573. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10574. (maxwidth (if (<= total-width width)
  10575. 10000 ;; everything fits
  10576. ;; we need to shorten the level headings
  10577. (/ (- width nsteps) nsteps)))
  10578. (org-odd-levels-only nil)
  10579. (n 0)
  10580. (total (1+ (length prefix))))
  10581. (setq maxwidth (max maxwidth 10))
  10582. (concat prefix
  10583. (if prefix (or separator "/"))
  10584. (mapconcat
  10585. (lambda (h)
  10586. (setq n (1+ n))
  10587. (if (and (= n nsteps) (< maxwidth 10000))
  10588. (setq maxwidth (- total-width total)))
  10589. (if (< (length h) maxwidth)
  10590. (progn (setq total (+ total (length h) 1)) h)
  10591. (setq h (substring h 0 (- maxwidth 2))
  10592. total (+ total maxwidth 1))
  10593. (if (string-match "[ \t]+\\'" h)
  10594. (setq h (substring h 0 (match-beginning 0))))
  10595. (setq h (concat h "..")))
  10596. (org-add-props h nil 'face
  10597. (nth (% (1- n) org-n-level-faces)
  10598. org-level-faces))
  10599. h)
  10600. path (or separator "/"))))))
  10601. (defun org-display-outline-path (&optional file current separator just-return-string)
  10602. "Display the current outline path in the echo area.
  10603. If FILE is non-nil, prepend the output with the file name.
  10604. If CURRENT is non-nil, append the current heading to the output.
  10605. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10606. the different parts of the path and defaults to \"/\".
  10607. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10608. (interactive "P")
  10609. (let* (case-fold-search
  10610. (bfn (buffer-file-name (buffer-base-buffer)))
  10611. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10612. res)
  10613. (if current (setq path (append path
  10614. (save-excursion
  10615. (org-back-to-heading t)
  10616. (if (looking-at org-complex-heading-regexp)
  10617. (list (match-string 4)))))))
  10618. (setq res
  10619. (org-format-outline-path
  10620. path
  10621. (1- (frame-width))
  10622. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10623. separator))
  10624. (if just-return-string
  10625. (org-no-properties res)
  10626. (org-unlogged-message "%s" res))))
  10627. (defvar org-refile-history nil
  10628. "History for refiling operations.")
  10629. (defvar org-after-refile-insert-hook nil
  10630. "Hook run after `org-refile' has inserted its stuff at the new location.
  10631. Note that this is still *before* the stuff will be removed from
  10632. the *old* location.")
  10633. (defvar org-capture-last-stored-marker)
  10634. (defvar org-refile-keep nil
  10635. "Non-nil means `org-refile' will copy instead of refile.")
  10636. (defun org-copy ()
  10637. "Like `org-refile', but copy."
  10638. (interactive)
  10639. (let ((org-refile-keep t))
  10640. (funcall 'org-refile nil nil nil "Copy")))
  10641. (defun org-refile (&optional arg default-buffer rfloc msg)
  10642. "Move the entry or entries at point to another heading.
  10643. The list of target headings is compiled using the information in
  10644. `org-refile-targets', which see.
  10645. At the target location, the entry is filed as a subitem of the
  10646. target heading. Depending on `org-reverse-note-order', the new
  10647. subitem will either be the first or the last subitem.
  10648. If there is an active region, all entries in that region will be
  10649. refiled. However, the region must fulfill the requirement that
  10650. the first heading sets the top-level of the moved text.
  10651. With prefix arg ARG, the command will only visit the target
  10652. location and not actually move anything.
  10653. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10654. refiling operation has put the subtree.
  10655. With a numeric prefix argument of `2', refile to the running clock.
  10656. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10657. being set to `t' and copy to the target location, don't move it.
  10658. Beware that keeping refiled entries may result in duplicated ID
  10659. properties.
  10660. RFLOC can be a refile location obtained in a different way.
  10661. MSG is a string to replace \"Refile\" in the default prompt with
  10662. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10663. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10664. If you are using target caching (see `org-refile-use-cache'), you
  10665. have to clear the target cache in order to find new targets.
  10666. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10667. prefix argument (`C-u C-u C-u C-c C-w')."
  10668. (interactive "P")
  10669. (if (member arg '(0 (64)))
  10670. (org-refile-cache-clear)
  10671. (let* ((actionmsg (cond (msg msg)
  10672. ((equal arg 3) "Refile (and keep)")
  10673. (t "Refile")))
  10674. (cbuf (current-buffer))
  10675. (regionp (org-region-active-p))
  10676. (region-start (and regionp (region-beginning)))
  10677. (region-end (and regionp (region-end)))
  10678. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10679. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10680. pos it nbuf file re level reversed)
  10681. (setq last-command nil)
  10682. (when regionp
  10683. (goto-char region-start)
  10684. (or (bolp) (goto-char (point-at-bol)))
  10685. (setq region-start (point))
  10686. (unless (or (org-kill-is-subtree-p
  10687. (buffer-substring region-start region-end))
  10688. (prog1 org-refile-active-region-within-subtree
  10689. (let ((s (point-at-eol)))
  10690. (org-toggle-heading)
  10691. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10692. (user-error "The region is not a (sequence of) subtree(s)")))
  10693. (if (equal arg '(16))
  10694. (org-refile-goto-last-stored)
  10695. (when (or
  10696. (and (equal arg 2)
  10697. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10698. (prog1
  10699. (setq it (list (or org-clock-heading "running clock")
  10700. (buffer-file-name
  10701. (marker-buffer org-clock-hd-marker))
  10702. ""
  10703. (marker-position org-clock-hd-marker)))
  10704. (setq arg nil)))
  10705. (setq it (or rfloc
  10706. (let (heading-text)
  10707. (save-excursion
  10708. (unless (and arg (listp arg))
  10709. (org-back-to-heading t)
  10710. (setq heading-text
  10711. (replace-regexp-in-string
  10712. org-bracket-link-regexp
  10713. "\\3"
  10714. (nth 4 (org-heading-components)))))
  10715. (org-refile-get-location
  10716. (cond ((and arg (listp arg)) "Goto")
  10717. (regionp (concat actionmsg " region to"))
  10718. (t (concat actionmsg " subtree \""
  10719. heading-text "\" to")))
  10720. default-buffer
  10721. (and (not (equal '(4) arg))
  10722. org-refile-allow-creating-parent-nodes)
  10723. arg))))))
  10724. (setq file (nth 1 it)
  10725. re (nth 2 it)
  10726. pos (nth 3 it))
  10727. (if (and (not arg)
  10728. pos
  10729. (equal (buffer-file-name) file)
  10730. (if regionp
  10731. (and (>= pos region-start)
  10732. (<= pos region-end))
  10733. (and (>= pos (point))
  10734. (< pos (save-excursion
  10735. (org-end-of-subtree t t))))))
  10736. (error "Cannot refile to position inside the tree or region"))
  10737. (setq nbuf (or (find-buffer-visiting file)
  10738. (find-file-noselect file)))
  10739. (if (and arg (not (equal arg 3)))
  10740. (progn
  10741. (org-pop-to-buffer-same-window nbuf)
  10742. (goto-char pos)
  10743. (org-show-context 'org-goto))
  10744. (if regionp
  10745. (progn
  10746. (org-kill-new (buffer-substring region-start region-end))
  10747. (org-save-markers-in-region region-start region-end))
  10748. (org-copy-subtree 1 nil t))
  10749. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10750. (find-file-noselect file)))
  10751. (setq reversed (org-notes-order-reversed-p))
  10752. (save-excursion
  10753. (save-restriction
  10754. (widen)
  10755. (if pos
  10756. (progn
  10757. (goto-char pos)
  10758. (looking-at org-outline-regexp)
  10759. (setq level (org-get-valid-level (funcall outline-level) 1))
  10760. (goto-char
  10761. (if reversed
  10762. (or (outline-next-heading) (point-max))
  10763. (or (save-excursion (org-get-next-sibling))
  10764. (org-end-of-subtree t t)
  10765. (point-max)))))
  10766. (setq level 1)
  10767. (if (not reversed)
  10768. (goto-char (point-max))
  10769. (goto-char (point-min))
  10770. (or (outline-next-heading) (goto-char (point-max)))))
  10771. (if (not (bolp)) (newline))
  10772. (org-paste-subtree level nil nil t)
  10773. (when org-log-refile
  10774. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10775. (unless (eq org-log-refile 'note)
  10776. (save-excursion (org-add-log-note))))
  10777. (and org-auto-align-tags
  10778. (let ((org-loop-over-headlines-in-active-region nil))
  10779. (org-set-tags nil t)))
  10780. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10781. :last-refile)))
  10782. (when bookmark-name
  10783. (with-demoted-errors
  10784. (bookmark-set bookmark-name))))
  10785. ;; If we are refiling for capture, make sure that the
  10786. ;; last-capture pointers point here
  10787. (when (org-bound-and-true-p org-refile-for-capture)
  10788. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10789. :last-capture-marker)))
  10790. (when bookmark-name
  10791. (with-demoted-errors
  10792. (bookmark-set bookmark-name))))
  10793. (move-marker org-capture-last-stored-marker (point)))
  10794. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10795. (run-hooks 'org-after-refile-insert-hook))))
  10796. (unless org-refile-keep
  10797. (if regionp
  10798. (delete-region (point) (+ (point) (- region-end region-start)))
  10799. (delete-region
  10800. (and (org-back-to-heading t) (point))
  10801. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10802. (when (featurep 'org-inlinetask)
  10803. (org-inlinetask-remove-END-maybe))
  10804. (setq org-markers-to-move nil)
  10805. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10806. (defun org-refile-goto-last-stored ()
  10807. "Go to the location where the last refile was stored."
  10808. (interactive)
  10809. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10810. (message "This is the location of the last refile"))
  10811. (defun org-refile--get-location (refloc tbl)
  10812. "When user refile to REFLOC, find the associated target in TBL.
  10813. Also check `org-refile-target-table'."
  10814. (car (delq
  10815. nil
  10816. (mapcar
  10817. (lambda (r) (or (assoc r tbl)
  10818. (assoc r org-refile-target-table)))
  10819. (list (replace-regexp-in-string "/$" "" refloc)
  10820. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10821. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10822. no-exclude)
  10823. "Prompt the user for a refile location, using PROMPT.
  10824. PROMPT should not be suffixed with a colon and a space, because
  10825. this function appends the default value from
  10826. `org-refile-history' automatically, if that is not empty.
  10827. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10828. this is used for the GOTO interface."
  10829. (let ((org-refile-targets org-refile-targets)
  10830. (org-refile-use-outline-path org-refile-use-outline-path)
  10831. excluded-entries)
  10832. (when (and (derived-mode-p 'org-mode)
  10833. (not org-refile-use-cache)
  10834. (not no-exclude))
  10835. (org-map-tree
  10836. (lambda()
  10837. (setq excluded-entries
  10838. (append excluded-entries (list (org-get-heading t t)))))))
  10839. (setq org-refile-target-table
  10840. (org-refile-get-targets default-buffer excluded-entries)))
  10841. (unless org-refile-target-table
  10842. (user-error "No refile targets"))
  10843. (let* ((cbuf (current-buffer))
  10844. (partial-completion-mode nil)
  10845. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10846. (cfunc (if (and org-refile-use-outline-path
  10847. org-outline-path-complete-in-steps)
  10848. 'org-olpath-completing-read
  10849. 'org-icompleting-read))
  10850. (extra (if org-refile-use-outline-path "/" ""))
  10851. (cbnex (concat (buffer-name) extra))
  10852. (filename (and cfn (expand-file-name cfn)))
  10853. (tbl (mapcar
  10854. (lambda (x)
  10855. (if (and (not (member org-refile-use-outline-path
  10856. '(file full-file-path)))
  10857. (not (equal filename (nth 1 x))))
  10858. (cons (concat (car x) extra " ("
  10859. (file-name-nondirectory (nth 1 x)) ")")
  10860. (cdr x))
  10861. (cons (concat (car x) extra) (cdr x))))
  10862. org-refile-target-table))
  10863. (completion-ignore-case t)
  10864. cdef
  10865. (prompt (concat prompt
  10866. (or (and (car org-refile-history)
  10867. (concat " (default " (car org-refile-history) ")"))
  10868. (and (assoc cbnex tbl) (setq cdef cbnex)
  10869. (concat " (default " cbnex ")"))) ": "))
  10870. pa answ parent-target child parent old-hist)
  10871. (setq old-hist org-refile-history)
  10872. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10873. nil 'org-refile-history (or cdef (car org-refile-history))))
  10874. (if (setq pa (org-refile--get-location answ tbl))
  10875. (progn
  10876. (org-refile-check-position pa)
  10877. (when (or (not org-refile-history)
  10878. (not (eq old-hist org-refile-history))
  10879. (not (equal (car pa) (car org-refile-history))))
  10880. (setq org-refile-history
  10881. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10882. org-refile-history
  10883. (cdr org-refile-history))))
  10884. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10885. (pop org-refile-history)))
  10886. pa)
  10887. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10888. (progn
  10889. (setq parent (match-string 1 answ)
  10890. child (match-string 2 answ))
  10891. (setq parent-target (org-refile--get-location parent tbl))
  10892. (when (and parent-target
  10893. (or (eq new-nodes t)
  10894. (and (eq new-nodes 'confirm)
  10895. (y-or-n-p (format "Create new node \"%s\"? "
  10896. child)))))
  10897. (org-refile-new-child parent-target child)))
  10898. (user-error "Invalid target location")))))
  10899. (declare-function org-string-nw-p "org-macs" (s))
  10900. (defun org-refile-check-position (refile-pointer)
  10901. "Check if the refile pointer matches the headline to which it points."
  10902. (let* ((file (nth 1 refile-pointer))
  10903. (re (nth 2 refile-pointer))
  10904. (pos (nth 3 refile-pointer))
  10905. buffer)
  10906. (if (and (not (markerp pos)) (not file))
  10907. (user-error "Please indicate a target file in the refile path")
  10908. (when (org-string-nw-p re)
  10909. (setq buffer (if (markerp pos)
  10910. (marker-buffer pos)
  10911. (or (find-buffer-visiting file)
  10912. (find-file-noselect file))))
  10913. (with-current-buffer buffer
  10914. (save-excursion
  10915. (save-restriction
  10916. (widen)
  10917. (goto-char pos)
  10918. (beginning-of-line 1)
  10919. (unless (org-looking-at-p re)
  10920. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10921. (defun org-refile-new-child (parent-target child)
  10922. "Use refile target PARENT-TARGET to add new CHILD below it."
  10923. (unless parent-target
  10924. (error "Cannot find parent for new node"))
  10925. (let ((file (nth 1 parent-target))
  10926. (pos (nth 3 parent-target))
  10927. level)
  10928. (with-current-buffer (or (find-buffer-visiting file)
  10929. (find-file-noselect file))
  10930. (save-excursion
  10931. (save-restriction
  10932. (widen)
  10933. (if pos
  10934. (goto-char pos)
  10935. (goto-char (point-max))
  10936. (if (not (bolp)) (newline)))
  10937. (when (looking-at org-outline-regexp)
  10938. (setq level (funcall outline-level))
  10939. (org-end-of-subtree t t))
  10940. (org-back-over-empty-lines)
  10941. (insert "\n" (make-string
  10942. (if pos (org-get-valid-level level 1) 1) ?*)
  10943. " " child "\n")
  10944. (beginning-of-line 0)
  10945. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10946. (defun org-olpath-completing-read (prompt collection &rest args)
  10947. "Read an outline path like a file name."
  10948. (let ((thetable collection)
  10949. (org-completion-use-ido nil) ; does not work with ido.
  10950. (org-completion-use-iswitchb nil)) ; or iswitchb
  10951. (apply
  10952. 'org-icompleting-read prompt
  10953. (lambda (string predicate &optional flag)
  10954. (let (rtn r f (l (length string)))
  10955. (cond
  10956. ((eq flag nil)
  10957. ;; try completion
  10958. (try-completion string thetable))
  10959. ((eq flag t)
  10960. ;; all-completions
  10961. (setq rtn (all-completions string thetable predicate))
  10962. (mapcar
  10963. (lambda (x)
  10964. (setq r (substring x l))
  10965. (if (string-match " ([^)]*)$" x)
  10966. (setq f (match-string 0 x))
  10967. (setq f ""))
  10968. (if (string-match "/" r)
  10969. (concat string (substring r 0 (match-end 0)) f)
  10970. x))
  10971. rtn))
  10972. ((eq flag 'lambda)
  10973. ;; exact match?
  10974. (assoc string thetable)))))
  10975. args)))
  10976. ;;;; Dynamic blocks
  10977. (defun org-find-dblock (name)
  10978. "Find the first dynamic block with name NAME in the buffer.
  10979. If not found, stay at current position and return nil."
  10980. (let ((case-fold-search t) pos)
  10981. (save-excursion
  10982. (goto-char (point-min))
  10983. (setq pos (and (re-search-forward
  10984. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10985. (match-beginning 0))))
  10986. (if pos (goto-char pos))
  10987. pos))
  10988. (defun org-create-dblock (plist)
  10989. "Create a dynamic block section, with parameters taken from PLIST.
  10990. PLIST must contain a :name entry which is used as the name of the block."
  10991. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10992. (end-of-line 1)
  10993. (newline))
  10994. (let ((col (current-column))
  10995. (name (plist-get plist :name)))
  10996. (insert "#+BEGIN: " name)
  10997. (while plist
  10998. (if (eq (car plist) :name)
  10999. (setq plist (cddr plist))
  11000. (insert " " (prin1-to-string (pop plist)))))
  11001. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11002. (beginning-of-line -2)))
  11003. (defun org-prepare-dblock ()
  11004. "Prepare dynamic block for refresh.
  11005. This empties the block, puts the cursor at the insert position and returns
  11006. the property list including an extra property :name with the block name."
  11007. (unless (looking-at org-dblock-start-re)
  11008. (user-error "Not at a dynamic block"))
  11009. (let* ((begdel (1+ (match-end 0)))
  11010. (name (org-no-properties (match-string 1)))
  11011. (params (append (list :name name)
  11012. (read (concat "(" (match-string 3) ")")))))
  11013. (save-excursion
  11014. (beginning-of-line 1)
  11015. (skip-chars-forward " \t")
  11016. (setq params (plist-put params :indentation-column (current-column))))
  11017. (unless (re-search-forward org-dblock-end-re nil t)
  11018. (error "Dynamic block not terminated"))
  11019. (setq params
  11020. (append params
  11021. (list :content (buffer-substring
  11022. begdel (match-beginning 0)))))
  11023. (delete-region begdel (match-beginning 0))
  11024. (goto-char begdel)
  11025. (open-line 1)
  11026. params))
  11027. (defun org-map-dblocks (&optional command)
  11028. "Apply COMMAND to all dynamic blocks in the current buffer.
  11029. If COMMAND is not given, use `org-update-dblock'."
  11030. (let ((cmd (or command 'org-update-dblock)))
  11031. (save-excursion
  11032. (goto-char (point-min))
  11033. (while (re-search-forward org-dblock-start-re nil t)
  11034. (goto-char (match-beginning 0))
  11035. (save-excursion
  11036. (condition-case nil
  11037. (funcall cmd)
  11038. (error (message "Error during update of dynamic block"))))
  11039. (unless (re-search-forward org-dblock-end-re nil t)
  11040. (error "Dynamic block not terminated"))))))
  11041. (defun org-dblock-update (&optional arg)
  11042. "User command for updating dynamic blocks.
  11043. Update the dynamic block at point. With prefix ARG, update all dynamic
  11044. blocks in the buffer."
  11045. (interactive "P")
  11046. (if arg
  11047. (org-update-all-dblocks)
  11048. (or (looking-at org-dblock-start-re)
  11049. (org-beginning-of-dblock))
  11050. (org-update-dblock)))
  11051. (defun org-update-dblock ()
  11052. "Update the dynamic block at point.
  11053. This means to empty the block, parse for parameters and then call
  11054. the correct writing function."
  11055. (interactive)
  11056. (save-excursion
  11057. (let* ((win (selected-window))
  11058. (pos (point))
  11059. (line (org-current-line))
  11060. (params (org-prepare-dblock))
  11061. (name (plist-get params :name))
  11062. (indent (plist-get params :indentation-column))
  11063. (cmd (intern (concat "org-dblock-write:" name))))
  11064. (message "Updating dynamic block `%s' at line %d..." name line)
  11065. (funcall cmd params)
  11066. (message "Updating dynamic block `%s' at line %d...done" name line)
  11067. (goto-char pos)
  11068. (when (and indent (> indent 0))
  11069. (setq indent (make-string indent ?\ ))
  11070. (save-excursion
  11071. (select-window win)
  11072. (org-beginning-of-dblock)
  11073. (forward-line 1)
  11074. (while (not (looking-at org-dblock-end-re))
  11075. (insert indent)
  11076. (beginning-of-line 2))
  11077. (when (looking-at org-dblock-end-re)
  11078. (and (looking-at "[ \t]+")
  11079. (replace-match ""))
  11080. (insert indent)))))))
  11081. (defun org-beginning-of-dblock ()
  11082. "Find the beginning of the dynamic block at point.
  11083. Error if there is no such block at point."
  11084. (let ((pos (point))
  11085. beg)
  11086. (end-of-line 1)
  11087. (if (and (re-search-backward org-dblock-start-re nil t)
  11088. (setq beg (match-beginning 0))
  11089. (re-search-forward org-dblock-end-re nil t)
  11090. (> (match-end 0) pos))
  11091. (goto-char beg)
  11092. (goto-char pos)
  11093. (error "Not in a dynamic block"))))
  11094. (defun org-update-all-dblocks ()
  11095. "Update all dynamic blocks in the buffer.
  11096. This function can be used in a hook."
  11097. (interactive)
  11098. (when (derived-mode-p 'org-mode)
  11099. (org-map-dblocks 'org-update-dblock)))
  11100. ;;;; Completion
  11101. (declare-function org-export-backend-name "org-export" (cl-x))
  11102. (declare-function org-export-backend-options "org-export" (cl-x))
  11103. (defun org-get-export-keywords ()
  11104. "Return a list of all currently understood export keywords.
  11105. Export keywords include options, block names, attributes and
  11106. keywords relative to each registered export back-end."
  11107. (let (keywords)
  11108. (dolist (backend
  11109. (org-bound-and-true-p org-export-registered-backends)
  11110. (delq nil keywords))
  11111. ;; Back-end name (for keywords, like #+LATEX:)
  11112. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11113. (dolist (option-entry (org-export-backend-options backend))
  11114. ;; Back-end options.
  11115. (push (nth 1 option-entry) keywords)))))
  11116. (defconst org-options-keywords
  11117. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11118. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11119. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11120. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11121. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11122. (defcustom org-structure-template-alist
  11123. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11124. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11125. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11126. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11127. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11128. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11129. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11130. ("L" "#+LaTeX: ")
  11131. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11132. ("H" "#+HTML: ")
  11133. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11134. ("A" "#+ASCII: ")
  11135. ("i" "#+INDEX: ?")
  11136. ("I" "#+INCLUDE: %file ?"))
  11137. "Structure completion elements.
  11138. This is a list of abbreviation keys and values. The value gets inserted
  11139. if you type `<' followed by the key and then press the completion key,
  11140. usually `TAB'. %file will be replaced by a file name after prompting
  11141. for the file using completion. The cursor will be placed at the position
  11142. of the `?` in the template.
  11143. There are two templates for each key, the first uses the original Org syntax,
  11144. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11145. the default when the /org-mtags.el/ module has been loaded. See also the
  11146. variable `org-mtags-prefer-muse-templates'."
  11147. :group 'org-completion
  11148. :type '(repeat
  11149. (list
  11150. (string :tag "Key")
  11151. (string :tag "Template")))
  11152. :version "25.1"
  11153. :package-version '(Org . "8.3"))
  11154. (defun org-try-structure-completion ()
  11155. "Try to complete a structure template before point.
  11156. This looks for strings like \"<e\" on an otherwise empty line and
  11157. expands them."
  11158. (let ((l (buffer-substring (point-at-bol) (point)))
  11159. a)
  11160. (when (and (looking-at "[ \t]*$")
  11161. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11162. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11163. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11164. (match-beginning 1)) a)
  11165. t)))
  11166. (defun org-complete-expand-structure-template (start cell)
  11167. "Expand a structure template."
  11168. (let ((rpl (nth 1 cell))
  11169. (ind ""))
  11170. (delete-region start (point))
  11171. (when (string-match "\\`[ \t]*#\\+" rpl)
  11172. (cond
  11173. ((bolp))
  11174. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11175. (setq ind (buffer-substring (point-at-bol) (point))))
  11176. (t (newline))))
  11177. (setq start (point))
  11178. (if (string-match "%file" rpl)
  11179. (setq rpl (replace-match
  11180. (concat
  11181. "\""
  11182. (save-match-data
  11183. (abbreviate-file-name (read-file-name "Include file: ")))
  11184. "\"")
  11185. t t rpl)))
  11186. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11187. (concat "\n" ind)))
  11188. (insert rpl)
  11189. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11190. ;;;; TODO, DEADLINE, Comments
  11191. (defun org-toggle-comment ()
  11192. "Change the COMMENT state of an entry."
  11193. (interactive)
  11194. (save-excursion
  11195. (org-back-to-heading)
  11196. (looking-at org-complex-heading-regexp)
  11197. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11198. (skip-chars-forward " \t")
  11199. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11200. (if (org-in-commented-heading-p t)
  11201. (delete-region (point)
  11202. (progn (search-forward " " (line-end-position) 'move)
  11203. (skip-chars-forward " \t")
  11204. (point)))
  11205. (insert org-comment-string)
  11206. (unless (eolp) (insert " ")))))
  11207. (defvar org-last-todo-state-is-todo nil
  11208. "This is non-nil when the last TODO state change led to a TODO state.
  11209. If the last change removed the TODO tag or switched to DONE, then
  11210. this is nil.")
  11211. (defvar org-setting-tags nil) ; dynamically skipped
  11212. (defvar org-todo-setup-filter-hook nil
  11213. "Hook for functions that pre-filter todo specs.
  11214. Each function takes a todo spec and returns either nil or the spec
  11215. transformed into canonical form." )
  11216. (defvar org-todo-get-default-hook nil
  11217. "Hook for functions that get a default item for todo.
  11218. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11219. nil or a string to be used for the todo mark." )
  11220. (defvar org-agenda-headline-snapshot-before-repeat)
  11221. (defun org-current-effective-time ()
  11222. "Return current time adjusted for `org-extend-today-until' variable."
  11223. (let* ((ct (org-current-time))
  11224. (dct (decode-time ct))
  11225. (ct1
  11226. (cond
  11227. (org-use-last-clock-out-time-as-effective-time
  11228. (or (org-clock-get-last-clock-out-time) ct))
  11229. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11230. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11231. (t ct))))
  11232. ct1))
  11233. (defun org-todo-yesterday (&optional arg)
  11234. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11235. (interactive "P")
  11236. (if (eq major-mode 'org-agenda-mode)
  11237. (apply 'org-agenda-todo-yesterday arg)
  11238. (let* ((org-use-effective-time t)
  11239. (hour (third (decode-time
  11240. (org-current-time))))
  11241. (org-extend-today-until (1+ hour)))
  11242. (org-todo arg))))
  11243. (defvar org-block-entry-blocking ""
  11244. "First entry preventing the TODO state change.")
  11245. (defun org-cancel-repeater ()
  11246. "Cancel a repeater by setting its numeric value to zero."
  11247. (interactive)
  11248. (save-excursion
  11249. (org-back-to-heading t)
  11250. (let ((bound1 (point))
  11251. (bound0 (save-excursion (outline-next-heading) (point))))
  11252. (when (re-search-forward
  11253. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11254. org-deadline-time-regexp "\\)\\|\\("
  11255. org-ts-regexp "\\)")
  11256. bound0 t)
  11257. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11258. (replace-match "0" t nil nil 1))))))
  11259. (defun org-todo (&optional arg)
  11260. "Change the TODO state of an item.
  11261. The state of an item is given by a keyword at the start of the heading,
  11262. like
  11263. *** TODO Write paper
  11264. *** DONE Call mom
  11265. The different keywords are specified in the variable `org-todo-keywords'.
  11266. By default the available states are \"TODO\" and \"DONE\".
  11267. So for this example: when the item starts with TODO, it is changed to DONE.
  11268. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11269. DONE are present, add TODO at the beginning of the heading.
  11270. With \\[universal-argument] prefix arg, use completion to determine the new \
  11271. state.
  11272. With numeric prefix arg, switch to that state.
  11273. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11274. keywords (nextset).
  11275. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11276. With a numeric prefix arg of 0, inhibit note taking for the change.
  11277. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11278. When called through ELisp, arg is also interpreted in the following way:
  11279. 'none -> empty state
  11280. \"\"(empty string) -> switch to empty state
  11281. 'done -> switch to DONE
  11282. 'nextset -> switch to the next set of keywords
  11283. 'previousset -> switch to the previous set of keywords
  11284. \"WAITING\" -> switch to the specified keyword, but only if it
  11285. really is a member of `org-todo-keywords'."
  11286. (interactive "P")
  11287. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11288. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11289. 'region-start-level 'region))
  11290. org-loop-over-headlines-in-active-region)
  11291. (org-map-entries
  11292. `(org-todo ,arg)
  11293. org-loop-over-headlines-in-active-region
  11294. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11295. (if (equal arg '(16)) (setq arg 'nextset))
  11296. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11297. (let ((org-blocker-hook org-blocker-hook)
  11298. commentp
  11299. case-fold-search)
  11300. (when (equal arg '(64))
  11301. (setq arg nil org-blocker-hook nil))
  11302. (when (and org-blocker-hook
  11303. (or org-inhibit-blocking
  11304. (org-entry-get nil "NOBLOCKING")))
  11305. (setq org-blocker-hook nil))
  11306. (save-excursion
  11307. (catch 'exit
  11308. (org-back-to-heading t)
  11309. (when (org-in-commented-heading-p t)
  11310. (org-toggle-comment)
  11311. (setq commentp t))
  11312. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11313. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11314. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11315. (let* ((match-data (match-data))
  11316. (startpos (point-at-bol))
  11317. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11318. (org-log-done org-log-done)
  11319. (org-log-repeat org-log-repeat)
  11320. (org-todo-log-states org-todo-log-states)
  11321. (org-inhibit-logging
  11322. (if (equal arg 0)
  11323. (progn (setq arg nil) 'note) org-inhibit-logging))
  11324. (this (match-string 1))
  11325. (hl-pos (match-beginning 0))
  11326. (head (org-get-todo-sequence-head this))
  11327. (ass (assoc head org-todo-kwd-alist))
  11328. (interpret (nth 1 ass))
  11329. (done-word (nth 3 ass))
  11330. (final-done-word (nth 4 ass))
  11331. (org-last-state (or this ""))
  11332. (completion-ignore-case t)
  11333. (member (member this org-todo-keywords-1))
  11334. (tail (cdr member))
  11335. (org-state (cond
  11336. ((and org-todo-key-trigger
  11337. (or (and (equal arg '(4))
  11338. (eq org-use-fast-todo-selection 'prefix))
  11339. (and (not arg) org-use-fast-todo-selection
  11340. (not (eq org-use-fast-todo-selection
  11341. 'prefix)))))
  11342. ;; Use fast selection
  11343. (org-fast-todo-selection))
  11344. ((and (equal arg '(4))
  11345. (or (not org-use-fast-todo-selection)
  11346. (not org-todo-key-trigger)))
  11347. ;; Read a state with completion
  11348. (org-icompleting-read
  11349. "State: " (mapcar 'list org-todo-keywords-1)
  11350. nil t))
  11351. ((eq arg 'right)
  11352. (if this
  11353. (if tail (car tail) nil)
  11354. (car org-todo-keywords-1)))
  11355. ((eq arg 'left)
  11356. (if (equal member org-todo-keywords-1)
  11357. nil
  11358. (if this
  11359. (nth (- (length org-todo-keywords-1)
  11360. (length tail) 2)
  11361. org-todo-keywords-1)
  11362. (org-last org-todo-keywords-1))))
  11363. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11364. (setq arg nil))) ; hack to fall back to cycling
  11365. (arg
  11366. ;; user or caller requests a specific state
  11367. (cond
  11368. ((equal arg "") nil)
  11369. ((eq arg 'none) nil)
  11370. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11371. ((eq arg 'nextset)
  11372. (or (car (cdr (member head org-todo-heads)))
  11373. (car org-todo-heads)))
  11374. ((eq arg 'previousset)
  11375. (let ((org-todo-heads (reverse org-todo-heads)))
  11376. (or (car (cdr (member head org-todo-heads)))
  11377. (car org-todo-heads))))
  11378. ((car (member arg org-todo-keywords-1)))
  11379. ((stringp arg)
  11380. (user-error "State `%s' not valid in this file" arg))
  11381. ((nth (1- (prefix-numeric-value arg))
  11382. org-todo-keywords-1))))
  11383. ((null member) (or head (car org-todo-keywords-1)))
  11384. ((equal this final-done-word) nil) ;; -> make empty
  11385. ((null tail) nil) ;; -> first entry
  11386. ((memq interpret '(type priority))
  11387. (if (eq this-command last-command)
  11388. (car tail)
  11389. (if (> (length tail) 0)
  11390. (or done-word (car org-done-keywords))
  11391. nil)))
  11392. (t
  11393. (car tail))))
  11394. (org-state (or
  11395. (run-hook-with-args-until-success
  11396. 'org-todo-get-default-hook org-state org-last-state)
  11397. org-state))
  11398. (next (if org-state (concat " " org-state " ") " "))
  11399. (change-plist (list :type 'todo-state-change :from this :to org-state
  11400. :position startpos))
  11401. dolog now-done-p)
  11402. (when org-blocker-hook
  11403. (setq org-last-todo-state-is-todo
  11404. (not (member this org-done-keywords)))
  11405. (unless (save-excursion
  11406. (save-match-data
  11407. (org-with-wide-buffer
  11408. (run-hook-with-args-until-failure
  11409. 'org-blocker-hook change-plist))))
  11410. (if (org-called-interactively-p 'interactive)
  11411. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11412. this org-state org-block-entry-blocking)
  11413. ;; fail silently
  11414. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11415. this org-state org-block-entry-blocking)
  11416. (throw 'exit nil))))
  11417. (store-match-data match-data)
  11418. (replace-match next t t)
  11419. (cond ((equal this org-state)
  11420. (message "TODO state was already %s" (org-trim next)))
  11421. ((pos-visible-in-window-p hl-pos)
  11422. (message "TODO state changed to %s" (org-trim next))))
  11423. (unless head
  11424. (setq head (org-get-todo-sequence-head org-state)
  11425. ass (assoc head org-todo-kwd-alist)
  11426. interpret (nth 1 ass)
  11427. done-word (nth 3 ass)
  11428. final-done-word (nth 4 ass)))
  11429. (when (memq arg '(nextset previousset))
  11430. (message "Keyword-Set %d/%d: %s"
  11431. (- (length org-todo-sets) -1
  11432. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11433. (length org-todo-sets)
  11434. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11435. (setq org-last-todo-state-is-todo
  11436. (not (member org-state org-done-keywords)))
  11437. (setq now-done-p (and (member org-state org-done-keywords)
  11438. (not (member this org-done-keywords))))
  11439. (and logging (org-local-logging logging))
  11440. (when (and (or org-todo-log-states org-log-done)
  11441. (not (eq org-inhibit-logging t))
  11442. (not (memq arg '(nextset previousset))))
  11443. ;; we need to look at recording a time and note
  11444. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11445. (nth 2 (assoc this org-todo-log-states))))
  11446. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11447. (setq dolog 'time))
  11448. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11449. (and org-state
  11450. (member org-state org-not-done-keywords)
  11451. (not (member this org-not-done-keywords))))
  11452. ;; This is now a todo state and was not one before
  11453. ;; If there was a CLOSED time stamp, get rid of it.
  11454. (org-add-planning-info nil nil 'closed))
  11455. (when (and now-done-p org-log-done)
  11456. ;; It is now done, and it was not done before
  11457. (org-add-planning-info 'closed (org-current-effective-time))
  11458. (if (and (not dolog) (eq 'note org-log-done))
  11459. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11460. (when (and org-state dolog)
  11461. ;; This is a non-nil state, and we need to log it
  11462. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11463. ;; Fixup tag positioning
  11464. (org-todo-trigger-tag-changes org-state)
  11465. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11466. (when org-provide-todo-statistics
  11467. (org-update-parent-todo-statistics))
  11468. (run-hooks 'org-after-todo-state-change-hook)
  11469. (if (and arg (not (member org-state org-done-keywords)))
  11470. (setq head (org-get-todo-sequence-head org-state)))
  11471. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11472. ;; Do we need to trigger a repeat?
  11473. (when now-done-p
  11474. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11475. ;; This is for the agenda, take a snapshot of the headline.
  11476. (save-match-data
  11477. (setq org-agenda-headline-snapshot-before-repeat
  11478. (org-get-heading))))
  11479. (org-auto-repeat-maybe org-state))
  11480. ;; Fixup cursor location if close to the keyword
  11481. (if (and (outline-on-heading-p)
  11482. (not (bolp))
  11483. (save-excursion (beginning-of-line 1)
  11484. (looking-at org-todo-line-regexp))
  11485. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11486. (progn
  11487. (goto-char (or (match-end 2) (match-end 1)))
  11488. (and (looking-at " ") (just-one-space))))
  11489. (when org-trigger-hook
  11490. (save-excursion
  11491. (run-hook-with-args 'org-trigger-hook change-plist)))
  11492. (when commentp (org-toggle-comment))))))))
  11493. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11494. "Block turning an entry into a TODO, using the hierarchy.
  11495. This checks whether the current task should be blocked from state
  11496. changes. Such blocking occurs when:
  11497. 1. The task has children which are not all in a completed state.
  11498. 2. A task has a parent with the property :ORDERED:, and there
  11499. are siblings prior to the current task with incomplete
  11500. status.
  11501. 3. The parent of the task is blocked because it has siblings that should
  11502. be done first, or is child of a block grandparent TODO entry."
  11503. (if (not org-enforce-todo-dependencies)
  11504. t ; if locally turned off don't block
  11505. (catch 'dont-block
  11506. ;; If this is not a todo state change, or if this entry is already DONE,
  11507. ;; do not block
  11508. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11509. (member (plist-get change-plist :from)
  11510. (cons 'done org-done-keywords))
  11511. (member (plist-get change-plist :to)
  11512. (cons 'todo org-not-done-keywords))
  11513. (not (plist-get change-plist :to)))
  11514. (throw 'dont-block t))
  11515. ;; If this task has children, and any are undone, it's blocked
  11516. (save-excursion
  11517. (org-back-to-heading t)
  11518. (let ((this-level (funcall outline-level)))
  11519. (outline-next-heading)
  11520. (let ((child-level (funcall outline-level)))
  11521. (while (and (not (eobp))
  11522. (> child-level this-level))
  11523. ;; this todo has children, check whether they are all
  11524. ;; completed
  11525. (if (and (not (org-entry-is-done-p))
  11526. (org-entry-is-todo-p))
  11527. (progn (setq org-block-entry-blocking (org-get-heading))
  11528. (throw 'dont-block nil)))
  11529. (outline-next-heading)
  11530. (setq child-level (funcall outline-level))))))
  11531. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11532. ;; any previous siblings are undone, it's blocked
  11533. (save-excursion
  11534. (org-back-to-heading t)
  11535. (let* ((pos (point))
  11536. (parent-pos (and (org-up-heading-safe) (point))))
  11537. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11538. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11539. (forward-line 1)
  11540. (re-search-forward org-not-done-heading-regexp pos t))
  11541. (setq org-block-entry-blocking (match-string 0))
  11542. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11543. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11544. (while t
  11545. (goto-char parent-pos)
  11546. (if (not (looking-at org-not-done-heading-regexp))
  11547. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11548. (setq pos (point))
  11549. (setq parent-pos (and (org-up-heading-safe) (point)))
  11550. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11551. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11552. (forward-line 1)
  11553. (re-search-forward org-not-done-heading-regexp pos t)
  11554. (setq org-block-entry-blocking (org-get-heading)))
  11555. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11556. (defcustom org-track-ordered-property-with-tag nil
  11557. "Should the ORDERED property also be shown as a tag?
  11558. The ORDERED property decides if an entry should require subtasks to be
  11559. completed in sequence. Since a property is not very visible, setting
  11560. this option means that toggling the ORDERED property with the command
  11561. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11562. not relevant for the behavior, but it makes things more visible.
  11563. Note that toggling the tag with tags commands will not change the property
  11564. and therefore not influence behavior!
  11565. This can be t, meaning the tag ORDERED should be used, It can also be a
  11566. string to select a different tag for this task."
  11567. :group 'org-todo
  11568. :type '(choice
  11569. (const :tag "No tracking" nil)
  11570. (const :tag "Track with ORDERED tag" t)
  11571. (string :tag "Use other tag")))
  11572. (defun org-toggle-ordered-property ()
  11573. "Toggle the ORDERED property of the current entry.
  11574. For better visibility, you can track the value of this property with a tag.
  11575. See variable `org-track-ordered-property-with-tag'."
  11576. (interactive)
  11577. (let* ((t1 org-track-ordered-property-with-tag)
  11578. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11579. (save-excursion
  11580. (org-back-to-heading)
  11581. (if (org-entry-get nil "ORDERED")
  11582. (progn
  11583. (org-delete-property "ORDERED")
  11584. (and tag (org-toggle-tag tag 'off))
  11585. (message "Subtasks can be completed in arbitrary order"))
  11586. (org-entry-put nil "ORDERED" "t")
  11587. (and tag (org-toggle-tag tag 'on))
  11588. (message "Subtasks must be completed in sequence")))))
  11589. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11590. (defun org-block-todo-from-checkboxes (change-plist)
  11591. "Block turning an entry into a TODO, using checkboxes.
  11592. This checks whether the current task should be blocked from state
  11593. changes because there are unchecked boxes in this entry."
  11594. (if (not org-enforce-todo-checkbox-dependencies)
  11595. t ; if locally turned off don't block
  11596. (catch 'dont-block
  11597. ;; If this is not a todo state change, or if this entry is already DONE,
  11598. ;; do not block
  11599. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11600. (member (plist-get change-plist :from)
  11601. (cons 'done org-done-keywords))
  11602. (member (plist-get change-plist :to)
  11603. (cons 'todo org-not-done-keywords))
  11604. (not (plist-get change-plist :to)))
  11605. (throw 'dont-block t))
  11606. ;; If this task has checkboxes that are not checked, it's blocked
  11607. (save-excursion
  11608. (org-back-to-heading t)
  11609. (let ((beg (point)) end)
  11610. (outline-next-heading)
  11611. (setq end (point))
  11612. (goto-char beg)
  11613. (if (org-list-search-forward
  11614. (concat (org-item-beginning-re)
  11615. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11616. "\\[[- ]\\]")
  11617. end t)
  11618. (progn
  11619. (if (boundp 'org-blocked-by-checkboxes)
  11620. (setq org-blocked-by-checkboxes t))
  11621. (throw 'dont-block nil)))))
  11622. t))) ; do not block
  11623. (defun org-entry-blocked-p ()
  11624. "Is the current entry blocked?"
  11625. (org-with-silent-modifications
  11626. (if (org-entry-get nil "NOBLOCKING")
  11627. nil ;; Never block this entry
  11628. (not (run-hook-with-args-until-failure
  11629. 'org-blocker-hook
  11630. (list :type 'todo-state-change
  11631. :position (point)
  11632. :from 'todo
  11633. :to 'done))))))
  11634. (defun org-update-statistics-cookies (all)
  11635. "Update the statistics cookie, either from TODO or from checkboxes.
  11636. This should be called with the cursor in a line with a statistics cookie."
  11637. (interactive "P")
  11638. (if all
  11639. (progn
  11640. (org-update-checkbox-count 'all)
  11641. (org-map-entries 'org-update-parent-todo-statistics))
  11642. (if (not (org-at-heading-p))
  11643. (org-update-checkbox-count)
  11644. (let ((pos (point-marker))
  11645. end l1 l2)
  11646. (ignore-errors (org-back-to-heading t))
  11647. (if (not (org-at-heading-p))
  11648. (org-update-checkbox-count)
  11649. (setq l1 (org-outline-level))
  11650. (setq end (save-excursion
  11651. (outline-next-heading)
  11652. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11653. (point)))
  11654. (if (and (save-excursion
  11655. (re-search-forward
  11656. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11657. (not (save-excursion (re-search-forward
  11658. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11659. (org-update-checkbox-count)
  11660. (if (and l2 (> l2 l1))
  11661. (progn
  11662. (goto-char end)
  11663. (org-update-parent-todo-statistics))
  11664. (goto-char pos)
  11665. (beginning-of-line 1)
  11666. (while (re-search-forward
  11667. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11668. (point-at-eol) t)
  11669. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11670. (goto-char pos)
  11671. (move-marker pos nil)))))
  11672. (defvar org-entry-property-inherited-from) ;; defined below
  11673. (defun org-update-parent-todo-statistics ()
  11674. "Update any statistics cookie in the parent of the current headline.
  11675. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11676. the entire subtree and this will travel up the hierarchy and update
  11677. statistics everywhere."
  11678. (let* ((prop (save-excursion (org-up-heading-safe)
  11679. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11680. (recursive (or (not org-hierarchical-todo-statistics)
  11681. (and prop (string-match "\\<recursive\\>" prop))))
  11682. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11683. 0))
  11684. (first t)
  11685. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11686. level ltoggle l1 new ndel
  11687. (cnt-all 0) (cnt-done 0) is-percent kwd
  11688. checkbox-beg ov ovs ove cookie-present)
  11689. (catch 'exit
  11690. (save-excursion
  11691. (beginning-of-line 1)
  11692. (setq ltoggle (funcall outline-level))
  11693. ;; Three situations are to consider:
  11694. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11695. ;; to the top-level ancestor on the headline;
  11696. ;; 2. If parent has "recursive" property, repeat up to the
  11697. ;; headline setting that property, taking inheritance into
  11698. ;; account;
  11699. ;; 3. Else, move up to direct parent and proceed only once.
  11700. (while (and (setq level (org-up-heading-safe))
  11701. (or recursive first)
  11702. (>= (point) lim))
  11703. (setq first nil cookie-present nil)
  11704. (unless (and level
  11705. (not (string-match
  11706. "\\<checkbox\\>"
  11707. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11708. "")))))
  11709. (throw 'exit nil))
  11710. (while (re-search-forward box-re (point-at-eol) t)
  11711. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11712. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11713. (save-match-data
  11714. (unless (outline-next-heading) (throw 'exit nil))
  11715. (while (and (looking-at org-complex-heading-regexp)
  11716. (> (setq l1 (length (match-string 1))) level))
  11717. (setq kwd (and (or recursive (= l1 ltoggle))
  11718. (match-string 2)))
  11719. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11720. (and (eq org-provide-todo-statistics t)
  11721. (or (member kwd org-done-keywords)))
  11722. (and (listp org-provide-todo-statistics)
  11723. (stringp (car org-provide-todo-statistics))
  11724. (or (member kwd org-provide-todo-statistics)
  11725. (member kwd org-done-keywords)))
  11726. (and (listp org-provide-todo-statistics)
  11727. (listp (car org-provide-todo-statistics))
  11728. (or (member kwd (car org-provide-todo-statistics))
  11729. (and (member kwd org-done-keywords)
  11730. (member kwd (cadr org-provide-todo-statistics))))))
  11731. (setq cnt-all (1+ cnt-all))
  11732. (if (eq org-provide-todo-statistics t)
  11733. (and kwd (setq cnt-all (1+ cnt-all)))))
  11734. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11735. (member kwd org-done-keywords))
  11736. (and (listp org-provide-todo-statistics)
  11737. (listp (car org-provide-todo-statistics))
  11738. (member kwd org-done-keywords)
  11739. (member kwd (cadr org-provide-todo-statistics)))
  11740. (and (listp org-provide-todo-statistics)
  11741. (stringp (car org-provide-todo-statistics))
  11742. (member kwd org-done-keywords)))
  11743. (setq cnt-done (1+ cnt-done)))
  11744. (outline-next-heading)))
  11745. (setq new
  11746. (if is-percent
  11747. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11748. (format "[%d/%d]" cnt-done cnt-all))
  11749. ndel (- (match-end 0) checkbox-beg))
  11750. ;; handle overlays when updating cookie from column view
  11751. (when (setq ov (car (overlays-at checkbox-beg)))
  11752. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11753. (delete-overlay ov))
  11754. (goto-char checkbox-beg)
  11755. (insert new)
  11756. (delete-region (point) (+ (point) ndel))
  11757. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11758. (when ov (move-overlay ov ovs ove)))
  11759. (when cookie-present
  11760. (run-hook-with-args 'org-after-todo-statistics-hook
  11761. cnt-done (- cnt-all cnt-done))))))
  11762. (run-hooks 'org-todo-statistics-hook)))
  11763. (defvar org-after-todo-statistics-hook nil
  11764. "Hook that is called after a TODO statistics cookie has been updated.
  11765. Each function is called with two arguments: the number of not-done entries
  11766. and the number of done entries.
  11767. For example, the following function, when added to this hook, will switch
  11768. an entry to DONE when all children are done, and back to TODO when new
  11769. entries are set to a TODO status. Note that this hook is only called
  11770. when there is a statistics cookie in the headline!
  11771. (defun org-summary-todo (n-done n-not-done)
  11772. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11773. (let (org-log-done org-log-states) ; turn off logging
  11774. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11775. ")
  11776. (defvar org-todo-statistics-hook nil
  11777. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11778. This hook runs even if there is no statistics cookie present, in which case
  11779. `org-after-todo-statistics-hook' would not run.")
  11780. (defun org-todo-trigger-tag-changes (state)
  11781. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11782. (let ((l org-todo-state-tags-triggers)
  11783. changes)
  11784. (when (or (not state) (equal state ""))
  11785. (setq changes (append changes (cdr (assoc "" l)))))
  11786. (when (and (stringp state) (> (length state) 0))
  11787. (setq changes (append changes (cdr (assoc state l)))))
  11788. (when (member state org-not-done-keywords)
  11789. (setq changes (append changes (cdr (assoc 'todo l)))))
  11790. (when (member state org-done-keywords)
  11791. (setq changes (append changes (cdr (assoc 'done l)))))
  11792. (dolist (c changes)
  11793. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11794. (defun org-local-logging (value)
  11795. "Get logging settings from a property VALUE."
  11796. (let* (words w a)
  11797. ;; directly set the variables, they are already local.
  11798. (setq org-log-done nil
  11799. org-log-repeat nil
  11800. org-todo-log-states nil)
  11801. (setq words (org-split-string value))
  11802. (while (setq w (pop words))
  11803. (cond
  11804. ((setq a (assoc w org-startup-options))
  11805. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11806. (set (nth 1 a) (nth 2 a))))
  11807. ((setq a (org-extract-log-state-settings w))
  11808. (and (member (car a) org-todo-keywords-1)
  11809. (push a org-todo-log-states)))))))
  11810. (defun org-get-todo-sequence-head (kwd)
  11811. "Return the head of the TODO sequence to which KWD belongs.
  11812. If KWD is not set, check if there is a text property remembering the
  11813. right sequence."
  11814. (let (p)
  11815. (cond
  11816. ((not kwd)
  11817. (or (get-text-property (point-at-bol) 'org-todo-head)
  11818. (progn
  11819. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11820. nil (point-at-eol)))
  11821. (get-text-property p 'org-todo-head))))
  11822. ((not (member kwd org-todo-keywords-1))
  11823. (car org-todo-keywords-1))
  11824. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11825. (defun org-fast-todo-selection ()
  11826. "Fast TODO keyword selection with single keys.
  11827. Returns the new TODO keyword, or nil if no state change should occur."
  11828. (let* ((fulltable org-todo-key-alist)
  11829. (done-keywords org-done-keywords) ;; needed for the faces.
  11830. (maxlen (apply 'max (mapcar
  11831. (lambda (x)
  11832. (if (stringp (car x)) (string-width (car x)) 0))
  11833. fulltable)))
  11834. (expert nil)
  11835. (fwidth (+ maxlen 3 1 3))
  11836. (ncol (/ (- (window-width) 4) fwidth))
  11837. tg cnt e c tbl
  11838. groups ingroup)
  11839. (save-excursion
  11840. (save-window-excursion
  11841. (if expert
  11842. (set-buffer (get-buffer-create " *Org todo*"))
  11843. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11844. (erase-buffer)
  11845. (org-set-local 'org-done-keywords done-keywords)
  11846. (setq tbl fulltable cnt 0)
  11847. (while (setq e (pop tbl))
  11848. (cond
  11849. ((equal e '(:startgroup))
  11850. (push '() groups) (setq ingroup t)
  11851. (when (not (= cnt 0))
  11852. (setq cnt 0)
  11853. (insert "\n"))
  11854. (insert "{ "))
  11855. ((equal e '(:endgroup))
  11856. (setq ingroup nil cnt 0)
  11857. (insert "}\n"))
  11858. ((equal e '(:newline))
  11859. (when (not (= cnt 0))
  11860. (setq cnt 0)
  11861. (insert "\n")
  11862. (setq e (car tbl))
  11863. (while (equal (car tbl) '(:newline))
  11864. (insert "\n")
  11865. (setq tbl (cdr tbl)))))
  11866. (t
  11867. (setq tg (car e) c (cdr e))
  11868. (if ingroup (push tg (car groups)))
  11869. (setq tg (org-add-props tg nil 'face
  11870. (org-get-todo-face tg)))
  11871. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11872. (insert "[" c "] " tg (make-string
  11873. (- fwidth 4 (length tg)) ?\ ))
  11874. (when (= (setq cnt (1+ cnt)) ncol)
  11875. (insert "\n")
  11876. (if ingroup (insert " "))
  11877. (setq cnt 0)))))
  11878. (insert "\n")
  11879. (goto-char (point-min))
  11880. (if (not expert) (org-fit-window-to-buffer))
  11881. (message "[a-z..]:Set [SPC]:clear")
  11882. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11883. (cond
  11884. ((or (= c ?\C-g)
  11885. (and (= c ?q) (not (rassoc c fulltable))))
  11886. (setq quit-flag t))
  11887. ((= c ?\ ) nil)
  11888. ((setq e (rassoc c fulltable) tg (car e))
  11889. tg)
  11890. (t (setq quit-flag t)))))))
  11891. (defun org-entry-is-todo-p ()
  11892. (member (org-get-todo-state) org-not-done-keywords))
  11893. (defun org-entry-is-done-p ()
  11894. (member (org-get-todo-state) org-done-keywords))
  11895. (defun org-get-todo-state ()
  11896. "Return the TODO keyword of the current subtree."
  11897. (save-excursion
  11898. (org-back-to-heading t)
  11899. (and (looking-at org-todo-line-regexp)
  11900. (match-end 2)
  11901. (match-string 2))))
  11902. (defun org-at-date-range-p (&optional inactive-ok)
  11903. "Is the cursor inside a date range?"
  11904. (interactive)
  11905. (save-excursion
  11906. (catch 'exit
  11907. (let ((pos (point)))
  11908. (skip-chars-backward "^[<\r\n")
  11909. (skip-chars-backward "<[")
  11910. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11911. (>= (match-end 0) pos)
  11912. (throw 'exit t))
  11913. (skip-chars-backward "^<[\r\n")
  11914. (skip-chars-backward "<[")
  11915. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11916. (>= (match-end 0) pos)
  11917. (throw 'exit t)))
  11918. nil)))
  11919. (defun org-get-repeat (&optional tagline)
  11920. "Check if there is a deadline/schedule with repeater in this entry."
  11921. (save-match-data
  11922. (save-excursion
  11923. (org-back-to-heading t)
  11924. (and (re-search-forward (if tagline
  11925. (concat tagline "\\s-*" org-repeat-re)
  11926. org-repeat-re)
  11927. (org-entry-end-position) t)
  11928. (match-string-no-properties 1)))))
  11929. (defvar org-last-changed-timestamp)
  11930. (defvar org-last-inserted-timestamp)
  11931. (defvar org-log-post-message)
  11932. (defvar org-log-note-purpose)
  11933. (defvar org-log-note-how nil)
  11934. (defvar org-log-note-extra)
  11935. (defun org-auto-repeat-maybe (done-word)
  11936. "Check if the current headline contains a repeated deadline/schedule.
  11937. If yes, set TODO state back to what it was and change the base date
  11938. of repeating deadline/scheduled time stamps to new date.
  11939. This function is run automatically after each state change to a DONE state."
  11940. ;; last-state is dynamically scoped into this function
  11941. (let* ((repeat (org-get-repeat))
  11942. (aa (assoc org-last-state org-todo-kwd-alist))
  11943. (interpret (nth 1 aa))
  11944. (head (nth 2 aa))
  11945. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11946. (msg "Entry repeats: ")
  11947. (org-log-done nil)
  11948. (org-todo-log-states nil)
  11949. re type n what ts time to-state)
  11950. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11951. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11952. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11953. org-todo-repeat-to-state))
  11954. (unless (and to-state (member to-state org-todo-keywords-1))
  11955. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11956. (org-todo to-state)
  11957. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11958. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11959. (org-time-stamp-format t t))))
  11960. (when org-log-repeat
  11961. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11962. (memq 'org-add-log-note post-command-hook))
  11963. ;; OK, we are already setup for some record
  11964. (if (eq org-log-repeat 'note)
  11965. ;; make sure we take a note, not only a time stamp
  11966. (setq org-log-note-how 'note))
  11967. ;; Set up for taking a record
  11968. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11969. org-last-state
  11970. 'findpos org-log-repeat)))
  11971. (org-back-to-heading t)
  11972. (org-add-planning-info nil nil 'closed)
  11973. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11974. org-deadline-time-regexp "\\)\\|\\("
  11975. org-ts-regexp "\\)"))
  11976. (while (re-search-forward
  11977. re (save-excursion (outline-next-heading) (point)) t)
  11978. (setq type (if (match-end 1) org-scheduled-string
  11979. (if (match-end 3) org-deadline-string "Plain:"))
  11980. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11981. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11982. (setq n (string-to-number (match-string 2 ts))
  11983. what (match-string 3 ts))
  11984. (if (equal what "w") (setq n (* n 7) what "d"))
  11985. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11986. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11987. ;; Preparation, see if we need to modify the start date for the change
  11988. (when (match-end 1)
  11989. (setq time (save-match-data (org-time-string-to-time ts)))
  11990. (cond
  11991. ((equal (match-string 1 ts) ".")
  11992. ;; Shift starting date to today
  11993. (org-timestamp-change
  11994. (- (org-today) (time-to-days time))
  11995. 'day))
  11996. ((equal (match-string 1 ts) "+")
  11997. (let ((nshiftmax 10) (nshift 0))
  11998. (while (or (= nshift 0)
  11999. (<= (time-to-days time)
  12000. (time-to-days (current-time))))
  12001. (when (= (incf nshift) nshiftmax)
  12002. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12003. (user-error "Abort")))
  12004. (org-timestamp-change n (cdr (assoc what whata)))
  12005. (org-at-timestamp-p t)
  12006. (setq ts (match-string 1))
  12007. (setq time (save-match-data (org-time-string-to-time ts)))))
  12008. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12009. ;; rematch, so that we have everything in place for the real shift
  12010. (org-at-timestamp-p t)
  12011. (setq ts (match-string 1))
  12012. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12013. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12014. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12015. (setq org-log-post-message msg)
  12016. (message "%s" msg))))
  12017. (defun org-show-todo-tree (arg)
  12018. "Make a compact tree which shows all headlines marked with TODO.
  12019. The tree will show the lines where the regexp matches, and all higher
  12020. headlines above the match.
  12021. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12022. With a numeric prefix N, construct a sparse tree for the Nth element
  12023. of `org-todo-keywords-1'."
  12024. (interactive "P")
  12025. (let ((case-fold-search nil)
  12026. (kwd-re
  12027. (cond ((null arg) org-not-done-regexp)
  12028. ((equal arg '(4))
  12029. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12030. (mapcar 'list org-todo-keywords-1))))
  12031. (concat "\\("
  12032. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12033. "\\)\\>")))
  12034. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12035. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12036. org-todo-keywords-1)))
  12037. (t (user-error "Invalid prefix argument: %s" arg)))))
  12038. (message "%d TODO entries found"
  12039. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12040. (defun org-deadline (arg &optional time)
  12041. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12042. With one universal prefix argument, remove any deadline from the item.
  12043. With two universal prefix arguments, prompt for a warning delay.
  12044. With argument TIME, set the deadline at the corresponding date. TIME
  12045. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12046. (interactive "P")
  12047. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12048. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12049. 'region-start-level 'region))
  12050. org-loop-over-headlines-in-active-region)
  12051. (org-map-entries
  12052. `(org-deadline ',arg ,time)
  12053. org-loop-over-headlines-in-active-region
  12054. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12055. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12056. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12057. (repeater (and old-date
  12058. (string-match
  12059. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12060. old-date)
  12061. (match-string 1 old-date))))
  12062. (cond
  12063. ((equal arg '(4))
  12064. (when (and old-date org-log-redeadline)
  12065. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12066. org-log-redeadline))
  12067. (org-remove-timestamp-with-keyword org-deadline-string)
  12068. (message "Item no longer has a deadline."))
  12069. ((equal arg '(16))
  12070. (save-excursion
  12071. (org-back-to-heading t)
  12072. (if (re-search-forward
  12073. org-deadline-time-regexp
  12074. (save-excursion (outline-next-heading) (point)) t)
  12075. (let* ((rpl0 (match-string 1))
  12076. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12077. (replace-match
  12078. (concat org-deadline-string
  12079. " <" rpl
  12080. (format " -%dd"
  12081. (abs
  12082. (- (time-to-days
  12083. (save-match-data
  12084. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12085. (time-to-days old-date-time))))
  12086. ">") t t))
  12087. (user-error "No deadline information to update"))))
  12088. (t
  12089. (org-add-planning-info 'deadline time 'closed)
  12090. (when (and old-date org-log-redeadline
  12091. (not (equal old-date
  12092. (substring org-last-inserted-timestamp 1 -1))))
  12093. (org-add-log-setup 'redeadline nil old-date 'findpos
  12094. org-log-redeadline))
  12095. (when repeater
  12096. (save-excursion
  12097. (org-back-to-heading t)
  12098. (when (re-search-forward (concat org-deadline-string " "
  12099. org-last-inserted-timestamp)
  12100. (save-excursion
  12101. (outline-next-heading) (point)) t)
  12102. (goto-char (1- (match-end 0)))
  12103. (insert " " repeater)
  12104. (setq org-last-inserted-timestamp
  12105. (concat (substring org-last-inserted-timestamp 0 -1)
  12106. " " repeater
  12107. (substring org-last-inserted-timestamp -1))))))
  12108. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12109. (defun org-schedule (arg &optional time)
  12110. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12111. With one universal prefix argument, remove any scheduling date from the item.
  12112. With two universal prefix arguments, prompt for a delay cookie.
  12113. With argument TIME, scheduled at the corresponding date. TIME can
  12114. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12115. (interactive "P")
  12116. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12117. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12118. 'region-start-level 'region))
  12119. org-loop-over-headlines-in-active-region)
  12120. (org-map-entries
  12121. `(org-schedule ',arg ,time)
  12122. org-loop-over-headlines-in-active-region
  12123. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12124. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12125. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12126. (repeater (and old-date
  12127. (string-match
  12128. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12129. old-date)
  12130. (match-string 1 old-date))))
  12131. (cond
  12132. ((equal arg '(4))
  12133. (progn
  12134. (when (and old-date org-log-reschedule)
  12135. (org-add-log-setup 'delschedule nil old-date 'findpos
  12136. org-log-reschedule))
  12137. (org-remove-timestamp-with-keyword org-scheduled-string)
  12138. (message "Item is no longer scheduled.")))
  12139. ((equal arg '(16))
  12140. (save-excursion
  12141. (org-back-to-heading t)
  12142. (if (re-search-forward
  12143. org-scheduled-time-regexp
  12144. (save-excursion (outline-next-heading) (point)) t)
  12145. (let* ((rpl0 (match-string 1))
  12146. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12147. (replace-match
  12148. (concat org-scheduled-string
  12149. " <" rpl
  12150. (format " -%dd"
  12151. (abs
  12152. (- (time-to-days
  12153. (save-match-data
  12154. (org-read-date nil t nil "Delay until" old-date-time)))
  12155. (time-to-days old-date-time))))
  12156. ">") t t))
  12157. (user-error "No scheduled information to update"))))
  12158. (t
  12159. (org-add-planning-info 'scheduled time 'closed)
  12160. (when (and old-date org-log-reschedule
  12161. (not (equal old-date
  12162. (substring org-last-inserted-timestamp 1 -1))))
  12163. (org-add-log-setup 'reschedule nil old-date 'findpos
  12164. org-log-reschedule))
  12165. (when repeater
  12166. (save-excursion
  12167. (org-back-to-heading t)
  12168. (when (re-search-forward (concat org-scheduled-string " "
  12169. org-last-inserted-timestamp)
  12170. (save-excursion
  12171. (outline-next-heading) (point)) t)
  12172. (goto-char (1- (match-end 0)))
  12173. (insert " " repeater)
  12174. (setq org-last-inserted-timestamp
  12175. (concat (substring org-last-inserted-timestamp 0 -1)
  12176. " " repeater
  12177. (substring org-last-inserted-timestamp -1))))))
  12178. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12179. (defun org-get-scheduled-time (pom &optional inherit)
  12180. "Get the scheduled time as a time tuple, of a format suitable
  12181. for calling org-schedule with, or if there is no scheduling,
  12182. returns nil."
  12183. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12184. (when time
  12185. (apply 'encode-time (org-parse-time-string time)))))
  12186. (defun org-get-deadline-time (pom &optional inherit)
  12187. "Get the deadline as a time tuple, of a format suitable for
  12188. calling org-deadline with, or if there is no scheduling, returns
  12189. nil."
  12190. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12191. (when time
  12192. (apply 'encode-time (org-parse-time-string time)))))
  12193. (defun org-remove-timestamp-with-keyword (keyword)
  12194. "Remove all time stamps with KEYWORD in the current entry."
  12195. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12196. beg)
  12197. (save-excursion
  12198. (org-back-to-heading t)
  12199. (setq beg (point))
  12200. (outline-next-heading)
  12201. (while (re-search-backward re beg t)
  12202. (replace-match "")
  12203. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12204. (equal (char-before) ?\ ))
  12205. (backward-delete-char 1)
  12206. (if (string-match "^[ \t]*$" (buffer-substring
  12207. (point-at-bol) (point-at-eol)))
  12208. (delete-region (point-at-bol)
  12209. (min (point-max) (1+ (point-at-eol))))))))))
  12210. (defvar org-time-was-given) ; dynamically scoped parameter
  12211. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12212. (defun org-at-planning-p ()
  12213. "Non-nil when point is on a planning info line."
  12214. ;; This is as accurate and faster than `org-element-at-point' since
  12215. ;; planning info location is fixed in the section.
  12216. (org-with-wide-buffer
  12217. (beginning-of-line)
  12218. (and (org-looking-at-p org-planning-line-re)
  12219. (eq (point)
  12220. (ignore-errors
  12221. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12222. (org-back-to-heading t)
  12223. (org-with-limited-levels (org-back-to-heading t)))
  12224. (line-beginning-position 2))))))
  12225. (defun org-add-planning-info (what &optional time &rest remove)
  12226. "Insert new timestamp with keyword in the planning line.
  12227. WHAT indicates what kind of time stamp to add. It is a symbol
  12228. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12229. the time to use. If none is given, the user is prompted for
  12230. a date. REMOVE indicates what kind of entries to remove. An old
  12231. WHAT entry will also be removed."
  12232. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12233. (catch 'exit
  12234. (when (and (memq what '(scheduled deadline))
  12235. (or (not time)
  12236. (and (stringp time)
  12237. (string-match "^[-+]+[0-9]" time))))
  12238. ;; Try to get a default date/time from existing timestamp
  12239. (save-excursion
  12240. (org-back-to-heading t)
  12241. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12242. (when (re-search-forward (if (eq what 'scheduled)
  12243. org-scheduled-time-regexp
  12244. org-deadline-time-regexp)
  12245. end t)
  12246. (setq ts (match-string 1)
  12247. default-time (apply 'encode-time (org-parse-time-string ts))
  12248. default-input (and ts (org-get-compact-tod ts)))))))
  12249. (when what
  12250. (setq time
  12251. (if (stringp time)
  12252. ;; This is a string (relative or absolute), set
  12253. ;; proper date.
  12254. (apply #'encode-time
  12255. (org-read-date-analyze
  12256. time default-time (decode-time default-time)))
  12257. ;; If necessary, get the time from the user
  12258. (or time (org-read-date nil 'to-time nil nil
  12259. default-time default-input)))))
  12260. (org-with-wide-buffer
  12261. (org-back-to-heading t)
  12262. (forward-line)
  12263. (unless (bolp) (insert "\n"))
  12264. (cond ((org-looking-at-p org-planning-line-re)
  12265. ;; Move to current indentation.
  12266. (skip-chars-forward " \t")
  12267. ;; Check if we have to remove something.
  12268. (dolist (type (if what (cons what remove) remove))
  12269. (when (save-excursion
  12270. (re-search-forward
  12271. (case type
  12272. (closed org-closed-time-regexp)
  12273. (deadline org-deadline-time-regexp)
  12274. (scheduled org-scheduled-time-regexp)
  12275. (otherwise (error "Invalid planning type: %s" type)))
  12276. (line-end-position) t))
  12277. (replace-match "")
  12278. (when (looking-at "--+<[^>]+>") (replace-match ""))
  12279. (when (and (not what) (eq type 'closed))
  12280. (save-excursion
  12281. (beginning-of-line)
  12282. (if (looking-at "[ \t]*$")
  12283. (delete-region (point) (1+ (point-at-eol)))))))
  12284. ;; Remove leading white spaces.
  12285. (when (and (not (bolp)) (looking-at "[ \t]+")) (replace-match ""))))
  12286. ((not what) (throw 'exit nil)) ; Nothing to do.
  12287. (t (insert-before-markers "\n")
  12288. (backward-char 1)
  12289. (when org-adapt-indentation
  12290. (org-indent-to-column (1+ (org-outline-level))))))
  12291. (when what
  12292. ;; Insert planning keyword.
  12293. (insert (case what
  12294. (closed org-closed-string)
  12295. (deadline org-deadline-string)
  12296. (scheduled org-scheduled-string)
  12297. (otherwise (error "Invalid planning type: %s" what)))
  12298. " ")
  12299. ;; Insert associated timestamp.
  12300. (let ((ts (org-insert-time-stamp
  12301. time
  12302. (or org-time-was-given
  12303. (and (eq what 'closed) org-log-done-with-time))
  12304. (eq what 'closed)
  12305. nil nil (list org-end-time-was-given))))
  12306. (unless (eolp) (insert " "))
  12307. ts))))))
  12308. (defvar org-log-note-marker (make-marker))
  12309. (defvar org-log-note-purpose nil)
  12310. (defvar org-log-note-state nil)
  12311. (defvar org-log-note-previous-state nil)
  12312. (defvar org-log-note-extra nil)
  12313. (defvar org-log-note-window-configuration nil)
  12314. (defvar org-log-note-return-to (make-marker))
  12315. (defvar org-log-note-effective-time nil
  12316. "Remembered current time so that dynamically scoped
  12317. `org-extend-today-until' affects tha timestamps in state change
  12318. log")
  12319. (defvar org-log-post-message nil
  12320. "Message to be displayed after a log note has been stored.
  12321. The auto-repeater uses this.")
  12322. (defun org-add-note ()
  12323. "Add a note to the current entry.
  12324. This is done in the same way as adding a state change note."
  12325. (interactive)
  12326. (org-add-log-setup 'note nil nil 'findpos nil))
  12327. (defun org-log-beginning (&optional create)
  12328. "Return expected start of log notes in current entry.
  12329. When optional argument CREATE is non-nil, the function creates
  12330. a drawer to store notes, if necessary. Returned position ignores
  12331. narrowing."
  12332. (org-with-wide-buffer
  12333. (org-end-of-meta-data)
  12334. (let ((end (if (org-at-heading-p) (point)
  12335. (save-excursion (outline-next-heading) (point))))
  12336. (drawer (org-log-into-drawer)))
  12337. (cond
  12338. (drawer
  12339. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12340. (case-fold-search t))
  12341. (catch 'exit
  12342. ;; Try to find existing drawer.
  12343. (while (re-search-forward regexp end t)
  12344. (let ((element (org-element-at-point)))
  12345. (when (eq (org-element-type element) 'drawer)
  12346. (let ((cend (org-element-property :contents-end element)))
  12347. (when (and (not org-log-states-order-reversed) cend)
  12348. (goto-char cend)))
  12349. (throw 'exit nil))))
  12350. ;; No drawer found. Create one, if permitted.
  12351. (when create
  12352. (unless (bolp) (insert "\n"))
  12353. (let ((beg (point)))
  12354. (insert ":" drawer ":\n:END:\n")
  12355. (org-indent-region beg (point)))
  12356. (end-of-line -1)))))
  12357. (org-log-state-notes-insert-after-drawers
  12358. (while (and (looking-at org-drawer-regexp)
  12359. (progn (goto-char (match-end 0))
  12360. (re-search-forward org-property-end-re end t)))
  12361. (forward-line)))))
  12362. (if (bolp) (point) (line-beginning-position 2))))
  12363. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12364. "Set up the post command hook to take a note.
  12365. If this is about to TODO state change, the new state is expected in STATE.
  12366. When FINDPOS is non-nil, find the correct position for the note in
  12367. the current entry. If not, assume that it can be inserted at point.
  12368. HOW is an indicator what kind of note should be created.
  12369. EXTRA is additional text that will be inserted into the notes buffer."
  12370. (org-with-wide-buffer
  12371. (when findpos
  12372. (goto-char (org-log-beginning t))
  12373. (unless org-log-states-order-reversed
  12374. (org-skip-over-state-notes)
  12375. (skip-chars-backward " \t\n\r")
  12376. (forward-line)))
  12377. (move-marker org-log-note-marker (point))
  12378. ;; Preserve position even if a property drawer is inserted in the
  12379. ;; process.
  12380. (set-marker-insertion-type org-log-note-marker t)
  12381. (setq org-log-note-purpose purpose
  12382. org-log-note-state state
  12383. org-log-note-previous-state prev-state
  12384. org-log-note-how how
  12385. org-log-note-extra extra
  12386. org-log-note-effective-time (org-current-effective-time))
  12387. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12388. (defun org-skip-over-state-notes ()
  12389. "Skip past the list of State notes in an entry."
  12390. (when (ignore-errors (goto-char (org-in-item-p)))
  12391. (let* ((struct (org-list-struct))
  12392. (prevs (org-list-prevs-alist struct))
  12393. (regexp
  12394. (concat "[ \t]*- +"
  12395. (replace-regexp-in-string
  12396. " +" " +"
  12397. (org-replace-escapes
  12398. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12399. `(("%d" . ,org-ts-regexp-inactive)
  12400. ("%D" . ,org-ts-regexp)
  12401. ("%s" . "\"\\S-+\"")
  12402. ("%S" . "\"\\S-+\"")
  12403. ("%t" . ,org-ts-regexp-inactive)
  12404. ("%T" . ,org-ts-regexp)
  12405. ("%u" . ".*?")
  12406. ("%U" . ".*?")))))))
  12407. (while (org-looking-at-p regexp)
  12408. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12409. (org-list-get-item-end (point) struct)))))))
  12410. (defun org-add-log-note (&optional purpose)
  12411. "Pop up a window for taking a note, and add this note later at point."
  12412. (remove-hook 'post-command-hook 'org-add-log-note)
  12413. (setq org-log-note-window-configuration (current-window-configuration))
  12414. (delete-other-windows)
  12415. (move-marker org-log-note-return-to (point))
  12416. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12417. (goto-char org-log-note-marker)
  12418. (org-switch-to-buffer-other-window "*Org Note*")
  12419. (erase-buffer)
  12420. (if (memq org-log-note-how '(time state))
  12421. (let (current-prefix-arg) (org-store-log-note))
  12422. (let ((org-inhibit-startup t)) (org-mode))
  12423. (insert (format "# Insert note for %s.
  12424. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12425. (cond
  12426. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12427. ((eq org-log-note-purpose 'done) "closed todo item")
  12428. ((eq org-log-note-purpose 'state)
  12429. (format "state change from \"%s\" to \"%s\""
  12430. (or org-log-note-previous-state "")
  12431. (or org-log-note-state "")))
  12432. ((eq org-log-note-purpose 'reschedule)
  12433. "rescheduling")
  12434. ((eq org-log-note-purpose 'delschedule)
  12435. "no longer scheduled")
  12436. ((eq org-log-note-purpose 'redeadline)
  12437. "changing deadline")
  12438. ((eq org-log-note-purpose 'deldeadline)
  12439. "removing deadline")
  12440. ((eq org-log-note-purpose 'refile)
  12441. "refiling")
  12442. ((eq org-log-note-purpose 'note)
  12443. "this entry")
  12444. (t (error "This should not happen")))))
  12445. (if org-log-note-extra (insert org-log-note-extra))
  12446. (org-set-local 'org-finish-function 'org-store-log-note)
  12447. (run-hooks 'org-log-buffer-setup-hook)))
  12448. (defvar org-note-abort nil) ; dynamically scoped
  12449. (defun org-store-log-note ()
  12450. "Finish taking a log note, and insert it to where it belongs."
  12451. (let ((txt (buffer-string)))
  12452. (kill-buffer (current-buffer))
  12453. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12454. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12455. (setq txt (replace-match "" t t txt)))
  12456. (if (string-match "\\s-+\\'" txt)
  12457. (setq txt (replace-match "" t t txt)))
  12458. (setq lines (org-split-string txt "\n"))
  12459. (when (and note (string-match "\\S-" note))
  12460. (setq note
  12461. (org-replace-escapes
  12462. note
  12463. (list (cons "%u" (user-login-name))
  12464. (cons "%U" user-full-name)
  12465. (cons "%t" (format-time-string
  12466. (org-time-stamp-format 'long 'inactive)
  12467. org-log-note-effective-time))
  12468. (cons "%T" (format-time-string
  12469. (org-time-stamp-format 'long nil)
  12470. org-log-note-effective-time))
  12471. (cons "%d" (format-time-string
  12472. (org-time-stamp-format nil 'inactive)
  12473. org-log-note-effective-time))
  12474. (cons "%D" (format-time-string
  12475. (org-time-stamp-format nil nil)
  12476. org-log-note-effective-time))
  12477. (cons "%s" (if org-log-note-state
  12478. (concat "\"" org-log-note-state "\"")
  12479. ""))
  12480. (cons "%S" (if org-log-note-previous-state
  12481. (concat "\"" org-log-note-previous-state "\"")
  12482. "\"\"")))))
  12483. (when lines (setq note (concat note " \\\\")))
  12484. (push note lines))
  12485. (when (or current-prefix-arg org-note-abort)
  12486. (when (org-log-into-drawer)
  12487. (org-remove-empty-drawer-at org-log-note-marker))
  12488. (setq lines nil))
  12489. (when lines
  12490. (with-current-buffer (marker-buffer org-log-note-marker)
  12491. (save-excursion
  12492. (goto-char org-log-note-marker)
  12493. (move-marker org-log-note-marker nil)
  12494. ;; Make sure point is at the beginning of an empty line.
  12495. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12496. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12497. ;; In an existing list, add a new item at the top level.
  12498. ;; Otherwise, indent line like a regular one.
  12499. (let ((itemp (org-in-item-p)))
  12500. (if itemp
  12501. (org-indent-line-to
  12502. (let ((struct (save-excursion
  12503. (goto-char itemp) (org-list-struct))))
  12504. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12505. (org-indent-line)))
  12506. (insert (org-list-bullet-string "-") (pop lines))
  12507. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12508. (dolist (line lines)
  12509. (insert "\n")
  12510. (org-indent-line-to ind)
  12511. (insert line)))
  12512. (message "Note stored")
  12513. (org-back-to-heading t)
  12514. (org-cycle-hide-drawers 'children))
  12515. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12516. ;; from within `org-add-log-note' because `buffer-undo-list'
  12517. ;; is then modified outside of `org-with-remote-undo'.
  12518. (when (eq this-command 'org-agenda-todo)
  12519. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12520. ;; Don't add undo information when called from `org-agenda-todo'
  12521. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12522. (set-window-configuration org-log-note-window-configuration)
  12523. (with-current-buffer (marker-buffer org-log-note-return-to)
  12524. (goto-char org-log-note-return-to))
  12525. (move-marker org-log-note-return-to nil)
  12526. (and org-log-post-message (message "%s" org-log-post-message))))
  12527. (defun org-remove-empty-drawer-at (pos)
  12528. "Remove an empty drawer at position POS.
  12529. POS may also be a marker."
  12530. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12531. (org-with-wide-buffer
  12532. (goto-char pos)
  12533. (let ((drawer (org-element-at-point)))
  12534. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12535. (not (org-element-property :contents-begin drawer)))
  12536. (delete-region (org-element-property :begin drawer)
  12537. (progn (goto-char (org-element-property :end drawer))
  12538. (skip-chars-backward " \r\t\n")
  12539. (forward-line)
  12540. (point))))))))
  12541. (defvar org-ts-type nil)
  12542. (defun org-sparse-tree (&optional arg type)
  12543. "Create a sparse tree, prompt for the details.
  12544. This command can create sparse trees. You first need to select the type
  12545. of match used to create the tree:
  12546. t Show all TODO entries.
  12547. T Show entries with a specific TODO keyword.
  12548. m Show entries selected by a tags/property match.
  12549. p Enter a property name and its value (both with completion on existing
  12550. names/values) and show entries with that property.
  12551. r Show entries matching a regular expression (`/' can be used as well).
  12552. b Show deadlines and scheduled items before a date.
  12553. a Show deadlines and scheduled items after a date.
  12554. d Show deadlines due within `org-deadline-warning-days'.
  12555. D Show deadlines and scheduled items between a date range."
  12556. (interactive "P")
  12557. (setq type (or type org-sparse-tree-default-date-type))
  12558. (setq org-ts-type type)
  12559. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12560. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12561. [c]ycle through date types: %s"
  12562. (case type
  12563. (all "all timestamps")
  12564. (scheduled "only scheduled")
  12565. (deadline "only deadline")
  12566. (active "only active timestamps")
  12567. (inactive "only inactive timestamps")
  12568. (closed "with a closed time-stamp")
  12569. (otherwise "scheduled/deadline")))
  12570. (let ((answer (read-char-exclusive)))
  12571. (case answer
  12572. (?c
  12573. (org-sparse-tree
  12574. arg
  12575. (cadr
  12576. (memq type '(nil all scheduled deadline active inactive closed)))))
  12577. (?d (call-interactively 'org-check-deadlines))
  12578. (?b (call-interactively 'org-check-before-date))
  12579. (?a (call-interactively 'org-check-after-date))
  12580. (?D (call-interactively 'org-check-dates-range))
  12581. (?t (call-interactively 'org-show-todo-tree))
  12582. (?T (org-show-todo-tree '(4)))
  12583. (?m (call-interactively 'org-match-sparse-tree))
  12584. ((?p ?P)
  12585. (let* ((kwd (org-icompleting-read
  12586. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12587. (value (org-icompleting-read
  12588. "Value: " (mapcar 'list (org-property-values kwd)))))
  12589. (unless (string-match "\\`{.*}\\'" value)
  12590. (setq value (concat "\"" value "\"")))
  12591. (org-match-sparse-tree arg (concat kwd "=" value))))
  12592. ((?r ?R ?/) (call-interactively 'org-occur))
  12593. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12594. (defvar org-occur-highlights nil
  12595. "List of overlays used for occur matches.")
  12596. (make-variable-buffer-local 'org-occur-highlights)
  12597. (defvar org-occur-parameters nil
  12598. "Parameters of the active org-occur calls.
  12599. This is a list, each call to org-occur pushes as cons cell,
  12600. containing the regular expression and the callback, onto the list.
  12601. The list can contain several entries if `org-occur' has been called
  12602. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12603. will only contain one set of parameters. When the highlights are
  12604. removed (for example with `C-c C-c', or with the next edit (depending
  12605. on `org-remove-highlights-with-change'), this variable is emptied
  12606. as well.")
  12607. (make-variable-buffer-local 'org-occur-parameters)
  12608. (defun org-occur (regexp &optional keep-previous callback)
  12609. "Make a compact tree which shows all matches of REGEXP.
  12610. The tree will show the lines where the regexp matches, and all higher
  12611. headlines above the match. It will also show the heading after the match,
  12612. to make sure editing the matching entry is easy.
  12613. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12614. call to `org-occur' will be kept, to allow stacking of calls to this
  12615. command.
  12616. If CALLBACK is non-nil, it is a function which is called to confirm
  12617. that the match should indeed be shown."
  12618. (interactive "sRegexp: \nP")
  12619. (when (equal regexp "")
  12620. (user-error "Regexp cannot be empty"))
  12621. (unless keep-previous
  12622. (org-remove-occur-highlights nil nil t))
  12623. (push (cons regexp callback) org-occur-parameters)
  12624. (let ((cnt 0))
  12625. (save-excursion
  12626. (goto-char (point-min))
  12627. (if (or (not keep-previous) ; do not want to keep
  12628. (not org-occur-highlights)) ; no previous matches
  12629. ;; hide everything
  12630. (org-overview))
  12631. (while (re-search-forward regexp nil t)
  12632. (when (or (not callback)
  12633. (save-match-data (funcall callback)))
  12634. (setq cnt (1+ cnt))
  12635. (when org-highlight-sparse-tree-matches
  12636. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12637. (org-show-context 'occur-tree))))
  12638. (when org-remove-highlights-with-change
  12639. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12640. nil 'local))
  12641. (unless org-sparse-tree-open-archived-trees
  12642. (org-hide-archived-subtrees (point-min) (point-max)))
  12643. (run-hooks 'org-occur-hook)
  12644. (if (org-called-interactively-p 'interactive)
  12645. (message "%d match(es) for regexp %s" cnt regexp))
  12646. cnt))
  12647. (defun org-occur-next-match (&optional n reset)
  12648. "Function for `next-error-function' to find sparse tree matches.
  12649. N is the number of matches to move, when negative move backwards.
  12650. RESET is entirely ignored - this function always goes back to the
  12651. starting point when no match is found."
  12652. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12653. (search-func (if (< n 0)
  12654. 'previous-single-char-property-change
  12655. 'next-single-char-property-change))
  12656. (n (abs n))
  12657. (pos (point))
  12658. p1)
  12659. (catch 'exit
  12660. (while (setq p1 (funcall search-func (point) 'org-type))
  12661. (when (equal p1 limit)
  12662. (goto-char pos)
  12663. (user-error "No more matches"))
  12664. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12665. (setq n (1- n))
  12666. (when (= n 0)
  12667. (goto-char p1)
  12668. (throw 'exit (point))))
  12669. (goto-char p1))
  12670. (goto-char p1)
  12671. (user-error "No more matches"))))
  12672. (defun org-show-context (&optional key)
  12673. "Make sure point and context are visible.
  12674. Optional argument KEY, when non-nil, is a symbol. See
  12675. `org-show-context-detail' for allowed values and how much is to
  12676. be shown."
  12677. (org-show-set-visibility
  12678. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12679. ((cdr (assq key org-show-context-detail)))
  12680. (t (cdr (assq 'default org-show-context-detail))))))
  12681. (defun org-show-set-visibility (detail)
  12682. "Set visibility around point according to DETAIL.
  12683. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12684. `tree', `canonical' or t. See `org-show-context-detail' for more
  12685. information."
  12686. (unless (org-before-first-heading-p)
  12687. ;; Show current heading and possibly its entry, following headline
  12688. ;; or all children.
  12689. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12690. (org-flag-heading nil)
  12691. (org-show-entry)
  12692. (org-with-limited-levels
  12693. (case detail
  12694. ((tree canonical t) (show-children))
  12695. ((nil minimal ancestors))
  12696. (t (save-excursion
  12697. (outline-next-heading)
  12698. (org-flag-heading nil))))))
  12699. ;; Show all siblings.
  12700. (when (eq detail 'lineage) (org-show-siblings))
  12701. ;; Show ancestors, possibly with their children.
  12702. (when (memq detail '(ancestors lineage tree canonical t))
  12703. (save-excursion
  12704. (while (org-up-heading-safe)
  12705. (org-flag-heading nil)
  12706. (when (memq detail '(canonical t)) (org-show-entry))
  12707. (when (memq detail '(tree canonical t)) (show-children)))))))
  12708. (defvar org-reveal-start-hook nil
  12709. "Hook run before revealing a location.")
  12710. (defun org-reveal (&optional siblings)
  12711. "Show current entry, hierarchy above it, and the following headline.
  12712. This can be used to show a consistent set of context around
  12713. locations exposed with `org-show-context'.
  12714. With optional argument SIBLINGS, on each level of the hierarchy all
  12715. siblings are shown. This repairs the tree structure to what it would
  12716. look like when opened with hierarchical calls to `org-cycle'.
  12717. With double optional argument \\[universal-argument] \\[universal-argument], \
  12718. go to the parent and show the
  12719. entire tree."
  12720. (interactive "P")
  12721. (run-hooks 'org-reveal-start-hook)
  12722. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12723. ((equal siblings '(16))
  12724. (save-excursion
  12725. (when (org-up-heading-safe)
  12726. (org-show-subtree)
  12727. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12728. (t (org-show-set-visibility 'lineage))))
  12729. (defun org-highlight-new-match (beg end)
  12730. "Highlight from BEG to END and mark the highlight is an occur headline."
  12731. (let ((ov (make-overlay beg end)))
  12732. (overlay-put ov 'face 'secondary-selection)
  12733. (overlay-put ov 'org-type 'org-occur)
  12734. (push ov org-occur-highlights)))
  12735. (defun org-remove-occur-highlights (&optional beg end noremove)
  12736. "Remove the occur highlights from the buffer.
  12737. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12738. from the `before-change-functions' in the current buffer."
  12739. (interactive)
  12740. (unless org-inhibit-highlight-removal
  12741. (mapc 'delete-overlay org-occur-highlights)
  12742. (setq org-occur-highlights nil)
  12743. (setq org-occur-parameters nil)
  12744. (unless noremove
  12745. (remove-hook 'before-change-functions
  12746. 'org-remove-occur-highlights 'local))))
  12747. ;;;; Priorities
  12748. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12749. "Regular expression matching the priority indicator.")
  12750. (defvar org-remove-priority-next-time nil)
  12751. (defun org-priority-up ()
  12752. "Increase the priority of the current item."
  12753. (interactive)
  12754. (org-priority 'up))
  12755. (defun org-priority-down ()
  12756. "Decrease the priority of the current item."
  12757. (interactive)
  12758. (org-priority 'down))
  12759. (defun org-priority (&optional action show)
  12760. "Change the priority of an item.
  12761. ACTION can be `set', `up', `down', or a character."
  12762. (interactive "P")
  12763. (if (equal action '(4))
  12764. (org-show-priority)
  12765. (unless org-enable-priority-commands
  12766. (user-error "Priority commands are disabled"))
  12767. (setq action (or action 'set))
  12768. (let (current new news have remove)
  12769. (save-excursion
  12770. (org-back-to-heading t)
  12771. (if (looking-at org-priority-regexp)
  12772. (setq current (string-to-char (match-string 2))
  12773. have t))
  12774. (cond
  12775. ((eq action 'remove)
  12776. (setq remove t new ?\ ))
  12777. ((or (eq action 'set)
  12778. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12779. (if (not (eq action 'set))
  12780. (setq new action)
  12781. (message "Priority %c-%c, SPC to remove: "
  12782. org-highest-priority org-lowest-priority)
  12783. (save-match-data
  12784. (setq new (read-char-exclusive))))
  12785. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12786. (= (upcase org-lowest-priority) org-lowest-priority))
  12787. (setq new (upcase new)))
  12788. (cond ((equal new ?\ ) (setq remove t))
  12789. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12790. (user-error "Priority must be between `%c' and `%c'"
  12791. org-highest-priority org-lowest-priority))))
  12792. ((eq action 'up)
  12793. (setq new (if have
  12794. (1- current) ; normal cycling
  12795. ;; last priority was empty
  12796. (if (eq last-command this-command)
  12797. org-lowest-priority ; wrap around empty to lowest
  12798. ;; default
  12799. (if org-priority-start-cycle-with-default
  12800. org-default-priority
  12801. (1- org-default-priority))))))
  12802. ((eq action 'down)
  12803. (setq new (if have
  12804. (1+ current) ; normal cycling
  12805. ;; last priority was empty
  12806. (if (eq last-command this-command)
  12807. org-highest-priority ; wrap around empty to highest
  12808. ;; default
  12809. (if org-priority-start-cycle-with-default
  12810. org-default-priority
  12811. (1+ org-default-priority))))))
  12812. (t (user-error "Invalid action")))
  12813. (if (or (< (upcase new) org-highest-priority)
  12814. (> (upcase new) org-lowest-priority))
  12815. (if (and (memq action '(up down))
  12816. (not have) (not (eq last-command this-command)))
  12817. ;; `new' is from default priority
  12818. (error
  12819. "The default can not be set, see `org-default-priority' why")
  12820. ;; normal cycling: `new' is beyond highest/lowest priority
  12821. ;; and is wrapped around to the empty priority
  12822. (setq remove t)))
  12823. (setq news (format "%c" new))
  12824. (if have
  12825. (if remove
  12826. (replace-match "" t t nil 1)
  12827. (replace-match news t t nil 2))
  12828. (if remove
  12829. (user-error "No priority cookie found in line")
  12830. (let ((case-fold-search nil))
  12831. (looking-at org-todo-line-regexp))
  12832. (if (match-end 2)
  12833. (progn
  12834. (goto-char (match-end 2))
  12835. (insert " [#" news "]"))
  12836. (goto-char (match-beginning 3))
  12837. (insert "[#" news "] "))))
  12838. (org-set-tags nil 'align))
  12839. (if remove
  12840. (message "Priority removed")
  12841. (message "Priority of current item set to %s" news)))))
  12842. (defun org-show-priority ()
  12843. "Show the priority of the current item.
  12844. This priority is composed of the main priority given with the [#A] cookies,
  12845. and by additional input from the age of a schedules or deadline entry."
  12846. (interactive)
  12847. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12848. (org-get-at-bol 'priority)
  12849. (save-excursion
  12850. (save-match-data
  12851. (beginning-of-line)
  12852. (and (looking-at org-heading-regexp)
  12853. (org-get-priority (match-string 0))))))))
  12854. (message "Priority is %d" (if pri pri -1000))))
  12855. (defun org-get-priority (s)
  12856. "Find priority cookie and return priority."
  12857. (save-match-data
  12858. (if (functionp org-get-priority-function)
  12859. (funcall org-get-priority-function)
  12860. (if (not (string-match org-priority-regexp s))
  12861. (* 1000 (- org-lowest-priority org-default-priority))
  12862. (* 1000 (- org-lowest-priority
  12863. (string-to-char (match-string 2 s))))))))
  12864. ;;;; Tags
  12865. (defvar org-agenda-archives-mode)
  12866. (defvar org-map-continue-from nil
  12867. "Position from where mapping should continue.
  12868. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12869. (defvar org-scanner-tags nil
  12870. "The current tag list while the tags scanner is running.")
  12871. (defvar org-trust-scanner-tags nil
  12872. "Should `org-get-tags-at' use the tags for the scanner.
  12873. This is for internal dynamical scoping only.
  12874. When this is non-nil, the function `org-get-tags-at' will return the value
  12875. of `org-scanner-tags' instead of building the list by itself. This
  12876. can lead to large speed-ups when the tags scanner is used in a file with
  12877. many entries, and when the list of tags is retrieved, for example to
  12878. obtain a list of properties. Building the tags list for each entry in such
  12879. a file becomes an N^2 operation - but with this variable set, it scales
  12880. as N.")
  12881. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12882. "Scan headline tags with inheritance and produce output ACTION.
  12883. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12884. or `agenda' to produce an entry list for an agenda view. It can also be
  12885. a Lisp form or a function that should be called at each matched headline, in
  12886. this case the return value is a list of all return values from these calls.
  12887. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12888. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12889. only lines with a not-done TODO keyword are included in the output.
  12890. This should be the same variable that was scoped into
  12891. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12892. START-LEVEL can be a string with asterisks, reducing the scope to
  12893. headlines matching this string."
  12894. (require 'org-agenda)
  12895. (let* ((re (concat "^"
  12896. (if start-level
  12897. ;; Get the correct level to match
  12898. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12899. org-outline-regexp)
  12900. " *\\(\\<\\("
  12901. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12902. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12903. (props (list 'face 'default
  12904. 'done-face 'org-agenda-done
  12905. 'undone-face 'default
  12906. 'mouse-face 'highlight
  12907. 'org-not-done-regexp org-not-done-regexp
  12908. 'org-todo-regexp org-todo-regexp
  12909. 'org-complex-heading-regexp org-complex-heading-regexp
  12910. 'help-echo
  12911. (format "mouse-2 or RET jump to org file %s"
  12912. (abbreviate-file-name
  12913. (or (buffer-file-name (buffer-base-buffer))
  12914. (buffer-name (buffer-base-buffer)))))))
  12915. (org-map-continue-from nil)
  12916. lspos tags tags-list
  12917. (tags-alist (list (cons 0 org-file-tags)))
  12918. (llast 0) rtn rtn1 level category i txt
  12919. todo marker entry priority
  12920. ts-date ts-date-type ts-date-pair)
  12921. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12922. (setq action (list 'lambda nil action)))
  12923. (save-excursion
  12924. (goto-char (point-min))
  12925. (when (eq action 'sparse-tree)
  12926. (org-overview)
  12927. (org-remove-occur-highlights))
  12928. (while (let (case-fold-search)
  12929. (re-search-forward re nil t))
  12930. (setq org-map-continue-from nil)
  12931. (catch :skip
  12932. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12933. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12934. (goto-char (setq lspos (match-beginning 0)))
  12935. (setq level (org-reduced-level (org-outline-level))
  12936. category (org-get-category))
  12937. (when (eq action 'agenda)
  12938. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12939. ts-date (car ts-date-pair)
  12940. ts-date-type (cdr ts-date-pair)))
  12941. (setq i llast llast level)
  12942. ;; remove tag lists from same and sublevels
  12943. (while (>= i level)
  12944. (when (setq entry (assoc i tags-alist))
  12945. (setq tags-alist (delete entry tags-alist)))
  12946. (setq i (1- i)))
  12947. ;; add the next tags
  12948. (when tags
  12949. (setq tags (org-split-string tags ":")
  12950. tags-alist
  12951. (cons (cons level tags) tags-alist)))
  12952. ;; compile tags for current headline
  12953. (setq tags-list
  12954. (if org-use-tag-inheritance
  12955. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12956. tags)
  12957. org-scanner-tags tags-list)
  12958. (when org-use-tag-inheritance
  12959. (setcdr (car tags-alist)
  12960. (mapcar (lambda (x)
  12961. (setq x (copy-sequence x))
  12962. (org-add-prop-inherited x))
  12963. (cdar tags-alist))))
  12964. (when (and tags org-use-tag-inheritance
  12965. (or (not (eq t org-use-tag-inheritance))
  12966. org-tags-exclude-from-inheritance))
  12967. ;; selective inheritance, remove uninherited ones
  12968. (setcdr (car tags-alist)
  12969. (org-remove-uninherited-tags (cdar tags-alist))))
  12970. (when (and
  12971. ;; eval matcher only when the todo condition is OK
  12972. (and (or (not todo-only) (member todo org-not-done-keywords))
  12973. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12974. (eval matcher)))
  12975. ;; Call the skipper, but return t if it does not skip,
  12976. ;; so that the `and' form continues evaluating
  12977. (progn
  12978. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12979. t)
  12980. ;; Check if timestamps are deselecting this entry
  12981. (or (not todo-only)
  12982. (and (member todo org-not-done-keywords)
  12983. (or (not org-agenda-tags-todo-honor-ignore-options)
  12984. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12985. ;; select this headline
  12986. (cond
  12987. ((eq action 'sparse-tree)
  12988. (and org-highlight-sparse-tree-matches
  12989. (org-get-heading) (match-end 0)
  12990. (org-highlight-new-match
  12991. (match-beginning 1) (match-end 1)))
  12992. (org-show-context 'tags-tree))
  12993. ((eq action 'agenda)
  12994. (setq txt (org-agenda-format-item
  12995. ""
  12996. (concat
  12997. (if (eq org-tags-match-list-sublevels 'indented)
  12998. (make-string (1- level) ?.) "")
  12999. (org-get-heading))
  13000. (make-string level ?\s)
  13001. category
  13002. tags-list)
  13003. priority (org-get-priority txt))
  13004. (goto-char lspos)
  13005. (setq marker (org-agenda-new-marker))
  13006. (org-add-props txt props
  13007. 'org-marker marker 'org-hd-marker marker 'org-category category
  13008. 'todo-state todo
  13009. 'ts-date ts-date
  13010. 'priority priority
  13011. 'type (concat "tagsmatch" ts-date-type))
  13012. (push txt rtn))
  13013. ((functionp action)
  13014. (setq org-map-continue-from nil)
  13015. (save-excursion
  13016. (setq rtn1 (funcall action))
  13017. (push rtn1 rtn)))
  13018. (t (user-error "Invalid action")))
  13019. ;; if we are to skip sublevels, jump to end of subtree
  13020. (unless org-tags-match-list-sublevels
  13021. (org-end-of-subtree t)
  13022. (backward-char 1))))
  13023. ;; Get the correct position from where to continue
  13024. (if org-map-continue-from
  13025. (goto-char org-map-continue-from)
  13026. (and (= (point) lspos) (end-of-line 1)))))
  13027. (when (and (eq action 'sparse-tree)
  13028. (not org-sparse-tree-open-archived-trees))
  13029. (org-hide-archived-subtrees (point-min) (point-max)))
  13030. (nreverse rtn)))
  13031. (defun org-remove-uninherited-tags (tags)
  13032. "Remove all tags that are not inherited from the list TAGS."
  13033. (cond
  13034. ((eq org-use-tag-inheritance t)
  13035. (if org-tags-exclude-from-inheritance
  13036. (org-delete-all org-tags-exclude-from-inheritance tags)
  13037. tags))
  13038. ((not org-use-tag-inheritance) nil)
  13039. ((stringp org-use-tag-inheritance)
  13040. (delq nil (mapcar
  13041. (lambda (x)
  13042. (if (and (string-match org-use-tag-inheritance x)
  13043. (not (member x org-tags-exclude-from-inheritance)))
  13044. x nil))
  13045. tags)))
  13046. ((listp org-use-tag-inheritance)
  13047. (delq nil (mapcar
  13048. (lambda (x)
  13049. (if (member x org-use-tag-inheritance) x nil))
  13050. tags)))))
  13051. (defun org-match-sparse-tree (&optional todo-only match)
  13052. "Create a sparse tree according to tags string MATCH.
  13053. MATCH can contain positive and negative selection of tags, like
  13054. \"+WORK+URGENT-WITHBOSS\".
  13055. If optional argument TODO-ONLY is non-nil, only select lines that are
  13056. also TODO lines."
  13057. (interactive "P")
  13058. (org-agenda-prepare-buffers (list (current-buffer)))
  13059. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13060. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13061. (defvar org-cached-props nil)
  13062. (defun org-cached-entry-get (pom property)
  13063. (if (or (eq t org-use-property-inheritance)
  13064. (and (stringp org-use-property-inheritance)
  13065. (let ((case-fold-search t))
  13066. (org-string-match-p org-use-property-inheritance property)))
  13067. (and (listp org-use-property-inheritance)
  13068. (member-ignore-case property org-use-property-inheritance)))
  13069. ;; Caching is not possible, check it directly.
  13070. (org-entry-get pom property 'inherit)
  13071. ;; Get all properties, so we can do complicated checks easily.
  13072. (cdr (assoc-string property
  13073. (or org-cached-props
  13074. (setq org-cached-props (org-entry-properties pom)))
  13075. t))))
  13076. (defun org-global-tags-completion-table (&optional files)
  13077. "Return the list of all tags in all agenda buffer/files.
  13078. Optional FILES argument is a list of files which can be used
  13079. instead of the agenda files."
  13080. (save-excursion
  13081. (org-uniquify
  13082. (delq nil
  13083. (apply 'append
  13084. (mapcar
  13085. (lambda (file)
  13086. (set-buffer (find-file-noselect file))
  13087. (append (org-get-buffer-tags)
  13088. (mapcar (lambda (x) (if (stringp (car-safe x))
  13089. (list (car-safe x)) nil))
  13090. org-tag-alist)))
  13091. (if (and files (car files))
  13092. files
  13093. (org-agenda-files))))))))
  13094. (defun org-make-tags-matcher (match)
  13095. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13096. The variable `todo-only' is scoped dynamically into this function.
  13097. It will be set to t if the matcher restricts matching to TODO entries,
  13098. otherwise will not be touched.
  13099. Returns a cons of the selection string MATCH and the constructed
  13100. lisp form implementing the matcher. The matcher is to be evaluated
  13101. at an Org entry, with point on the headline, and returns t if the
  13102. entry matches the selection string MATCH. The returned lisp form
  13103. references two variables with information about the entry, which
  13104. must be bound around the form's evaluation: todo, the TODO keyword
  13105. at the entry (or nil of none); and tags-list, the list of all tags
  13106. at the entry including inherited ones. Additionally, the category
  13107. of the entry (if any) must be specified as the text property
  13108. 'org-category on the headline.
  13109. See also `org-scan-tags'.
  13110. "
  13111. (declare (special todo-only))
  13112. (unless (boundp 'todo-only)
  13113. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13114. (unless match
  13115. ;; Get a new match request, with completion against the global
  13116. ;; tags table and the local tags in current buffer
  13117. (let ((org-last-tags-completion-table
  13118. (org-uniquify
  13119. (delq nil (append (org-get-buffer-tags)
  13120. (org-global-tags-completion-table))))))
  13121. (setq match (org-completing-read-no-i
  13122. "Match: " 'org-tags-completion-function nil nil nil
  13123. 'org-tags-history))))
  13124. ;; Parse the string and create a lisp form
  13125. (let ((match0 match)
  13126. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13127. minus tag mm
  13128. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13129. orterms term orlist re-p str-p level-p level-op time-p
  13130. prop-p pn pv po gv rest (start 0) (ss 0))
  13131. ;; Expand group tags
  13132. (setq match (org-tags-expand match))
  13133. ;; Check if there is a TODO part of this match, which would be the
  13134. ;; part after a "/". TO make sure that this slash is not part of
  13135. ;; a property value to be matched against, we also check that there
  13136. ;; is no " after that slash.
  13137. ;; First, find the last slash
  13138. (while (string-match "/+" match ss)
  13139. (setq start (match-beginning 0) ss (match-end 0)))
  13140. (if (and (string-match "/+" match start)
  13141. (not (save-match-data (string-match "\"" match start))))
  13142. ;; match contains also a todo-matching request
  13143. (progn
  13144. (setq tagsmatch (substring match 0 (match-beginning 0))
  13145. todomatch (substring match (match-end 0)))
  13146. (if (string-match "^!" todomatch)
  13147. (setq todo-only t todomatch (substring todomatch 1)))
  13148. (if (string-match "^\\s-*$" todomatch)
  13149. (setq todomatch nil)))
  13150. ;; only matching tags
  13151. (setq tagsmatch match todomatch nil))
  13152. ;; Make the tags matcher
  13153. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13154. (setq tagsmatcher t)
  13155. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13156. (while (setq term (pop orterms))
  13157. (while (and (equal (substring term -1) "\\") orterms)
  13158. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13159. (while (string-match re term)
  13160. (setq rest (substring term (match-end 0))
  13161. minus (and (match-end 1)
  13162. (equal (match-string 1 term) "-"))
  13163. tag (save-match-data (replace-regexp-in-string
  13164. "\\\\-" "-"
  13165. (match-string 2 term)))
  13166. re-p (equal (string-to-char tag) ?{)
  13167. level-p (match-end 4)
  13168. prop-p (match-end 5)
  13169. mm (cond
  13170. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13171. (level-p
  13172. (setq level-op (org-op-to-function (match-string 3 term)))
  13173. `(,level-op level ,(string-to-number
  13174. (match-string 4 term))))
  13175. (prop-p
  13176. (setq pn (match-string 5 term)
  13177. po (match-string 6 term)
  13178. pv (match-string 7 term)
  13179. re-p (equal (string-to-char pv) ?{)
  13180. str-p (equal (string-to-char pv) ?\")
  13181. time-p (save-match-data
  13182. (string-match "^\"[[<].*[]>]\"$" pv))
  13183. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13184. (if time-p (setq pv (org-matcher-time pv)))
  13185. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13186. (cond
  13187. ((equal pn "CATEGORY")
  13188. (setq gv '(get-text-property (point) 'org-category)))
  13189. ((equal pn "TODO")
  13190. (setq gv 'todo))
  13191. (t
  13192. (setq gv `(org-cached-entry-get nil ,pn))))
  13193. (if re-p
  13194. (if (eq po 'org<>)
  13195. `(not (string-match ,pv (or ,gv "")))
  13196. `(string-match ,pv (or ,gv "")))
  13197. (if str-p
  13198. `(,po (or ,gv "") ,pv)
  13199. `(,po (string-to-number (or ,gv ""))
  13200. ,(string-to-number pv) ))))
  13201. (t `(member ,tag tags-list)))
  13202. mm (if minus (list 'not mm) mm)
  13203. term rest)
  13204. (push mm tagsmatcher))
  13205. (push (if (> (length tagsmatcher) 1)
  13206. (cons 'and tagsmatcher)
  13207. (car tagsmatcher))
  13208. orlist)
  13209. (setq tagsmatcher nil))
  13210. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13211. (setq tagsmatcher
  13212. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13213. ;; Make the todo matcher
  13214. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13215. (setq todomatcher t)
  13216. (setq orterms (org-split-string todomatch "|") orlist nil)
  13217. (while (setq term (pop orterms))
  13218. (while (string-match re term)
  13219. (setq minus (and (match-end 1)
  13220. (equal (match-string 1 term) "-"))
  13221. kwd (match-string 2 term)
  13222. re-p (equal (string-to-char kwd) ?{)
  13223. term (substring term (match-end 0))
  13224. mm (if re-p
  13225. `(string-match ,(substring kwd 1 -1) todo)
  13226. (list 'equal 'todo kwd))
  13227. mm (if minus (list 'not mm) mm))
  13228. (push mm todomatcher))
  13229. (push (if (> (length todomatcher) 1)
  13230. (cons 'and todomatcher)
  13231. (car todomatcher))
  13232. orlist)
  13233. (setq todomatcher nil))
  13234. (setq todomatcher (if (> (length orlist) 1)
  13235. (cons 'or orlist) (car orlist))))
  13236. ;; Return the string and lisp forms of the matcher
  13237. (setq matcher (if todomatcher
  13238. (list 'and tagsmatcher todomatcher)
  13239. tagsmatcher))
  13240. (when todo-only
  13241. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13242. matcher)))
  13243. (cons match0 matcher)))
  13244. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13245. "Expand group tags in MATCH.
  13246. This replaces every group tag in MATCH with a regexp tag search.
  13247. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13248. will be replaced like this:
  13249. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13250. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13251. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13252. Replacing by a regexp preserves the structure of the match.
  13253. E.g., this expansion
  13254. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13255. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13256. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13257. A group tag in MATCH can contain regular expressions of its own.
  13258. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13259. will be replaced like this:
  13260. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13261. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13262. assumed to be a single group tag, and the function will return
  13263. the list of tags in this group.
  13264. When DOWNCASE is non-nil, expand downcased TAGS."
  13265. (if org-group-tags
  13266. (let* ((case-fold-search t)
  13267. (stable org-mode-syntax-table)
  13268. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13269. (taggroups (if downcased
  13270. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13271. taggroups)
  13272. taggroups))
  13273. (taggroups-keys (mapcar #'car taggroups))
  13274. (return-match (if downcased (downcase match) match))
  13275. (count 0)
  13276. (work-already-expanded tags-already-expanded)
  13277. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13278. ;; @ and _ are allowed as word-components in tags.
  13279. (modify-syntax-entry ?@ "w" stable)
  13280. (modify-syntax-entry ?_ "w" stable)
  13281. ;; Temporarily replace regexp-expressions in the match-expression.
  13282. (while (string-match "{.+?}" return-match)
  13283. (incf count)
  13284. (push (match-string 0 return-match) regexps-in-match)
  13285. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13286. (while (and taggroups-keys
  13287. (with-syntax-table stable
  13288. (string-match
  13289. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13290. (regexp-opt taggroups-keys) "\\>\\)") return-match)))
  13291. (let* ((dir (match-string 1 return-match))
  13292. (tag (match-string 2 return-match))
  13293. (tag (if downcased (downcase tag) tag)))
  13294. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13295. (member tag work-already-expanded))
  13296. (setq tags-in-group (assoc tag taggroups))
  13297. (push tag work-already-expanded)
  13298. ;; Recursively expand each tag in the group, if the tag hasn't
  13299. ;; already been expanded. Restore the match-data after all recursive calls.
  13300. (save-match-data
  13301. (let (tags-expanded)
  13302. (dolist (x (cdr tags-in-group))
  13303. (if (and (member x taggroups-keys)
  13304. (not (member x work-already-expanded)))
  13305. (setq tags-expanded
  13306. (delete-dups
  13307. (append
  13308. (org-tags-expand x t downcased
  13309. work-already-expanded)
  13310. tags-expanded)))
  13311. (setq tags-expanded
  13312. (append (list x) tags-expanded)))
  13313. (setq work-already-expanded
  13314. (delete-dups
  13315. (append tags-expanded
  13316. work-already-expanded))))
  13317. (setq tags-in-group
  13318. (delete-dups (cons (car tags-in-group)
  13319. tags-expanded)))))
  13320. ;; Filter tag-regexps from tags.
  13321. (setq regexp-in-group-escaped
  13322. (delq nil (mapcar (lambda (x)
  13323. (if (stringp x)
  13324. (and (equal "{" (substring x 0 1))
  13325. (equal "}" (substring x -1))
  13326. x)
  13327. x))
  13328. tags-in-group))
  13329. regexp-in-group
  13330. (mapcar (lambda (x)
  13331. (substring x 1 -1))
  13332. regexp-in-group-escaped)
  13333. tags-in-group
  13334. (delq nil (mapcar (lambda (x)
  13335. (if (stringp x)
  13336. (and (not (equal "{" (substring x 0 1)))
  13337. (not (equal "}" (substring x -1)))
  13338. x)
  13339. x))
  13340. tags-in-group)))
  13341. ;; If single-as-list, do no more in the while-loop.
  13342. (if (not single-as-list)
  13343. (progn
  13344. (when regexp-in-group
  13345. (setq regexp-in-group
  13346. (concat "\\|"
  13347. (mapconcat 'identity regexp-in-group
  13348. "\\|"))))
  13349. (setq tags-in-group
  13350. (concat dir
  13351. "{\\<"
  13352. (regexp-opt tags-in-group)
  13353. "\\>"
  13354. regexp-in-group
  13355. "}"))
  13356. (when (stringp tags-in-group)
  13357. (org-add-props tags-in-group '(grouptag t)))
  13358. (setq return-match
  13359. (replace-match tags-in-group t t return-match)))
  13360. (setq tags-in-group
  13361. (append regexp-in-group-escaped tags-in-group))))
  13362. (setq taggroups-keys (delete tag taggroups-keys))))
  13363. ;; Add the regular expressions back into the match-expression again.
  13364. (while regexps-in-match
  13365. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13366. (pop regexps-in-match)
  13367. return-match t t))
  13368. (decf count))
  13369. (if single-as-list
  13370. (if tags-in-group tags-in-group (list return-match))
  13371. return-match))
  13372. (if single-as-list
  13373. (list (if downcased (downcase match) match))
  13374. match)))
  13375. (defun org-op-to-function (op &optional stringp)
  13376. "Turn an operator into the appropriate function."
  13377. (setq op
  13378. (cond
  13379. ((equal op "<" ) '(< string< org-time<))
  13380. ((equal op ">" ) '(> org-string> org-time>))
  13381. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13382. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13383. ((member op '("=" "==")) '(= string= org-time=))
  13384. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13385. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13386. (defun org<> (a b) (not (= a b)))
  13387. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13388. (defun org-string>= (a b) (not (string< a b)))
  13389. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13390. (defun org-string<> (a b) (not (string= a b)))
  13391. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13392. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13393. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13394. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13395. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13396. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13397. (defun org-2ft (s)
  13398. "Convert S to a floating point time.
  13399. If S is already a number, just return it. If it is a string, parse
  13400. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13401. (cond
  13402. ((numberp s) s)
  13403. ((stringp s)
  13404. (condition-case nil
  13405. (float-time (apply 'encode-time (org-parse-time-string s)))
  13406. (error 0.)))
  13407. (t 0.)))
  13408. (defun org-time-today ()
  13409. "Time in seconds today at 0:00.
  13410. Returns the float number of seconds since the beginning of the
  13411. epoch to the beginning of today (00:00)."
  13412. (float-time (apply 'encode-time
  13413. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13414. (defun org-matcher-time (s)
  13415. "Interpret a time comparison value."
  13416. (save-match-data
  13417. (cond
  13418. ((string= s "<now>") (float-time))
  13419. ((string= s "<today>") (org-time-today))
  13420. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13421. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13422. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13423. (+ (org-time-today)
  13424. (* (string-to-number (match-string 1 s))
  13425. (cdr (assoc (match-string 2 s)
  13426. '(("d" . 86400.0) ("w" . 604800.0)
  13427. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13428. (t (org-2ft s)))))
  13429. (defun org-match-any-p (re list)
  13430. "Does re match any element of list?"
  13431. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13432. (delq nil list))
  13433. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13434. (defvar org-tags-overlay (make-overlay 1 1))
  13435. (org-detach-overlay org-tags-overlay)
  13436. (defun org-get-local-tags-at (&optional pos)
  13437. "Get a list of tags defined in the current headline."
  13438. (org-get-tags-at pos 'local))
  13439. (defun org-get-local-tags ()
  13440. "Get a list of tags defined in the current headline."
  13441. (org-get-tags-at nil 'local))
  13442. (defun org-get-tags-at (&optional pos local)
  13443. "Get a list of all headline tags applicable at POS.
  13444. POS defaults to point. If tags are inherited, the list contains
  13445. the targets in the same sequence as the headlines appear, i.e.
  13446. the tags of the current headline come last.
  13447. When LOCAL is non-nil, only return tags from the current headline,
  13448. ignore inherited ones."
  13449. (interactive)
  13450. (if (and org-trust-scanner-tags
  13451. (or (not pos) (equal pos (point)))
  13452. (not local))
  13453. org-scanner-tags
  13454. (let (tags ltags lastpos parent)
  13455. (save-excursion
  13456. (save-restriction
  13457. (widen)
  13458. (goto-char (or pos (point)))
  13459. (save-match-data
  13460. (catch 'done
  13461. (condition-case nil
  13462. (progn
  13463. (org-back-to-heading t)
  13464. (while (not (equal lastpos (point)))
  13465. (setq lastpos (point))
  13466. (when (looking-at
  13467. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13468. (setq ltags (org-split-string
  13469. (org-match-string-no-properties 1) ":"))
  13470. (when parent
  13471. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13472. (setq tags (append
  13473. (if parent
  13474. (org-remove-uninherited-tags ltags)
  13475. ltags)
  13476. tags)))
  13477. (or org-use-tag-inheritance (throw 'done t))
  13478. (if local (throw 'done t))
  13479. (or (org-up-heading-safe) (error nil))
  13480. (setq parent t)))
  13481. (error nil)))))
  13482. (if local
  13483. tags
  13484. (reverse (delete-dups
  13485. (reverse (append
  13486. (org-remove-uninherited-tags
  13487. org-file-tags) tags)))))))))
  13488. (defun org-add-prop-inherited (s)
  13489. (add-text-properties 0 (length s) '(inherited t) s)
  13490. s)
  13491. (defun org-toggle-tag (tag &optional onoff)
  13492. "Toggle the tag TAG for the current line.
  13493. If ONOFF is `on' or `off', don't toggle but set to this state."
  13494. (let (res current)
  13495. (save-excursion
  13496. (org-back-to-heading t)
  13497. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13498. (point-at-eol) t)
  13499. (progn
  13500. (setq current (match-string 1))
  13501. (replace-match ""))
  13502. (setq current ""))
  13503. (setq current (nreverse (org-split-string current ":")))
  13504. (cond
  13505. ((eq onoff 'on)
  13506. (setq res t)
  13507. (or (member tag current) (push tag current)))
  13508. ((eq onoff 'off)
  13509. (or (not (member tag current)) (setq current (delete tag current))))
  13510. (t (if (member tag current)
  13511. (setq current (delete tag current))
  13512. (setq res t)
  13513. (push tag current))))
  13514. (end-of-line 1)
  13515. (if current
  13516. (progn
  13517. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13518. (org-set-tags nil t))
  13519. (delete-horizontal-space))
  13520. (run-hooks 'org-after-tags-change-hook))
  13521. res))
  13522. (defun org-align-tags-here (to-col)
  13523. ;; Assumes that this is a headline
  13524. "Align tags on the current headline to TO-COL."
  13525. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13526. (beginning-of-line 1)
  13527. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13528. (< pos (match-beginning 2)))
  13529. (progn
  13530. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13531. (goto-char (match-beginning 1))
  13532. (insert " ")
  13533. (delete-region (point) (1+ (match-beginning 2)))
  13534. (setq ncol (max (current-column)
  13535. (1+ col)
  13536. (if (> to-col 0)
  13537. to-col
  13538. (- (abs to-col) tags-l))))
  13539. (setq p (point))
  13540. (insert (make-string (- ncol (current-column)) ?\ ))
  13541. (setq ncol (current-column))
  13542. (when indent-tabs-mode (tabify p (point-at-eol)))
  13543. (org-move-to-column (min ncol col)))
  13544. (goto-char pos))))
  13545. (defun org-set-tags-command (&optional arg just-align)
  13546. "Call the set-tags command for the current entry."
  13547. (interactive "P")
  13548. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13549. (org-set-tags arg just-align)
  13550. (save-excursion
  13551. (unless (and (org-region-active-p)
  13552. org-loop-over-headlines-in-active-region)
  13553. (org-back-to-heading t))
  13554. (org-set-tags arg just-align))))
  13555. (defun org-set-tags-to (data)
  13556. "Set the tags of the current entry to DATA, replacing the current tags.
  13557. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13558. If DATA is nil or the empty string, any tags will be removed."
  13559. (interactive "sTags: ")
  13560. (setq data
  13561. (cond
  13562. ((eq data nil) "")
  13563. ((equal data "") "")
  13564. ((stringp data)
  13565. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13566. ":"))
  13567. ((listp data)
  13568. (concat ":" (mapconcat 'identity data ":") ":"))))
  13569. (when data
  13570. (save-excursion
  13571. (org-back-to-heading t)
  13572. (when (looking-at org-complex-heading-regexp)
  13573. (if (match-end 5)
  13574. (progn
  13575. (goto-char (match-beginning 5))
  13576. (insert data)
  13577. (delete-region (point) (point-at-eol))
  13578. (org-set-tags nil 'align))
  13579. (goto-char (point-at-eol))
  13580. (insert " " data)
  13581. (org-set-tags nil 'align)))
  13582. (beginning-of-line 1)
  13583. (if (looking-at ".*?\\([ \t]+\\)$")
  13584. (delete-region (match-beginning 1) (match-end 1))))))
  13585. (defun org-align-all-tags ()
  13586. "Align the tags i all headings."
  13587. (interactive)
  13588. (save-excursion
  13589. (or (ignore-errors (org-back-to-heading t))
  13590. (outline-next-heading))
  13591. (if (org-at-heading-p)
  13592. (org-set-tags t)
  13593. (message "No headings"))))
  13594. (defvar org-indent-indentation-per-level)
  13595. (defun org-set-tags (&optional arg just-align)
  13596. "Set the tags for the current headline.
  13597. With prefix ARG, realign all tags in headings in the current buffer.
  13598. When JUST-ALIGN is non-nil, only align tags."
  13599. (interactive "P")
  13600. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13601. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13602. 'region-start-level 'region))
  13603. org-loop-over-headlines-in-active-region)
  13604. (org-map-entries
  13605. ;; We don't use ARG and JUST-ALIGN here because these args
  13606. ;; are not useful when looping over headlines.
  13607. `(org-set-tags)
  13608. org-loop-over-headlines-in-active-region
  13609. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13610. (let* ((re org-outline-regexp-bol)
  13611. (current (unless arg (org-get-tags-string)))
  13612. (col (current-column))
  13613. (org-setting-tags t)
  13614. table current-tags inherited-tags ; computed below when needed
  13615. tags p0 c0 c1 rpl di tc level)
  13616. (if arg
  13617. (save-excursion
  13618. (goto-char (point-min))
  13619. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13620. (while (re-search-forward re nil t)
  13621. (org-set-tags nil t)
  13622. (end-of-line 1)))
  13623. (message "All tags realigned to column %d" org-tags-column))
  13624. (if just-align
  13625. (setq tags current)
  13626. ;; Get a new set of tags from the user
  13627. (save-excursion
  13628. (setq table (append org-tag-persistent-alist
  13629. (or org-tag-alist (org-get-buffer-tags))
  13630. (and
  13631. org-complete-tags-always-offer-all-agenda-tags
  13632. (org-global-tags-completion-table
  13633. (org-agenda-files))))
  13634. org-last-tags-completion-table table
  13635. current-tags (org-split-string current ":")
  13636. inherited-tags (nreverse
  13637. (nthcdr (length current-tags)
  13638. (nreverse (org-get-tags-at))))
  13639. tags
  13640. (if (or (eq t org-use-fast-tag-selection)
  13641. (and org-use-fast-tag-selection
  13642. (delq nil (mapcar 'cdr table))))
  13643. (org-fast-tag-selection
  13644. current-tags inherited-tags table
  13645. (if org-fast-tag-selection-include-todo
  13646. org-todo-key-alist))
  13647. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13648. (org-trim
  13649. (org-icompleting-read "Tags: "
  13650. 'org-tags-completion-function
  13651. nil nil current 'org-tags-history))))))
  13652. (while (string-match "[-+&]+" tags)
  13653. ;; No boolean logic, just a list
  13654. (setq tags (replace-match ":" t t tags))))
  13655. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13656. (if org-tags-sort-function
  13657. (setq tags (mapconcat 'identity
  13658. (sort (org-split-string
  13659. tags (org-re "[^[:alnum:]_@#%]+"))
  13660. org-tags-sort-function) ":")))
  13661. (if (string-match "\\`[\t ]*\\'" tags)
  13662. (setq tags "")
  13663. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13664. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13665. ;; Insert new tags at the correct column
  13666. (beginning-of-line 1)
  13667. (setq level (or (and (looking-at org-outline-regexp)
  13668. (- (match-end 0) (point) 1))
  13669. 1))
  13670. (cond
  13671. ((and (equal current "") (equal tags "")))
  13672. ((re-search-forward
  13673. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13674. (point-at-eol) t)
  13675. (if (equal tags "")
  13676. (setq rpl "")
  13677. (goto-char (match-beginning 0))
  13678. (setq c0 (current-column)
  13679. ;; compute offset for the case of org-indent-mode active
  13680. di (if (org-bound-and-true-p org-indent-mode)
  13681. (* (1- org-indent-indentation-per-level) (1- level))
  13682. 0)
  13683. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13684. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13685. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13686. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13687. (replace-match rpl t t)
  13688. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13689. tags)
  13690. (t (error "Tags alignment failed")))
  13691. (org-move-to-column col)
  13692. (unless just-align
  13693. (run-hooks 'org-after-tags-change-hook))))))
  13694. (defun org-change-tag-in-region (beg end tag off)
  13695. "Add or remove TAG for each entry in the region.
  13696. This works in the agenda, and also in an org-mode buffer."
  13697. (interactive
  13698. (list (region-beginning) (region-end)
  13699. (let ((org-last-tags-completion-table
  13700. (if (derived-mode-p 'org-mode)
  13701. (org-uniquify
  13702. (delq nil (append (org-get-buffer-tags)
  13703. (org-global-tags-completion-table))))
  13704. (org-global-tags-completion-table))))
  13705. (org-icompleting-read
  13706. "Tag: " 'org-tags-completion-function nil nil nil
  13707. 'org-tags-history))
  13708. (progn
  13709. (message "[s]et or [r]emove? ")
  13710. (equal (read-char-exclusive) ?r))))
  13711. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13712. (let ((agendap (equal major-mode 'org-agenda-mode))
  13713. l1 l2 m buf pos newhead (cnt 0))
  13714. (goto-char end)
  13715. (setq l2 (1- (org-current-line)))
  13716. (goto-char beg)
  13717. (setq l1 (org-current-line))
  13718. (loop for l from l1 to l2 do
  13719. (org-goto-line l)
  13720. (setq m (get-text-property (point) 'org-hd-marker))
  13721. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13722. (and agendap m))
  13723. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13724. pos (if agendap m (point)))
  13725. (with-current-buffer buf
  13726. (save-excursion
  13727. (save-restriction
  13728. (goto-char pos)
  13729. (setq cnt (1+ cnt))
  13730. (org-toggle-tag tag (if off 'off 'on))
  13731. (setq newhead (org-get-heading)))))
  13732. (and agendap (org-agenda-change-all-lines newhead m))))
  13733. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13734. (defun org-tags-completion-function (string predicate &optional flag)
  13735. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13736. (confirm (lambda (x) (stringp (car x)))))
  13737. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13738. (setq s1 (match-string 1 string)
  13739. s2 (match-string 2 string))
  13740. (setq s1 "" s2 string))
  13741. (cond
  13742. ((eq flag nil)
  13743. ;; try completion
  13744. (setq rtn (try-completion s2 ctable confirm))
  13745. (if (stringp rtn)
  13746. (setq rtn
  13747. (concat s1 s2 (substring rtn (length s2))
  13748. (if (and org-add-colon-after-tag-completion
  13749. (assoc rtn ctable))
  13750. ":" ""))))
  13751. rtn)
  13752. ((eq flag t)
  13753. ;; all-completions
  13754. (all-completions s2 ctable confirm))
  13755. ((eq flag 'lambda)
  13756. ;; exact match?
  13757. (assoc s2 ctable)))))
  13758. (defun org-fast-tag-insert (kwd tags face &optional end)
  13759. "Insert KDW, and the TAGS, the latter with face FACE.
  13760. Also insert END."
  13761. (insert (format "%-12s" (concat kwd ":"))
  13762. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13763. (or end "")))
  13764. (defun org-fast-tag-show-exit (flag)
  13765. (save-excursion
  13766. (org-goto-line 3)
  13767. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13768. (replace-match ""))
  13769. (when flag
  13770. (end-of-line 1)
  13771. (org-move-to-column (- (window-width) 19) t)
  13772. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13773. (defun org-set-current-tags-overlay (current prefix)
  13774. "Add an overlay to CURRENT tag with PREFIX."
  13775. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13776. (if (featurep 'xemacs)
  13777. (org-overlay-display org-tags-overlay (concat prefix s)
  13778. 'secondary-selection)
  13779. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13780. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13781. (defvar org-last-tag-selection-key nil)
  13782. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13783. "Fast tag selection with single keys.
  13784. CURRENT is the current list of tags in the headline, INHERITED is the
  13785. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13786. possibly with grouping information. TODO-TABLE is a similar table with
  13787. TODO keywords, should these have keys assigned to them.
  13788. If the keys are nil, a-z are automatically assigned.
  13789. Returns the new tags string, or nil to not change the current settings."
  13790. (let* ((fulltable (append table todo-table))
  13791. (maxlen (apply 'max (mapcar
  13792. (lambda (x)
  13793. (if (stringp (car x)) (string-width (car x)) 0))
  13794. fulltable)))
  13795. (buf (current-buffer))
  13796. (expert (eq org-fast-tag-selection-single-key 'expert))
  13797. (buffer-tags nil)
  13798. (fwidth (+ maxlen 3 1 3))
  13799. (ncol (/ (- (window-width) 4) fwidth))
  13800. (i-face 'org-done)
  13801. (c-face 'org-todo)
  13802. tg cnt e c char c1 c2 ntable tbl rtn
  13803. ov-start ov-end ov-prefix
  13804. (exit-after-next org-fast-tag-selection-single-key)
  13805. (done-keywords org-done-keywords)
  13806. groups ingroup intaggroup)
  13807. (save-excursion
  13808. (beginning-of-line 1)
  13809. (if (looking-at
  13810. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13811. (setq ov-start (match-beginning 1)
  13812. ov-end (match-end 1)
  13813. ov-prefix "")
  13814. (setq ov-start (1- (point-at-eol))
  13815. ov-end (1+ ov-start))
  13816. (skip-chars-forward "^\n\r")
  13817. (setq ov-prefix
  13818. (concat
  13819. (buffer-substring (1- (point)) (point))
  13820. (if (> (current-column) org-tags-column)
  13821. " "
  13822. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13823. (move-overlay org-tags-overlay ov-start ov-end)
  13824. (save-window-excursion
  13825. (if expert
  13826. (set-buffer (get-buffer-create " *Org tags*"))
  13827. (delete-other-windows)
  13828. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13829. (org-switch-to-buffer-other-window " *Org tags*"))
  13830. (erase-buffer)
  13831. (org-set-local 'org-done-keywords done-keywords)
  13832. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13833. (org-fast-tag-insert "Current" current c-face "\n\n")
  13834. (org-fast-tag-show-exit exit-after-next)
  13835. (org-set-current-tags-overlay current ov-prefix)
  13836. (setq tbl fulltable char ?a cnt 0)
  13837. (while (setq e (pop tbl))
  13838. (cond
  13839. ((eq (car e) :startgroup)
  13840. (push '() groups) (setq ingroup t)
  13841. (unless (zerop cnt)
  13842. (setq cnt 0)
  13843. (insert "\n"))
  13844. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13845. ((eq (car e) :endgroup)
  13846. (setq ingroup nil cnt 0)
  13847. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13848. ((eq (car e) :startgrouptag)
  13849. (setq intaggroup t)
  13850. (unless (zerop cnt)
  13851. (setq cnt 0)
  13852. (insert "\n"))
  13853. (insert "[ "))
  13854. ((eq (car e) :endgrouptag)
  13855. (setq intaggroup nil cnt 0)
  13856. (insert "]\n"))
  13857. ((equal e '(:newline))
  13858. (unless (zerop cnt)
  13859. (setq cnt 0)
  13860. (insert "\n")
  13861. (setq e (car tbl))
  13862. (while (equal (car tbl) '(:newline))
  13863. (insert "\n")
  13864. (setq tbl (cdr tbl)))))
  13865. ((equal e '(:grouptags)) (insert " : "))
  13866. (t
  13867. (setq tg (copy-sequence (car e)) c2 nil)
  13868. (if (cdr e)
  13869. (setq c (cdr e))
  13870. ;; automatically assign a character.
  13871. (setq c1 (string-to-char
  13872. (downcase (substring
  13873. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13874. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13875. (while (or (rassoc char ntable) (rassoc char table))
  13876. (setq char (1+ char)))
  13877. (setq c2 c1))
  13878. (setq c (or c2 char)))
  13879. (when ingroup (push tg (car groups)))
  13880. (setq tg (org-add-props tg nil 'face
  13881. (cond
  13882. ((not (assoc tg table))
  13883. (org-get-todo-face tg))
  13884. ((member tg current) c-face)
  13885. ((member tg inherited) i-face))))
  13886. (when (equal (caar tbl) :grouptags)
  13887. (org-add-props tg nil 'face 'org-tag-group))
  13888. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13889. (insert "[" c "] " tg (make-string
  13890. (- fwidth 4 (length tg)) ?\ ))
  13891. (push (cons tg c) ntable)
  13892. (when (= (incf cnt) ncol)
  13893. (insert "\n")
  13894. (when (or ingroup intaggroup) (insert " "))
  13895. (setq cnt 0)))))
  13896. (setq ntable (nreverse ntable))
  13897. (insert "\n")
  13898. (goto-char (point-min))
  13899. (unless expert (org-fit-window-to-buffer))
  13900. (setq rtn
  13901. (catch 'exit
  13902. (while t
  13903. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13904. (if (not groups) "no " "")
  13905. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13906. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13907. (setq org-last-tag-selection-key c)
  13908. (cond
  13909. ((= c ?\r) (throw 'exit t))
  13910. ((= c ?!)
  13911. (setq groups (not groups))
  13912. (goto-char (point-min))
  13913. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13914. ((= c ?\C-c)
  13915. (if (not expert)
  13916. (org-fast-tag-show-exit
  13917. (setq exit-after-next (not exit-after-next)))
  13918. (setq expert nil)
  13919. (delete-other-windows)
  13920. (set-window-buffer (split-window-vertically) " *Org tags*")
  13921. (org-switch-to-buffer-other-window " *Org tags*")
  13922. (org-fit-window-to-buffer)))
  13923. ((or (= c ?\C-g)
  13924. (and (= c ?q) (not (rassoc c ntable))))
  13925. (org-detach-overlay org-tags-overlay)
  13926. (setq quit-flag t))
  13927. ((= c ?\ )
  13928. (setq current nil)
  13929. (when exit-after-next (setq exit-after-next 'now)))
  13930. ((= c ?\t)
  13931. (condition-case nil
  13932. (setq tg (org-icompleting-read
  13933. "Tag: "
  13934. (or buffer-tags
  13935. (with-current-buffer buf
  13936. (org-get-buffer-tags)))))
  13937. (quit (setq tg "")))
  13938. (when (string-match "\\S-" tg)
  13939. (add-to-list 'buffer-tags (list tg))
  13940. (if (member tg current)
  13941. (setq current (delete tg current))
  13942. (push tg current)))
  13943. (when exit-after-next (setq exit-after-next 'now)))
  13944. ((setq e (rassoc c todo-table) tg (car e))
  13945. (with-current-buffer buf
  13946. (save-excursion (org-todo tg)))
  13947. (when exit-after-next (setq exit-after-next 'now)))
  13948. ((setq e (rassoc c ntable) tg (car e))
  13949. (if (member tg current)
  13950. (setq current (delete tg current))
  13951. (loop for g in groups do
  13952. (when (member tg g)
  13953. (dolist (x g) (setq current (delete x current)))))
  13954. (push tg current))
  13955. (when exit-after-next (setq exit-after-next 'now))))
  13956. ;; Create a sorted list
  13957. (setq current
  13958. (sort current
  13959. (lambda (a b)
  13960. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13961. (when (eq exit-after-next 'now) (throw 'exit t))
  13962. (goto-char (point-min))
  13963. (beginning-of-line 2)
  13964. (delete-region (point) (point-at-eol))
  13965. (org-fast-tag-insert "Current" current c-face)
  13966. (org-set-current-tags-overlay current ov-prefix)
  13967. (while (re-search-forward
  13968. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13969. (setq tg (match-string 1))
  13970. (add-text-properties
  13971. (match-beginning 1) (match-end 1)
  13972. (list 'face
  13973. (cond
  13974. ((member tg current) c-face)
  13975. ((member tg inherited) i-face)
  13976. (t (get-text-property (match-beginning 1) 'face))))))
  13977. (goto-char (point-min)))))
  13978. (org-detach-overlay org-tags-overlay)
  13979. (if rtn
  13980. (mapconcat 'identity current ":")
  13981. nil))))
  13982. (defun org-get-tags-string ()
  13983. "Get the TAGS string in the current headline."
  13984. (unless (org-at-heading-p t)
  13985. (user-error "Not on a heading"))
  13986. (save-excursion
  13987. (beginning-of-line 1)
  13988. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13989. (org-match-string-no-properties 1)
  13990. "")))
  13991. (defun org-get-tags ()
  13992. "Get the list of tags specified in the current headline."
  13993. (org-split-string (org-get-tags-string) ":"))
  13994. (defun org-get-buffer-tags ()
  13995. "Get a table of all tags used in the buffer, for completion."
  13996. (org-with-wide-buffer
  13997. (goto-char (point-min))
  13998. (let ((tag-re (concat org-outline-regexp-bol
  13999. "\\(?:.*?[ \t]\\)?"
  14000. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14001. tags)
  14002. (while (re-search-forward tag-re nil t)
  14003. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14004. (push tag tags)))
  14005. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14006. ;;;; The mapping API
  14007. (defun org-map-entries (func &optional match scope &rest skip)
  14008. "Call FUNC at each headline selected by MATCH in SCOPE.
  14009. FUNC is a function or a lisp form. The function will be called without
  14010. arguments, with the cursor positioned at the beginning of the headline.
  14011. The return values of all calls to the function will be collected and
  14012. returned as a list.
  14013. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14014. does not need to preserve point. After evaluation, the cursor will be
  14015. moved to the end of the line (presumably of the headline of the
  14016. processed entry) and search continues from there. Under some
  14017. circumstances, this may not produce the wanted results. For example,
  14018. if you have removed (e.g. archived) the current (sub)tree it could
  14019. mean that the next entry will be skipped entirely. In such cases, you
  14020. can specify the position from where search should continue by making
  14021. FUNC set the variable `org-map-continue-from' to the desired buffer
  14022. position.
  14023. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14024. Only headlines that are matched by this query will be considered during
  14025. the iteration. When MATCH is nil or t, all headlines will be
  14026. visited by the iteration.
  14027. SCOPE determines the scope of this command. It can be any of:
  14028. nil The current buffer, respecting the restriction if any
  14029. tree The subtree started with the entry at point
  14030. region The entries within the active region, if any
  14031. region-start-level
  14032. The entries within the active region, but only those at
  14033. the same level than the first one.
  14034. file The current buffer, without restriction
  14035. file-with-archives
  14036. The current buffer, and any archives associated with it
  14037. agenda All agenda files
  14038. agenda-with-archives
  14039. All agenda files with any archive files associated with them
  14040. \(file1 file2 ...)
  14041. If this is a list, all files in the list will be scanned
  14042. The remaining args are treated as settings for the skipping facilities of
  14043. the scanner. The following items can be given here:
  14044. archive skip trees with the archive tag
  14045. comment skip trees with the COMMENT keyword
  14046. function or Emacs Lisp form:
  14047. will be used as value for `org-agenda-skip-function', so
  14048. whenever the function returns a position, FUNC will not be
  14049. called for that entry and search will continue from the
  14050. position returned
  14051. If your function needs to retrieve the tags including inherited tags
  14052. at the *current* entry, you can use the value of the variable
  14053. `org-scanner-tags' which will be much faster than getting the value
  14054. with `org-get-tags-at'. If your function gets properties with
  14055. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14056. to t around the call to `org-entry-properties' to get the same speedup.
  14057. Note that if your function moves around to retrieve tags and properties at
  14058. a *different* entry, you cannot use these techniques."
  14059. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14060. (not (org-region-active-p)))
  14061. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14062. (org-agenda-skip-archived-trees (memq 'archive skip))
  14063. (org-agenda-skip-comment-trees (memq 'comment skip))
  14064. (org-agenda-skip-function
  14065. (car (org-delete-all '(comment archive) skip)))
  14066. (org-tags-match-list-sublevels t)
  14067. (start-level (eq scope 'region-start-level))
  14068. matcher file res
  14069. org-todo-keywords-for-agenda
  14070. org-done-keywords-for-agenda
  14071. org-todo-keyword-alist-for-agenda
  14072. org-tag-alist-for-agenda
  14073. todo-only)
  14074. (cond
  14075. ((eq match t) (setq matcher t))
  14076. ((eq match nil) (setq matcher t))
  14077. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14078. (save-excursion
  14079. (save-restriction
  14080. (cond ((eq scope 'tree)
  14081. (org-back-to-heading t)
  14082. (org-narrow-to-subtree)
  14083. (setq scope nil))
  14084. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14085. (org-region-active-p))
  14086. ;; If needed, set start-level to a string like "2"
  14087. (when start-level
  14088. (save-excursion
  14089. (goto-char (region-beginning))
  14090. (unless (org-at-heading-p) (outline-next-heading))
  14091. (setq start-level (org-current-level))))
  14092. (narrow-to-region (region-beginning)
  14093. (save-excursion
  14094. (goto-char (region-end))
  14095. (unless (and (bolp) (org-at-heading-p))
  14096. (outline-next-heading))
  14097. (point)))
  14098. (setq scope nil)))
  14099. (if (not scope)
  14100. (progn
  14101. (org-agenda-prepare-buffers
  14102. (list (buffer-file-name (current-buffer))))
  14103. (setq res (org-scan-tags func matcher todo-only start-level)))
  14104. ;; Get the right scope
  14105. (cond
  14106. ((and scope (listp scope) (symbolp (car scope)))
  14107. (setq scope (eval scope)))
  14108. ((eq scope 'agenda)
  14109. (setq scope (org-agenda-files t)))
  14110. ((eq scope 'agenda-with-archives)
  14111. (setq scope (org-agenda-files t))
  14112. (setq scope (org-add-archive-files scope)))
  14113. ((eq scope 'file)
  14114. (setq scope (list (buffer-file-name))))
  14115. ((eq scope 'file-with-archives)
  14116. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14117. (org-agenda-prepare-buffers scope)
  14118. (while (setq file (pop scope))
  14119. (with-current-buffer (org-find-base-buffer-visiting file)
  14120. (save-excursion
  14121. (save-restriction
  14122. (widen)
  14123. (goto-char (point-min))
  14124. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14125. res)))
  14126. ;;; Properties API
  14127. (defconst org-special-properties
  14128. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14129. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14130. "The special properties valid in Org mode.
  14131. These are properties that are not defined in the property drawer,
  14132. but in some other way.")
  14133. (defconst org-default-properties
  14134. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14135. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14136. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14137. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14138. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14139. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14140. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14141. "Some properties that are used by Org mode for various purposes.
  14142. Being in this list makes sure that they are offered for completion.")
  14143. (defun org--update-property-plist (key val props)
  14144. "Associate KEY to VAL in alist PROPS.
  14145. Modifications are made by side-effect. Return new alist."
  14146. (let* ((appending (string= (substring key -1) "+"))
  14147. (key (if appending (substring key 0 -1) key))
  14148. (old (assoc-string key props t)))
  14149. (if (not old) (cons (cons key val) props)
  14150. (setcdr old (if appending (concat (cdr old) " " val) val))
  14151. props)))
  14152. (defun org-get-property-block (&optional beg force)
  14153. "Return the (beg . end) range of the body of the property drawer.
  14154. BEG is the beginning of the current subtree, or of the part
  14155. before the first headline. If it is not given, it will be found.
  14156. If the drawer does not exist, create it if FORCE is non-nil, or
  14157. return nil."
  14158. (org-with-wide-buffer
  14159. (when beg (goto-char beg))
  14160. (unless (org-before-first-heading-p)
  14161. (let ((beg (cond (beg)
  14162. ((or (not (featurep 'org-inlinetask))
  14163. (org-inlinetask-in-task-p))
  14164. (org-back-to-heading t))
  14165. (t (org-with-limited-levels (org-back-to-heading t))))))
  14166. (forward-line)
  14167. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14168. (cond ((looking-at org-property-drawer-re)
  14169. (forward-line)
  14170. (cons (point) (progn (goto-char (match-end 0))
  14171. (line-beginning-position))))
  14172. (force
  14173. (goto-char beg)
  14174. (org-insert-property-drawer)
  14175. (let ((pos (save-excursion (search-forward ":END:")
  14176. (line-beginning-position))))
  14177. (cons pos pos))))))))
  14178. (defun org-at-property-p ()
  14179. "Non-nil when point is inside a property drawer.
  14180. See `org-property-re' for match data, if applicable."
  14181. (save-excursion
  14182. (beginning-of-line)
  14183. (and (looking-at org-property-re)
  14184. (let ((property-drawer (save-match-data (org-get-property-block))))
  14185. (and property-drawer
  14186. (>= (point) (car property-drawer))
  14187. (< (point) (cdr property-drawer)))))))
  14188. (defun org-property-action ()
  14189. "Do an action on properties."
  14190. (interactive)
  14191. (unless (org-at-property-p) (user-error "Not at a property"))
  14192. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14193. (let ((c (read-char-exclusive)))
  14194. (case c
  14195. (?s (call-interactively #'org-set-property))
  14196. (?d (call-interactively #'org-delete-property))
  14197. (?D (call-interactively #'org-delete-property-globally))
  14198. (?c (call-interactively #'org-compute-property-at-point))
  14199. (otherwise (user-error "No such property action %c" c)))))
  14200. (defun org-inc-effort ()
  14201. "Increment the value of the effort property in the current entry."
  14202. (interactive)
  14203. (org-set-effort nil t))
  14204. (defvar org-clock-effort) ; Defined in org-clock.el.
  14205. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14206. (defun org-set-effort (&optional value increment)
  14207. "Set the effort property of the current entry.
  14208. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14209. 10th allowed value.
  14210. When INCREMENT is non-nil, set the property to the next allowed value."
  14211. (interactive "P")
  14212. (if (equal value 0) (setq value 10))
  14213. (let* ((completion-ignore-case t)
  14214. (prop org-effort-property)
  14215. (cur (org-entry-get nil prop))
  14216. (allowed (org-property-get-allowed-values nil prop 'table))
  14217. (existing (mapcar 'list (org-property-values prop)))
  14218. (heading (nth 4 (org-heading-components)))
  14219. rpl
  14220. (val (cond
  14221. ((stringp value) value)
  14222. ((and allowed (integerp value))
  14223. (or (car (nth (1- value) allowed))
  14224. (car (org-last allowed))))
  14225. ((and allowed increment)
  14226. (or (caadr (member (list cur) allowed))
  14227. (user-error "Allowed effort values are not set")))
  14228. (allowed
  14229. (message "Select 1-9,0, [RET%s]: %s"
  14230. (if cur (concat "=" cur) "")
  14231. (mapconcat 'car allowed " "))
  14232. (setq rpl (read-char-exclusive))
  14233. (if (equal rpl ?\r)
  14234. cur
  14235. (setq rpl (- rpl ?0))
  14236. (if (equal rpl 0) (setq rpl 10))
  14237. (if (and (> rpl 0) (<= rpl (length allowed)))
  14238. (car (nth (1- rpl) allowed))
  14239. (org-completing-read "Effort: " allowed nil))))
  14240. (t
  14241. (let (org-completion-use-ido org-completion-use-iswitchb)
  14242. (org-completing-read
  14243. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14244. (concat "[" cur "]") "")
  14245. ": ")
  14246. existing nil nil "" nil cur))))))
  14247. (unless (equal (org-entry-get nil prop) val)
  14248. (org-entry-put nil prop val))
  14249. (org-refresh-property
  14250. '((effort . identity)
  14251. (effort-minutes . org-duration-string-to-minutes))
  14252. val)
  14253. (when (string= heading org-clock-current-task)
  14254. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14255. (org-clock-update-mode-line))
  14256. (message "%s is now %s" prop val)))
  14257. (defun org-entry-properties (&optional pom which)
  14258. "Get all properties of the current entry.
  14259. When POM is a buffer position, get all properties from the entry
  14260. there instead.
  14261. This includes the TODO keyword, the tags, time strings for
  14262. deadline, scheduled, and clocking, and any additional properties
  14263. defined in the entry.
  14264. If WHICH is nil or `all', get all properties. If WHICH is
  14265. `special' or `standard', only get that subclass. If WHICH is
  14266. a string, only get that property.
  14267. Return value is an alist. Keys are properties, as upcased
  14268. strings."
  14269. (org-with-point-at pom
  14270. (when (and (derived-mode-p 'org-mode)
  14271. (ignore-errors (org-back-to-heading t)))
  14272. (catch 'exit
  14273. (let* ((beg (point))
  14274. (specific (and (stringp which) (upcase which)))
  14275. (which (cond ((not specific) which)
  14276. ((member specific org-special-properties) 'special)
  14277. (t 'standard)))
  14278. props)
  14279. ;; Get the special properties, like TODO and TAGS.
  14280. (when (memq which '(nil all special))
  14281. (when (or (not specific) (string= specific "CLOCKSUM"))
  14282. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14283. (when clocksum
  14284. (push (cons "CLOCKSUM"
  14285. (org-columns-number-to-string
  14286. (/ (float clocksum) 60.) 'add_times))
  14287. props)))
  14288. (when specific (throw 'exit props)))
  14289. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14290. (let ((clocksumt (get-text-property (point)
  14291. :org-clock-minutes-today)))
  14292. (when clocksumt
  14293. (push (cons "CLOCKSUM_T"
  14294. (org-columns-number-to-string
  14295. (/ (float clocksumt) 60.) 'add_times))
  14296. props)))
  14297. (when specific (throw 'exit props)))
  14298. (when (or (not specific) (string= specific "ITEM"))
  14299. (when (looking-at org-complex-heading-regexp)
  14300. (push (cons "ITEM"
  14301. (concat
  14302. (org-match-string-no-properties 1)
  14303. (let ((title (org-match-string-no-properties 4)))
  14304. (when (org-string-nw-p title)
  14305. (concat " " (org-remove-tabs title))))))
  14306. props))
  14307. (when specific (throw 'exit props)))
  14308. (when (or (not specific) (string= specific "TODO"))
  14309. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14310. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14311. (when specific (throw 'exit props)))
  14312. (when (or (not specific) (string= specific "PRIORITY"))
  14313. (when (looking-at org-priority-regexp)
  14314. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14315. (when specific (throw 'exit props)))
  14316. (when (or (not specific) (string= specific "FILE"))
  14317. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14318. props)
  14319. (when specific (throw 'exit props)))
  14320. (when (or (not specific) (string= specific "TAGS"))
  14321. (let ((value (org-string-nw-p (org-get-tags-string))))
  14322. (when value (push (cons "TAGS" value) props)))
  14323. (when specific (throw 'exit props)))
  14324. (when (or (not specific) (string= specific "ALLTAGS"))
  14325. (let ((value (org-get-tags-at)))
  14326. (when value
  14327. (push (cons "ALLTAGS"
  14328. (format ":%s:" (mapconcat #'identity value ":")))
  14329. props)))
  14330. (when specific (throw 'exit props)))
  14331. (when (or (not specific) (string= specific "BLOCKED"))
  14332. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14333. (when specific (throw 'exit props)))
  14334. (when (or (not specific)
  14335. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14336. (forward-line)
  14337. (when (org-looking-at-p org-planning-line-re)
  14338. (end-of-line)
  14339. (let ((bol (line-beginning-position))
  14340. ;; Backward compatibility: time keywords used to
  14341. ;; be configurable (before 8.3). Make sure we
  14342. ;; get the correct keyword.
  14343. (key-assoc `(("CLOSED" . ,org-closed-string)
  14344. ("DEADLINE" . ,org-deadline-string)
  14345. ("SCHEDULED" . ,org-scheduled-string))))
  14346. (dolist (pair (if specific (list (assoc specific key-assoc))
  14347. key-assoc))
  14348. (save-excursion
  14349. (when (search-backward (cdr pair) bol t)
  14350. (goto-char (match-end 0))
  14351. (skip-chars-forward " \t")
  14352. (and (looking-at org-ts-regexp-both)
  14353. (push (cons (car pair)
  14354. (org-match-string-no-properties 0))
  14355. props)))))))
  14356. (when specific (throw 'exit props)))
  14357. (when (or (not specific)
  14358. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14359. (let ((find-ts
  14360. (lambda (end ts)
  14361. (let ((regexp (if (or (string= specific "TIMESTAMP")
  14362. (assoc "TIMESTAMP_IA" ts))
  14363. org-ts-regexp
  14364. org-ts-regexp-both)))
  14365. (catch 'next
  14366. (while (re-search-forward regexp end t)
  14367. (backward-char)
  14368. (let ((object (org-element-context)))
  14369. ;; Accept to match timestamps in node
  14370. ;; properties, too.
  14371. (when (memq (org-element-type object)
  14372. '(node-property timestamp))
  14373. (let ((type
  14374. (org-element-property :type object)))
  14375. (cond
  14376. ((and (memq type '(active active-range))
  14377. (not (equal specific "TIMESTAMP_IA")))
  14378. (unless (assoc "TIMESTAMP" ts)
  14379. (push (cons "TIMESTAMP"
  14380. (org-element-property
  14381. :raw-value object))
  14382. ts)
  14383. (when specific (throw 'exit ts))))
  14384. ((and (memq type '(inactive inactive-range))
  14385. (not (string= specific "TIMESTAMP")))
  14386. (unless (assoc "TIMESTAMP_IA" ts)
  14387. (push (cons "TIMESTAMP_IA"
  14388. (org-element-property
  14389. :raw-value object))
  14390. ts)
  14391. (when specific (throw 'exit ts))))))
  14392. ;; Both timestamp types are found,
  14393. ;; move to next part.
  14394. (when (= (length ts) 2) (throw 'next ts)))))
  14395. ts)))))
  14396. (goto-char beg)
  14397. ;; First look for timestamps within headline.
  14398. (let ((ts (funcall find-ts (line-end-position) nil)))
  14399. (if (= (length ts) 2) (setq props (nconc ts props))
  14400. (forward-line)
  14401. ;; Then find timestamps in the section, skipping
  14402. ;; planning line.
  14403. (when (org-looking-at-p org-planning-line-re)
  14404. (forward-line))
  14405. (let ((end (save-excursion (outline-next-heading))))
  14406. (setq props (nconc (funcall find-ts end ts) props))))))))
  14407. ;; Get the standard properties, like :PROP:.
  14408. (when (memq which '(nil all standard))
  14409. ;; If we are looking after a specific property, delegate
  14410. ;; to `org-entry-get', which is faster. However, make an
  14411. ;; exception for "CATEGORY", since it can be also set
  14412. ;; through keywords (i.e. #+CATEGORY).
  14413. (if (and specific (not (equal specific "CATEGORY")))
  14414. (let ((value (org-entry-get beg specific nil t)))
  14415. (throw 'exit (and value (list (cons specific value)))))
  14416. (let ((range (org-get-property-block beg)))
  14417. (when range
  14418. (let ((end (cdr range)) seen-base)
  14419. (goto-char (car range))
  14420. ;; Unlike to `org--update-property-plist', we
  14421. ;; handle the case where base values is found
  14422. ;; after its extension. We also forbid standard
  14423. ;; properties to be named as special properties.
  14424. (while (re-search-forward org-property-re end t)
  14425. (let* ((key (upcase (org-match-string-no-properties 2)))
  14426. (extendp (org-string-match-p "\\+\\'" key))
  14427. (key-base (if extendp (substring key 0 -1) key))
  14428. (value (org-match-string-no-properties 3)))
  14429. (cond
  14430. ((member-ignore-case key-base org-special-properties))
  14431. (extendp
  14432. (setq props
  14433. (org--update-property-plist key value props)))
  14434. ((member key seen-base))
  14435. (t (push key seen-base)
  14436. (let ((p (assoc-string key props t)))
  14437. (if p (setcdr p (concat value " " (cdr p)))
  14438. (push (cons key value) props))))))))))))
  14439. (unless (assoc "CATEGORY" props)
  14440. (push (cons "CATEGORY" (org-get-category beg)) props)
  14441. (when (string= specific "CATEGORY") (throw 'exit props)))
  14442. ;; Return value.
  14443. (append (get-text-property beg 'org-summaries) props))))))
  14444. (defun org-entry-get (pom property &optional inherit literal-nil)
  14445. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14446. If INHERIT is non-nil and the entry does not have the property,
  14447. then also check higher levels of the hierarchy. If INHERIT is
  14448. the symbol `selective', use inheritance only if the setting in
  14449. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14450. If the property is present but empty, the return value is the
  14451. empty string. If the property is not present at all, nil is
  14452. returned. In any other case, return the value as a string.
  14453. Search is case-insensitive.
  14454. If LITERAL-NIL is set, return the string value \"nil\" as
  14455. a string, do not interpret it as the list atom nil. This is used
  14456. for inheritance when a \"nil\" value can supersede a non-nil
  14457. value higher up the hierarchy."
  14458. (org-with-point-at pom
  14459. (cond
  14460. ((and inherit
  14461. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14462. (org-entry-get-with-inheritance property literal-nil))
  14463. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14464. ;; We need a special property. Use `org-entry-properties' to
  14465. ;; retrieve it, but specify the wanted property.
  14466. (cdr (assoc-string property (org-entry-properties nil property))))
  14467. (t
  14468. (let ((range (org-get-property-block)))
  14469. (when range
  14470. (let* ((case-fold-search t)
  14471. (end (cdr range))
  14472. (props
  14473. (let ((global
  14474. (or (assoc-string property org-file-properties t)
  14475. (assoc-string property org-global-properties t)
  14476. (assoc-string
  14477. property org-global-properties-fixed t))))
  14478. ;; Make sure to not re-use GLOBAL as
  14479. ;; `org--update-property-plist' would alter it by
  14480. ;; side-effect.
  14481. (and global (list (cons property (cdr global))))))
  14482. (find-value
  14483. (lambda (key)
  14484. (when (re-search-forward (org-re-property key nil t) end t)
  14485. (setq props
  14486. (org--update-property-plist
  14487. key (org-match-string-no-properties 3) props))))))
  14488. (goto-char (car range))
  14489. ;; Find base value.
  14490. (save-excursion (funcall find-value property))
  14491. ;; Find additional values.
  14492. (let ((property+ (concat property "+")))
  14493. (while (funcall find-value property+)))
  14494. ;; Return final value.
  14495. (let ((val (cdr (assoc-string property props t))))
  14496. (if literal-nil val (org-not-nil val))))))))))
  14497. (defun org-property-or-variable-value (var &optional inherit)
  14498. "Check if there is a property fixing the value of VAR.
  14499. If yes, return this value. If not, return the current value of the variable."
  14500. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14501. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14502. (read prop)
  14503. (symbol-value var))))
  14504. (defun org-entry-delete (pom property)
  14505. "Delete PROPERTY from entry at point-or-marker POM.
  14506. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14507. non-nil when a property was removed."
  14508. (unless (member property org-special-properties)
  14509. (org-with-point-at pom
  14510. (let ((range (org-get-property-block)))
  14511. (when range
  14512. (let* ((begin (car range))
  14513. (origin (cdr range))
  14514. (end (copy-marker origin))
  14515. (re (org-re-property
  14516. (concat (regexp-quote property) "\\+?") t t)))
  14517. (goto-char begin)
  14518. (while (re-search-forward re end t)
  14519. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14520. ;; If drawer is empty, remove it altogether.
  14521. (when (= begin end)
  14522. (delete-region (line-beginning-position 0)
  14523. (line-beginning-position 2)))
  14524. ;; Return non-nil if some property was removed.
  14525. (prog1 (/= end origin) (set-marker end nil))))))))
  14526. ;; Multi-values properties are properties that contain multiple values
  14527. ;; These values are assumed to be single words, separated by whitespace.
  14528. (defun org-entry-add-to-multivalued-property (pom property value)
  14529. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14530. (let* ((old (org-entry-get pom property))
  14531. (values (and old (org-split-string old "[ \t]"))))
  14532. (setq value (org-entry-protect-space value))
  14533. (unless (member value values)
  14534. (setq values (append values (list value)))
  14535. (org-entry-put pom property
  14536. (mapconcat 'identity values " ")))))
  14537. (defun org-entry-remove-from-multivalued-property (pom property value)
  14538. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14539. (let* ((old (org-entry-get pom property))
  14540. (values (and old (org-split-string old "[ \t]"))))
  14541. (setq value (org-entry-protect-space value))
  14542. (when (member value values)
  14543. (setq values (delete value values))
  14544. (org-entry-put pom property
  14545. (mapconcat 'identity values " ")))))
  14546. (defun org-entry-member-in-multivalued-property (pom property value)
  14547. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14548. (let* ((old (org-entry-get pom property))
  14549. (values (and old (org-split-string old "[ \t]"))))
  14550. (setq value (org-entry-protect-space value))
  14551. (member value values)))
  14552. (defun org-entry-get-multivalued-property (pom property)
  14553. "Return a list of values in a multivalued property."
  14554. (let* ((value (org-entry-get pom property))
  14555. (values (and value (org-split-string value "[ \t]"))))
  14556. (mapcar 'org-entry-restore-space values)))
  14557. (defun org-entry-put-multivalued-property (pom property &rest values)
  14558. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14559. VALUES should be a list of strings. Spaces will be protected."
  14560. (org-entry-put pom property
  14561. (mapconcat 'org-entry-protect-space values " "))
  14562. (let* ((value (org-entry-get pom property))
  14563. (values (and value (org-split-string value "[ \t]"))))
  14564. (mapcar 'org-entry-restore-space values)))
  14565. (defun org-entry-protect-space (s)
  14566. "Protect spaces and newline in string S."
  14567. (while (string-match " " s)
  14568. (setq s (replace-match "%20" t t s)))
  14569. (while (string-match "\n" s)
  14570. (setq s (replace-match "%0A" t t s)))
  14571. s)
  14572. (defun org-entry-restore-space (s)
  14573. "Restore spaces and newline in string S."
  14574. (while (string-match "%20" s)
  14575. (setq s (replace-match " " t t s)))
  14576. (while (string-match "%0A" s)
  14577. (setq s (replace-match "\n" t t s)))
  14578. s)
  14579. (defvar org-entry-property-inherited-from (make-marker)
  14580. "Marker pointing to the entry from where a property was inherited.
  14581. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14582. location of the entry where the inheritance search matched. If there was
  14583. no match, the marker will point nowhere.
  14584. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14585. is set.")
  14586. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14587. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14588. The search will stop at the first ancestor which has the property defined.
  14589. If the value found is \"nil\", return nil to show that the property
  14590. should be considered as undefined (this is the meaning of nil here).
  14591. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14592. (move-marker org-entry-property-inherited-from nil)
  14593. (let (value)
  14594. (org-with-wide-buffer
  14595. (catch 'exit
  14596. (while t
  14597. (when (setq value (org-entry-get nil property nil literal-nil))
  14598. (org-back-to-heading t)
  14599. (move-marker org-entry-property-inherited-from (point))
  14600. (throw 'exit nil))
  14601. (or (org-up-heading-safe) (throw 'exit nil)))))
  14602. (unless value
  14603. (setq value
  14604. (cdr (or (assoc-string property org-file-properties t)
  14605. (assoc-string property org-global-properties t)
  14606. (assoc-string property org-global-properties-fixed t)))))
  14607. (if literal-nil value (org-not-nil value))))
  14608. (defvar org-property-changed-functions nil
  14609. "Hook called when the value of a property has changed.
  14610. Each hook function should accept two arguments, the name of the property
  14611. and the new value.")
  14612. (defun org-entry-put (pom property value)
  14613. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14614. If the value is `nil', it is converted to the empty string. If
  14615. it is not a string, an error is raised.
  14616. PROPERTY can be any regular property (see
  14617. `org-special-properties'). It can also be \"TODO\",
  14618. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14619. For the last two properties, VALUE may have any of the special
  14620. values \"earlier\" and \"later\". The function then increases or
  14621. decreases scheduled or deadline date by one day."
  14622. (cond ((null value) (setq value ""))
  14623. ((not (stringp value)) (error "Properties values should be strings")))
  14624. (org-with-point-at pom
  14625. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14626. (org-back-to-heading t)
  14627. (org-with-limited-levels (org-back-to-heading t)))
  14628. (let ((beg (point)))
  14629. (cond
  14630. ((equal property "TODO")
  14631. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14632. ((not (member value org-todo-keywords-1))
  14633. (user-error "\"%s\" is not a valid TODO state" value)))
  14634. (org-todo value)
  14635. (org-set-tags nil 'align))
  14636. ((equal property "PRIORITY")
  14637. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14638. (org-set-tags nil 'align))
  14639. ((equal property "SCHEDULED")
  14640. (forward-line)
  14641. (if (and (org-looking-at-p org-planning-line-re)
  14642. (re-search-forward
  14643. org-scheduled-time-regexp (line-end-position) t))
  14644. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14645. ((string= value "later") (org-timestamp-change 1 'day))
  14646. ((string= value "") (org-schedule '(4)))
  14647. (t (org-schedule nil value)))
  14648. (if (member value '("earlier" "later" ""))
  14649. (call-interactively #'org-schedule)
  14650. (org-schedule nil value))))
  14651. ((equal property "DEADLINE")
  14652. (forward-line)
  14653. (if (and (org-looking-at-p org-planning-line-re)
  14654. (re-search-forward
  14655. org-deadline-time-regexp (line-end-position) t))
  14656. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14657. ((string= value "later") (org-timestamp-change 1 'day))
  14658. ((string= value "") (org-deadline '(4)))
  14659. (t (org-deadline nil value)))
  14660. (if (member value '("earlier" "later" ""))
  14661. (call-interactively #'org-deadline)
  14662. (org-deadline nil value))))
  14663. ((member property org-special-properties)
  14664. (error "The %s property cannot be set with `org-entry-put'" property))
  14665. (t
  14666. (let* ((range (org-get-property-block beg 'force))
  14667. (end (cdr range))
  14668. (case-fold-search t))
  14669. (goto-char (car range))
  14670. (if (re-search-forward (org-re-property property nil t) end t)
  14671. (progn (delete-region (match-beginning 0) (match-end 0))
  14672. (goto-char (match-beginning 0)))
  14673. (goto-char end)
  14674. (insert "\n")
  14675. (backward-char))
  14676. (insert ":" property ":")
  14677. (when value (insert " " value))
  14678. (org-indent-line)))))
  14679. (run-hook-with-args 'org-property-changed-functions property value)))
  14680. (defun org-buffer-property-keys (&optional specials defaults columns)
  14681. "Get all property keys in the current buffer.
  14682. When SPECIALS is non-nil, also list the special properties that
  14683. reflect things like tags and TODO state.
  14684. When DEFAULTS is non-nil, also include properties that has
  14685. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14686. DESCRIPTION, LOCATION, and LOGGING and others.
  14687. When COLUMNS in non-nil, also include property names given in
  14688. COLUMN formats in the current buffer."
  14689. (let ((case-fold-search t)
  14690. (props (append
  14691. (and specials org-special-properties)
  14692. (and defaults (cons org-effort-property org-default-properties))
  14693. nil)))
  14694. (org-with-wide-buffer
  14695. (goto-char (point-min))
  14696. (while (re-search-forward org-property-start-re nil t)
  14697. (let ((range (org-get-property-block)))
  14698. (catch 'skip
  14699. (unless range
  14700. (when (and (not (org-before-first-heading-p))
  14701. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14702. (line-beginning-position))))
  14703. (org-get-property-block nil t))
  14704. (throw 'skip nil))
  14705. (goto-char (car range))
  14706. (let ((begin (car range))
  14707. (end (cdr range)))
  14708. ;; Make sure that found property block is not located
  14709. ;; before current point, as it would generate an infloop.
  14710. ;; It can happen, for example, in the following
  14711. ;; situation:
  14712. ;;
  14713. ;; * Headline
  14714. ;; :PROPERTIES:
  14715. ;; ...
  14716. ;; :END:
  14717. ;; *************** Inlinetask
  14718. ;; #+BEGIN_EXAMPLE
  14719. ;; :PROPERTIES:
  14720. ;; #+END_EXAMPLE
  14721. ;;
  14722. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14723. (while (< (point) end)
  14724. (let ((p (progn (looking-at org-property-re)
  14725. (org-match-string-no-properties 2))))
  14726. ;; Only add true property name, not extension symbol.
  14727. (add-to-list 'props
  14728. (if (not (org-string-match-p "\\+\\'" p)) p
  14729. (substring p 0 -1))))
  14730. (forward-line))))
  14731. (outline-next-heading)))
  14732. (when columns
  14733. (goto-char (point-min))
  14734. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14735. (let ((element (org-element-at-point)))
  14736. (when (memq (org-element-type element) '(keyword node-property))
  14737. (let ((value (org-element-property :value element))
  14738. (start 0))
  14739. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14740. (setq start (match-end 0))
  14741. (let ((p (org-match-string-no-properties 1 value)))
  14742. (unless (member-ignore-case p org-special-properties)
  14743. (add-to-list 'props p))))))))))
  14744. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14745. (defun org-property-values (key)
  14746. "List all non-nil values of property KEY in current buffer."
  14747. (org-with-wide-buffer
  14748. (goto-char (point-min))
  14749. (let ((case-fold-search t)
  14750. (re (org-re-property key))
  14751. values)
  14752. (while (re-search-forward re nil t)
  14753. (add-to-list 'values (org-entry-get (point) key)))
  14754. values)))
  14755. (defun org-insert-property-drawer ()
  14756. "Insert a property drawer into the current entry."
  14757. (org-with-wide-buffer
  14758. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14759. (org-back-to-heading t)
  14760. (org-with-limited-levels (org-back-to-heading t)))
  14761. (forward-line)
  14762. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14763. (unless (org-looking-at-p org-property-drawer-re)
  14764. (let ((inhibit-read-only t))
  14765. (unless (bolp) (insert "\n"))
  14766. (let ((begin (point)))
  14767. (insert ":PROPERTIES:\n:END:\n")
  14768. (org-indent-region begin (point)))))))
  14769. (defun org-insert-drawer (&optional arg drawer)
  14770. "Insert a drawer at point.
  14771. When optional argument ARG is non-nil, insert a property drawer.
  14772. Optional argument DRAWER, when non-nil, is a string representing
  14773. drawer's name. Otherwise, the user is prompted for a name.
  14774. If a region is active, insert the drawer around that region
  14775. instead.
  14776. Point is left between drawer's boundaries."
  14777. (interactive "P")
  14778. (let* ((drawer (if arg "PROPERTIES"
  14779. (or drawer (read-from-minibuffer "Drawer: ")))))
  14780. (cond
  14781. ;; With C-u, fall back on `org-insert-property-drawer'
  14782. (arg (org-insert-property-drawer))
  14783. ;; Check validity of suggested drawer's name.
  14784. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14785. (user-error "Invalid drawer name"))
  14786. ;; With an active region, insert a drawer at point.
  14787. ((not (org-region-active-p))
  14788. (progn
  14789. (unless (bolp) (insert "\n"))
  14790. (insert (format ":%s:\n\n:END:\n" drawer))
  14791. (forward-line -2)))
  14792. ;; Otherwise, insert the drawer at point
  14793. (t
  14794. (let ((rbeg (region-beginning))
  14795. (rend (copy-marker (region-end))))
  14796. (unwind-protect
  14797. (progn
  14798. (goto-char rbeg)
  14799. (beginning-of-line)
  14800. (when (save-excursion
  14801. (re-search-forward org-outline-regexp-bol rend t))
  14802. (user-error "Drawers cannot contain headlines"))
  14803. ;; Position point at the beginning of the first
  14804. ;; non-blank line in region. Insert drawer's opening
  14805. ;; there, then indent it.
  14806. (org-skip-whitespace)
  14807. (beginning-of-line)
  14808. (insert ":" drawer ":\n")
  14809. (forward-line -1)
  14810. (indent-for-tab-command)
  14811. ;; Move point to the beginning of the first blank line
  14812. ;; after the last non-blank line in region. Insert
  14813. ;; drawer's closing, then indent it.
  14814. (goto-char rend)
  14815. (skip-chars-backward " \r\t\n")
  14816. (insert "\n:END:")
  14817. (deactivate-mark t)
  14818. (indent-for-tab-command)
  14819. (unless (eolp) (insert "\n")))
  14820. ;; Clear marker, whatever the outcome of insertion is.
  14821. (set-marker rend nil)))))))
  14822. (defvar org-property-set-functions-alist nil
  14823. "Property set function alist.
  14824. Each entry should have the following format:
  14825. (PROPERTY . READ-FUNCTION)
  14826. The read function will be called with the same argument as
  14827. `org-completing-read'.")
  14828. (defun org-set-property-function (property)
  14829. "Get the function that should be used to set PROPERTY.
  14830. This is computed according to `org-property-set-functions-alist'."
  14831. (or (cdr (assoc property org-property-set-functions-alist))
  14832. 'org-completing-read))
  14833. (defun org-read-property-value (property)
  14834. "Read PROPERTY value from user."
  14835. (let* ((completion-ignore-case t)
  14836. (allowed (org-property-get-allowed-values nil property 'table))
  14837. (cur (org-entry-get nil property))
  14838. (prompt (concat property " value"
  14839. (if (and cur (string-match "\\S-" cur))
  14840. (concat " [" cur "]") "") ": "))
  14841. (set-function (org-set-property-function property))
  14842. (val (if allowed
  14843. (funcall set-function prompt allowed nil
  14844. (not (get-text-property 0 'org-unrestricted
  14845. (caar allowed))))
  14846. (let (org-completion-use-ido org-completion-use-iswitchb)
  14847. (funcall set-function prompt
  14848. (mapcar 'list (org-property-values property))
  14849. nil nil "" nil cur)))))
  14850. (org-trim val)))
  14851. (defvar org-last-set-property nil)
  14852. (defvar org-last-set-property-value nil)
  14853. (defun org-read-property-name ()
  14854. "Read a property name."
  14855. (let ((completion-ignore-case t)
  14856. (default-prop (or (and (org-at-property-p)
  14857. (org-match-string-no-properties 2))
  14858. org-last-set-property)))
  14859. (org-completing-read
  14860. (concat "Property"
  14861. (if default-prop (concat " [" default-prop "]") "")
  14862. ": ")
  14863. (mapcar #'list (org-buffer-property-keys nil t t))
  14864. nil nil nil nil default-prop)))
  14865. (defun org-set-property-and-value (use-last)
  14866. "Allow to set [PROPERTY]: [value] direction from prompt.
  14867. When use-default, don't even ask, just use the last
  14868. \"[PROPERTY]: [value]\" string from the history."
  14869. (interactive "P")
  14870. (let* ((completion-ignore-case t)
  14871. (pv (or (and use-last org-last-set-property-value)
  14872. (org-completing-read
  14873. "Enter a \"[Property]: [value]\" pair: "
  14874. nil nil nil nil nil
  14875. org-last-set-property-value)))
  14876. prop val)
  14877. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14878. (setq prop (match-string 1 pv)
  14879. val (match-string 2 pv))
  14880. (org-set-property prop val))))
  14881. (defun org-set-property (property value)
  14882. "In the current entry, set PROPERTY to VALUE.
  14883. When called interactively, this will prompt for a property name, offering
  14884. completion on existing and default properties. And then it will prompt
  14885. for a value, offering completion either on allowed values (via an inherited
  14886. xxx_ALL property) or on existing values in other instances of this property
  14887. in the current file."
  14888. (interactive (list nil nil))
  14889. (let* ((property (or property (org-read-property-name)))
  14890. (value (or value (org-read-property-value property)))
  14891. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14892. (setq org-last-set-property property)
  14893. (setq org-last-set-property-value (concat property ": " value))
  14894. ;; Possibly postprocess the inserted value:
  14895. (when fn (setq value (funcall fn value)))
  14896. (unless (equal (org-entry-get nil property) value)
  14897. (org-entry-put nil property value))))
  14898. (defun org-find-property (property &optional value)
  14899. "Find first entry in buffer that sets PROPERTY.
  14900. When optional argument VALUE is non-nil, only consider an entry
  14901. if it contains PROPERTY set to this value. If PROPERTY should be
  14902. explicitly set to nil, use string \"nil\" for VALUE.
  14903. Return position where the entry begins, or nil if there is no
  14904. such entry. If narrowing is in effect, only search the visible
  14905. part of the buffer."
  14906. (save-excursion
  14907. (goto-char (point-min))
  14908. (let ((case-fold-search t)
  14909. (re (org-re-property property nil (not value) value)))
  14910. (catch 'exit
  14911. (while (re-search-forward re nil t)
  14912. (when (if value (org-at-property-p)
  14913. (org-entry-get (point) property nil t))
  14914. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14915. (defun org-delete-property (property)
  14916. "In the current entry, delete PROPERTY."
  14917. (interactive
  14918. (let* ((completion-ignore-case t)
  14919. (cat (org-entry-get (point) "CATEGORY"))
  14920. (props0 (org-entry-properties nil 'standard))
  14921. (props (if cat props0
  14922. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14923. (prop (if (< 1 (length props))
  14924. (org-icompleting-read "Property: " props nil t)
  14925. (caar props))))
  14926. (list prop)))
  14927. (if (not property)
  14928. (message "No property to delete in this entry")
  14929. (org-entry-delete nil property)
  14930. (message "Property \"%s\" deleted" property)))
  14931. (defun org-delete-property-globally (property)
  14932. "Remove PROPERTY globally, from all entries.
  14933. This function ignores narrowing, if any."
  14934. (interactive
  14935. (let* ((completion-ignore-case t)
  14936. (prop (org-icompleting-read
  14937. "Globally remove property: "
  14938. (mapcar #'list (org-buffer-property-keys)))))
  14939. (list prop)))
  14940. (org-with-wide-buffer
  14941. (goto-char (point-min))
  14942. (let ((count 0)
  14943. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  14944. (while (re-search-forward re nil t)
  14945. (when (org-entry-delete (point) property) (incf count)))
  14946. (message "Property \"%s\" removed from %d entries" property count))))
  14947. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14948. (defun org-compute-property-at-point ()
  14949. "Compute the property at point.
  14950. This looks for an enclosing column format, extracts the operator and
  14951. then applies it to the property in the column format's scope."
  14952. (interactive)
  14953. (unless (org-at-property-p)
  14954. (user-error "Not at a property"))
  14955. (let ((prop (org-match-string-no-properties 2)))
  14956. (org-columns-get-format-and-top-level)
  14957. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14958. (user-error "No operator defined for property %s" prop))
  14959. (org-columns-compute prop)))
  14960. (defvar org-property-allowed-value-functions nil
  14961. "Hook for functions supplying allowed values for a specific property.
  14962. The functions must take a single argument, the name of the property, and
  14963. return a flat list of allowed values. If \":ETC\" is one of
  14964. the values, this means that these values are intended as defaults for
  14965. completion, but that other values should be allowed too.
  14966. The functions must return nil if they are not responsible for this
  14967. property.")
  14968. (defun org-property-get-allowed-values (pom property &optional table)
  14969. "Get allowed values for the property PROPERTY.
  14970. When TABLE is non-nil, return an alist that can directly be used for
  14971. completion."
  14972. (let (vals)
  14973. (cond
  14974. ((equal property "TODO")
  14975. (setq vals (org-with-point-at pom
  14976. (append org-todo-keywords-1 '("")))))
  14977. ((equal property "PRIORITY")
  14978. (let ((n org-lowest-priority))
  14979. (while (>= n org-highest-priority)
  14980. (push (char-to-string n) vals)
  14981. (setq n (1- n)))))
  14982. ((equal property "CATEGORY"))
  14983. ((member property org-special-properties))
  14984. ((setq vals (run-hook-with-args-until-success
  14985. 'org-property-allowed-value-functions property)))
  14986. (t
  14987. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14988. (when (and vals (string-match "\\S-" vals))
  14989. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14990. (setq vals (mapcar (lambda (x)
  14991. (cond ((stringp x) x)
  14992. ((numberp x) (number-to-string x))
  14993. ((symbolp x) (symbol-name x))
  14994. (t "???")))
  14995. vals)))))
  14996. (when (member ":ETC" vals)
  14997. (setq vals (remove ":ETC" vals))
  14998. (org-add-props (car vals) '(org-unrestricted t)))
  14999. (if table (mapcar 'list vals) vals)))
  15000. (defun org-property-previous-allowed-value (&optional previous)
  15001. "Switch to the next allowed value for this property."
  15002. (interactive)
  15003. (org-property-next-allowed-value t))
  15004. (defun org-property-next-allowed-value (&optional previous)
  15005. "Switch to the next allowed value for this property."
  15006. (interactive)
  15007. (unless (org-at-property-p)
  15008. (user-error "Not at a property"))
  15009. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15010. (key (match-string 2))
  15011. (value (match-string 3))
  15012. (allowed (or (org-property-get-allowed-values (point) key)
  15013. (and (member value '("[ ]" "[-]" "[X]"))
  15014. '("[ ]" "[X]"))))
  15015. (heading (save-match-data (nth 4 (org-heading-components))))
  15016. nval)
  15017. (unless allowed
  15018. (user-error "Allowed values for this property have not been defined"))
  15019. (if previous (setq allowed (reverse allowed)))
  15020. (if (member value allowed)
  15021. (setq nval (car (cdr (member value allowed)))))
  15022. (setq nval (or nval (car allowed)))
  15023. (if (equal nval value)
  15024. (user-error "Only one allowed value for this property"))
  15025. (org-at-property-p)
  15026. (replace-match (concat " :" key ": " nval) t t)
  15027. (org-indent-line)
  15028. (beginning-of-line 1)
  15029. (skip-chars-forward " \t")
  15030. (when (equal prop org-effort-property)
  15031. (org-refresh-property
  15032. '((effort . identity)
  15033. (effort-minutes . org-duration-string-to-minutes))
  15034. nval)
  15035. (when (string= org-clock-current-task heading)
  15036. (setq org-clock-effort nval)
  15037. (org-clock-update-mode-line)))
  15038. (run-hook-with-args 'org-property-changed-functions key nval)))
  15039. (defun org-find-olp (path &optional this-buffer)
  15040. "Return a marker pointing to the entry at outline path OLP.
  15041. If anything goes wrong, throw an error.
  15042. You can wrap this call to catch the error like this:
  15043. (condition-case msg
  15044. (org-mobile-locate-entry (match-string 4))
  15045. (error (nth 1 msg)))
  15046. The return value will then be either a string with the error message,
  15047. or a marker if everything is OK.
  15048. If THIS-BUFFER is set, the outline path does not contain a file,
  15049. only headings."
  15050. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15051. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15052. (level 1)
  15053. (lmin 1)
  15054. (lmax 1)
  15055. limit re end found pos heading cnt flevel)
  15056. (unless buffer (error "File not found :%s" file))
  15057. (with-current-buffer buffer
  15058. (save-excursion
  15059. (save-restriction
  15060. (widen)
  15061. (setq limit (point-max))
  15062. (goto-char (point-min))
  15063. (while (setq heading (pop path))
  15064. (setq re (format org-complex-heading-regexp-format
  15065. (regexp-quote heading)))
  15066. (setq cnt 0 pos (point))
  15067. (while (re-search-forward re end t)
  15068. (setq level (- (match-end 1) (match-beginning 1)))
  15069. (if (and (>= level lmin) (<= level lmax))
  15070. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15071. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15072. lmax heading))
  15073. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15074. lmax heading))
  15075. (goto-char found)
  15076. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15077. (setq end (save-excursion (org-end-of-subtree t t))))
  15078. (when (org-at-heading-p)
  15079. (point-marker)))))))
  15080. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15081. "Find node HEADING in BUFFER.
  15082. Return a marker to the heading if it was found, or nil if not.
  15083. If POS-ONLY is set, return just the position instead of a marker.
  15084. The heading text must match exact, but it may have a TODO keyword,
  15085. a priority cookie and tags in the standard locations."
  15086. (with-current-buffer (or buffer (current-buffer))
  15087. (save-excursion
  15088. (save-restriction
  15089. (widen)
  15090. (goto-char (point-min))
  15091. (let (case-fold-search)
  15092. (if (re-search-forward
  15093. (format org-complex-heading-regexp-format
  15094. (regexp-quote heading)) nil t)
  15095. (if pos-only
  15096. (match-beginning 0)
  15097. (move-marker (make-marker) (match-beginning 0)))))))))
  15098. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15099. "Find Org node headline HEADING in all .org files in directory DIR.
  15100. When the target headline is found, return a marker to this location."
  15101. (let ((files (directory-files (or dir default-directory)
  15102. t "\\`[^.#].*\\.org\\'"))
  15103. file visiting m buffer)
  15104. (catch 'found
  15105. (while (setq file (pop files))
  15106. (message "trying %s" file)
  15107. (setq visiting (org-find-base-buffer-visiting file))
  15108. (setq buffer (or visiting (find-file-noselect file)))
  15109. (setq m (org-find-exact-headline-in-buffer
  15110. heading buffer))
  15111. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15112. (and m (throw 'found m))))))
  15113. (defun org-find-entry-with-id (ident)
  15114. "Locate the entry that contains the ID property with exact value IDENT.
  15115. IDENT can be a string, a symbol or a number, this function will search for
  15116. the string representation of it.
  15117. Return the position where this entry starts, or nil if there is no such entry."
  15118. (interactive "sID: ")
  15119. (let ((id (cond
  15120. ((stringp ident) ident)
  15121. ((symbolp ident) (symbol-name ident))
  15122. ((numberp ident) (number-to-string ident))
  15123. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15124. (org-with-wide-buffer (org-find-property "ID" id))))
  15125. ;;;; Timestamps
  15126. (defvar org-last-changed-timestamp nil)
  15127. (defvar org-last-inserted-timestamp nil
  15128. "The last time stamp inserted with `org-insert-time-stamp'.")
  15129. (defvar org-ts-what) ; dynamically scoped parameter
  15130. (defun org-time-stamp (arg &optional inactive)
  15131. "Prompt for a date/time and insert a time stamp.
  15132. If the user specifies a time like HH:MM or if this command is
  15133. called with at least one prefix argument, the time stamp contains
  15134. the date and the time. Otherwise, only the date is included.
  15135. All parts of a date not specified by the user are filled in from
  15136. the timestamp at point, if any, or the current date/time
  15137. otherwise.
  15138. If there is already a timestamp at the cursor, it is replaced.
  15139. With two universal prefix arguments, insert an active timestamp
  15140. with the current time without prompting the user.
  15141. When called from lisp, the timestamp is inactive if INACTIVE is
  15142. non-nil."
  15143. (interactive "P")
  15144. (let* ((ts
  15145. (cond ((org-at-date-range-p t)
  15146. (save-excursion
  15147. (goto-char (match-beginning 0))
  15148. (looking-at (if inactive org-ts-regexp-both org-ts-regexp)))
  15149. (match-string 0))
  15150. ((org-at-timestamp-p t) (match-string 0))))
  15151. ;; Default time is either the timestamp at point or today.
  15152. ;; When entering a range, only the range start is considered.
  15153. (default-time (if (not ts) (current-time)
  15154. (apply #'encode-time (org-parse-time-string ts))))
  15155. (default-input (and ts (org-get-compact-tod ts)))
  15156. (repeater (and ts
  15157. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15158. (match-string 0 ts)))
  15159. org-time-was-given
  15160. org-end-time-was-given
  15161. (time
  15162. (and (if (equal arg '(16)) (current-time)
  15163. ;; Preserve `this-command' and `last-command'.
  15164. (let ((this-command this-command)
  15165. (last-command last-command))
  15166. (org-read-date
  15167. arg 'totime nil nil default-time default-input
  15168. inactive))))))
  15169. (cond
  15170. ((and ts
  15171. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15172. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15173. (insert "--")
  15174. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15175. (ts
  15176. ;; Make sure we're on a timestamp. When in the middle of a date
  15177. ;; range, move arbitrarily to range end.
  15178. (unless (org-at-timestamp-p t)
  15179. (skip-chars-forward "-")
  15180. (org-at-timestamp-p t))
  15181. (replace-match "")
  15182. (setq org-last-changed-timestamp
  15183. (org-insert-time-stamp
  15184. time (or org-time-was-given arg)
  15185. inactive nil nil (list org-end-time-was-given)))
  15186. (when repeater
  15187. (backward-char)
  15188. (insert " " repeater)
  15189. (setq org-last-changed-timestamp
  15190. (concat (substring org-last-inserted-timestamp 0 -1)
  15191. " " repeater ">")))
  15192. (message "Timestamp updated"))
  15193. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15194. (t (org-insert-time-stamp
  15195. time (or org-time-was-given arg) inactive nil nil
  15196. (list org-end-time-was-given))))))
  15197. ;; FIXME: can we use this for something else, like computing time differences?
  15198. (defun org-get-compact-tod (s)
  15199. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15200. (let* ((t1 (match-string 1 s))
  15201. (h1 (string-to-number (match-string 2 s)))
  15202. (m1 (string-to-number (match-string 3 s)))
  15203. (t2 (and (match-end 4) (match-string 5 s)))
  15204. (h2 (and t2 (string-to-number (match-string 6 s))))
  15205. (m2 (and t2 (string-to-number (match-string 7 s))))
  15206. dh dm)
  15207. (if (not t2)
  15208. t1
  15209. (setq dh (- h2 h1) dm (- m2 m1))
  15210. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15211. (concat t1 "+" (number-to-string dh)
  15212. (and (/= 0 dm) (format ":%02d" dm)))))))
  15213. (defun org-time-stamp-inactive (&optional arg)
  15214. "Insert an inactive time stamp.
  15215. An inactive time stamp is enclosed in square brackets instead of angle
  15216. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15217. does not link to the calendar and cannot be changed with the S-cursor keys.
  15218. So these are more for recording a certain time/date."
  15219. (interactive "P")
  15220. (org-time-stamp arg 'inactive))
  15221. (defvar org-date-ovl (make-overlay 1 1))
  15222. (overlay-put org-date-ovl 'face 'org-date-selected)
  15223. (org-detach-overlay org-date-ovl)
  15224. (defvar org-ans1) ; dynamically scoped parameter
  15225. (defvar org-ans2) ; dynamically scoped parameter
  15226. (defvar org-plain-time-of-day-regexp) ; defined below
  15227. (defvar org-overriding-default-time nil) ; dynamically scoped
  15228. (defvar org-read-date-overlay nil)
  15229. (defvar org-dcst nil) ; dynamically scoped
  15230. (defvar org-read-date-history nil)
  15231. (defvar org-read-date-final-answer nil)
  15232. (defvar org-read-date-analyze-futurep nil)
  15233. (defvar org-read-date-analyze-forced-year nil)
  15234. (defvar org-read-date-inactive)
  15235. (defvar org-read-date-minibuffer-local-map
  15236. (let* ((map (make-sparse-keymap)))
  15237. (set-keymap-parent map minibuffer-local-map)
  15238. (org-defkey map (kbd ".")
  15239. (lambda () (interactive)
  15240. ;; Are we at the beginning of the prompt?
  15241. (if (looking-back "^[^:]+: ")
  15242. (org-eval-in-calendar '(calendar-goto-today))
  15243. (insert "."))))
  15244. (org-defkey map (kbd "C-.")
  15245. (lambda () (interactive)
  15246. (org-eval-in-calendar '(calendar-goto-today))))
  15247. (org-defkey map [(meta shift left)]
  15248. (lambda () (interactive)
  15249. (org-eval-in-calendar '(calendar-backward-month 1))))
  15250. (org-defkey map [(meta shift right)]
  15251. (lambda () (interactive)
  15252. (org-eval-in-calendar '(calendar-forward-month 1))))
  15253. (org-defkey map [(meta shift up)]
  15254. (lambda () (interactive)
  15255. (org-eval-in-calendar '(calendar-backward-year 1))))
  15256. (org-defkey map [(meta shift down)]
  15257. (lambda () (interactive)
  15258. (org-eval-in-calendar '(calendar-forward-year 1))))
  15259. (org-defkey map [?\e (shift left)]
  15260. (lambda () (interactive)
  15261. (org-eval-in-calendar '(calendar-backward-month 1))))
  15262. (org-defkey map [?\e (shift right)]
  15263. (lambda () (interactive)
  15264. (org-eval-in-calendar '(calendar-forward-month 1))))
  15265. (org-defkey map [?\e (shift up)]
  15266. (lambda () (interactive)
  15267. (org-eval-in-calendar '(calendar-backward-year 1))))
  15268. (org-defkey map [?\e (shift down)]
  15269. (lambda () (interactive)
  15270. (org-eval-in-calendar '(calendar-forward-year 1))))
  15271. (org-defkey map [(shift up)]
  15272. (lambda () (interactive)
  15273. (org-eval-in-calendar '(calendar-backward-week 1))))
  15274. (org-defkey map [(shift down)]
  15275. (lambda () (interactive)
  15276. (org-eval-in-calendar '(calendar-forward-week 1))))
  15277. (org-defkey map [(shift left)]
  15278. (lambda () (interactive)
  15279. (org-eval-in-calendar '(calendar-backward-day 1))))
  15280. (org-defkey map [(shift right)]
  15281. (lambda () (interactive)
  15282. (org-eval-in-calendar '(calendar-forward-day 1))))
  15283. (org-defkey map "!"
  15284. (lambda () (interactive)
  15285. (org-eval-in-calendar '(diary-view-entries))
  15286. (message "")))
  15287. (org-defkey map ">"
  15288. (lambda () (interactive)
  15289. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15290. (org-defkey map "<"
  15291. (lambda () (interactive)
  15292. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15293. (org-defkey map "\C-v"
  15294. (lambda () (interactive)
  15295. (org-eval-in-calendar
  15296. '(calendar-scroll-left-three-months 1))))
  15297. (org-defkey map "\M-v"
  15298. (lambda () (interactive)
  15299. (org-eval-in-calendar
  15300. '(calendar-scroll-right-three-months 1))))
  15301. map)
  15302. "Keymap for minibuffer commands when using `org-read-date'.")
  15303. (defvar org-def)
  15304. (defvar org-defdecode)
  15305. (defvar org-with-time)
  15306. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15307. default-time default-input inactive)
  15308. "Read a date, possibly a time, and make things smooth for the user.
  15309. The prompt will suggest to enter an ISO date, but you can also enter anything
  15310. which will at least partially be understood by `parse-time-string'.
  15311. Unrecognized parts of the date will default to the current day, month, year,
  15312. hour and minute. If this command is called to replace a timestamp at point,
  15313. or to enter the second timestamp of a range, the default time is taken
  15314. from the existing stamp. Furthermore, the command prefers the future,
  15315. so if you are giving a date where the year is not given, and the day-month
  15316. combination is already past in the current year, it will assume you
  15317. mean next year. For details, see the manual. A few examples:
  15318. 3-2-5 --> 2003-02-05
  15319. feb 15 --> currentyear-02-15
  15320. 2/15 --> currentyear-02-15
  15321. sep 12 9 --> 2009-09-12
  15322. 12:45 --> today 12:45
  15323. 22 sept 0:34 --> currentyear-09-22 0:34
  15324. 12 --> currentyear-currentmonth-12
  15325. Fri --> nearest Friday after today
  15326. -Tue --> last Tuesday
  15327. etc.
  15328. Furthermore you can specify a relative date by giving, as the *first* thing
  15329. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15330. change in days weeks, months, years.
  15331. With a single plus or minus, the date is relative to today. With a double
  15332. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15333. +4d --> four days from today
  15334. +4 --> same as above
  15335. +2w --> two weeks from today
  15336. ++5 --> five days from default date
  15337. The function understands only English month and weekday abbreviations.
  15338. While prompting, a calendar is popped up - you can also select the
  15339. date with the mouse (button 1). The calendar shows a period of three
  15340. months. To scroll it to other months, use the keys `>' and `<'.
  15341. If you don't like the calendar, turn it off with
  15342. \(setq org-read-date-popup-calendar nil)
  15343. With optional argument TO-TIME, the date will immediately be converted
  15344. to an internal time.
  15345. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15346. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15347. still enter a time, and this function will inform the calling routine
  15348. about this change. The calling routine may then choose to change the
  15349. format used to insert the time stamp into the buffer to include the time.
  15350. With optional argument FROM-STRING, read from this string instead from
  15351. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15352. the time/date that is used for everything that is not specified by the
  15353. user."
  15354. (require 'parse-time)
  15355. (let* ((org-time-stamp-rounding-minutes
  15356. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15357. (org-dcst org-display-custom-times)
  15358. (ct (org-current-time))
  15359. (org-def (or org-overriding-default-time default-time ct))
  15360. (org-defdecode (decode-time org-def))
  15361. (dummy (progn
  15362. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15363. (setcar (nthcdr 2 org-defdecode) -1)
  15364. (setcar (nthcdr 1 org-defdecode) 59)
  15365. (setq org-def (apply 'encode-time org-defdecode)
  15366. org-defdecode (decode-time org-def)))))
  15367. (cur-frame (selected-frame))
  15368. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15369. (calendar-frame-setup nil)
  15370. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15371. (calendar-move-hook nil)
  15372. (calendar-view-diary-initially-flag nil)
  15373. (calendar-view-holidays-initially-flag nil)
  15374. (timestr (format-time-string
  15375. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15376. (prompt (concat (if prompt (concat prompt " ") "")
  15377. (format "Date+time [%s]: " timestr)))
  15378. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15379. (cond
  15380. (from-string (setq ans from-string))
  15381. (org-read-date-popup-calendar
  15382. (save-excursion
  15383. (save-window-excursion
  15384. (calendar)
  15385. (when (eq calendar-setup 'calendar-only)
  15386. (setq cal-frame
  15387. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15388. (select-frame cal-frame))
  15389. (org-eval-in-calendar '(setq cursor-type nil) t)
  15390. (unwind-protect
  15391. (progn
  15392. (calendar-forward-day (- (time-to-days org-def)
  15393. (calendar-absolute-from-gregorian
  15394. (calendar-current-date))))
  15395. (org-eval-in-calendar nil t)
  15396. (let* ((old-map (current-local-map))
  15397. (map (copy-keymap calendar-mode-map))
  15398. (minibuffer-local-map
  15399. (copy-keymap org-read-date-minibuffer-local-map)))
  15400. (org-defkey map (kbd "RET") 'org-calendar-select)
  15401. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15402. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15403. (unwind-protect
  15404. (progn
  15405. (use-local-map map)
  15406. (setq org-read-date-inactive inactive)
  15407. (add-hook 'post-command-hook 'org-read-date-display)
  15408. (setq org-ans0 (read-string prompt default-input
  15409. 'org-read-date-history nil))
  15410. ;; org-ans0: from prompt
  15411. ;; org-ans1: from mouse click
  15412. ;; org-ans2: from calendar motion
  15413. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15414. (remove-hook 'post-command-hook 'org-read-date-display)
  15415. (use-local-map old-map)
  15416. (when org-read-date-overlay
  15417. (delete-overlay org-read-date-overlay)
  15418. (setq org-read-date-overlay nil)))))
  15419. (bury-buffer "*Calendar*")
  15420. (when cal-frame
  15421. (delete-frame cal-frame)
  15422. (select-frame-set-input-focus cur-frame))))))
  15423. (t ; Naked prompt only
  15424. (unwind-protect
  15425. (setq ans (read-string prompt default-input
  15426. 'org-read-date-history timestr))
  15427. (when org-read-date-overlay
  15428. (delete-overlay org-read-date-overlay)
  15429. (setq org-read-date-overlay nil)))))
  15430. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15431. (when org-read-date-analyze-forced-year
  15432. (message "Year was forced into %s"
  15433. (if org-read-date-force-compatible-dates
  15434. "compatible range (1970-2037)"
  15435. "range representable on this machine"))
  15436. (ding))
  15437. ;; One round trip to get rid of 34th of August and stuff like that....
  15438. (setq final (decode-time (apply 'encode-time final)))
  15439. (setq org-read-date-final-answer ans)
  15440. (if to-time
  15441. (apply 'encode-time final)
  15442. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15443. (format "%04d-%02d-%02d %02d:%02d"
  15444. (nth 5 final) (nth 4 final) (nth 3 final)
  15445. (nth 2 final) (nth 1 final))
  15446. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15447. (defun org-read-date-display ()
  15448. "Display the current date prompt interpretation in the minibuffer."
  15449. (when org-read-date-display-live
  15450. (when org-read-date-overlay
  15451. (delete-overlay org-read-date-overlay))
  15452. (when (minibufferp (current-buffer))
  15453. (save-excursion
  15454. (end-of-line 1)
  15455. (while (not (equal (buffer-substring
  15456. (max (point-min) (- (point) 4)) (point))
  15457. " "))
  15458. (insert " ")))
  15459. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15460. " " (or org-ans1 org-ans2)))
  15461. (org-end-time-was-given nil)
  15462. (f (org-read-date-analyze ans org-def org-defdecode))
  15463. (fmts (if org-dcst
  15464. org-time-stamp-custom-formats
  15465. org-time-stamp-formats))
  15466. (fmt (if (or org-with-time
  15467. (and (boundp 'org-time-was-given) org-time-was-given))
  15468. (cdr fmts)
  15469. (car fmts)))
  15470. (txt (format-time-string fmt (apply 'encode-time f)))
  15471. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15472. (txt (concat "=> " txt)))
  15473. (when (and org-end-time-was-given
  15474. (string-match org-plain-time-of-day-regexp txt))
  15475. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15476. org-end-time-was-given
  15477. (substring txt (match-end 0)))))
  15478. (when org-read-date-analyze-futurep
  15479. (setq txt (concat txt " (=>F)")))
  15480. (setq org-read-date-overlay
  15481. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15482. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15483. (defun org-read-date-analyze (ans org-def org-defdecode)
  15484. "Analyze the combined answer of the date prompt."
  15485. ;; FIXME: cleanup and comment
  15486. (let ((nowdecode (decode-time (current-time)))
  15487. delta deltan deltaw deltadef year month day
  15488. hour minute second wday pm h2 m2 tl wday1
  15489. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15490. (setq org-read-date-analyze-futurep nil
  15491. org-read-date-analyze-forced-year nil)
  15492. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15493. (setq ans "+0"))
  15494. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15495. (setq ans (replace-match "" t t ans)
  15496. deltan (car delta)
  15497. deltaw (nth 1 delta)
  15498. deltadef (nth 2 delta)))
  15499. ;; Check if there is an iso week date in there. If yes, store the
  15500. ;; info and postpone interpreting it until the rest of the parsing
  15501. ;; is done.
  15502. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15503. (setq iso-year (if (match-end 1)
  15504. (org-small-year-to-year
  15505. (string-to-number (match-string 1 ans))))
  15506. iso-weekday (if (match-end 3)
  15507. (string-to-number (match-string 3 ans)))
  15508. iso-week (string-to-number (match-string 2 ans)))
  15509. (setq ans (replace-match "" t t ans)))
  15510. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15511. (when (string-match
  15512. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15513. (setq year (if (match-end 2)
  15514. (string-to-number (match-string 2 ans))
  15515. (progn (setq kill-year t)
  15516. (string-to-number (format-time-string "%Y"))))
  15517. month (string-to-number (match-string 3 ans))
  15518. day (string-to-number (match-string 4 ans)))
  15519. (if (< year 100) (setq year (+ 2000 year)))
  15520. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15521. t nil ans)))
  15522. ;; Help matching dotted european dates
  15523. (when (string-match
  15524. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15525. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15526. (setq kill-year t)
  15527. (string-to-number (format-time-string "%Y")))
  15528. day (string-to-number (match-string 1 ans))
  15529. month (string-to-number (match-string 2 ans))
  15530. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15531. t nil ans)))
  15532. ;; Help matching american dates, like 5/30 or 5/30/7
  15533. (when (string-match
  15534. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15535. (setq year (if (match-end 4)
  15536. (string-to-number (match-string 4 ans))
  15537. (progn (setq kill-year t)
  15538. (string-to-number (format-time-string "%Y"))))
  15539. month (string-to-number (match-string 1 ans))
  15540. day (string-to-number (match-string 2 ans)))
  15541. (if (< year 100) (setq year (+ 2000 year)))
  15542. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15543. t nil ans)))
  15544. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15545. ;; If there is a time with am/pm, and *no* time without it, we convert
  15546. ;; so that matching will be successful.
  15547. (loop for i from 1 to 2 do ; twice, for end time as well
  15548. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15549. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15550. (setq hour (string-to-number (match-string 1 ans))
  15551. minute (if (match-end 3)
  15552. (string-to-number (match-string 3 ans))
  15553. 0)
  15554. pm (equal ?p
  15555. (string-to-char (downcase (match-string 4 ans)))))
  15556. (if (and (= hour 12) (not pm))
  15557. (setq hour 0)
  15558. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15559. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15560. t t ans))))
  15561. ;; Check if a time range is given as a duration
  15562. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15563. (setq hour (string-to-number (match-string 1 ans))
  15564. h2 (+ hour (string-to-number (match-string 3 ans)))
  15565. minute (string-to-number (match-string 2 ans))
  15566. m2 (+ minute (if (match-end 5) (string-to-number
  15567. (match-string 5 ans))0)))
  15568. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15569. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15570. t t ans)))
  15571. ;; Check if there is a time range
  15572. (when (boundp 'org-end-time-was-given)
  15573. (setq org-time-was-given nil)
  15574. (when (and (string-match org-plain-time-of-day-regexp ans)
  15575. (match-end 8))
  15576. (setq org-end-time-was-given (match-string 8 ans))
  15577. (setq ans (concat (substring ans 0 (match-beginning 7))
  15578. (substring ans (match-end 7))))))
  15579. (setq tl (parse-time-string ans)
  15580. day (or (nth 3 tl) (nth 3 org-defdecode))
  15581. month
  15582. (cond ((nth 4 tl))
  15583. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15584. ;; Day was specified. Make sure DAY+MONTH
  15585. ;; combination happens in the future.
  15586. ((nth 3 tl)
  15587. (setq futurep t)
  15588. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15589. (nth 4 nowdecode)))
  15590. (t (nth 4 org-defdecode)))
  15591. year
  15592. (cond ((and (not kill-year) (nth 5 tl)))
  15593. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15594. ;; Month was guessed in the future and is at least
  15595. ;; equal to NOWDECODE's. Fix year accordingly.
  15596. (futurep
  15597. (if (or (> month (nth 4 nowdecode))
  15598. (>= day (nth 3 nowdecode)))
  15599. (nth 5 nowdecode)
  15600. (1+ (nth 5 nowdecode))))
  15601. ;; Month was specified. Make sure MONTH+YEAR
  15602. ;; combination happens in the future.
  15603. ((nth 4 tl)
  15604. (setq futurep t)
  15605. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15606. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15607. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15608. (t (nth 5 nowdecode))))
  15609. (t (nth 5 org-defdecode)))
  15610. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15611. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15612. second (or (nth 0 tl) 0)
  15613. wday (nth 6 tl))
  15614. (when (and (eq org-read-date-prefer-future 'time)
  15615. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15616. (equal day (nth 3 nowdecode))
  15617. (equal month (nth 4 nowdecode))
  15618. (equal year (nth 5 nowdecode))
  15619. (nth 2 tl)
  15620. (or (< (nth 2 tl) (nth 2 nowdecode))
  15621. (and (= (nth 2 tl) (nth 2 nowdecode))
  15622. (nth 1 tl)
  15623. (< (nth 1 tl) (nth 1 nowdecode)))))
  15624. (setq day (1+ day)
  15625. futurep t))
  15626. ;; Special date definitions below
  15627. (cond
  15628. (iso-week
  15629. ;; There was an iso week
  15630. (require 'cal-iso)
  15631. (setq futurep nil)
  15632. (setq year (or iso-year year)
  15633. day (or iso-weekday wday 1)
  15634. wday nil ; to make sure that the trigger below does not match
  15635. iso-date (calendar-gregorian-from-absolute
  15636. (calendar-iso-to-absolute
  15637. (list iso-week day year))))
  15638. ; FIXME: Should we also push ISO weeks into the future?
  15639. ; (when (and org-read-date-prefer-future
  15640. ; (not iso-year)
  15641. ; (< (calendar-absolute-from-gregorian iso-date)
  15642. ; (time-to-days (current-time))))
  15643. ; (setq year (1+ year)
  15644. ; iso-date (calendar-gregorian-from-absolute
  15645. ; (calendar-iso-to-absolute
  15646. ; (list iso-week day year)))))
  15647. (setq month (car iso-date)
  15648. year (nth 2 iso-date)
  15649. day (nth 1 iso-date)))
  15650. (deltan
  15651. (setq futurep nil)
  15652. (unless deltadef
  15653. (let ((now (decode-time (current-time))))
  15654. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15655. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15656. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15657. ((equal deltaw "m") (setq month (+ month deltan)))
  15658. ((equal deltaw "y") (setq year (+ year deltan)))))
  15659. ((and wday (not (nth 3 tl)))
  15660. ;; Weekday was given, but no day, so pick that day in the week
  15661. ;; on or after the derived date.
  15662. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15663. (unless (equal wday wday1)
  15664. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15665. (if (and (boundp 'org-time-was-given)
  15666. (nth 2 tl))
  15667. (setq org-time-was-given t))
  15668. (if (< year 100) (setq year (+ 2000 year)))
  15669. ;; Check of the date is representable
  15670. (if org-read-date-force-compatible-dates
  15671. (progn
  15672. (if (< year 1970)
  15673. (setq year 1970 org-read-date-analyze-forced-year t))
  15674. (if (> year 2037)
  15675. (setq year 2037 org-read-date-analyze-forced-year t)))
  15676. (condition-case nil
  15677. (ignore (encode-time second minute hour day month year))
  15678. (error
  15679. (setq year (nth 5 org-defdecode))
  15680. (setq org-read-date-analyze-forced-year t))))
  15681. (setq org-read-date-analyze-futurep futurep)
  15682. (list second minute hour day month year)))
  15683. (defvar parse-time-weekdays)
  15684. (defun org-read-date-get-relative (s today default)
  15685. "Check string S for special relative date string.
  15686. TODAY and DEFAULT are internal times, for today and for a default.
  15687. Return shift list (N what def-flag)
  15688. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15689. N is the number of WHATs to shift.
  15690. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15691. the DEFAULT date rather than TODAY."
  15692. (require 'parse-time)
  15693. (when (and
  15694. (string-match
  15695. (concat
  15696. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15697. "\\([0-9]+\\)?"
  15698. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15699. "\\([ \t]\\|$\\)") s)
  15700. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15701. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15702. (string-to-char (substring (match-string 1 s) -1))
  15703. ?+))
  15704. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15705. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15706. (what (if (match-end 3) (match-string 3 s) "d"))
  15707. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15708. (date (if rel default today))
  15709. (wday (nth 6 (decode-time date)))
  15710. delta)
  15711. (if wday1
  15712. (progn
  15713. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15714. (if (= delta 0) (setq delta 7))
  15715. (if (= dir ?-)
  15716. (progn
  15717. (setq delta (- delta 7))
  15718. (if (= delta 0) (setq delta -7))))
  15719. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15720. (list delta "d" rel))
  15721. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15722. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15723. "Turn a user-specified date into the internal representation.
  15724. The internal representation needed by the calendar is (month day year).
  15725. This is a wrapper to handle the brain-dead convention in calendar that
  15726. user function argument order change dependent on argument order."
  15727. (if (boundp 'calendar-date-style)
  15728. (cond
  15729. ((eq calendar-date-style 'american)
  15730. (list arg1 arg2 arg3))
  15731. ((eq calendar-date-style 'european)
  15732. (list arg2 arg1 arg3))
  15733. ((eq calendar-date-style 'iso)
  15734. (list arg2 arg3 arg1)))
  15735. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15736. (if (org-bound-and-true-p european-calendar-style)
  15737. (list arg2 arg1 arg3)
  15738. (list arg1 arg2 arg3)))))
  15739. (defun org-eval-in-calendar (form &optional keepdate)
  15740. "Eval FORM in the calendar window and return to current window.
  15741. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15742. otherwise stick to the current value of `org-ans2'."
  15743. (let ((sf (selected-frame))
  15744. (sw (selected-window)))
  15745. (select-window (get-buffer-window "*Calendar*" t))
  15746. (eval form)
  15747. (when (and (not keepdate) (calendar-cursor-to-date))
  15748. (let* ((date (calendar-cursor-to-date))
  15749. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15750. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15751. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15752. (select-window sw)
  15753. (org-select-frame-set-input-focus sf)))
  15754. (defun org-calendar-select ()
  15755. "Return to `org-read-date' with the date currently selected.
  15756. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15757. (interactive)
  15758. (when (calendar-cursor-to-date)
  15759. (let* ((date (calendar-cursor-to-date))
  15760. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15761. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15762. (if (active-minibuffer-window) (exit-minibuffer))))
  15763. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15764. "Insert a date stamp for the date given by the internal TIME.
  15765. See `format-time-string' for the format of TIME.
  15766. WITH-HM means use the stamp format that includes the time of the day.
  15767. INACTIVE means use square brackets instead of angular ones, so that the
  15768. stamp will not contribute to the agenda.
  15769. PRE and POST are optional strings to be inserted before and after the
  15770. stamp.
  15771. The command returns the inserted time stamp."
  15772. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15773. stamp)
  15774. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15775. (insert-before-markers (or pre ""))
  15776. (when (listp extra)
  15777. (setq extra (car extra))
  15778. (if (and (stringp extra)
  15779. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15780. (setq extra (format "-%02d:%02d"
  15781. (string-to-number (match-string 1 extra))
  15782. (string-to-number (match-string 2 extra))))
  15783. (setq extra nil)))
  15784. (when extra
  15785. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15786. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15787. (insert-before-markers (or post ""))
  15788. (setq org-last-inserted-timestamp stamp)))
  15789. (defun org-toggle-time-stamp-overlays ()
  15790. "Toggle the use of custom time stamp formats."
  15791. (interactive)
  15792. (setq org-display-custom-times (not org-display-custom-times))
  15793. (unless org-display-custom-times
  15794. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15795. (while (setq p (next-single-property-change p 'display))
  15796. (if (and (get-text-property p 'display)
  15797. (eq (get-text-property p 'face) 'org-date))
  15798. (remove-text-properties
  15799. p (setq p (next-single-property-change p 'display))
  15800. '(display t))))
  15801. (set-buffer-modified-p bmp)))
  15802. (if (featurep 'xemacs)
  15803. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15804. (org-restart-font-lock)
  15805. (setq org-table-may-need-update t)
  15806. (if org-display-custom-times
  15807. (message "Time stamps are overlaid with custom format")
  15808. (message "Time stamp overlays removed")))
  15809. (defun org-display-custom-time (beg end)
  15810. "Overlay modified time stamp format over timestamp between BEG and END."
  15811. (let* ((ts (buffer-substring beg end))
  15812. t1 w1 with-hm tf time str w2 (off 0))
  15813. (save-match-data
  15814. (setq t1 (org-parse-time-string ts t))
  15815. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15816. (setq off (- (match-end 0) (match-beginning 0)))))
  15817. (setq end (- end off))
  15818. (setq w1 (- end beg)
  15819. with-hm (and (nth 1 t1) (nth 2 t1))
  15820. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15821. time (org-fix-decoded-time t1)
  15822. str (org-add-props
  15823. (format-time-string
  15824. (substring tf 1 -1) (apply 'encode-time time))
  15825. nil 'mouse-face 'highlight)
  15826. w2 (length str))
  15827. (if (not (= w2 w1))
  15828. (add-text-properties (1+ beg) (+ 2 beg)
  15829. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15830. (if (featurep 'xemacs)
  15831. (progn
  15832. (put-text-property beg end 'invisible t)
  15833. (put-text-property beg end 'end-glyph (make-glyph str)))
  15834. (put-text-property beg end 'display str))))
  15835. (defun org-fix-decoded-time (time)
  15836. "Set 0 instead of nil for the first 6 elements of time.
  15837. Don't touch the rest."
  15838. (let ((n 0))
  15839. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15840. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15841. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15842. "Difference between TIMESTAMP-STRING and now in days.
  15843. If SECONDS is non-nil, return the difference in seconds."
  15844. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15845. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15846. (funcall fdiff (current-time)))))
  15847. (defun org-deadline-close (timestamp-string &optional ndays)
  15848. "Is the time in TIMESTAMP-STRING close to the current date?"
  15849. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15850. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15851. (not (org-entry-is-done-p))))
  15852. (defun org-get-wdays (ts &optional delay zero-delay)
  15853. "Get the deadline lead time appropriate for timestring TS.
  15854. When DELAY is non-nil, get the delay time for scheduled items
  15855. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15856. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15857. don't try to find the delay cookie in the scheduled timestamp."
  15858. (let ((tv (if delay org-scheduled-delay-days
  15859. org-deadline-warning-days)))
  15860. (cond
  15861. ((or (and delay (< tv 0))
  15862. (and delay zero-delay (<= tv 0))
  15863. (and (not delay) (<= tv 0)))
  15864. ;; Enforce this value no matter what
  15865. (- tv))
  15866. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15867. ;; lead time is specified.
  15868. (floor (* (string-to-number (match-string 1 ts))
  15869. (cdr (assoc (match-string 2 ts)
  15870. '(("d" . 1) ("w" . 7)
  15871. ("m" . 30.4) ("y" . 365.25)
  15872. ("h" . 0.041667)))))))
  15873. ;; go for the default.
  15874. (t tv))))
  15875. (defun org-calendar-select-mouse (ev)
  15876. "Return to `org-read-date' with the date currently selected.
  15877. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15878. (interactive "e")
  15879. (mouse-set-point ev)
  15880. (when (calendar-cursor-to-date)
  15881. (let* ((date (calendar-cursor-to-date))
  15882. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15883. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15884. (if (active-minibuffer-window) (exit-minibuffer))))
  15885. (defun org-check-deadlines (ndays)
  15886. "Check if there are any deadlines due or past due.
  15887. A deadline is considered due if it happens within `org-deadline-warning-days'
  15888. days from today's date. If the deadline appears in an entry marked DONE,
  15889. it is not shown. The prefix arg NDAYS can be used to test that many
  15890. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15891. (interactive "P")
  15892. (let* ((org-warn-days
  15893. (cond
  15894. ((equal ndays '(4)) 100000)
  15895. (ndays (prefix-numeric-value ndays))
  15896. (t (abs org-deadline-warning-days))))
  15897. (case-fold-search nil)
  15898. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15899. (callback
  15900. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15901. (message "%d deadlines past-due or due within %d days"
  15902. (org-occur regexp nil callback)
  15903. org-warn-days)))
  15904. (defsubst org-re-timestamp (type)
  15905. "Return a regexp for timestamp TYPE.
  15906. Allowed values for TYPE are:
  15907. all: all timestamps
  15908. active: only active timestamps (<...>)
  15909. inactive: only inactive timestamps ([...])
  15910. scheduled: only scheduled timestamps
  15911. deadline: only deadline timestamps
  15912. closed: only closed time-stamps
  15913. When TYPE is nil, fall back on returning a regexp that matches
  15914. both scheduled and deadline timestamps."
  15915. (case type
  15916. (all org-ts-regexp-both)
  15917. (active org-ts-regexp)
  15918. (inactive org-ts-regexp-inactive)
  15919. (scheduled org-scheduled-time-regexp)
  15920. (deadline org-deadline-time-regexp)
  15921. (closed org-closed-time-regexp)
  15922. (otherwise
  15923. (concat "\\<"
  15924. (regexp-opt (list org-deadline-string org-scheduled-string))
  15925. " *<\\([^>]+\\)>"))))
  15926. (defun org-check-before-date (date)
  15927. "Check if there are deadlines or scheduled entries before DATE."
  15928. (interactive (list (org-read-date)))
  15929. (let ((case-fold-search nil)
  15930. (regexp (org-re-timestamp org-ts-type))
  15931. (callback
  15932. `(lambda ()
  15933. (and ,(if (memq org-ts-type '(active inactive all))
  15934. '(eq (org-element-type (org-element-context) 'timestamp))
  15935. '(org-at-planning-p))
  15936. (time-less-p
  15937. (org-time-string-to-time (match-string 1))
  15938. (org-time-string-to-time date))))))
  15939. (message "%d entries before %s"
  15940. (org-occur regexp nil callback) date)))
  15941. (defun org-check-after-date (date)
  15942. "Check if there are deadlines or scheduled entries after DATE."
  15943. (interactive (list (org-read-date)))
  15944. (let ((case-fold-search nil)
  15945. (regexp (org-re-timestamp org-ts-type))
  15946. (callback
  15947. `(lambda ()
  15948. (and ,(if (memq org-ts-type '(active inactive all))
  15949. '(eq (org-element-type (org-element-context) 'timestamp))
  15950. '(org-at-planning-p))
  15951. (not (time-less-p
  15952. (org-time-string-to-time (match-string 1))
  15953. (org-time-string-to-time date)))))))
  15954. (message "%d entries after %s"
  15955. (org-occur regexp nil callback) date)))
  15956. (defun org-check-dates-range (start-date end-date)
  15957. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15958. (interactive (list (org-read-date nil nil nil "Range starts")
  15959. (org-read-date nil nil nil "Range end")))
  15960. (let ((case-fold-search nil)
  15961. (regexp (org-re-timestamp org-ts-type))
  15962. (callback
  15963. `(lambda ()
  15964. (let ((match (match-string 1)))
  15965. (and
  15966. ,(if (memq org-ts-type '(active inactive all))
  15967. '(eq (org-element-type (org-element-context) 'timestamp))
  15968. '(org-at-planning-p))
  15969. (not (time-less-p
  15970. (org-time-string-to-time match)
  15971. (org-time-string-to-time start-date)))
  15972. (time-less-p
  15973. (org-time-string-to-time match)
  15974. (org-time-string-to-time end-date)))))))
  15975. (message "%d entries between %s and %s"
  15976. (org-occur regexp nil callback) start-date end-date)))
  15977. (defun org-evaluate-time-range (&optional to-buffer)
  15978. "Evaluate a time range by computing the difference between start and end.
  15979. Normally the result is just printed in the echo area, but with prefix arg
  15980. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15981. If the time range is actually in a table, the result is inserted into the
  15982. next column.
  15983. For time difference computation, a year is assumed to be exactly 365
  15984. days in order to avoid rounding problems."
  15985. (interactive "P")
  15986. (or
  15987. (org-clock-update-time-maybe)
  15988. (save-excursion
  15989. (unless (org-at-date-range-p t)
  15990. (goto-char (point-at-bol))
  15991. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15992. (if (not (org-at-date-range-p t))
  15993. (user-error "Not at a time-stamp range, and none found in current line")))
  15994. (let* ((ts1 (match-string 1))
  15995. (ts2 (match-string 2))
  15996. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15997. (match-end (match-end 0))
  15998. (time1 (org-time-string-to-time ts1))
  15999. (time2 (org-time-string-to-time ts2))
  16000. (t1 (org-float-time time1))
  16001. (t2 (org-float-time time2))
  16002. (diff (abs (- t2 t1)))
  16003. (negative (< (- t2 t1) 0))
  16004. ;; (ys (floor (* 365 24 60 60)))
  16005. (ds (* 24 60 60))
  16006. (hs (* 60 60))
  16007. (fy "%dy %dd %02d:%02d")
  16008. (fy1 "%dy %dd")
  16009. (fd "%dd %02d:%02d")
  16010. (fd1 "%dd")
  16011. (fh "%02d:%02d")
  16012. y d h m align)
  16013. (if havetime
  16014. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16015. y 0
  16016. d (floor (/ diff ds)) diff (mod diff ds)
  16017. h (floor (/ diff hs)) diff (mod diff hs)
  16018. m (floor (/ diff 60)))
  16019. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16020. y 0
  16021. d (floor (+ (/ diff ds) 0.5))
  16022. h 0 m 0))
  16023. (if (not to-buffer)
  16024. (message "%s" (org-make-tdiff-string y d h m))
  16025. (if (org-at-table-p)
  16026. (progn
  16027. (goto-char match-end)
  16028. (setq align t)
  16029. (and (looking-at " *|") (goto-char (match-end 0))))
  16030. (goto-char match-end))
  16031. (if (looking-at
  16032. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16033. (replace-match ""))
  16034. (if negative (insert " -"))
  16035. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16036. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16037. (insert " " (format fh h m))))
  16038. (if align (org-table-align))
  16039. (message "Time difference inserted")))))
  16040. (defun org-make-tdiff-string (y d h m)
  16041. (let ((fmt "")
  16042. (l nil))
  16043. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16044. l (push y l)))
  16045. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16046. l (push d l)))
  16047. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16048. l (push h l)))
  16049. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16050. l (push m l)))
  16051. (apply 'format fmt (nreverse l))))
  16052. (defun org-time-string-to-time (s &optional buffer pos)
  16053. "Convert a timestamp string into internal time."
  16054. (condition-case errdata
  16055. (apply 'encode-time (org-parse-time-string s))
  16056. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16057. s (if (not (and buffer pos))
  16058. ""
  16059. (format " at %d in buffer `%s'" pos buffer))
  16060. (cdr errdata)))))
  16061. (defun org-time-string-to-seconds (s)
  16062. "Convert a timestamp string to a number of seconds."
  16063. (org-float-time (org-time-string-to-time s)))
  16064. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16065. "Convert a time stamp to an absolute day number.
  16066. If there is a specifier for a cyclic time stamp, get the closest
  16067. date to DAYNR.
  16068. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  16069. The variable `date' is bound by the calendar when this is called."
  16070. (cond
  16071. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16072. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16073. daynr
  16074. (+ daynr 1000)))
  16075. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  16076. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16077. (time-to-days (current-time))) (match-string 0 s)
  16078. prefer show-all))
  16079. (t (time-to-days
  16080. (condition-case errdata
  16081. (apply 'encode-time (org-parse-time-string s))
  16082. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16083. s (if (not (and buffer pos))
  16084. ""
  16085. (format " at %d in buffer `%s'" pos buffer))
  16086. (cdr errdata))))))))
  16087. (defun org-days-to-iso-week (days)
  16088. "Return the iso week number."
  16089. (require 'cal-iso)
  16090. (car (calendar-iso-from-absolute days)))
  16091. (defun org-small-year-to-year (year)
  16092. "Convert 2-digit years into 4-digit years.
  16093. YEAR is expanded into one of the 30 next years, if possible, or
  16094. into a past one. Any year larger than 99 is returned unchanged."
  16095. (if (>= year 100) year
  16096. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16097. (century (/ current 100))
  16098. (offset (- year (% current 100))))
  16099. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16100. ((> offset -70) (+ (* century 100) year))
  16101. (t (+ (* (1+ century) 100) year))))))
  16102. (defun org-time-from-absolute (d)
  16103. "Return the time corresponding to date D.
  16104. D may be an absolute day number, or a calendar-type list (month day year)."
  16105. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16106. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16107. (defun org-calendar-holiday ()
  16108. "List of holidays, for Diary display in Org-mode."
  16109. (require 'holidays)
  16110. (let ((hl (funcall
  16111. (if (fboundp 'calendar-check-holidays)
  16112. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16113. (if hl (mapconcat 'identity hl "; "))))
  16114. (defun org-diary-sexp-entry (sexp entry date)
  16115. "Process a SEXP diary ENTRY for DATE."
  16116. (require 'diary-lib)
  16117. (let ((result (if calendar-debug-sexp
  16118. (let ((stack-trace-on-error t))
  16119. (eval (car (read-from-string sexp))))
  16120. (condition-case nil
  16121. (eval (car (read-from-string sexp)))
  16122. (error
  16123. (beep)
  16124. (message "Bad sexp at line %d in %s: %s"
  16125. (org-current-line)
  16126. (buffer-file-name) sexp)
  16127. (sleep-for 2))))))
  16128. (cond ((stringp result) (split-string result "; "))
  16129. ((and (consp result)
  16130. (not (consp (cdr result)))
  16131. (stringp (cdr result))) (cdr result))
  16132. ((and (consp result)
  16133. (stringp (car result))) result)
  16134. (result entry))))
  16135. (defun org-diary-to-ical-string (frombuf)
  16136. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16137. This uses the icalendar.el library."
  16138. (let* ((tmpdir (if (featurep 'xemacs)
  16139. (temp-directory)
  16140. temporary-file-directory))
  16141. (tmpfile (make-temp-name
  16142. (expand-file-name "orgics" tmpdir)))
  16143. buf rtn b e)
  16144. (with-current-buffer frombuf
  16145. (icalendar-export-region (point-min) (point-max) tmpfile)
  16146. (setq buf (find-buffer-visiting tmpfile))
  16147. (set-buffer buf)
  16148. (goto-char (point-min))
  16149. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16150. (setq b (match-beginning 0)))
  16151. (goto-char (point-max))
  16152. (if (re-search-backward "^END:VEVENT" nil t)
  16153. (setq e (match-end 0)))
  16154. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16155. (kill-buffer buf)
  16156. (delete-file tmpfile)
  16157. rtn))
  16158. (defun org-closest-date (start current change prefer show-all)
  16159. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16160. When PREFER is `past', return a date that is either CURRENT or past.
  16161. When PREFER is `future', return a date that is either CURRENT or future.
  16162. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16163. ;; Make the proper lists from the dates
  16164. (catch 'exit
  16165. (let ((a1 '(("h" . hour)
  16166. ("d" . day)
  16167. ("w" . week)
  16168. ("m" . month)
  16169. ("y" . year)))
  16170. (shour (nth 2 (org-parse-time-string start)))
  16171. dn dw sday cday n1 n2 n0
  16172. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16173. (setq start (org-date-to-gregorian start)
  16174. current (org-date-to-gregorian
  16175. (if show-all
  16176. current
  16177. (time-to-days (current-time))))
  16178. sday (calendar-absolute-from-gregorian start)
  16179. cday (calendar-absolute-from-gregorian current))
  16180. (if (<= cday sday) (throw 'exit sday))
  16181. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16182. (setq dn (string-to-number (match-string 1 change))
  16183. dw (cdr (assoc (match-string 2 change) a1)))
  16184. (user-error "Invalid change specifier: %s" change))
  16185. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16186. (cond
  16187. ((eq dw 'hour)
  16188. (let ((missing-hours
  16189. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16190. dn)))
  16191. (setq n1 (if (zerop missing-hours) cday
  16192. (- cday (1+ (floor (/ missing-hours 24)))))
  16193. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16194. ((eq dw 'day)
  16195. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16196. n2 (+ n1 dn)))
  16197. ((eq dw 'year)
  16198. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16199. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16200. (setq date1 (list m d y1)
  16201. n1 (calendar-absolute-from-gregorian date1)
  16202. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16203. n2 (calendar-absolute-from-gregorian date2)))
  16204. ((eq dw 'month)
  16205. ;; approx number of month between the two dates
  16206. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16207. ;; How often does dn fit in there?
  16208. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16209. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16210. m (+ m nm)
  16211. ny (floor (/ m 12))
  16212. y (+ y ny)
  16213. m (- m (* ny 12)))
  16214. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16215. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16216. (setq m2 (+ m dn) y2 y)
  16217. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16218. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16219. (while (<= n2 cday)
  16220. (setq n1 n2 m m2 y y2)
  16221. (setq m2 (+ m dn) y2 y)
  16222. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16223. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16224. ;; Make sure n1 is the earlier date
  16225. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16226. (if show-all
  16227. (cond
  16228. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16229. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16230. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16231. (cond
  16232. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16233. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16234. (t (if (= cday n1) n1 n2)))))))
  16235. (defun org-date-to-gregorian (date)
  16236. "Turn any specification of DATE into a Gregorian date for the calendar."
  16237. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16238. ((and (listp date) (= (length date) 3)) date)
  16239. ((stringp date)
  16240. (setq date (org-parse-time-string date))
  16241. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16242. ((listp date)
  16243. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16244. (defun org-parse-time-string (s &optional nodefault)
  16245. "Parse the standard Org-mode time string.
  16246. This should be a lot faster than the normal `parse-time-string'.
  16247. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16248. hour and minute fields will be nil if not given."
  16249. (cond ((string-match org-ts-regexp0 s)
  16250. (list 0
  16251. (if (or (match-beginning 8) (not nodefault))
  16252. (string-to-number (or (match-string 8 s) "0")))
  16253. (if (or (match-beginning 7) (not nodefault))
  16254. (string-to-number (or (match-string 7 s) "0")))
  16255. (string-to-number (match-string 4 s))
  16256. (string-to-number (match-string 3 s))
  16257. (string-to-number (match-string 2 s))
  16258. nil nil nil))
  16259. ((string-match "^<[^>]+>$" s)
  16260. (decode-time (seconds-to-time (org-matcher-time s))))
  16261. (t (error "Not a standard Org-mode time string: %s" s))))
  16262. (defun org-timestamp-up (&optional arg)
  16263. "Increase the date item at the cursor by one.
  16264. If the cursor is on the year, change the year. If it is on the month,
  16265. the day or the time, change that.
  16266. With prefix ARG, change by that many units."
  16267. (interactive "p")
  16268. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16269. (defun org-timestamp-down (&optional arg)
  16270. "Decrease the date item at the cursor by one.
  16271. If the cursor is on the year, change the year. If it is on the month,
  16272. the day or the time, change that.
  16273. With prefix ARG, change by that many units."
  16274. (interactive "p")
  16275. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16276. (defun org-timestamp-up-day (&optional arg)
  16277. "Increase the date in the time stamp by one day.
  16278. With prefix ARG, change that many days."
  16279. (interactive "p")
  16280. (if (and (not (org-at-timestamp-p t))
  16281. (org-at-heading-p))
  16282. (org-todo 'up)
  16283. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16284. (defun org-timestamp-down-day (&optional arg)
  16285. "Decrease the date in the time stamp by one day.
  16286. With prefix ARG, change that many days."
  16287. (interactive "p")
  16288. (if (and (not (org-at-timestamp-p t))
  16289. (org-at-heading-p))
  16290. (org-todo 'down)
  16291. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16292. (defun org-at-timestamp-p (&optional inactive-ok)
  16293. "Determine if the cursor is in or at a timestamp."
  16294. (interactive)
  16295. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16296. (pos (point))
  16297. (ans (or (looking-at tsr)
  16298. (save-excursion
  16299. (skip-chars-backward "^[<\n\r\t")
  16300. (if (> (point) (point-min)) (backward-char 1))
  16301. (and (looking-at tsr)
  16302. (> (- (match-end 0) pos) -1))))))
  16303. (and ans
  16304. (boundp 'org-ts-what)
  16305. (setq org-ts-what
  16306. (cond
  16307. ((= pos (match-beginning 0)) 'bracket)
  16308. ;; Point is considered to be "on the bracket" whether
  16309. ;; it's really on it or right after it.
  16310. ((= pos (1- (match-end 0))) 'bracket)
  16311. ((= pos (match-end 0)) 'after)
  16312. ((org-pos-in-match-range pos 2) 'year)
  16313. ((org-pos-in-match-range pos 3) 'month)
  16314. ((org-pos-in-match-range pos 7) 'hour)
  16315. ((org-pos-in-match-range pos 8) 'minute)
  16316. ((or (org-pos-in-match-range pos 4)
  16317. (org-pos-in-match-range pos 5)) 'day)
  16318. ((and (> pos (or (match-end 8) (match-end 5)))
  16319. (< pos (match-end 0)))
  16320. (- pos (or (match-end 8) (match-end 5))))
  16321. (t 'day))))
  16322. ans))
  16323. (defun org-toggle-timestamp-type ()
  16324. "Toggle the type (<active> or [inactive]) of a time stamp."
  16325. (interactive)
  16326. (when (org-at-timestamp-p t)
  16327. (let ((beg (match-beginning 0)) (end (match-end 0))
  16328. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16329. (save-excursion
  16330. (goto-char beg)
  16331. (while (re-search-forward "[][<>]" end t)
  16332. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16333. t t)))
  16334. (message "Timestamp is now %sactive"
  16335. (if (equal (char-after beg) ?<) "" "in")))))
  16336. (defun org-at-clock-log-p nil
  16337. "Is the cursor on the clock log line?"
  16338. (save-excursion
  16339. (move-beginning-of-line 1)
  16340. (looking-at org-clock-line-re)))
  16341. (defvar org-clock-history) ; defined in org-clock.el
  16342. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16343. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16344. "Change the date in the time stamp at point.
  16345. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16346. `year', `minute', `second'. If WHAT is not given, the cursor position
  16347. in the timestamp determines what will be changed.
  16348. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16349. (let ((origin (point)) origin-cat
  16350. with-hm inactive
  16351. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16352. org-ts-what
  16353. extra rem
  16354. ts time time0 fixnext clrgx)
  16355. (if (not (org-at-timestamp-p t))
  16356. (user-error "Not at a timestamp"))
  16357. (if (and (not what) (eq org-ts-what 'bracket))
  16358. (org-toggle-timestamp-type)
  16359. ;; Point isn't on brackets. Remember the part of the time-stamp
  16360. ;; the point was in. Indeed, size of time-stamps may change,
  16361. ;; but point must be kept in the same category nonetheless.
  16362. (setq origin-cat org-ts-what)
  16363. (if (and (not what) (not (eq org-ts-what 'day))
  16364. org-display-custom-times
  16365. (get-text-property (point) 'display)
  16366. (not (get-text-property (1- (point)) 'display)))
  16367. (setq org-ts-what 'day))
  16368. (setq org-ts-what (or what org-ts-what)
  16369. inactive (= (char-after (match-beginning 0)) ?\[)
  16370. ts (match-string 0))
  16371. (replace-match "")
  16372. (when (string-match
  16373. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16374. ts)
  16375. (setq extra (match-string 1 ts))
  16376. (if suppress-tmp-delay
  16377. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16378. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16379. (setq with-hm t))
  16380. (setq time0 (org-parse-time-string ts))
  16381. (when (and updown
  16382. (eq org-ts-what 'minute)
  16383. (not current-prefix-arg))
  16384. ;; This looks like s-up and s-down. Change by one rounding step.
  16385. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16386. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16387. (setcar (cdr time0) (+ (nth 1 time0)
  16388. (if (> n 0) (- rem) (- dm rem))))))
  16389. (setq time
  16390. (encode-time (or (car time0) 0)
  16391. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16392. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16393. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16394. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16395. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16396. (nthcdr 6 time0)))
  16397. (when (and (member org-ts-what '(hour minute))
  16398. extra
  16399. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16400. (setq extra (org-modify-ts-extra
  16401. extra
  16402. (if (eq org-ts-what 'hour) 2 5)
  16403. n dm)))
  16404. (when (integerp org-ts-what)
  16405. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16406. (if (eq what 'calendar)
  16407. (let ((cal-date (org-get-date-from-calendar)))
  16408. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16409. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16410. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16411. (setcar time0 (or (car time0) 0))
  16412. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16413. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16414. (setq time (apply 'encode-time time0))))
  16415. ;; Insert the new time-stamp, and ensure point stays in the same
  16416. ;; category as before (i.e. not after the last position in that
  16417. ;; category).
  16418. (let ((pos (point)))
  16419. ;; Stay before inserted string. `save-excursion' is of no use.
  16420. (setq org-last-changed-timestamp
  16421. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16422. (goto-char pos))
  16423. (save-match-data
  16424. (looking-at org-ts-regexp3)
  16425. (goto-char (cond
  16426. ;; `day' category ends before `hour' if any, or at
  16427. ;; the end of the day name.
  16428. ((eq origin-cat 'day)
  16429. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16430. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16431. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16432. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16433. ;; `year' and `month' have both fixed size: point
  16434. ;; couldn't have moved into another part.
  16435. (t origin))))
  16436. ;; Update clock if on a CLOCK line.
  16437. (org-clock-update-time-maybe)
  16438. ;; Maybe adjust the closest clock in `org-clock-history'
  16439. (when org-clock-adjust-closest
  16440. (if (not (and (org-at-clock-log-p)
  16441. (< 1 (length (delq nil (mapcar 'marker-position
  16442. org-clock-history))))))
  16443. (message "No clock to adjust")
  16444. (cond ((save-excursion ; fix previous clock?
  16445. (re-search-backward org-ts-regexp0 nil t)
  16446. (org-looking-back (concat org-clock-string " \\[")))
  16447. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16448. ((save-excursion ; fix next clock?
  16449. (re-search-backward org-ts-regexp0 nil t)
  16450. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16451. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16452. (save-window-excursion
  16453. ;; Find closest clock to point, adjust the previous/next one in history
  16454. (let* ((p (save-excursion (org-back-to-heading t)))
  16455. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16456. (clfixnth
  16457. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16458. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16459. (if (not clfixpos)
  16460. (message "No clock to adjust")
  16461. (save-excursion
  16462. (org-goto-marker-or-bmk clfixpos)
  16463. (org-show-subtree)
  16464. (when (re-search-forward clrgx nil t)
  16465. (goto-char (match-beginning 1))
  16466. (let (org-clock-adjust-closest)
  16467. (org-timestamp-change n org-ts-what updown))
  16468. (message "Clock adjusted in %s for heading: %s"
  16469. (file-name-nondirectory (buffer-file-name))
  16470. (org-get-heading t t)))))))))
  16471. ;; Try to recenter the calendar window, if any.
  16472. (if (and org-calendar-follow-timestamp-change
  16473. (get-buffer-window "*Calendar*" t)
  16474. (memq org-ts-what '(day month year)))
  16475. (org-recenter-calendar (time-to-days time))))))
  16476. (defun org-modify-ts-extra (s pos n dm)
  16477. "Change the different parts of the lead-time and repeat fields in timestamp."
  16478. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16479. ng h m new rem)
  16480. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16481. (cond
  16482. ((or (org-pos-in-match-range pos 2)
  16483. (org-pos-in-match-range pos 3))
  16484. (setq m (string-to-number (match-string 3 s))
  16485. h (string-to-number (match-string 2 s)))
  16486. (if (org-pos-in-match-range pos 2)
  16487. (setq h (+ h n))
  16488. (setq n (* dm (org-no-warnings (signum n))))
  16489. (when (not (= 0 (setq rem (% m dm))))
  16490. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16491. (setq m (+ m n)))
  16492. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16493. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16494. (setq h (min 24 (max 0 h)))
  16495. (setq ng 1 new (format "-%02d:%02d" h m)))
  16496. ((org-pos-in-match-range pos 6)
  16497. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16498. ((org-pos-in-match-range pos 5)
  16499. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16500. ((org-pos-in-match-range pos 9)
  16501. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16502. ((org-pos-in-match-range pos 8)
  16503. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16504. (when ng
  16505. (setq s (concat
  16506. (substring s 0 (match-beginning ng))
  16507. new
  16508. (substring s (match-end ng))))))
  16509. s))
  16510. (defun org-recenter-calendar (date)
  16511. "If the calendar is visible, recenter it to DATE."
  16512. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16513. (when cwin
  16514. (let ((calendar-move-hook nil))
  16515. (with-selected-window cwin
  16516. (calendar-goto-date (if (listp date) date
  16517. (calendar-gregorian-from-absolute date))))))))
  16518. (defun org-goto-calendar (&optional arg)
  16519. "Go to the Emacs calendar at the current date.
  16520. If there is a time stamp in the current line, go to that date.
  16521. A prefix ARG can be used to force the current date."
  16522. (interactive "P")
  16523. (let ((tsr org-ts-regexp) diff
  16524. (calendar-move-hook nil)
  16525. (calendar-view-holidays-initially-flag nil)
  16526. (calendar-view-diary-initially-flag nil))
  16527. (if (or (org-at-timestamp-p)
  16528. (save-excursion
  16529. (beginning-of-line 1)
  16530. (looking-at (concat ".*" tsr))))
  16531. (let ((d1 (time-to-days (current-time)))
  16532. (d2 (time-to-days
  16533. (org-time-string-to-time (match-string 1)))))
  16534. (setq diff (- d2 d1))))
  16535. (calendar)
  16536. (calendar-goto-today)
  16537. (if (and diff (not arg)) (calendar-forward-day diff))))
  16538. (defun org-get-date-from-calendar ()
  16539. "Return a list (month day year) of date at point in calendar."
  16540. (with-current-buffer "*Calendar*"
  16541. (save-match-data
  16542. (calendar-cursor-to-date))))
  16543. (defun org-date-from-calendar ()
  16544. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16545. If there is already a time stamp at the cursor position, update it."
  16546. (interactive)
  16547. (if (org-at-timestamp-p t)
  16548. (org-timestamp-change 0 'calendar)
  16549. (let ((cal-date (org-get-date-from-calendar)))
  16550. (org-insert-time-stamp
  16551. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16552. (defcustom org-effort-durations
  16553. `(("min" . 1)
  16554. ("h" . 60)
  16555. ("d" . ,(* 60 8))
  16556. ("w" . ,(* 60 8 5))
  16557. ("m" . ,(* 60 8 5 4))
  16558. ("y" . ,(* 60 8 5 40)))
  16559. "Conversion factor to minutes for an effort modifier.
  16560. Each entry has the form (MODIFIER . MINUTES).
  16561. In an effort string, a number followed by MODIFIER is multiplied
  16562. by the specified number of MINUTES to obtain an effort in
  16563. minutes.
  16564. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16565. effort string \"2hours\" is equivalent to 120 minutes."
  16566. :group 'org-agenda
  16567. :version "25.1"
  16568. :package-version '(Org . "8.3")
  16569. :type '(alist :key-type (string :tag "Modifier")
  16570. :value-type (number :tag "Minutes")))
  16571. (defun org-minutes-to-clocksum-string (m)
  16572. "Format number of minutes as a clocksum string.
  16573. The format is determined by `org-time-clocksum-format',
  16574. `org-time-clocksum-use-fractional' and
  16575. `org-time-clocksum-fractional-format' and
  16576. `org-time-clocksum-use-effort-durations'."
  16577. (let ((clocksum "")
  16578. (m (round m)) ; Don't allow fractions of minutes
  16579. h d w mo y fmt n)
  16580. (setq h (if org-time-clocksum-use-effort-durations
  16581. (cdr (assoc "h" org-effort-durations)) 60)
  16582. d (if org-time-clocksum-use-effort-durations
  16583. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16584. w (if org-time-clocksum-use-effort-durations
  16585. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16586. mo (if org-time-clocksum-use-effort-durations
  16587. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16588. y (if org-time-clocksum-use-effort-durations
  16589. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16590. ;; fractional format
  16591. (if org-time-clocksum-use-fractional
  16592. (cond
  16593. ;; single format string
  16594. ((stringp org-time-clocksum-fractional-format)
  16595. (format org-time-clocksum-fractional-format (/ m (float h))))
  16596. ;; choice of fractional formats for different time units
  16597. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16598. (> (/ (truncate m) (* y d h)) 0))
  16599. (format fmt (/ m (* y d (float h)))))
  16600. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16601. (> (/ (truncate m) (* mo d h)) 0))
  16602. (format fmt (/ m (* mo d (float h)))))
  16603. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16604. (> (/ (truncate m) (* w d h)) 0))
  16605. (format fmt (/ m (* w d (float h)))))
  16606. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16607. (> (/ (truncate m) (* d h)) 0))
  16608. (format fmt (/ m (* d (float h)))))
  16609. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16610. (> (/ (truncate m) h) 0))
  16611. (format fmt (/ m (float h))))
  16612. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16613. (format fmt m))
  16614. ;; fall back to smallest time unit with a format
  16615. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16616. (format fmt (/ m (float h))))
  16617. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16618. (format fmt (/ m (* d (float h)))))
  16619. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16620. (format fmt (/ m (* w d (float h)))))
  16621. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16622. (format fmt (/ m (* mo d (float h)))))
  16623. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16624. (format fmt (/ m (* y d (float h))))))
  16625. ;; standard (non-fractional) format, with single format string
  16626. (if (stringp org-time-clocksum-format)
  16627. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16628. ;; separate formats components
  16629. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16630. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16631. (plist-get org-time-clocksum-format :require-years))
  16632. (setq clocksum (concat clocksum (format fmt n))
  16633. m (- m (* n y d h))))
  16634. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16635. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16636. (plist-get org-time-clocksum-format :require-months))
  16637. (setq clocksum (concat clocksum (format fmt n))
  16638. m (- m (* n mo d h))))
  16639. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16640. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16641. (plist-get org-time-clocksum-format :require-weeks))
  16642. (setq clocksum (concat clocksum (format fmt n))
  16643. m (- m (* n w d h))))
  16644. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16645. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16646. (plist-get org-time-clocksum-format :require-days))
  16647. (setq clocksum (concat clocksum (format fmt n))
  16648. m (- m (* n d h))))
  16649. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16650. (or (> (setq n (/ (truncate m) h)) 0)
  16651. (plist-get org-time-clocksum-format :require-hours))
  16652. (setq clocksum (concat clocksum (format fmt n))
  16653. m (- m (* n h))))
  16654. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16655. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16656. (setq clocksum (concat clocksum (format fmt m))))
  16657. ;; return formatted time duration
  16658. clocksum))))
  16659. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16660. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16661. "Org mode version 8.0")
  16662. (defun org-hours-to-clocksum-string (n)
  16663. (org-minutes-to-clocksum-string (* n 60)))
  16664. (defun org-hh:mm-string-to-minutes (s)
  16665. "Convert a string H:MM to a number of minutes.
  16666. If the string is just a number, interpret it as minutes.
  16667. In fact, the first hh:mm or number in the string will be taken,
  16668. there can be extra stuff in the string.
  16669. If no number is found, the return value is 0."
  16670. (cond
  16671. ((integerp s) s)
  16672. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16673. (+ (* (string-to-number (match-string 1 s)) 60)
  16674. (string-to-number (match-string 2 s))))
  16675. ((string-match "\\([0-9]+\\)" s)
  16676. (string-to-number (match-string 1 s)))
  16677. (t 0)))
  16678. (defcustom org-image-actual-width t
  16679. "Should we use the actual width of images when inlining them?
  16680. When set to `t', always use the image width.
  16681. When set to a number, use imagemagick (when available) to set
  16682. the image's width to this value.
  16683. When set to a number in a list, try to get the width from any
  16684. #+ATTR.* keyword if it matches a width specification like
  16685. #+ATTR_HTML: :width 300px
  16686. and fall back on that number if none is found.
  16687. When set to nil, try to get the width from an #+ATTR.* keyword
  16688. and fall back on the original width if none is found.
  16689. This requires Emacs >= 24.1, build with imagemagick support."
  16690. :group 'org-appearance
  16691. :version "24.4"
  16692. :package-version '(Org . "8.0")
  16693. :type '(choice
  16694. (const :tag "Use the image width" t)
  16695. (integer :tag "Use a number of pixels")
  16696. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16697. (const :tag "Use #+ATTR* or don't resize" nil)))
  16698. (defcustom org-agenda-inhibit-startup nil
  16699. "Inhibit startup when preparing agenda buffers.
  16700. When this variable is `t', the initialization of the Org agenda
  16701. buffers is inhibited: e.g. the visibility state is not set, the
  16702. tables are not re-aligned, etc."
  16703. :type 'boolean
  16704. :version "24.3"
  16705. :group 'org-agenda)
  16706. (define-obsolete-variable-alias
  16707. 'org-agenda-ignore-drawer-properties
  16708. 'org-agenda-ignore-properties "25.1")
  16709. (defcustom org-agenda-ignore-properties nil
  16710. "Avoid updating text properties when building the agenda.
  16711. Properties are used to prepare buffers for effort estimates,
  16712. appointments, statistics and subtree-local categories.
  16713. If you don't use these in the agenda, you can add them to this
  16714. list and agenda building will be a bit faster.
  16715. The value is a list, with zero or more of the symbols `effort', `appt',
  16716. `stats' or `category'."
  16717. :type '(set :greedy t
  16718. (const effort)
  16719. (const appt)
  16720. (const stats)
  16721. (const category))
  16722. :version "25.1"
  16723. :package-version '(Org . "8.3")
  16724. :group 'org-agenda)
  16725. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16726. "Convert a duration string S to minutes.
  16727. A bare number is interpreted as minutes, modifiers can be set by
  16728. customizing `org-effort-durations' (which see).
  16729. Entries containing a colon are interpreted as H:MM by
  16730. `org-hh:mm-string-to-minutes'."
  16731. (let ((result 0)
  16732. (re (concat "\\([0-9.]+\\) *\\("
  16733. (regexp-opt (mapcar 'car org-effort-durations))
  16734. "\\)")))
  16735. (while (string-match re s)
  16736. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16737. (string-to-number (match-string 1 s))))
  16738. (setq s (replace-match "" nil t s)))
  16739. (setq result (floor result))
  16740. (incf result (org-hh:mm-string-to-minutes s))
  16741. (if output-to-string (number-to-string result) result)))
  16742. ;;;; Files
  16743. (defun org-save-all-org-buffers ()
  16744. "Save all Org-mode buffers without user confirmation."
  16745. (interactive)
  16746. (message "Saving all Org-mode buffers...")
  16747. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16748. (when (featurep 'org-id) (org-id-locations-save))
  16749. (message "Saving all Org-mode buffers... done"))
  16750. (defun org-revert-all-org-buffers ()
  16751. "Revert all Org-mode buffers.
  16752. Prompt for confirmation when there are unsaved changes.
  16753. Be sure you know what you are doing before letting this function
  16754. overwrite your changes.
  16755. This function is useful in a setup where one tracks org files
  16756. with a version control system, to revert on one machine after pulling
  16757. changes from another. I believe the procedure must be like this:
  16758. 1. M-x org-save-all-org-buffers
  16759. 2. Pull changes from the other machine, resolve conflicts
  16760. 3. M-x org-revert-all-org-buffers"
  16761. (interactive)
  16762. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16763. (user-error "Abort"))
  16764. (save-excursion
  16765. (save-window-excursion
  16766. (mapc
  16767. (lambda (b)
  16768. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16769. (with-current-buffer b buffer-file-name))
  16770. (org-pop-to-buffer-same-window b)
  16771. (revert-buffer t 'no-confirm)))
  16772. (buffer-list))
  16773. (when (and (featurep 'org-id) org-id-track-globally)
  16774. (org-id-locations-load)))))
  16775. ;;;; Agenda files
  16776. ;;;###autoload
  16777. (defun org-switchb (&optional arg)
  16778. "Switch between Org buffers.
  16779. With one prefix argument, restrict available buffers to files.
  16780. With two prefix arguments, restrict available buffers to agenda files.
  16781. Defaults to `iswitchb' for buffer name completion.
  16782. Set `org-completion-use-ido' to make it use ido instead."
  16783. (interactive "P")
  16784. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16785. ((equal arg '(16)) (org-buffer-list 'agenda))
  16786. (t (org-buffer-list))))
  16787. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16788. (org-completion-use-ido org-completion-use-ido))
  16789. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16790. (setq org-completion-use-iswitchb t))
  16791. (org-pop-to-buffer-same-window
  16792. (org-icompleting-read "Org buffer: "
  16793. (mapcar 'list (mapcar 'buffer-name blist))
  16794. nil t))))
  16795. ;;; Define some older names previously used for this functionality
  16796. ;;;###autoload
  16797. (defalias 'org-ido-switchb 'org-switchb)
  16798. ;;;###autoload
  16799. (defalias 'org-iswitchb 'org-switchb)
  16800. (defun org-buffer-list (&optional predicate exclude-tmp)
  16801. "Return a list of Org buffers.
  16802. PREDICATE can be `export', `files' or `agenda'.
  16803. export restrict the list to Export buffers.
  16804. files restrict the list to buffers visiting Org files.
  16805. agenda restrict the list to buffers visiting agenda files.
  16806. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16807. (let* ((bfn nil)
  16808. (agenda-files (and (eq predicate 'agenda)
  16809. (mapcar 'file-truename (org-agenda-files t))))
  16810. (filter
  16811. (cond
  16812. ((eq predicate 'files)
  16813. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16814. ((eq predicate 'export)
  16815. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16816. ((eq predicate 'agenda)
  16817. (lambda (b)
  16818. (with-current-buffer b
  16819. (and (derived-mode-p 'org-mode)
  16820. (setq bfn (buffer-file-name b))
  16821. (member (file-truename bfn) agenda-files)))))
  16822. (t (lambda (b) (with-current-buffer b
  16823. (or (derived-mode-p 'org-mode)
  16824. (string-match "\*Org .*Export"
  16825. (buffer-name b)))))))))
  16826. (delq nil
  16827. (mapcar
  16828. (lambda(b)
  16829. (if (and (funcall filter b)
  16830. (or (not exclude-tmp)
  16831. (not (string-match "tmp" (buffer-name b)))))
  16832. b
  16833. nil))
  16834. (buffer-list)))))
  16835. (defun org-agenda-files (&optional unrestricted archives)
  16836. "Get the list of agenda files.
  16837. Optional UNRESTRICTED means return the full list even if a restriction
  16838. is currently in place.
  16839. When ARCHIVES is t, include all archive files that are really being
  16840. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16841. `org-agenda-archives-mode' is t."
  16842. (let ((files
  16843. (cond
  16844. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16845. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16846. ((listp org-agenda-files) org-agenda-files)
  16847. (t (error "Invalid value of `org-agenda-files'")))))
  16848. (setq files (apply 'append
  16849. (mapcar (lambda (f)
  16850. (if (file-directory-p f)
  16851. (directory-files
  16852. f t org-agenda-file-regexp)
  16853. (list f)))
  16854. files)))
  16855. (when org-agenda-skip-unavailable-files
  16856. (setq files (delq nil
  16857. (mapcar (function
  16858. (lambda (file)
  16859. (and (file-readable-p file) file)))
  16860. files))))
  16861. (when (or (eq archives t)
  16862. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16863. (setq files (org-add-archive-files files)))
  16864. files))
  16865. (defun org-agenda-file-p (&optional file)
  16866. "Return non-nil, if FILE is an agenda file.
  16867. If FILE is omitted, use the file associated with the current
  16868. buffer."
  16869. (let ((fname (or file (buffer-file-name))))
  16870. (and fname
  16871. (member (file-truename fname)
  16872. (mapcar #'file-truename (org-agenda-files t))))))
  16873. (defun org-edit-agenda-file-list ()
  16874. "Edit the list of agenda files.
  16875. Depending on setup, this either uses customize to edit the variable
  16876. `org-agenda-files', or it visits the file that is holding the list. In the
  16877. latter case, the buffer is set up in a way that saving it automatically kills
  16878. the buffer and restores the previous window configuration."
  16879. (interactive)
  16880. (if (stringp org-agenda-files)
  16881. (let ((cw (current-window-configuration)))
  16882. (find-file org-agenda-files)
  16883. (org-set-local 'org-window-configuration cw)
  16884. (org-add-hook 'after-save-hook
  16885. (lambda ()
  16886. (set-window-configuration
  16887. (prog1 org-window-configuration
  16888. (kill-buffer (current-buffer))))
  16889. (org-install-agenda-files-menu)
  16890. (message "New agenda file list installed"))
  16891. nil 'local)
  16892. (message "%s" (substitute-command-keys
  16893. "Edit list and finish with \\[save-buffer]")))
  16894. (customize-variable 'org-agenda-files)))
  16895. (defun org-store-new-agenda-file-list (list)
  16896. "Set new value for the agenda file list and save it correctly."
  16897. (if (stringp org-agenda-files)
  16898. (let ((fe (org-read-agenda-file-list t)) b u)
  16899. (while (setq b (find-buffer-visiting org-agenda-files))
  16900. (kill-buffer b))
  16901. (with-temp-file org-agenda-files
  16902. (insert
  16903. (mapconcat
  16904. (lambda (f) ;; Keep un-expanded entries.
  16905. (if (setq u (assoc f fe))
  16906. (cdr u)
  16907. f))
  16908. list "\n")
  16909. "\n")))
  16910. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16911. (org-insert-mode-line-in-empty-file nil))
  16912. (setq org-agenda-files list)
  16913. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16914. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16915. "Read the list of agenda files from a file.
  16916. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16917. filenames, used by `org-store-new-agenda-file-list' to write back
  16918. un-expanded file names."
  16919. (when (file-directory-p org-agenda-files)
  16920. (error "`org-agenda-files' cannot be a single directory"))
  16921. (when (stringp org-agenda-files)
  16922. (with-temp-buffer
  16923. (insert-file-contents org-agenda-files)
  16924. (mapcar
  16925. (lambda (f)
  16926. (let ((e (expand-file-name (substitute-in-file-name f)
  16927. org-directory)))
  16928. (if pair-with-expansion
  16929. (cons e f)
  16930. e)))
  16931. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16932. ;;;###autoload
  16933. (defun org-cycle-agenda-files ()
  16934. "Cycle through the files in `org-agenda-files'.
  16935. If the current buffer visits an agenda file, find the next one in the list.
  16936. If the current buffer does not, find the first agenda file."
  16937. (interactive)
  16938. (let* ((fs (org-agenda-files t))
  16939. (files (append fs (list (car fs))))
  16940. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16941. file)
  16942. (unless files (user-error "No agenda files"))
  16943. (catch 'exit
  16944. (while (setq file (pop files))
  16945. (if (equal (file-truename file) tcf)
  16946. (when (car files)
  16947. (find-file (car files))
  16948. (throw 'exit t))))
  16949. (find-file (car fs)))
  16950. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16951. (defun org-agenda-file-to-front (&optional to-end)
  16952. "Move/add the current file to the top of the agenda file list.
  16953. If the file is not present in the list, it is added to the front. If it is
  16954. present, it is moved there. With optional argument TO-END, add/move to the
  16955. end of the list."
  16956. (interactive "P")
  16957. (let ((org-agenda-skip-unavailable-files nil)
  16958. (file-alist (mapcar (lambda (x)
  16959. (cons (file-truename x) x))
  16960. (org-agenda-files t)))
  16961. (ctf (file-truename
  16962. (or buffer-file-name
  16963. (user-error "Please save the current buffer to a file"))))
  16964. x had)
  16965. (setq x (assoc ctf file-alist) had x)
  16966. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16967. (if to-end
  16968. (setq file-alist (append (delq x file-alist) (list x)))
  16969. (setq file-alist (cons x (delq x file-alist))))
  16970. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16971. (org-install-agenda-files-menu)
  16972. (message "File %s to %s of agenda file list"
  16973. (if had "moved" "added") (if to-end "end" "front"))))
  16974. (defun org-remove-file (&optional file)
  16975. "Remove current file from the list of files in variable `org-agenda-files'.
  16976. These are the files which are being checked for agenda entries.
  16977. Optional argument FILE means use this file instead of the current."
  16978. (interactive)
  16979. (let* ((org-agenda-skip-unavailable-files nil)
  16980. (file (or file buffer-file-name
  16981. (user-error "Current buffer does not visit a file")))
  16982. (true-file (file-truename file))
  16983. (afile (abbreviate-file-name file))
  16984. (files (delq nil (mapcar
  16985. (lambda (x)
  16986. (if (equal true-file
  16987. (file-truename x))
  16988. nil x))
  16989. (org-agenda-files t)))))
  16990. (if (not (= (length files) (length (org-agenda-files t))))
  16991. (progn
  16992. (org-store-new-agenda-file-list files)
  16993. (org-install-agenda-files-menu)
  16994. (message "Removed from Org Agenda list: %s" afile))
  16995. (message "File was not in list: %s (not removed)" afile))))
  16996. (defun org-file-menu-entry (file)
  16997. (vector file (list 'find-file file) t))
  16998. (defun org-check-agenda-file (file)
  16999. "Make sure FILE exists. If not, ask user what to do."
  17000. (when (not (file-exists-p file))
  17001. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17002. (abbreviate-file-name file))
  17003. (let ((r (downcase (read-char-exclusive))))
  17004. (cond
  17005. ((equal r ?r)
  17006. (org-remove-file file)
  17007. (throw 'nextfile t))
  17008. (t (user-error "Abort"))))))
  17009. (defun org-get-agenda-file-buffer (file)
  17010. "Get an agenda buffer visiting FILE.
  17011. If the buffer needs to be created, add it to the list of buffers
  17012. which might be released later."
  17013. (let ((buf (org-find-base-buffer-visiting file)))
  17014. (if buf
  17015. buf ; just return it
  17016. ;; Make a new buffer and remember it
  17017. (setq buf (find-file-noselect file))
  17018. (if buf (push buf org-agenda-new-buffers))
  17019. buf)))
  17020. (defun org-release-buffers (blist)
  17021. "Release all buffers in list, asking the user for confirmation when needed.
  17022. When a buffer is unmodified, it is just killed. When modified, it is saved
  17023. \(if the user agrees) and then killed."
  17024. (let (buf file)
  17025. (while (setq buf (pop blist))
  17026. (setq file (buffer-file-name buf))
  17027. (when (and (buffer-modified-p buf)
  17028. file
  17029. (y-or-n-p (format "Save file %s? " file)))
  17030. (with-current-buffer buf (save-buffer)))
  17031. (kill-buffer buf))))
  17032. (defun org-agenda-prepare-buffers (files)
  17033. "Create buffers for all agenda files, protect archived trees and comments."
  17034. (interactive)
  17035. (let ((pa '(:org-archived t))
  17036. (pc '(:org-comment t))
  17037. (pall '(:org-archived t :org-comment t))
  17038. (inhibit-read-only t)
  17039. (org-inhibit-startup org-agenda-inhibit-startup)
  17040. (rea (concat ":" org-archive-tag ":"))
  17041. file re pos)
  17042. (setq org-tag-alist-for-agenda nil
  17043. org-tag-groups-alist-for-agenda nil)
  17044. (save-excursion
  17045. (save-restriction
  17046. (while (setq file (pop files))
  17047. (catch 'nextfile
  17048. (if (bufferp file)
  17049. (set-buffer file)
  17050. (org-check-agenda-file file)
  17051. (set-buffer (org-get-agenda-file-buffer file)))
  17052. (widen)
  17053. (org-set-regexps-and-options 'tags-only)
  17054. (setq pos (point))
  17055. (or (memq 'category org-agenda-ignore-properties)
  17056. (org-refresh-category-properties))
  17057. (or (memq 'stats org-agenda-ignore-properties)
  17058. (org-refresh-stats-properties))
  17059. (or (memq 'effort org-agenda-ignore-properties)
  17060. (org-refresh-effort-properties))
  17061. (or (memq 'appt org-agenda-ignore-properties)
  17062. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17063. (setq org-todo-keywords-for-agenda
  17064. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17065. (setq org-done-keywords-for-agenda
  17066. (append org-done-keywords-for-agenda org-done-keywords))
  17067. (setq org-todo-keyword-alist-for-agenda
  17068. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17069. (setq org-tag-alist-for-agenda
  17070. (org-uniquify
  17071. (append org-tag-alist-for-agenda
  17072. org-tag-alist
  17073. org-tag-persistent-alist)))
  17074. (if org-group-tags
  17075. (setq org-tag-groups-alist-for-agenda
  17076. (org-uniquify-alist
  17077. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17078. (org-with-silent-modifications
  17079. (save-excursion
  17080. (remove-text-properties (point-min) (point-max) pall)
  17081. (when org-agenda-skip-archived-trees
  17082. (goto-char (point-min))
  17083. (while (re-search-forward rea nil t)
  17084. (if (org-at-heading-p t)
  17085. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17086. (goto-char (point-min))
  17087. (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
  17088. (while (re-search-forward re nil t)
  17089. (when (save-match-data (org-in-commented-heading-p t))
  17090. (add-text-properties
  17091. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17092. (goto-char pos)))))
  17093. (setq org-todo-keywords-for-agenda
  17094. (org-uniquify org-todo-keywords-for-agenda))
  17095. (setq org-todo-keyword-alist-for-agenda
  17096. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17097. ;;;; CDLaTeX minor mode
  17098. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17099. "Keymap for the minor `org-cdlatex-mode'.")
  17100. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17101. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17102. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17103. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17104. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17105. (defvar org-cdlatex-texmathp-advice-is-done nil
  17106. "Flag remembering if we have applied the advice to texmathp already.")
  17107. (define-minor-mode org-cdlatex-mode
  17108. "Toggle the minor `org-cdlatex-mode'.
  17109. This mode supports entering LaTeX environment and math in LaTeX fragments
  17110. in Org-mode.
  17111. \\{org-cdlatex-mode-map}"
  17112. nil " OCDL" nil
  17113. (when org-cdlatex-mode
  17114. (require 'cdlatex)
  17115. (run-hooks 'cdlatex-mode-hook)
  17116. (cdlatex-compute-tables))
  17117. (unless org-cdlatex-texmathp-advice-is-done
  17118. (setq org-cdlatex-texmathp-advice-is-done t)
  17119. (defadvice texmathp (around org-math-always-on activate)
  17120. "Always return t in org-mode buffers.
  17121. This is because we want to insert math symbols without dollars even outside
  17122. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17123. an embedded LaTeX fragment, let texmathp do its job.
  17124. \\[org-cdlatex-mode-map]"
  17125. (interactive)
  17126. (let (p)
  17127. (cond
  17128. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17129. ((eq this-command 'cdlatex-math-symbol)
  17130. (setq ad-return-value t
  17131. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17132. (t
  17133. (let ((p (org-inside-LaTeX-fragment-p)))
  17134. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17135. (setq ad-return-value t
  17136. texmathp-why '("Org-mode embedded math" . 0))
  17137. (if p ad-do-it)))))))))
  17138. (defun turn-on-org-cdlatex ()
  17139. "Unconditionally turn on `org-cdlatex-mode'."
  17140. (org-cdlatex-mode 1))
  17141. (defun org-try-cdlatex-tab ()
  17142. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17143. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17144. - inside a LaTeX fragment, or
  17145. - after the first word in a line, where an abbreviation expansion could
  17146. insert a LaTeX environment."
  17147. (when org-cdlatex-mode
  17148. (cond
  17149. ;; Before any word on the line: No expansion possible.
  17150. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17151. ;; Just after first word on the line: Expand it. Make sure it
  17152. ;; cannot happen on headlines, though.
  17153. ((save-excursion
  17154. (skip-chars-backward "a-zA-Z0-9*")
  17155. (skip-chars-backward " \t")
  17156. (and (bolp) (not (org-at-heading-p))))
  17157. (cdlatex-tab) t)
  17158. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17159. (defun org-cdlatex-underscore-caret (&optional arg)
  17160. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17161. Revert to the normal definition outside of these fragments."
  17162. (interactive "P")
  17163. (if (org-inside-LaTeX-fragment-p)
  17164. (call-interactively 'cdlatex-sub-superscript)
  17165. (let (org-cdlatex-mode)
  17166. (call-interactively (key-binding (vector last-input-event))))))
  17167. (defun org-cdlatex-math-modify (&optional arg)
  17168. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17169. Revert to the normal definition outside of these fragments."
  17170. (interactive "P")
  17171. (if (org-inside-LaTeX-fragment-p)
  17172. (call-interactively 'cdlatex-math-modify)
  17173. (let (org-cdlatex-mode)
  17174. (call-interactively (key-binding (vector last-input-event))))))
  17175. (defun org-cdlatex-environment-indent (&optional environment item)
  17176. "Execute `cdlatex-environment' and indent the inserted environment.
  17177. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17178. The inserted environment is indented to current indentation
  17179. unless point is at the beginning of the line, in which the
  17180. environment remains unintended."
  17181. (interactive)
  17182. ;; cdlatex-environment always return nil. Therefore, capture output
  17183. ;; first and determine if an environment was selected.
  17184. (let* ((beg (point-marker))
  17185. (end (copy-marker (point) t))
  17186. (inserted (progn
  17187. (ignore-errors (cdlatex-environment environment item))
  17188. (< beg end)))
  17189. ;; Figure out how many lines to move forward after the
  17190. ;; environment has been inserted.
  17191. (lines (when inserted
  17192. (save-excursion
  17193. (- (loop while (< beg (point))
  17194. with x = 0
  17195. do (forward-line -1)
  17196. (incf x)
  17197. finally return x)
  17198. (if (progn (goto-char beg)
  17199. (and (progn (skip-chars-forward " \t") (eolp))
  17200. (progn (skip-chars-backward " \t") (bolp))))
  17201. 1 0)))))
  17202. (env (org-trim (delete-and-extract-region beg end))))
  17203. (when inserted
  17204. ;; Get indentation of next line unless at column 0.
  17205. (let ((ind (if (bolp) 0
  17206. (save-excursion
  17207. (org-return-indent)
  17208. (prog1 (org-get-indentation)
  17209. (when (progn (skip-chars-forward " \t") (eolp))
  17210. (delete-region beg (point)))))))
  17211. (bol (progn (skip-chars-backward " \t") (bolp))))
  17212. ;; Insert a newline before environment unless at column zero
  17213. ;; to "escape" the current line. Insert a newline if
  17214. ;; something is one the same line as \end{ENVIRONMENT}.
  17215. (insert
  17216. (concat (unless bol "\n") env
  17217. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17218. (unless (zerop ind)
  17219. (save-excursion
  17220. (goto-char beg)
  17221. (while (< (point) end)
  17222. (unless (eolp) (org-indent-line-to ind))
  17223. (forward-line))))
  17224. (goto-char beg)
  17225. (forward-line lines)
  17226. (org-indent-line-to ind)))
  17227. (set-marker beg nil)
  17228. (set-marker end nil)))
  17229. ;;;; LaTeX fragments
  17230. (defun org-inside-LaTeX-fragment-p ()
  17231. "Test if point is inside a LaTeX fragment.
  17232. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17233. sequence appearing also before point.
  17234. Even though the matchers for math are configurable, this function assumes
  17235. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17236. delimiters are skipped when they have been removed by customization.
  17237. The return value is nil, or a cons cell with the delimiter and the
  17238. position of this delimiter.
  17239. This function does a reasonably good job, but can locally be fooled by
  17240. for example currency specifications. For example it will assume being in
  17241. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17242. fragments that are properly closed, but during editing, we have to live
  17243. with the uncertainty caused by missing closing delimiters. This function
  17244. looks only before point, not after."
  17245. (catch 'exit
  17246. (let ((pos (point))
  17247. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17248. (lim (progn
  17249. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17250. (point)))
  17251. dd-on str (start 0) m re)
  17252. (goto-char pos)
  17253. (when dodollar
  17254. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17255. re (nth 1 (assoc "$" org-latex-regexps)))
  17256. (while (string-match re str start)
  17257. (cond
  17258. ((= (match-end 0) (length str))
  17259. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17260. ((= (match-end 0) (- (length str) 5))
  17261. (throw 'exit nil))
  17262. (t (setq start (match-end 0))))))
  17263. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17264. (goto-char pos)
  17265. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17266. (and (match-beginning 2) (throw 'exit nil))
  17267. ;; count $$
  17268. (while (re-search-backward "\\$\\$" lim t)
  17269. (setq dd-on (not dd-on)))
  17270. (goto-char pos)
  17271. (if dd-on (cons "$$" m))))))
  17272. (defun org-inside-latex-macro-p ()
  17273. "Is point inside a LaTeX macro or its arguments?"
  17274. (save-match-data
  17275. (org-in-regexp
  17276. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17277. (defvar org-latex-fragment-image-overlays nil
  17278. "List of overlays carrying the images of latex fragments.")
  17279. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17280. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17281. "Remove all overlays with LaTeX fragment images in current buffer.
  17282. When optional arguments BEG and END are non-nil, remove all
  17283. overlays between them instead. Return t when some overlays were
  17284. removed, nil otherwise."
  17285. (let (removedp)
  17286. (setq org-latex-fragment-image-overlays
  17287. (let ((beg (or beg (point-min)))
  17288. (end (or end (point-max))))
  17289. (org-remove-if
  17290. (lambda (o)
  17291. (and (>= (overlay-start o) beg)
  17292. (<= (overlay-end o) end)
  17293. (progn (delete-overlay o)
  17294. (or removedp (setq removedp t)))))
  17295. org-latex-fragment-image-overlays)))
  17296. removedp))
  17297. (define-obsolete-function-alias
  17298. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17299. (defun org-toggle-latex-fragment (&optional arg)
  17300. "Preview the LaTeX fragment at point, or all locally or globally.
  17301. If the cursor is on a LaTeX fragment, create the image and overlay
  17302. it over the source code, if there is none. Remove it otherwise.
  17303. If there is no fragment at point, display all fragments in the
  17304. current section.
  17305. With prefix ARG, preview or clear image for all fragments in the
  17306. current subtree or in the whole buffer when used before the first
  17307. headline. With a double prefix ARG \\[universal-argument] \
  17308. \\[universal-argument] preview or clear images
  17309. for all fragments in the buffer."
  17310. (interactive "P")
  17311. (unless (buffer-file-name (buffer-base-buffer))
  17312. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17313. (when (display-graphic-p)
  17314. (catch 'exit
  17315. (save-excursion
  17316. (let ((window-start (window-start)) msg)
  17317. (save-restriction
  17318. (cond
  17319. ((or (equal arg '(16))
  17320. (and (equal arg '(4))
  17321. (org-with-limited-levels (org-before-first-heading-p))))
  17322. (if (org-remove-latex-fragment-image-overlays)
  17323. (progn (message "LaTeX fragments images removed from buffer")
  17324. (throw 'exit nil))
  17325. (setq msg "Creating images for buffer...")))
  17326. ((equal arg '(4))
  17327. (org-with-limited-levels (org-back-to-heading t))
  17328. (let ((beg (point))
  17329. (end (progn (org-end-of-subtree t) (point))))
  17330. (if (org-remove-latex-fragment-image-overlays beg end)
  17331. (progn
  17332. (message "LaTeX fragment images removed from subtree")
  17333. (throw 'exit nil))
  17334. (setq msg "Creating images for subtree...")
  17335. (narrow-to-region beg end))))
  17336. ((let ((datum (org-element-context)))
  17337. (when (memq (org-element-type datum)
  17338. '(latex-environment latex-fragment))
  17339. (let* ((beg (org-element-property :begin datum))
  17340. (end (org-element-property :end datum)))
  17341. (if (org-remove-latex-fragment-image-overlays beg end)
  17342. (progn (message "LaTeX fragment image removed")
  17343. (throw 'exit nil))
  17344. (narrow-to-region beg end)
  17345. (setq msg "Creating image..."))))))
  17346. (t
  17347. (org-with-limited-levels
  17348. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17349. (outline-previous-heading)
  17350. (point)))
  17351. (end (progn (outline-next-heading) (point))))
  17352. (if (org-remove-latex-fragment-image-overlays beg end)
  17353. (progn
  17354. (message "LaTeX fragment images removed from section")
  17355. (throw 'exit nil))
  17356. (setq msg "Creating images for section...")
  17357. (narrow-to-region beg end))))))
  17358. (let ((file (buffer-file-name (buffer-base-buffer))))
  17359. (org-format-latex
  17360. (concat org-latex-preview-ltxpng-directory
  17361. (file-name-sans-extension (file-name-nondirectory file)))
  17362. ;; Emacs cannot overlay images from remote hosts.
  17363. ;; Create it in `temporary-file-directory' instead.
  17364. (if (file-remote-p file) temporary-file-directory
  17365. default-directory)
  17366. 'overlays msg 'forbuffer
  17367. org-latex-create-formula-image-program)))
  17368. ;; Work around a bug that doesn't restore window's start
  17369. ;; when widening back the buffer.
  17370. (set-window-start nil window-start)
  17371. (message (concat msg "done")))))))
  17372. (defun org-format-latex
  17373. (prefix &optional dir overlays msg forbuffer processing-type)
  17374. "Replace LaTeX fragments with links to an image, and produce images.
  17375. When optional argument OVERLAYS is non-nil, display the image on
  17376. top of the fragment instead of replacing it.
  17377. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17378. Some of the options can be changed using the variable
  17379. `org-format-latex-options', which see."
  17380. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17381. (unless (eq processing-type 'verbatim)
  17382. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17383. (cnt 0)
  17384. checkdir-flag)
  17385. (goto-char (point-min))
  17386. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17387. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17388. (overlay-recenter (point-max)))
  17389. (while (re-search-forward math-regexp nil t)
  17390. (unless (and overlays
  17391. (eq (get-char-property (point) 'org-overlay-type)
  17392. 'org-latex-overlay))
  17393. (let* ((context (org-element-context))
  17394. (type (org-element-type context)))
  17395. (when (memq type '(latex-environment latex-fragment))
  17396. (let ((block-type (eq type 'latex-environment))
  17397. (value (org-element-property :value context))
  17398. (beg (org-element-property :begin context))
  17399. (end (save-excursion
  17400. (goto-char (org-element-property :end context))
  17401. (skip-chars-backward " \r\t\n")
  17402. (point))))
  17403. (case processing-type
  17404. (mathjax
  17405. ;; Prepare for MathJax processing.
  17406. (if (eq (char-after beg) ?$)
  17407. (save-excursion
  17408. (delete-region beg end)
  17409. (insert "\\(" (substring value 1 -1) "\\)"))
  17410. (goto-char end)))
  17411. ((dvipng imagemagick)
  17412. ;; Process to an image.
  17413. (incf cnt)
  17414. (goto-char beg)
  17415. (let* ((face (face-at-point))
  17416. ;; Get the colors from the face at point.
  17417. (fg
  17418. (let ((color (plist-get org-format-latex-options
  17419. :foreground)))
  17420. (if (and forbuffer (eq color 'auto))
  17421. (face-attribute face :foreground nil 'default)
  17422. color)))
  17423. (bg
  17424. (let ((color (plist-get org-format-latex-options
  17425. :background)))
  17426. (if (and forbuffer (eq color 'auto))
  17427. (face-attribute face :background nil 'default)
  17428. color)))
  17429. (hash (sha1 (prin1-to-string
  17430. (list org-format-latex-header
  17431. org-latex-default-packages-alist
  17432. org-latex-packages-alist
  17433. org-format-latex-options
  17434. forbuffer value fg bg))))
  17435. (absprefix (expand-file-name prefix dir))
  17436. (linkfile (format "%s_%s.png" prefix hash))
  17437. (movefile (format "%s_%s.png" absprefix hash))
  17438. (sep (and block-type "\n\n"))
  17439. (link (concat sep "[[file:" linkfile "]]" sep))
  17440. (options
  17441. (org-combine-plists
  17442. org-format-latex-options
  17443. `(:foreground ,fg :background ,bg))))
  17444. (when msg (message msg cnt))
  17445. (unless checkdir-flag ; Ensure the directory exists.
  17446. (setq checkdir-flag t)
  17447. (let ((todir (file-name-directory absprefix)))
  17448. (unless (file-directory-p todir)
  17449. (make-directory todir t))))
  17450. (unless (file-exists-p movefile)
  17451. (org-create-formula-image
  17452. value movefile options forbuffer processing-type))
  17453. (if overlays
  17454. (progn
  17455. (dolist (o (overlays-in beg end))
  17456. (when (eq (overlay-get o 'org-overlay-type)
  17457. 'org-latex-overlay)
  17458. (delete-overlay o)))
  17459. (let ((ov (make-overlay beg end)))
  17460. (overlay-put ov
  17461. 'org-overlay-type
  17462. 'org-latex-overlay)
  17463. (overlay-put ov 'evaporate t)
  17464. (if (featurep 'xemacs)
  17465. (progn
  17466. (overlay-put ov 'invisible t)
  17467. (overlay-put
  17468. ov 'end-glyph
  17469. (make-glyph
  17470. (vector 'png :file movefile))))
  17471. (overlay-put
  17472. ov 'display
  17473. (list 'image
  17474. :type 'png
  17475. :file movefile
  17476. :ascent 'center)))
  17477. (push ov org-latex-fragment-image-overlays))
  17478. (goto-char end))
  17479. (delete-region beg end)
  17480. (insert
  17481. (org-add-props link
  17482. (list 'org-latex-src
  17483. (replace-regexp-in-string "\"" "" value)
  17484. 'org-latex-src-embed-type
  17485. (if block-type 'paragraph 'character)))))))
  17486. (mathml
  17487. ;; Process to MathML.
  17488. (unless (org-format-latex-mathml-available-p)
  17489. (user-error "LaTeX to MathML converter not configured"))
  17490. (incf cnt)
  17491. (when msg (message msg cnt))
  17492. (goto-char beg)
  17493. (delete-region beg end)
  17494. (insert (org-format-latex-as-mathml
  17495. value block-type prefix dir)))
  17496. (otherwise
  17497. (error "Unknown conversion type %s for LaTeX fragments"
  17498. processing-type)))))))))))
  17499. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17500. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17501. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17502. sucessful, return the portion between \"<math...> </math>\"
  17503. elements otherwise return nil. When MATHML-FILE is specified,
  17504. write the results in to that file. When invoked as an
  17505. interactive command, prompt for LATEX-FRAG, with initial value
  17506. set to the current active region and echo the results for user
  17507. inspection."
  17508. (interactive (list (let ((frag (when (org-region-active-p)
  17509. (buffer-substring-no-properties
  17510. (region-beginning) (region-end)))))
  17511. (read-string "LaTeX Fragment: " frag nil frag))))
  17512. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17513. (let* ((tmp-in-file (file-relative-name
  17514. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17515. (ignore (write-region latex-frag nil tmp-in-file))
  17516. (tmp-out-file (file-relative-name
  17517. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17518. (cmd (format-spec
  17519. org-latex-to-mathml-convert-command
  17520. `((?j . ,(and org-latex-to-mathml-jar-file
  17521. (shell-quote-argument
  17522. (expand-file-name
  17523. org-latex-to-mathml-jar-file))))
  17524. (?I . ,(shell-quote-argument tmp-in-file))
  17525. (?i . ,latex-frag)
  17526. (?o . ,(shell-quote-argument tmp-out-file)))))
  17527. mathml shell-command-output)
  17528. (when (org-called-interactively-p 'any)
  17529. (unless (org-format-latex-mathml-available-p)
  17530. (user-error "LaTeX to MathML converter not configured")))
  17531. (message "Running %s" cmd)
  17532. (setq shell-command-output (shell-command-to-string cmd))
  17533. (setq mathml
  17534. (when (file-readable-p tmp-out-file)
  17535. (with-current-buffer (find-file-noselect tmp-out-file t)
  17536. (goto-char (point-min))
  17537. (when (re-search-forward
  17538. (concat
  17539. (regexp-quote
  17540. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17541. "[^>]*?>"
  17542. "\\(.\\|\n\\)*"
  17543. "</math>")
  17544. nil t)
  17545. (prog1 (match-string 0) (kill-buffer))))))
  17546. (cond
  17547. (mathml
  17548. (setq mathml
  17549. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17550. (when mathml-file
  17551. (write-region mathml nil mathml-file))
  17552. (when (org-called-interactively-p 'any)
  17553. (message mathml)))
  17554. ((message "LaTeX to MathML conversion failed")
  17555. (message shell-command-output)))
  17556. (delete-file tmp-in-file)
  17557. (when (file-exists-p tmp-out-file)
  17558. (delete-file tmp-out-file))
  17559. mathml))
  17560. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17561. prefix &optional dir)
  17562. "Use `org-create-math-formula' but check local cache first."
  17563. (let* ((absprefix (expand-file-name prefix dir))
  17564. (print-length nil) (print-level nil)
  17565. (formula-id (concat
  17566. "formula-"
  17567. (sha1
  17568. (prin1-to-string
  17569. (list latex-frag
  17570. org-latex-to-mathml-convert-command)))))
  17571. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17572. (formula-cache-dir (file-name-directory formula-cache)))
  17573. (unless (file-directory-p formula-cache-dir)
  17574. (make-directory formula-cache-dir t))
  17575. (unless (file-exists-p formula-cache)
  17576. (org-create-math-formula latex-frag formula-cache))
  17577. (if (file-exists-p formula-cache)
  17578. ;; Successful conversion. Return the link to MathML file.
  17579. (org-add-props
  17580. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17581. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17582. 'org-latex-src-embed-type (if latex-frag-type
  17583. 'paragraph 'character)))
  17584. ;; Failed conversion. Return the LaTeX fragment verbatim
  17585. latex-frag)))
  17586. (defun org-create-formula-image (string tofile options buffer &optional type)
  17587. "Create an image from LaTeX source using dvipng or convert.
  17588. This function calls either `org-create-formula-image-with-dvipng'
  17589. or `org-create-formula-image-with-imagemagick' depending on the
  17590. value of `org-latex-create-formula-image-program' or on the value
  17591. of the optional TYPE variable.
  17592. Note: ultimately these two function should be combined as they
  17593. share a good deal of logic."
  17594. (org-check-external-command
  17595. "latex" "needed to convert LaTeX fragments to images")
  17596. (funcall
  17597. (case (or type org-latex-create-formula-image-program)
  17598. ('dvipng
  17599. (org-check-external-command
  17600. "dvipng" "needed to convert LaTeX fragments to images")
  17601. 'org-create-formula-image-with-dvipng)
  17602. ('imagemagick
  17603. (org-check-external-command
  17604. "convert" "you need to install imagemagick")
  17605. 'org-create-formula-image-with-imagemagick)
  17606. (t (error
  17607. "Invalid value of `org-latex-create-formula-image-program'")))
  17608. string tofile options buffer))
  17609. (declare-function org-export-get-backend "ox" (name))
  17610. (declare-function org-export--get-global-options "ox" (&optional backend))
  17611. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17612. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17613. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17614. (defun org-create-formula--latex-header ()
  17615. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17616. (let ((info (org-combine-plists (org-export--get-global-options
  17617. (org-export-get-backend 'latex))
  17618. (org-export--get-inbuffer-options
  17619. (org-export-get-backend 'latex)))))
  17620. (org-latex-guess-babel-language
  17621. (org-latex-guess-inputenc
  17622. (org-splice-latex-header
  17623. org-format-latex-header
  17624. org-latex-default-packages-alist
  17625. org-latex-packages-alist t
  17626. (plist-get info :latex-header)))
  17627. info)))
  17628. (defun org--get-display-dpi ()
  17629. "Get the DPI of the display.
  17630. Assumes that the display has the same pixel width in the
  17631. horizontal and vertical directions."
  17632. (if (display-graphic-p)
  17633. (round (/ (display-pixel-height)
  17634. (/ (display-mm-height) 25.4)))
  17635. (error "Attempt to calculate the dpi of a non-graphic display")))
  17636. ;; This function borrows from Ganesh Swami's latex2png.el
  17637. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17638. "This calls dvipng."
  17639. (require 'ox-latex)
  17640. (let* ((tmpdir (if (featurep 'xemacs)
  17641. (temp-directory)
  17642. temporary-file-directory))
  17643. (texfilebase (make-temp-name
  17644. (expand-file-name "orgtex" tmpdir)))
  17645. (texfile (concat texfilebase ".tex"))
  17646. (dvifile (concat texfilebase ".dvi"))
  17647. (pngfile (concat texfilebase ".png"))
  17648. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17649. ;; This assumes that the display has the same pixel width in
  17650. ;; the horizontal and vertical directions
  17651. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17652. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17653. "Black"))
  17654. (bg (or (plist-get options (if buffer :background :html-background))
  17655. "Transparent")))
  17656. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17657. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17658. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17659. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17660. (let ((latex-header (org-create-formula--latex-header)))
  17661. (with-temp-file texfile
  17662. (insert latex-header)
  17663. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17664. (let ((dir default-directory))
  17665. (ignore-errors
  17666. (cd tmpdir)
  17667. (call-process "latex" nil nil nil texfile))
  17668. (cd dir))
  17669. (if (not (file-exists-p dvifile))
  17670. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17671. (ignore-errors
  17672. (if (featurep 'xemacs)
  17673. (call-process "dvipng" nil nil nil
  17674. "-fg" fg "-bg" bg
  17675. "-T" "tight"
  17676. "-o" pngfile
  17677. dvifile)
  17678. (call-process "dvipng" nil nil nil
  17679. "-fg" fg "-bg" bg
  17680. "-D" dpi
  17681. ;;"-x" scale "-y" scale
  17682. "-T" "tight"
  17683. "-o" pngfile
  17684. dvifile)))
  17685. (if (not (file-exists-p pngfile))
  17686. (if org-format-latex-signal-error
  17687. (error "Failed to create png file from %s" texfile)
  17688. (message "Failed to create png file from %s" texfile)
  17689. nil)
  17690. ;; Use the requested file name and clean up
  17691. (copy-file pngfile tofile 'replace)
  17692. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17693. (if (file-exists-p (concat texfilebase e))
  17694. (delete-file (concat texfilebase e))))
  17695. pngfile))))
  17696. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17697. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17698. "This calls convert, which is included into imagemagick."
  17699. (require 'ox-latex)
  17700. (let* ((tmpdir (if (featurep 'xemacs)
  17701. (temp-directory)
  17702. temporary-file-directory))
  17703. (texfilebase (make-temp-name
  17704. (expand-file-name "orgtex" tmpdir)))
  17705. (texfile (concat texfilebase ".tex"))
  17706. (pdffile (concat texfilebase ".pdf"))
  17707. (pngfile (concat texfilebase ".png"))
  17708. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17709. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17710. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17711. "black"))
  17712. (bg (or (plist-get options (if buffer :background :html-background))
  17713. "white")))
  17714. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17715. (setq fg (org-latex-color-format fg)))
  17716. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17717. (setq bg (org-latex-color-format
  17718. (if (string= bg "Transparent") "white" bg))))
  17719. (let ((latex-header (org-create-formula--latex-header)))
  17720. (with-temp-file texfile
  17721. (insert latex-header)
  17722. (insert "\n\\begin{document}\n"
  17723. "\\definecolor{fg}{rgb}{" fg "}\n"
  17724. "\\definecolor{bg}{rgb}{" bg "}\n"
  17725. "\n\\pagecolor{bg}\n"
  17726. "\n{\\color{fg}\n"
  17727. string
  17728. "\n}\n"
  17729. "\n\\end{document}\n")))
  17730. (org-latex-compile texfile t)
  17731. (if (not (file-exists-p pdffile))
  17732. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17733. (ignore-errors
  17734. (if (featurep 'xemacs)
  17735. (call-process "convert" nil nil nil
  17736. "-density" "96"
  17737. "-trim"
  17738. "-antialias"
  17739. pdffile
  17740. "-quality" "100"
  17741. ;; "-sharpen" "0x1.0"
  17742. pngfile)
  17743. (call-process "convert" nil nil nil
  17744. "-density" dpi
  17745. "-trim"
  17746. "-antialias"
  17747. pdffile
  17748. "-quality" "100"
  17749. ;; "-sharpen" "0x1.0"
  17750. pngfile)))
  17751. (if (not (file-exists-p pngfile))
  17752. (if org-format-latex-signal-error
  17753. (error "Failed to create png file from %s" texfile)
  17754. (message "Failed to create png file from %s" texfile)
  17755. nil)
  17756. ;; Use the requested file name and clean up
  17757. (copy-file pngfile tofile 'replace)
  17758. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17759. (if (file-exists-p (concat texfilebase e))
  17760. (delete-file (concat texfilebase e))))
  17761. pngfile))))
  17762. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17763. "Fill a LaTeX header template TPL.
  17764. In the template, the following place holders will be recognized:
  17765. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17766. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17767. [PACKAGES] \\usepackage statements for PKG
  17768. [NO-PACKAGES] do not include PKG
  17769. [EXTRA] the string EXTRA
  17770. [NO-EXTRA] do not include EXTRA
  17771. For backward compatibility, if both the positive and the negative place
  17772. holder is missing, the positive one (without the \"NO-\") will be
  17773. assumed to be present at the end of the template.
  17774. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17775. EXTRA is a string.
  17776. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17777. (let (rpl (end ""))
  17778. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17779. (setq rpl (if (or (match-end 1) (not def-pkg))
  17780. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17781. tpl (replace-match rpl t t tpl))
  17782. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17783. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17784. (setq rpl (if (or (match-end 1) (not pkg))
  17785. "" (org-latex-packages-to-string pkg snippets-p t))
  17786. tpl (replace-match rpl t t tpl))
  17787. (if pkg (setq end
  17788. (concat end "\n"
  17789. (org-latex-packages-to-string pkg snippets-p)))))
  17790. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17791. (setq rpl (if (or (match-end 1) (not extra))
  17792. "" (concat extra "\n"))
  17793. tpl (replace-match rpl t t tpl))
  17794. (if (and extra (string-match "\\S-" extra))
  17795. (setq end (concat end "\n" extra))))
  17796. (if (string-match "\\S-" end)
  17797. (concat tpl "\n" end)
  17798. tpl)))
  17799. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17800. "Turn an alist of packages into a string with the \\usepackage macros."
  17801. (setq pkg (mapconcat (lambda(p)
  17802. (cond
  17803. ((stringp p) p)
  17804. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17805. (format "%% Package %s omitted" (cadr p)))
  17806. ((equal "" (car p))
  17807. (format "\\usepackage{%s}" (cadr p)))
  17808. (t
  17809. (format "\\usepackage[%s]{%s}"
  17810. (car p) (cadr p)))))
  17811. pkg
  17812. "\n"))
  17813. (if newline (concat pkg "\n") pkg))
  17814. (defun org-dvipng-color (attr)
  17815. "Return a RGB color specification for dvipng."
  17816. (apply 'format "rgb %s %s %s"
  17817. (mapcar 'org-normalize-color
  17818. (if (featurep 'xemacs)
  17819. (color-rgb-components
  17820. (face-property 'default
  17821. (cond ((eq attr :foreground) 'foreground)
  17822. ((eq attr :background) 'background))))
  17823. (color-values (face-attribute 'default attr nil))))))
  17824. (defun org-dvipng-color-format (color-name)
  17825. "Convert COLOR-NAME to a RGB color value for dvipng."
  17826. (apply 'format "rgb %s %s %s"
  17827. (mapcar 'org-normalize-color
  17828. (color-values color-name))))
  17829. (defun org-latex-color (attr)
  17830. "Return a RGB color for the LaTeX color package."
  17831. (apply 'format "%s,%s,%s"
  17832. (mapcar 'org-normalize-color
  17833. (if (featurep 'xemacs)
  17834. (color-rgb-components
  17835. (face-property 'default
  17836. (cond ((eq attr :foreground) 'foreground)
  17837. ((eq attr :background) 'background))))
  17838. (color-values (face-attribute 'default attr nil))))))
  17839. (defun org-latex-color-format (color-name)
  17840. "Convert COLOR-NAME to a RGB color value."
  17841. (apply 'format "%s,%s,%s"
  17842. (mapcar 'org-normalize-color
  17843. (color-values color-name))))
  17844. (defun org-normalize-color (value)
  17845. "Return string to be used as color value for an RGB component."
  17846. (format "%g" (/ value 65535.0)))
  17847. ;; Image display
  17848. (defvar org-inline-image-overlays nil)
  17849. (make-variable-buffer-local 'org-inline-image-overlays)
  17850. (defun org-toggle-inline-images (&optional include-linked)
  17851. "Toggle the display of inline images.
  17852. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17853. (interactive "P")
  17854. (if org-inline-image-overlays
  17855. (progn
  17856. (org-remove-inline-images)
  17857. (when (org-called-interactively-p 'interactive)
  17858. (message "Inline image display turned off")))
  17859. (org-display-inline-images include-linked)
  17860. (when (org-called-interactively-p 'interactive)
  17861. (message (if org-inline-image-overlays
  17862. (format "%d images displayed inline"
  17863. (length org-inline-image-overlays))
  17864. "No images to display inline")))))
  17865. (defun org-redisplay-inline-images ()
  17866. "Refresh the display of inline images."
  17867. (interactive)
  17868. (if (not org-inline-image-overlays)
  17869. (org-toggle-inline-images)
  17870. (org-toggle-inline-images)
  17871. (org-toggle-inline-images)))
  17872. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17873. "Display inline images.
  17874. An inline image is a link which follows either of these
  17875. conventions:
  17876. 1. Its path is a file with an extension matching return value
  17877. from `image-file-name-regexp' and it has no contents.
  17878. 2. Its description consists in a single link of the previous
  17879. type.
  17880. When optional argument INCLUDE-LINKED is non-nil, also links with
  17881. a text description part will be inlined. This can be nice for
  17882. a quick look at those images, but it does not reflect what
  17883. exported files will look like.
  17884. When optional argument REFRESH is non-nil, refresh existing
  17885. images between BEG and END. This will create new image displays
  17886. only if necessary. BEG and END default to the buffer
  17887. boundaries."
  17888. (interactive "P")
  17889. (when (display-graphic-p)
  17890. (unless refresh
  17891. (org-remove-inline-images)
  17892. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17893. (org-with-wide-buffer
  17894. (goto-char (or beg (point-min)))
  17895. (let ((case-fold-search t)
  17896. (file-extension-re (org-image-file-name-regexp)))
  17897. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17898. (let ((link (save-match-data (org-element-context))))
  17899. ;; Check if we're at an inline image.
  17900. (when (and (equal (org-element-property :type link) "file")
  17901. (or include-linked
  17902. (not (org-element-property :contents-begin link)))
  17903. (let ((parent (org-element-property :parent link)))
  17904. (or (not (eq (org-element-type parent) 'link))
  17905. (not (cdr (org-element-contents parent)))))
  17906. (org-string-match-p file-extension-re
  17907. (org-element-property :path link)))
  17908. (let ((file (expand-file-name (org-element-property :path link))))
  17909. (when (file-exists-p file)
  17910. (let ((width
  17911. ;; Apply `org-image-actual-width' specifications.
  17912. (cond
  17913. ((not (image-type-available-p 'imagemagick)) nil)
  17914. ((eq org-image-actual-width t) nil)
  17915. ((listp org-image-actual-width)
  17916. (or
  17917. ;; First try to find a width among
  17918. ;; attributes associated to the paragraph
  17919. ;; containing link.
  17920. (let ((paragraph
  17921. (let ((e link))
  17922. (while (and (setq e (org-element-property
  17923. :parent e))
  17924. (not (eq (org-element-type e)
  17925. 'paragraph))))
  17926. e)))
  17927. (when paragraph
  17928. (save-excursion
  17929. (goto-char (org-element-property :begin paragraph))
  17930. (when
  17931. (re-search-forward
  17932. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17933. (org-element-property
  17934. :post-affiliated paragraph)
  17935. t)
  17936. (string-to-number (match-string 1))))))
  17937. ;; Otherwise, fall-back to provided number.
  17938. (car org-image-actual-width)))
  17939. ((numberp org-image-actual-width)
  17940. org-image-actual-width)))
  17941. (old (get-char-property-and-overlay
  17942. (org-element-property :begin link)
  17943. 'org-image-overlay)))
  17944. (if (and (car-safe old) refresh)
  17945. (image-refresh (overlay-get (cdr old) 'display))
  17946. (let ((image (create-image file
  17947. (and width 'imagemagick)
  17948. nil
  17949. :width width)))
  17950. (when image
  17951. (let* ((link
  17952. ;; If inline image is the description
  17953. ;; of another link, be sure to
  17954. ;; consider the latter as the one to
  17955. ;; apply the overlay on.
  17956. (let ((parent
  17957. (org-element-property :parent link)))
  17958. (if (eq (org-element-type parent) 'link)
  17959. parent
  17960. link)))
  17961. (ov (make-overlay
  17962. (org-element-property :begin link)
  17963. (progn
  17964. (goto-char
  17965. (org-element-property :end link))
  17966. (skip-chars-backward " \t")
  17967. (point)))))
  17968. (overlay-put ov 'display image)
  17969. (overlay-put ov 'face 'default)
  17970. (overlay-put ov 'org-image-overlay t)
  17971. (overlay-put
  17972. ov 'modification-hooks
  17973. (list 'org-display-inline-remove-overlay))
  17974. (push ov org-inline-image-overlays)))))))))))))))
  17975. (define-obsolete-function-alias
  17976. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17977. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17978. "Remove inline-display overlay if a corresponding region is modified."
  17979. (let ((inhibit-modification-hooks t))
  17980. (when (and ov after)
  17981. (delete ov org-inline-image-overlays)
  17982. (delete-overlay ov))))
  17983. (defun org-remove-inline-images ()
  17984. "Remove inline display of images."
  17985. (interactive)
  17986. (mapc 'delete-overlay org-inline-image-overlays)
  17987. (setq org-inline-image-overlays nil))
  17988. ;;;; Key bindings
  17989. ;; Outline functions from `outline-mode-prefix-map'
  17990. ;; that can be remapped in Org:
  17991. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17992. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17993. (define-key org-mode-map [remap outline-forward-same-level]
  17994. 'org-forward-heading-same-level)
  17995. (define-key org-mode-map [remap outline-backward-same-level]
  17996. 'org-backward-heading-same-level)
  17997. (define-key org-mode-map [remap show-branches]
  17998. 'org-kill-note-or-show-branches)
  17999. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18000. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18001. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18002. (define-key org-mode-map [remap outline-next-visible-heading]
  18003. 'org-next-visible-heading)
  18004. (define-key org-mode-map [remap outline-previous-visible-heading]
  18005. 'org-previous-visible-heading)
  18006. ;; Outline functions from `outline-mode-prefix-map' that can not
  18007. ;; be remapped in Org:
  18008. ;; - the column "key binding" shows whether the Outline function is still
  18009. ;; available in Org mode on the same key that it has been bound to in
  18010. ;; Outline mode:
  18011. ;; - "overridden": key used for a different functionality in Org mode
  18012. ;; - else: key still bound to the same Outline function in Org mode
  18013. ;; | Outline function | key binding | Org replacement |
  18014. ;; |------------------------------------+-------------+--------------------------|
  18015. ;; | `outline-next-visible-heading' | `C-c C-n' | better: skip inlinetasks |
  18016. ;; | `outline-previous-visible-heading' | `C-c C-p' | better: skip inlinetasks |
  18017. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18018. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18019. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18020. ;; | `show-entry' | overridden | no replacement |
  18021. ;; | `show-children' | `C-c C-i' | visibility cycling |
  18022. ;; | `show-branches' | `C-c C-k' | still same function |
  18023. ;; | `show-subtree' | overridden | visibility cycling |
  18024. ;; | `show-all' | overridden | no replacement |
  18025. ;; | `hide-subtree' | overridden | visibility cycling |
  18026. ;; | `hide-body' | overridden | no replacement |
  18027. ;; | `hide-entry' | overridden | visibility cycling |
  18028. ;; | `hide-leaves' | overridden | no replacement |
  18029. ;; | `hide-sublevels' | overridden | no replacement |
  18030. ;; | `hide-other' | overridden | no replacement |
  18031. ;; Make `C-c C-x' a prefix key
  18032. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18033. ;; TAB key with modifiers
  18034. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18035. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18036. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18037. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  18038. ;; The following line is necessary under Suse GNU/Linux
  18039. (unless (featurep 'xemacs)
  18040. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18041. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18042. (define-key org-mode-map [backtab] 'org-shifttab)
  18043. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18044. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18045. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18046. ;; Cursor keys with modifiers
  18047. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18048. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18049. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18050. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18051. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18052. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18053. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18054. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18055. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18056. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18057. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18058. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18059. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18060. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18061. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18062. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18063. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18064. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18065. ;; Babel keys
  18066. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18067. (mapc (lambda (pair)
  18068. (define-key org-babel-map (car pair) (cdr pair)))
  18069. org-babel-key-bindings)
  18070. ;;; Extra keys for tty access.
  18071. ;; We only set them when really needed because otherwise the
  18072. ;; menus don't show the simple keys
  18073. (when (or org-use-extra-keys
  18074. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18075. (not window-system))
  18076. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18077. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18078. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18079. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18080. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18081. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18082. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18083. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18084. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18085. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18086. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18087. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18088. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18089. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18090. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18091. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18092. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18093. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18094. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18095. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18096. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18097. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18098. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  18099. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18100. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18101. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18102. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18103. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18104. ;; All the other keys
  18105. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  18106. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18107. (if (boundp 'narrow-map)
  18108. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18109. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18110. (if (boundp 'narrow-map)
  18111. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18112. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18113. (if (boundp 'narrow-map)
  18114. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18115. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18116. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18117. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18118. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18119. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18120. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18121. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18122. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18123. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18124. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18125. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18126. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  18127. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18128. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18129. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18130. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18131. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18132. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18133. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18134. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18135. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18136. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18137. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18138. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18139. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18140. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18141. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18142. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18143. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18144. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18145. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18146. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18147. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18148. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18149. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18150. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18151. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18152. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18153. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18154. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18155. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18156. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18157. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18158. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18159. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18160. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18161. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18162. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18163. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18164. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18165. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18166. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18167. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18168. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18169. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18170. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18171. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18172. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18173. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18174. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18175. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18176. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18177. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18178. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18179. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18180. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18181. (org-defkey org-mode-map "\C-m" 'org-return)
  18182. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18183. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18184. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18185. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18186. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18187. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18188. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18189. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18190. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18191. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18192. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18193. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18194. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18195. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18196. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18197. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18198. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18199. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18200. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18201. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18202. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18203. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18204. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18205. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18206. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18207. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18208. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18209. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18210. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18211. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18212. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18213. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18214. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18215. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18216. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18217. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18218. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18219. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18220. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18221. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18222. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18223. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18224. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18225. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18226. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18227. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18228. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18229. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18230. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18231. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18232. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18233. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18234. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18235. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18236. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18237. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18238. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18239. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18240. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18241. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18242. (when (featurep 'xemacs)
  18243. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18244. (defconst org-speed-commands-default
  18245. '(
  18246. ("Outline Navigation")
  18247. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18248. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18249. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18250. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18251. ("F" . org-next-block)
  18252. ("B" . org-previous-block)
  18253. ("u" . (org-speed-move-safe 'outline-up-heading))
  18254. ("j" . org-goto)
  18255. ("g" . (org-refile t))
  18256. ("Outline Visibility")
  18257. ("c" . org-cycle)
  18258. ("C" . org-shifttab)
  18259. (" " . org-display-outline-path)
  18260. ("s" . org-narrow-to-subtree)
  18261. ("=" . org-columns)
  18262. ("Outline Structure Editing")
  18263. ("U" . org-metaup)
  18264. ("D" . org-metadown)
  18265. ("r" . org-metaright)
  18266. ("l" . org-metaleft)
  18267. ("R" . org-shiftmetaright)
  18268. ("L" . org-shiftmetaleft)
  18269. ("i" . (progn (forward-char 1) (call-interactively
  18270. 'org-insert-heading-respect-content)))
  18271. ("^" . org-sort)
  18272. ("w" . org-refile)
  18273. ("a" . org-archive-subtree-default-with-confirmation)
  18274. ("@" . org-mark-subtree)
  18275. ("#" . org-toggle-comment)
  18276. ("Clock Commands")
  18277. ("I" . org-clock-in)
  18278. ("O" . org-clock-out)
  18279. ("Meta Data Editing")
  18280. ("t" . org-todo)
  18281. ("," . (org-priority))
  18282. ("0" . (org-priority ?\ ))
  18283. ("1" . (org-priority ?A))
  18284. ("2" . (org-priority ?B))
  18285. ("3" . (org-priority ?C))
  18286. (":" . org-set-tags-command)
  18287. ("e" . org-set-effort)
  18288. ("E" . org-inc-effort)
  18289. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18290. (org-entry-put (point) "APPT_WARNTIME" m)))
  18291. ("Agenda Views etc")
  18292. ("v" . org-agenda)
  18293. ("/" . org-sparse-tree)
  18294. ("Misc")
  18295. ("o" . org-open-at-point)
  18296. ("?" . org-speed-command-help)
  18297. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18298. (">" . (org-agenda-remove-restriction-lock))
  18299. )
  18300. "The default speed commands.")
  18301. (defun org-print-speed-command (e)
  18302. (if (> (length (car e)) 1)
  18303. (progn
  18304. (princ "\n")
  18305. (princ (car e))
  18306. (princ "\n")
  18307. (princ (make-string (length (car e)) ?-))
  18308. (princ "\n"))
  18309. (princ (car e))
  18310. (princ " ")
  18311. (if (symbolp (cdr e))
  18312. (princ (symbol-name (cdr e)))
  18313. (prin1 (cdr e)))
  18314. (princ "\n")))
  18315. (defun org-speed-command-help ()
  18316. "Show the available speed commands."
  18317. (interactive)
  18318. (if (not org-use-speed-commands)
  18319. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18320. (with-output-to-temp-buffer "*Help*"
  18321. (princ "User-defined Speed commands\n===========================\n")
  18322. (mapc 'org-print-speed-command org-speed-commands-user)
  18323. (princ "\n")
  18324. (princ "Built-in Speed commands\n=======================\n")
  18325. (mapc 'org-print-speed-command org-speed-commands-default))
  18326. (with-current-buffer "*Help*"
  18327. (setq truncate-lines t))))
  18328. (defun org-speed-move-safe (cmd)
  18329. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18330. If not, return to the original position and throw an error."
  18331. (interactive)
  18332. (let ((pos (point)))
  18333. (call-interactively cmd)
  18334. (unless (and (bolp) (org-at-heading-p))
  18335. (goto-char pos)
  18336. (error "Boundary reached while executing %s" cmd))))
  18337. (defvar org-self-insert-command-undo-counter 0)
  18338. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18339. (defvar org-speed-command nil)
  18340. (define-obsolete-function-alias
  18341. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18342. (defun org-speed-command-activate (keys)
  18343. "Hook for activating single-letter speed commands.
  18344. `org-speed-commands-default' specifies a minimal command set.
  18345. Use `org-speed-commands-user' for further customization."
  18346. (when (or (and (bolp) (looking-at org-outline-regexp))
  18347. (and (functionp org-use-speed-commands)
  18348. (funcall org-use-speed-commands)))
  18349. (cdr (assoc keys (append org-speed-commands-user
  18350. org-speed-commands-default)))))
  18351. (define-obsolete-function-alias
  18352. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18353. (defun org-babel-speed-command-activate (keys)
  18354. "Hook for activating single-letter code block commands."
  18355. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18356. (cdr (assoc keys org-babel-key-bindings))))
  18357. (defcustom org-speed-command-hook
  18358. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18359. "Hook for activating speed commands at strategic locations.
  18360. Hook functions are called in sequence until a valid handler is
  18361. found.
  18362. Each hook takes a single argument, a user-pressed command key
  18363. which is also a `self-insert-command' from the global map.
  18364. Within the hook, examine the cursor position and the command key
  18365. and return nil or a valid handler as appropriate. Handler could
  18366. be one of an interactive command, a function, or a form.
  18367. Set `org-use-speed-commands' to non-nil value to enable this
  18368. hook. The default setting is `org-speed-command-activate'."
  18369. :group 'org-structure
  18370. :version "24.1"
  18371. :type 'hook)
  18372. (defun org-self-insert-command (N)
  18373. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18374. If the cursor is in a table looking at whitespace, the whitespace is
  18375. overwritten, and the table is not marked as requiring realignment."
  18376. (interactive "p")
  18377. (org-check-before-invisible-edit 'insert)
  18378. (cond
  18379. ((and org-use-speed-commands
  18380. (let ((kv (this-command-keys-vector)))
  18381. (setq org-speed-command
  18382. (run-hook-with-args-until-success
  18383. 'org-speed-command-hook
  18384. (make-string 1 (aref kv (1- (length kv))))))))
  18385. (cond
  18386. ((commandp org-speed-command)
  18387. (setq this-command org-speed-command)
  18388. (call-interactively org-speed-command))
  18389. ((functionp org-speed-command)
  18390. (funcall org-speed-command))
  18391. ((and org-speed-command (listp org-speed-command))
  18392. (eval org-speed-command))
  18393. (t (let (org-use-speed-commands)
  18394. (call-interactively 'org-self-insert-command)))))
  18395. ((and
  18396. (org-table-p)
  18397. (progn
  18398. ;; check if we blank the field, and if that triggers align
  18399. (and (featurep 'org-table) org-table-auto-blank-field
  18400. (memq last-command
  18401. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18402. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18403. ;; got extra space, this field does not determine column width
  18404. (let (org-table-may-need-update) (org-table-blank-field))
  18405. ;; no extra space, this field may determine column width
  18406. (org-table-blank-field)))
  18407. t)
  18408. (eq N 1)
  18409. (looking-at "[^|\n]* |"))
  18410. (let (org-table-may-need-update)
  18411. (goto-char (1- (match-end 0)))
  18412. (backward-delete-char 1)
  18413. (goto-char (match-beginning 0))
  18414. (self-insert-command N)))
  18415. (t
  18416. (setq org-table-may-need-update t)
  18417. (self-insert-command N)
  18418. (org-fix-tags-on-the-fly)
  18419. (if org-self-insert-cluster-for-undo
  18420. (if (not (eq last-command 'org-self-insert-command))
  18421. (setq org-self-insert-command-undo-counter 1)
  18422. (if (>= org-self-insert-command-undo-counter 20)
  18423. (setq org-self-insert-command-undo-counter 1)
  18424. (and (> org-self-insert-command-undo-counter 0)
  18425. buffer-undo-list (listp buffer-undo-list)
  18426. (not (cadr buffer-undo-list)) ; remove nil entry
  18427. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18428. (setq org-self-insert-command-undo-counter
  18429. (1+ org-self-insert-command-undo-counter))))))))
  18430. (defun org-check-before-invisible-edit (kind)
  18431. "Check is editing if kind KIND would be dangerous with invisible text around.
  18432. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18433. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18434. (if (and org-catch-invisible-edits
  18435. (or (not (boundp 'visible-mode)) (not visible-mode))
  18436. (or (get-char-property (point) 'invisible)
  18437. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18438. ;; OK, we need to take a closer look
  18439. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18440. (invisible-before-point (if (bobp) nil (get-char-property
  18441. (1- (point)) 'invisible)))
  18442. (border-and-ok-direction
  18443. (or
  18444. ;; Check if we are acting predictably before invisible text
  18445. (and invisible-at-point (not invisible-before-point)
  18446. (memq kind '(insert delete-backward)))
  18447. ;; Check if we are acting predictably after invisible text
  18448. ;; This works not well, and I have turned it off. It seems
  18449. ;; better to always show and stop after invisible text.
  18450. ;; (and (not invisible-at-point) invisible-before-point
  18451. ;; (memq kind '(insert delete)))
  18452. )))
  18453. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18454. (memq invisible-before-point '(outline org-hide-block t)))
  18455. (if (eq org-catch-invisible-edits 'error)
  18456. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18457. (if (and org-custom-properties-overlays
  18458. (y-or-n-p "Display invisible properties in this buffer? "))
  18459. (org-toggle-custom-properties-visibility)
  18460. ;; Make the area visible
  18461. (save-excursion
  18462. (if invisible-before-point
  18463. (goto-char (previous-single-char-property-change
  18464. (point) 'invisible)))
  18465. (show-subtree))
  18466. (cond
  18467. ((eq org-catch-invisible-edits 'show)
  18468. ;; That's it, we do the edit after showing
  18469. (message
  18470. "Unfolding invisible region around point before editing")
  18471. (sit-for 1))
  18472. ((and (eq org-catch-invisible-edits 'smart)
  18473. border-and-ok-direction)
  18474. (message "Unfolding invisible region around point before editing"))
  18475. (t
  18476. ;; Don't do the edit, make the user repeat it in full visibility
  18477. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18478. (defun org-fix-tags-on-the-fly ()
  18479. (when (and (equal (char-after (point-at-bol)) ?*)
  18480. (org-at-heading-p))
  18481. (org-align-tags-here org-tags-column)))
  18482. (defun org-delete-backward-char (N)
  18483. "Like `delete-backward-char', insert whitespace at field end in tables.
  18484. When deleting backwards, in tables this function will insert whitespace in
  18485. front of the next \"|\" separator, to keep the table aligned. The table will
  18486. still be marked for re-alignment if the field did fill the entire column,
  18487. because, in this case the deletion might narrow the column."
  18488. (interactive "p")
  18489. (save-match-data
  18490. (org-check-before-invisible-edit 'delete-backward)
  18491. (if (and (org-table-p)
  18492. (eq N 1)
  18493. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18494. (looking-at ".*?|"))
  18495. (let ((pos (point))
  18496. (noalign (looking-at "[^|\n\r]* |"))
  18497. (c org-table-may-need-update))
  18498. (backward-delete-char N)
  18499. (if (not overwrite-mode)
  18500. (progn
  18501. (skip-chars-forward "^|")
  18502. (insert " ")
  18503. (goto-char (1- pos))))
  18504. ;; noalign: if there were two spaces at the end, this field
  18505. ;; does not determine the width of the column.
  18506. (if noalign (setq org-table-may-need-update c)))
  18507. (backward-delete-char N)
  18508. (org-fix-tags-on-the-fly))))
  18509. (defun org-delete-char (N)
  18510. "Like `delete-char', but insert whitespace at field end in tables.
  18511. When deleting characters, in tables this function will insert whitespace in
  18512. front of the next \"|\" separator, to keep the table aligned. The table will
  18513. still be marked for re-alignment if the field did fill the entire column,
  18514. because, in this case the deletion might narrow the column."
  18515. (interactive "p")
  18516. (save-match-data
  18517. (org-check-before-invisible-edit 'delete)
  18518. (if (and (org-table-p)
  18519. (not (bolp))
  18520. (not (= (char-after) ?|))
  18521. (eq N 1))
  18522. (if (looking-at ".*?|")
  18523. (let ((pos (point))
  18524. (noalign (looking-at "[^|\n\r]* |"))
  18525. (c org-table-may-need-update))
  18526. (replace-match
  18527. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18528. (goto-char pos)
  18529. ;; noalign: if there were two spaces at the end, this field
  18530. ;; does not determine the width of the column.
  18531. (if noalign (setq org-table-may-need-update c)))
  18532. (delete-char N))
  18533. (delete-char N)
  18534. (org-fix-tags-on-the-fly))))
  18535. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18536. (put 'org-self-insert-command 'delete-selection
  18537. (lambda ()
  18538. (not (run-hook-with-args-until-success
  18539. 'self-insert-uses-region-functions))))
  18540. (put 'orgtbl-self-insert-command 'delete-selection
  18541. (lambda ()
  18542. (not (run-hook-with-args-until-success
  18543. 'self-insert-uses-region-functions))))
  18544. (put 'org-delete-char 'delete-selection 'supersede)
  18545. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18546. (put 'org-yank 'delete-selection 'yank)
  18547. ;; Make `flyspell-mode' delay after some commands
  18548. (put 'org-self-insert-command 'flyspell-delayed t)
  18549. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18550. (put 'org-delete-char 'flyspell-delayed t)
  18551. (put 'org-delete-backward-char 'flyspell-delayed t)
  18552. ;; Make pabbrev-mode expand after org-mode commands
  18553. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18554. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18555. (defun org-remap (map &rest commands)
  18556. "In MAP, remap the functions given in COMMANDS.
  18557. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18558. (let (new old)
  18559. (while commands
  18560. (setq old (pop commands) new (pop commands))
  18561. (if (fboundp 'command-remapping)
  18562. (org-defkey map (vector 'remap old) new)
  18563. (substitute-key-definition old new map global-map)))))
  18564. (defun org-transpose-words ()
  18565. "Transpose words for Org.
  18566. This uses the `org-mode-transpose-word-syntax-table' syntax
  18567. table, which interprets characters in `org-emphasis-alist' as
  18568. word constituents."
  18569. (interactive)
  18570. (with-syntax-table org-mode-transpose-word-syntax-table
  18571. (call-interactively 'transpose-words)))
  18572. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18573. (when (eq org-enable-table-editor 'optimized)
  18574. ;; If the user wants maximum table support, we need to hijack
  18575. ;; some standard editing functions
  18576. (org-remap org-mode-map
  18577. 'self-insert-command 'org-self-insert-command
  18578. 'delete-char 'org-delete-char
  18579. 'delete-backward-char 'org-delete-backward-char)
  18580. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18581. (defvar org-ctrl-c-ctrl-c-hook nil
  18582. "Hook for functions attaching themselves to `C-c C-c'.
  18583. This can be used to add additional functionality to the C-c C-c
  18584. key which executes context-dependent commands. This hook is run
  18585. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18586. run after the last test.
  18587. Each function will be called with no arguments. The function
  18588. must check if the context is appropriate for it to act. If yes,
  18589. it should do its thing and then return a non-nil value. If the
  18590. context is wrong, just do nothing and return nil.")
  18591. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18592. "Hook for functions attaching themselves to `C-c C-c'.
  18593. This can be used to add additional functionality to the C-c C-c
  18594. key which executes context-dependent commands. This hook is run
  18595. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18596. before the first test.
  18597. Each function will be called with no arguments. The function
  18598. must check if the context is appropriate for it to act. If yes,
  18599. it should do its thing and then return a non-nil value. If the
  18600. context is wrong, just do nothing and return nil.")
  18601. (defvar org-tab-first-hook nil
  18602. "Hook for functions to attach themselves to TAB.
  18603. See `org-ctrl-c-ctrl-c-hook' for more information.
  18604. This hook runs as the first action when TAB is pressed, even before
  18605. `org-cycle' messes around with the `outline-regexp' to cater for
  18606. inline tasks and plain list item folding.
  18607. If any function in this hook returns t, any other actions that
  18608. would have been caused by TAB (such as table field motion or visibility
  18609. cycling) will not occur.")
  18610. (defvar org-tab-after-check-for-table-hook nil
  18611. "Hook for functions to attach themselves to TAB.
  18612. See `org-ctrl-c-ctrl-c-hook' for more information.
  18613. This hook runs after it has been established that the cursor is not in a
  18614. table, but before checking if the cursor is in a headline or if global cycling
  18615. should be done.
  18616. If any function in this hook returns t, not other actions like visibility
  18617. cycling will be done.")
  18618. (defvar org-tab-after-check-for-cycling-hook nil
  18619. "Hook for functions to attach themselves to TAB.
  18620. See `org-ctrl-c-ctrl-c-hook' for more information.
  18621. This hook runs after it has been established that not table field motion and
  18622. not visibility should be done because of current context. This is probably
  18623. the place where a package like yasnippets can hook in.")
  18624. (defvar org-tab-before-tab-emulation-hook nil
  18625. "Hook for functions to attach themselves to TAB.
  18626. See `org-ctrl-c-ctrl-c-hook' for more information.
  18627. This hook runs after every other options for TAB have been exhausted, but
  18628. before indentation and \t insertion takes place.")
  18629. (defvar org-metaleft-hook nil
  18630. "Hook for functions attaching themselves to `M-left'.
  18631. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18632. (defvar org-metaright-hook nil
  18633. "Hook for functions attaching themselves to `M-right'.
  18634. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18635. (defvar org-metaup-hook nil
  18636. "Hook for functions attaching themselves to `M-up'.
  18637. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18638. (defvar org-metadown-hook nil
  18639. "Hook for functions attaching themselves to `M-down'.
  18640. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18641. (defvar org-shiftmetaleft-hook nil
  18642. "Hook for functions attaching themselves to `M-S-left'.
  18643. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18644. (defvar org-shiftmetaright-hook nil
  18645. "Hook for functions attaching themselves to `M-S-right'.
  18646. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18647. (defvar org-shiftmetaup-hook nil
  18648. "Hook for functions attaching themselves to `M-S-up'.
  18649. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18650. (defvar org-shiftmetadown-hook nil
  18651. "Hook for functions attaching themselves to `M-S-down'.
  18652. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18653. (defvar org-metareturn-hook nil
  18654. "Hook for functions attaching themselves to `M-RET'.
  18655. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18656. (defvar org-shiftup-hook nil
  18657. "Hook for functions attaching themselves to `S-up'.
  18658. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18659. (defvar org-shiftup-final-hook nil
  18660. "Hook for functions attaching themselves to `S-up'.
  18661. This one runs after all other options except shift-select have been excluded.
  18662. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18663. (defvar org-shiftdown-hook nil
  18664. "Hook for functions attaching themselves to `S-down'.
  18665. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18666. (defvar org-shiftdown-final-hook nil
  18667. "Hook for functions attaching themselves to `S-down'.
  18668. This one runs after all other options except shift-select have been excluded.
  18669. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18670. (defvar org-shiftleft-hook nil
  18671. "Hook for functions attaching themselves to `S-left'.
  18672. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18673. (defvar org-shiftleft-final-hook nil
  18674. "Hook for functions attaching themselves to `S-left'.
  18675. This one runs after all other options except shift-select have been excluded.
  18676. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18677. (defvar org-shiftright-hook nil
  18678. "Hook for functions attaching themselves to `S-right'.
  18679. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18680. (defvar org-shiftright-final-hook nil
  18681. "Hook for functions attaching themselves to `S-right'.
  18682. This one runs after all other options except shift-select have been excluded.
  18683. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18684. (defun org-modifier-cursor-error ()
  18685. "Throw an error, a modified cursor command was applied in wrong context."
  18686. (user-error "This command is active in special context like tables, headlines or items"))
  18687. (defun org-shiftselect-error ()
  18688. "Throw an error because Shift-Cursor command was applied in wrong context."
  18689. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18690. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18691. (user-error "This command works only in special context like headlines or timestamps")))
  18692. (defun org-call-for-shift-select (cmd)
  18693. (let ((this-command-keys-shift-translated t))
  18694. (call-interactively cmd)))
  18695. (defun org-shifttab (&optional arg)
  18696. "Global visibility cycling or move to previous table field.
  18697. Call `org-table-previous-field' within a table.
  18698. When ARG is nil, cycle globally through visibility states.
  18699. When ARG is a numeric prefix, show contents of this level."
  18700. (interactive "P")
  18701. (cond
  18702. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18703. ((integerp arg)
  18704. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18705. (message "Content view to level: %d" arg)
  18706. (org-content (prefix-numeric-value arg2))
  18707. (org-cycle-show-empty-lines t)
  18708. (setq org-cycle-global-status 'overview)))
  18709. (t (call-interactively 'org-global-cycle))))
  18710. (defun org-shiftmetaleft ()
  18711. "Promote subtree or delete table column.
  18712. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18713. `org-table-delete-column', depending on context. See the
  18714. individual commands for more information."
  18715. (interactive)
  18716. (cond
  18717. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18718. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18719. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18720. ((if (not (org-region-active-p)) (org-at-item-p)
  18721. (save-excursion (goto-char (region-beginning))
  18722. (org-at-item-p)))
  18723. (call-interactively 'org-outdent-item-tree))
  18724. (t (org-modifier-cursor-error))))
  18725. (defun org-shiftmetaright ()
  18726. "Demote subtree or insert table column.
  18727. Calls `org-demote-subtree', `org-indent-item-tree', or
  18728. `org-table-insert-column', depending on context. See the
  18729. individual commands for more information."
  18730. (interactive)
  18731. (cond
  18732. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18733. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18734. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18735. ((if (not (org-region-active-p)) (org-at-item-p)
  18736. (save-excursion (goto-char (region-beginning))
  18737. (org-at-item-p)))
  18738. (call-interactively 'org-indent-item-tree))
  18739. (t (org-modifier-cursor-error))))
  18740. (defun org-shiftmetaup (&optional arg)
  18741. "Drag the line at point up.
  18742. In a table, kill the current row.
  18743. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18744. but also adjust the previous clocked item in the clock history.
  18745. Everywhere else, drag the line at point up."
  18746. (interactive "P")
  18747. (cond
  18748. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18749. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18750. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18751. (call-interactively 'org-timestamp-up)))
  18752. (t (call-interactively 'org-drag-line-backward))))
  18753. (defun org-shiftmetadown (&optional arg)
  18754. "Drag the line at point down.
  18755. In a table, insert an empty row at the current line.
  18756. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18757. but also adjust the previous clocked item in the clock history.
  18758. Everywhere else, drag the line at point down."
  18759. (interactive "P")
  18760. (cond
  18761. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18762. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18763. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18764. (call-interactively 'org-timestamp-down)))
  18765. (t (call-interactively 'org-drag-line-forward))))
  18766. (defsubst org-hidden-tree-error ()
  18767. (user-error
  18768. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18769. (defun org-metaleft (&optional arg)
  18770. "Promote heading, list item at point or move table column left.
  18771. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18772. depending on context. With no specific context, calls the Emacs
  18773. default `backward-word'. See the individual commands for more
  18774. information.
  18775. This function runs the hook `org-metaleft-hook' as a first step,
  18776. and returns at first non-nil value."
  18777. (interactive "P")
  18778. (cond
  18779. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18780. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18781. ((org-with-limited-levels
  18782. (or (org-at-heading-p)
  18783. (and (org-region-active-p)
  18784. (save-excursion
  18785. (goto-char (region-beginning))
  18786. (org-at-heading-p)))))
  18787. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18788. (call-interactively 'org-do-promote))
  18789. ;; At an inline task.
  18790. ((org-at-heading-p)
  18791. (call-interactively 'org-inlinetask-promote))
  18792. ((or (org-at-item-p)
  18793. (and (org-region-active-p)
  18794. (save-excursion
  18795. (goto-char (region-beginning))
  18796. (org-at-item-p))))
  18797. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18798. (call-interactively 'org-outdent-item))
  18799. (t (call-interactively 'backward-word))))
  18800. (defun org-metaright (&optional arg)
  18801. "Demote heading, list item at point or move table column right.
  18802. In front of a drawer or a block keyword, indent it correctly.
  18803. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18804. `org-indnet-drawer' or `org-indent-block' depending on context.
  18805. With no specific context, calls the Emacs default `forward-word'.
  18806. See the individual commands for more information.
  18807. This function runs the hook `org-metaright-hook' as a first step,
  18808. and returns at first non-nil value."
  18809. (interactive "P")
  18810. (cond
  18811. ((run-hook-with-args-until-success 'org-metaright-hook))
  18812. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18813. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18814. ((org-at-block-p) (call-interactively 'org-indent-block))
  18815. ((org-with-limited-levels
  18816. (or (org-at-heading-p)
  18817. (and (org-region-active-p)
  18818. (save-excursion
  18819. (goto-char (region-beginning))
  18820. (org-at-heading-p)))))
  18821. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18822. (call-interactively 'org-do-demote))
  18823. ;; At an inline task.
  18824. ((org-at-heading-p)
  18825. (call-interactively 'org-inlinetask-demote))
  18826. ((or (org-at-item-p)
  18827. (and (org-region-active-p)
  18828. (save-excursion
  18829. (goto-char (region-beginning))
  18830. (org-at-item-p))))
  18831. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18832. (call-interactively 'org-indent-item))
  18833. (t (call-interactively 'forward-word))))
  18834. (defun org-check-for-hidden (what)
  18835. "Check if there are hidden headlines/items in the current visual line.
  18836. WHAT can be either `headlines' or `items'. If the current line is
  18837. an outline or item heading and it has a folded subtree below it,
  18838. this function returns t, nil otherwise."
  18839. (let ((re (cond
  18840. ((eq what 'headlines) org-outline-regexp-bol)
  18841. ((eq what 'items) (org-item-beginning-re))
  18842. (t (error "This should not happen"))))
  18843. beg end)
  18844. (save-excursion
  18845. (catch 'exit
  18846. (unless (org-region-active-p)
  18847. (setq beg (point-at-bol))
  18848. (beginning-of-line 2)
  18849. (while (and (not (eobp)) ;; this is like `next-line'
  18850. (get-char-property (1- (point)) 'invisible))
  18851. (beginning-of-line 2))
  18852. (setq end (point))
  18853. (goto-char beg)
  18854. (goto-char (point-at-eol))
  18855. (setq end (max end (point)))
  18856. (while (re-search-forward re end t)
  18857. (if (get-char-property (match-beginning 0) 'invisible)
  18858. (throw 'exit t))))
  18859. nil))))
  18860. (defun org-metaup (&optional arg)
  18861. "Move subtree up or move table row up.
  18862. Calls `org-move-subtree-up' or `org-table-move-row' or
  18863. `org-move-item-up', depending on context. See the individual commands
  18864. for more information."
  18865. (interactive "P")
  18866. (cond
  18867. ((run-hook-with-args-until-success 'org-metaup-hook))
  18868. ((org-region-active-p)
  18869. (let* ((a (min (region-beginning) (region-end)))
  18870. (b (1- (max (region-beginning) (region-end))))
  18871. (c (save-excursion (goto-char a)
  18872. (move-beginning-of-line 0)))
  18873. (d (save-excursion (goto-char a)
  18874. (move-end-of-line 0) (point))))
  18875. (transpose-regions a b c d)
  18876. (goto-char c)))
  18877. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18878. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18879. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18880. (t (org-drag-element-backward))))
  18881. (defun org-metadown (&optional arg)
  18882. "Move subtree down or move table row down.
  18883. Calls `org-move-subtree-down' or `org-table-move-row' or
  18884. `org-move-item-down', depending on context. See the individual
  18885. commands for more information."
  18886. (interactive "P")
  18887. (cond
  18888. ((run-hook-with-args-until-success 'org-metadown-hook))
  18889. ((org-region-active-p)
  18890. (let* ((a (min (region-beginning) (region-end)))
  18891. (b (max (region-beginning) (region-end)))
  18892. (c (save-excursion (goto-char b)
  18893. (move-beginning-of-line 1)))
  18894. (d (save-excursion (goto-char b)
  18895. (move-end-of-line 1) (1+ (point)))))
  18896. (transpose-regions a b c d)
  18897. (goto-char d)))
  18898. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18899. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18900. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18901. (t (org-drag-element-forward))))
  18902. (defun org-shiftup (&optional arg)
  18903. "Increase item in timestamp or increase priority of current headline.
  18904. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18905. depending on context. See the individual commands for more information."
  18906. (interactive "P")
  18907. (cond
  18908. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18909. ((and org-support-shift-select (org-region-active-p))
  18910. (org-call-for-shift-select 'previous-line))
  18911. ((org-at-timestamp-p t)
  18912. (call-interactively (if org-edit-timestamp-down-means-later
  18913. 'org-timestamp-down 'org-timestamp-up)))
  18914. ((and (not (eq org-support-shift-select 'always))
  18915. org-enable-priority-commands
  18916. (org-at-heading-p))
  18917. (call-interactively 'org-priority-up))
  18918. ((and (not org-support-shift-select) (org-at-item-p))
  18919. (call-interactively 'org-previous-item))
  18920. ((org-clocktable-try-shift 'up arg))
  18921. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18922. (org-support-shift-select
  18923. (org-call-for-shift-select 'previous-line))
  18924. (t (org-shiftselect-error))))
  18925. (defun org-shiftdown (&optional arg)
  18926. "Decrease item in timestamp or decrease priority of current headline.
  18927. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18928. depending on context. See the individual commands for more information."
  18929. (interactive "P")
  18930. (cond
  18931. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18932. ((and org-support-shift-select (org-region-active-p))
  18933. (org-call-for-shift-select 'next-line))
  18934. ((org-at-timestamp-p t)
  18935. (call-interactively (if org-edit-timestamp-down-means-later
  18936. 'org-timestamp-up 'org-timestamp-down)))
  18937. ((and (not (eq org-support-shift-select 'always))
  18938. org-enable-priority-commands
  18939. (org-at-heading-p))
  18940. (call-interactively 'org-priority-down))
  18941. ((and (not org-support-shift-select) (org-at-item-p))
  18942. (call-interactively 'org-next-item))
  18943. ((org-clocktable-try-shift 'down arg))
  18944. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18945. (org-support-shift-select
  18946. (org-call-for-shift-select 'next-line))
  18947. (t (org-shiftselect-error))))
  18948. (defun org-shiftright (&optional arg)
  18949. "Cycle the thing at point or in the current line, depending on context.
  18950. Depending on context, this does one of the following:
  18951. - switch a timestamp at point one day into the future
  18952. - on a headline, switch to the next TODO keyword.
  18953. - on an item, switch entire list to the next bullet type
  18954. - on a property line, switch to the next allowed value
  18955. - on a clocktable definition line, move time block into the future"
  18956. (interactive "P")
  18957. (cond
  18958. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18959. ((and org-support-shift-select (org-region-active-p))
  18960. (org-call-for-shift-select 'forward-char))
  18961. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18962. ((and (not (eq org-support-shift-select 'always))
  18963. (org-at-heading-p))
  18964. (let ((org-inhibit-logging
  18965. (not org-treat-S-cursor-todo-selection-as-state-change))
  18966. (org-inhibit-blocking
  18967. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18968. (org-call-with-arg 'org-todo 'right)))
  18969. ((or (and org-support-shift-select
  18970. (not (eq org-support-shift-select 'always))
  18971. (org-at-item-bullet-p))
  18972. (and (not org-support-shift-select) (org-at-item-p)))
  18973. (org-call-with-arg 'org-cycle-list-bullet nil))
  18974. ((and (not (eq org-support-shift-select 'always))
  18975. (org-at-property-p))
  18976. (call-interactively 'org-property-next-allowed-value))
  18977. ((org-clocktable-try-shift 'right arg))
  18978. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18979. (org-support-shift-select
  18980. (org-call-for-shift-select 'forward-char))
  18981. (t (org-shiftselect-error))))
  18982. (defun org-shiftleft (&optional arg)
  18983. "Cycle the thing at point or in the current line, depending on context.
  18984. Depending on context, this does one of the following:
  18985. - switch a timestamp at point one day into the past
  18986. - on a headline, switch to the previous TODO keyword.
  18987. - on an item, switch entire list to the previous bullet type
  18988. - on a property line, switch to the previous allowed value
  18989. - on a clocktable definition line, move time block into the past"
  18990. (interactive "P")
  18991. (cond
  18992. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18993. ((and org-support-shift-select (org-region-active-p))
  18994. (org-call-for-shift-select 'backward-char))
  18995. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18996. ((and (not (eq org-support-shift-select 'always))
  18997. (org-at-heading-p))
  18998. (let ((org-inhibit-logging
  18999. (not org-treat-S-cursor-todo-selection-as-state-change))
  19000. (org-inhibit-blocking
  19001. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19002. (org-call-with-arg 'org-todo 'left)))
  19003. ((or (and org-support-shift-select
  19004. (not (eq org-support-shift-select 'always))
  19005. (org-at-item-bullet-p))
  19006. (and (not org-support-shift-select) (org-at-item-p)))
  19007. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19008. ((and (not (eq org-support-shift-select 'always))
  19009. (org-at-property-p))
  19010. (call-interactively 'org-property-previous-allowed-value))
  19011. ((org-clocktable-try-shift 'left arg))
  19012. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19013. (org-support-shift-select
  19014. (org-call-for-shift-select 'backward-char))
  19015. (t (org-shiftselect-error))))
  19016. (defun org-shiftcontrolright ()
  19017. "Switch to next TODO set."
  19018. (interactive)
  19019. (cond
  19020. ((and org-support-shift-select (org-region-active-p))
  19021. (org-call-for-shift-select 'forward-word))
  19022. ((and (not (eq org-support-shift-select 'always))
  19023. (org-at-heading-p))
  19024. (org-call-with-arg 'org-todo 'nextset))
  19025. (org-support-shift-select
  19026. (org-call-for-shift-select 'forward-word))
  19027. (t (org-shiftselect-error))))
  19028. (defun org-shiftcontrolleft ()
  19029. "Switch to previous TODO set."
  19030. (interactive)
  19031. (cond
  19032. ((and org-support-shift-select (org-region-active-p))
  19033. (org-call-for-shift-select 'backward-word))
  19034. ((and (not (eq org-support-shift-select 'always))
  19035. (org-at-heading-p))
  19036. (org-call-with-arg 'org-todo 'previousset))
  19037. (org-support-shift-select
  19038. (org-call-for-shift-select 'backward-word))
  19039. (t (org-shiftselect-error))))
  19040. (defun org-shiftcontrolup (&optional n)
  19041. "Change timestamps synchronously up in CLOCK log lines.
  19042. Optional argument N tells to change by that many units."
  19043. (interactive "P")
  19044. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19045. (let (org-support-shift-select)
  19046. (org-clock-timestamps-up n))
  19047. (user-error "Not at a clock log")))
  19048. (defun org-shiftcontroldown (&optional n)
  19049. "Change timestamps synchronously down in CLOCK log lines.
  19050. Optional argument N tells to change by that many units."
  19051. (interactive "P")
  19052. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19053. (let (org-support-shift-select)
  19054. (org-clock-timestamps-down n))
  19055. (user-error "Not at a clock log")))
  19056. (defun org-increase-number-at-point (&optional inc)
  19057. "Increment the number at point.
  19058. With an optional prefix numeric argument INC, increment using
  19059. this numeric value."
  19060. (interactive "p")
  19061. (if (not (number-at-point))
  19062. (user-error "Not on a number")
  19063. (unless inc (setq inc 1))
  19064. (let ((pos (point))
  19065. (beg (skip-chars-backward "-+^/*0-9eE."))
  19066. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19067. (setq nap (buffer-substring-no-properties
  19068. (+ pos beg) (+ pos beg end)))
  19069. (delete-region (+ pos beg) (+ pos beg end))
  19070. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19071. (when (org-at-table-p)
  19072. (org-table-align)
  19073. (org-table-end-of-field 1))))
  19074. (defun org-decrease-number-at-point (&optional inc)
  19075. "Decrement the number at point.
  19076. With an optional prefix numeric argument INC, decrement using
  19077. this numeric value."
  19078. (interactive "p")
  19079. (org-increase-number-at-point (- inc)))
  19080. (defun org-ctrl-c-ret ()
  19081. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19082. (interactive)
  19083. (cond
  19084. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19085. (t (call-interactively 'org-insert-heading))))
  19086. (defun org-find-visible ()
  19087. (let ((s (point)))
  19088. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19089. (get-char-property s 'invisible)))
  19090. s))
  19091. (defun org-find-invisible ()
  19092. (let ((s (point)))
  19093. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19094. (not (get-char-property s 'invisible))))
  19095. s))
  19096. (defun org-copy-visible (beg end)
  19097. "Copy the visible parts of the region."
  19098. (interactive "r")
  19099. (let (snippets s)
  19100. (save-excursion
  19101. (save-restriction
  19102. (narrow-to-region beg end)
  19103. (setq s (goto-char (point-min)))
  19104. (while (not (= (point) (point-max)))
  19105. (goto-char (org-find-invisible))
  19106. (push (buffer-substring s (point)) snippets)
  19107. (setq s (goto-char (org-find-visible))))))
  19108. (kill-new (apply 'concat (nreverse snippets)))))
  19109. (defun org-copy-special ()
  19110. "Copy region in table or copy current subtree.
  19111. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19112. See the individual commands for more information."
  19113. (interactive)
  19114. (call-interactively
  19115. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19116. (defun org-cut-special ()
  19117. "Cut region in table or cut current subtree.
  19118. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19119. See the individual commands for more information."
  19120. (interactive)
  19121. (call-interactively
  19122. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19123. (defun org-paste-special (arg)
  19124. "Paste rectangular region into table, or past subtree relative to level.
  19125. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19126. See the individual commands for more information."
  19127. (interactive "P")
  19128. (if (org-at-table-p)
  19129. (org-table-paste-rectangle)
  19130. (org-paste-subtree arg)))
  19131. (defsubst org-in-fixed-width-region-p ()
  19132. "Is point in a fixed-width region?"
  19133. (save-match-data
  19134. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19135. (defun org-edit-special (&optional arg)
  19136. "Call a special editor for the element at point.
  19137. When at a table, call the formula editor with `org-table-edit-formulas'.
  19138. When in a source code block, call `org-edit-src-code'.
  19139. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19140. When in an export block, call `org-edit-export-block'.
  19141. When at an #+INCLUDE keyword, visit the included file.
  19142. When at a footnote reference, call `org-edit-footnote-reference'
  19143. On a link, call `ffap' to visit the link at point.
  19144. Otherwise, return a user error."
  19145. (interactive "P")
  19146. (let ((element (org-element-at-point)))
  19147. (assert (not buffer-read-only) nil
  19148. "Buffer is read-only: %s" (buffer-name))
  19149. (case (org-element-type element)
  19150. (src-block
  19151. (if (not arg) (org-edit-src-code)
  19152. (let* ((info (org-babel-get-src-block-info))
  19153. (lang (nth 0 info))
  19154. (params (nth 2 info))
  19155. (session (cdr (assq :session params))))
  19156. (if (not session) (org-edit-src-code)
  19157. ;; At a src-block with a session and function called with
  19158. ;; an ARG: switch to the buffer related to the inferior
  19159. ;; process.
  19160. (switch-to-buffer
  19161. (funcall (intern (concat "org-babel-prep-session:" lang))
  19162. session params))))))
  19163. (keyword
  19164. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19165. (org-open-link-from-string
  19166. (format "[[%s]]"
  19167. (expand-file-name
  19168. (let ((value (org-element-property :value element)))
  19169. (cond ((not (org-string-nw-p value))
  19170. (user-error "No file to edit"))
  19171. ((string-match "\\`\"\\(.*?\\)\"" value)
  19172. (match-string 1 value))
  19173. ((string-match "\\`[^ \t\"]\\S-*" value)
  19174. (match-string 0 value))
  19175. (t (user-error "No valid file specified")))))))
  19176. (user-error "No special environment to edit here")))
  19177. (table
  19178. (if (eq (org-element-property :type element) 'table.el)
  19179. (org-edit-table.el)
  19180. (call-interactively 'org-table-edit-formulas)))
  19181. ;; Only Org tables contain `table-row' type elements.
  19182. (table-row (call-interactively 'org-table-edit-formulas))
  19183. (example-block (org-edit-src-code))
  19184. (export-block (org-edit-export-block))
  19185. (fixed-width (org-edit-fixed-width-region))
  19186. (otherwise
  19187. ;; No notable element at point. Though, we may be at a link or
  19188. ;; a footnote reference, which are objects. Thus, scan deeper.
  19189. (let ((context (org-element-context element)))
  19190. (case (org-element-type context)
  19191. (link (call-interactively #'ffap))
  19192. (footnote-reference (org-edit-footnote-reference))
  19193. (t (user-error "No special environment to edit here"))))))))
  19194. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19195. (defun org-ctrl-c-ctrl-c (&optional arg)
  19196. "Set tags in headline, or update according to changed information at point.
  19197. This command does many different things, depending on context:
  19198. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19199. this is what we do.
  19200. - If the cursor is on a statistics cookie, update it.
  19201. - If the cursor is in a headline, prompt for tags and insert them
  19202. into the current line, aligned to `org-tags-column'. When called
  19203. with prefix arg, realign all tags in the current buffer.
  19204. - If the cursor is in one of the special #+KEYWORD lines, this
  19205. triggers scanning the buffer for these lines and updating the
  19206. information.
  19207. - If the cursor is inside a table, realign the table. This command
  19208. works even if the automatic table editor has been turned off.
  19209. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19210. the entire table.
  19211. - If the cursor is at a footnote reference or definition, jump to
  19212. the corresponding definition or references, respectively.
  19213. - If the cursor is a the beginning of a dynamic block, update it.
  19214. - If the current buffer is a capture buffer, close note and file it.
  19215. - If the cursor is on a <<<target>>>, update radio targets and
  19216. corresponding links in this buffer.
  19217. - If the cursor is on a numbered item in a plain list, renumber the
  19218. ordered list.
  19219. - If the cursor is on a checkbox, toggle it.
  19220. - If the cursor is on a code block, evaluate it. The variable
  19221. `org-confirm-babel-evaluate' can be used to control prompting
  19222. before code block evaluation, by default every code block
  19223. evaluation requires confirmation. Code block evaluation can be
  19224. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19225. (interactive "P")
  19226. (cond
  19227. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19228. org-occur-highlights)
  19229. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19230. (org-remove-occur-highlights)
  19231. (message "Temporary highlights/overlays removed from current buffer"))
  19232. ((and (local-variable-p 'org-finish-function (current-buffer))
  19233. (fboundp org-finish-function))
  19234. (funcall org-finish-function))
  19235. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19236. (t
  19237. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19238. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19239. (user-error "C-c C-c can do nothing useful at this location"))
  19240. (let* ((context (org-element-context))
  19241. (type (org-element-type context)))
  19242. (case type
  19243. ;; When at a link, act according to the parent instead.
  19244. (link (setq context (org-element-property :parent context))
  19245. (setq type (org-element-type context)))
  19246. ;; Unsupported object types: refer to the first supported
  19247. ;; element or object containing it.
  19248. ((bold code entity export-snippet inline-babel-call inline-src-block
  19249. italic latex-fragment line-break macro strike-through subscript
  19250. superscript underline verbatim)
  19251. (setq context
  19252. (org-element-lineage
  19253. context '(radio-target paragraph verse-block table-cell)))))
  19254. ;; For convenience: at the first line of a paragraph on the
  19255. ;; same line as an item, apply function on that item instead.
  19256. (when (eq type 'paragraph)
  19257. (let ((parent (org-element-property :parent context)))
  19258. (when (and (eq (org-element-type parent) 'item)
  19259. (= (line-beginning-position)
  19260. (org-element-property :begin parent)))
  19261. (setq context parent type 'item))))
  19262. ;; Act according to type of element or object at point.
  19263. (case type
  19264. (clock (org-clock-update-time-maybe))
  19265. (dynamic-block
  19266. (save-excursion
  19267. (goto-char (org-element-property :post-affiliated context))
  19268. (org-update-dblock)))
  19269. (footnote-definition
  19270. (goto-char (org-element-property :post-affiliated context))
  19271. (call-interactively 'org-footnote-action))
  19272. (footnote-reference (call-interactively 'org-footnote-action))
  19273. ((headline inlinetask)
  19274. (save-excursion (goto-char (org-element-property :begin context))
  19275. (call-interactively 'org-set-tags)))
  19276. (item
  19277. ;; At an item: a double C-u set checkbox to "[-]"
  19278. ;; unconditionally, whereas a single one will toggle its
  19279. ;; presence. Without an universal argument, if the item
  19280. ;; has a checkbox, toggle it. Otherwise repair the list.
  19281. (let* ((box (org-element-property :checkbox context))
  19282. (struct (org-element-property :structure context))
  19283. (old-struct (copy-tree struct))
  19284. (parents (org-list-parents-alist struct))
  19285. (prevs (org-list-prevs-alist struct))
  19286. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19287. (org-list-set-checkbox
  19288. (org-element-property :begin context) struct
  19289. (cond ((equal arg '(16)) "[-]")
  19290. ((and (not box) (equal arg '(4))) "[ ]")
  19291. ((or (not box) (equal arg '(4))) nil)
  19292. ((eq box 'on) "[ ]")
  19293. (t "[X]")))
  19294. ;; Mimic `org-list-write-struct' but with grabbing
  19295. ;; a return value from `org-list-struct-fix-box'.
  19296. (org-list-struct-fix-ind struct parents 2)
  19297. (org-list-struct-fix-item-end struct)
  19298. (org-list-struct-fix-bul struct prevs)
  19299. (org-list-struct-fix-ind struct parents)
  19300. (let ((block-item
  19301. (org-list-struct-fix-box struct parents prevs orderedp)))
  19302. (if (and box (equal struct old-struct))
  19303. (if (equal arg '(16))
  19304. (message "Checkboxes already reset")
  19305. (user-error "Cannot toggle this checkbox: %s"
  19306. (if (eq box 'on)
  19307. "all subitems checked"
  19308. "unchecked subitems")))
  19309. (org-list-struct-apply-struct struct old-struct)
  19310. (org-update-checkbox-count-maybe))
  19311. (when block-item
  19312. (message "Checkboxes were removed due to empty box at line %d"
  19313. (org-current-line block-item))))))
  19314. (keyword
  19315. (let ((org-inhibit-startup-visibility-stuff t)
  19316. (org-startup-align-all-tables nil))
  19317. (when (boundp 'org-table-coordinate-overlays)
  19318. (mapc 'delete-overlay org-table-coordinate-overlays)
  19319. (setq org-table-coordinate-overlays nil))
  19320. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19321. (message "Local setup has been refreshed"))
  19322. (plain-list
  19323. ;; At a plain list, with a double C-u argument, set
  19324. ;; checkboxes of each item to "[-]", whereas a single one
  19325. ;; will toggle their presence according to the state of the
  19326. ;; first item in the list. Without an argument, repair the
  19327. ;; list.
  19328. (let* ((begin (org-element-property :contents-begin context))
  19329. (beginm (move-marker (make-marker) begin))
  19330. (struct (org-element-property :structure context))
  19331. (old-struct (copy-tree struct))
  19332. (first-box (save-excursion
  19333. (goto-char begin)
  19334. (looking-at org-list-full-item-re)
  19335. (match-string-no-properties 3)))
  19336. (new-box (cond ((equal arg '(16)) "[-]")
  19337. ((equal arg '(4)) (unless first-box "[ ]"))
  19338. ((equal first-box "[X]") "[ ]")
  19339. (t "[X]"))))
  19340. (cond
  19341. (arg
  19342. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19343. (org-list-get-all-items
  19344. begin struct (org-list-prevs-alist struct))))
  19345. ((and first-box (eq (point) begin))
  19346. ;; For convenience, when point is at bol on the first
  19347. ;; item of the list and no argument is provided, simply
  19348. ;; toggle checkbox of that item, if any.
  19349. (org-list-set-checkbox begin struct new-box)))
  19350. (org-list-write-struct
  19351. struct (org-list-parents-alist struct) old-struct)
  19352. (org-update-checkbox-count-maybe)
  19353. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19354. ((property-drawer node-property)
  19355. (call-interactively 'org-property-action))
  19356. ((radio-target target)
  19357. (call-interactively 'org-update-radio-target-regexp))
  19358. (statistics-cookie
  19359. (call-interactively 'org-update-statistics-cookies))
  19360. ((table table-cell table-row)
  19361. ;; At a table, recalculate every field and align it. Also
  19362. ;; send the table if necessary. If the table has
  19363. ;; a `table.el' type, just give up. At a table row or
  19364. ;; cell, maybe recalculate line but always align table.
  19365. (if (eq (org-element-property :type context) 'table.el)
  19366. (message "Use C-c ' to edit table.el tables")
  19367. (let ((org-enable-table-editor t))
  19368. (if (or (eq type 'table)
  19369. ;; Check if point is at a TBLFM line.
  19370. (and (eq type 'table-row)
  19371. (= (point) (org-element-property :end context))))
  19372. (save-excursion
  19373. (if (org-at-TBLFM-p)
  19374. (progn (require 'org-table)
  19375. (org-table-calc-current-TBLFM))
  19376. (goto-char (org-element-property :contents-begin context))
  19377. (org-call-with-arg 'org-table-recalculate (or arg t))
  19378. (orgtbl-send-table 'maybe)))
  19379. (org-table-maybe-eval-formula)
  19380. (cond (arg (call-interactively 'org-table-recalculate))
  19381. ((org-table-maybe-recalculate-line))
  19382. (t (org-table-align)))))))
  19383. (timestamp (org-timestamp-change 0 'day))
  19384. (otherwise
  19385. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19386. (user-error
  19387. "C-c C-c can do nothing useful at this location")))))))))
  19388. (defun org-mode-restart ()
  19389. (interactive)
  19390. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19391. (funcall major-mode)
  19392. (hack-local-variables)
  19393. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19394. (org-indent-mode -1)))
  19395. (message "%s restarted" major-mode))
  19396. (defun org-kill-note-or-show-branches ()
  19397. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19398. (interactive)
  19399. (if (not org-finish-function)
  19400. (progn
  19401. (hide-subtree)
  19402. (call-interactively 'show-branches))
  19403. (let ((org-note-abort t))
  19404. (funcall org-finish-function))))
  19405. (defun org-delete-indentation (&optional ARG)
  19406. "Join current line to previous and fix whitespace at join.
  19407. If previous line is a headline add to headline title. Otherwise
  19408. the function calls `delete-indentation'.
  19409. With argument, join this line to following line."
  19410. (interactive "*P")
  19411. (if (save-excursion
  19412. (if ARG (beginning-of-line)
  19413. (forward-line -1))
  19414. (looking-at org-complex-heading-regexp))
  19415. ;; At headline.
  19416. (let ((tags-column (when (match-beginning 5)
  19417. (save-excursion (goto-char (match-beginning 5))
  19418. (current-column))))
  19419. (string (concat " " (progn (when ARG (forward-line 1))
  19420. (org-trim (delete-and-extract-region
  19421. (line-beginning-position)
  19422. (line-end-position)))))))
  19423. (unless (bobp) (delete-region (point) (1- (point))))
  19424. (goto-char (or (match-end 4)
  19425. (match-beginning 5)
  19426. (match-end 0)))
  19427. (skip-chars-backward " \t")
  19428. (save-excursion (insert string))
  19429. ;; Adjust alignment of tags.
  19430. (when tags-column
  19431. (org-align-tags-here (if org-auto-align-tags
  19432. org-tags-column
  19433. tags-column))))
  19434. (delete-indentation ARG)))
  19435. (defun org-open-line (n)
  19436. "Insert a new row in tables, call `open-line' elsewhere.
  19437. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19438. (interactive "*p")
  19439. (cond
  19440. ((not org-special-ctrl-o)
  19441. (open-line n))
  19442. ((org-at-table-p)
  19443. (org-table-insert-row))
  19444. (t
  19445. (open-line n))))
  19446. (defun org-return (&optional indent)
  19447. "Goto next table row or insert a newline.
  19448. Calls `org-table-next-row' or `newline', depending on context.
  19449. When optional INDENT argument is non-nil, call
  19450. `newline-and-indent' instead of `newline'.
  19451. When `org-return-follows-link' is non-nil and point is on
  19452. a timestamp or a link, call `org-open-at-point'. However, it
  19453. will not happen if point is in a table or on a \"dead\"
  19454. object (e.g., within a comment). In these case, you need to use
  19455. `org-open-at-point' directly."
  19456. (interactive)
  19457. (if (and (not (bolp))
  19458. (save-excursion (beginning-of-line)
  19459. (looking-at org-complex-heading-regexp)))
  19460. ;; At headline.
  19461. (let ((tags-column (when (match-beginning 5)
  19462. (save-excursion (goto-char (match-beginning 5))
  19463. (current-column))))
  19464. ;; Test if before or after headline title.
  19465. (string (when (not (or (< (point)
  19466. (or (match-end 3)
  19467. (match-end 2)
  19468. (save-excursion
  19469. (goto-char (match-beginning 4))
  19470. (skip-chars-backward " \t")
  19471. (point))))
  19472. (and (match-beginning 5)
  19473. (>= (point) (match-beginning 5)))))
  19474. ;; Point is on headline keywords, tags or cookies. Do not break
  19475. ;; them: add a newline after the headline instead.
  19476. (org-string-nw-p
  19477. (delete-and-extract-region (point) (match-end 4))))))
  19478. ;; Adjust alignment of tags.
  19479. (when (and tags-column string)
  19480. (org-align-tags-here (if org-auto-align-tags
  19481. org-tags-column
  19482. tags-column)))
  19483. (end-of-line)
  19484. (org-show-entry)
  19485. (if indent (newline-and-indent) (newline))
  19486. (and string (save-excursion (insert (org-trim string)))))
  19487. (let* ((context (if org-return-follows-link (org-element-context)
  19488. (org-element-at-point)))
  19489. (type (org-element-type context)))
  19490. (cond
  19491. ;; In a table, call `org-table-next-row'.
  19492. ((or (and (eq type 'table)
  19493. (>= (point) (org-element-property :contents-begin context))
  19494. (< (point) (org-element-property :contents-end context)))
  19495. (org-element-lineage context '(table-row table-cell) t))
  19496. (org-table-justify-field-maybe)
  19497. (call-interactively #'org-table-next-row))
  19498. ;; On a link or a timestamp but not on white spaces after it,
  19499. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19500. ((and org-return-follows-link
  19501. (memq type '(link timestamp))
  19502. (< (point)
  19503. (save-excursion (goto-char (org-element-property :end context))
  19504. (skip-chars-backward " \t")
  19505. (point))))
  19506. (call-interactively #'org-open-at-point))
  19507. ;; In a list, make sure indenting keeps trailing text within.
  19508. ((and indent
  19509. (not (eolp))
  19510. (org-element-lineage context '(item)))
  19511. (let ((trailing-data
  19512. (delete-and-extract-region (point) (line-end-position))))
  19513. (newline-and-indent)
  19514. (save-excursion (insert trailing-data))))
  19515. (t (if indent (newline-and-indent) (newline)))))))
  19516. (defun org-return-indent ()
  19517. "Goto next table row or insert a newline and indent.
  19518. Calls `org-table-next-row' or `newline-and-indent', depending on
  19519. context. See the individual commands for more information."
  19520. (interactive)
  19521. (org-return t))
  19522. (defun org-ctrl-c-star ()
  19523. "Compute table, or change heading status of lines.
  19524. Calls `org-table-recalculate' or `org-toggle-heading',
  19525. depending on context."
  19526. (interactive)
  19527. (cond
  19528. ((org-at-table-p)
  19529. (call-interactively 'org-table-recalculate))
  19530. (t
  19531. ;; Convert all lines in region to list items
  19532. (call-interactively 'org-toggle-heading))))
  19533. (defun org-ctrl-c-minus ()
  19534. "Insert separator line in table or modify bullet status of line.
  19535. Also turns a plain line or a region of lines into list items.
  19536. Calls `org-table-insert-hline', `org-toggle-item', or
  19537. `org-cycle-list-bullet', depending on context."
  19538. (interactive)
  19539. (cond
  19540. ((org-at-table-p)
  19541. (call-interactively 'org-table-insert-hline))
  19542. ((org-region-active-p)
  19543. (call-interactively 'org-toggle-item))
  19544. ((org-in-item-p)
  19545. (call-interactively 'org-cycle-list-bullet))
  19546. (t
  19547. (call-interactively 'org-toggle-item))))
  19548. (defun org-toggle-item (arg)
  19549. "Convert headings or normal lines to items, items to normal lines.
  19550. If there is no active region, only the current line is considered.
  19551. If the first non blank line in the region is a headline, convert
  19552. all headlines to items, shifting text accordingly.
  19553. If it is an item, convert all items to normal lines.
  19554. If it is normal text, change region into a list of items.
  19555. With a prefix argument ARG, change the region in a single item."
  19556. (interactive "P")
  19557. (let ((shift-text
  19558. (function
  19559. ;; Shift text in current section to IND, from point to END.
  19560. ;; The function leaves point to END line.
  19561. (lambda (ind end)
  19562. (let ((min-i 1000) (end (copy-marker end)))
  19563. ;; First determine the minimum indentation (MIN-I) of
  19564. ;; the text.
  19565. (save-excursion
  19566. (catch 'exit
  19567. (while (< (point) end)
  19568. (let ((i (org-get-indentation)))
  19569. (cond
  19570. ;; Skip blank lines and inline tasks.
  19571. ((looking-at "^[ \t]*$"))
  19572. ((looking-at org-outline-regexp-bol))
  19573. ;; We can't find less than 0 indentation.
  19574. ((zerop i) (throw 'exit (setq min-i 0)))
  19575. ((< i min-i) (setq min-i i))))
  19576. (forward-line))))
  19577. ;; Then indent each line so that a line indented to
  19578. ;; MIN-I becomes indented to IND. Ignore blank lines
  19579. ;; and inline tasks in the process.
  19580. (let ((delta (- ind min-i)))
  19581. (while (< (point) end)
  19582. (unless (or (looking-at "^[ \t]*$")
  19583. (looking-at org-outline-regexp-bol))
  19584. (org-indent-line-to (+ (org-get-indentation) delta)))
  19585. (forward-line)))))))
  19586. (skip-blanks
  19587. (function
  19588. ;; Return beginning of first non-blank line, starting from
  19589. ;; line at POS.
  19590. (lambda (pos)
  19591. (save-excursion
  19592. (goto-char pos)
  19593. (skip-chars-forward " \r\t\n")
  19594. (point-at-bol)))))
  19595. beg end)
  19596. ;; Determine boundaries of changes.
  19597. (if (org-region-active-p)
  19598. (setq beg (funcall skip-blanks (region-beginning))
  19599. end (copy-marker (region-end)))
  19600. (setq beg (funcall skip-blanks (point-at-bol))
  19601. end (copy-marker (point-at-eol))))
  19602. ;; Depending on the starting line, choose an action on the text
  19603. ;; between BEG and END.
  19604. (org-with-limited-levels
  19605. (save-excursion
  19606. (goto-char beg)
  19607. (cond
  19608. ;; Case 1. Start at an item: de-itemize. Note that it only
  19609. ;; happens when a region is active: `org-ctrl-c-minus'
  19610. ;; would call `org-cycle-list-bullet' otherwise.
  19611. ((org-at-item-p)
  19612. (while (< (point) end)
  19613. (when (org-at-item-p)
  19614. (skip-chars-forward " \t")
  19615. (delete-region (point) (match-end 0)))
  19616. (forward-line)))
  19617. ;; Case 2. Start at an heading: convert to items.
  19618. ((org-at-heading-p)
  19619. (let* ((bul (org-list-bullet-string "-"))
  19620. (bul-len (length bul))
  19621. (done (org-entry-is-done-p))
  19622. (todo (org-entry-is-todo-p))
  19623. ;; Indentation of the first heading. It should be
  19624. ;; relative to the indentation of its parent, if any.
  19625. (start-ind (save-excursion
  19626. (cond
  19627. ((not org-adapt-indentation) 0)
  19628. ((not (outline-previous-heading)) 0)
  19629. (t (length (match-string 0))))))
  19630. ;; Level of first heading. Further headings will be
  19631. ;; compared to it to determine hierarchy in the list.
  19632. (ref-level (org-reduced-level (org-outline-level))))
  19633. (when (or done todo) (org-todo ""))
  19634. (while (< (point) end)
  19635. (let* ((level (org-reduced-level (org-outline-level)))
  19636. (delta (max 0 (- level ref-level))))
  19637. ;; If current headline is less indented than the first
  19638. ;; one, set it as reference, in order to preserve
  19639. ;; subtrees.
  19640. (when (< level ref-level) (setq ref-level level))
  19641. (replace-match bul t t)
  19642. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19643. (when (or done todo)
  19644. (let* ((struct (org-list-struct))
  19645. (old (copy-tree struct)))
  19646. (org-list-set-checkbox (line-beginning-position)
  19647. struct
  19648. (if done "[X]" "[ ]"))
  19649. (org-list-write-struct struct
  19650. (org-list-parents-alist struct)
  19651. old)))
  19652. ;; Ensure all text down to END (or SECTION-END) belongs
  19653. ;; to the newly created item.
  19654. (let ((section-end (save-excursion
  19655. (or (outline-next-heading) (point)))))
  19656. (forward-line)
  19657. (funcall shift-text
  19658. (+ start-ind (* (1+ delta) bul-len))
  19659. (min end section-end)))))))
  19660. ;; Case 3. Normal line with ARG: make the first line of region
  19661. ;; an item, and shift indentation of others lines to
  19662. ;; set them as item's body.
  19663. (arg (let* ((bul (org-list-bullet-string "-"))
  19664. (bul-len (length bul))
  19665. (ref-ind (org-get-indentation)))
  19666. (skip-chars-forward " \t")
  19667. (insert bul)
  19668. (forward-line)
  19669. (while (< (point) end)
  19670. ;; Ensure that lines less indented than first one
  19671. ;; still get included in item body.
  19672. (funcall shift-text
  19673. (+ ref-ind bul-len)
  19674. (min end (save-excursion (or (outline-next-heading)
  19675. (point)))))
  19676. (forward-line))))
  19677. ;; Case 4. Normal line without ARG: turn each non-item line
  19678. ;; into an item.
  19679. (t
  19680. (while (< (point) end)
  19681. (unless (or (org-at-heading-p) (org-at-item-p))
  19682. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19683. (replace-match
  19684. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19685. (forward-line))))))))
  19686. (defun org-toggle-heading (&optional nstars)
  19687. "Convert headings to normal text, or items or text to headings.
  19688. If there is no active region, only convert the current line.
  19689. With a \\[universal-argument] prefix, convert the whole list at
  19690. point into heading.
  19691. In a region:
  19692. - If the first non blank line is a headline, remove the stars
  19693. from all headlines in the region.
  19694. - If it is a normal line, turn each and every normal line (i.e.,
  19695. not an heading or an item) in the region into headings. If you
  19696. want to convert only the first line of this region, use one
  19697. universal prefix argument.
  19698. - If it is a plain list item, turn all plain list items into headings.
  19699. When converting a line into a heading, the number of stars is chosen
  19700. such that the lines become children of the current entry. However,
  19701. when a numeric prefix argument is given, its value determines the
  19702. number of stars to add."
  19703. (interactive "P")
  19704. (let ((skip-blanks
  19705. (function
  19706. ;; Return beginning of first non-blank line, starting from
  19707. ;; line at POS.
  19708. (lambda (pos)
  19709. (save-excursion
  19710. (goto-char pos)
  19711. (while (org-at-comment-p) (forward-line))
  19712. (skip-chars-forward " \r\t\n")
  19713. (point-at-bol)))))
  19714. beg end toggled)
  19715. ;; Determine boundaries of changes. If a universal prefix has
  19716. ;; been given, put the list in a region. If region ends at a bol,
  19717. ;; do not consider the last line to be in the region.
  19718. (when (and current-prefix-arg (org-at-item-p))
  19719. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19720. (org-mark-element))
  19721. (if (org-region-active-p)
  19722. (setq beg (funcall skip-blanks (region-beginning))
  19723. end (copy-marker (save-excursion
  19724. (goto-char (region-end))
  19725. (if (bolp) (point) (point-at-eol)))))
  19726. (setq beg (funcall skip-blanks (point-at-bol))
  19727. end (copy-marker (point-at-eol))))
  19728. ;; Ensure inline tasks don't count as headings.
  19729. (org-with-limited-levels
  19730. (save-excursion
  19731. (goto-char beg)
  19732. (cond
  19733. ;; Case 1. Started at an heading: de-star headings.
  19734. ((org-at-heading-p)
  19735. (while (< (point) end)
  19736. (when (org-at-heading-p t)
  19737. (looking-at org-outline-regexp) (replace-match "")
  19738. (setq toggled t))
  19739. (forward-line)))
  19740. ;; Case 2. Started at an item: change items into headlines.
  19741. ;; One star will be added by `org-list-to-subtree'.
  19742. ((org-at-item-p)
  19743. (while (< (point) end)
  19744. (when (org-at-item-p)
  19745. ;; Pay attention to cases when region ends before list.
  19746. (let* ((struct (org-list-struct))
  19747. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19748. (save-restriction
  19749. (narrow-to-region (point) list-end)
  19750. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19751. (setq toggled t))
  19752. (forward-line)))
  19753. ;; Case 3. Started at normal text: make every line an heading,
  19754. ;; skipping headlines and items.
  19755. (t (let* ((stars
  19756. (make-string
  19757. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19758. (add-stars
  19759. (cond (nstars "") ; stars from prefix only
  19760. ((equal stars "") "*") ; before first heading
  19761. (org-odd-levels-only "**") ; inside heading, odd
  19762. (t "*"))) ; inside heading, oddeven
  19763. (rpl (concat stars add-stars " "))
  19764. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19765. (while (< (point) (if (equal nstars '(4)) lend end))
  19766. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19767. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19768. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19769. (forward-line)))))))
  19770. (unless toggled (message "Cannot toggle heading from here"))))
  19771. (defun org-meta-return (&optional arg)
  19772. "Insert a new heading or wrap a region in a table.
  19773. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19774. on context. See the individual commands for more information."
  19775. (interactive "P")
  19776. (org-check-before-invisible-edit 'insert)
  19777. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19778. (let* ((element (org-element-at-point))
  19779. (type (org-element-type element)))
  19780. (when (eq type 'table-row)
  19781. (setq element (org-element-property :parent element))
  19782. (setq type 'table))
  19783. (if (and (eq type 'table)
  19784. (eq (org-element-property :type element) 'org)
  19785. (>= (point) (org-element-property :contents-begin element))
  19786. (< (point) (org-element-property :contents-end element)))
  19787. (call-interactively 'org-table-wrap-region)
  19788. (call-interactively 'org-insert-heading)))))
  19789. ;;; Menu entries
  19790. (defsubst org-in-subtree-not-table-p ()
  19791. "Are we in a subtree and not in a table?"
  19792. (and (not (org-before-first-heading-p))
  19793. (not (org-at-table-p))))
  19794. ;; Define the Org-mode menus
  19795. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19796. '("Tbl"
  19797. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19798. ["Next Field" org-cycle (org-at-table-p)]
  19799. ["Previous Field" org-shifttab (org-at-table-p)]
  19800. ["Next Row" org-return (org-at-table-p)]
  19801. "--"
  19802. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19803. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19804. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19805. "--"
  19806. ("Column"
  19807. ["Move Column Left" org-metaleft (org-at-table-p)]
  19808. ["Move Column Right" org-metaright (org-at-table-p)]
  19809. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19810. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19811. ("Row"
  19812. ["Move Row Up" org-metaup (org-at-table-p)]
  19813. ["Move Row Down" org-metadown (org-at-table-p)]
  19814. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19815. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19816. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19817. "--"
  19818. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19819. ("Rectangle"
  19820. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19821. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19822. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19823. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19824. "--"
  19825. ("Calculate"
  19826. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19827. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19828. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19829. "--"
  19830. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19831. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19832. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19833. "--"
  19834. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19835. "--"
  19836. ["Sum Column/Rectangle" org-table-sum
  19837. (or (org-at-table-p) (org-region-active-p))]
  19838. ["Which Column?" org-table-current-column (org-at-table-p)])
  19839. ["Debug Formulas"
  19840. org-table-toggle-formula-debugger
  19841. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19842. ["Show Col/Row Numbers"
  19843. org-table-toggle-coordinate-overlays
  19844. :style toggle
  19845. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19846. "--"
  19847. ["Create" org-table-create (and (not (org-at-table-p))
  19848. org-enable-table-editor)]
  19849. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19850. ["Import from File" org-table-import (not (org-at-table-p))]
  19851. ["Export to File" org-table-export (org-at-table-p)]
  19852. "--"
  19853. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19854. "--"
  19855. ("Plot"
  19856. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19857. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19858. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19859. '("Org"
  19860. ("Show/Hide"
  19861. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19862. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19863. ["Sparse Tree..." org-sparse-tree t]
  19864. ["Reveal Context" org-reveal t]
  19865. ["Show All" show-all t]
  19866. "--"
  19867. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19868. "--"
  19869. ["New Heading" org-insert-heading t]
  19870. ("Navigate Headings"
  19871. ["Up" outline-up-heading t]
  19872. ["Next" outline-next-visible-heading t]
  19873. ["Previous" outline-previous-visible-heading t]
  19874. ["Next Same Level" outline-forward-same-level t]
  19875. ["Previous Same Level" outline-backward-same-level t]
  19876. "--"
  19877. ["Jump" org-goto t])
  19878. ("Edit Structure"
  19879. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19880. "--"
  19881. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19882. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19883. "--"
  19884. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19885. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19886. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19887. "--"
  19888. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19889. "--"
  19890. ["Copy visible text" org-copy-visible t]
  19891. "--"
  19892. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19893. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19894. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19895. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19896. "--"
  19897. ["Sort Region/Children" org-sort t]
  19898. "--"
  19899. ["Convert to odd levels" org-convert-to-odd-levels t]
  19900. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19901. ("Editing"
  19902. ["Emphasis..." org-emphasize t]
  19903. ["Edit Source Example" org-edit-special t]
  19904. "--"
  19905. ["Footnote new/jump" org-footnote-action t]
  19906. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19907. ("Archive"
  19908. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19909. "--"
  19910. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19911. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19912. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19913. )
  19914. "--"
  19915. ("Hyperlinks"
  19916. ["Store Link (Global)" org-store-link t]
  19917. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19918. ["Insert Link" org-insert-link t]
  19919. ["Follow Link" org-open-at-point t]
  19920. "--"
  19921. ["Next link" org-next-link t]
  19922. ["Previous link" org-previous-link t]
  19923. "--"
  19924. ["Descriptive Links"
  19925. org-toggle-link-display
  19926. :style radio
  19927. :selected org-descriptive-links
  19928. ]
  19929. ["Literal Links"
  19930. org-toggle-link-display
  19931. :style radio
  19932. :selected (not org-descriptive-links)])
  19933. "--"
  19934. ("TODO Lists"
  19935. ["TODO/DONE/-" org-todo t]
  19936. ("Select keyword"
  19937. ["Next keyword" org-shiftright (org-at-heading-p)]
  19938. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19939. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19940. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19941. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19942. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19943. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19944. "--"
  19945. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19946. :selected org-enforce-todo-dependencies :style toggle :active t]
  19947. "Settings for tree at point"
  19948. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19949. :selected (org-entry-get nil "ORDERED")
  19950. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19951. ["Do Children parallel" org-toggle-ordered-property :style radio
  19952. :selected (not (org-entry-get nil "ORDERED"))
  19953. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19954. "--"
  19955. ["Set Priority" org-priority t]
  19956. ["Priority Up" org-shiftup t]
  19957. ["Priority Down" org-shiftdown t]
  19958. "--"
  19959. ["Get news from all feeds" org-feed-update-all t]
  19960. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19961. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19962. ("TAGS and Properties"
  19963. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19964. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19965. "--"
  19966. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19967. ["Column view of properties" org-columns t]
  19968. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19969. ("Dates and Scheduling"
  19970. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19971. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19972. ("Change Date"
  19973. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19974. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19975. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19976. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19977. ["Compute Time Range" org-evaluate-time-range t]
  19978. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19979. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19980. "--"
  19981. ["Custom time format" org-toggle-time-stamp-overlays
  19982. :style radio :selected org-display-custom-times]
  19983. "--"
  19984. ["Goto Calendar" org-goto-calendar t]
  19985. ["Date from Calendar" org-date-from-calendar t]
  19986. "--"
  19987. ["Start/Restart Timer" org-timer-start t]
  19988. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19989. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19990. ["Insert Timer String" org-timer t]
  19991. ["Insert Timer Item" org-timer-item t])
  19992. ("Logging work"
  19993. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19994. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19995. ["Clock out" org-clock-out t]
  19996. ["Clock cancel" org-clock-cancel t]
  19997. "--"
  19998. ["Mark as default task" org-clock-mark-default-task t]
  19999. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20000. ["Goto running clock" org-clock-goto t]
  20001. "--"
  20002. ["Display times" org-clock-display t]
  20003. ["Create clock table" org-clock-report t]
  20004. "--"
  20005. ["Record DONE time"
  20006. (progn (setq org-log-done (not org-log-done))
  20007. (message "Switching to %s will %s record a timestamp"
  20008. (car org-done-keywords)
  20009. (if org-log-done "automatically" "not")))
  20010. :style toggle :selected org-log-done])
  20011. "--"
  20012. ["Agenda Command..." org-agenda t]
  20013. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20014. ("File List for Agenda")
  20015. ("Special views current file"
  20016. ["TODO Tree" org-show-todo-tree t]
  20017. ["Check Deadlines" org-check-deadlines t]
  20018. ["Timeline" org-timeline t]
  20019. ["Tags/Property tree" org-match-sparse-tree t])
  20020. "--"
  20021. ["Export/Publish..." org-export-dispatch t]
  20022. ("LaTeX"
  20023. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20024. :selected org-cdlatex-mode]
  20025. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20026. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20027. ["Modify math symbol" org-cdlatex-math-modify
  20028. (org-inside-LaTeX-fragment-p)]
  20029. ["Insert citation" org-reftex-citation t]
  20030. "--"
  20031. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20032. "--"
  20033. ("MobileOrg"
  20034. ["Push Files and Views" org-mobile-push t]
  20035. ["Get Captured and Flagged" org-mobile-pull t]
  20036. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20037. "--"
  20038. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20039. "--"
  20040. ("Documentation"
  20041. ["Show Version" org-version t]
  20042. ["Info Documentation" org-info t])
  20043. ("Customize"
  20044. ["Browse Org Group" org-customize t]
  20045. "--"
  20046. ["Expand This Menu" org-create-customize-menu
  20047. (fboundp 'customize-menu-create)])
  20048. ["Send bug report" org-submit-bug-report t]
  20049. "--"
  20050. ("Refresh/Reload"
  20051. ["Refresh setup current buffer" org-mode-restart t]
  20052. ["Reload Org (after update)" org-reload t]
  20053. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20054. ))
  20055. (defun org-info (&optional node)
  20056. "Read documentation for Org-mode in the info system.
  20057. With optional NODE, go directly to that node."
  20058. (interactive)
  20059. (info (format "(org)%s" (or node ""))))
  20060. ;;;###autoload
  20061. (defun org-submit-bug-report ()
  20062. "Submit a bug report on Org-mode via mail.
  20063. Don't hesitate to report any problems or inaccurate documentation.
  20064. If you don't have setup sending mail from (X)Emacs, please copy the
  20065. output buffer into your mail program, as it gives us important
  20066. information about your Org-mode version and configuration."
  20067. (interactive)
  20068. (require 'reporter)
  20069. (org-load-modules-maybe)
  20070. (org-require-autoloaded-modules)
  20071. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20072. (reporter-submit-bug-report
  20073. "emacs-orgmode@gnu.org"
  20074. (org-version nil 'full)
  20075. (let (list)
  20076. (save-window-excursion
  20077. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20078. (delete-other-windows)
  20079. (erase-buffer)
  20080. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20081. We would like to add your full Org-mode and Outline configuration to the
  20082. bug report. This greatly simplifies the work of the maintainer and
  20083. other experts on the mailing list.
  20084. HOWEVER, some variables you have customized may contain private
  20085. information. The names of customers, colleagues, or friends, might
  20086. appear in the form of file names, tags, todo states, or search strings.
  20087. If you answer yes to the prompt, you might want to check and remove
  20088. such private information before sending the email.")
  20089. (add-text-properties (point-min) (point-max) '(face org-warning))
  20090. (when (yes-or-no-p "Include your Org-mode configuration ")
  20091. (mapatoms
  20092. (lambda (v)
  20093. (and (boundp v)
  20094. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20095. (or (and (symbol-value v)
  20096. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20097. (and
  20098. (get v 'custom-type) (get v 'standard-value)
  20099. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20100. (push v list)))))
  20101. (kill-buffer (get-buffer "*Warn about privacy*"))
  20102. list))
  20103. nil nil
  20104. "Remember to cover the basics, that is, what you expected to happen and
  20105. what in fact did happen. You don't know how to make a good report? See
  20106. http://orgmode.org/manual/Feedback.html#Feedback
  20107. Your bug report will be posted to the Org-mode mailing list.
  20108. ------------------------------------------------------------------------")
  20109. (save-excursion
  20110. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20111. (replace-match "\\1Bug: \\3 [\\2]")))))
  20112. (defun org-install-agenda-files-menu ()
  20113. (let ((bl (buffer-list)))
  20114. (save-excursion
  20115. (while bl
  20116. (set-buffer (pop bl))
  20117. (if (derived-mode-p 'org-mode) (setq bl nil)))
  20118. (when (derived-mode-p 'org-mode)
  20119. (easy-menu-change
  20120. '("Org") "File List for Agenda"
  20121. (append
  20122. (list
  20123. ["Edit File List" (org-edit-agenda-file-list) t]
  20124. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20125. ["Remove Current File from List" org-remove-file t]
  20126. ["Cycle through agenda files" org-cycle-agenda-files t]
  20127. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20128. "--")
  20129. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20130. ;;;; Documentation
  20131. (defun org-require-autoloaded-modules ()
  20132. (interactive)
  20133. (mapc 'require
  20134. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20135. org-table org-timer)))
  20136. ;;;###autoload
  20137. (defun org-reload (&optional uncompiled)
  20138. "Reload all org lisp files.
  20139. With prefix arg UNCOMPILED, load the uncompiled versions."
  20140. (interactive "P")
  20141. (require 'loadhist)
  20142. (let* ((org-dir (org-find-library-dir "org"))
  20143. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20144. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20145. (remove-re (mapconcat 'identity
  20146. (mapcar (lambda (f) (concat "^" f "$"))
  20147. (list (if (featurep 'xemacs)
  20148. "org-colview"
  20149. "org-colview-xemacs")
  20150. "org" "org-loaddefs" "org-version"))
  20151. "\\|"))
  20152. (feats (delete-dups
  20153. (mapcar 'file-name-sans-extension
  20154. (mapcar 'file-name-nondirectory
  20155. (delq nil
  20156. (mapcar 'feature-file
  20157. features))))))
  20158. (lfeat (append
  20159. (sort
  20160. (setq feats
  20161. (delq nil (mapcar
  20162. (lambda (f)
  20163. (if (and (string-match feature-re f)
  20164. (not (string-match remove-re f)))
  20165. f nil))
  20166. feats)))
  20167. 'string-lessp)
  20168. (list "org-version" "org")))
  20169. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20170. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20171. load-uncore load-misses)
  20172. (setq load-misses
  20173. (delq 't
  20174. (mapcar (lambda (f)
  20175. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20176. (and (string= org-dir contrib-dir)
  20177. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20178. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20179. (add-to-list 'load-uncore f 'append)
  20180. 't)
  20181. f))
  20182. lfeat)))
  20183. (if load-uncore
  20184. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20185. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20186. (if load-misses
  20187. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20188. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20189. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20190. ;;;###autoload
  20191. (defun org-customize ()
  20192. "Call the customize function with org as argument."
  20193. (interactive)
  20194. (org-load-modules-maybe)
  20195. (org-require-autoloaded-modules)
  20196. (customize-browse 'org))
  20197. (defun org-create-customize-menu ()
  20198. "Create a full customization menu for Org-mode, insert it into the menu."
  20199. (interactive)
  20200. (org-load-modules-maybe)
  20201. (org-require-autoloaded-modules)
  20202. (if (fboundp 'customize-menu-create)
  20203. (progn
  20204. (easy-menu-change
  20205. '("Org") "Customize"
  20206. `(["Browse Org group" org-customize t]
  20207. "--"
  20208. ,(customize-menu-create 'org)
  20209. ["Set" Custom-set t]
  20210. ["Save" Custom-save t]
  20211. ["Reset to Current" Custom-reset-current t]
  20212. ["Reset to Saved" Custom-reset-saved t]
  20213. ["Reset to Standard Settings" Custom-reset-standard t]))
  20214. (message "\"Org\"-menu now contains full customization menu"))
  20215. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20216. ;;;; Miscellaneous stuff
  20217. ;;; Generally useful functions
  20218. (defsubst org-get-at-eol (property n)
  20219. "Get text property PROPERTY at the end of line less N characters."
  20220. (get-text-property (- (point-at-eol) n) property))
  20221. (defun org-find-text-property-in-string (prop s)
  20222. "Return the first non-nil value of property PROP in string S."
  20223. (or (get-text-property 0 prop s)
  20224. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20225. prop s)))
  20226. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20227. "Display the given MESSAGE as a warning."
  20228. (if (fboundp 'display-warning)
  20229. (display-warning 'org message
  20230. (if (featurep 'xemacs) 'warning :warning))
  20231. (let ((buf (get-buffer-create "*Org warnings*")))
  20232. (with-current-buffer buf
  20233. (goto-char (point-max))
  20234. (insert "Warning (Org): " message)
  20235. (unless (bolp)
  20236. (newline)))
  20237. (display-buffer buf)
  20238. (sit-for 0))))
  20239. (defun org-eval (form)
  20240. "Eval FORM and return result."
  20241. (condition-case error
  20242. (eval form)
  20243. (error (format "%%![Error: %s]" error))))
  20244. (defun org-in-clocktable-p ()
  20245. "Check if the cursor is in a clocktable."
  20246. (let ((pos (point)) start)
  20247. (save-excursion
  20248. (end-of-line 1)
  20249. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20250. (setq start (match-beginning 0))
  20251. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20252. (>= (match-end 0) pos)
  20253. start))))
  20254. (defun org-in-verbatim-emphasis ()
  20255. (save-match-data
  20256. (and (org-in-regexp org-emph-re 2)
  20257. (>= (point) (match-beginning 3))
  20258. (<= (point) (match-end 4))
  20259. (member (match-string 3) '("=" "~")))))
  20260. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20261. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20262. (if (and marker (marker-buffer marker)
  20263. (buffer-live-p (marker-buffer marker)))
  20264. (progn
  20265. (org-pop-to-buffer-same-window (marker-buffer marker))
  20266. (if (or (> marker (point-max)) (< marker (point-min)))
  20267. (widen))
  20268. (goto-char marker)
  20269. (org-show-context 'org-goto))
  20270. (if bookmark
  20271. (bookmark-jump bookmark)
  20272. (error "Cannot find location"))))
  20273. (defun org-quote-csv-field (s)
  20274. "Quote field for inclusion in CSV material."
  20275. (if (string-match "[\",]" s)
  20276. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20277. s))
  20278. (defun org-force-self-insert (N)
  20279. "Needed to enforce self-insert under remapping."
  20280. (interactive "p")
  20281. (self-insert-command N))
  20282. (defun org-string-width (s)
  20283. "Compute width of string, ignoring invisible characters.
  20284. This ignores character with invisibility property `org-link', and also
  20285. characters with property `org-cwidth', because these will become invisible
  20286. upon the next fontification round."
  20287. (let (b l)
  20288. (when (or (eq t buffer-invisibility-spec)
  20289. (assq 'org-link buffer-invisibility-spec))
  20290. (while (setq b (text-property-any 0 (length s)
  20291. 'invisible 'org-link s))
  20292. (setq s (concat (substring s 0 b)
  20293. (substring s (or (next-single-property-change
  20294. b 'invisible s) (length s)))))))
  20295. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20296. (setq s (concat (substring s 0 b)
  20297. (substring s (or (next-single-property-change
  20298. b 'org-cwidth s) (length s))))))
  20299. (setq l (string-width s) b -1)
  20300. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20301. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20302. l))
  20303. (defun org-shorten-string (s maxlength)
  20304. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20305. If the string is shorter or has length MAXLENGTH, just return the
  20306. original string. If it is longer, the functions finds a space in the
  20307. string, breaks this string off at that locations and adds three dots
  20308. as ellipsis. Including the ellipsis, the string will not be longer
  20309. than MAXLENGTH. If finding a good breaking point in the string does
  20310. not work, the string is just chopped off in the middle of a word
  20311. if necessary."
  20312. (if (<= (length s) maxlength)
  20313. s
  20314. (let* ((n (max (- maxlength 4) 1))
  20315. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20316. (if (string-match re s)
  20317. (concat (match-string 1 s) "...")
  20318. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20319. (defun org-get-indentation (&optional line)
  20320. "Get the indentation of the current line, interpreting tabs.
  20321. When LINE is given, assume it represents a line and compute its indentation."
  20322. (if line
  20323. (if (string-match "^ *" (org-remove-tabs line))
  20324. (match-end 0))
  20325. (save-excursion
  20326. (beginning-of-line 1)
  20327. (skip-chars-forward " \t")
  20328. (current-column))))
  20329. (defun org-get-string-indentation (s)
  20330. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20331. (let ((n -1) (i 0) (w tab-width) c)
  20332. (catch 'exit
  20333. (while (< (setq n (1+ n)) (length s))
  20334. (setq c (aref s n))
  20335. (cond ((= c ?\ ) (setq i (1+ i)))
  20336. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20337. (t (throw 'exit t)))))
  20338. i))
  20339. (defun org-remove-tabs (s &optional width)
  20340. "Replace tabulators in S with spaces.
  20341. Assumes that s is a single line, starting in column 0."
  20342. (setq width (or width tab-width))
  20343. (while (string-match "\t" s)
  20344. (setq s (replace-match
  20345. (make-string
  20346. (- (* width (/ (+ (match-beginning 0) width) width))
  20347. (match-beginning 0)) ?\ )
  20348. t t s)))
  20349. s)
  20350. (defun org-fix-indentation (line ind)
  20351. "Fix indentation in LINE.
  20352. IND is a cons cell with target and minimum indentation.
  20353. If the current indentation in LINE is smaller than the minimum,
  20354. leave it alone. If it is larger than ind, set it to the target."
  20355. (let* ((l (org-remove-tabs line))
  20356. (i (org-get-indentation l))
  20357. (i1 (car ind)) (i2 (cdr ind)))
  20358. (if (>= i i2) (setq l (substring line i2)))
  20359. (if (> i1 0)
  20360. (concat (make-string i1 ?\ ) l)
  20361. l)))
  20362. (defun org-remove-indentation (code &optional n)
  20363. "Remove the maximum common indentation from the lines in CODE.
  20364. N may optionally be the number of spaces to remove."
  20365. (with-temp-buffer
  20366. (insert code)
  20367. (org-do-remove-indentation n)
  20368. (buffer-string)))
  20369. (defun org-do-remove-indentation (&optional n)
  20370. "Remove the maximum common indentation from the buffer."
  20371. (untabify (point-min) (point-max))
  20372. (let ((min 10000) re)
  20373. (if n
  20374. (setq min n)
  20375. (goto-char (point-min))
  20376. (while (re-search-forward "^ *[^ \n]" nil t)
  20377. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20378. (unless (or (= min 0) (= min 10000))
  20379. (setq re (format "^ \\{%d\\}" min))
  20380. (goto-char (point-min))
  20381. (while (re-search-forward re nil t)
  20382. (replace-match "")
  20383. (end-of-line 1))
  20384. min)))
  20385. (defun org-fill-template (template alist)
  20386. "Find each %key of ALIST in TEMPLATE and replace it."
  20387. (let ((case-fold-search nil)
  20388. entry key value)
  20389. (setq alist (sort (copy-sequence alist)
  20390. (lambda (a b) (< (length (car a)) (length (car b))))))
  20391. (while (setq entry (pop alist))
  20392. (setq template
  20393. (replace-regexp-in-string
  20394. (concat "%" (regexp-quote (car entry)))
  20395. (or (cdr entry) "") template t t)))
  20396. template))
  20397. (defun org-base-buffer (buffer)
  20398. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20399. (if (not buffer)
  20400. buffer
  20401. (or (buffer-base-buffer buffer)
  20402. buffer)))
  20403. (defun org-wrap (string &optional width lines)
  20404. "Wrap string to either a number of lines, or a width in characters.
  20405. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20406. that costs. If there is a word longer than WIDTH, the text is actually
  20407. wrapped to the length of that word.
  20408. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20409. many lines, whatever width that takes.
  20410. The return value is a list of lines, without newlines at the end."
  20411. (let* ((words (org-split-string string "[ \t\n]+"))
  20412. (maxword (apply 'max (mapcar 'org-string-width words)))
  20413. w ll)
  20414. (cond (width
  20415. (org-do-wrap words (max maxword width)))
  20416. (lines
  20417. (setq w maxword)
  20418. (setq ll (org-do-wrap words maxword))
  20419. (if (<= (length ll) lines)
  20420. ll
  20421. (setq ll words)
  20422. (while (> (length ll) lines)
  20423. (setq w (1+ w))
  20424. (setq ll (org-do-wrap words w)))
  20425. ll))
  20426. (t (error "Cannot wrap this")))))
  20427. (defun org-do-wrap (words width)
  20428. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20429. (let (lines line)
  20430. (while words
  20431. (setq line (pop words))
  20432. (while (and words (< (+ (length line) (length (car words))) width))
  20433. (setq line (concat line " " (pop words))))
  20434. (setq lines (push line lines)))
  20435. (nreverse lines)))
  20436. (defun org-split-string (string &optional separators)
  20437. "Splits STRING into substrings at SEPARATORS.
  20438. No empty strings are returned if there are matches at the beginning
  20439. and end of string."
  20440. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  20441. (start 0)
  20442. notfirst
  20443. (list nil))
  20444. (while (and (string-match rexp string
  20445. (if (and notfirst
  20446. (= start (match-beginning 0))
  20447. (< start (length string)))
  20448. (1+ start) start))
  20449. (< (match-beginning 0) (length string)))
  20450. (setq notfirst t)
  20451. (or (eq (match-beginning 0) 0)
  20452. (and (eq (match-beginning 0) (match-end 0))
  20453. (eq (match-beginning 0) start))
  20454. (setq list
  20455. (cons (substring string start (match-beginning 0))
  20456. list)))
  20457. (setq start (match-end 0)))
  20458. (or (eq start (length string))
  20459. (setq list
  20460. (cons (substring string start)
  20461. list)))
  20462. (nreverse list)))
  20463. (defun org-quote-vert (s)
  20464. "Replace \"|\" with \"\\vert\"."
  20465. (while (string-match "|" s)
  20466. (setq s (replace-match "\\vert" t t s)))
  20467. s)
  20468. (defun org-uuidgen-p (s)
  20469. "Is S an ID created by UUIDGEN?"
  20470. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20471. (defun org-in-src-block-p (&optional inside)
  20472. "Whether point is in a code source block.
  20473. When INSIDE is non-nil, don't consider we are within a src block
  20474. when point is at #+BEGIN_SRC or #+END_SRC."
  20475. (let ((case-fold-search t) ov)
  20476. (or (and (eq (get-char-property (point) 'src-block) t))
  20477. (and (not inside)
  20478. (save-match-data
  20479. (save-excursion
  20480. (beginning-of-line)
  20481. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20482. (defun org-context ()
  20483. "Return a list of contexts of the current cursor position.
  20484. If several contexts apply, all are returned.
  20485. Each context entry is a list with a symbol naming the context, and
  20486. two positions indicating start and end of the context. Possible
  20487. contexts are:
  20488. :headline anywhere in a headline
  20489. :headline-stars on the leading stars in a headline
  20490. :todo-keyword on a TODO keyword (including DONE) in a headline
  20491. :tags on the TAGS in a headline
  20492. :priority on the priority cookie in a headline
  20493. :item on the first line of a plain list item
  20494. :item-bullet on the bullet/number of a plain list item
  20495. :checkbox on the checkbox in a plain list item
  20496. :table in an org-mode table
  20497. :table-special on a special filed in a table
  20498. :table-table in a table.el table
  20499. :clocktable in a clocktable
  20500. :src-block in a source block
  20501. :link on a hyperlink
  20502. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20503. :target on a <<target>>
  20504. :radio-target on a <<<radio-target>>>
  20505. :latex-fragment on a LaTeX fragment
  20506. :latex-preview on a LaTeX fragment with overlaid preview image
  20507. This function expects the position to be visible because it uses font-lock
  20508. faces as a help to recognize the following contexts: :table-special, :link,
  20509. and :keyword."
  20510. (let* ((f (get-text-property (point) 'face))
  20511. (faces (if (listp f) f (list f)))
  20512. (case-fold-search t)
  20513. (p (point)) clist o)
  20514. ;; First the large context
  20515. (cond
  20516. ((org-at-heading-p t)
  20517. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20518. (when (progn
  20519. (beginning-of-line 1)
  20520. (looking-at org-todo-line-tags-regexp))
  20521. (push (org-point-in-group p 1 :headline-stars) clist)
  20522. (push (org-point-in-group p 2 :todo-keyword) clist)
  20523. (push (org-point-in-group p 4 :tags) clist))
  20524. (goto-char p)
  20525. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20526. (if (looking-at "\\[#[A-Z0-9]\\]")
  20527. (push (org-point-in-group p 0 :priority) clist)))
  20528. ((org-at-item-p)
  20529. (push (org-point-in-group p 2 :item-bullet) clist)
  20530. (push (list :item (point-at-bol)
  20531. (save-excursion (org-end-of-item) (point)))
  20532. clist)
  20533. (and (org-at-item-checkbox-p)
  20534. (push (org-point-in-group p 0 :checkbox) clist)))
  20535. ((org-at-table-p)
  20536. (push (list :table (org-table-begin) (org-table-end)) clist)
  20537. (if (memq 'org-formula faces)
  20538. (push (list :table-special
  20539. (previous-single-property-change p 'face)
  20540. (next-single-property-change p 'face)) clist)))
  20541. ((org-at-table-p 'any)
  20542. (push (list :table-table) clist)))
  20543. (goto-char p)
  20544. (let ((case-fold-search t))
  20545. ;; New the "medium" contexts: clocktables, source blocks
  20546. (cond ((org-in-clocktable-p)
  20547. (push (list :clocktable
  20548. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20549. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20550. (match-beginning 1))
  20551. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20552. (match-end 0))) clist))
  20553. ((org-in-src-block-p)
  20554. (push (list :src-block
  20555. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20556. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20557. (match-beginning 1))
  20558. (and (search-forward "#+END_SRC" nil t)
  20559. (match-beginning 0))) clist))))
  20560. (goto-char p)
  20561. ;; Now the small context
  20562. (cond
  20563. ((org-at-timestamp-p)
  20564. (push (org-point-in-group p 0 :timestamp) clist))
  20565. ((memq 'org-link faces)
  20566. (push (list :link
  20567. (previous-single-property-change p 'face)
  20568. (next-single-property-change p 'face)) clist))
  20569. ((memq 'org-special-keyword faces)
  20570. (push (list :keyword
  20571. (previous-single-property-change p 'face)
  20572. (next-single-property-change p 'face)) clist))
  20573. ((org-at-target-p)
  20574. (push (org-point-in-group p 0 :target) clist)
  20575. (goto-char (1- (match-beginning 0)))
  20576. (if (looking-at org-radio-target-regexp)
  20577. (push (org-point-in-group p 0 :radio-target) clist))
  20578. (goto-char p))
  20579. ((setq o (car (delq nil
  20580. (mapcar
  20581. (lambda (x)
  20582. (if (memq x org-latex-fragment-image-overlays) x))
  20583. (overlays-at (point))))))
  20584. (push (list :latex-fragment
  20585. (overlay-start o) (overlay-end o)) clist)
  20586. (push (list :latex-preview
  20587. (overlay-start o) (overlay-end o)) clist))
  20588. ((org-inside-LaTeX-fragment-p)
  20589. ;; FIXME: positions wrong.
  20590. (push (list :latex-fragment (point) (point)) clist)))
  20591. (setq clist (nreverse (delq nil clist)))
  20592. clist))
  20593. (defun org-in-regexp (re &optional nlines visually)
  20594. "Check if point is inside a match of RE.
  20595. Normally only the current line is checked, but you can include
  20596. NLINES extra lines after point into the search. If VISUALLY is
  20597. set, require that the cursor is not after the match but really
  20598. on, so that the block visually is on the match."
  20599. (catch 'exit
  20600. (let ((pos (point))
  20601. (eol (point-at-eol (+ 1 (or nlines 0))))
  20602. (inc (if visually 1 0)))
  20603. (save-excursion
  20604. (beginning-of-line (- 1 (or nlines 0)))
  20605. (while (re-search-forward re eol t)
  20606. (if (and (<= (match-beginning 0) pos)
  20607. (>= (+ inc (match-end 0)) pos))
  20608. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20609. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20610. "Org mode 8.3")
  20611. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20612. "Non-nil when point is between matches of START-RE and END-RE.
  20613. Also return a non-nil value when point is on one of the matches.
  20614. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20615. buffer positions. Default values are the positions of headlines
  20616. surrounding the point.
  20617. The functions returns a cons cell whose car (resp. cdr) is the
  20618. position before START-RE (resp. after END-RE)."
  20619. (save-match-data
  20620. (let ((pos (point))
  20621. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20622. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20623. beg end)
  20624. (save-excursion
  20625. ;; Point is on a block when on START-RE or if START-RE can be
  20626. ;; found before it...
  20627. (and (or (org-in-regexp start-re)
  20628. (re-search-backward start-re limit-up t))
  20629. (setq beg (match-beginning 0))
  20630. ;; ... and END-RE after it...
  20631. (goto-char (match-end 0))
  20632. (re-search-forward end-re limit-down t)
  20633. (> (setq end (match-end 0)) pos)
  20634. ;; ... without another START-RE in-between.
  20635. (goto-char (match-beginning 0))
  20636. (not (re-search-backward start-re (1+ beg) t))
  20637. ;; Return value.
  20638. (cons beg end))))))
  20639. (defun org-in-block-p (names)
  20640. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20641. NAMES is a list of strings containing names of blocks.
  20642. Return first block name matched, or nil. Beware that in case of
  20643. nested blocks, the returned name may not belong to the closest
  20644. block from point."
  20645. (save-match-data
  20646. (catch 'exit
  20647. (let ((case-fold-search t)
  20648. (lim-up (save-excursion (outline-previous-heading)))
  20649. (lim-down (save-excursion (outline-next-heading))))
  20650. (mapc (lambda (name)
  20651. (let ((n (regexp-quote name)))
  20652. (when (org-between-regexps-p
  20653. (concat "^[ \t]*#\\+begin_" n)
  20654. (concat "^[ \t]*#\\+end_" n)
  20655. lim-up lim-down)
  20656. (throw 'exit n))))
  20657. names))
  20658. nil)))
  20659. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20660. "Call `multi-occur' with buffers for all agenda files."
  20661. (interactive "sOrg-files matching: \np")
  20662. (let* ((files (org-agenda-files))
  20663. (tnames (mapcar 'file-truename files))
  20664. (extra org-agenda-text-search-extra-files)
  20665. f)
  20666. (when (eq (car extra) 'agenda-archives)
  20667. (setq extra (cdr extra))
  20668. (setq files (org-add-archive-files files)))
  20669. (while (setq f (pop extra))
  20670. (unless (member (file-truename f) tnames)
  20671. (add-to-list 'files f 'append)
  20672. (add-to-list 'tnames (file-truename f) 'append)))
  20673. (multi-occur
  20674. (mapcar (lambda (x)
  20675. (with-current-buffer
  20676. (or (get-file-buffer x) (find-file-noselect x))
  20677. (widen)
  20678. (current-buffer)))
  20679. files)
  20680. regexp)))
  20681. (if (boundp 'occur-mode-find-occurrence-hook)
  20682. ;; Emacs 23
  20683. (add-hook 'occur-mode-find-occurrence-hook
  20684. (lambda ()
  20685. (when (derived-mode-p 'org-mode)
  20686. (org-reveal))))
  20687. ;; Emacs 22
  20688. (defadvice occur-mode-goto-occurrence
  20689. (after org-occur-reveal activate)
  20690. (and (derived-mode-p 'org-mode) (org-reveal)))
  20691. (defadvice occur-mode-goto-occurrence-other-window
  20692. (after org-occur-reveal activate)
  20693. (and (derived-mode-p 'org-mode) (org-reveal)))
  20694. (defadvice occur-mode-display-occurrence
  20695. (after org-occur-reveal activate)
  20696. (when (derived-mode-p 'org-mode)
  20697. (let ((pos (occur-mode-find-occurrence)))
  20698. (with-current-buffer (marker-buffer pos)
  20699. (save-excursion
  20700. (goto-char pos)
  20701. (org-reveal)))))))
  20702. (defun org-occur-link-in-agenda-files ()
  20703. "Create a link and search for it in the agendas.
  20704. The link is not stored in `org-stored-links', it is just created
  20705. for the search purpose."
  20706. (interactive)
  20707. (let ((link (condition-case nil
  20708. (org-store-link nil)
  20709. (error "Unable to create a link to here"))))
  20710. (org-occur-in-agenda-files (regexp-quote link))))
  20711. (defun org-reverse-string (string)
  20712. "Return the reverse of STRING."
  20713. (apply 'string (reverse (string-to-list string))))
  20714. ;; defsubst org-uniquify must be defined before first use
  20715. (defun org-uniquify-alist (alist)
  20716. "Merge elements of ALIST with the same key.
  20717. For example, in this alist:
  20718. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20719. => '((a 1 3) (b 2))
  20720. merge (a 1) and (a 3) into (a 1 3).
  20721. The function returns the new ALIST."
  20722. (let (rtn)
  20723. (mapc
  20724. (lambda (e)
  20725. (let (n)
  20726. (if (not (assoc (car e) rtn))
  20727. (push e rtn)
  20728. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20729. (setq rtn (assq-delete-all (car e) rtn))
  20730. (push n rtn))))
  20731. alist)
  20732. rtn))
  20733. (defun org-delete-all (elts list)
  20734. "Remove all elements in ELTS from LIST."
  20735. (while elts
  20736. (setq list (delete (pop elts) list)))
  20737. list)
  20738. (defun org-count (cl-item cl-seq)
  20739. "Count the number of occurrences of ITEM in SEQ.
  20740. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20741. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20742. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20743. (while (< cl-start cl-end)
  20744. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20745. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20746. (setq cl-start (1+ cl-start)))
  20747. cl-count))
  20748. (defun org-remove-if (predicate seq)
  20749. "Remove everything from SEQ that fulfills PREDICATE."
  20750. (let (res e)
  20751. (while seq
  20752. (setq e (pop seq))
  20753. (if (not (funcall predicate e)) (push e res)))
  20754. (nreverse res)))
  20755. (defun org-remove-if-not (predicate seq)
  20756. "Remove everything from SEQ that does not fulfill PREDICATE."
  20757. (let (res e)
  20758. (while seq
  20759. (setq e (pop seq))
  20760. (if (funcall predicate e) (push e res)))
  20761. (nreverse res)))
  20762. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20763. "Reduce two-argument FUNCTION across SEQ.
  20764. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20765. \":initial-value\" removed."
  20766. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20767. (cadr (memq :initial-value cl-keys)))
  20768. (cl-seq (pop cl-seq))
  20769. (t (funcall cl-func)))))
  20770. (while cl-seq
  20771. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20772. cl-accum))
  20773. (defun org-every (pred seq)
  20774. "Return true if PREDICATE is true of every element of SEQ.
  20775. Adapted from `every' in cl.el."
  20776. (catch 'org-every
  20777. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20778. t))
  20779. (defun org-some (pred seq)
  20780. "Return true if PREDICATE is true of any element of SEQ.
  20781. Adapted from `some' in cl.el."
  20782. (catch 'org-some
  20783. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20784. nil))
  20785. (defun org-back-over-empty-lines ()
  20786. "Move backwards over whitespace, to the beginning of the first empty line.
  20787. Returns the number of empty lines passed."
  20788. (let ((pos (point)))
  20789. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20790. (skip-chars-backward " \t\n\r")
  20791. (unless (eobp)
  20792. (forward-line -1)))
  20793. (beginning-of-line 2)
  20794. (goto-char (min (point) pos))
  20795. (count-lines (point) pos)))
  20796. (defun org-skip-whitespace ()
  20797. (skip-chars-forward " \t\n\r"))
  20798. (defun org-point-in-group (point group &optional context)
  20799. "Check if POINT is in match-group GROUP.
  20800. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20801. match. If the match group does not exist or point is not inside it,
  20802. return nil."
  20803. (and (match-beginning group)
  20804. (>= point (match-beginning group))
  20805. (<= point (match-end group))
  20806. (if context
  20807. (list context (match-beginning group) (match-end group))
  20808. t)))
  20809. (defun org-switch-to-buffer-other-window (&rest args)
  20810. "Switch to buffer in a second window on the current frame.
  20811. In particular, do not allow pop-up frames.
  20812. Returns the newly created buffer."
  20813. (org-no-popups
  20814. (apply 'switch-to-buffer-other-window args)))
  20815. (defun org-combine-plists (&rest plists)
  20816. "Create a single property list from all plists in PLISTS.
  20817. The process starts by copying the first list, and then setting properties
  20818. from the other lists. Settings in the last list are the most significant
  20819. ones and overrule settings in the other lists."
  20820. (let ((rtn (copy-sequence (pop plists)))
  20821. p v ls)
  20822. (while plists
  20823. (setq ls (pop plists))
  20824. (while ls
  20825. (setq p (pop ls) v (pop ls))
  20826. (setq rtn (plist-put rtn p v))))
  20827. rtn))
  20828. (defun org-replace-escapes (string table)
  20829. "Replace %-escapes in STRING with values in TABLE.
  20830. TABLE is an association list with keys like \"%a\" and string values.
  20831. The sequences in STRING may contain normal field width and padding information,
  20832. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20833. so values can contain further %-escapes if they are define later in TABLE."
  20834. (let ((tbl (copy-alist table))
  20835. (case-fold-search nil)
  20836. (pchg 0)
  20837. e re rpl)
  20838. (while (setq e (pop tbl))
  20839. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20840. (when (and (cdr e) (string-match re (cdr e)))
  20841. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20842. (safe "SREF"))
  20843. (add-text-properties 0 3 (list 'sref sref) safe)
  20844. (setcdr e (replace-match safe t t (cdr e)))))
  20845. (while (string-match re string)
  20846. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20847. (cdr e)))
  20848. (setq string (replace-match rpl t t string))))
  20849. (while (setq pchg (next-property-change pchg string))
  20850. (let ((sref (get-text-property pchg 'sref string)))
  20851. (when (and sref (string-match "SREF" string pchg))
  20852. (setq string (replace-match sref t t string)))))
  20853. string))
  20854. (defun org-sublist (list start end)
  20855. "Return a section of LIST, from START to END.
  20856. Counting starts at 1."
  20857. (let (rtn (c start))
  20858. (setq list (nthcdr (1- start) list))
  20859. (while (and list (<= c end))
  20860. (push (pop list) rtn)
  20861. (setq c (1+ c)))
  20862. (nreverse rtn)))
  20863. (defun org-find-base-buffer-visiting (file)
  20864. "Like `find-buffer-visiting' but always return the base buffer and
  20865. not an indirect buffer."
  20866. (let ((buf (or (get-file-buffer file)
  20867. (find-buffer-visiting file))))
  20868. (if buf
  20869. (or (buffer-base-buffer buf) buf)
  20870. nil)))
  20871. (defun org-image-file-name-regexp (&optional extensions)
  20872. "Return regexp matching the file names of images.
  20873. If EXTENSIONS is given, only match these."
  20874. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20875. (image-file-name-regexp)
  20876. (let ((image-file-name-extensions
  20877. (or extensions
  20878. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20879. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20880. (concat "\\."
  20881. (regexp-opt (nconc (mapcar 'upcase
  20882. image-file-name-extensions)
  20883. image-file-name-extensions)
  20884. t)
  20885. "\\'"))))
  20886. (defun org-file-image-p (file &optional extensions)
  20887. "Return non-nil if FILE is an image."
  20888. (save-match-data
  20889. (string-match (org-image-file-name-regexp extensions) file)))
  20890. (defun org-get-cursor-date (&optional with-time)
  20891. "Return the date at cursor in as a time.
  20892. This works in the calendar and in the agenda, anywhere else it just
  20893. returns the current time.
  20894. If WITH-TIME is non-nil, returns the time of the event at point (in
  20895. the agenda) or the current time of the day."
  20896. (let (date day defd tp tm hod mod)
  20897. (when with-time
  20898. (setq tp (get-text-property (point) 'time))
  20899. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20900. (setq hod (string-to-number (match-string 1 tp))
  20901. mod (string-to-number (match-string 2 tp))))
  20902. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20903. mod (nth 1 (decode-time (current-time))))))
  20904. (cond
  20905. ((eq major-mode 'calendar-mode)
  20906. (setq date (calendar-cursor-to-date)
  20907. defd (encode-time 0 (or mod 0) (or hod 0)
  20908. (nth 1 date) (nth 0 date) (nth 2 date))))
  20909. ((eq major-mode 'org-agenda-mode)
  20910. (setq day (get-text-property (point) 'day))
  20911. (if day
  20912. (setq date (calendar-gregorian-from-absolute day)
  20913. defd (encode-time 0 (or mod 0) (or hod 0)
  20914. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20915. (or defd (current-time))))
  20916. (defun org-mark-subtree (&optional up)
  20917. "Mark the current subtree.
  20918. This puts point at the start of the current subtree, and mark at
  20919. the end. If a numeric prefix UP is given, move up into the
  20920. hierarchy of headlines by UP levels before marking the subtree."
  20921. (interactive "P")
  20922. (org-with-limited-levels
  20923. (cond ((org-at-heading-p) (beginning-of-line))
  20924. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20925. (t (outline-previous-visible-heading 1))))
  20926. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20927. (if (org-called-interactively-p 'any)
  20928. (call-interactively 'org-mark-element)
  20929. (org-mark-element)))
  20930. ;;; Indentation
  20931. (defun org--get-expected-indentation (element contentsp)
  20932. "Expected indentation column for current line, according to ELEMENT.
  20933. ELEMENT is an element containing point. CONTENTSP is non-nil
  20934. when indentation is to be computed according to contents of
  20935. ELEMENT."
  20936. (let ((type (org-element-type element))
  20937. (start (org-element-property :begin element))
  20938. (post-affiliated (org-element-property :post-affiliated element)))
  20939. (org-with-wide-buffer
  20940. (cond
  20941. (contentsp
  20942. (case type
  20943. ((diary-sexp footnote-definition) 0)
  20944. ((headline inlinetask nil)
  20945. (if (not org-adapt-indentation) 0
  20946. (let ((level (org-current-level)))
  20947. (if level (1+ level) 0))))
  20948. ((item plain-list) (org-list-item-body-column post-affiliated))
  20949. (t
  20950. (goto-char start)
  20951. (org-get-indentation))))
  20952. ((memq type '(headline inlinetask nil))
  20953. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20954. (org--get-expected-indentation element t)
  20955. 0))
  20956. ((memq type '(diary-sexp footnote-definition)) 0)
  20957. ;; First paragraph of a footnote definition or an item.
  20958. ;; Indent like parent.
  20959. ((< (line-beginning-position) start)
  20960. (org--get-expected-indentation
  20961. (org-element-property :parent element) t))
  20962. ;; At first line: indent according to previous sibling, if any,
  20963. ;; ignoring footnote definitions and inline tasks, or parent's
  20964. ;; contents.
  20965. ((= (line-beginning-position) start)
  20966. (catch 'exit
  20967. (while t
  20968. (if (= (point-min) start) (throw 'exit 0)
  20969. (goto-char (1- start))
  20970. (let* ((previous (org-element-at-point))
  20971. (parent previous))
  20972. (while (and parent (<= (org-element-property :end parent) start))
  20973. (setq previous parent
  20974. parent (org-element-property :parent parent)))
  20975. (cond
  20976. ((not previous) (throw 'exit 0))
  20977. ((> (org-element-property :end previous) start)
  20978. (throw 'exit (org--get-expected-indentation previous t)))
  20979. ((memq (org-element-type previous)
  20980. '(footnote-definition inlinetask))
  20981. (setq start (org-element-property :begin previous)))
  20982. (t (goto-char (org-element-property :begin previous))
  20983. (throw 'exit
  20984. (if (bolp) (org-get-indentation)
  20985. ;; At first paragraph in an item or
  20986. ;; a footnote definition.
  20987. (org--get-expected-indentation
  20988. (org-element-property :parent previous) t))))))))))
  20989. ;; Otherwise, move to the first non-blank line above.
  20990. (t
  20991. (beginning-of-line)
  20992. (let ((pos (point)))
  20993. (skip-chars-backward " \r\t\n")
  20994. (cond
  20995. ;; Two blank lines end a footnote definition or a plain
  20996. ;; list. When we indent an empty line after them, the
  20997. ;; containing list or footnote definition is over, so it
  20998. ;; qualifies as a previous sibling. Therefore, we indent
  20999. ;; like its first line.
  21000. ((and (memq type '(footnote-definition plain-list))
  21001. (> (count-lines (point) pos) 2))
  21002. (goto-char start)
  21003. (org-get-indentation))
  21004. ;; Line above is the first one of a paragraph at the
  21005. ;; beginning of an item or a footnote definition. Indent
  21006. ;; like parent.
  21007. ((< (line-beginning-position) start)
  21008. (org--get-expected-indentation
  21009. (org-element-property :parent element) t))
  21010. ;; Line above is the beginning of an element, i.e., point
  21011. ;; was originally on the blank lines between element's start
  21012. ;; and contents.
  21013. ((= (line-beginning-position) post-affiliated)
  21014. (org--get-expected-indentation element t))
  21015. ;; POS is after contents in a greater element. Indent like
  21016. ;; the beginning of the element.
  21017. ;;
  21018. ;; As a special case, if point is at the end of a footnote
  21019. ;; definition or an item, indent like the very last element
  21020. ;; within.
  21021. ((and (not (eq type 'paragraph))
  21022. (let ((cend (org-element-property :contents-end element)))
  21023. (and cend (<= cend pos))))
  21024. (if (memq type '(footnote-definition item plain-list))
  21025. (let ((last (org-element-at-point)))
  21026. (org--get-expected-indentation
  21027. last
  21028. (memq (org-element-type last)
  21029. '(footnote-definition item plain-list))))
  21030. (goto-char start)
  21031. (org-get-indentation)))
  21032. ;; In any other case, indent like the current line.
  21033. (t (org-get-indentation)))))))))
  21034. (defun org--align-node-property ()
  21035. "Align node property at point.
  21036. Alignment is done according to `org-property-format', which see."
  21037. (when (save-excursion
  21038. (beginning-of-line)
  21039. (looking-at org-property-re))
  21040. (replace-match
  21041. (concat (match-string 4)
  21042. (org-trim
  21043. (format org-property-format (match-string 1) (match-string 3))))
  21044. t t)))
  21045. (defun org-indent-line ()
  21046. "Indent line depending on context.
  21047. Indentation is done according to the following rules:
  21048. - Footnote definitions, diary sexps, headlines and inline tasks
  21049. have to start at column 0.
  21050. - On the very first line of an element, consider, in order, the
  21051. next rules until one matches:
  21052. 1. If there's a sibling element before, ignoring footnote
  21053. definitions and inline tasks, indent like its first line.
  21054. 2. If element has a parent, indent like its contents. More
  21055. precisely, if parent is an item, indent after the
  21056. description part, if any, or the bullet (see
  21057. `org-list-description-max-indent'). Else, indent like
  21058. parent's first line.
  21059. 3. Otherwise, indent relatively to current level, if
  21060. `org-adapt-indentation' is non-nil, or to left margin.
  21061. - On a blank line at the end of an element, indent according to
  21062. the type of the element. More precisely
  21063. 1. If element is a plain list, an item, or a footnote
  21064. definition, indent like the very last element within.
  21065. 2. If element is a paragraph, indent like its last non blank
  21066. line.
  21067. 3. Otherwise, indent like its very first line.
  21068. - In the code part of a source block, use language major mode
  21069. to indent current line if `org-src-tab-acts-natively' is
  21070. non-nil. If it is nil, do nothing.
  21071. - Otherwise, indent like the first non-blank line above.
  21072. The function doesn't indent an item as it could break the whole
  21073. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21074. \\[org-shiftmetaright].
  21075. Also align node properties according to `org-property-format'."
  21076. (interactive)
  21077. (cond
  21078. (orgstruct-is-++
  21079. (let ((indent-line-function
  21080. (cadadr (assq 'indent-line-function org-fb-vars))))
  21081. (indent-according-to-mode)))
  21082. ((org-at-heading-p) 'noindent)
  21083. (t
  21084. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21085. (type (org-element-type element)))
  21086. (cond ((and (memq type '(plain-list item))
  21087. (= (line-beginning-position)
  21088. (org-element-property :post-affiliated element)))
  21089. 'noindent)
  21090. ((and (eq type 'src-block)
  21091. org-src-tab-acts-natively
  21092. (> (line-beginning-position)
  21093. (org-element-property :post-affiliated element))
  21094. (< (line-beginning-position)
  21095. (org-with-wide-buffer
  21096. (goto-char (org-element-property :end element))
  21097. (skip-chars-backward " \r\t\n")
  21098. (line-beginning-position))))
  21099. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21100. (t
  21101. (let ((column (org--get-expected-indentation element nil)))
  21102. ;; Preserve current column.
  21103. (if (<= (current-column) (current-indentation))
  21104. (org-indent-line-to column)
  21105. (save-excursion (org-indent-line-to column))))
  21106. ;; Align node property. Also preserve current column.
  21107. (when (eq type 'node-property)
  21108. (let ((column (current-column)))
  21109. (org--align-node-property)
  21110. (org-move-to-column column)))))))))
  21111. (defun org-indent-region (start end)
  21112. "Indent each non-blank line in the region.
  21113. Called from a program, START and END specify the region to
  21114. indent. The function will not indent contents of example blocks,
  21115. verse blocks and export blocks as leading white spaces are
  21116. assumed to be significant there."
  21117. (interactive "r")
  21118. (save-excursion
  21119. (goto-char start)
  21120. (skip-chars-forward " \r\t\n")
  21121. (unless (eobp) (beginning-of-line))
  21122. (let ((indent-to
  21123. (lambda (ind pos)
  21124. ;; Set IND as indentation for all lines between point and
  21125. ;; POS or END, whichever comes first. Blank lines are
  21126. ;; ignored. Leave point after POS once done.
  21127. (let ((limit (copy-marker (min end pos))))
  21128. (while (< (point) limit)
  21129. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21130. (forward-line))
  21131. (set-marker limit nil))))
  21132. (end (copy-marker end)))
  21133. (while (< (point) end)
  21134. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21135. (let* ((element (org-element-at-point))
  21136. (type (org-element-type element))
  21137. (element-end (copy-marker (org-element-property :end element)))
  21138. (ind (org--get-expected-indentation element nil)))
  21139. (cond
  21140. ((or (memq type '(paragraph table table-row))
  21141. (not (or (org-element-property :contents-begin element)
  21142. (memq type
  21143. '(example-block export-block src-block)))))
  21144. ;; Elements here are indented as a single block. Also
  21145. ;; align node properties.
  21146. (when (eq type 'node-property)
  21147. (org--align-node-property)
  21148. (beginning-of-line))
  21149. (funcall indent-to ind element-end))
  21150. (t
  21151. ;; Elements in this category consist of three parts:
  21152. ;; before the contents, the contents, and after the
  21153. ;; contents. The contents are treated specially,
  21154. ;; according to the element type, or not indented at
  21155. ;; all. Other parts are indented as a single block.
  21156. (let* ((post (copy-marker
  21157. (org-element-property :post-affiliated element)))
  21158. (cbeg
  21159. (copy-marker
  21160. (cond
  21161. ((not (org-element-property :contents-begin element))
  21162. ;; Fake contents for source blocks.
  21163. (org-with-wide-buffer
  21164. (goto-char post)
  21165. (forward-line)
  21166. (point)))
  21167. ((memq type '(footnote-definition item plain-list))
  21168. ;; Contents in these elements could start on
  21169. ;; the same line as the beginning of the
  21170. ;; element. Make sure we start indenting
  21171. ;; from the second line.
  21172. (org-with-wide-buffer
  21173. (goto-char post)
  21174. (end-of-line)
  21175. (skip-chars-forward " \r\t\n")
  21176. (if (eobp) (point) (line-beginning-position))))
  21177. (t (org-element-property :contents-begin element)))))
  21178. (cend (copy-marker
  21179. (or (org-element-property :contents-end element)
  21180. ;; Fake contents for source blocks.
  21181. (org-with-wide-buffer
  21182. (goto-char element-end)
  21183. (skip-chars-backward " \r\t\n")
  21184. (line-beginning-position)))
  21185. t)))
  21186. ;; Do not change items indentation individually as it
  21187. ;; might break the list as a whole. On the other
  21188. ;; hand, when at a plain list, indent it as a whole.
  21189. (cond ((eq type 'plain-list)
  21190. (let ((offset (- ind (org-get-indentation))))
  21191. (unless (zerop offset)
  21192. (indent-rigidly (org-element-property :begin element)
  21193. (org-element-property :end element)
  21194. offset))
  21195. (goto-char cbeg)))
  21196. ((eq type 'item) (goto-char cbeg))
  21197. (t (funcall indent-to ind cbeg)))
  21198. (when (< (point) end)
  21199. (case type
  21200. ((example-block export-block verse-block))
  21201. (src-block
  21202. ;; In a source block, indent source code
  21203. ;; according to language major mode, but only if
  21204. ;; `org-src-tab-acts-natively' is non-nil.
  21205. (when (and (< (point) end) org-src-tab-acts-natively)
  21206. (ignore-errors
  21207. (org-babel-do-in-edit-buffer
  21208. (indent-region (point-min) (point-max))))))
  21209. (t (org-indent-region (point) (min cend end))))
  21210. (goto-char (min cend end))
  21211. (when (< (point) end) (funcall indent-to ind element-end)))
  21212. (set-marker post nil)
  21213. (set-marker cbeg nil)
  21214. (set-marker cend nil))))
  21215. (set-marker element-end nil))))
  21216. (set-marker end nil))))
  21217. (defun org-indent-drawer ()
  21218. "Indent the drawer at point."
  21219. (interactive)
  21220. (unless (save-excursion
  21221. (beginning-of-line)
  21222. (org-looking-at-p org-drawer-regexp))
  21223. (user-error "Not at a drawer"))
  21224. (let ((element (org-element-at-point)))
  21225. (unless (memq (org-element-type element) '(drawer property-drawer))
  21226. (user-error "Not at a drawer"))
  21227. (org-with-wide-buffer
  21228. (org-indent-region (org-element-property :begin element)
  21229. (org-element-property :end element))))
  21230. (message "Drawer at point indented"))
  21231. (defun org-indent-block ()
  21232. "Indent the block at point."
  21233. (interactive)
  21234. (unless (save-excursion
  21235. (beginning-of-line)
  21236. (let ((case-fold-search t))
  21237. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21238. (user-error "Not at a block"))
  21239. (let ((element (org-element-at-point)))
  21240. (unless (memq (org-element-type element)
  21241. '(comment-block center-block dynamic-block example-block
  21242. export-block quote-block special-block
  21243. src-block verse-block))
  21244. (user-error "Not at a block"))
  21245. (org-with-wide-buffer
  21246. (org-indent-region (org-element-property :begin element)
  21247. (org-element-property :end element))))
  21248. (message "Block at point indented"))
  21249. ;;; Filling
  21250. ;; We use our own fill-paragraph and auto-fill functions.
  21251. ;; `org-fill-paragraph' relies on adaptive filling and context
  21252. ;; checking. Appropriate `fill-prefix' is computed with
  21253. ;; `org-adaptive-fill-function'.
  21254. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21255. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21256. ;; `org-adaptive-fill-function' value. Internally,
  21257. ;; `org-comment-line-break-function' breaks the line.
  21258. ;; `org-setup-filling' installs filling and auto-filling related
  21259. ;; variables during `org-mode' initialization.
  21260. (defvar org-element-paragraph-separate) ; org-element.el
  21261. (defun org-setup-filling ()
  21262. (require 'org-element)
  21263. ;; Prevent auto-fill from inserting unwanted new items.
  21264. (when (boundp 'fill-nobreak-predicate)
  21265. (org-set-local
  21266. 'fill-nobreak-predicate
  21267. (org-uniquify
  21268. (append fill-nobreak-predicate
  21269. '(org-fill-line-break-nobreak-p
  21270. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21271. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21272. (org-set-local 'paragraph-start paragraph-ending)
  21273. (org-set-local 'paragraph-separate paragraph-ending))
  21274. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21275. (org-set-local 'auto-fill-inhibit-regexp nil)
  21276. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21277. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21278. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21279. (defun org-fill-line-break-nobreak-p ()
  21280. "Non-nil when a new line at point would create an Org line break."
  21281. (save-excursion
  21282. (skip-chars-backward "[ \t]")
  21283. (skip-chars-backward "\\\\")
  21284. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21285. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21286. "Non-nil when a new line at point would split a timestamp."
  21287. (and (org-at-timestamp-p t)
  21288. (not (looking-at org-ts-regexp-both))))
  21289. (declare-function message-in-body-p "message" ())
  21290. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21291. (defun org-adaptive-fill-function ()
  21292. "Compute a fill prefix for the current line.
  21293. Return fill prefix, as a string, or nil if current line isn't
  21294. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21295. matches in paragraphs or comments, use it."
  21296. (catch 'exit
  21297. (when (derived-mode-p 'message-mode)
  21298. (save-excursion
  21299. (beginning-of-line)
  21300. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21301. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21302. ((looking-at message-cite-prefix-regexp)
  21303. (throw 'exit (match-string-no-properties 0)))
  21304. ((looking-at org-outline-regexp)
  21305. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21306. (org-with-wide-buffer
  21307. (unless (org-at-heading-p)
  21308. (let* ((p (line-beginning-position))
  21309. (element (save-excursion
  21310. (beginning-of-line)
  21311. (org-element-at-point)))
  21312. (type (org-element-type element))
  21313. (post-affiliated (org-element-property :post-affiliated element)))
  21314. (unless (< p post-affiliated)
  21315. (case type
  21316. (comment
  21317. (save-excursion
  21318. (beginning-of-line)
  21319. (looking-at "[ \t]*")
  21320. (concat (match-string 0) "# ")))
  21321. (footnote-definition "")
  21322. ((item plain-list)
  21323. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21324. (paragraph
  21325. ;; Fill prefix is usually the same as the current line,
  21326. ;; unless the paragraph is at the beginning of an item.
  21327. (let ((parent (org-element-property :parent element)))
  21328. (save-excursion
  21329. (beginning-of-line)
  21330. (cond ((eq (org-element-type parent) 'item)
  21331. (make-string (org-list-item-body-column
  21332. (org-element-property :begin parent))
  21333. ?\s))
  21334. ((and adaptive-fill-regexp
  21335. ;; Locally disable
  21336. ;; `adaptive-fill-function' to let
  21337. ;; `fill-context-prefix' handle
  21338. ;; `adaptive-fill-regexp' variable.
  21339. (let (adaptive-fill-function)
  21340. (fill-context-prefix
  21341. post-affiliated
  21342. (org-element-property :end element)))))
  21343. ((looking-at "[ \t]+") (match-string 0))
  21344. (t "")))))
  21345. (comment-block
  21346. ;; Only fill contents if P is within block boundaries.
  21347. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21348. (forward-line)
  21349. (point)))
  21350. (cend (save-excursion
  21351. (goto-char (org-element-property :end element))
  21352. (skip-chars-backward " \r\t\n")
  21353. (line-beginning-position))))
  21354. (when (and (>= p cbeg) (< p cend))
  21355. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21356. (match-string 0)
  21357. "")))))))))))
  21358. (declare-function message-goto-body "message" ())
  21359. (defvar message-cite-prefix-regexp) ; From message.el
  21360. (defun org-fill-paragraph (&optional justify)
  21361. "Fill element at point, when applicable.
  21362. This function only applies to comment blocks, comments, example
  21363. blocks and paragraphs. Also, as a special case, re-align table
  21364. when point is at one.
  21365. If JUSTIFY is non-nil (interactively, with prefix argument),
  21366. justify as well. If `sentence-end-double-space' is non-nil, then
  21367. period followed by one space does not end a sentence, so don't
  21368. break a line there. The variable `fill-column' controls the
  21369. width for filling.
  21370. For convenience, when point is at a plain list, an item or
  21371. a footnote definition, try to fill the first paragraph within."
  21372. (interactive)
  21373. (if (and (derived-mode-p 'message-mode)
  21374. (or (not (message-in-body-p))
  21375. (save-excursion (move-beginning-of-line 1)
  21376. (looking-at message-cite-prefix-regexp))))
  21377. ;; First ensure filling is correct in message-mode.
  21378. (let ((fill-paragraph-function
  21379. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21380. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21381. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21382. (paragraph-separate
  21383. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21384. (fill-paragraph nil))
  21385. (with-syntax-table org-mode-transpose-word-syntax-table
  21386. ;; Move to end of line in order to get the first paragraph
  21387. ;; within a plain list or a footnote definition.
  21388. (let ((element (save-excursion
  21389. (end-of-line)
  21390. (or (ignore-errors (org-element-at-point))
  21391. (user-error "An element cannot be parsed line %d"
  21392. (line-number-at-pos (point)))))))
  21393. ;; First check if point is in a blank line at the beginning of
  21394. ;; the buffer. In that case, ignore filling.
  21395. (case (org-element-type element)
  21396. ;; Use major mode filling function is src blocks.
  21397. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21398. ;; Align Org tables, leave table.el tables as-is.
  21399. (table-row (org-table-align) t)
  21400. (table
  21401. (when (eq (org-element-property :type element) 'org)
  21402. (save-excursion
  21403. (goto-char (org-element-property :post-affiliated element))
  21404. (org-table-align)))
  21405. t)
  21406. (paragraph
  21407. ;; Paragraphs may contain `line-break' type objects.
  21408. (let ((beg (max (point-min)
  21409. (org-element-property :contents-begin element)))
  21410. (end (min (point-max)
  21411. (org-element-property :contents-end element))))
  21412. ;; Do nothing if point is at an affiliated keyword.
  21413. (if (< (line-end-position) beg) t
  21414. (when (derived-mode-p 'message-mode)
  21415. ;; In `message-mode', do not fill following citation
  21416. ;; in current paragraph nor text before message body.
  21417. (let ((body-start (save-excursion (message-goto-body))))
  21418. (when body-start (setq beg (max body-start beg))))
  21419. (when (save-excursion
  21420. (re-search-forward
  21421. (concat "^" message-cite-prefix-regexp) end t))
  21422. (setq end (match-beginning 0))))
  21423. ;; Fill paragraph, taking line breaks into account.
  21424. (save-excursion
  21425. (goto-char beg)
  21426. (let ((cuts (list beg)))
  21427. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21428. (when (eq 'line-break
  21429. (org-element-type
  21430. (save-excursion (backward-char)
  21431. (org-element-context))))
  21432. (push (point) cuts)))
  21433. (dolist (c (delq end cuts))
  21434. (fill-region-as-paragraph c end justify)
  21435. (setq end c))))
  21436. t)))
  21437. ;; Contents of `comment-block' type elements should be
  21438. ;; filled as plain text, but only if point is within block
  21439. ;; markers.
  21440. (comment-block
  21441. (let* ((case-fold-search t)
  21442. (beg (save-excursion
  21443. (goto-char (org-element-property :begin element))
  21444. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21445. (forward-line)
  21446. (point)))
  21447. (end (save-excursion
  21448. (goto-char (org-element-property :end element))
  21449. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21450. (line-beginning-position))))
  21451. (if (or (< (point) beg) (> (point) end)) t
  21452. (fill-region-as-paragraph
  21453. (save-excursion (end-of-line)
  21454. (re-search-backward "^[ \t]*$" beg 'move)
  21455. (line-beginning-position))
  21456. (save-excursion (beginning-of-line)
  21457. (re-search-forward "^[ \t]*$" end 'move)
  21458. (line-beginning-position))
  21459. justify))))
  21460. ;; Fill comments.
  21461. (comment
  21462. (let ((begin (org-element-property :post-affiliated element))
  21463. (end (org-element-property :end element)))
  21464. (when (and (>= (point) begin) (<= (point) end))
  21465. (let ((begin (save-excursion
  21466. (end-of-line)
  21467. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21468. (progn (forward-line) (point))
  21469. begin)))
  21470. (end (save-excursion
  21471. (end-of-line)
  21472. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21473. (1- (line-beginning-position))
  21474. (skip-chars-backward " \r\t\n")
  21475. (line-end-position)))))
  21476. ;; Do not fill comments when at a blank line.
  21477. (when (> end begin)
  21478. (let ((fill-prefix
  21479. (save-excursion
  21480. (beginning-of-line)
  21481. (looking-at "[ \t]*#")
  21482. (let ((comment-prefix (match-string 0)))
  21483. (goto-char (match-end 0))
  21484. (if (looking-at adaptive-fill-regexp)
  21485. (concat comment-prefix (match-string 0))
  21486. (concat comment-prefix " "))))))
  21487. (save-excursion
  21488. (fill-region-as-paragraph begin end justify))))))
  21489. t))
  21490. ;; Ignore every other element.
  21491. (otherwise t))))))
  21492. (defun org-auto-fill-function ()
  21493. "Auto-fill function."
  21494. ;; Check if auto-filling is meaningful.
  21495. (let ((fc (current-fill-column)))
  21496. (when (and fc (> (current-column) fc))
  21497. (let* ((fill-prefix (org-adaptive-fill-function))
  21498. ;; Enforce empty fill prefix, if required. Otherwise, it
  21499. ;; will be computed again.
  21500. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21501. (when fill-prefix (do-auto-fill))))))
  21502. (defun org-comment-line-break-function (&optional soft)
  21503. "Break line at point and indent, continuing comment if within one.
  21504. The inserted newline is marked hard if variable
  21505. `use-hard-newlines' is true, unless optional argument SOFT is
  21506. non-nil."
  21507. (if soft (insert-and-inherit ?\n) (newline 1))
  21508. (save-excursion (forward-char -1) (delete-horizontal-space))
  21509. (delete-horizontal-space)
  21510. (indent-to-left-margin)
  21511. (insert-before-markers-and-inherit fill-prefix))
  21512. ;;; Fixed Width Areas
  21513. (defun org-toggle-fixed-width ()
  21514. "Toggle fixed-width markup.
  21515. Add or remove fixed-width markup on current line, whenever it
  21516. makes sense. Return an error otherwise.
  21517. If a region is active and if it contains only fixed-width areas
  21518. or blank lines, remove all fixed-width markup in it. If the
  21519. region contains anything else, convert all non-fixed-width lines
  21520. to fixed-width ones.
  21521. Blank lines at the end of the region are ignored unless the
  21522. region only contains such lines."
  21523. (interactive)
  21524. (if (not (org-region-active-p))
  21525. ;; No region:
  21526. ;;
  21527. ;; Remove fixed width marker only in a fixed-with element.
  21528. ;;
  21529. ;; Add fixed width maker in paragraphs, in blank lines after
  21530. ;; elements or at the beginning of a headline or an inlinetask,
  21531. ;; and before any one-line elements (e.g., a clock).
  21532. (progn
  21533. (beginning-of-line)
  21534. (let* ((element (org-element-at-point))
  21535. (type (org-element-type element)))
  21536. (cond
  21537. ((and (eq type 'fixed-width)
  21538. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21539. (replace-match
  21540. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21541. ((and (memq type '(babel-call clock comment diary-sexp headline
  21542. horizontal-rule keyword paragraph
  21543. planning))
  21544. (<= (org-element-property :post-affiliated element) (point)))
  21545. (skip-chars-forward " \t")
  21546. (insert ": "))
  21547. ((and (org-looking-at-p "[ \t]*$")
  21548. (or (eq type 'inlinetask)
  21549. (save-excursion
  21550. (skip-chars-forward " \r\t\n")
  21551. (<= (org-element-property :end element) (point)))))
  21552. (delete-region (point) (line-end-position))
  21553. (org-indent-line)
  21554. (insert ": "))
  21555. (t (user-error "Cannot insert a fixed-width line here")))))
  21556. ;; Region active.
  21557. (let* ((begin (save-excursion
  21558. (goto-char (region-beginning))
  21559. (line-beginning-position)))
  21560. (end (copy-marker
  21561. (save-excursion
  21562. (goto-char (region-end))
  21563. (unless (eolp) (beginning-of-line))
  21564. (if (save-excursion (re-search-backward "\\S-" begin t))
  21565. (progn (skip-chars-backward " \r\t\n") (point))
  21566. (point)))))
  21567. (all-fixed-width-p
  21568. (catch 'not-all-p
  21569. (save-excursion
  21570. (goto-char begin)
  21571. (skip-chars-forward " \r\t\n")
  21572. (when (eobp) (throw 'not-all-p nil))
  21573. (while (< (point) end)
  21574. (let ((element (org-element-at-point)))
  21575. (if (eq (org-element-type element) 'fixed-width)
  21576. (goto-char (org-element-property :end element))
  21577. (throw 'not-all-p nil))))
  21578. t))))
  21579. (if all-fixed-width-p
  21580. (save-excursion
  21581. (goto-char begin)
  21582. (while (< (point) end)
  21583. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21584. (replace-match
  21585. "" nil nil nil
  21586. (if (= (line-end-position) (match-end 0)) 0 1)))
  21587. (forward-line)))
  21588. (let ((min-ind (point-max)))
  21589. ;; Find minimum indentation across all lines.
  21590. (save-excursion
  21591. (goto-char begin)
  21592. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21593. (setq min-ind 0)
  21594. (catch 'zerop
  21595. (while (< (point) end)
  21596. (unless (org-looking-at-p "[ \t]*$")
  21597. (let ((ind (org-get-indentation)))
  21598. (setq min-ind (min min-ind ind))
  21599. (when (zerop ind) (throw 'zerop t))))
  21600. (forward-line)))))
  21601. ;; Loop over all lines and add fixed-width markup everywhere
  21602. ;; but in fixed-width lines.
  21603. (save-excursion
  21604. (goto-char begin)
  21605. (while (< (point) end)
  21606. (cond
  21607. ((org-at-heading-p)
  21608. (insert ": ")
  21609. (forward-line)
  21610. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21611. (insert ":")
  21612. (forward-line)))
  21613. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21614. (let* ((element (org-element-at-point))
  21615. (element-end (org-element-property :end element)))
  21616. (if (eq (org-element-type element) 'fixed-width)
  21617. (progn (goto-char element-end)
  21618. (skip-chars-backward " \r\t\n")
  21619. (forward-line))
  21620. (let ((limit (min end element-end)))
  21621. (while (< (point) limit)
  21622. (org-move-to-column min-ind t)
  21623. (insert ": ")
  21624. (forward-line))))))
  21625. (t
  21626. (org-move-to-column min-ind t)
  21627. (insert ": ")
  21628. (forward-line)))))))
  21629. (set-marker end nil))))
  21630. ;;; Comments
  21631. ;; Org comments syntax is quite complex. It requires the entire line
  21632. ;; to be just a comment. Also, even with the right syntax at the
  21633. ;; beginning of line, some some elements (i.e. verse-block or
  21634. ;; example-block) don't accept comments. Usual Emacs comment commands
  21635. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21636. ;; Org still relies on `comment-dwim', but cannot trust
  21637. ;; `comment-only-p'. So, `comment-region-function' and
  21638. ;; `uncomment-region-function' both point
  21639. ;; to`org-comment-or-uncomment-region'. Eventually,
  21640. ;; `org-insert-comment' takes care of insertion of comments at the
  21641. ;; beginning of line.
  21642. ;; `org-setup-comments-handling' install comments related variables
  21643. ;; during `org-mode' initialization.
  21644. (defun org-setup-comments-handling ()
  21645. (interactive)
  21646. (org-set-local 'comment-use-syntax nil)
  21647. (org-set-local 'comment-start "# ")
  21648. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21649. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21650. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21651. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21652. (defun org-insert-comment ()
  21653. "Insert an empty comment above current line.
  21654. If the line is empty, insert comment at its beginning. When
  21655. point is within a source block, comment according to the related
  21656. major mode."
  21657. (if (let ((element (org-element-at-point)))
  21658. (and (eq (org-element-type element) 'src-block)
  21659. (< (save-excursion
  21660. (goto-char (org-element-property :post-affiliated element))
  21661. (line-end-position))
  21662. (point))
  21663. (> (save-excursion
  21664. (goto-char (org-element-property :end element))
  21665. (skip-chars-backward " \r\t\n")
  21666. (line-beginning-position))
  21667. (point))))
  21668. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21669. (beginning-of-line)
  21670. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21671. (open-line 1))
  21672. (org-indent-line)
  21673. (insert "# ")))
  21674. (defvar comment-empty-lines) ; From newcomment.el.
  21675. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  21676. "Comment or uncomment each non-blank line in the region.
  21677. Uncomment each non-blank line between BEG and END if it only
  21678. contains commented lines. Otherwise, comment them. If region is
  21679. strictly within a source block, use appropriate comment syntax."
  21680. (if (let ((element (org-element-at-point)))
  21681. (and (eq (org-element-type element) 'src-block)
  21682. (< (save-excursion
  21683. (goto-char (org-element-property :post-affiliated element))
  21684. (line-end-position))
  21685. beg)
  21686. (>= (save-excursion
  21687. (goto-char (org-element-property :end element))
  21688. (skip-chars-backward " \r\t\n")
  21689. (line-beginning-position))
  21690. end)))
  21691. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21692. (save-restriction
  21693. ;; Restrict region
  21694. (narrow-to-region (save-excursion (goto-char beg)
  21695. (skip-chars-forward " \r\t\n" end)
  21696. (line-beginning-position))
  21697. (save-excursion (goto-char end)
  21698. (skip-chars-backward " \r\t\n" beg)
  21699. (line-end-position)))
  21700. (let ((uncommentp
  21701. ;; UNCOMMENTP is non-nil when every non blank line between
  21702. ;; BEG and END is a comment.
  21703. (save-excursion
  21704. (goto-char (point-min))
  21705. (while (and (not (eobp))
  21706. (let ((element (org-element-at-point)))
  21707. (and (eq (org-element-type element) 'comment)
  21708. (goto-char (min (point-max)
  21709. (org-element-property
  21710. :end element)))))))
  21711. (eobp))))
  21712. (if uncommentp
  21713. ;; Only blank lines and comments in region: uncomment it.
  21714. (save-excursion
  21715. (goto-char (point-min))
  21716. (while (not (eobp))
  21717. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21718. (replace-match "" nil nil nil 1))
  21719. (forward-line)))
  21720. ;; Comment each line in region.
  21721. (let ((min-indent (point-max)))
  21722. ;; First find the minimum indentation across all lines.
  21723. (save-excursion
  21724. (goto-char (point-min))
  21725. (while (and (not (eobp)) (not (zerop min-indent)))
  21726. (unless (looking-at "[ \t]*$")
  21727. (setq min-indent (min min-indent (current-indentation))))
  21728. (forward-line)))
  21729. ;; Then loop over all lines.
  21730. (save-excursion
  21731. (goto-char (point-min))
  21732. (while (not (eobp))
  21733. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21734. ;; Don't get fooled by invisible text (e.g. link path)
  21735. ;; when moving to column MIN-INDENT.
  21736. (let ((buffer-invisibility-spec nil))
  21737. (org-move-to-column min-indent t))
  21738. (insert comment-start))
  21739. (forward-line)))))))))
  21740. (defun org-comment-dwim (arg)
  21741. "Call `comment-dwim' within a source edit buffer if needed."
  21742. (interactive "*P")
  21743. (if (org-in-src-block-p)
  21744. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21745. (call-interactively 'comment-dwim)))
  21746. ;;; Timestamps API
  21747. ;; This section contains tools to operate on timestamp objects, as
  21748. ;; returned by, e.g. `org-element-context'.
  21749. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21750. "Encode TIMESTAMP object into Emacs internal time.
  21751. Use end of date range or time range when END is non-nil."
  21752. (apply #'encode-time
  21753. (cons 0
  21754. (mapcar
  21755. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21756. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21757. '(:minute-start :hour-start :day-start :month-start
  21758. :year-start))))))
  21759. (defun org-timestamp-has-time-p (timestamp)
  21760. "Non-nil when TIMESTAMP has a time specified."
  21761. (org-element-property :hour-start timestamp))
  21762. (defun org-timestamp-format (timestamp format &optional end utc)
  21763. "Format a TIMESTAMP object into a string.
  21764. FORMAT is a format specifier to be passed to
  21765. `format-time-string'.
  21766. When optional argument END is non-nil, use end of date-range or
  21767. time-range, if possible.
  21768. When optional argument UTC is non-nil, time will be expressed as
  21769. Universal Time."
  21770. (format-time-string
  21771. format (org-timestamp--to-internal-time timestamp end) utc))
  21772. (defun org-timestamp-split-range (timestamp &optional end)
  21773. "Extract a TIMESTAMP object from a date or time range.
  21774. END, when non-nil, means extract the end of the range.
  21775. Otherwise, extract its start.
  21776. Return a new timestamp object."
  21777. (let ((type (org-element-property :type timestamp)))
  21778. (if (memq type '(active inactive diary)) timestamp
  21779. (let ((split-ts (org-element-copy timestamp)))
  21780. ;; Set new type.
  21781. (org-element-put-property
  21782. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21783. ;; Copy start properties over end properties if END is
  21784. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21785. (let ((p-alist '((:minute-start . :minute-end)
  21786. (:hour-start . :hour-end)
  21787. (:day-start . :day-end)
  21788. (:month-start . :month-end)
  21789. (:year-start . :year-end))))
  21790. (dolist (p-cell p-alist)
  21791. (org-element-put-property
  21792. split-ts
  21793. (funcall (if end #'car #'cdr) p-cell)
  21794. (org-element-property
  21795. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21796. ;; Eventually refresh `:raw-value'.
  21797. (org-element-put-property split-ts :raw-value nil)
  21798. (org-element-put-property
  21799. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21800. (defun org-timestamp-translate (timestamp &optional boundary)
  21801. "Translate TIMESTAMP object to custom format.
  21802. Format string is defined in `org-time-stamp-custom-formats',
  21803. which see.
  21804. When optional argument BOUNDARY is non-nil, it is either the
  21805. symbol `start' or `end'. In this case, only translate the
  21806. starting or ending part of TIMESTAMP if it is a date or time
  21807. range. Otherwise, translate both parts.
  21808. Return timestamp as-is if `org-display-custom-times' is nil or if
  21809. it has a `diary' type."
  21810. (let ((type (org-element-property :type timestamp)))
  21811. (if (or (not org-display-custom-times) (eq type 'diary))
  21812. (org-element-interpret-data timestamp)
  21813. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21814. org-time-stamp-custom-formats)))
  21815. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21816. (concat (org-timestamp-format timestamp fmt)
  21817. "--"
  21818. (org-timestamp-format timestamp fmt t))
  21819. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21820. ;;; Other stuff.
  21821. (defun org-reftex-citation ()
  21822. "Use reftex-citation to insert a citation into the buffer.
  21823. This looks for a line like
  21824. #+BIBLIOGRAPHY: foo plain option:-d
  21825. and derives from it that foo.bib is the bibliography file relevant
  21826. for this document. It then installs the necessary environment for RefTeX
  21827. to work in this buffer and calls `reftex-citation' to insert a citation
  21828. into the buffer.
  21829. Export of such citations to both LaTeX and HTML is handled by the contributed
  21830. package ox-bibtex by Taru Karttunen."
  21831. (interactive)
  21832. (let ((reftex-docstruct-symbol 'rds)
  21833. (reftex-cite-format "\\cite{%l}")
  21834. rds bib)
  21835. (save-excursion
  21836. (save-restriction
  21837. (widen)
  21838. (let ((case-fold-search t)
  21839. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21840. (if (not (save-excursion
  21841. (or (re-search-forward re nil t)
  21842. (re-search-backward re nil t))))
  21843. (user-error "No bibliography defined in file")
  21844. (setq bib (concat (match-string 1) ".bib")
  21845. rds (list (list 'bib bib)))))))
  21846. (call-interactively 'reftex-citation)))
  21847. ;;;; Functions extending outline functionality
  21848. (defun org-beginning-of-line (&optional arg)
  21849. "Go to the beginning of the current line. If that is invisible, continue
  21850. to a visible line beginning. This makes the function of C-a more intuitive.
  21851. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21852. first attempt, and only move to after the tags when the cursor is already
  21853. beyond the end of the headline."
  21854. (interactive "P")
  21855. (let ((pos (point))
  21856. (special (if (consp org-special-ctrl-a/e)
  21857. (car org-special-ctrl-a/e)
  21858. org-special-ctrl-a/e))
  21859. deactivate-mark refpos)
  21860. (if (org-bound-and-true-p visual-line-mode)
  21861. (beginning-of-visual-line 1)
  21862. (beginning-of-line 1))
  21863. (if (and arg (fboundp 'move-beginning-of-line))
  21864. (call-interactively 'move-beginning-of-line)
  21865. (if (bobp)
  21866. nil
  21867. (backward-char 1)
  21868. (if (org-truely-invisible-p)
  21869. (while (and (not (bobp)) (org-truely-invisible-p))
  21870. (backward-char 1)
  21871. (beginning-of-line 1))
  21872. (forward-char 1))))
  21873. (when special
  21874. (cond
  21875. ((and (looking-at org-complex-heading-regexp)
  21876. (= (char-after (match-end 1)) ?\ ))
  21877. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21878. (point-at-eol)))
  21879. (goto-char
  21880. (if (eq special t)
  21881. (cond ((> pos refpos) refpos)
  21882. ((= pos (point)) refpos)
  21883. (t (point)))
  21884. (cond ((> pos (point)) (point))
  21885. ((not (eq last-command this-command)) (point))
  21886. (t refpos)))))
  21887. ((org-at-item-p)
  21888. ;; Being at an item and not looking at an the item means point
  21889. ;; was previously moved to beginning of a visual line, which
  21890. ;; doesn't contain the item. Therefore, do nothing special,
  21891. ;; just stay here.
  21892. (when (looking-at org-list-full-item-re)
  21893. ;; Set special position at first white space character after
  21894. ;; bullet, and check-box, if any.
  21895. (let ((after-bullet
  21896. (let ((box (match-end 3)))
  21897. (if (not box) (match-end 1)
  21898. (let ((after (char-after box)))
  21899. (if (and after (= after ? )) (1+ box) box))))))
  21900. ;; Special case: Move point to special position when
  21901. ;; currently after it or at beginning of line.
  21902. (if (eq special t)
  21903. (when (or (> pos after-bullet) (= (point) pos))
  21904. (goto-char after-bullet))
  21905. ;; Reversed case: Move point to special position when
  21906. ;; point was already at beginning of line and command is
  21907. ;; repeated.
  21908. (when (and (= (point) pos) (eq last-command this-command))
  21909. (goto-char after-bullet))))))))
  21910. (org-no-warnings
  21911. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21912. (setq disable-point-adjustment
  21913. (or (not (invisible-p (point)))
  21914. (not (invisible-p (max (point-min) (1- (point))))))))
  21915. (defun org-end-of-line (&optional arg)
  21916. "Go to the end of the line.
  21917. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21918. tags on the first attempt, and only move to after the tags when
  21919. the cursor is already beyond the end of the headline."
  21920. (interactive "P")
  21921. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21922. org-special-ctrl-a/e))
  21923. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21924. 'end-of-visual-line)
  21925. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21926. (t 'end-of-line)))
  21927. deactivate-mark)
  21928. (if (or (not special) arg) (call-interactively move-fun)
  21929. (let* ((element (save-excursion (beginning-of-line)
  21930. (org-element-at-point)))
  21931. (type (org-element-type element)))
  21932. (cond
  21933. ((memq type '(headline inlinetask))
  21934. (let ((pos (point)))
  21935. (beginning-of-line 1)
  21936. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21937. (if (eq special t)
  21938. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21939. (goto-char (match-beginning 1))
  21940. (goto-char (match-end 0)))
  21941. (if (or (< pos (match-end 0))
  21942. (not (eq this-command last-command)))
  21943. (goto-char (match-end 0))
  21944. (goto-char (match-beginning 1))))
  21945. (call-interactively move-fun))))
  21946. ((outline-invisible-p (line-end-position))
  21947. ;; If element is hidden, `move-end-of-line' would put point
  21948. ;; after it. Use `end-of-line' to stay on current line.
  21949. (call-interactively 'end-of-line))
  21950. (t (call-interactively move-fun)))))
  21951. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21952. (setq disable-point-adjustment
  21953. (or (not (invisible-p (point)))
  21954. (not (invisible-p (max (point-min) (1- (point))))))))
  21955. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21956. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21957. (defun org-backward-sentence (&optional arg)
  21958. "Go to beginning of sentence, or beginning of table field.
  21959. This will call `backward-sentence' or `org-table-beginning-of-field',
  21960. depending on context."
  21961. (interactive "P")
  21962. (let* ((element (org-element-at-point))
  21963. (contents-begin (org-element-property :contents-begin element))
  21964. (table (org-element-lineage element '(table) t)))
  21965. (if (and table
  21966. (> (point) contents-begin)
  21967. (<= (point) (org-element-property :contents-end table)))
  21968. (call-interactively #'org-table-beginning-of-field)
  21969. (save-restriction
  21970. (when (and contents-begin
  21971. (< (point-min) contents-begin)
  21972. (> (point) contents-begin))
  21973. (narrow-to-region contents-begin
  21974. (org-element-property :contents-end element)))
  21975. (call-interactively #'backward-sentence)))))
  21976. (defun org-forward-sentence (&optional arg)
  21977. "Go to end of sentence, or end of table field.
  21978. This will call `forward-sentence' or `org-table-end-of-field',
  21979. depending on context."
  21980. (interactive "P")
  21981. (let* ((element (org-element-at-point))
  21982. (contents-end (org-element-property :contents-end element))
  21983. (table (org-element-lineage element '(table) t)))
  21984. (if (and table
  21985. (>= (point) (org-element-property :contents-begin table))
  21986. (< (point) contents-end))
  21987. (call-interactively #'org-table-end-of-field)
  21988. (save-restriction
  21989. (when (and contents-end
  21990. (> (point-max) contents-end)
  21991. ;; Skip blank lines between elements.
  21992. (< (org-element-property :end element)
  21993. (save-excursion (goto-char contents-end)
  21994. (skip-chars-forward " \r\t\n"))))
  21995. (narrow-to-region (org-element-property :contents-begin element)
  21996. contents-end))
  21997. (call-interactively #'forward-sentence)))))
  21998. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21999. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22000. (defun org-kill-line (&optional arg)
  22001. "Kill line, to tags or end of line."
  22002. (interactive "P")
  22003. (cond
  22004. ((or (not org-special-ctrl-k)
  22005. (bolp)
  22006. (not (org-at-heading-p)))
  22007. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22008. org-ctrl-k-protect-subtree)
  22009. (if (or (eq org-ctrl-k-protect-subtree 'error)
  22010. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  22011. (user-error "C-k aborted as it would kill a hidden subtree")))
  22012. (call-interactively
  22013. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22014. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22015. (kill-region (point) (match-beginning 1))
  22016. (org-set-tags nil t))
  22017. (t (kill-region (point) (point-at-eol)))))
  22018. (define-key org-mode-map "\C-k" 'org-kill-line)
  22019. (defun org-yank (&optional arg)
  22020. "Yank. If the kill is a subtree, treat it specially.
  22021. This command will look at the current kill and check if is a single
  22022. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22023. cursor is at the beginning of a line or after the stars of a currently
  22024. empty headline, then the yank is handled specially. How exactly depends
  22025. on the value of the following variables, both set by default.
  22026. `org-yank-folded-subtrees'
  22027. When set, the subtree(s) will be folded after insertion, but only
  22028. if doing so would now swallow text after the yanked text.
  22029. `org-yank-adjusted-subtrees'
  22030. When set, the subtree will be promoted or demoted in order to
  22031. fit into the local outline tree structure, which means that the
  22032. level will be adjusted so that it becomes the smaller one of the
  22033. two *visible* surrounding headings.
  22034. Any prefix to this command will cause `yank' to be called directly with
  22035. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22036. will just
  22037. plainly yank the text as it is.
  22038. \[1] The test checks if the first non-white line is a heading
  22039. and if there are no other headings with fewer stars."
  22040. (interactive "P")
  22041. (org-yank-generic 'yank arg))
  22042. (defun org-yank-generic (command arg)
  22043. "Perform some yank-like command.
  22044. This function implements the behavior described in the `org-yank'
  22045. documentation. However, it has been generalized to work for any
  22046. interactive command with similar behavior."
  22047. ;; pretend to be command COMMAND
  22048. (setq this-command command)
  22049. (if arg
  22050. (call-interactively command)
  22051. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22052. (and (org-kill-is-subtree-p)
  22053. (or (bolp)
  22054. (and (looking-at "[ \t]*$")
  22055. (string-match
  22056. "\\`\\*+\\'"
  22057. (buffer-substring (point-at-bol) (point)))))))
  22058. swallowp)
  22059. (cond
  22060. ((and subtreep org-yank-folded-subtrees)
  22061. (let ((beg (point))
  22062. end)
  22063. (if (and subtreep org-yank-adjusted-subtrees)
  22064. (org-paste-subtree nil nil 'for-yank)
  22065. (call-interactively command))
  22066. (setq end (point))
  22067. (goto-char beg)
  22068. (when (and (bolp) subtreep
  22069. (not (setq swallowp
  22070. (org-yank-folding-would-swallow-text beg end))))
  22071. (org-with-limited-levels
  22072. (or (looking-at org-outline-regexp)
  22073. (re-search-forward org-outline-regexp-bol end t))
  22074. (while (and (< (point) end) (looking-at org-outline-regexp))
  22075. (hide-subtree)
  22076. (org-cycle-show-empty-lines 'folded)
  22077. (condition-case nil
  22078. (outline-forward-same-level 1)
  22079. (error (goto-char end))))))
  22080. (when swallowp
  22081. (message
  22082. "Inserted text not folded because that would swallow text"))
  22083. (goto-char end)
  22084. (skip-chars-forward " \t\n\r")
  22085. (beginning-of-line 1)
  22086. (push-mark beg 'nomsg)))
  22087. ((and subtreep org-yank-adjusted-subtrees)
  22088. (let ((beg (point-at-bol)))
  22089. (org-paste-subtree nil nil 'for-yank)
  22090. (push-mark beg 'nomsg)))
  22091. (t
  22092. (call-interactively command))))))
  22093. (defun org-yank-folding-would-swallow-text (beg end)
  22094. "Would hide-subtree at BEG swallow any text after END?"
  22095. (let (level)
  22096. (org-with-limited-levels
  22097. (save-excursion
  22098. (goto-char beg)
  22099. (when (or (looking-at org-outline-regexp)
  22100. (re-search-forward org-outline-regexp-bol end t))
  22101. (setq level (org-outline-level)))
  22102. (goto-char end)
  22103. (skip-chars-forward " \t\r\n\v\f")
  22104. (if (or (eobp)
  22105. (and (bolp) (looking-at org-outline-regexp)
  22106. (<= (org-outline-level) level)))
  22107. nil ; Nothing would be swallowed
  22108. t))))) ; something would swallow
  22109. (define-key org-mode-map "\C-y" 'org-yank)
  22110. (defun org-truely-invisible-p ()
  22111. "Check if point is at a character currently not visible.
  22112. This version does not only check the character property, but also
  22113. `visible-mode'."
  22114. ;; Early versions of noutline don't have `outline-invisible-p'.
  22115. (if (org-bound-and-true-p visible-mode)
  22116. nil
  22117. (outline-invisible-p)))
  22118. (defun org-invisible-p2 ()
  22119. "Check if point is at a character currently not visible."
  22120. (save-excursion
  22121. (if (and (eolp) (not (bobp))) (backward-char 1))
  22122. ;; Early versions of noutline don't have `outline-invisible-p'.
  22123. (outline-invisible-p)))
  22124. (defun org-back-to-heading (&optional invisible-ok)
  22125. "Call `outline-back-to-heading', but provide a better error message."
  22126. (condition-case nil
  22127. (outline-back-to-heading invisible-ok)
  22128. (error (error "Before first headline at position %d in buffer %s"
  22129. (point) (current-buffer)))))
  22130. (defun org-before-first-heading-p ()
  22131. "Before first heading?"
  22132. (save-excursion
  22133. (end-of-line)
  22134. (null (re-search-backward org-outline-regexp-bol nil t))))
  22135. (defun org-at-heading-p (&optional ignored)
  22136. (outline-on-heading-p t))
  22137. ;; Compatibility alias with Org versions < 7.8.03
  22138. (defalias 'org-on-heading-p 'org-at-heading-p)
  22139. (defun org-in-commented-heading-p (&optional no-inheritance)
  22140. "Non-nil if point is under a commented heading.
  22141. This function also checks ancestors of the current headline,
  22142. unless optional argument NO-INHERITANCE is non-nil."
  22143. (cond
  22144. ((org-before-first-heading-p) nil)
  22145. ((let ((headline (nth 4 (org-heading-components))))
  22146. (and headline
  22147. (let ((case-fold-search nil))
  22148. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22149. headline)))))
  22150. (no-inheritance nil)
  22151. (t
  22152. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22153. (defun org-at-comment-p nil
  22154. "Is cursor in a commented line?"
  22155. (save-excursion
  22156. (save-match-data
  22157. (beginning-of-line)
  22158. (looking-at "^[ \t]*# "))))
  22159. (defun org-at-drawer-p nil
  22160. "Is cursor at a drawer keyword?"
  22161. (save-excursion
  22162. (move-beginning-of-line 1)
  22163. (looking-at org-drawer-regexp)))
  22164. (defun org-at-block-p nil
  22165. "Is cursor at a block keyword?"
  22166. (save-excursion
  22167. (move-beginning-of-line 1)
  22168. (looking-at org-block-regexp)))
  22169. (defun org-point-at-end-of-empty-headline ()
  22170. "If point is at the end of an empty headline, return t, else nil.
  22171. If the heading only contains a TODO keyword, it is still still considered
  22172. empty."
  22173. (and (looking-at "[ \t]*$")
  22174. (when org-todo-line-regexp
  22175. (save-excursion
  22176. (beginning-of-line 1)
  22177. (let ((case-fold-search nil))
  22178. (looking-at org-todo-line-regexp)
  22179. (string= (match-string 3) ""))))))
  22180. (defun org-at-heading-or-item-p ()
  22181. (or (org-at-heading-p) (org-at-item-p)))
  22182. (defun org-at-target-p ()
  22183. (or (org-in-regexp org-radio-target-regexp)
  22184. (org-in-regexp org-target-regexp)))
  22185. ;; Compatibility alias with Org versions < 7.8.03
  22186. (defalias 'org-on-target-p 'org-at-target-p)
  22187. (defun org-up-heading-all (arg)
  22188. "Move to the heading line of which the present line is a subheading.
  22189. This function considers both visible and invisible heading lines.
  22190. With argument, move up ARG levels."
  22191. (if (fboundp 'outline-up-heading-all)
  22192. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22193. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22194. (defun org-up-heading-safe ()
  22195. "Move to the heading line of which the present line is a subheading.
  22196. This version will not throw an error. It will return the level of the
  22197. headline found, or nil if no higher level is found.
  22198. Also, this function will be a lot faster than `outline-up-heading',
  22199. because it relies on stars being the outline starters. This can really
  22200. make a significant difference in outlines with very many siblings."
  22201. (when (ignore-errors (org-back-to-heading t))
  22202. (let ((level-up (1- (funcall outline-level))))
  22203. (and (> level-up 0)
  22204. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22205. (funcall outline-level)))))
  22206. (defun org-first-sibling-p ()
  22207. "Is this heading the first child of its parents?"
  22208. (interactive)
  22209. (let ((re org-outline-regexp-bol)
  22210. level l)
  22211. (unless (org-at-heading-p t)
  22212. (user-error "Not at a heading"))
  22213. (setq level (funcall outline-level))
  22214. (save-excursion
  22215. (if (not (re-search-backward re nil t))
  22216. t
  22217. (setq l (funcall outline-level))
  22218. (< l level)))))
  22219. (defun org-goto-sibling (&optional previous)
  22220. "Goto the next sibling, even if it is invisible.
  22221. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22222. when a sibling was found. When none is found, return nil and don't
  22223. move point."
  22224. (let ((fun (if previous 're-search-backward 're-search-forward))
  22225. (pos (point))
  22226. (re org-outline-regexp-bol)
  22227. level l)
  22228. (when (ignore-errors (org-back-to-heading t))
  22229. (setq level (funcall outline-level))
  22230. (catch 'exit
  22231. (or previous (forward-char 1))
  22232. (while (funcall fun re nil t)
  22233. (setq l (funcall outline-level))
  22234. (when (< l level) (goto-char pos) (throw 'exit nil))
  22235. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22236. (goto-char pos)
  22237. nil))))
  22238. (defun org-show-siblings ()
  22239. "Show all siblings of the current headline."
  22240. (save-excursion
  22241. (while (org-goto-sibling) (org-flag-heading nil)))
  22242. (save-excursion
  22243. (while (org-goto-sibling 'previous)
  22244. (org-flag-heading nil))))
  22245. (defun org-goto-first-child ()
  22246. "Goto the first child, even if it is invisible.
  22247. Return t when a child was found. Otherwise don't move point and
  22248. return nil."
  22249. (let (level (pos (point)) (re org-outline-regexp-bol))
  22250. (when (ignore-errors (org-back-to-heading t))
  22251. (setq level (outline-level))
  22252. (forward-char 1)
  22253. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22254. (progn (goto-char (match-beginning 0)) t)
  22255. (goto-char pos) nil))))
  22256. (defun org-show-hidden-entry ()
  22257. "Show an entry where even the heading is hidden."
  22258. (save-excursion
  22259. (org-show-entry)))
  22260. (defun org-flag-heading (flag &optional entry)
  22261. "Flag the current heading. FLAG non-nil means make invisible.
  22262. When ENTRY is non-nil, show the entire entry."
  22263. (save-excursion
  22264. (org-back-to-heading t)
  22265. ;; Check if we should show the entire entry
  22266. (if entry
  22267. (progn
  22268. (org-show-entry)
  22269. (save-excursion
  22270. (and (outline-next-heading)
  22271. (org-flag-heading nil))))
  22272. (outline-flag-region (max (point-min) (1- (point)))
  22273. (save-excursion (outline-end-of-heading) (point))
  22274. flag))))
  22275. (defun org-get-next-sibling ()
  22276. "Move to next heading of the same level, and return point.
  22277. If there is no such heading, return nil.
  22278. This is like outline-next-sibling, but invisible headings are ok."
  22279. (let ((level (funcall outline-level)))
  22280. (outline-next-heading)
  22281. (while (and (not (eobp)) (> (funcall outline-level) level))
  22282. (outline-next-heading))
  22283. (if (or (eobp) (< (funcall outline-level) level))
  22284. nil
  22285. (point))))
  22286. (defun org-get-last-sibling ()
  22287. "Move to previous heading of the same level, and return point.
  22288. If there is no such heading, return nil."
  22289. (let ((opoint (point))
  22290. (level (funcall outline-level)))
  22291. (outline-previous-heading)
  22292. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22293. (while (and (> (funcall outline-level) level)
  22294. (not (bobp)))
  22295. (outline-previous-heading))
  22296. (if (< (funcall outline-level) level)
  22297. nil
  22298. (point)))))
  22299. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22300. "Goto to the end of a subtree."
  22301. ;; This contains an exact copy of the original function, but it uses
  22302. ;; `org-back-to-heading', to make it work also in invisible
  22303. ;; trees. And is uses an invisible-ok argument.
  22304. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22305. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22306. ;; with many children and grandchildren etc, this can be much faster
  22307. ;; than the outline version.
  22308. (org-back-to-heading invisible-ok)
  22309. (let ((first t)
  22310. (level (funcall outline-level)))
  22311. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22312. ;; A true heading (not a plain list item), in Org-mode
  22313. ;; This means we can easily find the end by looking
  22314. ;; only for the right number of stars. Using a regexp to do
  22315. ;; this is so much faster than using a Lisp loop.
  22316. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22317. (forward-char 1)
  22318. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22319. ;; something else, do it the slow way
  22320. (while (and (not (eobp))
  22321. (or first (> (funcall outline-level) level)))
  22322. (setq first nil)
  22323. (outline-next-heading)))
  22324. (unless to-heading
  22325. (if (memq (preceding-char) '(?\n ?\^M))
  22326. (progn
  22327. ;; Go to end of line before heading
  22328. (forward-char -1)
  22329. (if (memq (preceding-char) '(?\n ?\^M))
  22330. ;; leave blank line before heading
  22331. (forward-char -1))))))
  22332. (point))
  22333. (defun org-end-of-meta-data (&optional full)
  22334. "Skip planning line and properties drawer in current entry.
  22335. When optional argument FULL is non-nil, also skip empty lines,
  22336. clocking lines and regular drawers at the beginning of the
  22337. entry."
  22338. (org-back-to-heading t)
  22339. (forward-line)
  22340. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22341. (when (looking-at org-property-drawer-re)
  22342. (goto-char (match-end 0))
  22343. (forward-line))
  22344. (when (and full (not (org-at-heading-p)))
  22345. (catch 'exit
  22346. (let ((end (save-excursion (outline-next-heading) (point)))
  22347. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22348. (while (not (eobp))
  22349. (cond ((org-looking-at-p org-drawer-regexp)
  22350. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22351. (forward-line)
  22352. (throw 'exit t)))
  22353. ((org-looking-at-p re) (forward-line))
  22354. (t (throw 'exit t))))))))
  22355. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22356. "Move forward to the ARG'th subheading at same level as this one.
  22357. Stop at the first and last subheadings of a superior heading.
  22358. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22359. non-nil it will also look at invisible ones."
  22360. (interactive "p")
  22361. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22362. (if (and arg (< arg 0))
  22363. (goto-char (point-min))
  22364. (outline-next-heading))
  22365. (org-at-heading-p)
  22366. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22367. (f (if (and arg (< arg 0))
  22368. 're-search-backward
  22369. 're-search-forward))
  22370. (count (if arg (abs arg) 1))
  22371. (result (point)))
  22372. (while (and (prog1 (> count 0)
  22373. (forward-char (if (and arg (< arg 0)) -1 1)))
  22374. (funcall f org-outline-regexp-bol nil 'move))
  22375. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22376. (cond ((< l level) (setq count 0))
  22377. ((and (= l level)
  22378. (or invisible-ok
  22379. (progn
  22380. (goto-char (line-beginning-position))
  22381. (not (outline-invisible-p)))))
  22382. (setq count (1- count))
  22383. (when (eq l level)
  22384. (setq result (point)))))))
  22385. (goto-char result))
  22386. (beginning-of-line 1)))
  22387. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22388. "Move backward to the ARG'th subheading at same level as this one.
  22389. Stop at the first and last subheadings of a superior heading."
  22390. (interactive "p")
  22391. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22392. (defun org-next-visible-heading (arg)
  22393. "Move to the next visible heading.
  22394. This function wraps `outline-next-visible-heading' with
  22395. `org-with-limited-levels' in order to skip over inline tasks and
  22396. respect customization of `org-odd-levels-only'."
  22397. (interactive "p")
  22398. (org-with-limited-levels
  22399. (outline-next-visible-heading arg)))
  22400. (defun org-previous-visible-heading (arg)
  22401. "Move to the next visible heading.
  22402. This function wraps `outline-previous-visible-heading' with
  22403. `org-with-limited-levels' in order to skip over inline tasks and
  22404. respect customization of `org-odd-levels-only'."
  22405. (interactive "p")
  22406. (org-with-limited-levels
  22407. (outline-previous-visible-heading arg)))
  22408. (defun org-next-block (arg &optional backward block-regexp)
  22409. "Jump to the next block.
  22410. With a prefix argument ARG, jump forward ARG many blocks.
  22411. When BACKWARD is non-nil, jump to the previous block.
  22412. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22413. Match data is set according to this regexp when the function
  22414. returns.
  22415. Return point at beginning of the opening line of found block.
  22416. Throw an error if no block is found."
  22417. (interactive "p")
  22418. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22419. (case-fold-search t)
  22420. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22421. (count (or arg 1))
  22422. (origin (point))
  22423. last-element)
  22424. (if backward (beginning-of-line) (end-of-line))
  22425. (while (and (> count 0) (funcall search-fn re nil t))
  22426. (let ((element (save-excursion
  22427. (goto-char (match-beginning 0))
  22428. (save-match-data (org-element-at-point)))))
  22429. (when (and (memq (org-element-type element)
  22430. '(center-block comment-block dynamic-block
  22431. example-block export-block quote-block
  22432. special-block src-block verse-block))
  22433. (<= (match-beginning 0)
  22434. (org-element-property :post-affiliated element)))
  22435. (setq last-element element)
  22436. (decf count))))
  22437. (if (= count 0)
  22438. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22439. (save-match-data (org-show-context)))
  22440. (goto-char origin)
  22441. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22442. (defun org-previous-block (arg &optional block-regexp)
  22443. "Jump to the previous block.
  22444. With a prefix argument ARG, jump backward ARG many source blocks.
  22445. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22446. (interactive "p")
  22447. (org-next-block arg t block-regexp))
  22448. (defun org-forward-paragraph ()
  22449. "Move forward to beginning of next paragraph or equivalent.
  22450. The function moves point to the beginning of the next visible
  22451. structural element, which can be a paragraph, a table, a list
  22452. item, etc. It also provides some special moves for convenience:
  22453. - On an affiliated keyword, jump to the beginning of the
  22454. relative element.
  22455. - On an item or a footnote definition, move to the second
  22456. element inside, if any.
  22457. - On a table or a property drawer, jump after it.
  22458. - On a verse or source block, stop after blank lines."
  22459. (interactive)
  22460. (when (eobp) (user-error "Cannot move further down"))
  22461. (let* ((deactivate-mark nil)
  22462. (element (org-element-at-point))
  22463. (type (org-element-type element))
  22464. (post-affiliated (org-element-property :post-affiliated element))
  22465. (contents-begin (org-element-property :contents-begin element))
  22466. (contents-end (org-element-property :contents-end element))
  22467. (end (let ((end (org-element-property :end element)) (parent element))
  22468. (while (and (setq parent (org-element-property :parent parent))
  22469. (= (org-element-property :contents-end parent) end))
  22470. (setq end (org-element-property :end parent)))
  22471. end)))
  22472. (cond ((not element)
  22473. (skip-chars-forward " \r\t\n")
  22474. (or (eobp) (beginning-of-line)))
  22475. ;; On affiliated keywords, move to element's beginning.
  22476. ((< (point) post-affiliated)
  22477. (goto-char post-affiliated))
  22478. ;; At a table row, move to the end of the table. Similarly,
  22479. ;; at a node property, move to the end of the property
  22480. ;; drawer.
  22481. ((memq type '(node-property table-row))
  22482. (goto-char (org-element-property
  22483. :end (org-element-property :parent element))))
  22484. ((memq type '(property-drawer table)) (goto-char end))
  22485. ;; Consider blank lines as separators in verse and source
  22486. ;; blocks to ease editing.
  22487. ((memq type '(src-block verse-block))
  22488. (when (eq type 'src-block)
  22489. (setq contents-end
  22490. (save-excursion (goto-char end)
  22491. (skip-chars-backward " \r\t\n")
  22492. (line-beginning-position))))
  22493. (beginning-of-line)
  22494. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22495. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22496. (goto-char end)
  22497. (skip-chars-forward " \r\t\n")
  22498. (if (= (point) contents-end) (goto-char end)
  22499. (beginning-of-line))))
  22500. ;; With no contents, just skip element.
  22501. ((not contents-begin) (goto-char end))
  22502. ;; If contents are invisible, skip the element altogether.
  22503. ((outline-invisible-p (line-end-position))
  22504. (case type
  22505. (headline
  22506. (org-with-limited-levels (outline-next-visible-heading 1)))
  22507. ;; At a plain list, make sure we move to the next item
  22508. ;; instead of skipping the whole list.
  22509. (plain-list (forward-char)
  22510. (org-forward-paragraph))
  22511. (otherwise (goto-char end))))
  22512. ((>= (point) contents-end) (goto-char end))
  22513. ((>= (point) contents-begin)
  22514. ;; This can only happen on paragraphs and plain lists.
  22515. (case type
  22516. (paragraph (goto-char end))
  22517. ;; At a plain list, try to move to second element in
  22518. ;; first item, if possible.
  22519. (plain-list (end-of-line)
  22520. (org-forward-paragraph))))
  22521. ;; When contents start on the middle of a line (e.g. in
  22522. ;; items and footnote definitions), try to reach first
  22523. ;; element starting after current line.
  22524. ((> (line-end-position) contents-begin)
  22525. (end-of-line)
  22526. (org-forward-paragraph))
  22527. (t (goto-char contents-begin)))))
  22528. (defun org-backward-paragraph ()
  22529. "Move backward to start of previous paragraph or equivalent.
  22530. The function moves point to the beginning of the current
  22531. structural element, which can be a paragraph, a table, a list
  22532. item, etc., or to the beginning of the previous visible one if
  22533. point is already there. It also provides some special moves for
  22534. convenience:
  22535. - On an affiliated keyword, jump to the first one.
  22536. - On a table or a property drawer, move to its beginning.
  22537. - On a verse or source block, stop before blank lines."
  22538. (interactive)
  22539. (when (bobp) (user-error "Cannot move further up"))
  22540. (let* ((deactivate-mark nil)
  22541. (element (org-element-at-point))
  22542. (type (org-element-type element))
  22543. (contents-begin (org-element-property :contents-begin element))
  22544. (contents-end (org-element-property :contents-end element))
  22545. (post-affiliated (org-element-property :post-affiliated element))
  22546. (begin (org-element-property :begin element)))
  22547. (cond
  22548. ((not element) (goto-char (point-min)))
  22549. ((= (point) begin)
  22550. (backward-char)
  22551. (org-backward-paragraph))
  22552. ((<= (point) post-affiliated) (goto-char begin))
  22553. ((memq type '(node-property table-row))
  22554. (goto-char (org-element-property
  22555. :post-affiliated (org-element-property :parent element))))
  22556. ((memq type '(property-drawer table)) (goto-char begin))
  22557. ((memq type '(src-block verse-block))
  22558. (when (eq type 'src-block)
  22559. (setq contents-begin
  22560. (save-excursion (goto-char begin) (forward-line) (point))))
  22561. (if (= (point) contents-begin) (goto-char post-affiliated)
  22562. ;; Inside a verse block, see blank lines as paragraph
  22563. ;; separators.
  22564. (let ((origin (point)))
  22565. (skip-chars-backward " \r\t\n" contents-begin)
  22566. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22567. (skip-chars-forward " \r\t\n" origin)
  22568. (if (= (point) origin) (goto-char contents-begin)
  22569. (beginning-of-line))))))
  22570. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22571. ((eq type 'paragraph)
  22572. (goto-char contents-begin)
  22573. ;; When at first paragraph in an item or a footnote definition,
  22574. ;; move directly to beginning of line.
  22575. (let ((parent-contents
  22576. (org-element-property
  22577. :contents-begin (org-element-property :parent element))))
  22578. (when (and parent-contents (= parent-contents contents-begin))
  22579. (beginning-of-line))))
  22580. ;; At the end of a greater element, move to the beginning of the
  22581. ;; last element within.
  22582. ((>= (point) contents-end)
  22583. (goto-char (1- contents-end))
  22584. (org-backward-paragraph))
  22585. (t (goto-char (or post-affiliated begin))))
  22586. ;; Ensure we never leave point invisible.
  22587. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22588. (defun org-forward-element ()
  22589. "Move forward by one element.
  22590. Move to the next element at the same level, when possible."
  22591. (interactive)
  22592. (cond ((eobp) (user-error "Cannot move further down"))
  22593. ((org-with-limited-levels (org-at-heading-p))
  22594. (let ((origin (point)))
  22595. (goto-char (org-end-of-subtree nil t))
  22596. (unless (org-with-limited-levels (org-at-heading-p))
  22597. (goto-char origin)
  22598. (user-error "Cannot move further down"))))
  22599. (t
  22600. (let* ((elem (org-element-at-point))
  22601. (end (org-element-property :end elem))
  22602. (parent (org-element-property :parent elem)))
  22603. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22604. (goto-char (org-element-property :end parent)))
  22605. ((integer-or-marker-p end) (goto-char end))
  22606. (t (message "No element at point")))))))
  22607. (defun org-backward-element ()
  22608. "Move backward by one element.
  22609. Move to the previous element at the same level, when possible."
  22610. (interactive)
  22611. (cond ((bobp) (user-error "Cannot move further up"))
  22612. ((org-with-limited-levels (org-at-heading-p))
  22613. ;; At a headline, move to the previous one, if any, or stay
  22614. ;; here.
  22615. (let ((origin (point)))
  22616. (org-with-limited-levels (org-backward-heading-same-level 1))
  22617. ;; When current headline has no sibling above, move to its
  22618. ;; parent.
  22619. (when (= (point) origin)
  22620. (or (org-with-limited-levels (org-up-heading-safe))
  22621. (progn (goto-char origin)
  22622. (user-error "Cannot move further up"))))))
  22623. (t
  22624. (let* ((elem (org-element-at-point))
  22625. (beg (org-element-property :begin elem)))
  22626. (cond
  22627. ;; Move to beginning of current element if point isn't
  22628. ;; there already.
  22629. ((null beg) (message "No element at point"))
  22630. ((/= (point) beg) (goto-char beg))
  22631. (t (goto-char beg)
  22632. (skip-chars-backward " \r\t\n")
  22633. (unless (bobp)
  22634. (let ((prev (org-element-at-point)))
  22635. (goto-char (org-element-property :begin prev))
  22636. (while (and (setq prev (org-element-property :parent prev))
  22637. (<= (org-element-property :end prev) beg))
  22638. (goto-char (org-element-property :begin prev)))))))))))
  22639. (defun org-up-element ()
  22640. "Move to upper element."
  22641. (interactive)
  22642. (if (org-with-limited-levels (org-at-heading-p))
  22643. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22644. (let* ((elem (org-element-at-point))
  22645. (parent (org-element-property :parent elem)))
  22646. (if parent (goto-char (org-element-property :begin parent))
  22647. (if (org-with-limited-levels (org-before-first-heading-p))
  22648. (user-error "No surrounding element")
  22649. (org-with-limited-levels (org-back-to-heading)))))))
  22650. (defvar org-element-greater-elements)
  22651. (defun org-down-element ()
  22652. "Move to inner element."
  22653. (interactive)
  22654. (let ((element (org-element-at-point)))
  22655. (cond
  22656. ((memq (org-element-type element) '(plain-list table))
  22657. (goto-char (org-element-property :contents-begin element))
  22658. (forward-char))
  22659. ((memq (org-element-type element) org-element-greater-elements)
  22660. ;; If contents are hidden, first disclose them.
  22661. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22662. (goto-char (or (org-element-property :contents-begin element)
  22663. (user-error "No content for this element"))))
  22664. (t (user-error "No inner element")))))
  22665. (defun org-drag-element-backward ()
  22666. "Move backward element at point."
  22667. (interactive)
  22668. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22669. (let* ((elem (org-element-at-point))
  22670. (prev-elem
  22671. (save-excursion
  22672. (goto-char (org-element-property :begin elem))
  22673. (skip-chars-backward " \r\t\n")
  22674. (unless (bobp)
  22675. (let* ((beg (org-element-property :begin elem))
  22676. (prev (org-element-at-point))
  22677. (up prev))
  22678. (while (and (setq up (org-element-property :parent up))
  22679. (<= (org-element-property :end up) beg))
  22680. (setq prev up))
  22681. prev)))))
  22682. ;; Error out if no previous element or previous element is
  22683. ;; a parent of the current one.
  22684. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22685. (user-error "Cannot drag element backward")
  22686. (let ((pos (point)))
  22687. (org-element-swap-A-B prev-elem elem)
  22688. (goto-char (+ (org-element-property :begin prev-elem)
  22689. (- pos (org-element-property :begin elem)))))))))
  22690. (defun org-drag-element-forward ()
  22691. "Move forward element at point."
  22692. (interactive)
  22693. (let* ((pos (point))
  22694. (elem (org-element-at-point)))
  22695. (when (= (point-max) (org-element-property :end elem))
  22696. (user-error "Cannot drag element forward"))
  22697. (goto-char (org-element-property :end elem))
  22698. (let ((next-elem (org-element-at-point)))
  22699. (when (or (org-element-nested-p elem next-elem)
  22700. (and (eq (org-element-type next-elem) 'headline)
  22701. (not (eq (org-element-type elem) 'headline))))
  22702. (goto-char pos)
  22703. (user-error "Cannot drag element forward"))
  22704. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22705. ;; body's length (without final blanks) and by the length of
  22706. ;; blanks between ELEM and NEXT-ELEM.
  22707. (let ((size-next (- (save-excursion
  22708. (goto-char (org-element-property :end next-elem))
  22709. (skip-chars-backward " \r\t\n")
  22710. (forward-line)
  22711. ;; Small correction if buffer doesn't end
  22712. ;; with a newline character.
  22713. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22714. (org-element-property :begin next-elem)))
  22715. (size-blank (- (org-element-property :end elem)
  22716. (save-excursion
  22717. (goto-char (org-element-property :end elem))
  22718. (skip-chars-backward " \r\t\n")
  22719. (forward-line)
  22720. (point)))))
  22721. (org-element-swap-A-B elem next-elem)
  22722. (goto-char (+ pos size-next size-blank))))))
  22723. (defun org-drag-line-forward (arg)
  22724. "Drag the line at point ARG lines forward."
  22725. (interactive "p")
  22726. (dotimes (n (abs arg))
  22727. (let ((c (current-column)))
  22728. (if (< 0 arg)
  22729. (progn
  22730. (beginning-of-line 2)
  22731. (transpose-lines 1)
  22732. (beginning-of-line 0))
  22733. (transpose-lines 1)
  22734. (beginning-of-line -1))
  22735. (org-move-to-column c))))
  22736. (defun org-drag-line-backward (arg)
  22737. "Drag the line at point ARG lines backward."
  22738. (interactive "p")
  22739. (org-drag-line-forward (- arg)))
  22740. (defun org-mark-element ()
  22741. "Put point at beginning of this element, mark at end.
  22742. Interactively, if this command is repeated or (in Transient Mark
  22743. mode) if the mark is active, it marks the next element after the
  22744. ones already marked."
  22745. (interactive)
  22746. (let (deactivate-mark)
  22747. (if (and (org-called-interactively-p 'any)
  22748. (or (and (eq last-command this-command) (mark t))
  22749. (and transient-mark-mode mark-active)))
  22750. (set-mark
  22751. (save-excursion
  22752. (goto-char (mark))
  22753. (goto-char (org-element-property :end (org-element-at-point)))))
  22754. (let ((element (org-element-at-point)))
  22755. (end-of-line)
  22756. (push-mark (org-element-property :end element) t t)
  22757. (goto-char (org-element-property :begin element))))))
  22758. (defun org-narrow-to-element ()
  22759. "Narrow buffer to current element."
  22760. (interactive)
  22761. (let ((elem (org-element-at-point)))
  22762. (cond
  22763. ((eq (car elem) 'headline)
  22764. (narrow-to-region
  22765. (org-element-property :begin elem)
  22766. (org-element-property :end elem)))
  22767. ((memq (car elem) org-element-greater-elements)
  22768. (narrow-to-region
  22769. (org-element-property :contents-begin elem)
  22770. (org-element-property :contents-end elem)))
  22771. (t
  22772. (narrow-to-region
  22773. (org-element-property :begin elem)
  22774. (org-element-property :end elem))))))
  22775. (defun org-transpose-element ()
  22776. "Transpose current and previous elements, keeping blank lines between.
  22777. Point is moved after both elements."
  22778. (interactive)
  22779. (org-skip-whitespace)
  22780. (let ((end (org-element-property :end (org-element-at-point))))
  22781. (org-drag-element-backward)
  22782. (goto-char end)))
  22783. (defun org-unindent-buffer ()
  22784. "Un-indent the visible part of the buffer.
  22785. Relative indentation (between items, inside blocks, etc.) isn't
  22786. modified."
  22787. (interactive)
  22788. (unless (eq major-mode 'org-mode)
  22789. (user-error "Cannot un-indent a buffer not in Org mode"))
  22790. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22791. unindent-tree ; For byte-compiler.
  22792. (unindent-tree
  22793. (function
  22794. (lambda (contents)
  22795. (mapc
  22796. (lambda (element)
  22797. (if (memq (org-element-type element) '(headline section))
  22798. (funcall unindent-tree (org-element-contents element))
  22799. (save-excursion
  22800. (save-restriction
  22801. (narrow-to-region
  22802. (org-element-property :begin element)
  22803. (org-element-property :end element))
  22804. (org-do-remove-indentation)))))
  22805. (reverse contents))))))
  22806. (funcall unindent-tree (org-element-contents parse-tree))))
  22807. (defun org-show-subtree ()
  22808. "Show everything after this heading at deeper levels."
  22809. (interactive)
  22810. (outline-flag-region
  22811. (point)
  22812. (save-excursion
  22813. (org-end-of-subtree t t))
  22814. nil))
  22815. (defun org-show-entry ()
  22816. "Show the body directly following this heading.
  22817. Show the heading too, if it is currently invisible."
  22818. (interactive)
  22819. (save-excursion
  22820. (ignore-errors
  22821. (org-back-to-heading t)
  22822. (outline-flag-region
  22823. (max (point-min) (1- (point)))
  22824. (save-excursion
  22825. (if (re-search-forward
  22826. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22827. (match-beginning 1)
  22828. (point-max)))
  22829. nil)
  22830. (org-cycle-hide-drawers 'children))))
  22831. (defun org-make-options-regexp (kwds &optional extra)
  22832. "Make a regular expression for keyword lines.
  22833. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22834. when non-nil, is a regexp matching keywords names."
  22835. (concat "^[ \t]*#\\+\\("
  22836. (regexp-opt kwds)
  22837. (and extra (concat (and kwds "\\|") extra))
  22838. "\\):[ \t]*\\(.*\\)"))
  22839. ;; Make isearch reveal the necessary context
  22840. (defun org-isearch-end ()
  22841. "Reveal context after isearch exits."
  22842. (when isearch-success ; only if search was successful
  22843. (if (featurep 'xemacs)
  22844. ;; Under XEmacs, the hook is run in the correct place,
  22845. ;; we directly show the context.
  22846. (org-show-context 'isearch)
  22847. ;; In Emacs the hook runs *before* restoring the overlays.
  22848. ;; So we have to use a one-time post-command-hook to do this.
  22849. ;; (Emacs 22 has a special variable, see function `org-mode')
  22850. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22851. isearch-mode-end-hook-quit)
  22852. ;; Only when the isearch was not quitted.
  22853. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22854. 'append 'local)))))
  22855. (defun org-isearch-post-command ()
  22856. "Remove self from hook, and show context."
  22857. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22858. (org-show-context 'isearch))
  22859. ;;;; Integration with and fixes for other packages
  22860. ;;; Imenu support
  22861. (defvar org-imenu-markers nil
  22862. "All markers currently used by Imenu.")
  22863. (make-variable-buffer-local 'org-imenu-markers)
  22864. (defun org-imenu-new-marker (&optional pos)
  22865. "Return a new marker for use by Imenu, and remember the marker."
  22866. (let ((m (make-marker)))
  22867. (move-marker m (or pos (point)))
  22868. (push m org-imenu-markers)
  22869. m))
  22870. (defun org-imenu-get-tree ()
  22871. "Produce the index for Imenu."
  22872. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22873. (setq org-imenu-markers nil)
  22874. (let* ((n org-imenu-depth)
  22875. (re (concat "^" (org-get-limited-outline-regexp)))
  22876. (subs (make-vector (1+ n) nil))
  22877. (last-level 0)
  22878. m level head0 head)
  22879. (save-excursion
  22880. (save-restriction
  22881. (widen)
  22882. (goto-char (point-max))
  22883. (while (re-search-backward re nil t)
  22884. (setq level (org-reduced-level (funcall outline-level)))
  22885. (when (and (<= level n)
  22886. (looking-at org-complex-heading-regexp)
  22887. (setq head0 (org-match-string-no-properties 4)))
  22888. (setq head (org-link-display-format head0)
  22889. m (org-imenu-new-marker))
  22890. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22891. (if (>= level last-level)
  22892. (push (cons head m) (aref subs level))
  22893. (push (cons head (aref subs (1+ level))) (aref subs level))
  22894. (loop for i from (1+ level) to n do (aset subs i nil)))
  22895. (setq last-level level)))))
  22896. (aref subs 1)))
  22897. (eval-after-load "imenu"
  22898. '(progn
  22899. (add-hook 'imenu-after-jump-hook
  22900. (lambda ()
  22901. (if (derived-mode-p 'org-mode)
  22902. (org-show-context 'org-goto))))))
  22903. (defun org-link-display-format (link)
  22904. "Replace a link with its the description.
  22905. If there is no description, use the link target."
  22906. (save-match-data
  22907. (if (string-match org-bracket-link-analytic-regexp link)
  22908. (replace-match (if (match-end 5)
  22909. (match-string 5 link)
  22910. (concat (match-string 1 link)
  22911. (match-string 3 link)))
  22912. nil t link)
  22913. link)))
  22914. (defun org-toggle-link-display ()
  22915. "Toggle the literal or descriptive display of links."
  22916. (interactive)
  22917. (if org-descriptive-links
  22918. (progn (org-remove-from-invisibility-spec '(org-link))
  22919. (org-restart-font-lock)
  22920. (setq org-descriptive-links nil))
  22921. (progn (add-to-invisibility-spec '(org-link))
  22922. (org-restart-font-lock)
  22923. (setq org-descriptive-links t))))
  22924. ;; Speedbar support
  22925. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22926. "Overlay marking the agenda restriction line in speedbar.")
  22927. (overlay-put org-speedbar-restriction-lock-overlay
  22928. 'face 'org-agenda-restriction-lock)
  22929. (overlay-put org-speedbar-restriction-lock-overlay
  22930. 'help-echo "Agendas are currently limited to this item.")
  22931. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22932. (defun org-speedbar-set-agenda-restriction ()
  22933. "Restrict future agenda commands to the location at point in speedbar.
  22934. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22935. (interactive)
  22936. (require 'org-agenda)
  22937. (let (p m tp np dir txt)
  22938. (cond
  22939. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22940. 'org-imenu t))
  22941. (setq m (get-text-property p 'org-imenu-marker))
  22942. (with-current-buffer (marker-buffer m)
  22943. (goto-char m)
  22944. (org-agenda-set-restriction-lock 'subtree)))
  22945. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22946. 'speedbar-function 'speedbar-find-file))
  22947. (setq tp (previous-single-property-change
  22948. (1+ p) 'speedbar-function)
  22949. np (next-single-property-change
  22950. tp 'speedbar-function)
  22951. dir (speedbar-line-directory)
  22952. txt (buffer-substring-no-properties (or tp (point-min))
  22953. (or np (point-max))))
  22954. (with-current-buffer (find-file-noselect
  22955. (let ((default-directory dir))
  22956. (expand-file-name txt)))
  22957. (unless (derived-mode-p 'org-mode)
  22958. (user-error "Cannot restrict to non-Org-mode file"))
  22959. (org-agenda-set-restriction-lock 'file)))
  22960. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22961. (move-overlay org-speedbar-restriction-lock-overlay
  22962. (point-at-bol) (point-at-eol))
  22963. (setq current-prefix-arg nil)
  22964. (org-agenda-maybe-redo)))
  22965. (defvar speedbar-file-key-map)
  22966. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22967. (eval-after-load "speedbar"
  22968. '(progn
  22969. (speedbar-add-supported-extension ".org")
  22970. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22971. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22972. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22973. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22974. (add-hook 'speedbar-visiting-tag-hook
  22975. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22976. ;;; Fixes and Hacks for problems with other packages
  22977. (defun org--flyspell-object-check-p (element)
  22978. "Non-nil when Flyspell can check object at point.
  22979. ELEMENT is the element at point."
  22980. (let ((object (save-excursion
  22981. (when (org-looking-at-p "\\>") (backward-char))
  22982. (org-element-context element))))
  22983. (case (org-element-type object)
  22984. ;; Prevent checks in links due to keybinding conflict with
  22985. ;; Flyspell.
  22986. ((code entity export-snippet inline-babel-call
  22987. inline-src-block line-break latex-fragment link macro
  22988. statistics-cookie target timestamp verbatim)
  22989. nil)
  22990. (footnote-reference
  22991. ;; Only in inline footnotes, within the definition.
  22992. (and (eq (org-element-property :type object) 'inline)
  22993. (< (save-excursion
  22994. (goto-char (org-element-property :begin object))
  22995. (search-forward ":" nil t 2))
  22996. (point))))
  22997. (otherwise t))))
  22998. (defun org-mode-flyspell-verify ()
  22999. "Function used for `flyspell-generic-check-word-predicate'."
  23000. (if (org-at-heading-p)
  23001. ;; At a headline or an inlinetask, check title only. This is
  23002. ;; faster than relying on `org-element-at-point'.
  23003. (and (save-excursion (beginning-of-line)
  23004. (and (let ((case-fold-search t))
  23005. (not (looking-at "\\*+ END[ \t]*$")))
  23006. (looking-at org-complex-heading-regexp)))
  23007. (match-beginning 4)
  23008. (>= (point) (match-beginning 4))
  23009. (or (not (match-beginning 5))
  23010. (< (point) (match-beginning 5))))
  23011. (let* ((element (org-element-at-point))
  23012. (post-affiliated (org-element-property :post-affiliated element)))
  23013. (cond
  23014. ;; Ignore checks in all affiliated keywords but captions.
  23015. ((< (point) post-affiliated)
  23016. (and (save-excursion
  23017. (beginning-of-line)
  23018. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23019. (> (point) (match-end 0))
  23020. (org--flyspell-object-check-p element)))
  23021. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23022. ((let ((log (org-log-into-drawer)))
  23023. (and log
  23024. (let ((drawer (org-element-lineage element '(drawer))))
  23025. (and drawer
  23026. (eq (compare-strings
  23027. log nil nil
  23028. (org-element-property :drawer-name drawer) nil nil t)
  23029. t)))))
  23030. nil)
  23031. (t
  23032. (case (org-element-type element)
  23033. ((comment quote-section) t)
  23034. (comment-block
  23035. ;; Allow checks between block markers, not on them.
  23036. (and (> (line-beginning-position) post-affiliated)
  23037. (save-excursion
  23038. (end-of-line)
  23039. (skip-chars-forward " \r\t\n")
  23040. (< (point) (org-element-property :end element)))))
  23041. ;; Arbitrary list of keywords where checks are meaningful.
  23042. ;; Make sure point is on the value part of the element.
  23043. (keyword
  23044. (and (member (org-element-property :key element)
  23045. '("DESCRIPTION" "TITLE"))
  23046. (save-excursion
  23047. (search-backward ":" (line-beginning-position) t))))
  23048. ;; Check is globally allowed in paragraphs verse blocks and
  23049. ;; table rows (after affiliated keywords) but some objects
  23050. ;; must not be affected.
  23051. ((paragraph table-row verse-block)
  23052. (let ((cbeg (org-element-property :contents-begin element))
  23053. (cend (org-element-property :contents-end element)))
  23054. (and cbeg (>= (point) cbeg) (< (point) cend)
  23055. (org--flyspell-object-check-p element))))))))))
  23056. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23057. (defun org-remove-flyspell-overlays-in (beg end)
  23058. "Remove flyspell overlays in region."
  23059. (and (org-bound-and-true-p flyspell-mode)
  23060. (fboundp 'flyspell-delete-region-overlays)
  23061. (flyspell-delete-region-overlays beg end)))
  23062. (defvar flyspell-delayed-commands)
  23063. (eval-after-load "flyspell"
  23064. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23065. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23066. (eval-after-load "bookmark"
  23067. '(if (boundp 'bookmark-after-jump-hook)
  23068. ;; We can use the hook
  23069. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23070. ;; Hook not available, use advice
  23071. (defadvice bookmark-jump (after org-make-visible activate)
  23072. "Make the position visible."
  23073. (org-bookmark-jump-unhide))))
  23074. ;; Make sure saveplace shows the location if it was hidden
  23075. (eval-after-load "saveplace"
  23076. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23077. "Make the position visible."
  23078. (org-bookmark-jump-unhide)))
  23079. ;; Make sure ecb shows the location if it was hidden
  23080. (eval-after-load "ecb"
  23081. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23082. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23083. (if (derived-mode-p 'org-mode)
  23084. (org-show-context))))
  23085. (defun org-bookmark-jump-unhide ()
  23086. "Unhide the current position, to show the bookmark location."
  23087. (and (derived-mode-p 'org-mode)
  23088. (or (outline-invisible-p)
  23089. (save-excursion (goto-char (max (point-min) (1- (point))))
  23090. (outline-invisible-p)))
  23091. (org-show-context 'bookmark-jump)))
  23092. (defun org-mark-jump-unhide ()
  23093. "Make the point visible with `org-show-context' after jumping to the mark."
  23094. (when (and (derived-mode-p 'org-mode)
  23095. (outline-invisible-p))
  23096. (org-show-context 'mark-goto)))
  23097. (eval-after-load "simple"
  23098. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23099. "Make the point visible with `org-show-context'."
  23100. (org-mark-jump-unhide)))
  23101. (eval-after-load "simple"
  23102. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23103. "Make the point visible with `org-show-context'."
  23104. (org-mark-jump-unhide)))
  23105. (eval-after-load "simple"
  23106. '(defadvice pop-global-mark (after org-make-visible activate)
  23107. "Make the point visible with `org-show-context'."
  23108. (org-mark-jump-unhide)))
  23109. ;; Make session.el ignore our circular variable
  23110. (defvar session-globals-exclude)
  23111. (eval-after-load "session"
  23112. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23113. ;;;; Finish up
  23114. (provide 'org)
  23115. (run-hooks 'org-load-hook)
  23116. ;;; org.el ends here